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

lightningnetwork / lnd / 13086553769

01 Feb 2025 07:39AM UTC coverage: 58.822% (+0.01%) from 58.808%
13086553769

Pull #9175

github

ellemouton
lnwire: add NodeAnnouncement2
Pull Request #9175: lnwire+netann: update structure of g175 messages to be pure TLV

414 of 581 new or added lines in 13 files covered. (71.26%)

87 existing lines in 23 files now uncovered.

136444 of 231959 relevant lines covered (58.82%)

19261.9 hits per line

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

93.75
/lnwire/pure_tlv.go
1
package lnwire
2

3
import (
4
        "bytes"
5

6
        "github.com/lightningnetwork/lnd/tlv"
7
)
8

9
const (
10
        // pureTLVUnsignedRangeOneStart defines the start of the first unsigned
11
        // TLV range used for pure TLV messages. The range is inclusive of this
12
        // number.
13
        pureTLVUnsignedRangeOneStart = 160
14

15
        // pureTLVSignedSecondRangeStart defines the start of the second signed
16
        // TLV range used for pure TLV messages. The range is inclusive of this
17
        // number. Note that the first range is the inclusive range of 0-159.
18
        pureTLVSignedSecondRangeStart = 1000000000
19

20
        // pureTLVUnsignedRangeTwoStart defines the start of the second unsigned
21
        // TLV range used for pure TLV message.
22
        pureTLVUnsignedRangeTwoStart = 3000000000
23
)
24

25
// PureTLVMessage describes an LN message that is a pure TLV stream. If the
26
// message includes a signature, it will sign all the TLV records in the
27
// inclusive ranges: 0 to 159 and 1000000000 to 2999999999.
28
type PureTLVMessage interface {
29
        // AllRecords returns all the TLV records for the message. This will
30
        // include all the records we know about along with any that we don't
31
        // know about but that fall in the signed TLV range.
32
        AllRecords() []tlv.Record
33
}
34

35
// EncodePureTLVMessage encodes the given PureTLVMessage to the given buffer.
36
func EncodePureTLVMessage(msg PureTLVMessage, buf *bytes.Buffer) error {
407✔
37
        return EncodeRecordsTo(buf, msg.AllRecords())
407✔
38
}
407✔
39

40
// SerialiseFieldsToSign serialises all the records from the given
41
// PureTLVMessage that fall within the signed TLV range.
42
func SerialiseFieldsToSign(msg PureTLVMessage) ([]byte, error) {
8✔
43
        // Filter out all the fields not in the signed ranges.
8✔
44
        var signedRecords []tlv.Record
8✔
45
        for _, record := range msg.AllRecords() {
66✔
46
                if InUnsignedRange(record.Type()) {
68✔
47
                        continue
10✔
48
                }
49

50
                signedRecords = append(signedRecords, record)
48✔
51
        }
52

53
        var buf bytes.Buffer
8✔
54
        if err := EncodeRecordsTo(&buf, signedRecords); err != nil {
8✔
NEW
55
                return nil, err
×
NEW
56
        }
×
57

58
        return buf.Bytes(), nil
8✔
59
}
60

61
// InUnsignedRange returns true if the given TLV type falls outside the TLV
62
// ranges that the signature of a pure TLV message will cover.
63
func InUnsignedRange(t tlv.Type) bool {
2,003✔
64
        return (t >= pureTLVUnsignedRangeOneStart &&
2,003✔
65
                t < pureTLVSignedSecondRangeStart) ||
2,003✔
66
                t >= pureTLVUnsignedRangeTwoStart
2,003✔
67
}
2,003✔
68

69
// ExtraSignedFields is a type that stores a map from TLV types in the signed
70
// range (for PureMessages) to their corresponding serialised values. This type
71
// can be used to keep around data that we don't yet understand but that we need
72
// for re-composing the wire message since the signature covers these fields.
73
type ExtraSignedFields map[uint64][]byte
74

75
// ExtraSignedFieldsFromTypeMap is a helper that can be used alongside calls to
76
// the tlv.Stream DecodeWithParsedTypesP2P or DecodeWithParsedTypes methods to
77
// extract the tlv type and value pairs in the defined PureTLVMessage signed
78
// range which we have not handled with any of our defined Records. These
79
// methods will return a tlv.TypeMap containing the records that were extracted
80
// from an io.Reader. If the record was know and handled by a defined record,
81
// then the value accompanying the record's type in the map will be nil.
82
// Otherwise, if the record was unhandled, it will be non-nil.
83
func ExtraSignedFieldsFromTypeMap(m tlv.TypeMap) ExtraSignedFields {
410✔
84
        extraFields := make(ExtraSignedFields)
410✔
85
        for t, v := range m {
4,846✔
86
                // If the value in the type map is nil, then it indicates that
4,436✔
87
                // we know this type, and it was handled by one of the records
4,436✔
88
                // we passed to the decode function vai the TLV stream.
4,436✔
89
                if v == nil {
6,927✔
90
                        continue
2,491✔
91
                }
92

93
                // No need to keep this field if it is unknown to us and is not
94
                // in the sign range.
95
                if InUnsignedRange(t) {
1,947✔
96
                        continue
2✔
97
                }
98

99
                // Otherwise, this is an un-handled type, so we keep track of
100
                // it for signature validation and re-encoding later on.
101
                extraFields[uint64(t)] = v
1,943✔
102
        }
103

104
        return extraFields
410✔
105
}
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