2019-02-07 14:38:45 -08:00
|
|
|
package server_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-02-08 13:45:41 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2019-02-07 14:38:45 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
2020-04-08 02:38:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types"
|
2019-02-07 14:38:45 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateCoinKey(t *testing.T) {
|
2019-02-08 13:45:41 -08:00
|
|
|
t.Parallel()
|
2020-08-12 01:34:10 -07:00
|
|
|
addr, mnemonic, err := server.GenerateCoinKey(hd.Secp256k1)
|
2019-02-07 14:38:45 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test creation
|
2020-04-08 02:38:28 -07:00
|
|
|
info, err := keyring.NewInMemory().NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.GetConfig().GetCoinType(), 0).String(), hd.Secp256k1)
|
2019-02-07 14:38:45 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, addr, info.GetAddress())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateSaveCoinKey(t *testing.T) {
|
2019-02-08 13:45:41 -08:00
|
|
|
t.Parallel()
|
2019-02-07 14:38:45 -08:00
|
|
|
|
2020-09-18 04:08:24 -07:00
|
|
|
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil)
|
2019-02-07 14:38:45 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-12 01:34:10 -07:00
|
|
|
addr, mnemonic, err := server.GenerateSaveCoinKey(kb, "keyname", false, hd.Secp256k1)
|
2019-02-07 14:38:45 -08:00
|
|
|
require.NoError(t, err)
|
2019-11-14 06:17:21 -08:00
|
|
|
|
|
|
|
// Test key was actually saved
|
2020-04-08 02:38:28 -07:00
|
|
|
info, err := kb.Key("keyname")
|
2019-02-07 14:38:45 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, addr, info.GetAddress())
|
|
|
|
|
|
|
|
// Test in-memory recovery
|
2020-04-08 02:38:28 -07:00
|
|
|
info, err = keyring.NewInMemory().NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.GetConfig().GetCoinType(), 0).String(), hd.Secp256k1)
|
2019-02-07 14:38:45 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, addr, info.GetAddress())
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:45:41 -08:00
|
|
|
func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-11-14 06:17:21 -08:00
|
|
|
|
2020-09-18 04:08:24 -07:00
|
|
|
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil)
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
2019-02-08 13:45:41 -08:00
|
|
|
|
|
|
|
keyname := "justakey"
|
2020-08-12 01:34:10 -07:00
|
|
|
addr1, _, err := server.GenerateSaveCoinKey(kb, keyname, false, hd.Secp256k1)
|
2019-02-08 13:45:41 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test overwrite with overwrite=false
|
2020-08-12 01:34:10 -07:00
|
|
|
_, _, err = server.GenerateSaveCoinKey(kb, keyname, false, hd.Secp256k1)
|
2019-02-08 13:45:41 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Test overwrite with overwrite=true
|
2020-08-12 01:34:10 -07:00
|
|
|
addr2, _, err := server.GenerateSaveCoinKey(kb, keyname, true, hd.Secp256k1)
|
2019-02-08 13:45:41 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotEqual(t, addr1, addr2)
|
|
|
|
}
|