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

lightningnetwork / lnd / 18852986778

27 Oct 2025 07:10PM UTC coverage: 54.859% (-11.8%) from 66.648%
18852986778

Pull #10265

github

web-flow
Merge 45787b3d5 into 9a7b526c0
Pull Request #10265: multi: update close logic to handle re-orgs of depth n-1, where n is num confs - add min conf floor

529 of 828 new or added lines in 17 files covered. (63.89%)

24026 existing lines in 286 files now uncovered.

110927 of 202205 relevant lines covered (54.86%)

21658.16 hits per line

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

30.95
/netann/node_announcement.go
1
package netann
2

3
import (
4
        "bytes"
5
        "errors"
6
        "fmt"
7
        "image/color"
8
        "net"
9
        "time"
10

11
        "github.com/btcsuite/btcd/btcec/v2"
12
        "github.com/btcsuite/btcd/chaincfg/chainhash"
13
        "github.com/lightningnetwork/lnd/keychain"
14
        "github.com/lightningnetwork/lnd/lnwallet"
15
        "github.com/lightningnetwork/lnd/lnwire"
16
)
17

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

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

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

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

46
// NodeAnnSetFeatures is a functional option that allows updating the features of
47
// the given node announcement.
48
func NodeAnnSetFeatures(
UNCOV
49
        features *lnwire.RawFeatureVector) func(*lnwire.NodeAnnouncement1) {
×
UNCOV
50

×
UNCOV
51
        return func(nodeAnn *lnwire.NodeAnnouncement1) {
×
UNCOV
52
                nodeAnn.Features = features
×
UNCOV
53
        }
×
54
}
55

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

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

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

82
        // Parse the DER-encoded signature into a fixed-size 64-byte array.
UNCOV
83
        nodeAnn.Signature, err = lnwire.NewSigFromSignature(sig)
×
UNCOV
84
        return err
×
85
}
86

87
// ValidateNodeAnn validates the fields and signature of a node announcement.
88
func ValidateNodeAnn(a *lnwire.NodeAnnouncement1) error {
17✔
89
        err := ValidateNodeAnnFields(a)
17✔
90
        if err != nil {
17✔
91
                return fmt.Errorf("invalid node announcement fields: %w", err)
×
92
        }
×
93

94
        return ValidateNodeAnnSignature(a)
17✔
95
}
96

97
// ValidateNodeAnnFields validates the fields of a node announcement.
98
func ValidateNodeAnnFields(a *lnwire.NodeAnnouncement1) error {
31✔
99
        // Check that it only has at most one DNS address.
31✔
100
        hasDNSAddr := false
31✔
101
        for _, addr := range a.Addresses {
61✔
102
                dnsAddr, ok := addr.(*lnwire.DNSAddress)
30✔
103
                if !ok {
60✔
104
                        continue
30✔
105
                }
106
                if hasDNSAddr {
×
107
                        return errors.New("node announcement contains " +
×
108
                                "multiple DNS addresses. Only one is allowed")
×
109
                }
×
110

111
                hasDNSAddr = true
×
112

×
113
                err := lnwire.ValidateDNSAddr(dnsAddr.Hostname, dnsAddr.Port)
×
114
                if err != nil {
×
115
                        return err
×
116
                }
×
117
        }
118

119
        return nil
31✔
120
}
121

122
// ValidateNodeAnnSignature validates the node announcement by ensuring that the
123
// attached signature is needed a signature of the node announcement under the
124
// specified node public key.
125
func ValidateNodeAnnSignature(a *lnwire.NodeAnnouncement1) error {
17✔
126
        // Reconstruct the data of announcement which should be covered by the
17✔
127
        // signature so we can verify the signature shortly below
17✔
128
        data, err := a.DataToSign()
17✔
129
        if err != nil {
18✔
130
                return err
1✔
131
        }
1✔
132

133
        nodeSig, err := a.Signature.ToSignature()
16✔
134
        if err != nil {
16✔
135
                return err
×
136
        }
×
137
        nodeKey, err := btcec.ParsePubKey(a.NodeID[:])
16✔
138
        if err != nil {
16✔
139
                return err
×
140
        }
×
141

142
        // Finally ensure that the passed signature is valid, if not we'll
143
        // return an error so this node announcement can be rejected.
144
        dataHash := chainhash.DoubleHashB(data)
16✔
145
        if !nodeSig.Verify(dataHash, nodeKey) {
16✔
146
                var msgBuf bytes.Buffer
×
147
                if _, err := lnwire.WriteMessage(&msgBuf, a, 0); err != nil {
×
148
                        return err
×
149
                }
×
150

151
                return fmt.Errorf("signature on NodeAnnouncement1(%x) is "+
×
152
                        "invalid: %x", nodeKey.SerializeCompressed(),
×
153
                        msgBuf.Bytes())
×
154
        }
155

156
        return nil
16✔
157
}
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