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

lightningnetwork / lnd / 16263831630

14 Jul 2025 10:00AM UTC coverage: 67.315% (-0.02%) from 67.339%
16263831630

Pull #10072

github

web-flow
Merge 480167d37 into 5bb227774
Pull Request #10072: [1/2] lnwire: fix encoding customized TLV records

265 of 275 new or added lines in 8 files covered. (96.36%)

101 existing lines in 19 files now uncovered.

135293 of 200984 relevant lines covered (67.32%)

21801.32 hits per line

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

75.96
/lnwire/channel_announcement_2.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "io"
6

7
        "github.com/btcsuite/btcd/chaincfg"
8
        "github.com/btcsuite/btcd/chaincfg/chainhash"
9
        "github.com/lightningnetwork/lnd/tlv"
10
)
11

12
// ChannelAnnouncement2 message is used to announce the existence of a taproot
13
// channel between two peers in the network.
14
type ChannelAnnouncement2 struct {
15
        // Signature is a Schnorr signature over the TLV stream of the message.
16
        Signature Sig
17

18
        // ChainHash denotes the target chain that this channel was opened
19
        // within. This value should be the genesis hash of the target chain.
20
        ChainHash tlv.RecordT[tlv.TlvType0, chainhash.Hash]
21

22
        // Features is the feature vector that encodes the features supported
23
        // by the target node. This field can be used to signal the type of the
24
        // channel, or modifications to the fields that would normally follow
25
        // this vector.
26
        Features tlv.RecordT[tlv.TlvType2, RawFeatureVector]
27

28
        // ShortChannelID is the unique description of the funding transaction,
29
        // or where exactly it's located within the target blockchain.
30
        ShortChannelID tlv.RecordT[tlv.TlvType4, ShortChannelID]
31

32
        // Capacity is the number of satoshis of the capacity of this channel.
33
        // It must be less than or equal to the value of the on-chain funding
34
        // output.
35
        Capacity tlv.RecordT[tlv.TlvType6, uint64]
36

37
        // NodeID1 is the numerically-lesser public key ID of one of the channel
38
        // operators.
39
        NodeID1 tlv.RecordT[tlv.TlvType8, [33]byte]
40

41
        // NodeID2 is the numerically-greater public key ID of one of the
42
        // channel operators.
43
        NodeID2 tlv.RecordT[tlv.TlvType10, [33]byte]
44

45
        // BitcoinKey1 is the public key of the key used by Node1 in the
46
        // construction of the on-chain funding transaction. This is an optional
47
        // field and only needs to be set if the 4-of-4 MuSig construction was
48
        // used in the creation of the message signature.
49
        BitcoinKey1 tlv.OptionalRecordT[tlv.TlvType12, [33]byte]
50

51
        // BitcoinKey2 is the public key of the key used by Node2 in the
52
        // construction of the on-chain funding transaction. This is an optional
53
        // field and only needs to be set if the 4-of-4 MuSig construction was
54
        // used in the creation of the message signature.
55
        BitcoinKey2 tlv.OptionalRecordT[tlv.TlvType14, [33]byte]
56

57
        // MerkleRootHash is the hash used to create the optional tweak in the
58
        // funding output. If this is not set but the bitcoin keys are, then
59
        // the funding output is a pure 2-of-2 MuSig aggregate public key.
60
        MerkleRootHash tlv.OptionalRecordT[tlv.TlvType16, [32]byte]
61

62
        // ExtraOpaqueData is the set of data that was appended to this
63
        // message, some of which we may not actually know how to iterate or
64
        // parse. By holding onto this data, we ensure that we're able to
65
        // properly validate the set of signatures that cover these new fields,
66
        // and ensure we're able to make upgrades to the network in a forwards
67
        // compatible manner.
68
        ExtraOpaqueData ExtraOpaqueData
69
}
70

