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

lightningnetwork / lnd / 16990665124

15 Aug 2025 01:10PM UTC coverage: 66.74% (-0.03%) from 66.765%
16990665124

Pull #9455

github

web-flow
Merge 035fac41d into fb1adfc21
Pull Request #9455: [1/2] discovery+lnwire: add support for DNS host name in NodeAnnouncement msg

116 of 188 new or added lines in 8 files covered. (61.7%)

110 existing lines in 23 files now uncovered.

136011 of 203791 relevant lines covered (66.74%)

21482.89 hits per line

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

69.88
/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.NodeAnnouncement.
20
type NodeAnnModifier func(*lnwire.NodeAnnouncement)
21

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

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

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

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

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

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

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

80
        // Parse the DER-encoded signature into a fixed-size 64-byte array.
81
        nodeAnn.Signature, err = lnwire.NewSigFromSignature(sig)
3✔
82
        return err
3✔
83
}
84

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

92
        return ValidateNodeAnnSignature(a)
20✔
93
}
94

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

NEW
109
                hasDNSAddr = true
×
NEW
110

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

117
        return nil
34✔
118
}
119

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

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

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

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

154
        return nil
19✔
155
}
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