Skip to content

Commit 1974903

Browse files
committed
multi: move node ann validation code to netann pkg
The `netann` package is a more appropriate place for this code to live. Also, once the funding transaction code is moved out of the `graph.Builder`, then no `lnwire` validation will occur in the `graph` package.
1 parent 457a245 commit 1974903

File tree

3 files changed

+42
-48
lines changed

3 files changed

+42
-48
lines changed

discovery/gossiper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ func (d *AuthenticatedGossiper) fetchPKScript(chanID *lnwire.ShortChannelID) (
19771977
func (d *AuthenticatedGossiper) addNode(msg *lnwire.NodeAnnouncement,
19781978
op ...batch.SchedulerOption) error {
19791979

1980-
if err := graph.ValidateNodeAnn(msg); err != nil {
1980+
if err := netann.ValidateNodeAnn(msg); err != nil {
19811981
return fmt.Errorf("unable to validate node announcement: %w",
19821982
err)
19831983
}

graph/ann_validation.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

netann/node_announcement.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package netann
22

33
import (
4+
"bytes"
45
"image/color"
56
"net"
67
"time"
78

9+
"github.com/btcsuite/btcd/btcec/v2"
10+
"github.com/btcsuite/btcd/chaincfg/chainhash"
11+
"github.com/go-errors/errors"
812
"github.com/lightningnetwork/lnd/keychain"
913
"github.com/lightningnetwork/lnd/lnwallet"
1014
"github.com/lightningnetwork/lnd/lnwire"
@@ -76,3 +80,40 @@ func SignNodeAnnouncement(signer lnwallet.MessageSigner,
7680
nodeAnn.Signature, err = lnwire.NewSigFromSignature(sig)
7781
return err
7882
}
83+
84+
// ValidateNodeAnn validates the node announcement by ensuring that the
85+
// attached signature is needed a signature of the node announcement under the
86+
// specified node public key.
87+
func ValidateNodeAnn(a *lnwire.NodeAnnouncement) error {
88+
// Reconstruct the data of announcement which should be covered by the
89+
// signature so we can verify the signature shortly below
90+
data, err := a.DataToSign()
91+
if err != nil {
92+
return err
93+
}
94+
95+
nodeSig, err := a.Signature.ToSignature()
96+
if err != nil {
97+
return err
98+
}
99+
nodeKey, err := btcec.ParsePubKey(a.NodeID[:])
100+
if err != nil {
101+
return err
102+
}
103+
104+
// Finally ensure that the passed signature is valid, if not we'll
105+
// return an error so this node announcement can be rejected.
106+
dataHash := chainhash.DoubleHashB(data)
107+
if !nodeSig.Verify(dataHash, nodeKey) {
108+
var msgBuf bytes.Buffer
109+
if _, err := lnwire.WriteMessage(&msgBuf, a, 0); err != nil {
110+
return err
111+
}
112+
113+
return errors.Errorf("signature on NodeAnnouncement(%x) is "+
114+
"invalid: %x", nodeKey.SerializeCompressed(),
115+
msgBuf.Bytes())
116+
}
117+
118+
return nil
119+
}

0 commit comments

Comments
 (0)