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

lightningnetwork / lnd / 17917482292

22 Sep 2025 01:50PM UTC coverage: 56.562% (-10.1%) from 66.668%
17917482292

Pull #10182

github

web-flow
Merge 9efe3bd8c into 055fb436e
Pull Request #10182: Aux feature bits

32 of 68 new or added lines in 5 files covered. (47.06%)

29734 existing lines in 467 files now uncovered.

98449 of 174056 relevant lines covered (56.56%)

1.18 hits per line

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

62.55
/lnwire/writer.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "encoding/binary"
6
        "errors"
7
        "fmt"
8
        "image/color"
9
        "math"
10
        "net"
11

12
        "github.com/btcsuite/btcd/btcec/v2"
13
        "github.com/btcsuite/btcd/btcutil"
14
        "github.com/btcsuite/btcd/wire"
15
        "github.com/lightningnetwork/lnd/tor"
16
)
17

18
var (
19
        // ErrNilFeatureVector is returned when the supplied feature is nil.
20
        ErrNilFeatureVector = errors.New("cannot write nil feature vector")
21

22
        // ErrPkScriptTooLong is returned when the length of the provided
23
        // script exceeds 34.
24
        ErrPkScriptTooLong = errors.New("'PkScript' too long")
25

26
        // ErrNilTCPAddress is returned when the supplied address is nil.
27
        ErrNilTCPAddress = errors.New("cannot write nil TCPAddr")
28

29
        // ErrNilOnionAddress is returned when the supplied address is nil.
30
        ErrNilOnionAddress = errors.New("cannot write nil onion address")
31

32
        // ErrNilNetAddress is returned when a nil value is used in []net.Addr.
33
        ErrNilNetAddress = errors.New("cannot write nil address")
34

35
        // ErrNilOpaqueAddrs is returned when the supplied address is nil.
36
        ErrNilOpaqueAddrs = errors.New("cannot write nil OpaqueAddrs")
37

38
        // ErrNilDNSAddress is returned when the supplied address is nil.
39
        ErrNilDNSAddress = errors.New("cannot write nil DNS address")
40

41
        // ErrNilPublicKey is returned when a nil pubkey is used.
42
        ErrNilPublicKey = errors.New("cannot write nil pubkey")
43

44
        // ErrUnknownServiceLength is returned when the onion service length is
45
        // unknown.
46
        ErrUnknownServiceLength = errors.New("unknown onion service length")
47
)
48

49
// ErrOutpointIndexTooBig is used when the outpoint index exceeds the max value
50
// of uint16.
UNCOV
51
func ErrOutpointIndexTooBig(index uint32) error {
×
UNCOV
52
        return fmt.Errorf(
×
UNCOV
53
                "index for outpoint (%v) is greater than "+
×
UNCOV
54
                        "max index of %v", index, math.MaxUint16,
×
UNCOV
55
        )
×
UNCOV
56
}
×
57

58
// WriteBytes appends the given bytes to the provided buffer.
59
func WriteBytes(buf *bytes.Buffer, b []byte) error {
2✔
60
        _, err := buf.Write(b)
2✔
61
        return err
2✔
62
}
2✔
63

64
// WriteUint8 appends the uint8 to the provided buffer.
65
func WriteUint8(buf *bytes.Buffer, n uint8) error {
2✔
66
        _, err := buf.Write([]byte{n})
2✔
67
        return err
2✔
68
}
2✔
69

70
// WriteUint16 appends the uint16 to the provided buffer. It encodes the
71
// integer using big endian byte order.
72
func WriteUint16(buf *bytes.Buffer, n uint16) error {
2✔
73
        var b [2]byte
2✔
74
        binary.BigEndian.PutUint16(b[:], n)
2✔
75
        _, err := buf.Write(b[:])
2✔
76
        return err
2✔
77
}
2✔
78

79
// WriteUint32 appends the uint32 to the provided buffer. It encodes the
80
// integer using big endian byte order.
81
func WriteUint32(buf *bytes.Buffer, n uint32) error {
2✔
82
        var b [4]byte
2✔
83
        binary.BigEndian.PutUint32(b[:], n)
2✔
84
        _, err := buf.Write(b[:])
2✔
85
        return err
2✔
86
}
2✔
87

