forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimitive.go
364 lines (319 loc) · 10 KB
/
primitive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package tlv
import (
"encoding/binary"
"fmt"
"io"
"github.com/btcsuite/btcd/btcec/v2"
)
// ErrTypeForEncoding signals that an incorrect type was passed to an Encoder.
type ErrTypeForEncoding struct {
val interface{}
expType string
}
// NewTypeForEncodingErr creates a new ErrTypeForEncoding given the incorrect
// val and the expected type.
func NewTypeForEncodingErr(val interface{}, expType string) ErrTypeForEncoding {
return ErrTypeForEncoding{
val: val,
expType: expType,
}
}
// Error returns a human-readable description of the type mismatch.
func (e ErrTypeForEncoding) Error() string {
return fmt.Sprintf("ErrTypeForEncoding want (type: *%s), "+
"got (type: %T)", e.expType, e.val)
}
// ErrTypeForDecoding signals that an incorrect type was passed to a Decoder or
// that the expected length of the encoding is different from that required by
// the expected type.
type ErrTypeForDecoding struct {
val interface{}
expType string
valLength uint64
expLength uint64
}
// NewTypeForDecodingErr creates a new ErrTypeForDecoding given the incorrect
// val and expected type, or the mismatch in their expected lengths.
func NewTypeForDecodingErr(val interface{}, expType string,
valLength, expLength uint64) ErrTypeForDecoding {
return ErrTypeForDecoding{
val: val,
expType: expType,
valLength: valLength,
expLength: expLength,
}
}
// Error returns a human-readable description of the type mismatch.
func (e ErrTypeForDecoding) Error() string {
return fmt.Sprintf("ErrTypeForDecoding want (type: *%s, length: %v), "+
"got (type: %T, length: %v)", e.expType, e.expLength, e.val,
e.valLength)
}
var (
byteOrder = binary.BigEndian
)
// EUint8 is an Encoder for uint8 values. An error is returned if val is not a
// *uint8.
func EUint8(w io.Writer, val interface{}, buf *[8]byte) error {
if i, ok := val.(*uint8); ok {
buf[0] = *i
_, err := w.Write(buf[:1])
return err
}
return NewTypeForEncodingErr(val, "uint8")
}
// EUint8T encodes a uint8 val to the provided io.Writer. This method is exposed
// so that encodings for custom uint8-like types can be created without
// incurring an extra heap allocation.
func EUint8T(w io.Writer, val uint8, buf *[8]byte) error {
buf[0] = val
_, err := w.Write(buf[:1])
return err
}
// EUint16 is an Encoder for uint16 values. An error is returned if val is not a
// *uint16.
func EUint16(w io.Writer, val interface{}, buf *[8]byte) error {
if i, ok := val.(*uint16); ok {
byteOrder.PutUint16(buf[:2], *i)
_, err := w.Write(buf[:2])
return err
}
return NewTypeForEncodingErr(val, "uint16")
}
// EUint16T encodes a uint16 val to the provided io.Writer. This method is
// exposed so that encodings for custom uint16-like types can be created without
// incurring an extra heap allocation.
func EUint16T(w io.Writer, val uint16, buf *[8]byte) error {
byteOrder.PutUint16(buf[:2], val)
_, err := w.Write(buf[:2])
return err
}
// EUint32 is an Encoder for uint32 values. An error is returned if val is not a
// *uint32.
func EUint32(w io.Writer, val interface{}, buf *[8]byte) error {
if i, ok := val.(*uint32); ok {
byteOrder.PutUint32(buf[:4], *i)
_, err := w.Write(buf[:4])
return err
}
return NewTypeForEncodingErr(val, "uint32")
}
// EUint32T encodes a uint32 val to the provided io.Writer. This method is
// exposed so that encodings for custom uint32-like types can be created without
// incurring an extra heap allocation.
func EUint32T(w io.Writer, val uint32, buf *[8]byte) error {
byteOrder.PutUint32(buf[:4], val)
_, err := w.Write(buf[:4])
return err
}
// EUint64 is an Encoder for uint64 values. An error is returned if val is not a
// *uint64.
func EUint64(w io.Writer, val interface{}, buf *[8]byte) error {
if i, ok := val.(*uint64); ok {
byteOrder.PutUint64(buf[:], *i)
_, err := w.Write(buf[:])
return err
}
return NewTypeForEncodingErr(val, "uint64")
}
// EUint64T encodes a uint64 val to the provided io.Writer. This method is
// exposed so that encodings for custom uint64-like types can be created without
// incurring an extra heap allocation.
func EUint64T(w io.Writer, val uint64, buf *[8]byte) error {
byteOrder.PutUint64(buf[:], val)
_, err := w.Write(buf[:])
return err
}
// DUint8 is a Decoder for uint8 values. An error is returned if val is not a
// *uint8.
func DUint8(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
if i, ok := val.(*uint8); ok && l == 1 {
if _, err := io.ReadFull(r, buf[:1]); err != nil {
return err
}
*i = buf[0]
return nil
}
return NewTypeForDecodingErr(val, "uint8", l, 1)
}
// DUint16 is a Decoder for uint16 values. An error is returned if val is not a
// *uint16.
func DUint16(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
if i, ok := val.(*uint16); ok && l == 2 {
if _, err := io.ReadFull(r, buf[:2]); err != nil {
return err
}
*i = byteOrder.Uint16(buf[:2])
return nil
}
return NewTypeForDecodingErr(val, "uint16", l, 2)
}
// DUint32 is a Decoder for uint32 values. An error is returned if val is not a
// *uint32.
func DUint32(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
if i, ok := val.(*uint32); ok && l == 4 {
if _, err := io.ReadFull(r, buf[:4]); err != nil {
return err
}
*i = byteOrder.Uint32(buf[:4])
return nil
}
return NewTypeForDecodingErr(val, "uint32", l, 4)
}
// DUint64 is a Decoder for uint64 values. An error is returned if val is not a
// *uint64.
func DUint64(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
if i, ok := val.(*uint64); ok && l == 8 {
if _, err := io.ReadFull(r, buf[:]); err != nil {
return err
}
*i = byteOrder.Uint64(buf[:])
return nil
}
return NewTypeForDecodingErr(val, "uint64", l, 8)
}
// EBytes32 is an Encoder for 32-byte arrays. An error is returned if val is not
// a *[32]byte.
func EBytes32(w io.Writer, val interface{}, _ *[8]byte) error {
if b, ok := val.(*[32]byte); ok {
_, err := w.Write(b[:])
return err
}
return NewTypeForEncodingErr(val, "[32]byte")
}
// DBytes32 is a Decoder for 32-byte arrays. An error is returned if val is not
// a *[32]byte.
func DBytes32(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if b, ok := val.(*[32]byte); ok && l == 32 {
_, err := io.ReadFull(r, b[:])
return err
}
return NewTypeForDecodingErr(val, "[32]byte", l, 32)
}
// EBytes33 is an Encoder for 33-byte arrays. An error is returned if val is not
// a *[33]byte.
func EBytes33(w io.Writer, val interface{}, _ *[8]byte) error {
if b, ok := val.(*[33]byte); ok {
_, err := w.Write(b[:])
return err
}
return NewTypeForEncodingErr(val, "[33]byte")
}
// DBytes33 is a Decoder for 33-byte arrays. An error is returned if val is not
// a *[33]byte.
func DBytes33(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if b, ok := val.(*[33]byte); ok {
_, err := io.ReadFull(r, b[:])
return err
}
return NewTypeForDecodingErr(val, "[33]byte", l, 33)
}
// EBytes64 is an Encoder for 64-byte arrays. An error is returned if val is not
// a *[64]byte.
func EBytes64(w io.Writer, val interface{}, _ *[8]byte) error {
if b, ok := val.(*[64]byte); ok {
_, err := w.Write(b[:])
return err
}
return NewTypeForEncodingErr(val, "[64]byte")
}
// DBytes64 is an Decoder for 64-byte arrays. An error is returned if val is not
// a *[64]byte.
func DBytes64(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if b, ok := val.(*[64]byte); ok && l == 64 {
_, err := io.ReadFull(r, b[:])
return err
}
return NewTypeForDecodingErr(val, "[64]byte", l, 64)
}
// EPubKey is an Encoder for *btcec.PublicKey values. An error is returned if
// val is not a **btcec.PublicKey.
func EPubKey(w io.Writer, val interface{}, _ *[8]byte) error {
if pk, ok := val.(**btcec.PublicKey); ok {
_, err := w.Write((*pk).SerializeCompressed())
return err
}
return NewTypeForEncodingErr(val, "*btcec.PublicKey")
}
// DPubKey is a Decoder for *btcec.PublicKey values. An error is returned if val
// is not a **btcec.PublicKey.
func DPubKey(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if pk, ok := val.(**btcec.PublicKey); ok && l == 33 {
var b [33]byte
_, err := io.ReadFull(r, b[:])
if err != nil {
return err
}
p, err := btcec.ParsePubKey(b[:])
if err != nil {
return err
}
*pk = p
return nil
}
return NewTypeForDecodingErr(val, "*btcec.PublicKey", l, 33)
}
// EVarBytes is an Encoder for variable byte slices. An error is returned if val
// is not *[]byte.
func EVarBytes(w io.Writer, val interface{}, _ *[8]byte) error {
if b, ok := val.(*[]byte); ok {
_, err := w.Write(*b)
return err
}
return NewTypeForEncodingErr(val, "[]byte")
}
// DVarBytes is a Decoder for variable byte slices. An error is returned if val
// is not *[]byte.
func DVarBytes(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if b, ok := val.(*[]byte); ok {
*b = make([]byte, l)
_, err := io.ReadFull(r, *b)
return err
}
return NewTypeForDecodingErr(val, "[]byte", l, l)
}
// EBigSize encodes an uint32 or an uint64 using BigSize format. An error is
// returned if val is not either *uint32 or *uint64.
func EBigSize(w io.Writer, val interface{}, buf *[8]byte) error {
if i, ok := val.(*uint32); ok {
return WriteVarInt(w, uint64(*i), buf)
}
if i, ok := val.(*uint64); ok {
return WriteVarInt(w, uint64(*i), buf)
}
return NewTypeForEncodingErr(val, "BigSize")
}
// DBigSize decodes an uint32 or an uint64 using BigSize format. An error is
// returned if val is not either *uint32 or *uint64.
func DBigSize(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
if i, ok := val.(*uint32); ok {
v, err := ReadVarInt(r, buf)
if err != nil {
return err
}
*i = uint32(v)
return nil
}
if i, ok := val.(*uint64); ok {
v, err := ReadVarInt(r, buf)
if err != nil {
return err
}
*i = v
return nil
}
return NewTypeForDecodingErr(val, "BigSize", l, 8)
}
// SizeBigSize returns a SizeFunc that can compute the length of BigSize.
func SizeBigSize(val interface{}) SizeFunc {
var size uint64
if i, ok := val.(*uint32); ok {
size = VarIntSize(uint64(*i))
}
if i, ok := val.(*uint64); ok {
size = VarIntSize(uint64(*i))
}
return func() uint64 {
return size
}
}