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 Nov 19, 2021
1 parent 11bcb4c commit 9db9e0d
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 105 deletions.
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion 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
94 changes: 5 additions & 89 deletions btcec/example_test.go
Expand Up @@ -8,7 +8,7 @@ import (
"encoding/hex"
"fmt"

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

Expand All @@ -22,16 +22,12 @@ func Example_signMessage() {
fmt.Println(err)
return
}
privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes)
privKey, pubKey := btcec.PrivKeyFromBytes(pkBytes)

// Sign a message using the private key.
message := "test message"
messageHash := chainhash.DoubleHashB([]byte(message))
signature, err := privKey.Sign(messageHash)
if err != nil {
fmt.Println(err)
return
}
signature := btcec.Sign(privKey, messageHash)

// Serialize and display the signature.
fmt.Printf("Serialized Signature: %x\n", signature.Serialize())
Expand All @@ -56,7 +52,7 @@ func Example_verifySignature() {
fmt.Println(err)
return
}
pubKey, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256())
pubKey, err := btcec.ParsePubKey(pubKeyBytes)
if err != nil {
fmt.Println(err)
return
Expand All @@ -71,7 +67,7 @@ func Example_verifySignature() {
fmt.Println(err)
return
}
signature, err := btcec.ParseSignature(sigBytes, btcec.S256())
signature, err := btcec.ParseSignature(sigBytes)
if err != nil {
fmt.Println(err)
return
Expand All @@ -86,83 +82,3 @@ func Example_verifySignature() {
// Output:
// Signature Verified? true
}

// This example demonstrates encrypting a message for a public key that is first
// parsed from raw bytes, then decrypting it using the corresponding private key.
func Example_encryptMessage() {
// Decode the hex-encoded pubkey of the recipient.
pubKeyBytes, err := hex.DecodeString("04115c42e757b2efb7671c578530ec191a1" +
"359381e6a71127a9d37c486fd30dae57e76dc58f693bd7e7010358ce6b165e483a29" +
"21010db67ac11b1b51b651953d2") // uncompressed pubkey
if err != nil {
fmt.Println(err)
return
}
pubKey, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256())
if err != nil {
fmt.Println(err)
return
}

// Encrypt a message decryptable by the private key corresponding to pubKey
message := "test message"
ciphertext, err := btcec.Encrypt(pubKey, []byte(message))
if err != nil {
fmt.Println(err)
return
}

// Decode the hex-encoded private key.
pkBytes, err := hex.DecodeString("a11b0a4e1a132305652ee7a8eb7848f6ad" +
"5ea381e3ce20a2c086a2e388230811")
if err != nil {
fmt.Println(err)
return
}
// note that we already have corresponding pubKey
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes)

// Try decrypting and verify if it's the same message.
plaintext, err := btcec.Decrypt(privKey, ciphertext)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(string(plaintext))

// Output:
// test message
}

// This example demonstrates decrypting a message using a private key that is
// first parsed from raw bytes.
func Example_decryptMessage() {
// Decode the hex-encoded private key.
pkBytes, err := hex.DecodeString("a11b0a4e1a132305652ee7a8eb7848f6ad" +
"5ea381e3ce20a2c086a2e388230811")
if err != nil {
fmt.Println(err)
return
}

privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes)

ciphertext, err := hex.DecodeString("35f644fbfb208bc71e57684c3c8b437402ca" +
"002047a2f1b38aa1a8f1d5121778378414f708fe13ebf7b4a7bb74407288c1958969" +
"00207cf4ac6057406e40f79961c973309a892732ae7a74ee96cd89823913b8b8d650" +
"a44166dc61ea1c419d47077b748a9c06b8d57af72deb2819d98a9d503efc59fc8307" +
"d14174f8b83354fac3ff56075162")

// Try decrypting the message.
plaintext, err := btcec.Decrypt(privKey, ciphertext)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(string(plaintext))

// Output:
// test message
}
5 changes: 4 additions & 1 deletion go.mod
@@ -1,6 +1,7 @@
module github.com/btcsuite/btcd

require (
github.com/btcsuite/btcec/v2 v2.0.0
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,6 @@ require (
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
)

go 1.16
replace github.com/btcsuite/btcec/v2 => ./btcec

go 1.17
3 changes: 2 additions & 1 deletion 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 Down
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion 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
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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion 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
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
2 changes: 1 addition & 1 deletion txscript/sigcache_test.go
Expand Up @@ -8,7 +8,7 @@ import (
"crypto/rand"
"testing"

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

Expand Down
2 changes: 1 addition & 1 deletion txscript/sign.go
Expand Up @@ -8,7 +8,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/sign_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"testing"

"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/wire"
Expand Down

0 comments on commit 9db9e0d

Please sign in to comment.