71
// Decode deserializes a serialized AnnounceSignatures1 stored in the passed
72
// io.Reader observing the specified protocol version.
73
//
74
// This is part of the lnwire.Message interface.
75
func (c *ChannelAnnouncement2) Decode(r io.Reader, _ uint32) error {
232✔
76
        err := ReadElement(r, &c.Signature)
232✔
77
        if err != nil {
234✔
78
                return err
2✔
79
        }
2✔
80
        c.Signature.ForceSchnorr()
230✔
81

230✔
82
        return c.DecodeTLVRecords(r)
230✔
83
}
84

85
// DecodeTLVRecords decodes only the TLV section of the message.
86
func (c *ChannelAnnouncement2) DecodeTLVRecords(r io.Reader) error {
230✔
87
        // First extract into extra opaque data.
230✔
88
        var tlvRecords ExtraOpaqueData
230✔
89
        if err := ReadElement(r, &tlvRecords); err != nil {
230✔
90
                return err
×
91
        }
×
92

93
        var (
230✔
94
                chainHash      = tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
230✔
95
                btcKey1        = tlv.ZeroRecordT[tlv.TlvType12, [33]byte]()
230✔
96
                btcKey2        = tlv.ZeroRecordT[tlv.TlvType14, [33]byte]()
230✔
97
                merkleRootHash = tlv.ZeroRecordT[tlv.TlvType16, [32]byte]()
230✔
98
        )
230✔
99

230✔
100
        knownRecords, extraData, err := ParseAndExtractExtraData(
230✔
101
                tlvRecords, &chainHash, &c.Features,
230✔
102
                &c.ShortChannelID, &c.Capacity, &c.NodeID1, &c.NodeID2,
230✔
103
                &btcKey1, &btcKey2, &merkleRootHash,
230✔
104
        )
230✔
105
        if err != nil {
281✔
106
                return err
51✔
107
        }
51✔
108

109
        // By default, the chain-hash is the bitcoin mainnet genesis block hash.
110
        c.ChainHash.Val = *chaincfg.MainNetParams.GenesisHash
179✔
111
        if _, ok := knownRecords[c.ChainHash.TlvType()]; ok {
286✔
112
                c.ChainHash.Val = chainHash.Val
107✔
113
        }
107✔
114

115
        if _, ok := knownRecords[c.BitcoinKey1.TlvType()]; ok {
230✔
116
                c.BitcoinKey1 = tlv.SomeRecordT(btcKey1)
51✔
117
        }
51✔
118

119
        if _, ok := knownRecords[c.BitcoinKey2.TlvType()]; ok {
227✔
120
                c.BitcoinKey2 = tlv.SomeRecordT(btcKey2)
48✔
121
        }
48✔
122

123
        if _, ok := knownRecords[c.MerkleRootHash.TlvType()]; ok {
239✔
124
                c.MerkleRootHash = tlv.SomeRecordT(merkleRootHash)
60✔
125
        }
60✔
126

127
        c.ExtraOpaqueData = extraData
179✔
128

179✔
129
        return c.ExtraOpaqueData.ValidateTLV()
179✔
130
}
131

132
// Encode serializes the target AnnounceSignatures1 into the passed io.Writer
133
// observing the protocol version specified.
134
//
135
// This is part of the lnwire.Message interface.
136
func (c *ChannelAnnouncement2) Encode(w *bytes.Buffer, _ uint32) error {
140✔
137
        _, err := w.Write(c.Signature.RawBytes())
140✔
138
        if err != nil {
140✔
139
                return err
×
140
        }
×
141

142
        tlvRecords, err := c.DataToSign()
140✔
143
        if err != nil {
140✔
144
                return err
×
145
        }
×
146

147
        return WriteBytes(w, tlvRecords)
140✔
148
}
149