88
// WriteUint64 appends the uint64 to the provided buffer. It encodes the
89
// integer using big endian byte order.
90
func WriteUint64(buf *bytes.Buffer, n uint64) error {
2✔
91
        var b [8]byte
2✔
92
        binary.BigEndian.PutUint64(b[:], n)
2✔
93
        _, err := buf.Write(b[:])
2✔
94
        return err
2✔
95
}
2✔
96

97
// WriteSatoshi appends the Satoshi value to the provided buffer.
98
func WriteSatoshi(buf *bytes.Buffer, amount btcutil.Amount) error {
2✔
99
        return WriteUint64(buf, uint64(amount))
2✔
100
}
2✔
101

102
// WriteMilliSatoshi appends the MilliSatoshi value to the provided buffer.
103
func WriteMilliSatoshi(buf *bytes.Buffer, amount MilliSatoshi) error {
2✔
104
        return WriteUint64(buf, uint64(amount))
2✔
105
}
2✔
106

107
// WritePublicKey appends the compressed public key to the provided buffer.
108
func WritePublicKey(buf *bytes.Buffer, pub *btcec.PublicKey) error {
2✔
109
        if pub == nil {
2✔
UNCOV
110
                return ErrNilPublicKey
×
UNCOV
111
        }
×
112

113
        serializedPubkey := pub.SerializeCompressed()
2✔
114
        return WriteBytes(buf, serializedPubkey)
2✔
115
}
116

117
// WriteChannelID appends the ChannelID to the provided buffer.
118
func WriteChannelID(buf *bytes.Buffer, channelID ChannelID) error {
2✔
119
        return WriteBytes(buf, channelID[:])
2✔
120
}
2✔
121

122
// WriteNodeAlias appends the alias to the provided buffer.
123
func WriteNodeAlias(buf *bytes.Buffer, alias NodeAlias) error {
2✔
124
        return WriteBytes(buf, alias[:])
2✔
125
}
2✔
126

127
// WriteShortChannelID appends the ShortChannelID to the provided buffer. It
128
// encodes the BlockHeight and TxIndex each using 3 bytes with big endian byte
129
// order, and encodes txPosition using 2 bytes with big endian byte order.
130
func WriteShortChannelID(buf *bytes.Buffer, shortChanID ShortChannelID) error {
2✔
131
        // Check that field fit in 3 bytes and write the blockHeight
2✔
132
        if shortChanID.BlockHeight > ((1 << 24) - 1) {
2✔
133
                return errors.New("block height should fit in 3 bytes")
×
134
        }
×
135

136
        var blockHeight [4]byte
2✔
137
        binary.BigEndian.PutUint32(blockHeight[:], shortChanID.BlockHeight)
2✔
138

2✔
139
        if _, err := buf.Write(blockHeight[1:]); err != nil {
2✔
140
                return err
×
141
        }
×
142

143
        // Check that field fit in 3 bytes and write the txIndex
144
        if shortChanID.TxIndex > ((1 << 24) - 1) {
2✔
145
                return errors.New("tx index should fit in 3 bytes")
×
146
        }
×
147

148
        var txIndex [4]byte
2✔
149
        binary.BigEndian.PutUint32(txIndex[:], shortChanID.TxIndex)
2✔
150
        if _, err := buf.Write(txIndex[1:]); err != nil {
2✔
151
                return err
×
152
        }
×
153

154
        // Write the TxPosition
155
        return WriteUint16(buf, shortChanID.TxPosition)
2✔
156
}
157

158
// WriteSig appends the signature to the provided buffer.
159
func WriteSig(buf *bytes.Buffer, sig Sig) error {
2✔
160
        return WriteBytes(buf, sig.bytes[:])
2✔
161
}
2✔
162

163
// WriteSigs appends the slice of signatures to the provided buffer with its
164
// length.
165
func WriteSigs(buf *bytes.Buffer, sigs []Sig) error {
2✔
166
        // Write the length of the sigs.
2✔
167
        if err := WriteUint16(buf, uint16(len(sigs))); err != nil {
2✔
168
                return err
×
169
        }
×
170

171
        for _, sig := range sigs {
4✔
172
                if err := WriteSig(buf, sig); err != nil {
2✔
173
                        return err
×
174
                }
×
175
        }
176
        return nil
2✔
177
}
178

