From 80e97522504e79dca2f0e2c35168e6f47dc307ce Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 31 May 2018 21:30:20 +0200 Subject: [PATCH] Move TODOs to #114 --- Gopkg.lock | 4 ++-- ledger_secp256k1.go | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 2090ac2e3816..f51e2b22950e 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -11,7 +11,7 @@ branch = "master" name = "github.com/btcsuite/btcd" packages = ["btcec"] - revision = "bc0944904505aab55e089371a892be2f87883161" + revision = "86fed781132ac890ee03e906e4ecd5d6fa180c64" [[projects]] branch = "master" @@ -172,7 +172,7 @@ "ripemd160", "salsa20/salsa" ] - revision = "ab813273cd59e1333f7ae7bff5d027d4aadf528c" + revision = "5ba7f63082460102a45837dbd1827e10f9479ac0" [solve-meta] analyzer-name = "dep" diff --git a/ledger_secp256k1.go b/ledger_secp256k1.go index b574035d4332..f485bb41589b 100644 --- a/ledger_secp256k1.go +++ b/ledger_secp256k1.go @@ -2,7 +2,6 @@ package crypto import ( "fmt" - "github.com/pkg/errors" secp256k1 "github.com/btcsuite/btcd/btcec" ledger "github.com/zondax/ledger-goclient" @@ -11,13 +10,14 @@ import ( func pubkeyLedgerSecp256k1(device *ledger.Ledger, path DerivationPath) (pub PubKey, err error) { key, err := device.GetPublicKeySECP256K1(path) if err != nil { - return pub, fmt.Errorf("Error fetching public key: %v", err) + return nil, fmt.Errorf("error fetching public key: %v", err) } var p PubKeySecp256k1 // Reserialize in the 33-byte compressed format cmp, err := secp256k1.ParsePubKey(key[:], secp256k1.S256()) copy(p[:], cmp.SerializeCompressed()) - return p, err + pub = p + return } func signLedgerSecp256k1(device *ledger.Ledger, path DerivationPath, msg []byte) (sig Signature, err error) { @@ -26,7 +26,7 @@ func signLedgerSecp256k1(device *ledger.Ledger, path DerivationPath, msg []byte) return sig, err } sig = SignatureSecp256k1FromBytes(bsig) - return sig, nil + return } // PrivKeyLedgerSecp256k1 implements PrivKey, calling the ledger nano @@ -62,7 +62,7 @@ func (pk PrivKeyLedgerSecp256k1) ValidateKey() error { } // verify this matches cached address if !pub.Equals(pk.CachedPubKey) { - return errors.New("Cached key does not match retrieved key") + return fmt.Errorf("cached key does not match retrieved key") } return nil } @@ -82,8 +82,6 @@ func (pk PrivKeyLedgerSecp256k1) Bytes() []byte { // Sign calls the ledger and stores the PubKey for future use // -// XXX/TODO: panics if there is an error communicating with the ledger. -// // Communication is checked on NewPrivKeyLedger and PrivKeyFromBytes, // returning an error, so this should only trigger if the privkey is held // in memory for a while before use. @@ -108,13 +106,12 @@ func (pk PrivKeyLedgerSecp256k1) Sign(msg []byte) Signature { if pk.CachedPubKey == nil { pk.CachedPubKey = pub } else if !pk.CachedPubKey.Equals(pub) { - panic("Stored key does not match signing key") + panic("stored key does not match signing key") } return sig } // PubKey returns the stored PubKey -// TODO: query the ledger if not there, once it is not volatile func (pk PrivKeyLedgerSecp256k1) PubKey() PubKey { key, err := pk.getPubKey() if err != nil { @@ -139,11 +136,11 @@ func (pk PrivKeyLedgerSecp256k1) getPubKey() (key PubKey, err error) { func (pk PrivKeyLedgerSecp256k1) forceGetPubKey() (key PubKey, err error) { dev, err := getLedger() if err != nil { - return key, errors.New(fmt.Sprintf("Cannot connect to Ledger device - error: %v", err)) + return key, fmt.Errorf("cannot connect to Ledger device - error: %v", err) } key, err = pubkeyLedgerSecp256k1(dev, pk.Path) if err != nil { - return key, errors.New(fmt.Sprintf("Please open Cosmos app on the Ledger device - error: %v", err)) + return key, fmt.Errorf("please open Cosmos app on the Ledger device - error: %v", err) } return key, err }