mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-19 05:45:21 +01:00
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.
This commit is contained in:
parent
fbf766e3c6
commit
a23715a9c7
@ -3,8 +3,10 @@ package btcwallet
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-errors/errors"
|
||||||
"github.com/lightningnetwork/lnd/lnwallet"
|
"github.com/lightningnetwork/lnd/lnwallet"
|
||||||
"github.com/roasbeef/btcd/btcec"
|
"github.com/roasbeef/btcd/btcec"
|
||||||
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
||||||
"github.com/roasbeef/btcd/txscript"
|
"github.com/roasbeef/btcd/txscript"
|
||||||
"github.com/roasbeef/btcd/wire"
|
"github.com/roasbeef/btcd/wire"
|
||||||
"github.com/roasbeef/btcutil"
|
"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
|
// A compile time check to ensure that BtcWallet implements the Signer
|
||||||
// interface.
|
// interface.
|
||||||
var _ lnwallet.Signer = (*BtcWallet)(nil)
|
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
|
||||||
|
}
|
||||||
|
33
lnwallet/signer.go
Normal file
33
lnwallet/signer.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user