From 87cb57c3e5e76cab8b766253c218ec7b68ecec09 Mon Sep 17 00:00:00 2001 From: Zach Ramsay Date: Wed, 4 Oct 2017 18:16:48 -0400 Subject: [PATCH] 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 4114de74..b1dc51ae 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 b04afe08..165b1e15 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 b6532e35..5b664b49 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 60d77ad6..fcb348b5 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 f9e5f5bb..f4d82583 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 0ef4ae4b..434966f4 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 4820c937..3fb94731 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 cae3e451..80852802 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 cad41564..2aa17753 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 d8058a5b..e33ca500 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 403a0131..6464b130 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 915dda10..58ff4118 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 (