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

lightningnetwork / lnd / 11364879019

16 Oct 2024 11:39AM UTC coverage: 49.778% (+0.5%) from 49.297%
11364879019

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%)

99 existing lines in 18 files now uncovered.

98794 of 198470 relevant lines covered (49.78%)

2.07 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  = 160
11
        pureTLVSignedSecondRangeStart = 1000000000
12
        pureTLVUnsignedRangeTwoStart  = 3000000000
13
)
14

15
// PureTLVMessage describes an LN message that is a pure TLV stream. If the
16
// message includes a signature, it will sign all the TLV records in the
17
// inclusive ranges: 0 to 159 and 1000000000 to 2999999999.
18
type PureTLVMessage interface {
19
        // AllRecords returns all the TLV records for the message. This will
20
        // include all the records we know about along with any that we don't
21
        // know about but that fall in the signed TLV range.
22
        AllRecords() []tlv.Record
23
}
24

25
// EncodePureTLVMessage encodes the given PureTLVMessage to the given buffer.
NEW
26
func EncodePureTLVMessage(msg PureTLVMessage, buf *bytes.Buffer) error {
×
NEW
27
        return EncodeRecordsTo(buf, msg.AllRecords())
×
NEW
28
}
×
29

30
// SerialiseFieldsToSign serialises all the records from the given
31
// PureTLVMessage that fall within the signed TLV range.
NEW
32
func SerialiseFieldsToSign(msg PureTLVMessage) ([]byte, error) {
×
NEW
33
        // Filter out all the fields not in the signed ranges.
×
NEW
34
        var signedRecords []tlv.Record
×
NEW
35
        for _, record := range msg.AllRecords() {
×
NEW
36
                if InUnsignedRange(record.Type()) {
×
NEW
37
                        continue
×
38
                }
39

NEW
40
                signedRecords = append(signedRecords, record)
×
41
        }
42

NEW
43
        var buf bytes.Buffer
×
NEW
44
        if err := EncodeRecordsTo(&buf, signedRecords); err != nil {
×
NEW
45
                return nil, err
×
NEW
46
        }
×
47

NEW
48
        return buf.Bytes(), nil
×
49
}
50

51
// InUnsignedRange returns true if the given TLV type falls outside the TLV
52
// ranges that the signature of a pure TLV message will cover.
NEW
53
func InUnsignedRange(t tlv.Type) bool {
×
NEW
54
        return (t >= pureTLVUnsignedRangeOneStart &&
×
NEW
55
                t < pureTLVSignedSecondRangeStart) ||
×
NEW
56
                t >= pureTLVUnsignedRangeTwoStart
×
NEW
57
}
×
58

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

77
                // No need to keep this field if it is unknown to us and is not
78
                // in the sign range.
NEW
79
                if InUnsignedRange(t) {
×
NEW
80
                        continue
×
81
                }
82

83
                // Otherwise, this is an un-handled type, so we keep track of
84
                // it for signature validation and re-encoding later on.
NEW
85
                extraFields[uint64(t)] = v
×
86
        }
87

NEW
88
        return extraFields
×
89
}
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