179
// WriteFailCode appends the FailCode to the provided buffer.
180
func WriteFailCode(buf *bytes.Buffer, e FailCode) error {
2✔
181
        return WriteUint16(buf, uint16(e))
2✔
182
}
2✔
183

184
// WriteRawFeatureVector encodes the feature using the feature's Encode method
185
// and appends the data to the provided buffer. An error will return if the
186
// passed feature is nil.
187
func WriteRawFeatureVector(buf *bytes.Buffer, feature *RawFeatureVector) error {
2✔
188
        if feature == nil {
2✔
UNCOV
189
                return ErrNilFeatureVector
×
UNCOV
190
        }
×
191

192
        return feature.Encode(buf)
2✔
193
}
194

195
// WriteColorRGBA appends the RGBA color using three bytes.
196
func WriteColorRGBA(buf *bytes.Buffer, e color.RGBA) error {
2✔
197
        // Write R
2✔
198
        if err := WriteUint8(buf, e.R); err != nil {
2✔
199
                return err
×
200
        }
×
201

202
        // Write G
203
        if err := WriteUint8(buf, e.G); err != nil {
2✔
204
                return err
×
205
        }
×
206

207
        // Write B
208
        return WriteUint8(buf, e.B)
2✔
209
}
210

211
// WriteQueryEncoding appends the QueryEncoding to the provided buffer.
212
func WriteQueryEncoding(buf *bytes.Buffer, e QueryEncoding) error {
2✔
213
        return WriteUint8(buf, uint8(e))
2✔
214
}
2✔
215

216
// WriteFundingFlag appends the FundingFlag to the provided buffer.
217
func WriteFundingFlag(buf *bytes.Buffer, flag FundingFlag) error {
2✔
218
        return WriteUint8(buf, uint8(flag))
2✔
219
}
2✔
220

221
// WriteChanUpdateMsgFlags appends the update flag to the provided buffer.
222
func WriteChanUpdateMsgFlags(buf *bytes.Buffer, f ChanUpdateMsgFlags) error {
2✔
223
        return WriteUint8(buf, uint8(f))
2✔
224
}
2✔
225

226
// WriteChanUpdateChanFlags appends the update flag to the provided buffer.
227
func WriteChanUpdateChanFlags(buf *bytes.Buffer, f ChanUpdateChanFlags) error {
2✔
228
        return WriteUint8(buf, uint8(f))
2✔
229
}
2✔
230

231
// WriteDeliveryAddress appends the address to the provided buffer.
232
func WriteDeliveryAddress(buf *bytes.Buffer, addr DeliveryAddress) error {
2✔
233
        return writeDataWithLength(buf, addr)
2✔
234
}
2✔
235

236
// WritePingPayload appends the payload to the provided buffer.
UNCOV
237
func WritePingPayload(buf *bytes.Buffer, payload PingPayload) error {
×
UNCOV
238
        return writeDataWithLength(buf, payload)
×
UNCOV
239
}
×
240

241
// WritePongPayload appends the payload to the provided buffer.
UNCOV
242
func WritePongPayload(buf *bytes.Buffer, payload PongPayload) error {
×
UNCOV
243
        return writeDataWithLength(buf, payload)
×
UNCOV
244
}
×
245

246
// WriteWarningData appends the data to the provided buffer.
UNCOV
247
func WriteWarningData(buf *bytes.Buffer, data WarningData) error {
×
UNCOV
248
        return writeDataWithLength(buf, data)
×
UNCOV
249
}
×
250

251
// WriteErrorData appends the data to the provided buffer.
252
func WriteErrorData(buf *bytes.Buffer, data ErrorData) error {
2✔
253
        return writeDataWithLength(buf, data)
2✔
254
}
2✔
255

256
// WriteOpaqueReason appends the reason to the provided buffer.
257
func WriteOpaqueReason(buf *bytes.Buffer, reason OpaqueReason) error {
2✔
258
        return writeDataWithLength(buf, reason)
2✔
259
}
2✔
260

