• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 14358372723

09 Apr 2025 01:26PM UTC coverage: 56.696% (-12.3%) from 69.037%
14358372723

Pull #9696

github

web-flow
Merge e2837e400 into 867d27d68
Pull Request #9696: Add `development_guidelines.md` for both human and machine

107055 of 188823 relevant lines covered (56.7%)

22721.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

23.73
/netann/node_announcement.go
1
package netann
2

3
import (
4
        "bytes"
5
        "image/color"
6
        "net"
7
        "time"
8

9
        "github.com/btcsuite/btcd/btcec/v2"
10
        "github.com/btcsuite/btcd/chaincfg/chainhash"
11
        "github.com/go-errors/errors"
12
        "github.com/lightningnetwork/lnd/keychain"
13
        "github.com/lightningnetwork/lnd/lnwallet"
14
        "github.com/lightningnetwork/lnd/lnwire"
15
)
16

17
// NodeAnnModifier is a closure that makes in-place modifications to an
18
// lnwire.NodeAnnouncement.
19
type NodeAnnModifier func(*lnwire.NodeAnnouncement)
20

21
// NodeAnnSetAlias is a functional option that sets the alias of the
22
// given node announcement.
23
func NodeAnnSetAlias(alias lnwire.NodeAlias) func(*lnwire.NodeAnnouncement) {
×
24
        return func(nodeAnn *lnwire.NodeAnnouncement) {
×
25
                nodeAnn.Alias = alias
×
26
        }
×
27
}
28

29
// NodeAnnSetAddrs is a functional option that allows updating the addresses of
30
// the given node announcement.
31
func NodeAnnSetAddrs(addrs []net.Addr) func(*lnwire.NodeAnnouncement) {
×
32
        return func(nodeAnn *lnwire.NodeAnnouncement) {
×
33
                nodeAnn.Addresses = addrs
×
34
        }
×
35
}
36

37
// NodeAnnSetColor is a functional option that sets the color of the
38
// given node announcement.
39
func NodeAnnSetColor(newColor color.RGBA) func(*lnwire.NodeAnnouncement) {
×
40
        return func(nodeAnn *lnwire.NodeAnnouncement) {
×
41
                nodeAnn.RGBColor = newColor
×
42
        }
×
43
}
44

45
// NodeAnnSetFeatures is a functional option that allows updating the features of
46
// the given node announcement.
47
func NodeAnnSetFeatures(features *lnwire.RawFeatureVector) func(*lnwire.NodeAnnouncement) {
×
48
        return func(nodeAnn *lnwire.NodeAnnouncement) {
×
49
                nodeAnn.Features = features
×
50
        }
×
51
}
52

53
// NodeAnnSetTimestamp is a functional option that sets the timestamp of the
54
// announcement to the current time, or increments it if the timestamp is
55
// already in the future.
56
func NodeAnnSetTimestamp(nodeAnn *lnwire.NodeAnnouncement) {
×
57
        newTimestamp := uint32(time.Now().Unix())
×
58
        if newTimestamp <= nodeAnn.Timestamp {
×
59
                // Increment the prior value to  ensure the timestamp
×
60
                // monotonically increases, otherwise the announcement won't
×
61
                // propagate.
×
62
                newTimestamp = nodeAnn.Timestamp + 1
×
63
        }
×
64
        nodeAnn.Timestamp = newTimestamp
×
65
}
66

67
// SignNodeAnnouncement signs the lnwire.NodeAnnouncement provided, which
68
// should be the most recent, valid update, otherwise the timestamp may not
69
// monotonically increase from the prior.
70
func SignNodeAnnouncement(signer lnwallet.MessageSigner,
71
        keyLoc keychain.KeyLocator, nodeAnn *lnwire.NodeAnnouncement) error {
×
72

×
73
        // Create the DER-encoded ECDSA signature over the message digest.
×
74
        sig, err := SignAnnouncement(signer, keyLoc, nodeAnn)
×
75
        if err != nil {
×
76
                return err
×
77
        }
×
78

79
        // Parse the DER-encoded signature into a fixed-size 64-byte array.
80
        nodeAnn.Signature, err = lnwire.NewSigFromSignature(sig)
×
81
        return err
×
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 {
17✔
88
        // Reconstruct the data of announcement which should be covered by the
17✔
89
        // signature so we can verify the signature shortly below
17✔
90
        data, err := a.DataToSign()
17✔
91
        if err != nil {
18✔
92
                return err
1✔
93
        }
1✔
94

95
        nodeSig, err := a.Signature.ToSignature()
16✔
96
        if err != nil {
16✔
97
                return err
×
98
        }
×
99
        nodeKey, err := btcec.ParsePubKey(a.NodeID[:])
16✔
100
        if err != nil {
16✔
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)
16✔
107
        if !nodeSig.Verify(dataHash, nodeKey) {
16✔
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
16✔
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc