multi: refactor SignDigest into SignMessage

To make it possible to use a remote signrpc server as a signer for our
wallet, we need to change our main interface to sign the message instead
of the message's digest. Otherwise we'd need to alter the
signrpc.SignMessage RPC to accept a digest instead of only the message
which has security implications.
This commit is contained in:
Oliver Gugger
2021-09-23 16:54:26 +02:00
parent dd3719d5de
commit 02757f6735
9 changed files with 123 additions and 86 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/wallet"
"github.com/btcsuite/btcwallet/walletdb"
@@ -390,18 +391,25 @@ func (b *BtcWalletKeyRing) ECDH(keyDesc KeyDescriptor,
return h, nil
}
// SignDigest signs the given SHA256 message digest with the private key
// described in the key descriptor.
// SignMessage signs the given message, single or double SHA256 hashing it
// first, with the private key described in the key descriptor.
//
// NOTE: This is part of the keychain.DigestSignerRing interface.
func (b *BtcWalletKeyRing) SignDigest(keyDesc KeyDescriptor,
digest [32]byte) (*btcec.Signature, error) {
func (b *BtcWalletKeyRing) SignMessage(keyDesc KeyDescriptor,
msg []byte, doubleHash bool) (*btcec.Signature, error) {
privKey, err := b.DerivePrivKey(keyDesc)
if err != nil {
return nil, err
}
return privKey.Sign(digest[:])
var digest []byte
if doubleHash {
digest = chainhash.DoubleHashB(msg)
} else {
digest = chainhash.HashB(msg)
}
return privKey.Sign(digest)
}
// SignDigestCompact signs the given SHA256 message digest with the private key

View File

@@ -191,10 +191,10 @@ type SecretKeyRing interface {
// DigestSignerRing is an interface that abstracts away basic low-level ECDSA
// signing on keys within a key ring.
type DigestSignerRing interface {
// SignDigest signs the given SHA256 message digest with the private key
// described in the key descriptor.
SignDigest(keyDesc KeyDescriptor, digest [32]byte) (*btcec.Signature,
error)
// SignMessage signs the given message, single or double SHA256 hashing
// it first, with the private key described in the key descriptor.
SignMessage(keyDesc KeyDescriptor, message []byte,
doubleHash bool) (*btcec.Signature, error)
// SignDigestCompact signs the given SHA256 message digest with the
// private key described in the key descriptor and returns the signature
@@ -209,9 +209,9 @@ type SingleKeyDigestSigner interface {
// PubKey returns the public key of the wrapped private key.
PubKey() *btcec.PublicKey
// SignDigest signs the given SHA256 message digest with the wrapped
// private key.
SignDigest(digest [32]byte) (*btcec.Signature, error)
// 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)
// SignDigestCompact signs the given SHA256 message digest with the
// wrapped private key and returns the signature in the compact, public

View File

@@ -1,6 +1,9 @@
package keychain
import "github.com/btcsuite/btcd/btcec"
import (
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg/chainhash"
)
func NewPubKeyDigestSigner(keyDesc KeyDescriptor,
signer DigestSignerRing) *PubKeyDigestSigner {
@@ -20,10 +23,10 @@ func (p *PubKeyDigestSigner) PubKey() *btcec.PublicKey {
return p.keyDesc.PubKey
}
func (p *PubKeyDigestSigner) SignDigest(digest [32]byte) (*btcec.Signature,
error) {
func (p *PubKeyDigestSigner) SignMessage(message []byte,
doubleHash bool) (*btcec.Signature, error) {
return p.digestSigner.SignDigest(p.keyDesc, digest)
return p.digestSigner.SignMessage(p.keyDesc, message, doubleHash)
}
func (p *PubKeyDigestSigner) SignDigestCompact(digest [32]byte) ([]byte,
@@ -40,10 +43,16 @@ func (p *PrivKeyDigestSigner) PubKey() *btcec.PublicKey {
return p.PrivKey.PubKey()
}
func (p *PrivKeyDigestSigner) SignDigest(digest [32]byte) (*btcec.Signature,
error) {
func (p *PrivKeyDigestSigner) SignMessage(msg []byte,
doubleHash bool) (*btcec.Signature, error) {
return p.PrivKey.Sign(digest[:])
var digest []byte
if doubleHash {
digest = chainhash.DoubleHashB(msg)
} else {
digest = chainhash.HashB(msg)
}
return p.PrivKey.Sign(digest)
}
func (p *PrivKeyDigestSigner) SignDigestCompact(digest [32]byte) ([]byte,