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

lightningnetwork / lnd / 12115442155

02 Dec 2024 08:28AM UTC coverage: 48.662% (-10.3%) from 58.948%
12115442155

Pull #9175

github

ellemouton
netann: update ChanAnn2 validation to work for P2WSH channels

This commit expands the ChannelAnnouncement2 validation for the case
where it is announcing a P2WSH channel.
Pull Request #9175: lnwire+netann: update structure of g175 messages to be pure TLV

6 of 314 new or added lines in 9 files covered. (1.91%)

27532 existing lines in 434 files now uncovered.

97890 of 201164 relevant lines covered (48.66%)

0.52 hits per line

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

0.0
/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.
NEW
36
func EncodePureTLVMessage(msg PureTLVMessage, buf *bytes.Buffer) error {
×
NEW
37
        return EncodeRecordsTo(buf, msg.AllRecords())
×
NEW
38
}
×
39

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

NEW
50
                signedRecords = append(signedRecords, record)
×
51
        }
52

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

NEW
58
        return buf.Bytes(), nil
×
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.
NEW
63
func InUnsignedRange(t tlv.Type) bool {
×
NEW
64
        return (t >= pureTLVUnsignedRangeOneStart &&
×
NEW
65
                t < pureTLVSignedSecondRangeStart) ||
×
NEW
66
                t >= pureTLVUnsignedRangeTwoStart
×
NEW
67
}
×
68

69
// ExtraSignedFieldsFromTypeMap is a helper that can be used alongside calls to
70
// the tlv.Stream DecodeWithParsedTypesP2P or DecodeWithParsedTypes methods to
71
// extract the tlv type and value pairs in the defined PureTLVMessage signed
72
// range which we have not handled with any of our defined Records. These
73
// methods will return a tlv.TypeMap containing the records that were extracted
74
// from an io.Reader. If the record was know and handled by a defined record,
75
// then the value accompanying the record's type in the map will be nil.
76
// Otherwise, if the record was unhandled, it will be non-nil.
NEW
77
func ExtraSignedFieldsFromTypeMap(m tlv.TypeMap) map[uint64][]byte {
×
NEW
78
        extraFields := make(map[uint64][]byte)
×
NEW
79
        for t, v := range m {
×
NEW
80
                // If the value in the type map is nil, then it indicates that
×
NEW
81
                // we know this type, and it was handled by one of the records
×
NEW
82
                // we passed to the decode function vai the TLV stream.
×
NEW
83
                if v == nil {
×
NEW
84
                        continue
×
85
                }
86

87
                // No need to keep this field if it is unknown to us and is not
88
                // in the sign range.
NEW
89
                if InUnsignedRange(t) {
×
NEW
90
                        continue
×
91
                }
92

93
                // Otherwise, this is an un-handled type, so we keep track of
94
                // it for signature validation and re-encoding later on.
NEW
95
                extraFields[uint64(t)] = v
×
96
        }
97

NEW
98
        return extraFields
×
99
}
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