Skip to content

Commit a1f12a7

Browse files
feat: wire custom coin type and get bech32 prefix (backport #4569) (#4572)
* feat: wire custom coin type and get bech32 prefix (#4569) (cherry picked from commit 2b45eaa) # Conflicts: # go.mod # ignite/cmd/scaffold_chain.go # ignite/cmd/testnet_inplace.go # ignite/pkg/cosmosaccount/cosmosaccount.go # ignite/services/chain/chain.go # ignite/services/scaffolder/init.go # ignite/templates/app/app.go # integration/app/cmd_app_test.go * conflicts * updates * consumer --------- Co-authored-by: julienrbrt <[email protected]>
1 parent 22ba6b2 commit a1f12a7

26 files changed

+419
-43
lines changed

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
## Unreleased
44

5+
## [`v28.8.2`](https://github.com/ignite/cli/releases/tag/v28.8.2)
6+
57
### Changes
68

79
- [#4568](https://github.com/ignite/cli/pull/4568) Bump Cosmos SDK to v0.50.13.
10+
- [#4569](https://github.com/ignite/cli/pull/4569) Add flags to set coin type on commands. Add getters for bech32 prefix and coin type.
811

912
## [`v28.8.1`](https://github.com/ignite/cli/releases/tag/v28.8.1)
1013

go.mod

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ replace (
1313
)
1414

1515
require (
16-
cosmossdk.io/math v1.4.0
16+
cosmossdk.io/core v0.11.2
17+
cosmossdk.io/math v1.5.0
1718
github.com/99designs/keyring v1.2.2
1819
github.com/AlecAivazis/survey/v2 v2.3.7
1920
github.com/DATA-DOG/go-sqlmock v1.5.2
@@ -97,13 +98,12 @@ require (
9798
connectrpc.com/otelconnect v0.7.0 // indirect
9899
cosmossdk.io/api v0.7.6 // indirect
99100
cosmossdk.io/collections v0.4.0 // indirect
100-
cosmossdk.io/core v0.11.1 // indirect
101101
cosmossdk.io/depinject v1.1.0 // indirect
102102
cosmossdk.io/errors v1.0.1 // indirect
103103
cosmossdk.io/log v1.4.1 // indirect
104104
cosmossdk.io/store v1.1.1 // indirect
105105
cosmossdk.io/x/tx v0.13.7 // indirect
106-
dario.cat/mergo v1.0.0 // indirect
106+
dario.cat/mergo v1.0.1 // indirect
107107
filippo.io/edwards25519 v1.0.0 // indirect
108108
github.com/4meepo/tagalign v1.3.4 // indirect
109109
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
@@ -163,6 +163,7 @@ require (
163163
github.com/cilium/ebpf v0.11.0 // indirect
164164
github.com/ckaznocha/intrange v0.1.2 // indirect
165165
github.com/cloudflare/circl v1.3.7 // indirect
166+
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
166167
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect
167168
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
168169
github.com/cockroachdb/pebble v1.1.2 // indirect

go.sum

+8-6
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ cosmossdk.io/api v0.7.6 h1:PC20PcXy1xYKH2KU4RMurVoFjjKkCgYRbVAD4PdqUuY=
2828
cosmossdk.io/api v0.7.6/go.mod h1:IcxpYS5fMemZGqyYtErK7OqvdM0C8kdW3dq8Q/XIG38=
2929
cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s=
3030
cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0=
31-
cosmossdk.io/core v0.11.1 h1:h9WfBey7NAiFfIcUhDVNS503I2P2HdZLebJlUIs8LPA=
32-
cosmossdk.io/core v0.11.1/go.mod h1:OJzxcdC+RPrgGF8NJZR2uoQr56tc7gfBKhiKeDO7hH0=
31+
cosmossdk.io/core v0.11.2 h1:20PXbQxhWRKA83pSYW76OXrc1MI2E93flbMAGSVFlyc=
32+
cosmossdk.io/core v0.11.2/go.mod h1:q137AJUo+/BFZ0hTTgx+7enPAar5c3Nr0h042BgcZMY=
3333
cosmossdk.io/depinject v1.1.0 h1:wLan7LG35VM7Yo6ov0jId3RHWCGRhe8E8bsuARorl5E=
3434
cosmossdk.io/depinject v1.1.0/go.mod h1:kkI5H9jCGHeKeYWXTqYdruogYrEeWvBQCw1Pj4/eCFI=
3535
cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0=
3636
cosmossdk.io/errors v1.0.1/go.mod h1:MeelVSZThMi4bEakzhhhE/CKqVv3nOJDA25bIqRDu/U=
3737
cosmossdk.io/log v1.4.1 h1:wKdjfDRbDyZRuWa8M+9nuvpVYxrEOwbD/CA8hvhU8QM=
3838
cosmossdk.io/log v1.4.1/go.mod h1:k08v0Pyq+gCP6phvdI6RCGhLf/r425UT6Rk/m+o74rU=
39-
cosmossdk.io/math v1.4.0 h1:XbgExXFnXmF/CccPPEto40gOO7FpWu9yWNAZPN3nkNQ=
40-
cosmossdk.io/math v1.4.0/go.mod h1:O5PkD4apz2jZs4zqFdTr16e1dcaQCc5z6lkEnrrppuk=
39+
cosmossdk.io/math v1.5.0 h1:sbOASxee9Zxdjd6OkzogvBZ25/hP929vdcYcBJQbkLc=
40+
cosmossdk.io/math v1.5.0/go.mod h1:AAwwBmUhqtk2nlku174JwSll+/DepUXW3rWIXN5q+Nw=
4141
cosmossdk.io/store v1.1.1 h1:NA3PioJtWDVU7cHHeyvdva5J/ggyLDkyH0hGHl2804Y=
4242
cosmossdk.io/store v1.1.1/go.mod h1:8DwVTz83/2PSI366FERGbWSH7hL6sB7HbYp8bqksNwM=
4343
cosmossdk.io/x/tx v0.13.7 h1:8WSk6B/OHJLYjiZeMKhq7DK7lHDMyK0UfDbBMxVmeOI=
4444
cosmossdk.io/x/tx v0.13.7/go.mod h1:V6DImnwJMTq5qFjeGWpXNiT/fjgE4HtmclRmTqRVM3w=
45-
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
46-
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
45+
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
46+
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
4747
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
4848
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
4949
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
@@ -267,6 +267,8 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
267267
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
268268
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
269269
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
270+
github.com/cockroachdb/apd/v3 v3.2.1 h1:U+8j7t0axsIgvQUqthuNm82HIrYXodOV2iWLWtEaIwg=
271+
github.com/cockroachdb/apd/v3 v3.2.1/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
270272
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
271273
github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f h1:otljaYPt5hWxV3MUfO5dFPFiOXg9CyG5/kCfayTqsJ4=
272274
github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=

ignite/cmd/account.go

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
const (
1515
flagAddressPrefix = "address-prefix"
16+
flagCoinType = "coin-type"
1617
flagPassphrase = "passphrase"
1718
flagNonInteractive = "non-interactive"
1819
flagKeyringBackend = "keyring-backend"
@@ -102,6 +103,17 @@ func getAddressPrefix(cmd *cobra.Command) string {
102103
return prefix
103104
}
104105

106+
func flagSetCoinType() *flag.FlagSet {
107+
fs := flag.NewFlagSet("", flag.ContinueOnError)
108+
fs.Uint32(flagCoinType, cosmosaccount.CoinTypeCosmos, "coin type to use for the account")
109+
return fs
110+
}
111+
112+
func getCoinType(cmd *cobra.Command) uint32 {
113+
coinType, _ := cmd.Flags().GetUint32(flagCoinType)
114+
return coinType
115+
}
116+
105117
func flagSetAccountImport() *flag.FlagSet {
106118
fs := flag.NewFlagSet("", flag.ContinueOnError)
107119
fs.Bool(flagNonInteractive, false, "do not enter into interactive mode")

ignite/cmd/account_create.go

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func NewAccountCreate() *cobra.Command {
1515
RunE: accountCreateHandler,
1616
}
1717

18+
c.Flags().AddFlagSet(flagSetCoinType())
19+
1820
return c
1921
}
2022

@@ -24,6 +26,7 @@ func accountCreateHandler(cmd *cobra.Command, args []string) error {
2426
ca, err := cosmosaccount.New(
2527
cosmosaccount.WithKeyringBackend(getKeyringBackend(cmd)),
2628
cosmosaccount.WithHome(getKeyringDir(cmd)),
29+
cosmosaccount.WithCoinType(getCoinType(cmd)),
2730
)
2831
if err != nil {
2932
return errors.Errorf("unable to create registry: %w", err)

ignite/cmd/account_import.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewAccountImport() *cobra.Command {
2323

2424
c.Flags().String(flagSecret, "", "Your mnemonic or path to your private key (use interactive mode instead to securely pass your mnemonic)")
2525
c.Flags().AddFlagSet(flagSetAccountImport())
26+
c.Flags().AddFlagSet(flagSetCoinType())
2627

2728
return c
2829
}
@@ -61,6 +62,7 @@ func accountImportHandler(cmd *cobra.Command, args []string) error {
6162
ca, err := cosmosaccount.New(
6263
cosmosaccount.WithKeyringBackend(getKeyringBackend(cmd)),
6364
cosmosaccount.WithHome(getKeyringDir(cmd)),
65+
cosmosaccount.WithCoinType(getCoinType(cmd)),
6466
)
6567
if err != nil {
6668
return err

ignite/cmd/account_list.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func accountListHandler(cmd *cobra.Command, _ []string) error {
2222
ca, err := cosmosaccount.New(
2323
cosmosaccount.WithKeyringBackend(getKeyringBackend(cmd)),
2424
cosmosaccount.WithHome(getKeyringDir(cmd)),
25+
cosmosaccount.WithBech32Prefix(getAddressPrefix(cmd)),
2526
)
2627
if err != nil {
2728
return err

ignite/cmd/account_show.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func accountShowHandler(cmd *cobra.Command, args []string) error {
2525
ca, err := cosmosaccount.New(
2626
cosmosaccount.WithKeyringBackend(getKeyringBackend(cmd)),
2727
cosmosaccount.WithHome(getKeyringDir(cmd)),
28+
cosmosaccount.WithBech32Prefix(getAddressPrefix(cmd)),
2829
)
2930
if err != nil {
3031
return err

ignite/cmd/scaffold_chain.go

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ about Cosmos SDK on https://docs.cosmos.network
7979

8080
flagSetClearCache(c)
8181
c.Flags().AddFlagSet(flagSetAccountPrefixes())
82+
c.Flags().AddFlagSet(flagSetCoinType())
8283
c.Flags().StringP(flagPath, "p", "", "create a project in a specific path")
8384
c.Flags().Bool(flagNoDefaultModule, false, "create a project without a default module")
8485
c.Flags().StringSlice(flagParams, []string{}, "add default module parameters")
@@ -99,6 +100,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
99100
var (
100101
name = args[0]
101102
addressPrefix = getAddressPrefix(cmd)
103+
coinType = getCoinType(cmd)
102104
appPath = flagGetPath(cmd)
103105
noDefaultModule, _ = cmd.Flags().GetBool(flagNoDefaultModule)
104106
skipGit, _ = cmd.Flags().GetBool(flagSkipGit)
@@ -124,6 +126,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
124126
appPath,
125127
name,
126128
addressPrefix,
129+
coinType,
127130
noDefaultModule,
128131
skipGit,
129132
skipProto,

ignite/cmd/scaffold_chain_registry.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func scaffoldChainRegistryFiles(cmd *cobra.Command, _ []string) error {
5555
return err
5656
}
5757

58-
if err = sc.AddChainRegistryFiles(c, cfg); err != nil {
58+
if err = sc.CreateChainRegistryFiles(c, cfg); err != nil {
5959
return err
6060
}
6161

ignite/cmd/testnet_inplace.go

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ We can create a testnet from the local network state and mint additional coins f
2929
c.Flags().AddFlagSet(flagSetCheckDependencies())
3030
c.Flags().AddFlagSet(flagSetSkipProto())
3131
c.Flags().AddFlagSet(flagSetVerbose())
32+
c.Flags().AddFlagSet(flagSetAccountPrefixes())
33+
c.Flags().AddFlagSet(flagSetCoinType())
3234

3335
c.Flags().Bool(flagQuitOnFail, false, "quit program if the app fails to start")
3436
return c
@@ -70,17 +72,25 @@ func testnetInplace(cmd *cobra.Command, session *cliui.Session) error {
7072
if err != nil {
7173
return err
7274
}
75+
7376
home, err := c.Home()
7477
if err != nil {
7578
return err
7679
}
80+
7781
keyringBackend, err := c.KeyringBackend()
7882
if err != nil {
7983
return err
8084
}
85+
86+
prefix := getAddressPrefix(cmd)
87+
coinType := getCoinType(cmd)
88+
8189
ca, err := cosmosaccount.New(
8290
cosmosaccount.WithKeyringBackend(cosmosaccount.KeyringBackend(keyringBackend)),
8391
cosmosaccount.WithHome(home),
92+
cosmosaccount.WithBech32Prefix(prefix),
93+
cosmosaccount.WithCoinType(coinType),
8494
)
8595
if err != nil {
8696
return err
@@ -91,6 +101,7 @@ func testnetInplace(cmd *cobra.Command, session *cliui.Session) error {
91101
accounts string
92102
accErr *cosmosaccount.AccountDoesNotExistError
93103
)
104+
94105
for _, acc := range cfg.Accounts {
95106
sdkAcc, err := ca.GetByName(acc.Name)
96107
if errors.As(err, &accErr) {
@@ -127,5 +138,6 @@ func testnetInplace(cmd *cobra.Command, session *cliui.Session) error {
127138
NewOperatorAddress: operatorAddress.String(),
128139
AccountsToFund: accounts,
129140
}
141+
130142
return c.TestnetInPlace(cmd.Context(), args)
131143
}

ignite/pkg/chainregistry/chain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Chain struct {
1919
DaemonName string `json:"daemon_name"`
2020
NodeHome string `json:"node_home"`
2121
KeyAlgos []KeyAlgos `json:"key_algos"`
22-
Slip44 int `json:"slip44"`
22+
Slip44 uint32 `json:"slip44"`
2323
Fees Fees `json:"fees"`
2424
Staking Staking `json:"staking"`
2525
Codebase Codebase `json:"codebase"`

ignite/pkg/cosmosaccount/cosmosaccount.go

+31-14
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import (
66
"fmt"
77
"os"
88

9+
addresscodec "cosmossdk.io/core/address"
910
dkeyring "github.com/99designs/keyring"
11+
1012
"github.com/cosmos/cosmos-sdk/codec"
13+
"github.com/cosmos/cosmos-sdk/codec/address"
1114
"github.com/cosmos/cosmos-sdk/codec/types"
1215
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
1316
"github.com/cosmos/cosmos-sdk/crypto/hd"
1417
"github.com/cosmos/cosmos-sdk/crypto/keyring"
1518
sdktypes "github.com/cosmos/cosmos-sdk/types"
16-
"github.com/cosmos/cosmos-sdk/types/bech32"
1719
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
1820
"github.com/cosmos/go-bip39"
1921

@@ -22,7 +24,7 @@ import (
2224

2325
const (
2426
// KeyringServiceName used for the name of keyring in OS backend.
25-
KeyringServiceName = "starport"
27+
KeyringServiceName = "ignite"
2628

2729
// DefaultAccount is the name of the default account.
2830
DefaultAccount = "default"
@@ -34,6 +36,7 @@ var KeyringHome = os.ExpandEnv("$HOME/.ignite/accounts")
3436
var ErrAccountExists = errors.New("account already exists")
3537

3638
const (
39+
CoinTypeCosmos = sdktypes.CoinType
3740
AccountPrefixCosmos = "cosmos"
3841
)
3942

@@ -58,6 +61,8 @@ type Registry struct {
5861
homePath string
5962
keyringServiceName string
6063
keyringBackend KeyringBackend
64+
addressCodec addresscodec.Codec
65+
coinType uint32
6166

6267
Keyring keyring.Keyring
6368
}
@@ -83,12 +88,26 @@ func WithKeyringBackend(backend KeyringBackend) Option {
8388
}
8489
}
8590

91+
func WithBech32Prefix(prefix string) Option {
92+
return func(c *Registry) {
93+
c.addressCodec = address.NewBech32Codec(prefix)
94+
}
95+
}
96+
97+
func WithCoinType(coinType uint32) Option {
98+
return func(c *Registry) {
99+
c.coinType = coinType
100+
}
101+
}
102+
86103
// New creates a new registry to manage accounts.
87104
func New(options ...Option) (Registry, error) {
88105
r := Registry{
89106
keyringServiceName: sdktypes.KeyringServiceName(),
90107
keyringBackend: KeyringTest,
91108
homePath: KeyringHome,
109+
addressCodec: address.NewBech32Codec(AccountPrefixCosmos),
110+
coinType: CoinTypeCosmos,
92111
}
93112

94113
for _, apply := range options {
@@ -145,7 +164,13 @@ func (a Account) Address(accPrefix string) (string, error) {
145164
return "", err
146165
}
147166

148-
return toBech32(accPrefix, pk.Address())
167+
addressCodec := address.NewBech32Codec(accPrefix)
168+
addr, err := addressCodec.BytesToString(pk.Address())
169+
if err != nil {
170+
return "", err
171+
}
172+
173+
return addr, nil
149174
}
150175

151176
// PubKey returns a public key for account.
@@ -158,14 +183,6 @@ func (a Account) PubKey() (string, error) {
158183
return pk.String(), nil
159184
}
160185

161-
func toBech32(prefix string, addr []byte) (string, error) {
162-
bech32Addr, err := bech32.ConvertAndEncode(prefix, addr)
163-
if err != nil {
164-
return "", err
165-
}
166-
return bech32Addr, nil
167-
}
168-
169186
// EnsureDefaultAccount ensures that default account exists.
170187
func (r Registry) EnsureDefaultAccount() error {
171188
_, err := r.GetByName(DefaultAccount)
@@ -288,11 +305,11 @@ func (r Registry) GetByName(name string) (Account, error) {
288305

289306
// GetByAddress returns an account by its address.
290307
func (r Registry) GetByAddress(address string) (Account, error) {
291-
sdkAddr, err := sdktypes.AccAddressFromBech32(address)
308+
sdkAddr, err := r.addressCodec.StringToBytes(address)
292309
if err != nil {
293310
return Account{}, err
294311
}
295-
record, err := r.Keyring.KeyByAddress(sdkAddr)
312+
record, err := r.Keyring.KeyByAddress(sdktypes.AccAddress(sdkAddr))
296313
if errors.Is(err, dkeyring.ErrKeyNotFound) || errors.Is(err, sdkerrors.ErrKeyNotFound) {
297314
return Account{}, &AccountDoesNotExistError{address}
298315
}
@@ -334,7 +351,7 @@ func (r Registry) DeleteByName(name string) error {
334351
}
335352

336353
func (r Registry) hdPath() string {
337-
return hd.CreateHDPath(sdktypes.GetConfig().GetCoinType(), 0, 0).String()
354+
return hd.CreateHDPath(r.coinType, 0, 0).String()
338355
}
339356

340357
func (r Registry) algo() (keyring.SignatureAlgo, error) {

ignite/pkg/cosmosclient/bank.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c Client) BankBalances(ctx context.Context, address string, pagination *qu
2626
}
2727

2828
func (c Client) BankSendTx(ctx context.Context, fromAccount cosmosaccount.Account, toAddress string, amount sdk.Coins) (TxService, error) {
29-
addr, err := fromAccount.Address(c.addressPrefix)
29+
addr, err := fromAccount.Address(c.bech32Prefix)
3030
if err != nil {
3131
return TxService{}, err
3232
}

0 commit comments

Comments
 (0)