2019-06-22 02:24:59 -07:00
|
|
|
//+build ledger test_ledger_mock
|
2019-02-11 09:02:47 -08:00
|
|
|
|
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2020-10-08 10:41:35 -07:00
|
|
|
"context"
|
2020-07-06 12:50:09 -07:00
|
|
|
"fmt"
|
2019-02-11 09:02:47 -08:00
|
|
|
"testing"
|
|
|
|
|
2019-11-14 06:17:21 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-11 09:02:47 -08:00
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-06-11 07:30:42 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-07-09 05:21:20 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
2019-05-28 01:44:04 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-11 09:02:47 -08:00
|
|
|
)
|
|
|
|
|
2019-07-29 11:01:53 -07:00
|
|
|
func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
|
|
|
|
config := sdk.GetConfig()
|
|
|
|
|
|
|
|
bech32PrefixAccAddr := "terra"
|
|
|
|
bech32PrefixAccPub := "terrapub"
|
|
|
|
bech32PrefixValAddr := "terravaloper"
|
|
|
|
bech32PrefixValPub := "terravaloperpub"
|
|
|
|
bech32PrefixConsAddr := "terravalcons"
|
|
|
|
bech32PrefixConsPub := "terravalconspub"
|
|
|
|
|
2021-02-22 07:14:09 -08:00
|
|
|
config.SetPurpose(44)
|
2019-07-29 11:01:53 -07:00
|
|
|
config.SetCoinType(330)
|
|
|
|
config.SetBech32PrefixForAccount(bech32PrefixAccAddr, bech32PrefixAccPub)
|
|
|
|
config.SetBech32PrefixForValidator(bech32PrefixValAddr, bech32PrefixValPub)
|
|
|
|
config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub)
|
|
|
|
|
2019-12-12 13:52:24 -08:00
|
|
|
cmd := AddKeyCommand()
|
2020-07-20 08:12:33 -07:00
|
|
|
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
|
2019-07-29 11:01:53 -07:00
|
|
|
|
|
|
|
// Prepare a keybase
|
2020-09-18 04:08:24 -07:00
|
|
|
kbHome := t.TempDir()
|
2020-07-06 12:50:09 -07:00
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
clientCtx := client.Context{}.WithKeyringDir(kbHome)
|
|
|
|
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
|
|
|
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{
|
|
|
|
"keyname1",
|
|
|
|
fmt.Sprintf("--%s=true", flags.FlagUseLedger),
|
|
|
|
fmt.Sprintf("--%s=0", flagAccount),
|
|
|
|
fmt.Sprintf("--%s=0", flagIndex),
|
|
|
|
fmt.Sprintf("--%s=330", flagCoinType),
|
|
|
|
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
|
2020-08-12 01:34:10 -07:00
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)),
|
2020-07-06 12:50:09 -07:00
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
|
|
|
|
})
|
|
|
|
|
2020-07-09 23:55:48 -07:00
|
|
|
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
|
2019-07-29 11:01:53 -07:00
|
|
|
mockIn.Reset("test1234\ntest1234\n")
|
2020-10-08 10:41:35 -07:00
|
|
|
require.NoError(t, cmd.ExecuteContext(ctx))
|
2019-07-29 11:01:53 -07:00
|
|
|
|
|
|
|
// Now check that it has been stored properly
|
2020-07-06 12:50:09 -07:00
|
|
|
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, kb)
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(func() {
|
2020-06-11 07:30:42 -07:00
|
|
|
_ = kb.Delete("keyname1")
|
2020-03-01 16:16:23 -08:00
|
|
|
})
|
2019-11-14 06:17:21 -08:00
|
|
|
mockIn.Reset("test1234\n")
|
2020-04-08 02:38:28 -07:00
|
|
|
key1, err := kb.Key("keyname1")
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, key1)
|
2019-07-29 11:01:53 -07:00
|
|
|
|
2019-11-14 06:17:21 -08:00
|
|
|
require.Equal(t, "keyname1", key1.GetName())
|
2020-03-25 08:20:36 -07:00
|
|
|
require.Equal(t, keyring.TypeLedger, key1.GetType())
|
2019-11-14 06:17:21 -08:00
|
|
|
require.Equal(t,
|
2021-03-25 07:53:22 -07:00
|
|
|
"PubKeySecp256k1{03028F0D5A9FD41600191CDEFDEA05E77A68DFBCE286241C0190805B9346667D07}",
|
|
|
|
key1.GetPubKey().String())
|
2019-07-29 11:01:53 -07:00
|
|
|
|
2021-02-22 07:14:09 -08:00
|
|
|
config.SetPurpose(44)
|
2019-07-29 11:01:53 -07:00
|
|
|
config.SetCoinType(118)
|
|
|
|
config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub)
|
|
|
|
config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub)
|
|
|
|
config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
|
|
|
|
}
|
|
|
|
|
2019-02-11 09:02:47 -08:00
|
|
|
func Test_runAddCmdLedger(t *testing.T) {
|
2019-12-12 13:52:24 -08:00
|
|
|
cmd := AddKeyCommand()
|
2020-07-20 08:12:33 -07:00
|
|
|
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
|
2020-10-08 10:41:35 -07:00
|
|
|
|
2020-07-09 23:55:48 -07:00
|
|
|
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
|
2020-09-18 04:08:24 -07:00
|
|
|
kbHome := t.TempDir()
|
2020-07-06 12:50:09 -07:00
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
clientCtx := client.Context{}.WithKeyringDir(kbHome)
|
|
|
|
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
|
|
|
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{
|
|
|
|
"keyname1",
|
|
|
|
fmt.Sprintf("--%s=true", flags.FlagUseLedger),
|
|
|
|
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
|
2020-08-12 01:34:10 -07:00
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)),
|
2020-07-06 12:50:09 -07:00
|
|
|
fmt.Sprintf("--%s=%d", flagCoinType, sdk.CoinType),
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
|
|
|
|
})
|
2019-06-22 02:24:59 -07:00
|
|
|
mockIn.Reset("test1234\ntest1234\n")
|
2020-07-06 12:50:09 -07:00
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
require.NoError(t, cmd.ExecuteContext(ctx))
|
2019-02-11 09:02:47 -08:00
|
|
|
|
|
|
|
// Now check that it has been stored properly
|
2020-07-06 12:50:09 -07:00
|
|
|
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, kb)
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(func() {
|
2020-06-11 07:30:42 -07:00
|
|
|
_ = kb.Delete("keyname1")
|
2020-03-01 16:16:23 -08:00
|
|
|
})
|
2020-07-06 12:50:09 -07:00
|
|
|
|
2019-11-14 06:17:21 -08:00
|
|
|
mockIn.Reset("test1234\n")
|
2020-04-08 02:38:28 -07:00
|
|
|
key1, err := kb.Key("keyname1")
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, key1)
|
2019-02-11 09:02:47 -08:00
|
|
|
|
2019-11-14 06:17:21 -08:00
|
|
|
require.Equal(t, "keyname1", key1.GetName())
|
2020-03-25 08:20:36 -07:00
|
|
|
require.Equal(t, keyring.TypeLedger, key1.GetType())
|
2019-11-14 06:17:21 -08:00
|
|
|
require.Equal(t,
|
2021-03-25 07:53:22 -07:00
|
|
|
"PubKeySecp256k1{034FEF9CD7C4C63588D3B03FEB5281B9D232CBA34D6F3D71AEE59211FFBFE1FE87}",
|
|
|
|
key1.GetPubKey().String())
|
2019-02-11 09:02:47 -08:00
|
|
|
}
|