|
1 | 1 | package netann
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "image/color"
|
5 | 6 | "net"
|
6 | 7 | "time"
|
7 | 8 |
|
| 9 | + "github.com/btcsuite/btcd/btcec/v2" |
| 10 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 11 | + "github.com/go-errors/errors" |
8 | 12 | "github.com/lightningnetwork/lnd/keychain"
|
9 | 13 | "github.com/lightningnetwork/lnd/lnwallet"
|
10 | 14 | "github.com/lightningnetwork/lnd/lnwire"
|
@@ -76,3 +80,40 @@ func SignNodeAnnouncement(signer lnwallet.MessageSigner,
|
76 | 80 | nodeAnn.Signature, err = lnwire.NewSigFromSignature(sig)
|
77 | 81 | return err
|
78 | 82 | }
|
| 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