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

lightningnetwork / lnd / 16990665124

15 Aug 2025 01:10PM UTC coverage: 66.74% (-0.03%) from 66.765%
16990665124

Pull #9455

github

web-flow
Merge 035fac41d into fb1adfc21
Pull Request #9455: [1/2] discovery+lnwire: add support for DNS host name in NodeAnnouncement msg

116 of 188 new or added lines in 8 files covered. (61.7%)

110 existing lines in 23 files now uncovered.

136011 of 203791 relevant lines covered (66.74%)

21482.89 hits per line

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

83.01
/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.
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 {
66,058✔
60
        _, err := buf.Write(b)
66,058✔
61
        return err
66,058✔
62
}
66,058✔
63

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

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

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

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

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

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

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

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

6,693✔
139
        if _, err := buf.Write(blockHeight[1:]); err != nil {
6,693✔
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,693✔
145
                return errors.New("tx index should fit in 3 bytes")
×
146
        }
×
147

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

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

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

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

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

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

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

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

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

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

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

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

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

231
// WriteDeliveryAddress appends the address to the provided buffer.
232
func WriteDeliveryAddress(buf *bytes.Buffer, addr DeliveryAddress) error {
523✔
233
        return writeDataWithLength(buf, addr)
523✔
234
}
523✔
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 {
108✔
243
        return writeDataWithLength(buf, payload)
108✔
244
}
108✔
245

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

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

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

261
// WriteBool appends the boolean to the provided buffer.
262
func WriteBool(buf *bytes.Buffer, b bool) error {
128✔
263
        if b {
177✔
264
                return WriteBytes(buf, []byte{1})
49✔
265
        }
49✔
266
        return WriteBytes(buf, []byte{0})
82✔
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 {
126✔
284
        // Before we write anything to the buffer, check the Index is sane.
126✔
285
        if p.Index > math.MaxUint16 {
128✔
286
                return ErrOutpointIndexTooBig(p.Index)
2✔
287
        }
2✔
288

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

295
        // Write the index using two bytes.
296
        return WriteUint16(buf, uint16(p.Index))
124✔
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,426✔
302
        if addr == nil {
1,427✔
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,425✔
310

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

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

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

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

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

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

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

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

351
        // Decode the address.
352
        host, err := tor.Base32Encoding.DecodeString(
301✔
353
                addr.OnionService[:suffixIndex],
301✔
354
        )
301✔
355
        if err != nil {
302✔
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 {
300✔
361
                return err
×
362
        }
×
363
        if _, err := buf.Write(host); err != nil {
300✔
364
                return err
×
365
        }
×
366

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

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

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

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

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

389
        return WriteUint16(buf, addr.Port)
37✔
390
}
391

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

398
        _, err := buf.Write(addr.Payload)
140✔
399
        return err
140✔
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 {
273✔
405
        // First, we'll encode all the addresses into an intermediate
273✔
406
        // buffer. We need to do this in order to compute the total
273✔
407
        // length of the addresses.
273✔
408
        buffer := make([]byte, 0, MaxMsgBody)
273✔
409
        addrBuf := bytes.NewBuffer(buffer)
273✔
410

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

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

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

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