Skip to content

Commit

Permalink
feat(config): add HD wallet parameters address index and `account n…
Browse files Browse the repository at this point in the history
…umber` to the chain account config (#3999)

* add address index and account number flags to the add keys command

* add changelog

---------

Co-authored-by: Pantani <Pantani>
  • Loading branch information
Pantani authored and julienrbrt committed May 29, 2024
1 parent ac303b3 commit 84b97f9
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#3977](https://github.com/ignite/cli/pull/3977) Add `chain lint` command to lint the chain's codebase using `golangci-lint`
- [#3770](https://github.com/ignite/cli/pull/3770) Add `scaffold configs` and `scaffold params` commands
- [#3985](https://github.com/ignite/cli/pull/3985) Make some `cmd` pkg functions public
- [#3967](https://github.com/ignite/cli/issues/3967) Add HD wallet parameters `address index` and `account number` to the chain account config

### Changes

Expand Down
12 changes: 7 additions & 5 deletions ignite/config/chain/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ var (

// Account holds the options related to setting up Cosmos wallets.
type Account struct {
Name string `yaml:"name"`
Coins []string `yaml:"coins,omitempty"`
Mnemonic string `yaml:"mnemonic,omitempty"`
Address string `yaml:"address,omitempty"`
CoinType string `yaml:"cointype,omitempty"`
Name string `yaml:"name"`
Coins []string `yaml:"coins,omitempty"`
Mnemonic string `yaml:"mnemonic,omitempty"`
Address string `yaml:"address,omitempty"`
CoinType string `yaml:"cointype,omitempty"`
AccountNumber string `yaml:"account_number,omitempty"`
AddressIndex string `yaml:"address_index,omitempty"`
}

// Build holds build configs.
Expand Down
18 changes: 16 additions & 2 deletions ignite/pkg/chaincmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
optionVestingAmount = "--vesting-amount"
optionVestingEndTime = "--vesting-end-time"
optionBroadcastMode = "--broadcast-mode"
optionAccount = "--account"
optionIndex = "--index"

constTendermint = "tendermint"
constJSON = "json"
Expand Down Expand Up @@ -185,7 +187,7 @@ func (c ChainCmd) InitCommand(moniker string) step.Option {
}

// AddKeyCommand returns the command to add a new key in the chain keyring.
func (c ChainCmd) AddKeyCommand(accountName, coinType string) step.Option {
func (c ChainCmd) AddKeyCommand(accountName, coinType, accountNumber, addressIndex string) step.Option {
command := []string{
commandKeys,
"add",
Expand All @@ -196,13 +198,19 @@ func (c ChainCmd) AddKeyCommand(accountName, coinType string) step.Option {
if coinType != "" {
command = append(command, optionCoinType, coinType)
}
if accountNumber != "" {
command = append(command, optionAccount, accountNumber)
}
if addressIndex != "" {
command = append(command, optionIndex, addressIndex)
}
command = c.attachKeyringBackend(command)

return c.cliCommand(command)
}

// RecoverKeyCommand returns the command to recover a key into the chain keyring from a mnemonic.
func (c ChainCmd) RecoverKeyCommand(accountName, coinType string) step.Option {
func (c ChainCmd) RecoverKeyCommand(accountName, coinType, accountNumber, addressIndex string) step.Option {
command := []string{
commandKeys,
"add",
Expand All @@ -212,6 +220,12 @@ func (c ChainCmd) RecoverKeyCommand(accountName, coinType string) step.Option {
if coinType != "" {
command = append(command, optionCoinType, coinType)
}
if accountNumber != "" {
command = append(command, optionAccount, accountNumber)
}
if addressIndex != "" {
command = append(command, optionIndex, addressIndex)
}
command = c.attachKeyringBackend(command)

return c.cliCommand(command)
Expand Down
13 changes: 10 additions & 3 deletions ignite/pkg/chaincmd/runner/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ type Account struct {
// AddAccount creates a new account or imports an account when mnemonic is provided.
// returns with an error if the operation went unsuccessful or an account with the provided name
// already exists.
func (r Runner) AddAccount(ctx context.Context, name, mnemonic, coinType string) (Account, error) {
func (r Runner) AddAccount(
ctx context.Context,
name,
mnemonic,
coinType,
accountNumber,
addressIndex string,
) (Account, error) {
if err := r.CheckAccountExist(ctx, name); err != nil {
return Account{}, err
}
Expand Down Expand Up @@ -66,7 +73,7 @@ func (r Runner) AddAccount(ctx context.Context, name, mnemonic, coinType string)
if err := r.run(
ctx,
runOptions{},
r.chainCmd.RecoverKeyCommand(name, coinType),
r.chainCmd.RecoverKeyCommand(name, coinType, accountNumber, addressIndex),
step.Write(input.Bytes()),
); err != nil {
return Account{}, err
Expand All @@ -76,7 +83,7 @@ func (r Runner) AddAccount(ctx context.Context, name, mnemonic, coinType string)
stdout: b,
stderr: b,
stdin: os.Stdin,
}, r.chainCmd.AddKeyCommand(name, coinType)); err != nil {
}, r.chainCmd.AddKeyCommand(name, coinType, accountNumber, addressIndex)); err != nil {
return Account{}, err
}

Expand Down
19 changes: 17 additions & 2 deletions ignite/pkg/cosmosfaucet/cosmosfaucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ type Faucet struct {
// coinType registered coin type number for HD derivation (BIP-0044).
coinType string

// accountNumber registered account number for HD derivation (BIP-0044).
accountNumber string

// addressIndex registered address index for HD derivation (BIP-0044).
addressIndex string

// coins keeps a list of coins that can be distributed by the faucet.
coins sdk.Coins

Expand All @@ -74,11 +80,13 @@ type Option func(*Faucet)

// Account provides the account information to transfer tokens from.
// when mnemonic isn't provided, account assumed to be exists in the keyring.
func Account(name, mnemonic string, coinType string) Option {
func Account(name, mnemonic, coinType, accountNumber, addressIndex string) Option {
return func(f *Faucet) {
f.accountName = name
f.accountMnemonic = mnemonic
f.coinType = coinType
f.accountNumber = accountNumber
f.addressIndex = addressIndex
}
}

Expand Down Expand Up @@ -153,7 +161,14 @@ func New(ctx context.Context, ccr chaincmdrunner.Runner, options ...Option) (Fau

// import the account if mnemonic is provided.
if f.accountMnemonic != "" {
_, err := f.runner.AddAccount(ctx, f.accountName, f.accountMnemonic, f.coinType)
_, err := f.runner.AddAccount(
ctx,
f.accountName,
f.accountMnemonic,
f.coinType,
f.accountNumber,
f.addressIndex,
)
if err != nil && !errors.Is(err, chaincmdrunner.ErrAccountAlreadyExists) {
return Faucet{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/services/chain/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *Chain) Faucet(ctx context.Context) (cosmosfaucet.Faucet, error) {
}

faucetOptions := []cosmosfaucet.Option{
cosmosfaucet.Account(*conf.Faucet.Name, "", ""),
cosmosfaucet.Account(*conf.Faucet.Name, "", "", "", ""),
cosmosfaucet.ChainID(id),
cosmosfaucet.OpenAPI(apiAddress),
cosmosfaucet.Version(c.Version),
Expand Down
9 changes: 8 additions & 1 deletion ignite/services/chain/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ func (c *Chain) InitAccounts(ctx context.Context, cfg *chainconfig.Config) error

// If the account doesn't provide an address, we create one
if accountAddress == "" {
generatedAccount, err = commands.AddAccount(ctx, account.Name, account.Mnemonic, account.CoinType)
generatedAccount, err = commands.AddAccount(
ctx,
account.Name,
account.Mnemonic,
account.CoinType,
account.AccountNumber,
account.AddressIndex,
)
if err != nil {
return err
}
Expand Down

0 comments on commit 84b97f9

Please sign in to comment.