From 0383feab494f1a8a3d4d1327890375fa123f3f07 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 13 Sep 2017 11:59:17 +0200 Subject: [PATCH] Add and test serialization of ledger privkey --- nano/keys.go | 5 +++-- nano/keys_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/nano/keys.go b/nano/keys.go index 7cac5eee8..6b337e8b7 100644 --- a/nano/keys.go +++ b/nano/keys.go @@ -9,6 +9,7 @@ import ( ledger "github.com/ethanfrey/ledger" crypto "github.com/tendermint/go-crypto" + wire "github.com/tendermint/go-wire" ) var device *ledger.Ledger @@ -62,9 +63,9 @@ func NewPrivKeyLedger() (crypto.PrivKey, error) { // AssertIsPrivKeyInner fulfils PrivKey Interface func (pk *PrivKeyLedger) AssertIsPrivKeyInner() {} -// Bytes fulfils pk Interface - not supported +// Bytes fulfils pk Interface - no data, just type info func (pk *PrivKeyLedger) Bytes() []byte { - return nil + return wire.BinaryBytes(pk.Wrap()) } // Sign calls the ledger and stores the pk for future use diff --git a/nano/keys_test.go b/nano/keys_test.go index 3a4d9c1eb..b42b091f0 100644 --- a/nano/keys_test.go +++ b/nano/keys_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + crypto "github.com/tendermint/go-crypto" ) func TestLedgerKeys(t *testing.T) { @@ -88,4 +89,19 @@ func TestRealLedger(t *testing.T) { valid := pub.VerifyBytes(msg, sig) assert.True(valid) + + // now, let's serialize the key and make sure it still works + bs := priv.Bytes() + priv2, err := crypto.PrivKeyFromBytes(bs) + require.Nil(err, "%+v", err) + + // make sure we get the same pubkey when we load from disk + pub2 := priv2.PubKey() + require.Equal(pub, pub2) + + // signing with the loaded key should match the original pubkey + sig = priv2.Sign(msg) + valid = pub.VerifyBytes(msg, sig) + assert.True(valid) + }