diff --git a/eth/signature.go b/eth/signature.go index 2f668a4..514fa16 100644 --- a/eth/signature.go +++ b/eth/signature.go @@ -3,10 +3,10 @@ package eth import ( "encoding/hex" + "github.com/decred/dcrd/dcrec/secp256k1/v4" + "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa" "github.com/pkg/errors" "golang.org/x/crypto/sha3" - - secp256k1 "github.com/btcsuite/btcd/btcec" ) type Signature struct { @@ -73,32 +73,34 @@ func NewEIP155Signature(r Quantity, s Quantity, v Quantity) (*Signature, error) // https://github.com/golang/go/pull/26873 <-- rejected // https://github.com/golang/go/issues/26776 <-- rejected // -// For the meantime, we will use btcd's eliptic curve implementation. +// For the meantime, we will use dcrd's eliptic curve implementation. func ECSign(h *Hash, privKeyBytes []byte, chainId Quantity) (*Signature, error) { // code for this method inspired by https://github.com/ethereumjs/ethereumjs-util/blob/master/src/signature.ts#L15 - priv, pub := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes) - addr, err := pubKeyBytesToAddress(pub.SerializeUncompressed()) + priv := secp256k1.PrivKeyFromBytes(privKeyBytes) + addr, err := pubKeyBytesToAddress(priv.PubKey().SerializeUncompressed()) if err != nil { return nil, errors.Wrap(err, "could not convert key to ethereum address") } - rawsig, err := priv.Sign(h.Bytes()) + signature := ecdsa.Sign(priv, h.Bytes()) + r, err := NewQuantity("0x" + signature.R().String()) if err != nil { - return nil, errors.Wrap(err, "signing failed") + return nil, errors.Wrap(err, "could not convert signature R value to quantity") + } + s, err := NewQuantity("0x" + signature.S().String()) + if err != nil { + return nil, errors.Wrap(err, "could not convert signature S value to quantity") } - - r := QuantityFromBigInt(rawsig.R) - s := QuantityFromBigInt(rawsig.S) // Unfortunately the ECDSA package we are using doesn't return the recovery V value // so the only recourse is to try both values 0 or 1 and see which one produces a valid // value. v := QuantityFromInt64(1) - sender, err := ECRecover(h, &r, &s, &v) + sender, err := ECRecover(h, r, s, &v) if err != nil || sender.String() != addr.String() { // ok try the other recovery value v = QuantityFromInt64(0) - sender, err = ECRecover(h, &r, &s, &v) + sender, err = ECRecover(h, r, s, &v) if err != nil { return nil, errors.Wrap(err, "recovery failed") } @@ -108,7 +110,7 @@ func ECSign(h *Hash, privKeyBytes []byte, chainId Quantity) (*Signature, error) return nil, errors.New("signature mismatch") } - return &Signature{r, s, v, chainId}, nil + return &Signature{*r, *s, v, chainId}, nil } // EIP155Values returns the expected R,S, and V values for an EIP-155 Signature. Namely, the V value includes the @@ -148,10 +150,10 @@ func (s *Signature) ChainId() (*Quantity, error) { func ECRecover(h *Hash, r, s, v *Quantity) (*Address, error) { // The code below is based heavily on the secp256k1.recoverAddress implementation at: // https://github.com/ethers-io/ethers.js/blob/34397fa2aaa9187f307881ec10f07dc035dc0854/src.ts/utils/secp256k1.ts#L109 - // with some trial and error to get working with btcd. + // with some trial and error to get working with dcrd. // I also used some of the code changes proposed in: // https://github.com/tendermint/tendermint/pull/3441 - // To determine that btcd is capable of recovering ethereuem addresses. + // To determine that dcrd is capable of recovering ethereuem addresses. // recover the public key hashBytes, err := hex.DecodeString(h.String()[2:]) @@ -159,7 +161,7 @@ func ECRecover(h *Hash, r, s, v *Quantity) (*Address, error) { return nil, errors.Wrap(err, "could not convert hash to bytes") } - // NOTE: btcd's secp256k1 expects V at offset 0 NOT offset 64 + // NOTE: dcrd's secp256k1 expects V at offset 0 NOT offset 64 vb := byte(v.Big().Uint64() + 27) rb, sb := r.Big().Bytes(), s.Big().Bytes() sig := make([]byte, 65) @@ -167,7 +169,7 @@ func ECRecover(h *Hash, r, s, v *Quantity) (*Address, error) { copy(sig[33-len(rb):33], rb) copy(sig[65-len(sb):65], sb) - key, _, err := secp256k1.RecoverCompact(secp256k1.S256(), sig, hashBytes) + key, _, err := ecdsa.RecoverCompact(sig, hashBytes) if err != nil { return nil, errors.Wrap(err, "could not recover secp256k1 key") } diff --git a/eth/signature_test.go b/eth/signature_test.go index 4923034..8becc8d 100644 --- a/eth/signature_test.go +++ b/eth/signature_test.go @@ -4,7 +4,7 @@ import ( "encoding/hex" "testing" - secp256k1 "github.com/btcsuite/btcd/btcec" + secp256k1 "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/stretchr/testify/require" "github.com/INFURA/go-ethlibs/eth" @@ -12,7 +12,7 @@ import ( func TestECSignAndRecover(t *testing.T) { key, _ := hex.DecodeString("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19") - secp256k1.PrivKeyFromBytes(secp256k1.S256(), key) + secp256k1.PrivKeyFromBytes(key) digest := eth.MustHash("0x40340296657f4ca5b25addda7b14d31458cbf1efab963e949daef0e84415c5dc") chainId := eth.QuantityFromInt64(1) diff --git a/go.mod b/go.mod index 5617e92..11f8e36 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/INFURA/go-ethlibs go 1.17 require ( - github.com/btcsuite/btcd v0.0.0-20190614013741-962a206e94e9 + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.1-0.20230921164230-9754217aff8e github.com/golang/mock v1.6.0 github.com/gorilla/websocket v1.4.1 github.com/pkg/errors v0.8.1 diff --git a/go.sum b/go.sum index 30827fc..1277943 100644 --- a/go.sum +++ b/go.sum @@ -1,29 +1,13 @@ -github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= -github.com/btcsuite/btcd v0.0.0-20190614013741-962a206e94e9 h1:18Pe+JPyglNO9lzLrQHj7Dwmdi4c49D8xQTqTRz3qJo= -github.com/btcsuite/btcd v0.0.0-20190614013741-962a206e94e9/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI= -github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= -github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= -github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= -github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= -github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= -github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= -github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= -github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.1-0.20230921164230-9754217aff8e h1:w9bBgdQZOVQjncgQE3mKjhYen+utx1bS57cMos+T7uI= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.1-0.20230921164230-9754217aff8e/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= -github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= -github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= -github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -32,20 +16,16 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -61,7 +41,3 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/rlp/fuzz/go.sum b/rlp/fuzz/go.sum index d070318..2521990 100644 --- a/rlp/fuzz/go.sum +++ b/rlp/fuzz/go.sum @@ -10,6 +10,8 @@ github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46f github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.1-0.20230921164230-9754217aff8e/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=