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

feat: support custom mnemonics in in-process testing network #10922

Merged
merged 14 commits into from
Jan 12, 2022
Merged
39 changes: 27 additions & 12 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ func GenerateCoinKey(algo keyring.SignatureAlgo, cdc codec.Codec) (sdk.AccAddres
// generate a private key, with recovery phrase
k, secret, err := keyring.NewInMemory(cdc).NewMnemonic("name", keyring.English, sdk.GetConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
return sdk.AccAddress{}, "", err
}

addr, err := k.GetAddress()
if err != nil {
return nil, "", err
}

return addr, secret, nil
}

// GenerateSaveCoinKey returns the address of a public key, along with the secret
// phrase to recover the private key.
func GenerateSaveCoinKey(keybase keyring.Keyring, keyName string, overwrite bool, algo keyring.SignatureAlgo) (sdk.AccAddress, string, error) {
func GenerateSaveCoinKey(
keybase keyring.Keyring,
keyName, mnemonic string,
overwrite bool,
algo keyring.SignatureAlgo,
) (sdk.AccAddress, string, error) {
exists := false
_, err := keybase.Key(keyName)
if err == nil {
Expand All @@ -34,25 +41,33 @@ func GenerateSaveCoinKey(keybase keyring.Keyring, keyName string, overwrite bool

// ensure no overwrite
if !overwrite && exists {
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
"key already exists, overwrite is disabled")
return sdk.AccAddress{}, "", fmt.Errorf("key already exists, overwrite is disabled")
}

// generate a private key, with recovery phrase
// remove the old key by name if it exists
if exists {
err = keybase.Delete(keyName)
if err != nil {
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
"failed to overwrite key")
if err = keybase.Delete(keyName); err != nil {
return sdk.AccAddress{}, "", fmt.Errorf("failed to overwrite key")
}
}

k, secret, err := keybase.NewMnemonic(keyName, keyring.English, sdk.GetConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo)
var (
record *keyring.Record
secret string
)

// generate or recover a new account
if mnemonic != "" {
secret = mnemonic
record, err = keybase.NewAccount(keyName, mnemonic, keyring.DefaultBIP39Passphrase, sdk.GetConfig().GetFullBIP44Path(), algo)
} else {
record, secret, err = keybase.NewMnemonic(keyName, keyring.English, sdk.GetConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo)
}
if err != nil {
return sdk.AccAddress([]byte{}), "", err
return sdk.AccAddress{}, "", err
}

addr, err := k.GetAddress()
addr, err := record.GetAddress()
if err != nil {
return nil, "", err
}
Expand Down
8 changes: 4 additions & 4 deletions server/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestGenerateSaveCoinKey(t *testing.T) {
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, encCfg.Codec)
require.NoError(t, err)

addr, mnemonic, err := server.GenerateSaveCoinKey(kb, "keyname", false, hd.Secp256k1)
addr, mnemonic, err := server.GenerateSaveCoinKey(kb, "keyname", "", false, hd.Secp256k1)
require.NoError(t, err)

// Test key was actually saved
Expand All @@ -59,15 +59,15 @@ func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) {
require.NoError(t, err)

keyname := "justakey"
addr1, _, err := server.GenerateSaveCoinKey(kb, keyname, false, hd.Secp256k1)
addr1, _, err := server.GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1)
require.NoError(t, err)

// Test overwrite with overwrite=false
_, _, err = server.GenerateSaveCoinKey(kb, keyname, false, hd.Secp256k1)
_, _, err = server.GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1)
require.Error(t, err)

// Test overwrite with overwrite=true
addr2, _, err := server.GenerateSaveCoinKey(kb, keyname, true, hd.Secp256k1)
addr2, _, err := server.GenerateSaveCoinKey(kb, keyname, "", true, hd.Secp256k1)
require.NoError(t, err)

require.NotEqual(t, addr1, addr2)
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func initTestnetFiles(
return err
}

addr, secret, err := server.GenerateSaveCoinKey(kb, nodeDirName, true, algo)
addr, secret, err := server.GenerateSaveCoinKey(kb, nodeDirName, "", true, algo)
if err != nil {
_ = os.RemoveAll(args.outputDir)
return err
Expand Down
10 changes: 9 additions & 1 deletion testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Config struct {
TimeoutCommit time.Duration // the consensus commitment timeout
ChainID string // the network chain-id
NumValidators int // the total number of validators to create and bond
Mnemonics []string // custom user-provided validator operator mnemonics
BondDenom string // the staking bond denomination
MinGasPrices string // the minimum gas prices each validator will accept
AccountTokens sdk.Int // the amount of unique validator tokens (e.g. 1000node0)
Expand Down Expand Up @@ -327,6 +328,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
if err != nil {
return nil, err
}

tmCfg.P2P.ListenAddress = p2pAddr
tmCfg.P2P.AddrBookStrict = false
tmCfg.P2P.AllowDuplicateIP = true
Expand All @@ -335,6 +337,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
if err != nil {
return nil, err
}

nodeIDs[i] = nodeID
valPubKeys[i] = pubKey

Expand All @@ -349,7 +352,12 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
return nil, err
}

addr, secret, err := server.GenerateSaveCoinKey(kb, nodeDirName, true, algo)
var mnemonic string
if i < len(cfg.Mnemonics) {
mnemonic = cfg.Mnemonics[i]
}

addr, secret, err := server.GenerateSaveCoinKey(kb, nodeDirName, mnemonic, true, algo)
if err != nil {
return nil, err
}
Expand Down