2019-01-09 03:18:06 +03:00
|
|
|
package netann
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2020-04-06 03:06:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2020-04-28 11:06:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2019-01-09 03:18:06 +03:00
|
|
|
"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 {
|
2020-04-28 11:06:27 +03:00
|
|
|
keySigner keychain.SingleKeyDigestSigner
|
2019-01-09 03:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeSigner creates a new instance of the NodeSigner backed by the target
|
|
|
|
// private key.
|
2020-04-28 11:06:27 +03:00
|
|
|
func NewNodeSigner(keySigner keychain.SingleKeyDigestSigner) *NodeSigner {
|
2019-01-09 03:18:06 +03:00
|
|
|
return &NodeSigner{
|
2020-04-28 11:06:27 +03:00
|
|
|
keySigner: keySigner,
|
2019-01-09 03:18:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2020-04-06 03:06:38 +03:00
|
|
|
msg []byte) (input.Signature, error) {
|
2019-01-09 03:18:06 +03:00
|
|
|
|
|
|
|
// If this isn't our identity public key, then we'll exit early with an
|
|
|
|
// error as we can't sign with this key.
|
2020-04-28 11:06:27 +03:00
|
|
|
if !pubKey.IsEqual(n.keySigner.PubKey()) {
|
2019-01-09 03:18:06 +03:00
|
|
|
return nil, fmt.Errorf("unknown public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll sign the dsha256 of the target message.
|
2020-04-28 11:06:27 +03:00
|
|
|
var digest [32]byte
|
|
|
|
copy(digest[:], chainhash.DoubleHashB(msg))
|
|
|
|
sig, err := n.keySigner.SignDigest(digest)
|
2019-01-09 03:18:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't sign the message: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-06 03:06:38 +03:00
|
|
|
return sig, nil
|
2019-01-09 03:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignCompact signs a 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) SignCompact(msg []byte) ([]byte, error) {
|
|
|
|
// We'll sign the dsha256 of the target message.
|
|
|
|
digest := chainhash.DoubleHashB(msg)
|
|
|
|
|
|
|
|
return n.SignDigestCompact(digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignDigestCompact signs the provided message digest under the resident
|
|
|
|
// node's private key. The returned signature is a pubkey-recoverable signature.
|
|
|
|
func (n *NodeSigner) SignDigestCompact(hash []byte) ([]byte, error) {
|
2020-04-28 11:06:27 +03:00
|
|
|
var digest [32]byte
|
|
|
|
copy(digest[:], hash)
|
2019-01-09 03:18:06 +03:00
|
|
|
|
2020-04-28 11:06:27 +03:00
|
|
|
// keychain.SignDigestCompact returns a pubkey-recoverable signature.
|
|
|
|
sig, err := n.keySigner.SignDigestCompact(digest)
|
2019-01-09 03:18:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't sign the hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure that NodeSigner implements the MessageSigner
|
|
|
|
// interface.
|
|
|
|
var _ lnwallet.MessageSigner = (*NodeSigner)(nil)
|