2020-03-25 08:20:36 -07:00
|
|
|
package keyring
|
2019-02-11 09:02:47 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2021-03-25 07:53:22 -07:00
|
|
|
"fmt"
|
2019-02-11 09:02:47 -08:00
|
|
|
"testing"
|
|
|
|
|
2020-12-02 05:50:50 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2020-08-28 09:02:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
2020-01-09 06:04:28 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-11 09:02:47 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_writeReadLedgerInfo(t *testing.T) {
|
2020-09-16 04:08:55 -07:00
|
|
|
tmpKey := make([]byte, secp256k1.PubKeySize)
|
2021-03-25 07:53:22 -07:00
|
|
|
hexPK := "035AD6810A47F073553FF30D2FCC7E0D3B1C0B74B61A1AAA2582344037151E143A"
|
|
|
|
bz, err := hex.DecodeString(hexPK)
|
|
|
|
require.NoError(t, err)
|
2019-02-11 09:02:47 -08:00
|
|
|
copy(tmpKey[:], bz)
|
|
|
|
|
2020-09-16 04:08:55 -07:00
|
|
|
lInfo := newLedgerInfo("some_name", &secp256k1.PubKey{Key: tmpKey}, *hd.NewFundraiserParams(5, sdk.CoinType, 1), hd.Secp256k1Type)
|
2020-11-18 10:42:45 -08:00
|
|
|
require.Equal(t, TypeLedger, lInfo.GetType())
|
2019-02-25 03:34:39 -08:00
|
|
|
|
|
|
|
path, err := lInfo.GetPath()
|
2020-11-18 10:42:45 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "m/44'/118'/5'/0/1", path.String())
|
|
|
|
require.Equal(t,
|
2021-03-25 07:53:22 -07:00
|
|
|
fmt.Sprintf("PubKeySecp256k1{%s}", hexPK),
|
|
|
|
lInfo.GetPubKey().String())
|
2019-02-11 09:02:47 -08:00
|
|
|
|
|
|
|
// Serialize and restore
|
2019-09-21 09:54:14 -07:00
|
|
|
serialized := marshalInfo(lInfo)
|
|
|
|
restoredInfo, err := unmarshalInfo(serialized)
|
2020-11-18 10:42:45 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, restoredInfo)
|
2019-02-11 09:02:47 -08:00
|
|
|
|
|
|
|
// Check both keys match
|
2020-11-18 10:42:45 -08:00
|
|
|
require.Equal(t, lInfo.GetName(), restoredInfo.GetName())
|
|
|
|
require.Equal(t, lInfo.GetType(), restoredInfo.GetType())
|
|
|
|
require.Equal(t, lInfo.GetPubKey(), restoredInfo.GetPubKey())
|
2019-02-11 09:02:47 -08:00
|
|
|
|
2019-02-25 03:34:39 -08:00
|
|
|
restoredPath, err := restoredInfo.GetPath()
|
2020-11-18 10:42:45 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, path, restoredPath)
|
2019-02-11 09:02:47 -08:00
|
|
|
}
|