multi: refactor SignMessage to specify hashing

This commit is contained in:
Oliver Gugger
2021-10-14 15:42:44 +02:00
parent 00af978bf2
commit 6093393e2f
8 changed files with 27 additions and 17 deletions

View File

@@ -327,7 +327,7 @@ type Config struct {
// TODO(roasbeef): should instead pass on this responsibility to a // TODO(roasbeef): should instead pass on this responsibility to a
// distinct sub-system? // distinct sub-system?
SignMessage func(keyLoc keychain.KeyLocator, SignMessage func(keyLoc keychain.KeyLocator,
msg []byte) (*btcec.Signature, error) msg []byte, doubleHash bool) (*btcec.Signature, error)
// CurrentNodeAnnouncement should return the latest, fully signed node // CurrentNodeAnnouncement should return the latest, fully signed node
// announcement from the backing Lightning Network node. // announcement from the backing Lightning Network node.
@@ -2911,7 +2911,7 @@ func (f *Manager) newChanAnnouncement(localPubKey,
if err != nil { if err != nil {
return nil, err return nil, err
} }
sig, err := f.cfg.SignMessage(f.cfg.IDKeyLoc, chanUpdateMsg) sig, err := f.cfg.SignMessage(f.cfg.IDKeyLoc, chanUpdateMsg, true)
if err != nil { if err != nil {
return nil, errors.Errorf("unable to generate channel "+ return nil, errors.Errorf("unable to generate channel "+
"update announcement signature: %v", err) "update announcement signature: %v", err)
@@ -2933,13 +2933,13 @@ func (f *Manager) newChanAnnouncement(localPubKey,
if err != nil { if err != nil {
return nil, err return nil, err
} }
nodeSig, err := f.cfg.SignMessage(f.cfg.IDKeyLoc, chanAnnMsg) nodeSig, err := f.cfg.SignMessage(f.cfg.IDKeyLoc, chanAnnMsg, true)
if err != nil { if err != nil {
return nil, errors.Errorf("unable to generate node "+ return nil, errors.Errorf("unable to generate node "+
"signature for channel announcement: %v", err) "signature for channel announcement: %v", err)
} }
bitcoinSig, err := f.cfg.SignMessage( bitcoinSig, err := f.cfg.SignMessage(
localFundingKey.KeyLocator, chanAnnMsg, localFundingKey.KeyLocator, chanAnnMsg, true,
) )
if err != nil { if err != nil {
return nil, errors.Errorf("unable to generate bitcoin "+ return nil, errors.Errorf("unable to generate bitcoin "+

View File

@@ -362,7 +362,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
Notifier: chainNotifier, Notifier: chainNotifier,
FeeEstimator: estimator, FeeEstimator: estimator,
SignMessage: func(_ keychain.KeyLocator, SignMessage: func(_ keychain.KeyLocator,
_ []byte) (*btcec.Signature, error) { _ []byte, _ bool) (*btcec.Signature, error) {
return testSig, nil return testSig, nil
}, },
@@ -510,7 +510,7 @@ func recreateAliceFundingManager(t *testing.T, alice *testNode) {
Notifier: oldCfg.Notifier, Notifier: oldCfg.Notifier,
FeeEstimator: oldCfg.FeeEstimator, FeeEstimator: oldCfg.FeeEstimator,
SignMessage: func(_ keychain.KeyLocator, SignMessage: func(_ keychain.KeyLocator,
_ []byte) (*btcec.Signature, error) { _ []byte, _ bool) (*btcec.Signature, error) {
return testSig, nil return testSig, nil
}, },

View File

@@ -116,7 +116,7 @@ func (s *SingleSigner) ComputeInputScript(tx *wire.MsgTx,
// SignMessage takes a public key and a message and only signs the message // SignMessage takes a public key and a message and only signs the message
// with the stored private key if the public key matches the private key. // with the stored private key if the public key matches the private key.
func (s *SingleSigner) SignMessage(keyLoc keychain.KeyLocator, func (s *SingleSigner) SignMessage(keyLoc keychain.KeyLocator,
msg []byte) (*btcec.Signature, error) { msg []byte, doubleHash bool) (*btcec.Signature, error) {
mockKeyLoc := s.KeyLoc mockKeyLoc := s.KeyLoc
if s.KeyLoc.IsEmpty() { if s.KeyLoc.IsEmpty() {
@@ -127,7 +127,12 @@ func (s *SingleSigner) SignMessage(keyLoc keychain.KeyLocator,
return nil, fmt.Errorf("unknown public key") return nil, fmt.Errorf("unknown public key")
} }
digest := chainhash.DoubleHashB(msg) var digest []byte
if doubleHash {
digest = chainhash.DoubleHashB(msg)
} else {
digest = chainhash.HashB(msg)
}
sign, err := s.Privkey.Sign(digest) sign, err := s.Privkey.Sign(digest)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't sign the message: %v", err) return nil, fmt.Errorf("can't sign the message: %v", err)

View File

@@ -267,7 +267,7 @@ var _ input.Signer = (*BtcWallet)(nil)
// //
// NOTE: This is a part of the MessageSigner interface. // NOTE: This is a part of the MessageSigner interface.
func (b *BtcWallet) SignMessage(keyLoc keychain.KeyLocator, func (b *BtcWallet) SignMessage(keyLoc keychain.KeyLocator,
msg []byte) (*btcec.Signature, error) { msg []byte, doubleHash bool) (*btcec.Signature, error) {
// First attempt to fetch the private key which corresponds to the // First attempt to fetch the private key which corresponds to the
// specified public key. // specified public key.
@@ -279,7 +279,12 @@ func (b *BtcWallet) SignMessage(keyLoc keychain.KeyLocator,
} }
// Double hash and sign the data. // Double hash and sign the data.
msgDigest := chainhash.DoubleHashB(msg) var msgDigest []byte
if doubleHash {
msgDigest = chainhash.DoubleHashB(msg)
} else {
msgDigest = chainhash.HashB(msg)
}
sign, err := privKey.Sign(msgDigest) sign, err := privKey.Sign(msgDigest)
if err != nil { if err != nil {
return nil, errors.Errorf("unable sign the message: %v", err) return nil, errors.Errorf("unable sign the message: %v", err)

View File

@@ -442,9 +442,9 @@ type MessageSigner interface {
// SignMessage attempts to sign a target message with the private key // SignMessage attempts to sign a target message with the private key
// described in the key locator. If the target private key is unable to // described in the key locator. If the target private key is unable to
// be found, then an error will be returned. The actual digest signed is // be found, then an error will be returned. The actual digest signed is
// the double SHA-256 of the passed message. // the single or double SHA-256 of the passed message.
SignMessage(keyLoc keychain.KeyLocator, msg []byte) (*btcec.Signature, SignMessage(keyLoc keychain.KeyLocator, msg []byte,
error) doubleHash bool) (*btcec.Signature, error)
} }
// WalletDriver represents a "driver" for a particular concrete // WalletDriver represents a "driver" for a particular concrete

View File

@@ -18,7 +18,7 @@ type mockSigner struct {
} }
func (m *mockSigner) SignMessage(_ keychain.KeyLocator, func (m *mockSigner) SignMessage(_ keychain.KeyLocator,
_ []byte) (*btcec.Signature, error) { _ []byte, _ bool) (*btcec.Signature, error) {
if m.err != nil { if m.err != nil {
return nil, m.err return nil, m.err

View File

@@ -26,7 +26,7 @@ func NewNodeSigner(keySigner keychain.SingleKeyMessageSigner) *NodeSigner {
// resident node's private key described in the key locator. If the target key // resident node's private key described in the key locator. If the target key
// locator is _not_ the node's private key, then an error will be returned. // locator is _not_ the node's private key, then an error will be returned.
func (n *NodeSigner) SignMessage(keyLoc keychain.KeyLocator, func (n *NodeSigner) SignMessage(keyLoc keychain.KeyLocator,
msg []byte) (*btcec.Signature, error) { msg []byte, doubleHash bool) (*btcec.Signature, error) {
// If this isn't our identity public key, then we'll exit early with an // If this isn't our identity public key, then we'll exit early with an
// error as we can't sign with this key. // error as we can't sign with this key.
@@ -35,7 +35,7 @@ func (n *NodeSigner) SignMessage(keyLoc keychain.KeyLocator,
} }
// Otherwise, we'll sign the double-sha256 of the target message. // Otherwise, we'll sign the double-sha256 of the target message.
sig, err := n.keySigner.SignMessage(msg, true) sig, err := n.keySigner.SignMessage(msg, doubleHash)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't sign the message: %v", err) return nil, fmt.Errorf("can't sign the message: %v", err)
} }

View File

@@ -33,5 +33,5 @@ func SignAnnouncement(signer lnwallet.MessageSigner, keyLoc keychain.KeyLocator,
return nil, fmt.Errorf("unable to get data to sign: %v", err) return nil, fmt.Errorf("unable to get data to sign: %v", err)
} }
return signer.SignMessage(keyLoc, data) return signer.SignMessage(keyLoc, data, true)
} }