Skip to content

Commit

Permalink
multi: refactor SignMessage to specify hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Oct 14, 2021
1 parent 00af978 commit 6093393
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 17 deletions.
8 changes: 4 additions & 4 deletions funding/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ type Config struct {
// TODO(roasbeef): should instead pass on this responsibility to a
// distinct sub-system?
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
// announcement from the backing Lightning Network node.
Expand Down Expand Up @@ -2911,7 +2911,7 @@ func (f *Manager) newChanAnnouncement(localPubKey,
if err != nil {
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 {
return nil, errors.Errorf("unable to generate channel "+
"update announcement signature: %v", err)
Expand All @@ -2933,13 +2933,13 @@ func (f *Manager) newChanAnnouncement(localPubKey,
if err != nil {
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 {
return nil, errors.Errorf("unable to generate node "+
"signature for channel announcement: %v", err)
}
bitcoinSig, err := f.cfg.SignMessage(
localFundingKey.KeyLocator, chanAnnMsg,
localFundingKey.KeyLocator, chanAnnMsg, true,
)
if err != nil {
return nil, errors.Errorf("unable to generate bitcoin "+
Expand Down
4 changes: 2 additions & 2 deletions funding/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
Notifier: chainNotifier,
FeeEstimator: estimator,
SignMessage: func(_ keychain.KeyLocator,
_ []byte) (*btcec.Signature, error) {
_ []byte, _ bool) (*btcec.Signature, error) {

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

return testSig, nil
},
Expand Down
9 changes: 7 additions & 2 deletions lntest/mock/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *SingleSigner) ComputeInputScript(tx *wire.MsgTx,
// 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.
func (s *SingleSigner) SignMessage(keyLoc keychain.KeyLocator,
msg []byte) (*btcec.Signature, error) {
msg []byte, doubleHash bool) (*btcec.Signature, error) {

mockKeyLoc := s.KeyLoc
if s.KeyLoc.IsEmpty() {
Expand All @@ -127,7 +127,12 @@ func (s *SingleSigner) SignMessage(keyLoc keychain.KeyLocator,
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)
if err != nil {
return nil, fmt.Errorf("can't sign the message: %v", err)
Expand Down
9 changes: 7 additions & 2 deletions lnwallet/btcwallet/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ var _ input.Signer = (*BtcWallet)(nil)
//
// NOTE: This is a part of the MessageSigner interface.
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
// specified public key.
Expand All @@ -279,7 +279,12 @@ func (b *BtcWallet) SignMessage(keyLoc keychain.KeyLocator,
}

// 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)
if err != nil {
return nil, errors.Errorf("unable sign the message: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions lnwallet/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ type MessageSigner interface {
// SignMessage attempts to sign a target message with the private key
// 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
// the double SHA-256 of the passed message.
SignMessage(keyLoc keychain.KeyLocator, msg []byte) (*btcec.Signature,
error)
// the single or double SHA-256 of the passed message.
SignMessage(keyLoc keychain.KeyLocator, msg []byte,
doubleHash bool) (*btcec.Signature, error)
}

// WalletDriver represents a "driver" for a particular concrete
Expand Down
2 changes: 1 addition & 1 deletion netann/channel_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type mockSigner struct {
}

func (m *mockSigner) SignMessage(_ keychain.KeyLocator,
_ []byte) (*btcec.Signature, error) {
_ []byte, _ bool) (*btcec.Signature, error) {

if m.err != nil {
return nil, m.err
Expand Down
4 changes: 2 additions & 2 deletions netann/node_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewNodeSigner(keySigner keychain.SingleKeyMessageSigner) *NodeSigner {
// 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.
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
// error as we can't sign with this key.
Expand All @@ -35,7 +35,7 @@ func (n *NodeSigner) SignMessage(keyLoc keychain.KeyLocator,
}

// 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 {
return nil, fmt.Errorf("can't sign the message: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion netann/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 signer.SignMessage(keyLoc, data)
return signer.SignMessage(keyLoc, data, true)
}

0 comments on commit 6093393

Please sign in to comment.