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

lightningnetwork / lnd / 16683051882

01 Aug 2025 07:03PM UTC coverage: 54.949% (-12.1%) from 67.047%
16683051882

Pull #9455

github

web-flow
Merge 3f1f50be8 into 37523b6cb
Pull Request #9455: discovery+lnwire: add support for DNS host name in NodeAnnouncement msg

144 of 226 new or added lines in 7 files covered. (63.72%)

23852 existing lines in 290 files now uncovered.

108751 of 197912 relevant lines covered (54.95%)

22080.83 hits per line

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

83.33
/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
        // ErrNilDNSAddress is returned when the supplied address is nil.
33
        ErrNilDNSAddress = errors.New("cannot write nil DNS address")
34

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

38
        // ErrNilOpaqueAddrs is returned when the supplied address is nil.
39
        ErrNilOpaqueAddrs = errors.New("cannot write nil OpaqueAddrs")
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.
51
func ErrOutpointIndexTooBig(index uint32) error {
3✔
52
        return fmt.Errorf(
3✔
53
                "index for outpoint (%v) is greater than "+
3✔
54
                        "max index of %v", index, math.MaxUint16,
3✔
55
        )
3✔
56
}
3✔
57

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

64
// WriteUint8 appends the uint8 to the provided buffer.
65
func WriteUint8(buf *bytes.Buffer, n uint8) error {
3,408✔
66
        _, err := buf.Write([]byte{n})
3,408✔
67
        return err
3,408✔
68
}
3,408✔
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 {
15,579✔
73
        var b [2]byte
15,579✔
74
        binary.BigEndian.PutUint16(b[:], n)
15,579✔
75
        _, err := buf.Write(b[:])
15,579✔
76
        return err
15,579✔
77
}
15,579✔
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 {
8,083✔
82
        var b [4]byte
8,083✔
83
        binary.BigEndian.PutUint32(b[:], n)
8,083✔
84
        _, err := buf.Write(b[:])
8,083✔
85
        return err
8,083✔
86
}
8,083✔
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 {
11,974✔
91
        var b [8]byte
11,974✔
92
        binary.BigEndian.PutUint64(b[:], n)
11,974✔
93
        _, err := buf.Write(b[:])
11,974✔
94
        return err
11,974✔
95
}
11,974✔
96

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

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

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

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

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

122
// WriteNodeAlias appends the alias to the provided buffer.
123
func WriteNodeAlias(buf *bytes.Buffer, alias NodeAlias) error {
267✔
124
        return WriteBytes(buf, alias[:])
267✔
125
}
267✔
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 {
6,798✔
131
        // Check that field fit in 3 bytes and write the blockHeight
6,798✔
132
        if shortChanID.BlockHeight > ((1 << 24) - 1) {
6,798✔
133
                return errors.New("block height should fit in 3 bytes")
×
134
        }
×
135

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

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

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

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

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

158
// WriteSig appends the signature to the provided buffer.
159
func WriteSig(buf *bytes.Buffer, sig Sig) error {
15,075✔
160
        return WriteBytes(buf, sig.bytes[:])
15,075✔
161
}
15,075✔
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 {
4,167✔
166
        // Write the length of the sigs.
4,167✔
167
        if err := WriteUint16(buf, uint16(len(sigs))); err != nil {
4,167✔
168
                return err
×
169
        }
×
170

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

179
// WriteFailCode appends the FailCode to the provided buffer.
180
func WriteFailCode(buf *bytes.Buffer, e FailCode) error {
119✔
181
        return WriteUint16(buf, uint16(e))
119✔
182
}
119✔
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 {
1,987✔
188
        if feature == nil {
1,989✔
189
                return ErrNilFeatureVector
2✔
190
        }
2✔
191

192
        return feature.Encode(buf)
1,985✔
193
}
194

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

279
        return wire.WriteVarBytes(buf, 0, s)
1✔
280
}
281

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

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

295
        // Write the index using two bytes.
296
        return WriteUint16(buf, uint16(p.Index))
121✔
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 {
1,415✔
302
        if addr == nil {
1,416✔
303
                return ErrNilTCPAddress
1✔
304
        }
1✔
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)
1,414✔
310

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

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

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

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

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

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

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

347
        default:
1✔
348
                return ErrUnknownServiceLength
1✔
349
        }
350

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

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

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

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

376
        // Write the DNS address type descriptor.
377
        if err := buf.WriteByte(byte(dnsHostnameAddr)); err != nil {
4✔
NEW
378
                return err
×
NEW
379
        }
×
380

381
        // Write the DNS hostname address length.
382
        if err := buf.WriteByte(byte(len(addr.Hostname))); err != nil {
4✔
NEW
383
                return err
×
NEW
384
        }
×
385

386
        // Write the DNS Hostname address.
387
        if _, err := buf.WriteString(addr.Hostname); err != nil {
4✔
NEW
388
                return err
×
NEW
389
        }
×
390

391
        return WriteUint16(buf, addr.Port)
4✔
392
}
393

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

400
        _, err := buf.Write(addr.Payload)
140✔
401
        return err
140✔
402
}
403

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

271✔
413
        var dnsAddrIncluded bool
271✔
414
        for _, address := range addresses {
1,978✔
415
                switch a := address.(type) {
1,707✔
416
                case *net.TCPAddr:
1,340✔
417
                        if err := WriteTCPAddr(addrBuf, a); err != nil {
1,340✔
418
                                return err
×
419
                        }
×
420
                case *tor.OnionAddr:
259✔
421
                        if err := WriteOnionAddr(addrBuf, a); err != nil {
259✔
422
                                return err
×
423
                        }
×
424
                case *DNSAddr:
3✔
425
                        if dnsAddrIncluded {
4✔
426
                                return ErrMultipleDNSAddresses
1✔
427
                        }
1✔
428
                        if err := WriteDNSAddr(addrBuf, a); err != nil {
2✔
NEW
429
                                return err
×
NEW
430
                        }
×
431
                        dnsAddrIncluded = true
2✔
432
                case *OpaqueAddrs:
104✔
433
                        if err := WriteOpaqueAddrs(addrBuf, a); err != nil {
104✔
434
                                return err
×
435
                        }
×
436
                default:
1✔
437
                        return ErrNilNetAddress
1✔
438
                }
439
        }
440

441
        // With the addresses fully encoded, we can now write out data.
442
        return writeDataWithLength(buf, addrBuf.Bytes())
269✔
443
}
444

445
// writeDataWithLength writes the data and its length to the buffer.
446
func writeDataWithLength(buf *bytes.Buffer, data []byte) error {
2,013✔
447
        var l [2]byte
2,013✔
448
        binary.BigEndian.PutUint16(l[:], uint16(len(data)))
2,013✔
449
        if _, err := buf.Write(l[:]); err != nil {
2,013✔
450
                return err
×
451
        }
×
452

453
        _, err := buf.Write(data)
2,013✔
454
        return err
2,013✔
455
}
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