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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

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

77.97
/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) {
3✔
24
        return func(nodeAnn *lnwire.NodeAnnouncement) {
6✔
25
                nodeAnn.Alias = alias
3✔
26
        }
3✔
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) {
3✔
32
        return func(nodeAnn *lnwire.NodeAnnouncement) {
6✔
33
                nodeAnn.Addresses = addrs
3✔
34
        }
3✔
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) {
3✔
40
        return func(nodeAnn *lnwire.NodeAnnouncement) {
6✔
41
                nodeAnn.RGBColor = newColor
3✔
42
        }
3✔
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) {
3✔
48
        return func(nodeAnn *lnwire.NodeAnnouncement) {
6✔
49
                nodeAnn.Features = features
3✔
50
        }
3✔
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) {
3✔
57
        newTimestamp := uint32(time.Now().Unix())
3✔
58
        if newTimestamp <= nodeAnn.Timestamp {
6✔
59
                // Increment the prior value to  ensure the timestamp
3✔
60
                // monotonically increases, otherwise the announcement won't
3✔
61
                // propagate.
3✔
62
                newTimestamp = nodeAnn.Timestamp + 1
3✔
63
        }
3✔
64
        nodeAnn.Timestamp = newTimestamp
3✔
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 {
3✔
72

3✔
73
        // Create the DER-encoded ECDSA signature over the message digest.
3✔
74
        sig, err := SignAnnouncement(signer, keyLoc, nodeAnn)
3✔
75
        if err != nil {
3✔
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)
3✔
81
        return err
3✔
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 {
20✔
88
        // Reconstruct the data of announcement which should be covered by the
20✔
89
        // signature so we can verify the signature shortly below
20✔
90
        data, err := a.DataToSign()
20✔
91
        if err != nil {
21✔
92
                return err
1✔
93
        }
1✔
94

95
        nodeSig, err := a.Signature.ToSignature()
19✔
96
        if err != nil {
19✔
97
                return err
×
98
        }
×
99
        nodeKey, err := btcec.ParsePubKey(a.NodeID[:])
19✔
100
        if err != nil {
19✔
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)
19✔
107
        if !nodeSig.Verify(dataHash, nodeKey) {
19✔
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
19✔
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