diff --git a/lnwallet/btcwallet/signer.go b/lnwallet/btcwallet/signer.go index 2579386ce..c73732591 100644 --- a/lnwallet/btcwallet/signer.go +++ b/lnwallet/btcwallet/signer.go @@ -3,8 +3,10 @@ package btcwallet import ( "fmt" + "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/lnwallet" "github.com/roasbeef/btcd/btcec" + "github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcd/txscript" "github.com/roasbeef/btcd/wire" "github.com/roasbeef/btcutil" @@ -203,3 +205,39 @@ func (b *BtcWallet) ComputeInputScript(tx *wire.MsgTx, // A compile time check to ensure that BtcWallet implements the Signer // interface. var _ lnwallet.Signer = (*BtcWallet)(nil) + +// FundingSigner responsible for signing the data with the keys that were +// generated by the wallet. At the moment of creation it is used for signing +// the data with funding keys. +type FundingSigner struct { + wallet *BtcWallet +} + +// NewFundingSigner creates new instance of signer. +func NewFundingSigner(wallet *BtcWallet) *FundingSigner { + return &FundingSigner{ + wallet: wallet, + } +} + +// SignData sign given data with the private key which corresponds to +// the given public key. +// This is a part of the DataSigner interface. +func (s *FundingSigner) SignData(data []byte, + pubKey *btcec.PublicKey) (*btcec.Signature, error) { + + // First attempt to fetch the private key which corresponds to the + // specified public key. + privKey, err := s.wallet.fetchPrivKey(pubKey) + if err != nil { + return nil, err + } + + // Double hash and sign data. + sign, err := privKey.Sign(chainhash.DoubleHashB(data)) + if err != nil { + return nil, errors.Errorf("unable sign the message: %v", err) + } + + return sign, nil +} diff --git a/lnwallet/signer.go b/lnwallet/signer.go new file mode 100644 index 000000000..db8135ba2 --- /dev/null +++ b/lnwallet/signer.go @@ -0,0 +1,33 @@ +package lnwallet + +import ( + "fmt" + + "github.com/roasbeef/btcd/btcec" + "github.com/roasbeef/btcd/chaincfg/chainhash" +) + +// MessageSigner is used for creation the signatures using the node identity key. +// By message we mean the whole range of data that might require our approve, +// starting from node, channel, channel update announcements and ending by user +// data. +type MessageSigner struct { + identityKey *btcec.PrivateKey +} + +// NewMessageSigner returns the new instance of message signer. +func NewMessageSigner(key *btcec.PrivateKey) *MessageSigner { + return &MessageSigner{ + identityKey: key, + } +} + +// SignData sign the message with the node private key. +func (s *MessageSigner) SignData(data []byte) (*btcec.Signature, error) { + sign, err := s.identityKey.Sign(chainhash.DoubleHashB(data)) + if err != nil { + return nil, fmt.Errorf("can't sign the message: %v", err) + } + + return sign, nil +}