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

lightningnetwork / lnd / 14277115115

05 Apr 2025 01:43AM UTC coverage: 58.056% (-11.0%) from 69.04%
14277115115

Pull #9670

github

web-flow
Merge a7e89c130 into f0ea5bf3b
Pull Request #9670: build: bump version to v0.19.0 rc2

96191 of 165688 relevant lines covered (58.06%)

1.22 hits per line

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

67.22
/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
        // ErrNilPublicKey is returned when a nil pubkey is used.
39
        ErrNilPublicKey = errors.New("cannot write nil pubkey")
40

41
        // ErrUnknownServiceLength is returned when the onion service length is
42
        // unknown.
43
        ErrUnknownServiceLength = errors.New("unknown onion service length")
44
)
45

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

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

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

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

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

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

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

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

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

110
        serializedPubkey := pub.SerializeCompressed()
2✔
111
        return WriteBytes(buf, serializedPubkey)
2✔
112
}
113

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

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

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

133
        var blockHeight [4]byte
2✔
134
        binary.BigEndian.PutUint32(blockHeight[:], shortChanID.BlockHeight)
2✔
135

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

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

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

151
        // Write the TxPosition
152
        return WriteUint16(buf, shortChanID.TxPosition)
2✔
153
}
154

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

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

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

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

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

189
        return feature.Encode(buf)
2✔
190
}
191

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

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

204
        // Write B
205
        return WriteUint8(buf, e.B)
2✔
206
}
207

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

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

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

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

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

233
// WritePingPayload appends the payload to the provided buffer.
234
func WritePingPayload(buf *bytes.Buffer, payload PingPayload) error {
×
235
        return writeDataWithLength(buf, payload)
×
236
}
×
237

238
// WritePongPayload appends the payload to the provided buffer.
239
func WritePongPayload(buf *bytes.Buffer, payload PongPayload) error {
×
240
        return writeDataWithLength(buf, payload)
×
241
}
×
242

243
// WriteWarningData appends the data to the provided buffer.
244
func WriteWarningData(buf *bytes.Buffer, data WarningData) error {
×
245
        return writeDataWithLength(buf, data)
×
246
}
×
247

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

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

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

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

276
        return wire.WriteVarBytes(buf, 0, s)
×
277
}
278

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

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

292
        // Write the index using two bytes.
293
        return WriteUint16(buf, uint16(p.Index))
2✔
294
}
295

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

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

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

316
        if _, err := buf.Write(data); err != nil {
2✔
317
                return err
×
318
        }
×
319

320
        return WriteUint16(buf, uint16(addr.Port))
2✔
321
}
322

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

329
        var (
2✔
330
                suffixIndex int
2✔
331
                descriptor  []byte
2✔
332
        )
2✔
333

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

340
        case tor.V3Len:
2✔
341
                descriptor = []byte{byte(v3OnionAddr)}
2✔
342
                suffixIndex = tor.V3Len - tor.OnionSuffixLen
2✔
343

344
        default:
×
345
                return ErrUnknownServiceLength
×
346
        }
347

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

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

364
        return WriteUint16(buf, uint16(addr.Port))
2✔
365
}
366

367
// WriteOpaqueAddrs appends the payload of the given OpaqueAddrs to buffer.
368
func WriteOpaqueAddrs(buf *bytes.Buffer, addr *OpaqueAddrs) error {
×
369
        if addr == nil {
×
370
                return ErrNilOpaqueAddrs
×
371
        }
×
372

373
        _, err := buf.Write(addr.Payload)
×
374
        return err
×
375
}
376

377
// WriteNetAddrs appends a slice of addresses to the provided buffer with the
378
// length info.
379
func WriteNetAddrs(buf *bytes.Buffer, addresses []net.Addr) error {
2✔
380
        // First, we'll encode all the addresses into an intermediate
2✔
381
        // buffer. We need to do this in order to compute the total
2✔
382
        // length of the addresses.
2✔
383
        buffer := make([]byte, 0, MaxMsgBody)
2✔
384
        addrBuf := bytes.NewBuffer(buffer)
2✔
385

2✔
386
        for _, address := range addresses {
4✔
387
                switch a := address.(type) {
2✔
388
                case *net.TCPAddr:
2✔
389
                        if err := WriteTCPAddr(addrBuf, a); err != nil {
2✔
390
                                return err
×
391
                        }
×
392
                case *tor.OnionAddr:
2✔
393
                        if err := WriteOnionAddr(addrBuf, a); err != nil {
2✔
394
                                return err
×
395
                        }
×
396
                case *OpaqueAddrs:
×
397
                        if err := WriteOpaqueAddrs(addrBuf, a); err != nil {
×
398
                                return err
×
399
                        }
×
400
                default:
×
401
                        return ErrNilNetAddress
×
402
                }
403
        }
404

405
        // With the addresses fully encoded, we can now write out data.
406
        return writeDataWithLength(buf, addrBuf.Bytes())
2✔
407
}
408

409
// writeDataWithLength writes the data and its length to the buffer.
410
func writeDataWithLength(buf *bytes.Buffer, data []byte) error {
2✔
411
        var l [2]byte
2✔
412
        binary.BigEndian.PutUint16(l[:], uint16(len(data)))
2✔
413
        if _, err := buf.Write(l[:]); err != nil {
2✔
414
                return err
×
415
        }
×
416

417
        _, err := buf.Write(data)
2✔
418
        return err
2✔
419
}
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