Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cosmosaccount): return errors from sdk crypto functions #2760

Merged
merged 16 commits into from
Aug 25, 2022
7 changes: 6 additions & 1 deletion docs/docs/guide/03-blog/02-connect-blockchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,14 @@ func main() {
log.Fatal(err)
}

addr, err := account.Address(addressPrefix)
if err != nil {
log.Fatal(err)
}

// Define a message to create a post
msg := &types.MsgCreatePost{
Creator: account.Address(addressPrefix),
Creator: addr,
Title: "Hello!",
Body: "This is the first post",
}
Expand Down
12 changes: 11 additions & 1 deletion ignite/cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ Ignite CLI uses accounts to interact with the Ignite blockchain, use an IBC rela
func printAccounts(cmd *cobra.Command, accounts ...cosmosaccount.Account) error {
var accEntries [][]string
for _, acc := range accounts {
accEntries = append(accEntries, []string{acc.Name, acc.Address(getAddressPrefix(cmd)), acc.PubKey()})
addr, err := acc.Address(getAddressPrefix(cmd))
if err != nil {
return err
}

pubKey, err := acc.PubKey()
if err != nil {
return err
}

accEntries = append(accEntries, []string{acc.Name, addr, pubKey})
}
return entrywriter.MustWrite(os.Stdout, []string{"name", "address", "public key"}, accEntries...)
}
Expand Down
4 changes: 1 addition & 3 deletions ignite/cmd/node_query_bank_balances.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ignitecmd

