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)
|
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
pk := &secp256k1.PubKey{Key: tmpKey}
|
|
|
|
path := hd.NewFundraiserParams(5, sdk.CoinType, 1)
|
|
|
|
k, err := NewLedgerRecord("some_name", pk, path)
|
2020-11-18 10:42:45 -08:00
|
|
|
require.NoError(t, err)
|
2021-09-20 05:02:15 -07:00
|
|
|
|
|
|
|
l := k.GetLedger()
|
|
|
|
require.NotNil(t, l)
|
|
|
|
path = l.Path
|
2020-11-18 10:42:45 -08:00
|
|
|
require.Equal(t, "m/44'/118'/5'/0/1", path.String())
|
2021-09-20 05:02:15 -07:00
|
|
|
pubKey, err := k.GetPubKey()
|
|
|
|
require.NoError(t, err)
|
2020-11-18 10:42:45 -08:00
|
|
|
require.Equal(t,
|
2021-03-25 07:53:22 -07:00
|
|
|
fmt.Sprintf("PubKeySecp256k1{%s}", hexPK),
|
2021-09-20 05:02:15 -07:00
|
|
|
pubKey.String())
|
2019-02-11 09:02:47 -08:00
|
|
|
|
|
|
|
// Serialize and restore
|
2021-09-20 05:02:15 -07:00
|
|
|
cdc := getCodec()
|
|
|
|
serialized, err := cdc.Marshal(k)
|
2020-11-18 10:42:45 -08:00
|
|
|
require.NoError(t, err)
|
2021-09-20 05:02:15 -07:00
|
|
|
var restoredRecord Record
|
|
|
|
err = cdc.Unmarshal(serialized, &restoredRecord)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, restoredRecord)
|
2019-02-11 09:02:47 -08:00
|
|
|
|
|
|
|
// Check both keys match
|
2021-09-20 05:02:15 -07:00
|
|
|
require.Equal(t, k.Name, restoredRecord.Name)
|
|
|
|
require.Equal(t, k.GetType(), restoredRecord.GetType())
|
|
|
|
|
|
|
|
restoredPubKey, err := restoredRecord.GetPubKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, pubKey, restoredPubKey)
|
2019-02-11 09:02:47 -08:00
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
l = restoredRecord.GetLedger()
|
|
|
|
require.NotNil(t, l)
|
|
|
|
restoredPath := l.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
|
|
|
}
|