From f48baf86fb190f61b8e44baa9d033c6d85658b28 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 27 Dec 2017 14:37:37 -0800 Subject: [PATCH] Add Address type which is HexBytes --- glide.lock | 30 ++++++++++++++---------------- glide.yaml | 2 +- hd/address.go | 4 ++++ hd/hd_test.go | 10 +--------- keys/keybase.go | 4 +++- pub_key.go | 27 ++++++++++++++++----------- pub_key_test.go | 3 ++- 7 files changed, 41 insertions(+), 39 deletions(-) diff --git a/glide.lock b/glide.lock index c8c922c6a..5032685c2 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 765599cf3dcac232d3d2ece65c3b016d0abe55d0536adfe93d11e0dd29287de4 -updated: 2017-11-13T14:37:54.127163059+01:00 +hash: a21061afc44c148eb6bfeb91478b520661f3d086234383a0208d915b0cb058b8 +updated: 2017-12-27T14:29:49.534901894-08:00 imports: - name: github.com/btcsuite/btcd version: c7588cbf7690cd9f047a28efa2dcd8f2435a4e5e @@ -9,10 +9,10 @@ imports: version: 66871daeb12123ece012a9628d2798d01195c4b3 subpackages: - base58 -- name: github.com/ethanfrey/hid - version: ea8f4c923d3bc8c9c312b7d38724a294f184258a - name: github.com/ethanfrey/ledger - version: 23a7bb9d74bc83a862fcb4bddde24215b2295ad9 + version: 5e432577be582bd18a3b4a9cd75dae7a317ade36 +- name: github.com/flynn/hid + version: ed06a31c6245d4552e8dbba7e32e5b010b875d65 - name: github.com/go-kit/kit version: e2b298466b32c7cd5579a9b9b07e968fc9d9452c subpackages: @@ -21,14 +21,14 @@ imports: - log/term - name: github.com/go-logfmt/logfmt version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5 -- name: github.com/go-playground/locales - version: e4cbcb5d0652150d40ad0646651076b6bd2be4f6 - subpackages: - - currency -- name: github.com/go-playground/universal-translator - version: 71201497bace774495daed26a3874fd339e0b538 - name: github.com/go-stack/stack version: 817915b46b97fd7bb80e8ab6b69f01a53ac3eebf +- name: github.com/gogo/protobuf + version: 342cbe0a04158f6dcb03ca0079991a51a4248c02 + subpackages: + - gogoproto + - proto + - protoc-gen-gogo/descriptor - name: github.com/golang/snappy version: 553a641470496b2327abcac10b36396bd98e45c9 - name: github.com/howeyc/crc16 @@ -55,17 +55,17 @@ imports: - leveldb/table - leveldb/util - name: github.com/tendermint/ed25519 - version: d8387025d2b9d158cf4efb07e7ebf814bcce2057 + version: 1f52c6f8b8a5c7908aff4497c186af344b428925 subpackages: - edwards25519 - extra25519 - name: github.com/tendermint/go-wire - version: 1c96861c03231361546944d883d99593b2e6b408 + version: 27be46e25124ddf775e23317a83647ce62a93f6b subpackages: - data - data/base58 - name: github.com/tendermint/tmlibs - version: 56e51bc1133bd54a331e0314d5f80fb547460362 + version: 93c05aa8c06ef38f2b15fcdd1d91eafefda2732d subpackages: - common - db @@ -82,8 +82,6 @@ imports: - poly1305 - ripemd160 - salsa20/salsa -- name: gopkg.in/go-playground/validator.v9 - version: 1304298bf10d085adec514b076772a79c9cadb6b testImports: - name: github.com/davecgh/go-spew version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9 diff --git a/glide.yaml b/glide.yaml index 64b275f10..f9df5c18d 100644 --- a/glide.yaml +++ b/glide.yaml @@ -17,7 +17,7 @@ import: subpackages: - extra25519 - package: github.com/tendermint/tmlibs - version: sdk-develop + version: sdk2 - package: github.com/tendermint/go-wire version: develop subpackages: diff --git a/hd/address.go b/hd/address.go index 5b664b496..f59876739 100644 --- a/hd/address.go +++ b/hd/address.go @@ -1,5 +1,9 @@ package hd +// XXX This package doesn't work with our address scheme, +// XXX and it probably doesn't work for our other pubkey types. +// XXX Fix it up to be more general but compatible. + import ( "crypto/ecdsa" "crypto/hmac" diff --git a/hd/hd_test.go b/hd/hd_test.go index 02d80388c..0ec47f5c3 100644 --- a/hd/hd_test.go +++ b/hd/hd_test.go @@ -2,9 +2,6 @@ package hd import ( "bytes" - //"crypto/hmac" - //"crypto/sha512" - //"encoding/binary" "encoding/hex" "encoding/json" "fmt" @@ -15,11 +12,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/tyler-smith/go-bip39" - //"github.com/btcsuite/btcd/chaincfg" - //"github.com/btcsuite/btcutil/hdkeychain" - //"github.com/mndrix/btcutil" - //"github.com/tyler-smith/go-bip32" - "github.com/tendermint/go-crypto" ) @@ -84,7 +76,7 @@ func TestHDToAddr(t *testing.T) { copy(pubT[:], pub) addr := pubT.Address() fmt.Printf("ADDR \t%X %X\n", addrB, addr) - assert.Equal(t, addr, addrB, fmt.Sprintf("Expected addresses to match %d", i)) + assert.Equal(t, addr, crypto.Address(addrB), fmt.Sprintf("Expected addresses to match %d", i)) } } diff --git a/keys/keybase.go b/keys/keybase.go index 7f53b2979..c68564d5a 100644 --- a/keys/keybase.go +++ b/keys/keybase.go @@ -86,7 +86,9 @@ func (kb dbKeybase) Recover(name, passphrase, seedphrase string) (Info, error) { // List loads the keys from the storage and enforces alphabetical order func (kb dbKeybase) List() ([]Info, error) { var res []Info - for iter := kb.db.Iterator(); iter.Valid(); iter.Next() { + iter := kb.db.Iterator(nil, nil) + defer iter.Close() + for ; iter.Valid(); iter.Next() { key := iter.Key() if isPub(key) { info, err := readInfo(iter.Value()) diff --git a/pub_key.go b/pub_key.go index dfa012969..cda01a9df 100644 --- a/pub_key.go +++ b/pub_key.go @@ -3,16 +3,21 @@ package crypto import ( "bytes" "crypto/sha256" + "fmt" secp256k1 "github.com/btcsuite/btcd/btcec" "github.com/tendermint/ed25519" "github.com/tendermint/ed25519/extra25519" "github.com/tendermint/go-wire" data "github.com/tendermint/go-wire/data" - . "github.com/tendermint/tmlibs/common" + cmn "github.com/tendermint/tmlibs/common" "golang.org/x/crypto/ripemd160" ) +// An address is a []byte, but hex-encoded even in JSON. +// []byte leaves us the option to change the address length. +type Address cmn.HexBytes + func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) { if err := wire.ReadBinaryBytes(pubKeyBytes, &pubKey); err != nil { return PubKey{}, err @@ -27,7 +32,7 @@ func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) { // +gen wrapper:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1],ed25519,secp256k1" type PubKeyInner interface { AssertIsPubKeyInner() - Address() []byte + Address() Address Bytes() []byte KeyString() string VerifyBytes(msg []byte, sig Signature) bool @@ -44,17 +49,17 @@ type PubKeyEd25519 [32]byte func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {} -func (pubKey PubKeyEd25519) Address() []byte { +func (pubKey PubKeyEd25519) Address() Address { w, n, err := new(bytes.Buffer), new(int), new(error) wire.WriteBinary(pubKey[:], w, n, err) if *err != nil { - PanicCrisis(*err) + panic(*err) } // append type byte encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...) hasher := ripemd160.New() hasher.Write(encodedPubkey) // does not error - return hasher.Sum(nil) + return Address(hasher.Sum(nil)) } func (pubKey PubKeyEd25519) Bytes() []byte { @@ -95,13 +100,13 @@ func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte { } func (pubKey PubKeyEd25519) String() string { - return Fmt("PubKeyEd25519{%X}", pubKey[:]) + return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:]) } // Must return the full bytes in hex. // Used for map keying, etc. func (pubKey PubKeyEd25519) KeyString() string { - return Fmt("%X", pubKey[:]) + return fmt.Sprintf("%X", pubKey[:]) } func (pubKey PubKeyEd25519) Equals(other PubKey) bool { @@ -124,14 +129,14 @@ type PubKeySecp256k1 [33]byte func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {} // Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey)) -func (pubKey PubKeySecp256k1) Address() []byte { +func (pubKey PubKeySecp256k1) Address() Address { hasherSHA256 := sha256.New() hasherSHA256.Write(pubKey[:]) // does not error sha := hasherSHA256.Sum(nil) hasherRIPEMD160 := ripemd160.New() hasherRIPEMD160.Write(sha) // does not error - return hasherRIPEMD160.Sum(nil) + return Address(hasherRIPEMD160.Sum(nil)) } func (pubKey PubKeySecp256k1) Bytes() []byte { @@ -168,13 +173,13 @@ func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error { } func (pubKey PubKeySecp256k1) String() string { - return Fmt("PubKeySecp256k1{%X}", pubKey[:]) + return fmt.Sprintf("PubKeySecp256k1{%X}", pubKey[:]) } // Must return the full bytes in hex. // Used for map keying, etc. func (pubKey PubKeySecp256k1) KeyString() string { - return Fmt("%X", pubKey[:]) + return fmt.Sprintf("%X", pubKey[:]) } func (pubKey PubKeySecp256k1) Equals(other PubKey) bool { diff --git a/pub_key_test.go b/pub_key_test.go index 097c5ea3b..95c82534d 100644 --- a/pub_key_test.go +++ b/pub_key_test.go @@ -27,7 +27,8 @@ func TestPubKeySecp256k1Address(t *testing.T) { for _, d := range secpDataTable { privB, _ := hex.DecodeString(d.priv) pubB, _ := hex.DecodeString(d.pub) - addrB, _, _ := base58.CheckDecode(d.addr) + addrBbz, _, _ := base58.CheckDecode(d.addr) + addrB := Address(addrBbz) var priv PrivKeySecp256k1 copy(priv[:], privB)