Add and test serialization of ledger privkey

This commit is contained in:
Ethan Frey
2017-09-13 11:59:17 +02:00
parent 3edeb0cd45
commit 0383feab49
2 changed files with 19 additions and 2 deletions

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

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