150
// DataToSign encodes the data to be signed and returns it.
151
func (c *ChannelAnnouncement2) DataToSign() ([]byte, error) {
144✔
152
        producers, err := c.ExtraOpaqueData.RecordProducers()
144✔
153
        if err != nil {
144✔
NEW
154
                return nil, err
×
NEW
155
        }
×
156

157
        // The chain-hash record is only included if it is _not_ equal to the
158
        // bitcoin mainnet genisis block hash.
159
        if !c.ChainHash.Val.IsEqual(chaincfg.MainNetParams.GenesisHash) {
248✔
160
                hash := tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
104✔
161
                hash.Val = c.ChainHash.Val
104✔
162
                producers = append(producers, &hash)
104✔
163
        }
104✔
164

165
        producers = append(producers,
144✔
166
                &c.Features, &c.ShortChannelID, &c.Capacity, &c.NodeID1,
144✔
167
                &c.NodeID2,
144✔
168
        )
144✔
169

144✔
170
        c.BitcoinKey1.WhenSome(func(key tlv.RecordT[tlv.TlvType12, [33]byte]) {
193✔
171
                producers = append(producers, &key)
49✔
172
        })
49✔
173

174
        c.BitcoinKey2.WhenSome(func(key tlv.RecordT[tlv.TlvType14, [33]byte]) {
192✔
175
                producers = append(producers, &key)
48✔
176
        })
48✔
177

178
        c.MerkleRootHash.WhenSome(
144✔
179
                func(hash tlv.RecordT[tlv.TlvType16, [32]byte]) {
201✔
180
                        producers = append(producers, &hash)
57✔
181
                },
57✔
182
        )
183

184
        var tlvData ExtraOpaqueData
144✔
185
        err = tlvData.PackRecords(producers...)
144✔
186
        if err != nil {
144✔
187
                return nil, err
×
188
        }
×
189

190
        return tlvData, nil
144✔
191
}
192

193
// MsgType returns the integer uniquely identifying this message type on the
194
// wire.
195
//
196
// This is part of the lnwire.Message interface.
197
func (c *ChannelAnnouncement2) MsgType() MessageType {
139✔
198
        return MsgChannelAnnouncement2
139✔
199
}
139✔
200

201
// SerializedSize returns the serialized size of the message in bytes.
202
//
203
// This is part of the lnwire.SizeableMessage interface.
204
func (c *ChannelAnnouncement2) SerializedSize() (uint32, error) {
×
205
        return MessageSerializedSize(c)
×
206
}
×
207

208
// A compile time check to ensure ChannelAnnouncement2 implements the
209
// lnwire.Message interface.
210
var _ Message = (*ChannelAnnouncement2)(nil)
211

212
// A compile time check to ensure ChannelAnnouncement2 implements the
213
// lnwire.SizeableMessage interface.
214
var _ SizeableMessage = (*ChannelAnnouncement2)(nil)
215

216
// Node1KeyBytes returns the bytes representing the public key of node 1 in the
217
// channel.
218
//
219
// NOTE: This is part of the ChannelAnnouncement interface.
220
func (c *ChannelAnnouncement2) Node1KeyBytes() [33]byte {
×
221
        return c.NodeID1.Val
×
222
}
×
223

224
// Node2KeyBytes returns the bytes representing the public key of node 2 in the
225
// channel.
226
//
227
// NOTE: This is part of the ChannelAnnouncement interface.
228
func (c *ChannelAnnouncement2) Node2KeyBytes() [33]byte {
×
229
        return c.NodeID2.Val
×
230
}
×
231

232
// GetChainHash returns the hash of the chain which this channel's funding
233
// transaction is confirmed in.
234
//
235
// NOTE: This is part of the ChannelAnnouncement interface.
236
func (c *ChannelAnnouncement2) GetChainHash() chainhash.Hash {
×
237
        return c.ChainHash.Val
×
238
}
×
239

240
// SCID returns the short channel ID of the channel.
241
//
242
// NOTE: This is part of the ChannelAnnouncement interface.
243
func (c *ChannelAnnouncement2) SCID() ShortChannelID {
×
244
        return c.ShortChannelID.Val
×
245
}
×
246

247
// A compile-time check to ensure that ChannelAnnouncement2 implements the
248
// ChannelAnnouncement interface.
249
var _ ChannelAnnouncement = (*ChannelAnnouncement2)(nil)
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