Skip to content

Commit

Permalink
fix(cosmosaccount): return errors from sdk crypto functions (ignite…
Browse files Browse the repository at this point in the history
…#2760)

* return errors

* make corresponding changes

* format

* fix doc

* fix merge conflicts

* actually fix merge

* revert

* format:

* add changelog

Co-authored-by: Jerónimo Albi <[email protected]>
Co-authored-by: İlker G. Öztürk <[email protected]>
  • Loading branch information
3 people authored Aug 25, 2022
1 parent ccfca78 commit b762c18
Show file tree
Hide file tree
Showing 32 changed files with 384 additions and 186 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- Upgraded Cosmos SDK to v0.46.0 and IBC to v5 in CLI and scaffolding templates
- Removed `handler.go` from scaffolded module template

### Fixes
- Improved error handling for crypto wrapper functions

### Features

- Add `--skip-proto` flag to `build`, `init` and `serve` commands to build the chain without building proto files
Expand Down
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
3 changes: 2 additions & 1 deletion ignite/cmd/node.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ignitecmd

import (
"github.com/spf13/cobra"

"github.com/ignite/cli/ignite/pkg/cosmosclient"
"github.com/ignite/cli/ignite/pkg/xurl"
"github.com/spf13/cobra"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/node_query_bank_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package ignitecmd
import (
"fmt"

"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/spf13/cobra"

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

func NewNodeQueryBankBalances() *cobra.Command {
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/node_tx_bank_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package ignitecmd
import (
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/spf13/cobra"

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

func NewNodeTxBankSend() *cobra.Command {
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
1 change: 1 addition & 0 deletions ignite/pkg/chaincmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/flags"

"github.com/ignite/cli/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/ignite/pkg/cosmosver"
)
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
8 changes: 7 additions & 1 deletion ignite/pkg/cosmosclient/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

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

Expand All @@ -25,8 +26,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

0 comments on commit b762c18

Please sign in to comment.