From fe66a683bcbb28d25a07299e45741a835cd13dbd Mon Sep 17 00:00:00 2001 From: Zach Ramsay Date: Fri, 22 Sep 2017 13:31:01 -0400 Subject: [PATCH 1/7] linter: add metalinter to Makefile & apply some fixes --- Makefile | 38 ++++++++++++- _gen.go | 6 -- armor.go | 2 +- bcrypt/bcrypt.go | 2 +- hash.go | 1 + hd/address.go | 19 +------ hd/hd_test.go | 97 +------------------------------- keys/cryptostore/holder.go | 2 + keys/server/helpers.go | 4 +- keys/storage/filestorage/main.go | 5 +- keys/storage/memstorage/main.go | 1 + keys/wordcodec_test.go | 4 +- keys/wordlist/wordlist.go | 15 ++--- 13 files changed, 65 insertions(+), 131 deletions(-) delete mode 100644 _gen.go diff --git a/Makefile b/Makefile index 0f414e1c2..cdfee009b 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,9 @@ GOTOOLS = \ github.com/Masterminds/glide \ - github.com/jteeuwen/go-bindata/go-bindata + github.com/jteeuwen/go-bindata/go-bindata \ + github.com/alecthomas/gometalinter + REPO:=github.com/tendermint/go-crypto all: get_vendor_deps test @@ -31,3 +33,37 @@ codegen: @echo "--> regenerating all interface wrappers" @gen @echo "Done!" + +metalinter: ensure_tools + @gometalinter --install + gometalinter --vendor --deadline=600s --enable-all --disable=lll ./... + +metalinter_test: ensure_tools + @gometalinter --install + gometalinter --vendor --deadline=600s --disable-all \ + --enable=aligncheck \ + --enable=deadcode \ + --enable=gas \ + --enable=goconst \ + --enable=gocyclo \ + --enable=goimports \ + --enable=gosimple \ + --enable=gotype \ + --enable=ineffassign \ + --enable=interfacer \ + --enable=megacheck \ + --enable=misspell \ + --enable=safesql \ + --enable=staticcheck \ + --enable=structcheck \ + --enable=unconvert \ + --enable=unused \ + --enable=vetshadow \ + --enable=vet \ + --enable=unparam \ + --enable=varcheck \ + ./... + + #--enable=dupl \ + #--enable=errcheck \ + #--enable=golint \ <== comments on anything exported diff --git a/_gen.go b/_gen.go deleted file mode 100644 index a98feaf4e..000000000 --- a/_gen.go +++ /dev/null @@ -1,6 +0,0 @@ -package main - -import ( - _ "github.com/tendermint/go-wire/gen" - _ "github.com/clipperhouse/stringer" -) diff --git a/armor.go b/armor.go index 3d2eff5e7..5f199df43 100644 --- a/armor.go +++ b/armor.go @@ -22,7 +22,7 @@ func EncodeArmor(blockType string, headers map[string]string, data []byte) strin if err != nil { PanicSanity("Error encoding ascii armor: " + err.Error()) } - return string(buf.Bytes()) + return buf.String() } func DecodeArmor(armorStr string) (blockType string, headers map[string]string, data []byte, err error) { diff --git a/bcrypt/bcrypt.go b/bcrypt/bcrypt.go index a6b4a2cf6..6b23b7a9d 100644 --- a/bcrypt/bcrypt.go +++ b/bcrypt/bcrypt.go @@ -50,7 +50,7 @@ func (ih InvalidHashPrefixError) Error() string { type InvalidCostError int func (ic InvalidCostError) Error() string { - return fmt.Sprintf("crypto/bcrypt: cost %d is outside allowed range (%d,%d)", int(ic), int(MinCost), int(MaxCost)) + return fmt.Sprintf("crypto/bcrypt: cost %d is outside allowed range (%d,%d)", int(ic), int(MinCost), int(MaxCost)) // nolint: unconvert } const ( diff --git a/hash.go b/hash.go index 165b1e153..b04afe085 100644 --- a/hash.go +++ b/hash.go @@ -1,3 +1,4 @@ +// nolint: goimports package crypto import ( diff --git a/hd/address.go b/hd/address.go index d7553a4d7..b6532e351 100644 --- a/hd/address.go +++ b/hd/address.go @@ -11,7 +11,6 @@ import ( "errors" "fmt" "hash" - "log" "math/big" "strconv" "strings" @@ -88,18 +87,6 @@ func ComputeTxId(rawTxHex string) string { // Private methods... -func printKeyInfo(privKeyBytes []byte, pubKeyBytes []byte, chain []byte) { - if pubKeyBytes == nil { - pubKeyBytes = PubKeyBytesFromPrivKeyBytes(privKeyBytes, true) - } - addr := AddrFromPubKeyBytes(pubKeyBytes) - log.Println("\nprikey:\t%v\npubKeyBytes:\t%v\naddr:\t%v\nchain:\t%v", - HexEncode(privKeyBytes), - HexEncode(pubKeyBytes), - addr, - HexEncode(chain)) -} - func DerivePrivateKeyForPath(privKeyBytes []byte, chain []byte, path string) []byte { data := privKeyBytes parts := strings.Split(path, "/") @@ -144,7 +131,7 @@ func DerivePublicKeyForPath(pubKeyBytes []byte, chain []byte, path string) []byt } func DerivePrivateKey(privKeyBytes []byte, chain []byte, i uint32, prime bool) ([]byte, []byte) { - data := []byte{} + data := []byte{} // nolint [ megacheck, deadcode ] if prime { i = i | 0x80000000 data = append([]byte{byte(0)}, privKeyBytes...) @@ -177,7 +164,7 @@ func addPoints(a []byte, b []byte) []byte { panic(err) } sumX, sumY := btcec.S256().Add(ap.X, ap.Y, bp.X, bp.Y) - sum := (*btcec.PublicKey)(&btcec.PublicKey{ + sum := (*btcec.PublicKey)(&btcec.PublicKey{ // nolint: unconvert Curve: btcec.S256(), X: sumX, Y: sumY, @@ -248,7 +235,7 @@ func WIFFromPrivKeyBytes(privKeyBytes []byte, compress bool) string { func PubKeyBytesFromPrivKeyBytes(privKeyBytes []byte, compress bool) (pubKeyBytes []byte) { x, y := btcec.S256().ScalarBaseMult(privKeyBytes) - pub := (*btcec.PublicKey)(&btcec.PublicKey{ + pub := (*btcec.PublicKey)(&btcec.PublicKey{ // nolint: unconvert Curve: btcec.S256(), X: x, Y: y, diff --git a/hd/hd_test.go b/hd/hd_test.go index b2f7d2e8f..60d77ad66 100644 --- a/hd/hd_test.go +++ b/hd/hd_test.go @@ -1,10 +1,8 @@ +// nolint: goimports package hd import ( "bytes" - "crypto/hmac" - "crypto/sha512" - "encoding/binary" "encoding/hex" "encoding/json" "fmt" @@ -15,11 +13,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" ) @@ -33,7 +26,7 @@ type addrData struct { } // NOTE: atom fundraiser address -var hdPath string = "m/44'/118'/0'/0/0" +// var hdPath string = "m/44'/118'/0'/0/0" var hdToAddrTable []addrData func init() { @@ -109,13 +102,6 @@ func TestReverseBytes(t *testing.T) { } } -func ifExit(err error, n int) { - if err != nil { - fmt.Println(n, err) - os.Exit(1) - } -} - func gocrypto(seed []byte) ([]byte, []byte, []byte) { _, priv, ch, _ := ComputeMastersFromSeed(string(seed)) @@ -131,83 +117,6 @@ func gocrypto(seed []byte) ([]byte, []byte, []byte) { return HexDecode(priv), privBytes, pubBytes } -func btcsuite(seed []byte) ([]byte, []byte, []byte) { - fmt.Println("HD") - masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams) - if err != nil { - hmac := hmac.New(sha512.New, []byte("Bitcoin seed")) - hmac.Write([]byte(seed)) - intermediary := hmac.Sum(nil) - - curve := btcutil.Secp256k1() - curveParams := curve.Params() - - // Split it into our key and chain code - keyBytes := intermediary[:32] - fmt.Printf("\t%X\n", keyBytes) - fmt.Printf("\t%X\n", curveParams.N.Bytes()) - keyInt, _ := binary.ReadVarint(bytes.NewBuffer(keyBytes)) - fmt.Printf("\t%d\n", keyInt) - } - fh := hdkeychain.HardenedKeyStart - k, err := masterKey.Child(uint32(fh + 44)) - ifExit(err, 44) - k, err = k.Child(uint32(fh + 118)) - ifExit(err, 118) - k, err = k.Child(uint32(fh + 0)) - ifExit(err, 1) - k, err = k.Child(uint32(0)) - ifExit(err, 2) - k, err = k.Child(uint32(0)) - ifExit(err, 3) - ecpriv, err := k.ECPrivKey() - ifExit(err, 10) - ecpub, err := k.ECPubKey() - ifExit(err, 11) - - priv := ecpriv.Serialize() - pub := ecpub.SerializeCompressed() - mkey, _ := masterKey.ECPrivKey() - return mkey.Serialize(), priv, pub -} - -// return priv and pub -func tylerSmith(seed []byte) ([]byte, []byte, []byte) { - masterKey, err := bip32.NewMasterKey(seed) - if err != nil { - hmac := hmac.New(sha512.New, []byte("Bitcoin seed")) - hmac.Write([]byte(seed)) - intermediary := hmac.Sum(nil) - - curve := btcutil.Secp256k1() - curveParams := curve.Params() - - // Split it into our key and chain code - keyBytes := intermediary[:32] - fmt.Printf("\t%X\n", keyBytes) - fmt.Printf("\t%X\n", curveParams.N.Bytes()) - keyInt, _ := binary.ReadVarint(bytes.NewBuffer(keyBytes)) - fmt.Printf("\t%d\n", keyInt) - - } - ifExit(err, 0) - fh := bip32.FirstHardenedChild - k, err := masterKey.NewChildKey(fh + 44) - ifExit(err, 44) - k, err = k.NewChildKey(fh + 118) - ifExit(err, 118) - k, err = k.NewChildKey(fh + 0) - ifExit(err, 1) - k, err = k.NewChildKey(0) - ifExit(err, 2) - k, err = k.NewChildKey(0) - ifExit(err, 3) - - priv := k.Key - pub := k.PublicKey().Key - return masterKey.Key, priv, pub -} - // Benchmarks var revBytesCases = [][]byte{ @@ -237,6 +146,6 @@ func BenchmarkReverseBytes(b *testing.B) { // sink is necessary to ensure if the compiler tries // to smart, that it won't optimize away the benchmarks. - if sink != nil { + if sink != nil { // nolint: megacheck } } diff --git a/keys/cryptostore/holder.go b/keys/cryptostore/holder.go index 18437a9b5..f9e5f5bb3 100644 --- a/keys/cryptostore/holder.go +++ b/keys/cryptostore/holder.go @@ -25,11 +25,13 @@ func New(coder Encoder, store keys.Storage, codec keys.Codec) Manager { } // exists just to make sure we fulfill the Signer interface +// nolint [ megacheck, deadcode ] func (s Manager) assertSigner() keys.Signer { return s } // exists just to make sure we fulfill the Manager interface +// nolint [ megacheck, deadcode ] func (s Manager) assertKeyManager() keys.Manager { return s } diff --git a/keys/server/helpers.go b/keys/server/helpers.go index 710e4f392..4820c937e 100644 --- a/keys/server/helpers.go +++ b/keys/server/helpers.go @@ -5,6 +5,8 @@ for key management, transaction signing, and query validation. Please read the README and godoc to see how to configure the server for your application. */ + +// nolint: goimports package server import ( @@ -12,8 +14,8 @@ import ( "io/ioutil" "net/http" - data "github.com/tendermint/go-wire/data" "github.com/tendermint/go-crypto/keys/server/types" + data "github.com/tendermint/go-wire/data" "github.com/pkg/errors" ) diff --git a/keys/storage/filestorage/main.go b/keys/storage/filestorage/main.go index 696b200fc..d8058a5b9 100644 --- a/keys/storage/filestorage/main.go +++ b/keys/storage/filestorage/main.go @@ -22,8 +22,8 @@ const ( PrivExt = "tlc" PubExt = "pub" keyPerm = os.FileMode(0600) - pubPerm = os.FileMode(0644) - dirPerm = os.FileMode(0700) + // pubPerm = os.FileMode(0644) + dirPerm = os.FileMode(0700) ) type FileStore struct { @@ -43,6 +43,7 @@ func New(dir string) FileStore { } // assertStorage just makes sure we implement the proper Storage interface +// nolint [ megacheck, deadcode ] func (s FileStore) assertStorage() keys.Storage { return s } diff --git a/keys/storage/memstorage/main.go b/keys/storage/memstorage/main.go index 195fa7a17..403a01318 100644 --- a/keys/storage/memstorage/main.go +++ b/keys/storage/memstorage/main.go @@ -23,6 +23,7 @@ func New() MemStore { } // assertStorage just makes sure we implement the Storage interface +// nolint [ megacheck, deadcode ] func (s MemStore) assertStorage() keys.Storage { return s } diff --git a/keys/wordcodec_test.go b/keys/wordcodec_test.go index 25c5439a6..a44607be2 100644 --- a/keys/wordcodec_test.go +++ b/keys/wordcodec_test.go @@ -119,8 +119,8 @@ func TestCheckInvalidLists(t *testing.T) { w, err := codec.BytesToWords(data) if tc.valid { assert.Nil(err, "%d: %+v", i, err) - b, err := codec.WordsToBytes(w) - assert.Nil(err, "%d: %+v", i, err) + b, err1 := codec.WordsToBytes(w) + assert.Nil(err1, "%d: %+v", i, err1) assert.Equal(data, b) } else { assert.NotNil(err, "%d", i) diff --git a/keys/wordlist/wordlist.go b/keys/wordlist/wordlist.go index 97ddb2369..915dda102 100644 --- a/keys/wordlist/wordlist.go +++ b/keys/wordlist/wordlist.go @@ -6,6 +6,7 @@ // keys/wordlist/spanish.txt // DO NOT EDIT! +// nolint: goimports package wordlist import ( @@ -204,9 +205,9 @@ func AssetNames() []string { // _bindata is a table, holding each asset generator, mapped to its name. var _bindata = map[string]func() (*asset, error){ "keys/wordlist/chinese_simplified.txt": keysWordlistChinese_simplifiedTxt, - "keys/wordlist/english.txt": keysWordlistEnglishTxt, - "keys/wordlist/japanese.txt": keysWordlistJapaneseTxt, - "keys/wordlist/spanish.txt": keysWordlistSpanishTxt, + "keys/wordlist/english.txt": keysWordlistEnglishTxt, + "keys/wordlist/japanese.txt": keysWordlistJapaneseTxt, + "keys/wordlist/spanish.txt": keysWordlistSpanishTxt, } // AssetDir returns the file names below a certain @@ -248,13 +249,14 @@ type bintree struct { Func func() (*asset, error) Children map[string]*bintree } + var _bintree = &bintree{nil, map[string]*bintree{ "keys": &bintree{nil, map[string]*bintree{ "wordlist": &bintree{nil, map[string]*bintree{ "chinese_simplified.txt": &bintree{keysWordlistChinese_simplifiedTxt, map[string]*bintree{}}, - "english.txt": &bintree{keysWordlistEnglishTxt, map[string]*bintree{}}, - "japanese.txt": &bintree{keysWordlistJapaneseTxt, map[string]*bintree{}}, - "spanish.txt": &bintree{keysWordlistSpanishTxt, map[string]*bintree{}}, + "english.txt": &bintree{keysWordlistEnglishTxt, map[string]*bintree{}}, + "japanese.txt": &bintree{keysWordlistJapaneseTxt, map[string]*bintree{}}, + "spanish.txt": &bintree{keysWordlistSpanishTxt, map[string]*bintree{}}, }}, }}, }} @@ -305,4 +307,3 @@ func _filePath(dir, name string) string { cannonicalName := strings.Replace(name, "\\", "/", -1) return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) } - From 7921fb0c05c61cf55ad8dca18d492cea741e29dc Mon Sep 17 00:00:00 2001 From: Zach Ramsay Date: Fri, 22 Sep 2017 13:35:02 -0400 Subject: [PATCH 2/7] linter: last fixes & add to circle --- Makefile | 2 +- keys/cryptostore/holder_test.go | 1 + keys/server/keys.go | 1 + keys/server/keys_test.go | 3 +++ 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cdfee009b..4114de740 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ GOTOOLS = \ REPO:=github.com/tendermint/go-crypto -all: get_vendor_deps test +all: get_vendor_deps metalinter_test test test: go test `glide novendor` diff --git a/keys/cryptostore/holder_test.go b/keys/cryptostore/holder_test.go index 434966f46..0ef4ae4ba 100644 --- a/keys/cryptostore/holder_test.go +++ b/keys/cryptostore/holder_test.go @@ -144,6 +144,7 @@ func TestKeyManagement(t *testing.T) { // } // } +// nolint: unparam func assertPassword(assert *assert.Assertions, cstore cryptostore.Manager, name, pass, badpass string) { err := cstore.Update(name, badpass, pass) assert.NotNil(err) diff --git a/keys/server/keys.go b/keys/server/keys.go index 80852802e..cae3e451d 100644 --- a/keys/server/keys.go +++ b/keys/server/keys.go @@ -52,6 +52,7 @@ func (k Keys) GetKey(w http.ResponseWriter, r *http.Request) { writeSuccess(w, &key) } +// nolint: unparam func (k Keys) ListKeys(w http.ResponseWriter, r *http.Request) { keys, err := k.manager.List() diff --git a/keys/server/keys_test.go b/keys/server/keys_test.go index 2aa17753c..cad415644 100644 --- a/keys/server/keys_test.go +++ b/keys/server/keys_test.go @@ -120,6 +120,7 @@ func listKeys(h http.Handler) (keys.Infos, int, error) { return data, rr.Code, err } +// nolint: unparam func getKey(h http.Handler, name string) (*keys.Info, int, error) { rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "/keys/"+name, nil) @@ -137,6 +138,7 @@ func getKey(h http.Handler, name string) (*keys.Info, int, error) { return &data, rr.Code, err } +// nolint: unparam func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyResponse, int, error) { rr := httptest.NewRecorder() post := types.CreateKeyRequest{ @@ -165,6 +167,7 @@ func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyR return data, rr.Code, err } +// nolint: unparam func deleteKey(h http.Handler, name, passphrase string) (*types.ErrorResponse, int, error) { rr := httptest.NewRecorder() post := types.DeleteKeyRequest{ From 87cb57c3e5e76cab8b766253c218ec7b68ecec09 Mon Sep 17 00:00:00 2001 From: Zach Ramsay Date: Wed, 4 Oct 2017 18:16:48 -0400 Subject: [PATCH 3/7] linting: fixup some stuffs --- Makefile | 4 +- hash.go | 1 - hd/address.go | 24 ++++++-- hd/hd_test.go | 96 +++++++++++++++++++++++++++++++- keys/cryptostore/holder.go | 5 +- keys/cryptostore/holder_test.go | 1 - keys/server/helpers.go | 1 - keys/server/keys.go | 1 - keys/server/keys_test.go | 3 - keys/storage/filestorage/main.go | 3 +- keys/storage/memstorage/main.go | 3 +- keys/wordlist/wordlist.go | 1 - 12 files changed, 121 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 4114de740..b1dc51ae0 100644 --- a/Makefile +++ b/Makefile @@ -46,9 +46,7 @@ metalinter_test: ensure_tools --enable=gas \ --enable=goconst \ --enable=gocyclo \ - --enable=goimports \ --enable=gosimple \ - --enable=gotype \ --enable=ineffassign \ --enable=interfacer \ --enable=megacheck \ @@ -66,4 +64,6 @@ metalinter_test: ensure_tools #--enable=dupl \ #--enable=errcheck \ + #--enable=goimports \ #--enable=golint \ <== comments on anything exported + #--enable=gotype \ diff --git a/hash.go b/hash.go index b04afe085..165b1e153 100644 --- a/hash.go +++ b/hash.go @@ -1,4 +1,3 @@ -// nolint: goimports package crypto import ( diff --git a/hd/address.go b/hd/address.go index b6532e351..5b664b496 100644 --- a/hd/address.go +++ b/hd/address.go @@ -85,7 +85,19 @@ func ComputeTxId(rawTxHex string) string { return HexEncode(ReverseBytes(CalcHash256(HexDecode(rawTxHex)))) } -// Private methods... +/* +func printKeyInfo(privKeyBytes []byte, pubKeyBytes []byte, chain []byte) { + if pubKeyBytes == nil { + pubKeyBytes = PubKeyBytesFromPrivKeyBytes(privKeyBytes, true) + } + addr := AddrFromPubKeyBytes(pubKeyBytes) + log.Println("\nprikey:\t%v\npubKeyBytes:\t%v\naddr:\t%v\nchain:\t%v", + HexEncode(privKeyBytes), + HexEncode(pubKeyBytes), + addr, + HexEncode(chain)) +} +*/ func DerivePrivateKeyForPath(privKeyBytes []byte, chain []byte, path string) []byte { data := privKeyBytes @@ -131,7 +143,7 @@ func DerivePublicKeyForPath(pubKeyBytes []byte, chain []byte, path string) []byt } func DerivePrivateKey(privKeyBytes []byte, chain []byte, i uint32, prime bool) ([]byte, []byte) { - data := []byte{} // nolint [ megacheck, deadcode ] + var data []byte if prime { i = i | 0x80000000 data = append([]byte{byte(0)}, privKeyBytes...) @@ -164,11 +176,11 @@ func addPoints(a []byte, b []byte) []byte { panic(err) } sumX, sumY := btcec.S256().Add(ap.X, ap.Y, bp.X, bp.Y) - sum := (*btcec.PublicKey)(&btcec.PublicKey{ // nolint: unconvert + sum := &btcec.PublicKey{ Curve: btcec.S256(), X: sumX, Y: sumY, - }) + } return sum.SerializeCompressed() } @@ -235,11 +247,11 @@ func WIFFromPrivKeyBytes(privKeyBytes []byte, compress bool) string { func PubKeyBytesFromPrivKeyBytes(privKeyBytes []byte, compress bool) (pubKeyBytes []byte) { x, y := btcec.S256().ScalarBaseMult(privKeyBytes) - pub := (*btcec.PublicKey)(&btcec.PublicKey{ // nolint: unconvert + pub := &btcec.PublicKey{ Curve: btcec.S256(), X: x, Y: y, - }) + } if compress { return pub.SerializeCompressed() diff --git a/hd/hd_test.go b/hd/hd_test.go index 60d77ad66..fcb348b5c 100644 --- a/hd/hd_test.go +++ b/hd/hd_test.go @@ -1,8 +1,10 @@ -// nolint: goimports package hd import ( "bytes" + "crypto/hmac" + "crypto/sha512" + "encoding/binary" "encoding/hex" "encoding/json" "fmt" @@ -13,6 +15,11 @@ 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" ) @@ -102,6 +109,13 @@ func TestReverseBytes(t *testing.T) { } } +func ifExit(err error, n int) { + if err != nil { + fmt.Println(n, err) + os.Exit(1) + } +} + func gocrypto(seed []byte) ([]byte, []byte, []byte) { _, priv, ch, _ := ComputeMastersFromSeed(string(seed)) @@ -117,6 +131,83 @@ func gocrypto(seed []byte) ([]byte, []byte, []byte) { return HexDecode(priv), privBytes, pubBytes } +func btcsuite(seed []byte) ([]byte, []byte, []byte) { + fmt.Println("HD") + masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams) + if err != nil { + hmac := hmac.New(sha512.New, []byte("Bitcoin seed")) + hmac.Write([]byte(seed)) + intermediary := hmac.Sum(nil) + + curve := btcutil.Secp256k1() + curveParams := curve.Params() + + // Split it into our key and chain code + keyBytes := intermediary[:32] + fmt.Printf("\t%X\n", keyBytes) + fmt.Printf("\t%X\n", curveParams.N.Bytes()) + keyInt, _ := binary.ReadVarint(bytes.NewBuffer(keyBytes)) + fmt.Printf("\t%d\n", keyInt) + } + fh := hdkeychain.HardenedKeyStart + k, err := masterKey.Child(uint32(fh + 44)) + ifExit(err, 44) + k, err = k.Child(uint32(fh + 118)) + ifExit(err, 118) + k, err = k.Child(uint32(fh + 0)) + ifExit(err, 1) + k, err = k.Child(uint32(0)) + ifExit(err, 2) + k, err = k.Child(uint32(0)) + ifExit(err, 3) + ecpriv, err := k.ECPrivKey() + ifExit(err, 10) + ecpub, err := k.ECPubKey() + ifExit(err, 11) + + priv := ecpriv.Serialize() + pub := ecpub.SerializeCompressed() + mkey, _ := masterKey.ECPrivKey() + return mkey.Serialize(), priv, pub +} + +// return priv and pub +func tylerSmith(seed []byte) ([]byte, []byte, []byte) { + masterKey, err := bip32.NewMasterKey(seed) + if err != nil { + hmac := hmac.New(sha512.New, []byte("Bitcoin seed")) + hmac.Write([]byte(seed)) + intermediary := hmac.Sum(nil) + + curve := btcutil.Secp256k1() + curveParams := curve.Params() + + // Split it into our key and chain code + keyBytes := intermediary[:32] + fmt.Printf("\t%X\n", keyBytes) + fmt.Printf("\t%X\n", curveParams.N.Bytes()) + keyInt, _ := binary.ReadVarint(bytes.NewBuffer(keyBytes)) + fmt.Printf("\t%d\n", keyInt) + + } + ifExit(err, 0) + fh := bip32.FirstHardenedChild + k, err := masterKey.NewChildKey(fh + 44) + ifExit(err, 44) + k, err = k.NewChildKey(fh + 118) + ifExit(err, 118) + k, err = k.NewChildKey(fh + 0) + ifExit(err, 1) + k, err = k.NewChildKey(0) + ifExit(err, 2) + k, err = k.NewChildKey(0) + ifExit(err, 3) + + priv := k.Key + pub := k.PublicKey().Key + return masterKey.Key, priv, pub +} + // Benchmarks var revBytesCases = [][]byte{ @@ -146,6 +237,7 @@ func BenchmarkReverseBytes(b *testing.B) { // sink is necessary to ensure if the compiler tries // to smart, that it won't optimize away the benchmarks. - if sink != nil { // nolint: megacheck + if sink != nil { + _ = sink } } diff --git a/keys/cryptostore/holder.go b/keys/cryptostore/holder.go index f9e5f5bb3..f4d82583b 100644 --- a/keys/cryptostore/holder.go +++ b/keys/cryptostore/holder.go @@ -24,14 +24,15 @@ func New(coder Encoder, store keys.Storage, codec keys.Codec) Manager { } } +var _ keys.Signer = Manager{} +var _ keys.Manager = Manager{} + // exists just to make sure we fulfill the Signer interface -// nolint [ megacheck, deadcode ] func (s Manager) assertSigner() keys.Signer { return s } // exists just to make sure we fulfill the Manager interface -// nolint [ megacheck, deadcode ] func (s Manager) assertKeyManager() keys.Manager { return s } diff --git a/keys/cryptostore/holder_test.go b/keys/cryptostore/holder_test.go index 0ef4ae4ba..434966f46 100644 --- a/keys/cryptostore/holder_test.go +++ b/keys/cryptostore/holder_test.go @@ -144,7 +144,6 @@ func TestKeyManagement(t *testing.T) { // } // } -// nolint: unparam func assertPassword(assert *assert.Assertions, cstore cryptostore.Manager, name, pass, badpass string) { err := cstore.Update(name, badpass, pass) assert.NotNil(err) diff --git a/keys/server/helpers.go b/keys/server/helpers.go index 4820c937e..3fb947316 100644 --- a/keys/server/helpers.go +++ b/keys/server/helpers.go @@ -6,7 +6,6 @@ Please read the README and godoc to see how to configure the server for your application. */ -// nolint: goimports package server import ( diff --git a/keys/server/keys.go b/keys/server/keys.go index cae3e451d..80852802e 100644 --- a/keys/server/keys.go +++ b/keys/server/keys.go @@ -52,7 +52,6 @@ func (k Keys) GetKey(w http.ResponseWriter, r *http.Request) { writeSuccess(w, &key) } -// nolint: unparam func (k Keys) ListKeys(w http.ResponseWriter, r *http.Request) { keys, err := k.manager.List() diff --git a/keys/server/keys_test.go b/keys/server/keys_test.go index cad415644..2aa17753c 100644 --- a/keys/server/keys_test.go +++ b/keys/server/keys_test.go @@ -120,7 +120,6 @@ func listKeys(h http.Handler) (keys.Infos, int, error) { return data, rr.Code, err } -// nolint: unparam func getKey(h http.Handler, name string) (*keys.Info, int, error) { rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "/keys/"+name, nil) @@ -138,7 +137,6 @@ func getKey(h http.Handler, name string) (*keys.Info, int, error) { return &data, rr.Code, err } -// nolint: unparam func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyResponse, int, error) { rr := httptest.NewRecorder() post := types.CreateKeyRequest{ @@ -167,7 +165,6 @@ func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyR return data, rr.Code, err } -// nolint: unparam func deleteKey(h http.Handler, name, passphrase string) (*types.ErrorResponse, int, error) { rr := httptest.NewRecorder() post := types.DeleteKeyRequest{ diff --git a/keys/storage/filestorage/main.go b/keys/storage/filestorage/main.go index d8058a5b9..e33ca5000 100644 --- a/keys/storage/filestorage/main.go +++ b/keys/storage/filestorage/main.go @@ -42,8 +42,9 @@ func New(dir string) FileStore { return FileStore{dir} } +var _ keys.Storage = FileStore{} + // assertStorage just makes sure we implement the proper Storage interface -// nolint [ megacheck, deadcode ] func (s FileStore) assertStorage() keys.Storage { return s } diff --git a/keys/storage/memstorage/main.go b/keys/storage/memstorage/main.go index 403a01318..6464b1308 100644 --- a/keys/storage/memstorage/main.go +++ b/keys/storage/memstorage/main.go @@ -22,8 +22,9 @@ func New() MemStore { return MemStore{} } +var _ keys.Storage = MemStore{} + // assertStorage just makes sure we implement the Storage interface -// nolint [ megacheck, deadcode ] func (s MemStore) assertStorage() keys.Storage { return s } diff --git a/keys/wordlist/wordlist.go b/keys/wordlist/wordlist.go index 915dda102..58ff41181 100644 --- a/keys/wordlist/wordlist.go +++ b/keys/wordlist/wordlist.go @@ -6,7 +6,6 @@ // keys/wordlist/spanish.txt // DO NOT EDIT! -// nolint: goimports package wordlist import ( From 796024f42f215b85ace955538ccbb5c2acddb8ea Mon Sep 17 00:00:00 2001 From: Zach Ramsay Date: Wed, 4 Oct 2017 18:27:35 -0400 Subject: [PATCH 4/7] linting: little more fixes --- Makefile | 2 +- example_test.go | 10 ++++++---- hd/hd_test.go | 19 +++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index b1dc51ae0..c7d11ce0a 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,6 @@ metalinter_test: ensure_tools --enable=unused \ --enable=vetshadow \ --enable=vet \ - --enable=unparam \ --enable=varcheck \ ./... @@ -67,3 +66,4 @@ metalinter_test: ensure_tools #--enable=goimports \ #--enable=golint \ <== comments on anything exported #--enable=gotype \ + #--enable=unparam \ diff --git a/example_test.go b/example_test.go index c9cd2688d..fff49c84d 100644 --- a/example_test.go +++ b/example_test.go @@ -15,21 +15,23 @@ package crypto_test import ( - "fmt" +//"fmt" - "github.com/tendermint/go-crypto" +//"github.com/tendermint/go-crypto" ) -func Example_Sha256() { +/* +func example_Sha256() { sum := crypto.Sha256([]byte("This is Tendermint")) fmt.Printf("%x\n", sum) // Output: // f91afb642f3d1c87c17eb01aae5cb65c242dfdbe7cf1066cc260f4ce5d33b94e } -func Example_Ripemd160() { +func example_Ripemd160() { sum := crypto.Ripemd160([]byte("This is Tendermint")) fmt.Printf("%x\n", sum) // Output: // 051e22663e8f0fd2f2302f1210f954adff009005 } +*/ diff --git a/hd/hd_test.go b/hd/hd_test.go index fcb348b5c..02d80388c 100644 --- a/hd/hd_test.go +++ b/hd/hd_test.go @@ -2,9 +2,9 @@ package hd import ( "bytes" - "crypto/hmac" - "crypto/sha512" - "encoding/binary" + //"crypto/hmac" + //"crypto/sha512" + //"encoding/binary" "encoding/hex" "encoding/json" "fmt" @@ -15,10 +15,10 @@ 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/btcsuite/btcd/chaincfg" + //"github.com/btcsuite/btcutil/hdkeychain" + //"github.com/mndrix/btcutil" + //"github.com/tyler-smith/go-bip32" "github.com/tendermint/go-crypto" ) @@ -109,12 +109,14 @@ func TestReverseBytes(t *testing.T) { } } +/* func ifExit(err error, n int) { if err != nil { fmt.Println(n, err) os.Exit(1) } } +*/ func gocrypto(seed []byte) ([]byte, []byte, []byte) { @@ -131,6 +133,7 @@ func gocrypto(seed []byte) ([]byte, []byte, []byte) { return HexDecode(priv), privBytes, pubBytes } +/* func btcsuite(seed []byte) ([]byte, []byte, []byte) { fmt.Println("HD") masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams) @@ -207,9 +210,9 @@ func tylerSmith(seed []byte) ([]byte, []byte, []byte) { pub := k.PublicKey().Key return masterKey.Key, priv, pub } +*/ // Benchmarks - var revBytesCases = [][]byte{ nil, []byte(""), From 1775be1cd93549a48bc3453adb4cfb96fa8d1c60 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Oct 2017 22:19:48 -0400 Subject: [PATCH 5/7] remove some assertXxx funcs --- keys/cryptostore/holder.go | 11 +---------- keys/storage/filestorage/main.go | 6 +----- keys/storage/memstorage/main.go | 6 +----- 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/keys/cryptostore/holder.go b/keys/cryptostore/holder.go index f4d82583b..44332f175 100644 --- a/keys/cryptostore/holder.go +++ b/keys/cryptostore/holder.go @@ -24,19 +24,10 @@ func New(coder Encoder, store keys.Storage, codec keys.Codec) Manager { } } +// assert Manager satisfies keys.Signer and keys.Manager interfaces var _ keys.Signer = Manager{} var _ keys.Manager = Manager{} -// exists just to make sure we fulfill the Signer interface -func (s Manager) assertSigner() keys.Signer { - return s -} - -// exists just to make sure we fulfill the Manager interface -func (s Manager) assertKeyManager() keys.Manager { - return s -} - // Create adds a new key to the storage engine, returning error if // another key already stored under this name // diff --git a/keys/storage/filestorage/main.go b/keys/storage/filestorage/main.go index e33ca5000..bed80efb3 100644 --- a/keys/storage/filestorage/main.go +++ b/keys/storage/filestorage/main.go @@ -42,13 +42,9 @@ func New(dir string) FileStore { return FileStore{dir} } +// assert FileStore satisfies keys.Storage var _ keys.Storage = FileStore{} -// assertStorage just makes sure we implement the proper Storage interface -func (s FileStore) assertStorage() keys.Storage { - return s -} - // Put creates two files, one with the public info as json, the other // with the (encoded) private key as gpg ascii-armor style func (s FileStore) Put(name string, key []byte, info keys.Info) error { diff --git a/keys/storage/memstorage/main.go b/keys/storage/memstorage/main.go index 6464b1308..a988fe0ff 100644 --- a/keys/storage/memstorage/main.go +++ b/keys/storage/memstorage/main.go @@ -22,13 +22,9 @@ func New() MemStore { return MemStore{} } +// assert MemStore satisfies keys.Storage var _ keys.Storage = MemStore{} -// assertStorage just makes sure we implement the Storage interface -func (s MemStore) assertStorage() keys.Storage { - return s -} - // Put adds the given key, returns an error if it another key // is already stored under this name func (s MemStore) Put(name string, key []byte, info keys.Info) error { From 32dec98c1c78f9b0cb326e10e167a2d1cf12c515 Mon Sep 17 00:00:00 2001 From: Zach Ramsay Date: Tue, 10 Oct 2017 17:33:51 -0400 Subject: [PATCH 6/7] example: fix func suffix --- example_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/example_test.go b/example_test.go index fff49c84d..2c8b94549 100644 --- a/example_test.go +++ b/example_test.go @@ -15,23 +15,21 @@ package crypto_test import ( -//"fmt" + "fmt" -//"github.com/tendermint/go-crypto" + "github.com/tendermint/go-crypto" ) -/* -func example_Sha256() { +func ExampleSha256() { sum := crypto.Sha256([]byte("This is Tendermint")) fmt.Printf("%x\n", sum) // Output: // f91afb642f3d1c87c17eb01aae5cb65c242dfdbe7cf1066cc260f4ce5d33b94e } -func example_Ripemd160() { +func ExampleRipemd160() { sum := crypto.Ripemd160([]byte("This is Tendermint")) fmt.Printf("%x\n", sum) // Output: // 051e22663e8f0fd2f2302f1210f954adff009005 } -*/ From 47d3fa47419156c3c2f0c6e83d6eb4dfded0eb42 Mon Sep 17 00:00:00 2001 From: Zach Ramsay Date: Mon, 23 Oct 2017 08:30:18 -0400 Subject: [PATCH 7/7] linting: a few fixes --- Makefile | 2 +- embed_test.go | 8 ++++---- keys/cryptostore/holder_test.go | 14 +++++++------- keys/storage/filestorage/main.go | 15 +++++---------- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index c7d11ce0a..f775704a9 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,6 @@ metalinter: ensure_tools metalinter_test: ensure_tools @gometalinter --install gometalinter --vendor --deadline=600s --disable-all \ - --enable=aligncheck \ --enable=deadcode \ --enable=gas \ --enable=goconst \ @@ -49,6 +48,7 @@ metalinter_test: ensure_tools --enable=gosimple \ --enable=ineffassign \ --enable=interfacer \ + --enable=maligned \ --enable=megacheck \ --enable=misspell \ --enable=safesql \ diff --git a/embed_test.go b/embed_test.go index e2d2fe504..e5c37c0c1 100644 --- a/embed_test.go +++ b/embed_test.go @@ -73,8 +73,8 @@ func TestEncodeDemo(t *testing.T) { // Try to encode as binary b, err := data.ToWire(tc.in) if assert.Nil(err, "%d: %#v", i, tc.in) { - err := data.FromWire(b, tc.out) - if assert.Nil(err) { + err2 := data.FromWire(b, tc.out) + if assert.Nil(err2) { assert.Equal(tc.expected, tc.out.String()) } } @@ -82,8 +82,8 @@ func TestEncodeDemo(t *testing.T) { // Try to encode it as json j, err := data.ToJSON(tc.in) if assert.Nil(err, "%d: %#v", i, tc.in) { - err := data.FromJSON(j, tc.out) - if assert.Nil(err) { + err2 := data.FromJSON(j, tc.out) + if assert.Nil(err2) { assert.Equal(tc.expected, tc.out.String()) } } diff --git a/keys/cryptostore/holder_test.go b/keys/cryptostore/holder_test.go index 7484b1517..a8fc90989 100644 --- a/keys/cryptostore/holder_test.go +++ b/keys/cryptostore/holder_test.go @@ -50,22 +50,22 @@ func TestKeyManagement(t *testing.T) { assert.NotNil(err) // list shows them in order - keys, err := cstore.List() + keyS, err := cstore.List() require.Nil(err) - require.Equal(2, len(keys)) + require.Equal(2, len(keyS)) // note these are in alphabetical order - assert.Equal(n2, keys[0].Name) - assert.Equal(n1, keys[1].Name) - assert.Equal(i2.PubKey, keys[0].PubKey) + assert.Equal(n2, keyS[0].Name) + assert.Equal(n1, keyS[1].Name) + assert.Equal(i2.PubKey, keyS[0].PubKey) // deleting a key removes it err = cstore.Delete("bad name", "foo") require.NotNil(err) err = cstore.Delete(n1, p1) require.Nil(err) - keys, err = cstore.List() + keyS, err = cstore.List() require.Nil(err) - assert.Equal(1, len(keys)) + assert.Equal(1, len(keyS)) _, err = cstore.Get(n1) assert.NotNil(err) diff --git a/keys/storage/filestorage/main.go b/keys/storage/filestorage/main.go index 70b7e6251..2bd4dcc1f 100644 --- a/keys/storage/filestorage/main.go +++ b/keys/storage/filestorage/main.go @@ -89,11 +89,10 @@ func (s FileStore) Get(name string) (salt []byte, key []byte, info keys.Info, er // Info for all keys located in this directory. func (s FileStore) List() (keys.Infos, error) { dir, err := os.Open(s.keyDir) - defer dir.Close() - if err != nil { return nil, errors.Wrap(err, "List Keys") } + defer dir.Close() names, err := dir.Readdirnames(0) if err != nil { @@ -141,11 +140,10 @@ func (s FileStore) nameToPaths(name string) (pub, priv string) { func readInfo(path string) (info keys.Info, err error) { f, err := os.Open(path) - defer f.Close() - if err != nil { return info, errors.Wrap(err, "Reading data") } + defer f.Close() d, err := ioutil.ReadAll(f) if err != nil { @@ -170,11 +168,10 @@ func readInfo(path string) (info keys.Info, err error) { func read(path string) (salt, key []byte, name string, err error) { f, err := os.Open(path) - defer f.Close() - if err != nil { return nil, nil, "", errors.Wrap(err, "Reading data") } + defer f.Close() d, err := ioutil.ReadAll(f) if err != nil { @@ -208,11 +205,10 @@ func read(path string) (salt, key []byte, name string, err error) { func writeInfo(path string, info keys.Info) error { f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, keyPerm) - defer f.Close() - if err != nil { return errors.Wrap(err, "Writing data") } + defer f.Close() headers := map[string]string{"name": info.Name} text := crypto.EncodeArmor(BlockType, headers, info.PubKey.Bytes()) @@ -223,11 +219,10 @@ func writeInfo(path string, info keys.Info) error { func write(path, name string, salt, key []byte) error { f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, keyPerm) - defer f.Close() - if err != nil { return errors.Wrap(err, "Writing data") } + defer f.Close() headers := map[string]string{ "name": name,