Browse Source

Add and test serialization of ledger privkey

pull/1782/head
Ethan Frey 7 years ago
parent
commit
0383feab49
2 changed files with 19 additions and 2 deletions
  1. +3
    -2
      nano/keys.go
  2. +16
    -0
      nano/keys_test.go

+ 3
- 2
nano/keys.go View File

@ -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


+ 16
- 0
nano/keys_test.go View File

@ -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)
}

Loading…
Cancel
Save