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

lightningnetwork / lnd / 16338168815

17 Jul 2025 06:45AM UTC coverage: 57.555% (-0.02%) from 57.577%
16338168815

Pull #10072

github

web-flow
Merge c3fbbc73d into 47e9f05dd
Pull Request #10072: [1/2] lnwire: fix encoding customized TLV records

0 of 138 new or added lines in 4 files covered. (0.0%)

56 existing lines in 13 files now uncovered.

98641 of 171385 relevant lines covered (57.56%)

1.79 hits per line

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

0.0
/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 {
×
76
        err := ReadElement(r, &c.Signature)
×
77
        if err != nil {
×
78
                return err
×
79
        }
×
80
        c.Signature.ForceSchnorr()
×
81

×
82
        return c.DecodeTLVRecords(r)
×
83
}
84

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

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

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

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

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

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

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

NEW
127
        c.ExtraOpaqueData = extraData
×
128

×
129
        return c.ExtraOpaqueData.ValidateTLV()
×
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 {
×
137
        _, err := w.Write(c.Signature.RawBytes())
×
138
        if err != nil {
×
139
                return err
×
140
        }
×
141

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

NEW
147
        return WriteBytes(w, tlvRecords)
×
148
}
149

150
// DataToSign encodes the data to be signed and returns it.
UNCOV
151
func (c *ChannelAnnouncement2) DataToSign() ([]byte, error) {
×
NEW
152
        producers, err := c.ExtraOpaqueData.RecordProducers()
×
NEW
153
        if err != nil {
×
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) {
×
160
                hash := tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
×
161
                hash.Val = c.ChainHash.Val
×
NEW
162
                producers = append(producers, &hash)
×
163
        }
×
164

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

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

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

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

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

NEW
190
        return tlvData, nil
×
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 {
×
198
        return MsgChannelAnnouncement2
×
199
}
×
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