mirror of
https://github.com/aljazceru/breez-lnd.git
synced 2025-12-17 06:04:20 +01:00
To make it possible to use a remote lnrpc 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 lnrpc.SignMessage RPC to accept a digest instead of only the message which has security implications.
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package netann
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
"github.com/lightningnetwork/lnd/input"
|
|
"github.com/lightningnetwork/lnd/keychain"
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
)
|
|
|
|
// 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. If the target public key is _not_ the node's
|
|
// private key, then an error will be returned.
|
|
func (n *NodeSigner) SignMessage(pubKey *btcec.PublicKey,
|
|
msg []byte) (input.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 !pubKey.IsEqual(n.keySigner.PubKey()) {
|
|
return nil, fmt.Errorf("unknown public key")
|
|
}
|
|
|
|
// Otherwise, we'll sign the double-sha256 of the target message.
|
|
sig, err := n.keySigner.SignMessage(msg, true)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("can't sign the message: %v", err)
|
|
}
|
|
|
|
return sig, nil
|
|
}
|
|
|
|
// SignMessageCompact 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.
|
|
func (n *NodeSigner) SignMessageCompact(msg []byte, doubleHash bool) ([]byte,
|
|
error) {
|
|
|
|
return n.keySigner.SignMessageCompact(msg, doubleHash)
|
|
}
|
|
|
|
// A compile time check to ensure that NodeSigner implements the MessageSigner
|
|
// interface.
|
|
var _ lnwallet.MessageSigner = (*NodeSigner)(nil)
|