mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-04-12 14:09:06 +02:00
In this commit, we pass the MessageSignerRing around in places where Schnorr signing will be needed to sign Gossip 1.75 messages.
95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
package netann
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
|
"github.com/lightningnetwork/lnd/keychain"
|
|
)
|
|
|
|
// NodeSigner is an implementation of the MessageSigner interface backed by the
|
|
// identity private key of running lnd node.
|
|
type NodeSigner struct {
|
|
keySigner keychain.SingleKeyMessageSigner
|
|
}
|
|
|
|
// NewNodeSigner creates a new instance of the NodeSigner backed by the target
|
|
// private key.
|
|
func NewNodeSigner(keySigner keychain.SingleKeyMessageSigner) *NodeSigner {
|
|
return &NodeSigner{
|
|
keySigner: keySigner,
|
|
}
|
|
}
|
|
|
|
// SignMessage signs a double-sha256 digest of the passed msg under the
|
|
// 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, doubleHash bool) (*ecdsa.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.
|
|
if keyLoc != n.keySigner.KeyLocator() {
|
|
return nil, fmt.Errorf("unknown public key locator")
|
|
}
|
|
|
|
// Otherwise, we'll sign the double-sha256 of the target message.
|
|
sig, err := n.keySigner.SignMessage(msg, doubleHash)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("can't sign the message: %w", err)
|
|
}
|
|
|
|
return sig, nil
|
|
}
|
|
|
|
// SignMessageCompactNoKeyLoc signs a single or double sha256 digest of the msg
|
|
// parameter under the resident node's private key. The returned signature is a
|
|
// pubkey-recoverable signature. No key locator is required for this since the
|
|
// NodeSigner already has the key to sign with.
|
|
func (n *NodeSigner) SignMessageCompactNoKeyLoc(msg []byte, doubleHash bool) (
|
|
[]byte, error) {
|
|
|
|
return n.keySigner.SignMessageCompact(msg, doubleHash)
|
|
}
|
|
|
|
// SignMessageCompact signs the given message, single or double SHA256 hashing
|
|
// it first, with the private key described in the key locator and returns the
|
|
// signature in the compact, public key recoverable format.
|
|
//
|
|
// NOTE: this is part of the keychain.MessageSignerRing interface.
|
|
func (n *NodeSigner) SignMessageCompact(keyLoc keychain.KeyLocator, msg []byte,
|
|
doubleHash bool) ([]byte, 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.
|
|
if keyLoc != n.keySigner.KeyLocator() {
|
|
return nil, fmt.Errorf("unknown public key locator")
|
|
}
|
|
|
|
return n.SignMessageCompactNoKeyLoc(msg, doubleHash)
|
|
}
|
|
|
|
// SignMessageSchnorr signs the given message, single or double SHA256 hashing
|
|
// it first, with the private key described in the key locator and the optional
|
|
// Taproot tweak applied to the private key.
|
|
//
|
|
// NOTE: this is part of the keychain.MessageSignerRing interface.
|
|
func (n *NodeSigner) SignMessageSchnorr(keyLoc keychain.KeyLocator, msg []byte,
|
|
doubleHash bool, taprootTweak, tag []byte) (*schnorr.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.
|
|
if keyLoc != n.keySigner.KeyLocator() {
|
|
return nil, fmt.Errorf("unknown public key locator")
|
|
}
|
|
|
|
return n.keySigner.SignMessageSchnorr(
|
|
keyLoc, msg, doubleHash, taprootTweak, tag,
|
|
)
|
|
}
|
|
|
|
// A compile time check to ensure that NodeSigner implements the MessageSigner
|
|
// interface.
|
|
var _ keychain.MessageSignerRing = (*NodeSigner)(nil)
|