multi: use btcd's btcec/v2 and btcutil modules

This commit was previously split into the following parts to ease
review:
 - 2d746f68: replace imports
 - 4008f0fd: use ecdsa.Signature
 - 849e33d1: remove btcec.S256()
 - b8f6ebbd: use v2 library correctly
 - fa80bca9: bump go modules
This commit is contained in:
Oliver Gugger
2022-02-23 14:48:00 +01:00
parent 8ee9fc837b
commit 7dfe4018ce
350 changed files with 2421 additions and 1289 deletions

View File

@@ -3,7 +3,7 @@ package keychain
import (
"testing"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/require"
)

View File

@@ -4,7 +4,8 @@ import (
"crypto/sha256"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/wallet"
@@ -391,12 +392,16 @@ func (b *BtcWalletKeyRing) ECDH(keyDesc KeyDescriptor,
return [32]byte{}, err
}
s := &btcec.PublicKey{}
x, y := btcec.S256().ScalarMult(pub.X, pub.Y, privKey.D.Bytes())
s.X = x
s.Y = y
var (
pubJacobian btcec.JacobianPoint
s btcec.JacobianPoint
)
pub.AsJacobian(&pubJacobian)
h := sha256.Sum256(s.SerializeCompressed())
btcec.ScalarMultNonConst(&privKey.Key, &pubJacobian, &s)
s.ToAffine()
sPubKey := btcec.NewPublicKey(&s.X, &s.Y)
h := sha256.Sum256(sPubKey.SerializeCompressed())
return h, nil
}
@@ -406,7 +411,7 @@ func (b *BtcWalletKeyRing) ECDH(keyDesc KeyDescriptor,
//
// NOTE: This is part of the keychain.MessageSignerRing interface.
func (b *BtcWalletKeyRing) SignMessage(keyLoc KeyLocator,
msg []byte, doubleHash bool) (*btcec.Signature, error) {
msg []byte, doubleHash bool) (*ecdsa.Signature, error) {
privKey, err := b.DerivePrivKey(KeyDescriptor{
KeyLocator: keyLoc,
@@ -421,7 +426,7 @@ func (b *BtcWalletKeyRing) SignMessage(keyLoc KeyLocator,
} else {
digest = chainhash.HashB(msg)
}
return privKey.Sign(digest)
return ecdsa.Sign(privKey, digest), nil
}
// SignMessageCompact signs the given message, single or double SHA256 hashing
@@ -445,5 +450,5 @@ func (b *BtcWalletKeyRing) SignMessageCompact(keyLoc KeyLocator,
} else {
digest = chainhash.HashB(msg)
}
return btcec.SignCompact(btcec.S256(), privKey, digest, true)
return ecdsa.SignCompact(privKey, digest, true)
}

View File

@@ -3,7 +3,8 @@ package keychain
import (
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
)
const (
@@ -209,7 +210,7 @@ type MessageSignerRing interface {
// SignMessage signs the given message, single or double SHA256 hashing
// it first, with the private key described in the key locator.
SignMessage(keyLoc KeyLocator, msg []byte,
doubleHash bool) (*btcec.Signature, error)
doubleHash bool) (*ecdsa.Signature, error)
// SignMessageCompact signs the given message, single or double SHA256
// hashing it first, with the private key described in the key locator
@@ -232,7 +233,7 @@ type SingleKeyMessageSigner interface {
// SignMessage signs the given message, single or double SHA256 hashing
// it first, with the wrapped private key.
SignMessage(message []byte, doubleHash bool) (*btcec.Signature, error)
SignMessage(message []byte, doubleHash bool) (*ecdsa.Signature, error)
// SignMessageCompact signs the given message, single or double SHA256
// hashing it first, with the wrapped private key and returns the

View File

@@ -3,7 +3,7 @@ package keychain
import (
"crypto/sha256"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)
// NewPubKeyECDH wraps the given key of the key ring so it adheres to the
@@ -72,10 +72,16 @@ func (p *PrivKeyECDH) PubKey() *btcec.PublicKey {
//
// NOTE: This is part of the SingleKeyECDH interface.
func (p *PrivKeyECDH) ECDH(pub *btcec.PublicKey) ([32]byte, error) {
s := &btcec.PublicKey{}
s.X, s.Y = btcec.S256().ScalarMult(pub.X, pub.Y, p.PrivKey.D.Bytes())
var (
pubJacobian btcec.JacobianPoint
s btcec.JacobianPoint
)
pub.AsJacobian(&pubJacobian)
return sha256.Sum256(s.SerializeCompressed()), nil
btcec.ScalarMultNonConst(&p.PrivKey.Key, &pubJacobian, &s)
s.ToAffine()
sPubKey := btcec.NewPublicKey(&s.X, &s.Y)
return sha256.Sum256(sPubKey.SerializeCompressed()), nil
}
var _ SingleKeyECDH = (*PubKeyECDH)(nil)

View File

@@ -8,7 +8,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/btcwallet/snacl"
@@ -376,7 +376,7 @@ func TestSecretKeyRingDerivation(t *testing.T) {
// We'll try again, but this time with an
// unknown public key.
_, pub := btcec.PrivKeyFromBytes(
btcec.S256(), testHDSeed[:],
testHDSeed[:],
)
keyDesc.PubKey = pub

View File

@@ -1,7 +1,8 @@
package keychain
import (
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/chaincfg/chainhash"
)
@@ -30,7 +31,7 @@ func (p *PubKeyMessageSigner) KeyLocator() KeyLocator {
}
func (p *PubKeyMessageSigner) SignMessage(message []byte,
doubleHash bool) (*btcec.Signature, error) {
doubleHash bool) (*ecdsa.Signature, error) {
return p.digestSigner.SignMessage(p.keyLoc, message, doubleHash)
}
@@ -64,7 +65,7 @@ func (p *PrivKeyMessageSigner) KeyLocator() KeyLocator {
}
func (p *PrivKeyMessageSigner) SignMessage(msg []byte,
doubleHash bool) (*btcec.Signature, error) {
doubleHash bool) (*ecdsa.Signature, error) {
var digest []byte
if doubleHash {
@@ -72,7 +73,7 @@ func (p *PrivKeyMessageSigner) SignMessage(msg []byte,
} else {
digest = chainhash.HashB(msg)
}
return p.privKey.Sign(digest)
return ecdsa.Sign(p.privKey, digest), nil
}
func (p *PrivKeyMessageSigner) SignMessageCompact(msg []byte,
@@ -84,7 +85,7 @@ func (p *PrivKeyMessageSigner) SignMessageCompact(msg []byte,
} else {
digest = chainhash.HashB(msg)
}
return btcec.SignCompact(btcec.S256(), p.privKey, digest, true)
return ecdsa.SignCompact(p.privKey, digest, true)
}
var _ SingleKeyMessageSigner = (*PubKeyMessageSigner)(nil)