github.com/decred/dcrlnd@v0.7.6/netann/node_signer.go (about)

     1  package netann
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
     7  	"github.com/decred/dcrlnd/keychain"
     8  	"github.com/decred/dcrlnd/lnwallet"
     9  )
    10  
    11  // NodeSigner is an implementation of the MessageSigner interface backed by the
    12  // identity private key of running lnd node.
    13  type NodeSigner struct {
    14  	keySigner keychain.SingleKeyMessageSigner
    15  }
    16  
    17  // NewNodeSigner creates a new instance of the NodeSigner backed by the target
    18  // private key.
    19  func NewNodeSigner(keySigner keychain.SingleKeyMessageSigner) *NodeSigner {
    20  	return &NodeSigner{
    21  		keySigner: keySigner,
    22  	}
    23  }
    24  
    25  // SignMessage signs a double-sha256 digest of the passed msg under the
    26  // resident node's private key described in the key locator. If the target key
    27  // locator is _not_ the node's private key, then an error will be returned.
    28  func (n *NodeSigner) SignMessage(keyLoc keychain.KeyLocator,
    29  	msg []byte, doubleHash bool) (*ecdsa.Signature, error) {
    30  
    31  	// If this isn't our identity public key, then we'll exit early with an
    32  	// error as we can't sign with this key.
    33  	if keyLoc != n.keySigner.KeyLocator() {
    34  		return nil, fmt.Errorf("unknown public key locator")
    35  	}
    36  
    37  	// Otherwise, we'll sign the chainhash of the target message.
    38  	sig, err := n.keySigner.SignMessage(msg, doubleHash)
    39  	if err != nil {
    40  		return nil, fmt.Errorf("can't sign the message: %v", err)
    41  	}
    42  
    43  	return sig, nil
    44  }
    45  
    46  // SignMessageCompact signs a single or double chainhash digest of the msg
    47  // parameter under the resident node's private key. The returned signature is a
    48  // pubkey-recoverable signature.
    49  func (n *NodeSigner) SignMessageCompact(msg []byte, doubleHash bool) ([]byte,
    50  	error) {
    51  
    52  	return n.keySigner.SignMessageCompact(msg, doubleHash)
    53  }
    54  
    55  // A compile time check to ensure that NodeSigner implements the MessageSigner
    56  // interface.
    57  var _ lnwallet.MessageSigner = (*NodeSigner)(nil)