From 337ad8e594d31a16449cf2756ed33ba959ea3964 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 10 May 2018 02:03:28 +0200 Subject: [PATCH] Update to new Ledger API in progress --- ledger.go | 19 +++++++++++-------- ledger_test.go | 7 +++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ledger.go b/ledger.go index 4effa2590..d44b2dab9 100644 --- a/ledger.go +++ b/ledger.go @@ -9,6 +9,8 @@ import ( var device *ledger.Ledger +type DerivationPath = []uint32 + // getLedger gets a copy of the device, and caches it func getLedger() (*ledger.Ledger, error) { var err error @@ -18,8 +20,8 @@ func getLedger() (*ledger.Ledger, error) { return device, err } -func pubkeyLedger(device *ledger.Ledger) (pub PubKey, err error) { - key, err := device.GetPublicKey() +func pubkeyLedger(device *ledger.Ledger, path DerivationPath) (pub PubKey, err error) { + key, err := device.GetPublicKeySECP256K1(path) if err != nil { return pub, err } @@ -30,8 +32,8 @@ func pubkeyLedger(device *ledger.Ledger) (pub PubKey, err error) { return p, err } -func signLedger(device *ledger.Ledger, msg []byte) (sig Signature, err error) { - bsig, err := device.Sign(msg) +func signLedger(device *ledger.Ledger, path DerivationPath, msg []byte) (sig Signature, err error) { + bsig, err := device.SignSECP256K1(path, msg) if err != nil { return sig, err } @@ -46,11 +48,12 @@ type PrivKeyLedgerSecp256k1 struct { // so we can view the address later, even without having the ledger // attached CachedPubKey PubKey + Path DerivationPath } // NewPrivKeyLedgerSecp256k1 will generate a new key and store the // public key for later use. -func NewPrivKeyLedgerSecp256k1() (PrivKey, error) { +func NewPrivKeyLedgerSecp256k1(path DerivationPath) (PrivKey, error) { var pk PrivKeyLedgerSecp256k1 // getPubKey will cache the pubkey for later use, // this allows us to return an error early if the ledger @@ -102,12 +105,12 @@ func (pk PrivKeyLedgerSecp256k1) Sign(msg []byte) Signature { panic(err) } - sig, err := signLedger(dev, msg) + sig, err := signLedger(dev, pk.Path, msg) if err != nil { panic(err) } - pub, err := pubkeyLedger(dev) + pub, err := pubkeyLedger(dev, pk.Path) if err != nil { panic(err) } @@ -149,7 +152,7 @@ func (pk PrivKeyLedgerSecp256k1) forceGetPubKey() (key PubKey, err error) { if err != nil { return key, errors.New("Cannot connect to Ledger device") } - key, err = pubkeyLedger(dev) + key, err = pubkeyLedger(dev, pk.Path) if err != nil { return key, errors.New("Please open Cosmos app on the Ledger device") } diff --git a/ledger_test.go b/ledger_test.go index 7b2b4ea1e..1c193b5f1 100644 --- a/ledger_test.go +++ b/ledger_test.go @@ -15,7 +15,9 @@ func TestRealLedger(t *testing.T) { } msg := []byte("kuhehfeohg") - priv, err := NewPrivKeyLedgerSecp256k1() + path := DerivationPath{44, 60, 0, 0, 0} + + priv, err := NewPrivKeyLedgerSecp256k1(path) require.Nil(t, err, "%+v", err) pub := priv.PubKey() sig := priv.Sign(msg) @@ -53,6 +55,7 @@ func TestRealLedgerErrorHandling(t *testing.T) { // first, try to generate a key, must return an error // (no panic) - _, err := NewPrivKeyLedgerSecp256k1() + path := DerivationPath{44, 60, 0, 0, 0} + _, err := NewPrivKeyLedgerSecp256k1(path) require.Error(t, err) }