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 8c921bc commit bdcd742
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 130 deletions.
8 changes: 4 additions & 4 deletions blockchain/compress.go
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
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
@@ -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
@@ -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
5 changes: 3 additions & 2 deletions integration/csv_fork_test.go
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 All @@ -35,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
}
Expand Down
1 change: 1 addition & 0 deletions integration/rpcserver_test.go
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 Down
17 changes: 13 additions & 4 deletions integration/rpctest/memwallet.go
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
1 change: 1 addition & 0 deletions integration/rpctest/rpc_harness_test.go
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 rpctest
Expand Down
4 changes: 2 additions & 2 deletions mempool/mempool_test.go
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 Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions mempool/policy_test.go
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 Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions rpcserver.go
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
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
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
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
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
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

0 comments on commit bdcd742

Please sign in to comment.