linting: fixup some stuffs

This commit is contained in:
Zach Ramsay
2017-10-04 18:16:48 -04:00
parent 7921fb0c05
commit 87cb57c3e5
12 changed files with 121 additions and 22 deletions

View File

@ -46,9 +46,7 @@ metalinter_test: ensure_tools
--enable=gas \ --enable=gas \
--enable=goconst \ --enable=goconst \
--enable=gocyclo \ --enable=gocyclo \
--enable=goimports \
--enable=gosimple \ --enable=gosimple \
--enable=gotype \
--enable=ineffassign \ --enable=ineffassign \
--enable=interfacer \ --enable=interfacer \
--enable=megacheck \ --enable=megacheck \
@ -66,4 +64,6 @@ metalinter_test: ensure_tools
#--enable=dupl \ #--enable=dupl \
#--enable=errcheck \ #--enable=errcheck \
#--enable=goimports \
#--enable=golint \ <== comments on anything exported #--enable=golint \ <== comments on anything exported
#--enable=gotype \

View File

@ -1,4 +1,3 @@
// nolint: goimports
package crypto package crypto
import ( import (

View File

@ -85,7 +85,19 @@ func ComputeTxId(rawTxHex string) string {
return HexEncode(ReverseBytes(CalcHash256(HexDecode(rawTxHex)))) 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 { func DerivePrivateKeyForPath(privKeyBytes []byte, chain []byte, path string) []byte {
data := privKeyBytes 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) { func DerivePrivateKey(privKeyBytes []byte, chain []byte, i uint32, prime bool) ([]byte, []byte) {
data := []byte{} // nolint [ megacheck, deadcode ] var data []byte
if prime { if prime {
i = i | 0x80000000 i = i | 0x80000000
data = append([]byte{byte(0)}, privKeyBytes...) data = append([]byte{byte(0)}, privKeyBytes...)
@ -164,11 +176,11 @@ func addPoints(a []byte, b []byte) []byte {
panic(err) panic(err)
} }
sumX, sumY := btcec.S256().Add(ap.X, ap.Y, bp.X, bp.Y) 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(), Curve: btcec.S256(),
X: sumX, X: sumX,
Y: sumY, Y: sumY,
}) }
return sum.SerializeCompressed() return sum.SerializeCompressed()
} }
@ -235,11 +247,11 @@ func WIFFromPrivKeyBytes(privKeyBytes []byte, compress bool) string {
func PubKeyBytesFromPrivKeyBytes(privKeyBytes []byte, compress bool) (pubKeyBytes []byte) { func PubKeyBytesFromPrivKeyBytes(privKeyBytes []byte, compress bool) (pubKeyBytes []byte) {
x, y := btcec.S256().ScalarBaseMult(privKeyBytes) x, y := btcec.S256().ScalarBaseMult(privKeyBytes)
pub := (*btcec.PublicKey)(&btcec.PublicKey{ // nolint: unconvert pub := &btcec.PublicKey{
Curve: btcec.S256(), Curve: btcec.S256(),
X: x, X: x,
Y: y, Y: y,
}) }
if compress { if compress {
return pub.SerializeCompressed() return pub.SerializeCompressed()

View File

@ -1,8 +1,10 @@
// nolint: goimports
package hd package hd
import ( import (
"bytes" "bytes"
"crypto/hmac"
"crypto/sha512"
"encoding/binary"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -13,6 +15,11 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tyler-smith/go-bip39" "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" "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) { func gocrypto(seed []byte) ([]byte, []byte, []byte) {
_, priv, ch, _ := ComputeMastersFromSeed(string(seed)) _, priv, ch, _ := ComputeMastersFromSeed(string(seed))
@ -117,6 +131,83 @@ func gocrypto(seed []byte) ([]byte, []byte, []byte) {
return HexDecode(priv), privBytes, pubBytes 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 // Benchmarks
var revBytesCases = [][]byte{ var revBytesCases = [][]byte{
@ -146,6 +237,7 @@ func BenchmarkReverseBytes(b *testing.B) {
// sink is necessary to ensure if the compiler tries // sink is necessary to ensure if the compiler tries
// to smart, that it won't optimize away the benchmarks. // to smart, that it won't optimize away the benchmarks.
if sink != nil { // nolint: megacheck if sink != nil {
_ = sink
} }
} }

View File

@ -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 // exists just to make sure we fulfill the Signer interface
// nolint [ megacheck, deadcode ]
func (s Manager) assertSigner() keys.Signer { func (s Manager) assertSigner() keys.Signer {
return s return s
} }
// exists just to make sure we fulfill the Manager interface // exists just to make sure we fulfill the Manager interface
// nolint [ megacheck, deadcode ]
func (s Manager) assertKeyManager() keys.Manager { func (s Manager) assertKeyManager() keys.Manager {
return s return s
} }

View File

@ -144,7 +144,6 @@ func TestKeyManagement(t *testing.T) {
// } // }
// } // }
// nolint: unparam
func assertPassword(assert *assert.Assertions, cstore cryptostore.Manager, name, pass, badpass string) { func assertPassword(assert *assert.Assertions, cstore cryptostore.Manager, name, pass, badpass string) {
err := cstore.Update(name, badpass, pass) err := cstore.Update(name, badpass, pass)
assert.NotNil(err) assert.NotNil(err)

View File

@ -6,7 +6,6 @@ Please read the README and godoc to see how to
configure the server for your application. configure the server for your application.
*/ */
// nolint: goimports
package server package server
import ( import (

View File

@ -52,7 +52,6 @@ func (k Keys) GetKey(w http.ResponseWriter, r *http.Request) {
writeSuccess(w, &key) writeSuccess(w, &key)
} }
// nolint: unparam
func (k Keys) ListKeys(w http.ResponseWriter, r *http.Request) { func (k Keys) ListKeys(w http.ResponseWriter, r *http.Request) {
keys, err := k.manager.List() keys, err := k.manager.List()

View File

@ -120,7 +120,6 @@ func listKeys(h http.Handler) (keys.Infos, int, error) {
return data, rr.Code, err return data, rr.Code, err
} }
// nolint: unparam
func getKey(h http.Handler, name string) (*keys.Info, int, error) { func getKey(h http.Handler, name string) (*keys.Info, int, error) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/keys/"+name, nil) 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 return &data, rr.Code, err
} }
// nolint: unparam
func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyResponse, int, error) { func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyResponse, int, error) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
post := types.CreateKeyRequest{ post := types.CreateKeyRequest{
@ -167,7 +165,6 @@ func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyR
return data, rr.Code, err return data, rr.Code, err
} }
// nolint: unparam
func deleteKey(h http.Handler, name, passphrase string) (*types.ErrorResponse, int, error) { func deleteKey(h http.Handler, name, passphrase string) (*types.ErrorResponse, int, error) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
post := types.DeleteKeyRequest{ post := types.DeleteKeyRequest{

View File

@ -42,8 +42,9 @@ func New(dir string) FileStore {
return FileStore{dir} return FileStore{dir}
} }
var _ keys.Storage = FileStore{}
// assertStorage just makes sure we implement the proper Storage interface // assertStorage just makes sure we implement the proper Storage interface
// nolint [ megacheck, deadcode ]
func (s FileStore) assertStorage() keys.Storage { func (s FileStore) assertStorage() keys.Storage {
return s return s
} }

View File

@ -22,8 +22,9 @@ func New() MemStore {
return MemStore{} return MemStore{}
} }
var _ keys.Storage = MemStore{}
// assertStorage just makes sure we implement the Storage interface // assertStorage just makes sure we implement the Storage interface
// nolint [ megacheck, deadcode ]
func (s MemStore) assertStorage() keys.Storage { func (s MemStore) assertStorage() keys.Storage {
return s return s
} }

View File

@ -6,7 +6,6 @@
// keys/wordlist/spanish.txt // keys/wordlist/spanish.txt
// DO NOT EDIT! // DO NOT EDIT!
// nolint: goimports
package wordlist package wordlist
import ( import (