From 072951f9f5262a23f86d41cbca0343db482f3798 Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Sun, 7 Jan 2024 22:37:00 +0900 Subject: [PATCH] multi: switch over the btcec/v2 from btcd --- blockchain/compress.go | 8 +- blockchain/fullblocktests/generate.go | 4 +- btcutil/address.go | 7 +- btcutil/address_test.go | 116 -------------------------- btcutil/internal_test.go | 4 +- btcutil/wif.go | 10 +-- btcutil/wif_test.go | 6 +- go.mod | 7 +- go.sum | 16 ++++ integration/csv_fork_test.go | 4 +- integration/rpctest/memwallet.go | 13 +-- mempool/mempool_test.go | 4 +- mempool/policy_test.go | 4 +- rpcserver.go | 8 +- txscript/engine.go | 2 +- txscript/example_test.go | 4 +- txscript/opcode.go | 23 +++-- txscript/pkscript.go | 5 +- txscript/sigcache.go | 9 +- txscript/sigcache_test.go | 28 +++---- txscript/sign.go | 20 ++--- txscript/sign_test.go | 114 ++++++++++--------------- txscript/standard.go | 2 +- 23 files changed, 140 insertions(+), 278 deletions(-) diff --git a/blockchain/compress.go b/blockchain/compress.go index 59ed391b..4a90dbe5 100644 --- a/blockchain/compress.go +++ b/blockchain/compress.go @@ -5,7 +5,7 @@ package blockchain import ( - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/txscript" ) @@ -218,7 +218,7 @@ func isPubKey(script []byte) (bool, []byte) { // Ensure the public key is valid. serializedPubKey := script[1:34] - _, err := btcec.ParsePubKey(serializedPubKey, btcec.S256()) + _, err := btcec.ParsePubKey(serializedPubKey) if err == nil { return true, serializedPubKey } @@ -230,7 +230,7 @@ func isPubKey(script []byte) (bool, []byte) { // Ensure the public key is valid. serializedPubKey := script[1:66] - _, err := btcec.ParsePubKey(serializedPubKey, btcec.S256()) + _, err := btcec.ParsePubKey(serializedPubKey) if err == nil { return true, serializedPubKey } @@ -399,7 +399,7 @@ func decompressScript(compressedPkScript []byte) []byte { compressedKey := make([]byte, 33) compressedKey[0] = byte(encodedScriptSize - 2) copy(compressedKey[1:], compressedPkScript[1:]) - key, err := btcec.ParsePubKey(compressedKey, btcec.S256()) + key, err := btcec.ParsePubKey(compressedKey) if err != nil { return nil } diff --git a/blockchain/fullblocktests/generate.go b/blockchain/fullblocktests/generate.go index 76c29d4c..68c24038 100644 --- a/blockchain/fullblocktests/generate.go +++ b/blockchain/fullblocktests/generate.go @@ -18,8 +18,8 @@ import ( "runtime" "time" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/blockchain" - "github.com/utreexo/utreexod/btcec" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash" @@ -234,7 +234,7 @@ type testGenerator struct { // makeTestGenerator returns a test generator instance initialized with the // genesis block as the tip. func makeTestGenerator(params *chaincfg.Params) (testGenerator, error) { - privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), []byte{0x01}) + privKey, _ := btcec.PrivKeyFromBytes([]byte{0x01}) genesis := params.GenesisBlock genesisHash := genesis.BlockHash() return testGenerator{ diff --git a/btcutil/address.go b/btcutil/address.go index dec6a474..99e03f97 100644 --- a/btcutil/address.go +++ b/btcutil/address.go @@ -11,7 +11,7 @@ import ( "fmt" "strings" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/btcutil/base58" "github.com/utreexo/utreexod/btcutil/bech32" "github.com/utreexo/utreexod/chaincfg" @@ -434,7 +434,7 @@ type AddressPubKey struct { // address. The serializedPubKey parameter must be a valid pubkey and can be // uncompressed, compressed, or hybrid. func NewAddressPubKey(serializedPubKey []byte, net *chaincfg.Params) (*AddressPubKey, error) { - pubKey, err := btcec.ParsePubKey(serializedPubKey, btcec.S256()) + pubKey, err := btcec.ParsePubKey(serializedPubKey) if err != nil { return nil, err } @@ -469,9 +469,6 @@ func (a *AddressPubKey) serialize() []byte { case PKFCompressed: return a.pubKey.SerializeCompressed() - - case PKFHybrid: - return a.pubKey.SerializeHybrid() } } diff --git a/btcutil/address_test.go b/btcutil/address_test.go index e2e78add..edfd5a06 100644 --- a/btcutil/address_test.go +++ b/btcutil/address_test.go @@ -338,64 +338,6 @@ func TestAddresses(t *testing.T) { }, net: &chaincfg.MainNetParams, }, - { - name: "mainnet p2pk hybrid (0x06)", - addr: "06192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4" + - "0d45264838c0bd96852662ce6a847b197376830160c6d2eb5e6a4c44d33f453e", - encoded: "1Ja5rs7XBZnK88EuLVcFqYGMEbBitzchmX", - valid: true, - result: btcutil.TstAddressPubKey( - []byte{ - 0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, - 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, - 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, - 0x52, 0xc6, 0xb4, 0x0d, 0x45, 0x26, 0x48, 0x38, 0xc0, 0xbd, - 0x96, 0x85, 0x26, 0x62, 0xce, 0x6a, 0x84, 0x7b, 0x19, 0x73, - 0x76, 0x83, 0x01, 0x60, 0xc6, 0xd2, 0xeb, 0x5e, 0x6a, 0x4c, - 0x44, 0xd3, 0x3f, 0x45, 0x3e}, - btcutil.PKFHybrid, chaincfg.MainNetParams.PubKeyHashAddrID), - f: func() (btcutil.Address, error) { - serializedPubKey := []byte{ - 0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, - 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, - 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, - 0x52, 0xc6, 0xb4, 0x0d, 0x45, 0x26, 0x48, 0x38, 0xc0, 0xbd, - 0x96, 0x85, 0x26, 0x62, 0xce, 0x6a, 0x84, 0x7b, 0x19, 0x73, - 0x76, 0x83, 0x01, 0x60, 0xc6, 0xd2, 0xeb, 0x5e, 0x6a, 0x4c, - 0x44, 0xd3, 0x3f, 0x45, 0x3e} - return btcutil.NewAddressPubKey(serializedPubKey, &chaincfg.MainNetParams) - }, - net: &chaincfg.MainNetParams, - }, - { - name: "mainnet p2pk hybrid (0x07)", - addr: "07b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65" + - "37a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b", - encoded: "1ExqMmf6yMxcBMzHjbj41wbqYuqoX6uBLG", - valid: true, - result: btcutil.TstAddressPubKey( - []byte{ - 0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, - 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, - 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, - 0xb1, 0x6e, 0x65, 0x37, 0xa5, 0x76, 0x78, 0x2e, 0xba, 0x66, - 0x8a, 0x7e, 0xf8, 0xbd, 0x3b, 0x3c, 0xfb, 0x1e, 0xdb, 0x71, - 0x17, 0xab, 0x65, 0x12, 0x9b, 0x8a, 0x2e, 0x68, 0x1f, 0x3c, - 0x1e, 0x09, 0x08, 0xef, 0x7b}, - btcutil.PKFHybrid, chaincfg.MainNetParams.PubKeyHashAddrID), - f: func() (btcutil.Address, error) { - serializedPubKey := []byte{ - 0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, - 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, - 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, - 0xb1, 0x6e, 0x65, 0x37, 0xa5, 0x76, 0x78, 0x2e, 0xba, 0x66, - 0x8a, 0x7e, 0xf8, 0xbd, 0x3b, 0x3c, 0xfb, 0x1e, 0xdb, 0x71, - 0x17, 0xab, 0x65, 0x12, 0x9b, 0x8a, 0x2e, 0x68, 0x1f, 0x3c, - 0x1e, 0x09, 0x08, 0xef, 0x7b} - return btcutil.NewAddressPubKey(serializedPubKey, &chaincfg.MainNetParams) - }, - net: &chaincfg.MainNetParams, - }, { name: "testnet p2pk compressed (0x02)", addr: "02192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4", @@ -469,64 +411,6 @@ func TestAddresses(t *testing.T) { }, net: &chaincfg.TestNet3Params, }, - { - name: "testnet p2pk hybrid (0x06)", - addr: "06192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b" + - "40d45264838c0bd96852662ce6a847b197376830160c6d2eb5e6a4c44d33f453e", - encoded: "my639vCVzbDZuEiX44adfTUg6anRomZLEP", - valid: true, - result: btcutil.TstAddressPubKey( - []byte{ - 0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, - 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, - 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, - 0x52, 0xc6, 0xb4, 0x0d, 0x45, 0x26, 0x48, 0x38, 0xc0, 0xbd, - 0x96, 0x85, 0x26, 0x62, 0xce, 0x6a, 0x84, 0x7b, 0x19, 0x73, - 0x76, 0x83, 0x01, 0x60, 0xc6, 0xd2, 0xeb, 0x5e, 0x6a, 0x4c, - 0x44, 0xd3, 0x3f, 0x45, 0x3e}, - btcutil.PKFHybrid, chaincfg.TestNet3Params.PubKeyHashAddrID), - f: func() (btcutil.Address, error) { - serializedPubKey := []byte{ - 0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, - 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, - 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, - 0x52, 0xc6, 0xb4, 0x0d, 0x45, 0x26, 0x48, 0x38, 0xc0, 0xbd, - 0x96, 0x85, 0x26, 0x62, 0xce, 0x6a, 0x84, 0x7b, 0x19, 0x73, - 0x76, 0x83, 0x01, 0x60, 0xc6, 0xd2, 0xeb, 0x5e, 0x6a, 0x4c, - 0x44, 0xd3, 0x3f, 0x45, 0x3e} - return btcutil.NewAddressPubKey(serializedPubKey, &chaincfg.TestNet3Params) - }, - net: &chaincfg.TestNet3Params, - }, - { - name: "testnet p2pk hybrid (0x07)", - addr: "07b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6" + - "537a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b", - encoded: "muUnepk5nPPrxUTuTAhRqrpAQuSWS5fVii", - valid: true, - result: btcutil.TstAddressPubKey( - []byte{ - 0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, - 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, - 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, - 0xb1, 0x6e, 0x65, 0x37, 0xa5, 0x76, 0x78, 0x2e, 0xba, 0x66, - 0x8a, 0x7e, 0xf8, 0xbd, 0x3b, 0x3c, 0xfb, 0x1e, 0xdb, 0x71, - 0x17, 0xab, 0x65, 0x12, 0x9b, 0x8a, 0x2e, 0x68, 0x1f, 0x3c, - 0x1e, 0x09, 0x08, 0xef, 0x7b}, - btcutil.PKFHybrid, chaincfg.TestNet3Params.PubKeyHashAddrID), - f: func() (btcutil.Address, error) { - serializedPubKey := []byte{ - 0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, - 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, - 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, - 0xb1, 0x6e, 0x65, 0x37, 0xa5, 0x76, 0x78, 0x2e, 0xba, 0x66, - 0x8a, 0x7e, 0xf8, 0xbd, 0x3b, 0x3c, 0xfb, 0x1e, 0xdb, 0x71, - 0x17, 0xab, 0x65, 0x12, 0x9b, 0x8a, 0x2e, 0x68, 0x1f, 0x3c, - 0x1e, 0x09, 0x08, 0xef, 0x7b} - return btcutil.NewAddressPubKey(serializedPubKey, &chaincfg.TestNet3Params) - }, - net: &chaincfg.TestNet3Params, - }, // Segwit address tests. { name: "segwit mainnet p2wpkh v0", diff --git a/btcutil/internal_test.go b/btcutil/internal_test.go index eacad994..39989d48 100644 --- a/btcutil/internal_test.go +++ b/btcutil/internal_test.go @@ -12,7 +12,7 @@ interface. The functions are only exported while the tests are being run. package btcutil import ( - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/btcutil/base58" "github.com/utreexo/utreexod/btcutil/bech32" "golang.org/x/crypto/ripemd160" @@ -99,7 +99,7 @@ func TstAddressTaproot(version byte, program [32]byte, func TstAddressPubKey(serializedPubKey []byte, pubKeyFormat PubKeyFormat, netID byte) *AddressPubKey { - pubKey, _ := btcec.ParsePubKey(serializedPubKey, btcec.S256()) + pubKey, _ := btcec.ParsePubKey(serializedPubKey) return &AddressPubKey{ pubKeyFormat: pubKeyFormat, pubKey: pubKey, diff --git a/btcutil/wif.go b/btcutil/wif.go index d674db14..9488c49c 100644 --- a/btcutil/wif.go +++ b/btcutil/wif.go @@ -8,7 +8,7 @@ import ( "bytes" "errors" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/btcutil/base58" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash" @@ -117,7 +117,7 @@ func DecodeWIF(wif string) (*WIF, error) { netID := decoded[0] privKeyBytes := decoded[1 : 1+btcec.PrivKeyBytesLen] - privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyBytes) + privKey, _ := btcec.PrivKeyFromBytes(privKeyBytes) return &WIF{privKey, compress, netID}, nil } @@ -136,9 +136,7 @@ func (w *WIF) String() string { a := make([]byte, 0, encodeLen) a = append(a, w.netID) - // Pad and append bytes manually, instead of using Serialize, to - // avoid another call to make. - a = paddedAppend(btcec.PrivKeyBytesLen, a, w.PrivKey.D.Bytes()) + a = append(a, w.PrivKey.Serialize()...) if w.CompressPubKey { a = append(a, compressMagic) } @@ -151,7 +149,7 @@ func (w *WIF) String() string { // exported private key in either a compressed or uncompressed format. The // serialization format chosen depends on the value of w.CompressPubKey. func (w *WIF) SerializePubKey() []byte { - pk := (*btcec.PublicKey)(&w.PrivKey.PublicKey) + pk := w.PrivKey.PubKey() if w.CompressPubKey { return pk.SerializeCompressed() } diff --git a/btcutil/wif_test.go b/btcutil/wif_test.go index 1beace48..07cabd9a 100644 --- a/btcutil/wif_test.go +++ b/btcutil/wif_test.go @@ -9,7 +9,7 @@ import ( "encoding/hex" "testing" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" . "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" ) @@ -65,7 +65,7 @@ func TestEncodeDecodeWIF(t *testing.T) { for _, validCase := range validEncodeCases { t.Run(validCase.name, func(t *testing.T) { - priv, _ := btcec.PrivKeyFromBytes(btcec.S256(), validCase.privateKey) + priv, _ := btcec.PrivKeyFromBytes(validCase.privateKey) wif, err := NewWIF(priv, validCase.net, validCase.compress) if err != nil { t.Fatalf("NewWIF failed: expected no error, got '%v'", err) @@ -141,7 +141,7 @@ func TestEncodeDecodeWIF(t *testing.T) { 0x60, 0x0b, 0x2f, 0xe5, 0x0b, 0x7c, 0xae, 0x11, 0xec, 0x86, 0xd3, 0xbf, 0x1f, 0xbe, 0x47, 0x1b, 0xe8, 0x98, 0x27, 0xe1, 0x9d, 0x72, 0xaa, 0x1d} - priv, _ := btcec.PrivKeyFromBytes(btcec.S256(), privateKey) + priv, _ := btcec.PrivKeyFromBytes(privateKey) wif, err := NewWIF(priv, nil, true) diff --git a/go.mod b/go.mod index 6cae5068..25a597fd 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,8 @@ module github.com/utreexo/utreexod require ( github.com/aead/siphash v1.0.1 - github.com/btcsuite/btcd v0.21.0-beta.0.20201124191514-610bb55ae85c - github.com/btcsuite/btcd/btcutil v1.0.0 + github.com/btcsuite/btcd v0.24.0 + github.com/btcsuite/btcd/btcutil v1.1.5 github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 @@ -20,12 +20,15 @@ require ( require ( github.com/btcsuite/btcd/btcec/v2 v2.1.3 // indirect + github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect github.com/btcsuite/goleveldb v1.0.0 // indirect github.com/btcsuite/snappy-go v1.0.0 // indirect + github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/golang/snappy v0.0.4 // indirect golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd // indirect + golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect ) go 1.18 diff --git a/go.sum b/go.sum index 8fa8faf3..eb3024ec 100644 --- a/go.sum +++ b/go.sum @@ -6,14 +6,23 @@ github.com/btcsuite/btcd v0.21.0-beta.0.20201124191514-610bb55ae85c h1:r38ojjb2+ github.com/btcsuite/btcd v0.21.0-beta.0.20201124191514-610bb55ae85c/go.mod h1:Sv4JPQ3/M+teHz9Bo5jBpkNcP0x6r7rdihlNL/7tTAs= github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c h1:lnAMg3ra/Gw4AkRMxrxYs8nrprWsHowg8H9zaYsJOo4= github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M= +github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd/go.mod h1:nm3Bko6zh6bWP60UxwoT5LzdGJsQJaPo6HjduXq9p6A= +github.com/btcsuite/btcd v0.24.0 h1:gL3uHE/IaFj6fcZSu03SvqPMSx7s/dPzfpG/atRwWdo= +github.com/btcsuite/btcd v0.24.0/go.mod h1:K4IDc1593s8jKXIF7yS7yCTSxrknB9z0STzc2j6XgE4= +github.com/btcsuite/btcd/btcec/v2 v2.1.0/go.mod h1:2VzYrv4Gm4apmbVVsSq5bqf1Ec8v56E48Vt0Y/umPgA= github.com/btcsuite/btcd/btcec/v2 v2.1.3 h1:xM/n3yIhHAhHy04z4i43C8p4ehixJZMsnrVJkgl+MTE= github.com/btcsuite/btcd/btcec/v2 v2.1.3/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE= github.com/btcsuite/btcd/btcutil v1.0.0 h1:dB36qRTOucIh6NUe40UCieOS+axPhP6VNyRtYkTUKKk= github.com/btcsuite/btcd/btcutil v1.0.0/go.mod h1:Uoxwv0pqYWhD//tfTiipkxNfdhG9UrLwaeswfjfdF0A= +github.com/btcsuite/btcd/btcutil v1.1.0/go.mod h1:5OapHB7A2hBBWLm48mmw4MOHNJCcUBTwmWH/0Jn8VHE= github.com/btcsuite/btcd/btcutil v1.1.2-0.20220607174035-04aac1ec7d5f h1:muP56lbMC+woUb0D2EbG7FyGpZsaMSXqGiS1ePsCDC0= github.com/btcsuite/btcd/btcutil v1.1.2-0.20220607174035-04aac1ec7d5f/go.mod h1:wYclbmZpStebXVLZip5/IOf55vZYmey1gyhNVr6L4x8= +github.com/btcsuite/btcd/btcutil v1.1.5 h1:+wER79R5670vs/ZusMTF1yTcRYE5GUsFbdjdisflzM8= +github.com/btcsuite/btcd/btcutil v1.1.5/go.mod h1:PSZZ4UitpLBWzxGd5VGOrLnmOjtPP/a6HaFo12zMs00= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= +github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ= +github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo= 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= @@ -36,6 +45,7 @@ github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= @@ -58,6 +68,7 @@ github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/gorilla/websocket v1.5.0/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/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= @@ -80,6 +91,9 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= @@ -127,6 +141,7 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -149,3 +164,4 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/integration/csv_fork_test.go b/integration/csv_fork_test.go index 48fc69d9..348c1b03 100644 --- a/integration/csv_fork_test.go +++ b/integration/csv_fork_test.go @@ -15,8 +15,8 @@ import ( "testing" "time" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/blockchain" - "github.com/utreexo/utreexod/btcec" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash" @@ -36,7 +36,7 @@ func makeTestOutput(r *rpctest.Harness, t *testing.T, // Create a fresh key, then send some coins to an address spendable by // that key. - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { return nil, nil, nil, err } diff --git a/integration/rpctest/memwallet.go b/integration/rpctest/memwallet.go index ef6e118c..a03579b2 100644 --- a/integration/rpctest/memwallet.go +++ b/integration/rpctest/memwallet.go @@ -6,14 +6,13 @@ package rpctest import ( "bytes" - "crypto/ecdsa" "encoding/binary" "fmt" "sync" + "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil/hdkeychain" "github.com/utreexo/utreexod/blockchain" - "github.com/utreexo/utreexod/btcec" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash" @@ -21,7 +20,6 @@ import ( "github.com/utreexo/utreexod/txscript" "github.com/utreexo/utreexod/wire" - btcdbtcec "github.com/btcsuite/btcd/btcec" btcdcfg "github.com/btcsuite/btcd/chaincfg" ) @@ -162,10 +160,9 @@ func newMemWallet(net *chaincfg.Params, harnessID uint32) (*memWallet, error) { addrs := make(map[uint32]btcutil.Address) addrs[0] = coinbaseAddr - coinbasePrivKey := btcec.PrivateKey(ecdsa.PrivateKey(*coinbaseKey)) return &memWallet{ net: net, - coinbaseKey: &coinbasePrivKey, + coinbaseKey: coinbaseKey, coinbaseAddr: coinbaseAddr, hdIndex: 1, hdRoot: hdRoot, @@ -539,10 +536,8 @@ func (m *memWallet) CreateTransaction(outputs []*wire.TxOut, return nil, err } - utreexoPrivKey := btcec.PrivateKey(ecdsa.PrivateKey(*privKey)) - sigScript, err := txscript.SignatureScript(tx, i, utxo.pkScript, - txscript.SigHashAll, &utreexoPrivKey, true) + txscript.SigHashAll, privKey, true) if err != nil { return nil, err } @@ -603,7 +598,7 @@ func (m *memWallet) ConfirmedBalance() btcutil.Amount { } // keyToAddr maps the passed private to corresponding p2pkh address. -func keyToAddr(key *btcdbtcec.PrivateKey, net *chaincfg.Params) (btcutil.Address, error) { +func keyToAddr(key *btcec.PrivateKey, net *chaincfg.Params) (btcutil.Address, error) { serializedKey := key.PubKey().SerializeCompressed() pubKeyAddr, err := btcutil.NewAddressPubKey(serializedKey, net) if err != nil { diff --git a/mempool/mempool_test.go b/mempool/mempool_test.go index 7beefcd7..76c6f121 100644 --- a/mempool/mempool_test.go +++ b/mempool/mempool_test.go @@ -12,8 +12,8 @@ import ( "testing" "time" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/blockchain" - "github.com/utreexo/utreexod/btcec" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash" @@ -291,7 +291,7 @@ func newPoolHarness(chainParams *chaincfg.Params) (*poolHarness, []spendableOutp if err != nil { return nil, nil, err } - signKey, signPub := btcec.PrivKeyFromBytes(btcec.S256(), keyBytes) + signKey, signPub := btcec.PrivKeyFromBytes(keyBytes) // Generate associated pay-to-script-hash address and resulting payment // script. diff --git a/mempool/policy_test.go b/mempool/policy_test.go index 29a556f5..4b659b60 100644 --- a/mempool/policy_test.go +++ b/mempool/policy_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash" @@ -98,7 +98,7 @@ func TestCalcMinRequiredTxRelayFee(t *testing.T) { func TestCheckPkScriptStandard(t *testing.T) { var pubKeys [][]byte for i := 0; i < 4; i++ { - pk, err := btcec.NewPrivateKey(btcec.S256()) + pk, err := btcec.NewPrivateKey() if err != nil { t.Fatalf("TestCheckPkScriptStandard NewPrivateKey failed: %v", err) diff --git a/rpcserver.go b/rpcserver.go index c1b4bfc1..efb9266c 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -27,10 +27,10 @@ import ( "sync/atomic" "time" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/btcsuite/websocket" "github.com/utreexo/utreexod/blockchain" "github.com/utreexo/utreexod/blockchain/indexers" - "github.com/utreexo/utreexod/btcec" "github.com/utreexo/utreexod/btcjson" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" @@ -3944,8 +3944,7 @@ func handleSignMessageWithPrivKey(s *rpcServer, cmd interface{}, closeChan <-cha wire.WriteVarString(&buf, 0, c.Message) messageHash := chainhash.DoubleHashB(buf.Bytes()) - sig, err := btcec.SignCompact(btcec.S256(), wif.PrivKey, - messageHash, wif.CompressPubKey) + sig, err := ecdsa.SignCompact(wif.PrivKey, messageHash, wif.CompressPubKey) if err != nil { return nil, &btcjson.RPCError{ Code: btcjson.ErrRPCInvalidAddressOrKey, @@ -4139,8 +4138,7 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{ wire.WriteVarString(&buf, 0, messageSignatureHeader) wire.WriteVarString(&buf, 0, c.Message) expectedMessageHash := chainhash.DoubleHashB(buf.Bytes()) - pk, wasCompressed, err := btcec.RecoverCompact(btcec.S256(), sig, - expectedMessageHash) + pk, wasCompressed, err := ecdsa.RecoverCompact(sig, expectedMessageHash) if err != nil { // Mirror Bitcoin Core behavior, which treats error in // RecoverCompact as invalid signature. diff --git a/txscript/engine.go b/txscript/engine.go index 195778dd..248f3b7b 100644 --- a/txscript/engine.go +++ b/txscript/engine.go @@ -12,7 +12,7 @@ import ( "math/big" "strings" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/wire" ) diff --git a/txscript/example_test.go b/txscript/example_test.go index 5ee2042d..f3e0d979 100644 --- a/txscript/example_test.go +++ b/txscript/example_test.go @@ -9,7 +9,7 @@ import ( "encoding/hex" "fmt" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash" @@ -90,7 +90,7 @@ func ExampleSignTxOutput() { fmt.Println(err) return } - privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), privKeyBytes) + privKey, pubKey := btcec.PrivKeyFromBytes(privKeyBytes) pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed()) addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, &chaincfg.MainNetParams) diff --git a/txscript/opcode.go b/txscript/opcode.go index 4c509706..9ec64924 100644 --- a/txscript/opcode.go +++ b/txscript/opcode.go @@ -15,7 +15,8 @@ import ( "golang.org/x/crypto/ripemd160" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/utreexo/utreexod/chaincfg/chainhash" "github.com/utreexo/utreexod/wire" ) @@ -1929,19 +1930,19 @@ func opcodeCheckSig(op *opcode, data []byte, vm *Engine) error { hash = calcSignatureHash(subScript, hashType, &vm.tx, vm.txIdx) } - pubKey, err := btcec.ParsePubKey(pkBytes, btcec.S256()) + pubKey, err := btcec.ParsePubKey(pkBytes) if err != nil { vm.dstack.PushBool(false) return nil } - var signature *btcec.Signature + var signature *ecdsa.Signature if vm.hasFlag(ScriptVerifyStrictEncoding) || vm.hasFlag(ScriptVerifyDERSignatures) { - signature, err = btcec.ParseDERSignature(sigBytes, btcec.S256()) + signature, err = ecdsa.ParseDERSignature(sigBytes) } else { - signature, err = btcec.ParseSignature(sigBytes, btcec.S256()) + signature, err = ecdsa.ParseSignature(sigBytes) } if err != nil { vm.dstack.PushBool(false) @@ -1989,7 +1990,7 @@ func opcodeCheckSigVerify(op *opcode, data []byte, vm *Engine) error { // the same signature multiple times when verifying a multisig. type parsedSigInfo struct { signature []byte - parsedSignature *btcec.Signature + parsedSignature *ecdsa.Signature parsed bool } @@ -2134,7 +2135,7 @@ func opcodeCheckMultiSig(op *opcode, data []byte, vm *Engine) error { signature := rawSig[:len(rawSig)-1] // Only parse and check the signature encoding once. - var parsedSig *btcec.Signature + var parsedSig *ecdsa.Signature if !sigInfo.parsed { if err := vm.checkHashTypeEncoding(hashType); err != nil { return err @@ -2148,11 +2149,9 @@ func opcodeCheckMultiSig(op *opcode, data []byte, vm *Engine) error { if vm.hasFlag(ScriptVerifyStrictEncoding) || vm.hasFlag(ScriptVerifyDERSignatures) { - parsedSig, err = btcec.ParseDERSignature(signature, - btcec.S256()) + parsedSig, err = ecdsa.ParseDERSignature(signature) } else { - parsedSig, err = btcec.ParseSignature(signature, - btcec.S256()) + parsedSig, err = ecdsa.ParseSignature(signature) } sigInfo.parsed = true if err != nil { @@ -2174,7 +2173,7 @@ func opcodeCheckMultiSig(op *opcode, data []byte, vm *Engine) error { } // Parse the pubkey. - parsedPubKey, err := btcec.ParsePubKey(pubKey, btcec.S256()) + parsedPubKey, err := btcec.ParsePubKey(pubKey) if err != nil { continue } diff --git a/txscript/pkscript.go b/txscript/pkscript.go index b1e9e00c..c8906146 100644 --- a/txscript/pkscript.go +++ b/txscript/pkscript.go @@ -5,7 +5,8 @@ import ( "errors" "fmt" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/wire" @@ -20,7 +21,7 @@ const ( // Signature hash type (1 byte) // Public key length (1 byte) // Public key (33 byte) - minPubKeyHashSigScriptLen = 1 + btcec.MinSigLen + 1 + 1 + 33 + minPubKeyHashSigScriptLen = 1 + ecdsa.MinSigLen + 1 + 1 + 33 // maxPubKeyHashSigScriptLen is the maximum length of a signature script // that spends a P2PKH output. The length is composed of the following: diff --git a/txscript/sigcache.go b/txscript/sigcache.go index d3fe56f1..69b8a844 100644 --- a/txscript/sigcache.go +++ b/txscript/sigcache.go @@ -7,7 +7,8 @@ package txscript import ( "sync" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/utreexo/utreexod/chaincfg/chainhash" ) @@ -18,7 +19,7 @@ import ( // match. In the occasion that two sigHashes collide, the newer sigHash will // simply overwrite the existing entry. type sigCacheEntry struct { - sig *btcec.Signature + sig *ecdsa.Signature pubKey *btcec.PublicKey } @@ -55,7 +56,7 @@ func NewSigCache(maxEntries uint) *SigCache { // // NOTE: This function is safe for concurrent access. Readers won't be blocked // unless there exists a writer, adding an entry to the SigCache. -func (s *SigCache) Exists(sigHash chainhash.Hash, sig *btcec.Signature, pubKey *btcec.PublicKey) bool { +func (s *SigCache) Exists(sigHash chainhash.Hash, sig *ecdsa.Signature, pubKey *btcec.PublicKey) bool { s.RLock() entry, ok := s.validSigs[sigHash] s.RUnlock() @@ -70,7 +71,7 @@ func (s *SigCache) Exists(sigHash chainhash.Hash, sig *btcec.Signature, pubKey * // // NOTE: This function is safe for concurrent access. Writers will block // simultaneous readers until function execution has concluded. -func (s *SigCache) Add(sigHash chainhash.Hash, sig *btcec.Signature, pubKey *btcec.PublicKey) { +func (s *SigCache) Add(sigHash chainhash.Hash, sig *ecdsa.Signature, pubKey *btcec.PublicKey) { s.Lock() defer s.Unlock() diff --git a/txscript/sigcache_test.go b/txscript/sigcache_test.go index a0877f43..622107fc 100644 --- a/txscript/sigcache_test.go +++ b/txscript/sigcache_test.go @@ -8,15 +8,16 @@ import ( "crypto/rand" "testing" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/utreexo/utreexod/chaincfg/chainhash" ) // genRandomSig returns a random message, a signature of the message under the // public key and the public key. This function is used to generate randomized // test data. -func genRandomSig() (*chainhash.Hash, *btcec.Signature, *btcec.PublicKey, error) { - privKey, err := btcec.NewPrivateKey(btcec.S256()) +func genRandomSig() (*chainhash.Hash, *ecdsa.Signature, *btcec.PublicKey, error) { + privKey, err := btcec.NewPrivateKey() if err != nil { return nil, nil, nil, err } @@ -26,10 +27,7 @@ func genRandomSig() (*chainhash.Hash, *btcec.Signature, *btcec.PublicKey, error) return nil, nil, nil, err } - sig, err := privKey.Sign(msgHash[:]) - if err != nil { - return nil, nil, nil, err - } + sig := ecdsa.Sign(privKey, msgHash[:]) return &msgHash, sig, privKey.PubKey(), nil } @@ -49,8 +47,8 @@ func TestSigCacheAddExists(t *testing.T) { sigCache.Add(*msg1, sig1, key1) // The previously added triplet should now be found within the sigcache. - sig1Copy, _ := btcec.ParseSignature(sig1.Serialize(), btcec.S256()) - key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed(), btcec.S256()) + sig1Copy, _ := ecdsa.ParseSignature(sig1.Serialize()) + key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed()) if !sigCache.Exists(*msg1, sig1Copy, key1Copy) { t.Errorf("previously added item not found in signature cache") } @@ -73,8 +71,8 @@ func TestSigCacheAddEvictEntry(t *testing.T) { sigCache.Add(*msg, sig, key) - sigCopy, _ := btcec.ParseSignature(sig.Serialize(), btcec.S256()) - keyCopy, _ := btcec.ParsePubKey(key.SerializeCompressed(), btcec.S256()) + sigCopy, _ := ecdsa.ParseSignature(sig.Serialize()) + keyCopy, _ := btcec.ParsePubKey(key.SerializeCompressed()) if !sigCache.Exists(*msg, sigCopy, keyCopy) { t.Errorf("previously added item not found in signature" + "cache") @@ -102,8 +100,8 @@ func TestSigCacheAddEvictEntry(t *testing.T) { } // The entry added above should be found within the sigcache. - sigNewCopy, _ := btcec.ParseSignature(sigNew.Serialize(), btcec.S256()) - keyNewCopy, _ := btcec.ParsePubKey(keyNew.SerializeCompressed(), btcec.S256()) + sigNewCopy, _ := ecdsa.ParseSignature(sigNew.Serialize()) + keyNewCopy, _ := btcec.ParsePubKey(keyNew.SerializeCompressed()) if !sigCache.Exists(*msgNew, sigNewCopy, keyNewCopy) { t.Fatalf("previously added item not found in signature cache") } @@ -125,8 +123,8 @@ func TestSigCacheAddMaxEntriesZeroOrNegative(t *testing.T) { sigCache.Add(*msg1, sig1, key1) // The generated triplet should not be found. - sig1Copy, _ := btcec.ParseSignature(sig1.Serialize(), btcec.S256()) - key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed(), btcec.S256()) + sig1Copy, _ := ecdsa.ParseSignature(sig1.Serialize()) + key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed()) if sigCache.Exists(*msg1, sig1Copy, key1Copy) { t.Errorf("previously added signature found in sigcache, but" + "shouldn't have been") diff --git a/txscript/sign.go b/txscript/sign.go index 0ec4c578..f02efbab 100644 --- a/txscript/sign.go +++ b/txscript/sign.go @@ -6,9 +6,9 @@ package txscript import ( "errors" - "fmt" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/wire" @@ -28,10 +28,7 @@ func RawTxInWitnessSignature(tx *wire.MsgTx, sigHashes *TxSigHashes, idx int, return nil, err } - signature, err := key.Sign(hash) - if err != nil { - return nil, fmt.Errorf("cannot sign tx input: %s", err) - } + signature := ecdsa.Sign(key, hash) return append(signature.Serialize(), byte(hashType)), nil } @@ -51,7 +48,7 @@ func WitnessSignature(tx *wire.MsgTx, sigHashes *TxSigHashes, idx int, amt int64 return nil, err } - pk := (*btcec.PublicKey)(&privKey.PublicKey) + pk := privKey.PubKey() var pkData []byte if compress { pkData = pk.SerializeCompressed() @@ -73,10 +70,7 @@ func RawTxInSignature(tx *wire.MsgTx, idx int, subScript []byte, if err != nil { return nil, err } - signature, err := key.Sign(hash) - if err != nil { - return nil, fmt.Errorf("cannot sign tx input: %s", err) - } + signature := ecdsa.Sign(key, hash) return append(signature.Serialize(), byte(hashType)), nil } @@ -95,7 +89,7 @@ func SignatureScript(tx *wire.MsgTx, idx int, subscript []byte, hashType SigHash return nil, err } - pk := (*btcec.PublicKey)(&privKey.PublicKey) + pk := privKey.PubKey() var pkData []byte if compress { pkData = pk.SerializeCompressed() @@ -270,7 +264,7 @@ sigLoop: tSig := sig[:len(sig)-1] hashType := SigHashType(sig[len(sig)-1]) - pSig, err := btcec.ParseDERSignature(tSig, btcec.S256()) + pSig, err := ecdsa.ParseDERSignature(tSig) if err != nil { continue } diff --git a/txscript/sign_test.go b/txscript/sign_test.go index eca12c18..0017198f 100644 --- a/txscript/sign_test.go +++ b/txscript/sign_test.go @@ -9,7 +9,7 @@ import ( "fmt" "testing" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash" @@ -143,15 +143,14 @@ func TestSignTxOutput(t *testing.T) { for _, hashType := range hashTypes { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeUncompressed() + pk := key.PubKey().SerializeUncompressed() address, err := btcutil.NewAddressPubKeyHash( btcutil.Hash160(pk), &chaincfg.TestNet3Params) if err != nil { @@ -180,15 +179,14 @@ func TestSignTxOutput(t *testing.T) { for _, hashType := range hashTypes { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeUncompressed() + pk := key.PubKey().SerializeUncompressed() address, err := btcutil.NewAddressPubKeyHash( btcutil.Hash160(pk), &chaincfg.TestNet3Params) if err != nil { @@ -241,15 +239,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeCompressed() + pk := key.PubKey().SerializeCompressed() address, err := btcutil.NewAddressPubKeyHash( btcutil.Hash160(pk), &chaincfg.TestNet3Params) if err != nil { @@ -280,15 +277,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeCompressed() + pk := key.PubKey().SerializeCompressed() address, err := btcutil.NewAddressPubKeyHash( btcutil.Hash160(pk), &chaincfg.TestNet3Params) if err != nil { @@ -342,15 +338,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeUncompressed() + pk := key.PubKey().SerializeUncompressed() address, err := btcutil.NewAddressPubKey(pk, &chaincfg.TestNet3Params) if err != nil { @@ -381,15 +376,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeUncompressed() + pk := key.PubKey().SerializeUncompressed() address, err := btcutil.NewAddressPubKey(pk, &chaincfg.TestNet3Params) if err != nil { @@ -442,15 +436,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeCompressed() + pk := key.PubKey().SerializeCompressed() address, err := btcutil.NewAddressPubKey(pk, &chaincfg.TestNet3Params) if err != nil { @@ -481,15 +474,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeCompressed() + pk := key.PubKey().SerializeCompressed() address, err := btcutil.NewAddressPubKey(pk, &chaincfg.TestNet3Params) if err != nil { @@ -543,15 +535,14 @@ func TestSignTxOutput(t *testing.T) { for _, hashType := range hashTypes { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeUncompressed() + pk := key.PubKey().SerializeUncompressed() address, err := btcutil.NewAddressPubKeyHash( btcutil.Hash160(pk), &chaincfg.TestNet3Params) if err != nil { @@ -600,15 +591,14 @@ func TestSignTxOutput(t *testing.T) { for _, hashType := range hashTypes { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeUncompressed() + pk := key.PubKey().SerializeUncompressed() address, err := btcutil.NewAddressPubKeyHash( btcutil.Hash160(pk), &chaincfg.TestNet3Params) if err != nil { @@ -683,15 +673,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeCompressed() + pk := key.PubKey().SerializeCompressed() address, err := btcutil.NewAddressPubKeyHash( btcutil.Hash160(pk), &chaincfg.TestNet3Params) if err != nil { @@ -740,15 +729,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeCompressed() + pk := key.PubKey().SerializeCompressed() address, err := btcutil.NewAddressPubKeyHash( btcutil.Hash160(pk), &chaincfg.TestNet3Params) if err != nil { @@ -822,15 +810,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeUncompressed() + pk := key.PubKey().SerializeUncompressed() address, err := btcutil.NewAddressPubKey(pk, &chaincfg.TestNet3Params) if err != nil { @@ -879,15 +866,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeUncompressed() + pk := key.PubKey().SerializeUncompressed() address, err := btcutil.NewAddressPubKey(pk, &chaincfg.TestNet3Params) if err != nil { @@ -960,15 +946,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeCompressed() + pk := key.PubKey().SerializeCompressed() address, err := btcutil.NewAddressPubKey(pk, &chaincfg.TestNet3Params) if err != nil { @@ -1016,15 +1001,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key, err := btcec.NewPrivateKey(btcec.S256()) + key, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk := (*btcec.PublicKey)(&key.PublicKey). - SerializeCompressed() + pk := key.PubKey().SerializeCompressed() address, err := btcutil.NewAddressPubKey(pk, &chaincfg.TestNet3Params) if err != nil { @@ -1097,15 +1081,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key1, err := btcec.NewPrivateKey(btcec.S256()) + key1, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk1 := (*btcec.PublicKey)(&key1.PublicKey). - SerializeCompressed() + pk1 := key1.PubKey().SerializeCompressed() address1, err := btcutil.NewAddressPubKey(pk1, &chaincfg.TestNet3Params) if err != nil { @@ -1114,15 +1097,14 @@ func TestSignTxOutput(t *testing.T) { break } - key2, err := btcec.NewPrivateKey(btcec.S256()) + key2, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey 2 for %s: %v", msg, err) break } - pk2 := (*btcec.PublicKey)(&key2.PublicKey). - SerializeCompressed() + pk2 := key2.PubKey().SerializeCompressed() address2, err := btcutil.NewAddressPubKey(pk2, &chaincfg.TestNet3Params) if err != nil { @@ -1173,15 +1155,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key1, err := btcec.NewPrivateKey(btcec.S256()) + key1, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk1 := (*btcec.PublicKey)(&key1.PublicKey). - SerializeCompressed() + pk1 := key1.PubKey().SerializeCompressed() address1, err := btcutil.NewAddressPubKey(pk1, &chaincfg.TestNet3Params) if err != nil { @@ -1190,15 +1171,14 @@ func TestSignTxOutput(t *testing.T) { break } - key2, err := btcec.NewPrivateKey(btcec.S256()) + key2, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey 2 for %s: %v", msg, err) break } - pk2 := (*btcec.PublicKey)(&key2.PublicKey). - SerializeCompressed() + pk2 := key2.PubKey().SerializeCompressed() address2, err := btcutil.NewAddressPubKey(pk2, &chaincfg.TestNet3Params) if err != nil { @@ -1279,15 +1259,14 @@ func TestSignTxOutput(t *testing.T) { for i := range tx.TxIn { msg := fmt.Sprintf("%d:%d", hashType, i) - key1, err := btcec.NewPrivateKey(btcec.S256()) + key1, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey for %s: %v", msg, err) break } - pk1 := (*btcec.PublicKey)(&key1.PublicKey). - SerializeCompressed() + pk1 := key1.PubKey().SerializeCompressed() address1, err := btcutil.NewAddressPubKey(pk1, &chaincfg.TestNet3Params) if err != nil { @@ -1296,15 +1275,14 @@ func TestSignTxOutput(t *testing.T) { break } - key2, err := btcec.NewPrivateKey(btcec.S256()) + key2, err := btcec.NewPrivateKey() if err != nil { t.Errorf("failed to make privKey 2 for %s: %v", msg, err) break } - pk2 := (*btcec.PublicKey)(&key2.PublicKey). - SerializeCompressed() + pk2 := key2.PubKey().SerializeCompressed() address2, err := btcutil.NewAddressPubKey(pk2, &chaincfg.TestNet3Params) if err != nil { @@ -1635,7 +1613,7 @@ var sigScriptTests = []tstSigScript{ func TestSignatureScript(t *testing.T) { t.Parallel() - privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyD) + privKey, _ := btcec.PrivKeyFromBytes(privKeyD) nexttest: for i := range sigScriptTests { diff --git a/txscript/standard.go b/txscript/standard.go index e70fdc2c..25a5a04e 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -7,7 +7,7 @@ package txscript import ( "fmt" - "github.com/utreexo/utreexod/btcec" + "github.com/btcsuite/btcd/btcec/v2" "github.com/utreexo/utreexod/btcutil" "github.com/utreexo/utreexod/chaincfg" "github.com/utreexo/utreexod/chaincfg/chainhash"