From 87541c4c3673b659dd949bf28f01faf03a136e67 Mon Sep 17 00:00:00 2001 From: mrekucci Date: Thu, 21 Dec 2023 16:51:18 +0700 Subject: [PATCH] refactor: revert btcd version bump --- go.mod | 4 ++-- go.sum | 6 ++++-- pkg/api/pss_test.go | 3 ++- pkg/crypto/crypto.go | 18 +++++++----------- pkg/crypto/dh_test.go | 7 ++++--- pkg/crypto/signer.go | 20 ++++++-------------- pkg/keystore/file/key.go | 2 +- pkg/pss/trojan.go | 15 ++++++--------- 8 files changed, 32 insertions(+), 43 deletions(-) diff --git a/go.mod b/go.mod index 3f9b836e08a..35c719e85bf 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( contrib.go.opencensus.io/exporter/prometheus v0.4.2 github.com/armon/go-radix v1.0.0 - github.com/btcsuite/btcd/btcec/v2 v2.3.2 + github.com/btcsuite/btcd v0.22.3 github.com/casbin/casbin/v2 v2.35.0 github.com/coreos/go-semver v0.3.0 github.com/ethereum/go-ethereum v1.13.4 @@ -63,7 +63,7 @@ require ( github.com/benbjohnson/clock v1.3.5 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bits-and-blooms/bitset v1.7.0 // indirect - github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect + github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/codahale/hdrhistogram v0.0.0-00010101000000-000000000000 // indirect github.com/consensys/bavard v0.1.13 // indirect diff --git a/go.sum b/go.sum index 5c83af6a368..b0ff56db2a9 100644 --- a/go.sum +++ b/go.sum @@ -124,8 +124,10 @@ github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx2 github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA= -github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= -github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= +github.com/btcsuite/btcd v0.22.3 h1:kYNaWFvOw6xvqP0vR20RP1Zq1DVMBxEO8QN5d1/EfNg= +github.com/btcsuite/btcd v0.22.3/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y= +github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= +github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= diff --git a/pkg/api/pss_test.go b/pkg/api/pss_test.go index d40a4889d4e..84ae0e05f3f 100644 --- a/pkg/api/pss_test.go +++ b/pkg/api/pss_test.go @@ -19,6 +19,7 @@ import ( "testing" "time" + "github.com/btcsuite/btcd/btcec" "github.com/ethersphere/bee/pkg/api" "github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/jsonhttp" @@ -170,7 +171,7 @@ func TestPssSend(t *testing.T) { done bool privk, _ = crypto.GenerateSecp256k1Key() - publicKeyBytes = crypto.EncodeSecp256k1PublicKey(&privk.PublicKey) + publicKeyBytes = (*btcec.PublicKey)(&privk.PublicKey).SerializeCompressed() sendFn = func(ctx context.Context, targets pss.Targets, chunk swarm.Chunk) error { mtx.Lock() diff --git a/pkg/crypto/crypto.go b/pkg/crypto/crypto.go index a2e0ba2e468..fb8cab1f94f 100644 --- a/pkg/crypto/crypto.go +++ b/pkg/crypto/crypto.go @@ -13,7 +13,7 @@ import ( "errors" "fmt" - "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec" "github.com/ethersphere/bee/pkg/swarm" "golang.org/x/crypto/sha3" ) @@ -66,16 +66,12 @@ func GenerateSecp256k1Key() (*ecdsa.PrivateKey, error) { // EncodeSecp256k1PrivateKey encodes raw ECDSA private key. func EncodeSecp256k1PrivateKey(k *ecdsa.PrivateKey) ([]byte, error) { - pvk, _ := btcec.PrivKeyFromBytes(k.D.Bytes()) - return pvk.Serialize(), nil + return (*btcec.PrivateKey)(k).Serialize(), nil } // EncodeSecp256k1PublicKey encodes raw ECDSA public key in a 33-byte compressed format. func EncodeSecp256k1PublicKey(k *ecdsa.PublicKey) []byte { - var x, y btcec.FieldVal - x.SetByteSlice(k.X.Bytes()) - y.SetByteSlice(k.Y.Bytes()) - return btcec.NewPublicKey(&x, &y).SerializeCompressed() + return (*btcec.PublicKey)(k).SerializeCompressed() } // DecodeSecp256k1PrivateKey decodes raw ECDSA private key. @@ -83,8 +79,8 @@ func DecodeSecp256k1PrivateKey(data []byte) (*ecdsa.PrivateKey, error) { if l := len(data); l != btcec.PrivKeyBytesLen { return nil, fmt.Errorf("secp256k1 data size %d expected %d", l, btcec.PrivKeyBytesLen) } - pvk, _ := btcec.PrivKeyFromBytes(data) - return pvk.ToECDSA(), nil + privk, _ := btcec.PrivKeyFromBytes(btcec.S256(), data) + return (*ecdsa.PrivateKey)(privk), nil } // GenerateSecp256k1Key generates an ECDSA private key using @@ -106,8 +102,8 @@ func DecodeSecp256r1PrivateKey(data []byte) (*ecdsa.PrivateKey, error) { // Secp256k1PrivateKeyFromBytes returns an ECDSA private key based on // the byte slice. func Secp256k1PrivateKeyFromBytes(data []byte) *ecdsa.PrivateKey { - pvk, _ := btcec.PrivKeyFromBytes(data) - return pvk.ToECDSA() + privk, _ := btcec.PrivKeyFromBytes(btcec.S256(), data) + return (*ecdsa.PrivateKey)(privk) } // NewEthereumAddress returns a binary representation of ethereum blockchain address. diff --git a/pkg/crypto/dh_test.go b/pkg/crypto/dh_test.go index 67cb26ebbd2..1790895d65d 100644 --- a/pkg/crypto/dh_test.go +++ b/pkg/crypto/dh_test.go @@ -6,12 +6,13 @@ package crypto_test import ( "bytes" + "crypto/ecdsa" "crypto/rand" "encoding/hex" "io" "testing" - "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec" "github.com/ethersphere/bee/pkg/crypto" ) @@ -65,7 +66,7 @@ func TestSharedKey(t *testing.T) { if err != nil { t.Fatal(err) } - pubkey, err := btcec.ParsePubKey(data) + pubkey, err := btcec.ParsePubKey(data, btcec.S256()) if err != nil { t.Fatal(err) } @@ -75,7 +76,7 @@ func TestSharedKey(t *testing.T) { } dh := crypto.NewDH(privKey) - sk, err := dh.SharedKey(pubkey.ToECDSA(), salt) + sk, err := dh.SharedKey((*ecdsa.PublicKey)(pubkey), salt) if err != nil { t.Fatal(err) } diff --git a/pkg/crypto/signer.go b/pkg/crypto/signer.go index b7d1927aafe..6a82101eff0 100644 --- a/pkg/crypto/signer.go +++ b/pkg/crypto/signer.go @@ -10,8 +10,7 @@ import ( "fmt" "math/big" - "github.com/btcsuite/btcd/btcec/v2" - btcecdsa "github.com/btcsuite/btcd/btcec/v2/ecdsa" + "github.com/btcsuite/btcd/btcec" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethersphere/bee/pkg/crypto/eip712" @@ -60,11 +59,8 @@ func Recover(signature, data []byte) (*ecdsa.PublicKey, error) { return nil, err } - pbk, _, err := btcecdsa.RecoverCompact(btcsig, hash) - if err != nil { - return nil, err - } - return pbk.ToECDSA(), err + p, _, err := btcec.RecoverCompact(btcec.S256(), btcsig, hash) + return (*ecdsa.PublicKey)(p), err } type defaultSigner struct { @@ -139,8 +135,7 @@ func (d *defaultSigner) SignTypedData(typedData *eip712.TypedData) ([]byte, erro // sign the provided hash and convert it to the ethereum (r,s,v) format. func (d *defaultSigner) sign(sighash []byte, isCompressedKey bool) ([]byte, error) { - pvk, _ := btcec.PrivKeyFromBytes(d.key.D.Bytes()) - signature, err := btcecdsa.SignCompact(pvk, sighash, false) + signature, err := btcec.SignCompact(btcec.S256(), (*btcec.PrivateKey)(d.key), sighash, false) if err != nil { return nil, err } @@ -172,9 +167,6 @@ func RecoverEIP712(signature []byte, data *eip712.TypedData) (*ecdsa.PublicKey, return nil, err } - pbk, _, err := btcecdsa.RecoverCompact(btcsig, sighash) - if err != nil { - return nil, err - } - return pbk.ToECDSA(), err + p, _, err := btcec.RecoverCompact(btcec.S256(), btcsig, sighash) + return (*ecdsa.PublicKey)(p), err } diff --git a/pkg/keystore/file/key.go b/pkg/keystore/file/key.go index fbec230f942..d6eafaf4326 100644 --- a/pkg/keystore/file/key.go +++ b/pkg/keystore/file/key.go @@ -16,7 +16,7 @@ import ( "fmt" "io" - "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec" "github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/keystore" "github.com/google/uuid" diff --git a/pkg/pss/trojan.go b/pkg/pss/trojan.go index 4e742875333..5a901b1086a 100644 --- a/pkg/pss/trojan.go +++ b/pkg/pss/trojan.go @@ -15,7 +15,7 @@ import ( "fmt" "io" - "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec" "github.com/ethersphere/bee/pkg/bmtpool" "github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/encryption" @@ -96,7 +96,7 @@ func Wrap(ctx context.Context, topic Topic, msg []byte, recipient *ecdsa.PublicK // NOTE: only the random bytes of the compressed public key are used // in order not to leak anything, the one bit parity info of the magic byte // is encoded in the parity of the 28th byte of the mined nonce - ephpubBytes := crypto.EncodeSecp256k1PublicKey(ephpub) + ephpubBytes := (*btcec.PublicKey)(ephpub).SerializeCompressed() payload := append(ephpubBytes[1:], ciphertext...) odd := ephpubBytes[0]&0x1 != 0 @@ -259,11 +259,8 @@ func extractPublicKey(chunkData []byte) (*ecdsa.PublicKey, error) { if chunkData[36]|0x1 != 0 { pubkeyBytes[0] |= 0x1 } - pubkey, err := btcec.ParsePubKey(pubkeyBytes) - if err != nil { - return nil, err - } - return pubkey.ToECDSA(), err + pubkey, err := btcec.ParsePubKey(pubkeyBytes, btcec.S256()) + return (*ecdsa.PublicKey)(pubkey), err } // topic is needed to decrypt the trojan payload, but no need to perform decryption with each @@ -315,9 +312,9 @@ func ParseRecipient(recipientHexString string) (*ecdsa.PublicKey, error) { if err != nil { return nil, err } - pubkey, err := btcec.ParsePubKey(publicKeyBytes) + pubkey, err := btcec.ParsePubKey(publicKeyBytes, btcec.S256()) if err != nil { return nil, err } - return pubkey.ToECDSA(), err + return (*ecdsa.PublicKey)(pubkey), err }