From 7ebc7c08e7de58540762d6ab2fc688677bef80c9 Mon Sep 17 00:00:00 2001 From: Liamsi Date: Wed, 20 Jun 2018 14:30:57 -0700 Subject: [PATCH] delete keys package - also delete ledger code and deps (thx cwgoes) Signed-off-by: Liamsi --- Gopkg.toml | 12 ----- Makefile | 2 +- amino.go | 2 - encode_test.go | 1 - ledger_common.go | 19 ------- ledger_secp256k1.go | 124 -------------------------------------------- ledger_test.go | 63 ---------------------- 7 files changed, 1 insertion(+), 222 deletions(-) delete mode 100644 ledger_common.go delete mode 100644 ledger_secp256k1.go delete mode 100644 ledger_test.go diff --git a/Gopkg.toml b/Gopkg.toml index 92bfecacd..a614df866 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -28,10 +28,6 @@ name = "github.com/btcsuite/btcutil" branch = "master" -[[constraint]] - name = "github.com/pkg/errors" - version = "0.8.0" - [[constraint]] name = "github.com/stretchr/testify" version = "1.2.1" @@ -48,14 +44,6 @@ name = "github.com/tendermint/tmlibs" version = "0.8.1" -[[constraint]] - name = "github.com/tyler-smith/go-bip39" - branch = "master" - -[[constraint]] - name = "github.com/zondax/ledger-goclient" - revision = "065cbf938a16f20335c40cfe180f9cd4955c6a5a" - [prune] go-tests = true unused-packages = true diff --git a/Makefile b/Makefile index 96f4ae06d..a4fd3c37f 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ get_vendor_deps: ### Testing test: - go test -p 1 $(shell go list ./... | grep -v vendor) + CGO_ENABLED=0 go test -p 1 $(shell go list ./... | grep -v vendor) ######################################## ### Formatting, linting, and vetting diff --git a/amino.go b/amino.go index 63e4c4ac3..2a57afdee 100644 --- a/amino.go +++ b/amino.go @@ -28,8 +28,6 @@ func RegisterAmino(cdc *amino.Codec) { "tendermint/PrivKeyEd25519", nil) cdc.RegisterConcrete(PrivKeySecp256k1{}, "tendermint/PrivKeySecp256k1", nil) - cdc.RegisterConcrete(PrivKeyLedgerSecp256k1{}, - "tendermint/PrivKeyLedgerSecp256k1", nil) cdc.RegisterInterface((*Signature)(nil), nil) cdc.RegisterConcrete(SignatureEd25519{}, diff --git a/encode_test.go b/encode_test.go index 025b09c80..a122f20e2 100644 --- a/encode_test.go +++ b/encode_test.go @@ -50,7 +50,6 @@ func ExamplePrintRegisteredTypes() { //| PubKeySecp256k1 | tendermint/PubKeySecp256k1 | 0xEB5AE987 | 0x21 | | //| PrivKeyEd25519 | tendermint/PrivKeyEd25519 | 0xA3288910 | 0x40 | | //| PrivKeySecp256k1 | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | 0x20 | | - //| PrivKeyLedgerSecp256k1 | tendermint/PrivKeyLedgerSecp256k1 | 0x10CAB393 | variable | | //| SignatureEd25519 | tendermint/SignatureEd25519 | 0x2031EA53 | 0x40 | | //| SignatureSecp256k1 | tendermint/SignatureSecp256k1 | 0x7FC4A495 | variable | | } diff --git a/ledger_common.go b/ledger_common.go deleted file mode 100644 index 39f15464a..000000000 --- a/ledger_common.go +++ /dev/null @@ -1,19 +0,0 @@ -package crypto - -import ( - ledger "github.com/zondax/ledger-goclient" -) - -var device *ledger.Ledger - -// Ledger derivation path -type DerivationPath = []uint32 - -// getLedger gets a copy of the device, and caches it -func getLedger() (*ledger.Ledger, error) { - var err error - if device == nil { - device, err = ledger.FindLedger() - } - return device, err -} diff --git a/ledger_secp256k1.go b/ledger_secp256k1.go deleted file mode 100644 index 21dfbb8b5..000000000 --- a/ledger_secp256k1.go +++ /dev/null @@ -1,124 +0,0 @@ -package crypto - -import ( - "fmt" - - secp256k1 "github.com/btcsuite/btcd/btcec" - ledger "github.com/zondax/ledger-goclient" -) - -func pubkeyLedgerSecp256k1(device *ledger.Ledger, path DerivationPath) (pub PubKey, err error) { - key, err := device.GetPublicKeySECP256K1(path) - if err != nil { - 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()) - pub = p - return -} - -func signLedgerSecp256k1(device *ledger.Ledger, path DerivationPath, msg []byte) (sig Signature, err error) { - bsig, err := device.SignSECP256K1(path, msg) - if err != nil { - return sig, err - } - sig = SignatureSecp256k1FromBytes(bsig) - return -} - -// PrivKeyLedgerSecp256k1 implements PrivKey, calling the ledger nano -// we cache the PubKey from the first call to use it later -type PrivKeyLedgerSecp256k1 struct { - // PubKey should be private, but we want to encode it via go-amino - // 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(path DerivationPath) (PrivKey, error) { - var pk PrivKeyLedgerSecp256k1 - pk.Path = path - // cache the pubkey for later use - pubKey, err := pk.getPubKey() - if err != nil { - return nil, err - } - pk.CachedPubKey = pubKey - return &pk, err -} - -// ValidateKey allows us to verify the sanity of a key -// after loading it from disk -func (pk PrivKeyLedgerSecp256k1) ValidateKey() error { - // getPubKey will return an error if the ledger is not - pub, err := pk.getPubKey() - if err != nil { - return err - } - // verify this matches cached address - if !pub.Equals(pk.CachedPubKey) { - return fmt.Errorf("cached key does not match retrieved key") - } - return nil -} - -// AssertIsPrivKeyInner fulfils PrivKey Interface -func (pk *PrivKeyLedgerSecp256k1) AssertIsPrivKeyInner() {} - -// Bytes fulfils PrivKey Interface - but it stores the cached pubkey so we can verify -// the same key when we reconnect to a ledger -func (pk PrivKeyLedgerSecp256k1) Bytes() []byte { - return cdc.MustMarshalBinaryBare(pk) -} - -// Sign calls the ledger and stores the PubKey for future use -// -// 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. -func (pk PrivKeyLedgerSecp256k1) Sign(msg []byte) (Signature, error) { - dev, err := getLedger() - if err != nil { - return nil, err - } - sig, err := signLedgerSecp256k1(dev, pk.Path, msg) - if err != nil { - return nil, err - } - return sig, nil -} - -// PubKey returns the stored PubKey -func (pk PrivKeyLedgerSecp256k1) PubKey() PubKey { - return pk.CachedPubKey -} - -// getPubKey reads the pubkey the ledger itself -// since this involves IO, it may return an error, which is not exposed -// in the PubKey interface, so this function allows better error handling -func (pk PrivKeyLedgerSecp256k1) getPubKey() (key PubKey, err error) { - dev, err := getLedger() - if err != nil { - return key, fmt.Errorf("cannot connect to Ledger device - error: %v", err) - } - key, err = pubkeyLedgerSecp256k1(dev, pk.Path) - if err != nil { - return key, fmt.Errorf("please open Cosmos app on the Ledger device - error: %v", err) - } - return key, err -} - -// Equals fulfils PrivKey Interface - makes sure both keys refer to the -// same -func (pk PrivKeyLedgerSecp256k1) Equals(other PrivKey) bool { - if ledger, ok := other.(*PrivKeyLedgerSecp256k1); ok { - return pk.CachedPubKey.Equals(ledger.CachedPubKey) - } - return false -} diff --git a/ledger_test.go b/ledger_test.go deleted file mode 100644 index 83390cc36..000000000 --- a/ledger_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package crypto - -import ( - "os" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestRealLedgerSecp256k1(t *testing.T) { - - if os.Getenv("WITH_LEDGER") == "" { - t.Skip("Set WITH_LEDGER to run code on real ledger") - } - msg := []byte("kuhehfeohg") - - path := DerivationPath{44, 60, 0, 0, 0} - - priv, err := NewPrivKeyLedgerSecp256k1(path) - require.Nil(t, err, "%+v", err) - pub := priv.PubKey() - sig, err := priv.Sign(msg) - require.Nil(t, err) - - valid := pub.VerifyBytes(msg, sig) - assert.True(t, valid) - - // now, let's serialize the key and make sure it still works - bs := priv.Bytes() - priv2, err := PrivKeyFromBytes(bs) - require.Nil(t, err, "%+v", err) - - // make sure we get the same pubkey when we load from disk - pub2 := priv2.PubKey() - require.Equal(t, pub, pub2) - - // signing with the loaded key should match the original pubkey - sig, err = priv2.Sign(msg) - require.Nil(t, err) - valid = pub.VerifyBytes(msg, sig) - assert.True(t, valid) - - // make sure pubkeys serialize properly as well - bs = pub.Bytes() - bpub, err := PubKeyFromBytes(bs) - require.NoError(t, err) - assert.Equal(t, pub, bpub) -} - -// TestRealLedgerErrorHandling calls. These tests assume -// the ledger is not plugged in.... -func TestRealLedgerErrorHandling(t *testing.T) { - if os.Getenv("WITH_LEDGER") != "" { - t.Skip("Skipping on WITH_LEDGER as it tests unplugged cases") - } - - // first, try to generate a key, must return an error - // (no panic) - path := DerivationPath{44, 60, 0, 0, 0} - _, err := NewPrivKeyLedgerSecp256k1(path) - require.Error(t, err) -}