261
// WriteBool appends the boolean to the provided buffer.
262
func WriteBool(buf *bytes.Buffer, b bool) error {
2✔
263
        if b {
4✔
264
                return WriteBytes(buf, []byte{1})
2✔
265
        }
2✔
266
        return WriteBytes(buf, []byte{0})
2✔
267
}
268

269
// WritePkScript appends the script to the provided buffer. Returns an error if
270
// the provided script exceeds 34 bytes.
UNCOV
271
func WritePkScript(buf *bytes.Buffer, s PkScript) error {
×
UNCOV
272
        // The largest script we'll accept is a p2wsh which is exactly
×
UNCOV
273
        // 34 bytes long.
×
UNCOV
274
        scriptLength := len(s)
×
UNCOV
275
        if scriptLength > 34 {
×
UNCOV
276
                return ErrPkScriptTooLong
×
UNCOV
277
        }
×
278

UNCOV
279
        return wire.WriteVarBytes(buf, 0, s)
×
280
}
281

282
// WriteOutPoint appends the outpoint to the provided buffer.
283
func WriteOutPoint(buf *bytes.Buffer, p wire.OutPoint) error {
2✔
284
        // Before we write anything to the buffer, check the Index is sane.
2✔
285
        if p.Index > math.MaxUint16 {
2✔
UNCOV
286
                return ErrOutpointIndexTooBig(p.Index)
×
UNCOV
287
        }
×
288

289
        var h [32]byte
2✔
290
        copy(h[:], p.Hash[:])
2✔
291
        if _, err := buf.Write(h[:]); err != nil {
2✔
292
                return err
×
293
        }
×
294

295
        // Write the index using two bytes.
296
        return WriteUint16(buf, uint16(p.Index))
2✔
297
}
298

299
// WriteTCPAddr appends the TCP address to the provided buffer, either a IPv4
300
// or a IPv6.
301
func WriteTCPAddr(buf *bytes.Buffer, addr *net.TCPAddr) error {
2✔
302
        if addr == nil {
2✔
UNCOV
303
                return ErrNilTCPAddress
×
UNCOV
304
        }
×
305

306
        // Make a slice of bytes to hold the data of descriptor and ip. At
307
        // most, we need 17 bytes - 1 byte for the descriptor, 16 bytes for
308
        // IPv6.
309
        data := make([]byte, 0, 17)
2✔
310

2✔
311
        if addr.IP.To4() != nil {
4✔
312
                data = append(data, uint8(tcp4Addr))
2✔
313
                data = append(data, addr.IP.To4()...)
2✔
314
        } else {
4✔
315
                data = append(data, uint8(tcp6Addr))
2✔
316
                data = append(data, addr.IP.To16()...)
2✔
317
        }
2✔
318

319
        if _, err := buf.Write(data); err != nil {
2✔
320
                return err
×
321
        }
×
322

323
        return WriteUint16(buf, uint16(addr.Port))
2✔
324
}
325

326
// WriteOnionAddr appends the onion address to the provided buffer.
327
func WriteOnionAddr(buf *bytes.Buffer, addr *tor.OnionAddr) error {
2✔
328
        if addr == nil {
2✔
UNCOV
329
                return ErrNilOnionAddress
×
UNCOV
330
        }
×
331

332
        var (
2✔
333
                suffixIndex int
2✔
334
                descriptor  []byte
2✔
335
        )
2✔
336

2✔
337
        // Decide the suffixIndex and descriptor.
2✔
338
        switch len(addr.OnionService) {
2✔
339
        case tor.V2Len:
2✔
340
                descriptor = []byte{byte(v2OnionAddr)}
2✔
341
                suffixIndex = tor.V2Len - tor.OnionSuffixLen
2✔
342

343
        case tor.V3Len:
2✔
344
                descriptor = []byte{byte(v3OnionAddr)}
2✔
345
                suffixIndex = tor.V3Len - tor.OnionSuffixLen
2✔
346

UNCOV
347
        default:
×
UNCOV
348
                return ErrUnknownServiceLength
×
349
        }
350

351
        // Decode the address.
352
        host, err := tor.Base32Encoding.DecodeString(
2✔
353
                addr.OnionService[:suffixIndex],
2✔
354
        )
2✔
355
        if err != nil {
2✔
UNCOV
356
                return err
×
UNCOV
357
        }
×
358

359
        // Perform the actual write when the above checks passed.
360
        if _, err := buf.Write(descriptor); err != nil {
2✔
361
                return err
×
362
        }
×
363
        if _, err := buf.Write(host); err != nil {
2✔
364
                return err
×
365
        }
×
366

367
        return WriteUint16(buf, uint16(addr.Port))
2✔
368
}
369

