Skip to content

Commit

Permalink
multi: switch project over to using btcec/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Dec 3, 2021
1 parent 1b7a200 commit 2edaf4b
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 127 deletions.
8 changes: 4 additions & 4 deletions blockchain/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package blockchain

import (
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/txscript"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions blockchain/fullblocktests/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"time"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
Expand Down Expand Up @@ -198,7 +198,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{
Expand Down
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/btcsuite/btcd

require (
github.com/btcsuite/btcd/btcec/v2 v2.0.0-00010101000000-000000000000
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd
Expand All @@ -14,4 +15,13 @@ require (
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
)

go 1.16
require (
github.com/aead/siphash v1.0.1 // indirect
github.com/btcsuite/snappy-go v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 // indirect
)

replace github.com/btcsuite/btcd/btcec/v2 => ./btcec/v2

go 1.17
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/aead/siphash v1.0.1 h1:FwHfE/T45KPKYuuSAKyyvE+oPWcaQ+CUmFW0bPlM+kg=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
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/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=
Expand All @@ -22,6 +23,10 @@ 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=
github.com/decred/dcrd/lru v1.0.0 h1:Kbsb1SFDsIlaupWPwsPp+dkxiBY1frcS07PCPgotKz8=
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down
3 changes: 2 additions & 1 deletion integration/csv_fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.

// This file is ignored during the regular tests due to the following build tag.
//go:build rpctest
// +build rpctest

package integration
Expand All @@ -15,7 +16,7 @@ import (
"time"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/integration/rpctest"
Expand Down
17 changes: 13 additions & 4 deletions integration/rpctest/memwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sync"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
Expand Down Expand Up @@ -129,10 +129,15 @@ func newMemWallet(net *chaincfg.Params, harnessID uint32) (*memWallet, error) {
if err != nil {
return nil, err
}
coinbaseKey, err := coinbaseChild.ECPrivKey()
coinbaseKeyOld, err := coinbaseChild.ECPrivKey()
if err != nil {
return nil, err
}

// Some modules weirdness: we need to conver types here as btcutil
// still uses the old version of the module.
coinbaseKey, _ := btcec.PrivKeyFromBytes(coinbaseKeyOld.Serialize())

coinbaseAddr, err := keyToAddr(coinbaseKey, net)
if err != nil {
return nil, err
Expand Down Expand Up @@ -341,11 +346,13 @@ func (m *memWallet) newAddress() (btcutil.Address, error) {
if err != nil {
return nil, err
}
privKey, err := childKey.ECPrivKey()
privKeyOld, err := childKey.ECPrivKey()
if err != nil {
return nil, err
}

privKey, _ := btcec.PrivKeyFromBytes(privKeyOld.Serialize())

addr, err := keyToAddr(privKey, m.net)
if err != nil {
return nil, err
Expand Down Expand Up @@ -514,11 +521,13 @@ func (m *memWallet) CreateTransaction(outputs []*wire.TxOut,
return nil, err
}

privKey, err := extendedKey.ECPrivKey()
privKeyOld, err := extendedKey.ECPrivKey()
if err != nil {
return nil, err
}

privKey, _ := btcec.PrivKeyFromBytes(privKeyOld.Serialize())

sigScript, err := txscript.SignatureScript(tx, i, utxo.pkScript,
txscript.SigHashAll, privKey, true)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
Expand Down
2 changes: 1 addition & 1 deletion mempool/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
Expand Down
12 changes: 9 additions & 3 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/blockchain/indexers"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand Down Expand Up @@ -3512,7 +3512,13 @@ 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,
// Another instance of modules, weirdness: we'll need to convert things
// back over to a private key format we can use.
//
// TODO(roasbeef): remove after updating btcutil
privKey, _ := btcec.PrivKeyFromBytes(wif.PrivKey.Serialize())

sig, err := btcec.SignCompact(privKey,
messageHash, wif.CompressPubKey)
if err != nil {
return nil, &btcjson.RPCError{
Expand Down Expand Up @@ -3707,7 +3713,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,
pk, wasCompressed, err := btcec.RecoverCompact(sig,
expectedMessageHash)
if err != nil {
// Mirror Bitcoin Core behavior, which treats error in
Expand Down
2 changes: 1 addition & 1 deletion txscript/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"math/big"
"strings"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire"
)

Expand Down
4 changes: 2 additions & 2 deletions txscript/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"encoding/hex"
"fmt"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions txscript/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

"golang.org/x/crypto/ripemd160"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
)
Expand Down Expand Up @@ -1929,7 +1929,7 @@ 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
Expand All @@ -1939,9 +1939,9 @@ func opcodeCheckSig(op *opcode, data []byte, vm *Engine) error {
if vm.hasFlag(ScriptVerifyStrictEncoding) ||
vm.hasFlag(ScriptVerifyDERSignatures) {

signature, err = btcec.ParseDERSignature(sigBytes, btcec.S256())
signature, err = btcec.ParseDERSignature(sigBytes)
} else {
signature, err = btcec.ParseSignature(sigBytes, btcec.S256())
signature, err = btcec.ParseSignature(sigBytes)
}
if err != nil {
vm.dstack.PushBool(false)
Expand Down Expand Up @@ -2148,11 +2148,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 = btcec.ParseDERSignature(signature)
} else {
parsedSig, err = btcec.ParseSignature(signature,
btcec.S256())
parsedSig, err = btcec.ParseSignature(signature)
}
sigInfo.parsed = true
if err != nil {
Expand All @@ -2174,7 +2172,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
}
Expand Down
2 changes: 1 addition & 1 deletion txscript/pkscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
Expand Down
2 changes: 1 addition & 1 deletion txscript/sigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package txscript
import (
"sync"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg/chainhash"
)

Expand Down
31 changes: 17 additions & 14 deletions txscript/sigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"crypto/rand"
"testing"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/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())
privKey, err := btcec.NewPrivateKey()
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -26,10 +26,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 := btcec.Sign(privKey, msgHash[:])

return &msgHash, sig, privKey.PubKey(), nil
}
Expand All @@ -49,8 +46,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, _ := btcec.ParseSignature(sig1.Serialize())
key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed())
if !sigCache.Exists(*msg1, sig1Copy, key1Copy) {
t.Errorf("previously added item not found in signature cache")
}
Expand All @@ -73,8 +70,14 @@ 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, err := btcec.ParseSignature(sig.Serialize())
if err != nil {
t.Fatalf("unable to parse sig: %v", err)
}
keyCopy, err := btcec.ParsePubKey(key.SerializeCompressed())
if err != nil {
t.Fatalf("unable to parse key: %v", err)
}
if !sigCache.Exists(*msg, sigCopy, keyCopy) {
t.Errorf("previously added item not found in signature" +
"cache")
Expand Down Expand Up @@ -102,8 +105,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, _ := btcec.ParseSignature(sigNew.Serialize())
keyNewCopy, _ := btcec.ParsePubKey(keyNew.SerializeCompressed())
if !sigCache.Exists(*msgNew, sigNewCopy, keyNewCopy) {
t.Fatalf("previously added item not found in signature cache")
}
Expand All @@ -125,8 +128,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, _ := btcec.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")
Expand Down
Loading

0 comments on commit 2edaf4b

Please sign in to comment.