import (
"fmt"

"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -53,7 +51,7 @@ func nodeQueryBankBalancesHandler(cmd *cobra.Command, args []string) error {

var rows [][]string
for _, b := range balances {
rows = append(rows, []string{fmt.Sprintf("%s", b.Amount), b.Denom})
rows = append(rows, []string{b.String()})
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
}

session.StopSpinner()
Expand Down
5 changes: 4 additions & 1 deletion ignite/cmd/relayer_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,10 @@ func initChain(
return nil, errors.Wrapf(err, "cannot resolve %s", name)
}

accountAddr := account.Address(addressPrefix)
accountAddr, err := account.Address(addressPrefix)
if err != nil {
return nil, err
}

session.StopSpinner()
session.Printf("🔐 Account on %q is %s(%s)\n \n", name, accountName, accountAddr)
Expand Down
18 changes: 8 additions & 10 deletions ignite/pkg/cosmosaccount/cosmosaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,37 +135,35 @@ type Account struct {
}

// Address returns the address of the account from given prefix.
func (a Account) Address(accPrefix string) string {
func (a Account) Address(accPrefix string) (string, error) {
if accPrefix == "" {
accPrefix = AccountPrefixCosmos
}

// TODO: handle error
pk, err := a.Record.GetPubKey()
if err != nil {
panic(err)
return "", err
}

return toBech32(accPrefix, pk.Address())
}

// PubKey returns a public key for account.
func (a Account) PubKey() string {
// TODO: handle error
func (a Account) PubKey() (string, error) {
pk, err := a.Record.GetPubKey()
if err != nil {
panic(err)
return "", nil
}

return pk.String()
return pk.String(), nil
}

func toBech32(prefix string, addr []byte) string {
func toBech32(prefix string, addr []byte) (string, error) {
bech32Addr, err := bech32.ConvertAndEncode(prefix, addr)
if err != nil {
panic(err)
return "", err
}
return bech32Addr
return bech32Addr, nil
}

// EnsureDefaultAccount ensures that default account exists.
Expand Down
3 changes: 2 additions & 1 deletion ignite/pkg/cosmosaccount/cosmosaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func TestRegistry(t *testing.T) {
require.Equal(t, getAccount.Name, account.Name)
require.Equal(t, getAccount.Name, account.Record.Name)

addr = account.Address("cosmos")
addr, err = account.Address("cosmos")
require.NoError(t, err)
getAccount, err = registry.GetByAddress(addr)
require.NoError(t, err)
require.Equal(t, getAccount.Record.PubKey, account.Record.PubKey)
Expand Down
7 changes: 6 additions & 1 deletion ignite/pkg/cosmosclient/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ func (c Client) BankBalances(ctx context.Context, address string, pagination *qu
}

func (c Client) BankSendTx(fromAccount cosmosaccount.Account, toAddress string, amount sdk.Coins) (TxService, error) {
addr, err := fromAccount.Address(c.addressPrefix)
if err != nil {
return TxService{}, err
}

msg := &banktypes.MsgSend{
FromAddress: fromAccount.Address(c.addressPrefix),
FromAddress: addr,
ToAddress: toAddress,
Amount: amount,
}
Expand Down
6 changes: 5 additions & 1 deletion ignite/pkg/cosmosclient/cosmosclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,11 @@ func (c *Client) prepareBroadcast(ctx context.Context, accountName string, _ []s

// make sure that account has enough balances before broadcasting.
if c.useFaucet {
if err := c.makeSureAccountHasTokens(ctx, account.Address(c.addressPrefix)); err != nil {
addr, err := account.Address(c.addressPrefix)
if err != nil {
return err
}
if err := c.makeSureAccountHasTokens(ctx, addr); err != nil {
return err
}
}
Expand Down
5 changes: 4 additions & 1 deletion ignite/pkg/relayer/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ func (c *Chain) TryRetrieve(ctx context.Context) (sdk.Coins, error) {
return nil, err
}

addr := acc.Address(c.addressPrefix)
addr, err := acc.Address(c.addressPrefix)
if err != nil {
return nil, err
}

if err = cosmosfaucet.TryRetrieve(ctx, c.ID, c.rpcAddress, c.faucetAddress, addr); err != nil {
return nil, err
Expand Down
12 changes: 10 additions & 2 deletions ignite/pkg/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ func (r Relayer) prepare(ctx context.Context, conf relayerconf.Config, chainID s
return relayerconf.Chain{}, "", err
}

addr, err := account.Address(chain.AddressPrefix)
if err != nil {
return relayerconf.Chain{}, "", err
}

errMissingBalance := fmt.Errorf(`account "%s(%s)" on %q chain does not have enough balances`,
account.Address(chain.AddressPrefix),
addr,
chain.Account,
chain.ID,
)
Expand Down Expand Up @@ -235,7 +240,10 @@ func (r Relayer) balance(ctx context.Context, rpcAddress, account, addressPrefix
return nil, err
}

addr := acc.Address(addressPrefix)
addr, err := acc.Address(addressPrefix)
if err != nil {
return nil, err
}

queryClient := banktypes.NewQueryClient(client.Context())
res, err := queryClient.AllBalances(ctx, &banktypes.QueryAllBalancesRequest{Address: addr})
Expand Down
3 changes: 2 additions & 1 deletion ignite/pkg/xos/mv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"path"
"testing"

"github.com/ignite/cli/ignite/pkg/xos"
"github.com/stretchr/testify/require"

"github.com/ignite/cli/ignite/pkg/xos"
)

func TestRename(t *testing.T) {
Expand Down
19 changes: 15 additions & 4 deletions ignite/services/network/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ func (n Network) Campaigns(ctx context.Context) ([]networktypes.Campaign, error)
// CreateCampaign creates a campaign in Network
func (n Network) CreateCampaign(name, metadata string, totalSupply sdk.Coins) (uint64, error) {
n.ev.Send(events.New(events.StatusOngoing, fmt.Sprintf("Creating campaign %s", name)))

addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return 0, err
}
msgCreateCampaign := campaigntypes.NewMsgCreateCampaign(
n.account.Address(networktypes.SPN),
addr,
name,
totalSupply,
[]byte(metadata),
Expand All @@ -109,8 +112,13 @@ func (n Network) InitializeMainnet(
mainnetChainID string,
) (uint64, error) {
n.ev.Send(events.New(events.StatusOngoing, "Initializing the mainnet campaign"))
addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return 0, err
}

msg := campaigntypes.NewMsgInitializeMainnet(
n.account.Address(networktypes.SPN),
addr,
campaignID,
sourceURL,
sourceHash,
Expand Down Expand Up @@ -144,7 +152,10 @@ func (n Network) UpdateCampaign(
}

n.ev.Send(events.New(events.StatusOngoing, fmt.Sprintf("Updating the campaign %d", id)))
account := n.account.Address(networktypes.SPN)
account, err := n.account.Address(networktypes.SPN)
if err != nil {
return err
}
msgs := make([]sdk.Msg, 0)
if p.name != "" || len(p.metadata) > 0 {
msgs = append(msgs, campaigntypes.NewMsgEditCampaign(
Expand Down
7 changes: 6 additions & 1 deletion ignite/services/network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ func (n Network) CreateClient(
unbondingTime int64,
rewardsInfo networktypes.Reward,
) (string, error) {
addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return "", err
}

msgCreateClient := monitoringctypes.NewMsgCreateClient(
n.account.Address(networktypes.SPN),
addr,
launchID,
rewardsInfo.ConsensusState,
rewardsInfo.ValidatorSet,
Expand Down
7 changes: 6 additions & 1 deletion ignite/services/network/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ func (n Network) sendValidatorRequest(
gentx []byte,
gentxInfo cosmosutil.GentxInfo,
) error {
addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return err
}

msg := launchtypes.NewMsgRequestAddValidator(
n.account.Address(networktypes.SPN),
addr,
launchID,
valAddress,
gentx,
Expand Down
Loading