Browse Source

ledger pubkey serializes properly, better load checks

pull/1782/head
Ethan Frey 7 years ago
parent
commit
9afceb7ee8
2 changed files with 28 additions and 9 deletions
  1. +22
    -9
      nano/keys.go
  2. +6
    -0
      nano/keys_test.go

+ 22
- 9
nano/keys.go View File

@ -12,6 +12,7 @@ import (
wire "github.com/tendermint/go-wire"
)
//nolint
const (
NameLedgerEd25519 = "ledger"
TypeLedgerEd25519 = 0x10
@ -132,16 +133,23 @@ func (pk *PrivKeyLedger) PubKey() crypto.PubKey {
func (pk *PrivKeyLedger) getPubKey() (key crypto.PubKey, err error) {
// if we have no pubkey, set it
if pk.CachedPubKey.Empty() {
dev, err := getLedger()
if err != nil {
return key, errors.WithMessage(err, "Can't connect to ledger")
}
pk.CachedPubKey, _, err = signLedger(dev, []byte{0})
if err != nil {
return key, errors.WithMessage(err, "Can't sign with app")
}
pk.CachedPubKey, err = pk.forceGetPubKey()
}
return pk.CachedPubKey, nil
return pk.CachedPubKey, err
}
// forceGetPubKey is like getPubKey but ignores any cached key
// and ensures we get it from the ledger itself.
func (pk *PrivKeyLedger) forceGetPubKey() (key crypto.PubKey, err error) {
dev, err := getLedger()
if err != nil {
return key, errors.New("Can't connect to ledger device")
}
key, _, err = signLedger(dev, []byte{0})
if err != nil {
return key, errors.New("Please open cosmos app on the ledger")
}
return key, err
}
// Equals fulfils PrivKey Interface
@ -229,6 +237,11 @@ func PubKeyLedgerFromBytes(key [32]byte) crypto.PubKey {
return PubKeyLedger{crypto.PubKeyEd25519(key)}.Wrap()
}
// Bytes fulfils pk Interface - no data, just type info
func (pk PubKeyLedger) Bytes() []byte {
return wire.BinaryBytes(pk.Wrap())
}
// VerifyBytes uses the normal Ed25519 algorithm but a sha512 hash beforehand
func (pk PubKeyLedger) VerifyBytes(msg []byte, sig crypto.Signature) bool {
hmsg := hashMsg(msg)


+ 6
- 0
nano/keys_test.go View File

@ -104,4 +104,10 @@ func TestRealLedger(t *testing.T) {
valid = pub.VerifyBytes(msg, sig)
assert.True(valid)
// make sure pubkeys serialize properly as well
bs = pub.Bytes()
bpub, err := crypto.PubKeyFromBytes(bs)
require.NoError(err)
assert.Equal(pub, bpub)
}

Loading…
Cancel
Save