370
// WriteDNSAddress appends the DNS address to the provided buffer.
UNCOV
371
func WriteDNSAddress(buf *bytes.Buffer, addr *DNSAddress) error {
×
UNCOV
372
        if addr == nil {
×
373
                return ErrNilDNSAddress
×
374
        }
×
375

376
        // Write the descriptor, the hostname length, and the hostname.
UNCOV
377
        if _, err := buf.Write([]byte{byte(dnsAddr)}); err != nil {
×
378
                return err
×
379
        }
×
380

UNCOV
381
        if err := WriteUint8(buf, uint8(len(addr.Hostname))); err != nil {
×
382
                return err
×
383
        }
×
384

UNCOV
385
        if _, err := buf.WriteString(addr.Hostname); err != nil {
×
386
                return err
×
387
        }
×
388

UNCOV
389
        return WriteUint16(buf, addr.Port)
×
390
}
391

392
// WriteOpaqueAddrs appends the payload of the given OpaqueAddrs to buffer.
UNCOV
393
func WriteOpaqueAddrs(buf *bytes.Buffer, addr *OpaqueAddrs) error {
×
UNCOV
394
        if addr == nil {
×
395
                return ErrNilOpaqueAddrs
×
396
        }
×
397

UNCOV
398
        _, err := buf.Write(addr.Payload)
×
UNCOV
399
        return err
×
400
}
401

402
// WriteNetAddrs appends a slice of addresses to the provided buffer with the
403
// length info.
404
func WriteNetAddrs(buf *bytes.Buffer, addresses []net.Addr) error {
2✔
405
        // First, we'll encode all the addresses into an intermediate
2✔
406
        // buffer. We need to do this in order to compute the total
2✔
407
        // length of the addresses.
2✔
408
        buffer := make([]byte, 0, MaxMsgBody)
2✔
409
        addrBuf := bytes.NewBuffer(buffer)
2✔
410

2✔
411
        for _, address := range addresses {
4✔
412
                switch a := address.(type) {
2✔
413
                case *net.TCPAddr:
2✔
414
                        if err := WriteTCPAddr(addrBuf, a); err != nil {
2✔
415
                                return err
×
416
                        }
×
417
                case *tor.OnionAddr:
2✔
418
                        if err := WriteOnionAddr(addrBuf, a); err != nil {
2✔
419
                                return err
×
420
                        }
×
UNCOV
421
                case *OpaqueAddrs:
×
UNCOV
422
                        if err := WriteOpaqueAddrs(addrBuf, a); err != nil {
×
423
                                return err
×
424
                        }
×
UNCOV
425
                case *DNSAddress:
×
UNCOV
426
                        if err := WriteDNSAddress(addrBuf, a); err != nil {
×
427
                                return err
×
428
                        }
×
UNCOV
429
                default:
×
UNCOV
430
                        return ErrNilNetAddress
×
431
                }
432
        }
433

434
        // With the addresses fully encoded, we can now write out data.
435
        return writeDataWithLength(buf, addrBuf.Bytes())
2✔
436
}
437

438
// writeDataWithLength writes the data and its length to the buffer.
439
func writeDataWithLength(buf *bytes.Buffer, data []byte) error {
2✔
440
        var l [2]byte
2✔
441
        binary.BigEndian.PutUint16(l[:], uint16(len(data)))
2✔
442
        if _, err := buf.Write(l[:]); err != nil {
2✔
443
                return err
×
444
        }
×
445

446
        _, err := buf.Write(data)
2✔
447
        return err
2✔
448
}
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