mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-02 09:42:31 +02:00
multi: use key locator for lnwallet.MessageSigner
To simplify the message signing API even further, we refactor the lnwallet.MessageSigner interface to use a key locator instead of the public key to identify which key should be signed with.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
@@ -43,6 +44,10 @@ type ChanStatusConfig struct {
|
||||
// OurPubKey is the public key identifying this node on the network.
|
||||
OurPubKey *btcec.PublicKey
|
||||
|
||||
// OurKeyLoc is the locator for the public key identifying this node on
|
||||
// the network.
|
||||
OurKeyLoc keychain.KeyLocator
|
||||
|
||||
// MessageSigner signs messages that validate under OurPubKey.
|
||||
MessageSigner lnwallet.MessageSigner
|
||||
|
||||
@@ -621,7 +626,7 @@ func (m *ChanStatusManager) signAndSendNextUpdate(outpoint wire.OutPoint,
|
||||
}
|
||||
|
||||
err = SignChannelUpdate(
|
||||
m.cfg.MessageSigner, m.cfg.OurPubKey, chanUpdate,
|
||||
m.cfg.MessageSigner, m.cfg.OurKeyLoc, chanUpdate,
|
||||
ChanUpdSetDisable(disabled), ChanUpdSetTimestamp,
|
||||
)
|
||||
if err != nil {
|
||||
|
@@ -19,6 +19,10 @@ import (
|
||||
"github.com/lightningnetwork/lnd/netann"
|
||||
)
|
||||
|
||||
var (
|
||||
testKeyLoc = keychain.KeyLocator{Family: keychain.KeyFamilyNodeKey}
|
||||
)
|
||||
|
||||
// randOutpoint creates a random wire.Outpoint.
|
||||
func randOutpoint(t *testing.T) wire.OutPoint {
|
||||
t.Helper()
|
||||
@@ -310,7 +314,7 @@ func newManagerCfg(t *testing.T, numChannels int,
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate key pair: %v", err)
|
||||
}
|
||||
privKeySigner := &keychain.PrivKeyMessageSigner{PrivKey: privKey}
|
||||
privKeySigner := keychain.NewPrivKeyMessageSigner(privKey, testKeyLoc)
|
||||
|
||||
graph := newMockGraph(
|
||||
t, numChannels, startEnabled, startEnabled, privKey.PubKey(),
|
||||
@@ -322,6 +326,7 @@ func newManagerCfg(t *testing.T, numChannels int,
|
||||
ChanEnableTimeout: 500 * time.Millisecond,
|
||||
ChanDisableTimeout: time.Second,
|
||||
OurPubKey: privKey.PubKey(),
|
||||
OurKeyLoc: testKeyLoc,
|
||||
MessageSigner: netann.NewNodeSigner(privKeySigner),
|
||||
IsChannelActive: htlcSwitch.HasActiveLink,
|
||||
ApplyChannelUpdate: graph.ApplyChannelUpdate,
|
||||
|
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
@@ -55,7 +56,7 @@ func ChanUpdSetTimestamp(update *lnwire.ChannelUpdate) {
|
||||
// monotonically increase from the prior.
|
||||
//
|
||||
// NOTE: This method modifies the given update.
|
||||
func SignChannelUpdate(signer lnwallet.MessageSigner, pubKey *btcec.PublicKey,
|
||||
func SignChannelUpdate(signer lnwallet.MessageSigner, keyLoc keychain.KeyLocator,
|
||||
update *lnwire.ChannelUpdate, mods ...ChannelUpdateModifier) error {
|
||||
|
||||
// Apply the requested changes to the channel update.
|
||||
@@ -64,7 +65,7 @@ func SignChannelUpdate(signer lnwallet.MessageSigner, pubKey *btcec.PublicKey,
|
||||
}
|
||||
|
||||
// Create the DER-encoded ECDSA signature over the message digest.
|
||||
sig, err := SignAnnouncement(signer, pubKey, update)
|
||||
sig, err := SignAnnouncement(signer, keyLoc, update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/input"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
@@ -18,8 +17,8 @@ type mockSigner struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (m *mockSigner) SignMessage(pk *btcec.PublicKey,
|
||||
msg []byte) (input.Signature, error) {
|
||||
func (m *mockSigner) SignMessage(_ keychain.KeyLocator,
|
||||
_ []byte) (*btcec.Signature, error) {
|
||||
|
||||
if m.err != nil {
|
||||
return nil, m.err
|
||||
@@ -32,7 +31,7 @@ var _ lnwallet.MessageSigner = (*mockSigner)(nil)
|
||||
|
||||
var (
|
||||
privKey, _ = btcec.NewPrivateKey(btcec.S256())
|
||||
privKeySigner = &keychain.PrivKeyMessageSigner{PrivKey: privKey}
|
||||
privKeySigner = keychain.NewPrivKeyMessageSigner(privKey, testKeyLoc)
|
||||
|
||||
pubKey = privKey.PubKey()
|
||||
|
||||
@@ -130,7 +129,7 @@ func TestUpdateDisableFlag(t *testing.T) {
|
||||
// Attempt to update and sign the new update, specifying
|
||||
// disabled or enabled as prescribed in the test case.
|
||||
err := netann.SignChannelUpdate(
|
||||
tc.signer, pubKey, newUpdate,
|
||||
tc.signer, testKeyLoc, newUpdate,
|
||||
netann.ChanUpdSetDisable(tc.disable),
|
||||
netann.ChanUpdSetTimestamp,
|
||||
)
|
||||
|
@@ -4,7 +4,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
@@ -40,7 +40,7 @@ func NodeAnnSetTimestamp(nodeAnn *lnwire.NodeAnnouncement) {
|
||||
// update should be the most recent, valid update, otherwise the timestamp may
|
||||
// not monotonically increase from the prior.
|
||||
func SignNodeAnnouncement(signer lnwallet.MessageSigner,
|
||||
pubKey *btcec.PublicKey, nodeAnn *lnwire.NodeAnnouncement,
|
||||
keyLoc keychain.KeyLocator, nodeAnn *lnwire.NodeAnnouncement,
|
||||
mods ...NodeAnnModifier) error {
|
||||
|
||||
// Apply the requested changes to the node announcement.
|
||||
@@ -49,7 +49,7 @@ func SignNodeAnnouncement(signer lnwallet.MessageSigner,
|
||||
}
|
||||
|
||||
// Create the DER-encoded ECDSA signature over the message digest.
|
||||
sig, err := SignAnnouncement(signer, pubKey, nodeAnn)
|
||||
sig, err := SignAnnouncement(signer, keyLoc, nodeAnn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/input"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
)
|
||||
@@ -24,15 +23,15 @@ func NewNodeSigner(keySigner keychain.SingleKeyMessageSigner) *NodeSigner {
|
||||
}
|
||||
|
||||
// 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) (input.Signature, error) {
|
||||
// resident node's private key described in the key locator. If the target key
|
||||
// locator is _not_ the node's private key, then an error will be returned.
|
||||
func (n *NodeSigner) SignMessage(keyLoc keychain.KeyLocator,
|
||||
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.keySigner.PubKey()) {
|
||||
return nil, fmt.Errorf("unknown public key")
|
||||
if keyLoc != n.keySigner.KeyLocator() {
|
||||
return nil, fmt.Errorf("unknown public key locator")
|
||||
}
|
||||
|
||||
// Otherwise, we'll sign the double-sha256 of the target message.
|
||||
|
@@ -3,15 +3,15 @@ package netann
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/input"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
|
||||
// SignAnnouncement signs any type of gossip message that is announced on the
|
||||
// network.
|
||||
func SignAnnouncement(signer lnwallet.MessageSigner, pubKey *btcec.PublicKey,
|
||||
func SignAnnouncement(signer lnwallet.MessageSigner, keyLoc keychain.KeyLocator,
|
||||
msg lnwire.Message) (input.Signature, error) {
|
||||
|
||||
var (
|
||||
@@ -33,5 +33,5 @@ func SignAnnouncement(signer lnwallet.MessageSigner, pubKey *btcec.PublicKey,
|
||||
return nil, fmt.Errorf("unable to get data to sign: %v", err)
|
||||
}
|
||||
|
||||
return signer.SignMessage(pubKey, data)
|
||||
return signer.SignMessage(keyLoc, data)
|
||||
}
|
||||
|
Reference in New Issue
Block a user