From a23715a9c759c701b96409656c2de1b9ad337dc7 Mon Sep 17 00:00:00 2001 From: Andrey Samokhvalov Date: Mon, 27 Mar 2017 20:13:40 +0300 Subject: [PATCH] lnwallet: add message signer Added the signer which will be needed in the funding manager to sign the lnwaire announcement message before sending them to discovery package. Also in the future the message signer will be used to sign the users data. --- lnwallet/btcwallet/signer.go | 38 ++++++++++++++++++++++++++++++++++++ lnwallet/signer.go | 33 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 lnwallet/signer.go diff --git a/lnwallet/btcwallet/signer.go b/lnwallet/btcwallet/signer.go index 2579386c..c7373259 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 00000000..db8135ba --- /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 +}