From 30441871d3adc2eed3802e8852a449e7006496dd Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 8 Jan 2019 16:18:06 -0800 Subject: [PATCH 1/3] netann/node_signer: copy and export NodeSigner from main --- netann/node_signer.go | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 netann/node_signer.go diff --git a/netann/node_signer.go b/netann/node_signer.go new file mode 100644 index 00000000..8946c2c6 --- /dev/null +++ b/netann/node_signer.go @@ -0,0 +1,82 @@ +package netann + +import ( + "fmt" + + "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcd/chaincfg/chainhash" + "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 { + privKey *btcec.PrivateKey +} + +// NewNodeSigner creates a new instance of the NodeSigner backed by the target +// private key. +func NewNodeSigner(key *btcec.PrivateKey) *NodeSigner { + priv := &btcec.PrivateKey{} + priv.Curve = btcec.S256() + priv.PublicKey.X = key.X + priv.PublicKey.Y = key.Y + priv.D = key.D + return &NodeSigner{ + privKey: priv, + } +} + +// 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, + msg []byte) (*btcec.Signature, error) { + + // If this isn't our identity public key, then we'll exit early with an + // error as we can't sign with this key. + if !pubKey.IsEqual(n.privKey.PubKey()) { + return nil, fmt.Errorf("unknown public key") + } + + // Otherwise, we'll sign the dsha256 of the target message. + digest := chainhash.DoubleHashB(msg) + sign, err := n.privKey.Sign(digest) + if err != nil { + return nil, fmt.Errorf("can't sign the message: %v", err) + } + + return sign, nil +} + +// 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) { + + // Should the signature reference a compressed public key or not. + isCompressedKey := true + + // btcec.SignCompact returns a pubkey-recoverable signature + sig, err := btcec.SignCompact( + btcec.S256(), n.privKey, hash, isCompressedKey, + ) + 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) From 32041e703be8ce31bfe2af5f2949bc1944125856 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 8 Jan 2019 16:18:39 -0800 Subject: [PATCH 2/3] server: replace nodeSigner in main with netann.NodeSigner --- server.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server.go b/server.go index b4693f3d..c9ea87fc 100644 --- a/server.go +++ b/server.go @@ -37,6 +37,7 @@ import ( "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/nat" + "github.com/lightningnetwork/lnd/netann" "github.com/lightningnetwork/lnd/routing" "github.com/lightningnetwork/lnd/sweep" "github.com/lightningnetwork/lnd/ticker" @@ -87,7 +88,7 @@ type server struct { // nodeSigner is an implementation of the MessageSigner implementation // that's backed by the identity private key of the running lnd node. - nodeSigner *nodeSigner + nodeSigner *netann.NodeSigner // listenAddrs is the list of addresses the server is currently // listening on. @@ -269,7 +270,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, cc *chainControl, invoices: newInvoiceRegistry(chanDB), identityPriv: privKey, - nodeSigner: newNodeSigner(privKey), + nodeSigner: netann.NewNodeSigner(privKey), listenAddrs: listenAddrs, @@ -757,7 +758,6 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, cc *chainControl, maxRemoteDelay = maxLtcRemoteDelay } - nodeSigner := newNodeSigner(privKey) var chanIDSeed [32]byte if _, err := rand.Read(chanIDSeed[:]); err != nil { return nil, err @@ -772,7 +772,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, cc *chainControl, msg []byte) (*btcec.Signature, error) { if pubKey.IsEqual(privKey.PubKey()) { - return nodeSigner.SignMessage(pubKey, msg) + return s.nodeSigner.SignMessage(pubKey, msg) } return cc.msgSigner.SignMessage(pubKey, msg) From 7987a41c0ddddeeec2711f7e42fe6c428f86aa62 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 8 Jan 2019 16:18:55 -0800 Subject: [PATCH 3/3] nodesigner: remove file, now in netann pkg --- nodesigner.go | 82 --------------------------------------------------- 1 file changed, 82 deletions(-) delete mode 100644 nodesigner.go diff --git a/nodesigner.go b/nodesigner.go deleted file mode 100644 index b40bce48..00000000 --- a/nodesigner.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/btcsuite/btcd/btcec" - "github.com/btcsuite/btcd/chaincfg/chainhash" - "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 { - privKey *btcec.PrivateKey -} - -// newNodeSigner creates a new instance of the nodeSigner backed by the target -// private key. -func newNodeSigner(key *btcec.PrivateKey) *nodeSigner { - priv := &btcec.PrivateKey{} - priv.Curve = btcec.S256() - priv.PublicKey.X = key.X - priv.PublicKey.Y = key.Y - priv.D = key.D - return &nodeSigner{ - privKey: priv, - } -} - -// 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, - msg []byte) (*btcec.Signature, error) { - - // If this isn't our identity public key, then we'll exit early with an - // error as we can't sign with this key. - if !pubKey.IsEqual(n.privKey.PubKey()) { - return nil, fmt.Errorf("unknown public key") - } - - // Otherwise, we'll sign the dsha256 of the target message. - digest := chainhash.DoubleHashB(msg) - sign, err := n.privKey.Sign(digest) - if err != nil { - return nil, fmt.Errorf("can't sign the message: %v", err) - } - - return sign, nil -} - -// 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) { - - // Should the signature reference a compressed public key or not. - isCompressedKey := true - - // btcec.SignCompact returns a pubkey-recoverable signature - sig, err := btcec.SignCompact( - btcec.S256(), n.privKey, hash, isCompressedKey, - ) - 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)