@bsv/sdk 1.10.2 → 1.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/primitives/ReaderUint8Array.js +180 -0
- package/dist/cjs/src/primitives/ReaderUint8Array.js.map +1 -0
- package/dist/cjs/src/primitives/WriterUint8Array.js +173 -0
- package/dist/cjs/src/primitives/WriterUint8Array.js.map +1 -0
- package/dist/cjs/src/primitives/utils.js +20 -2
- package/dist/cjs/src/primitives/utils.js.map +1 -1
- package/dist/cjs/src/transaction/Beef.js +85 -27
- package/dist/cjs/src/transaction/Beef.js.map +1 -1
- package/dist/cjs/src/transaction/BeefTx.js +32 -14
- package/dist/cjs/src/transaction/BeefTx.js.map +1 -1
- package/dist/cjs/src/transaction/MerklePath.js +25 -6
- package/dist/cjs/src/transaction/MerklePath.js.map +1 -1
- package/dist/cjs/src/transaction/Transaction.js +77 -26
- package/dist/cjs/src/transaction/Transaction.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/primitives/ReaderUint8Array.js +176 -0
- package/dist/esm/src/primitives/ReaderUint8Array.js.map +1 -0
- package/dist/esm/src/primitives/WriterUint8Array.js +169 -0
- package/dist/esm/src/primitives/WriterUint8Array.js.map +1 -0
- package/dist/esm/src/primitives/utils.js +18 -1
- package/dist/esm/src/primitives/utils.js.map +1 -1
- package/dist/esm/src/transaction/Beef.js +86 -28
- package/dist/esm/src/transaction/Beef.js.map +1 -1
- package/dist/esm/src/transaction/BeefTx.js +33 -15
- package/dist/esm/src/transaction/BeefTx.js.map +1 -1
- package/dist/esm/src/transaction/MerklePath.js +26 -7
- package/dist/esm/src/transaction/MerklePath.js.map +1 -1
- package/dist/esm/src/transaction/Transaction.js +78 -27
- package/dist/esm/src/transaction/Transaction.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/primitives/ReaderUint8Array.d.ts +32 -0
- package/dist/types/src/primitives/ReaderUint8Array.d.ts.map +1 -0
- package/dist/types/src/primitives/WriterUint8Array.d.ts +54 -0
- package/dist/types/src/primitives/WriterUint8Array.d.ts.map +1 -0
- package/dist/types/src/primitives/utils.d.ts +15 -3
- package/dist/types/src/primitives/utils.d.ts.map +1 -1
- package/dist/types/src/transaction/Beef.d.ts +24 -7
- package/dist/types/src/transaction/Beef.d.ts.map +1 -1
- package/dist/types/src/transaction/BeefTx.d.ts +13 -6
- package/dist/types/src/transaction/BeefTx.d.ts.map +1 -1
- package/dist/types/src/transaction/MerklePath.d.ts +16 -3
- package/dist/types/src/transaction/MerklePath.d.ts.map +1 -1
- package/dist/types/src/transaction/Transaction.d.ts +44 -7
- package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +3 -3
- package/dist/umd/bundle.js.map +1 -1
- package/docs/reference/primitives.md +167 -29
- package/docs/reference/script.md +1 -1
- package/docs/reference/transaction.md +177 -34
- package/package.json +1 -1
- package/src/overlay-tools/__tests/SHIPBroadcaster.test.ts +9 -0
- package/src/primitives/ReaderUint8Array.ts +196 -0
- package/src/primitives/WriterUint8Array.ts +195 -0
- package/src/primitives/__tests/ReaderUint8Array.test.ts +317 -0
- package/src/primitives/__tests/WriterUint8Array.test.ts +208 -0
- package/src/primitives/utils.ts +20 -2
- package/src/transaction/Beef.ts +103 -40
- package/src/transaction/BeefTx.ts +38 -19
- package/src/transaction/MerklePath.ts +30 -9
- package/src/transaction/Transaction.ts +91 -38
- package/src/transaction/__tests/Beef.test.ts +75 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import BigNumber from '../BigNumber'
|
|
2
|
+
import { ReaderUint8Array, Writer, toHex } from '../utils'
|
|
3
|
+
|
|
4
|
+
describe('ReaderUint8Array', () => {
|
|
5
|
+
it('should make a new Br', () => {
|
|
6
|
+
let br = new ReaderUint8Array()
|
|
7
|
+
expect(br).toBeDefined()
|
|
8
|
+
br = new ReaderUint8Array()
|
|
9
|
+
expect(br).toBeDefined()
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('should create a new ReaderUint8Array with an Array', () => {
|
|
13
|
+
const arr: number[] = []
|
|
14
|
+
const br = new ReaderUint8Array(arr)
|
|
15
|
+
expect(br).toBeDefined()
|
|
16
|
+
expect(br.bin instanceof Uint8Array).toBeTruthy()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
describe('#eof', () => {
|
|
20
|
+
it('should return true for a blank br', () => {
|
|
21
|
+
const br = new ReaderUint8Array([...Buffer.from([])])
|
|
22
|
+
expect(br.eof()).toBeTruthy()
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('#read', () => {
|
|
27
|
+
it('should return the same buffer', () => {
|
|
28
|
+
const buf = Buffer.from([0])
|
|
29
|
+
const br = new ReaderUint8Array([...buf])
|
|
30
|
+
expect(Buffer.from(br.read()).toString('hex')).toEqual(
|
|
31
|
+
buf.toString('hex')
|
|
32
|
+
)
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should return a buffer of this length', () => {
|
|
37
|
+
const buf = Buffer.alloc(10)
|
|
38
|
+
buf.fill(0)
|
|
39
|
+
const br = new ReaderUint8Array([...buf])
|
|
40
|
+
const buf2 = br.read(2)
|
|
41
|
+
expect(buf2.length).toEqual(2)
|
|
42
|
+
expect(br.eof()).toBeFalsy()
|
|
43
|
+
expect(br.pos).toEqual(2)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should be able to read 0 bytes', () => {
|
|
47
|
+
const buf = Buffer.from('0101', 'hex')
|
|
48
|
+
expect(new ReaderUint8Array([...buf]).read(0).length).toEqual(0)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
describe('#readReverse', () => {
|
|
53
|
+
it('should reverse this [0, 1]', () => {
|
|
54
|
+
const buf = Buffer.from([0, 1])
|
|
55
|
+
const br = new ReaderUint8Array([...buf])
|
|
56
|
+
expect(toHex(br.readReverse())).toEqual('0100')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('should be able to read 0 bytes', () => {
|
|
60
|
+
const buf = Buffer.from('0101', 'hex')
|
|
61
|
+
expect(new ReaderUint8Array([...buf]).readReverse(0).length).toEqual(0)
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
describe('#readUInt8', () => {
|
|
66
|
+
it('should return 1', () => {
|
|
67
|
+
const buf = Buffer.alloc(1)
|
|
68
|
+
buf.writeUInt8(1, 0)
|
|
69
|
+
const br = new ReaderUint8Array([...buf])
|
|
70
|
+
expect(br.readUInt8()).toEqual(1)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
describe('#readInt8', () => {
|
|
75
|
+
it('should return 1', () => {
|
|
76
|
+
const buf = Buffer.alloc(1)
|
|
77
|
+
buf.writeInt8(1, 0)
|
|
78
|
+
const br = new ReaderUint8Array([...buf])
|
|
79
|
+
expect(br.readInt8()).toEqual(1)
|
|
80
|
+
const bufNegative = Buffer.from('ff', 'hex')
|
|
81
|
+
const brNegative = new ReaderUint8Array([...bufNegative])
|
|
82
|
+
expect(brNegative.readInt8()).toEqual(-1)
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
describe('#readUInt16BE', () => {
|
|
87
|
+
it('should return 1', () => {
|
|
88
|
+
const buf = Buffer.alloc(2)
|
|
89
|
+
buf.writeUInt16BE(1, 0)
|
|
90
|
+
const br = new ReaderUint8Array([...buf])
|
|
91
|
+
expect(br.readUInt16BE()).toEqual(1)
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('#readInt16BE', () => {
|
|
96
|
+
it('should return 1', () => {
|
|
97
|
+
const buf = Buffer.alloc(2)
|
|
98
|
+
buf.writeInt16BE(1, 0)
|
|
99
|
+
const br = new ReaderUint8Array([...buf])
|
|
100
|
+
expect(br.readInt16BE()).toEqual(1)
|
|
101
|
+
const bufNegative = Buffer.from('ffff', 'hex')
|
|
102
|
+
const brNegative = new ReaderUint8Array([...bufNegative])
|
|
103
|
+
expect(brNegative.readInt8()).toEqual(-1)
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
describe('#readUInt16LE', () => {
|
|
108
|
+
it('should return 1', () => {
|
|
109
|
+
const buf = Buffer.alloc(2)
|
|
110
|
+
buf.writeUInt16LE(1, 0)
|
|
111
|
+
const br = new ReaderUint8Array([...buf])
|
|
112
|
+
expect(br.readUInt16LE()).toEqual(1)
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
describe('#readInt16LE', () => {
|
|
117
|
+
it('should return 1', () => {
|
|
118
|
+
const buf = Buffer.alloc(2)
|
|
119
|
+
buf.writeInt16LE(1, 0)
|
|
120
|
+
const br = new ReaderUint8Array([...buf])
|
|
121
|
+
expect(br.readInt16LE()).toEqual(1)
|
|
122
|
+
const bufNegative = Buffer.from('ffff', 'hex')
|
|
123
|
+
const brNegative = new ReaderUint8Array([...bufNegative])
|
|
124
|
+
expect(brNegative.readInt8()).toEqual(-1)
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
describe('#readUInt32BE', () => {
|
|
129
|
+
it('should return 1', () => {
|
|
130
|
+
const buf = Buffer.alloc(4)
|
|
131
|
+
buf.writeUInt32BE(1, 0)
|
|
132
|
+
const br = new ReaderUint8Array([...buf])
|
|
133
|
+
expect(br.readUInt32BE()).toEqual(1)
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
describe('#readInt32BE', () => {
|
|
138
|
+
it('should return 1', () => {
|
|
139
|
+
const buf = Buffer.alloc(4)
|
|
140
|
+
buf.writeInt32BE(1, 0)
|
|
141
|
+
const br = new ReaderUint8Array([...buf])
|
|
142
|
+
expect(br.readInt32BE()).toEqual(1)
|
|
143
|
+
const bufNegative = Buffer.from('ffffffff', 'hex')
|
|
144
|
+
const brNegative = new ReaderUint8Array([...bufNegative])
|
|
145
|
+
expect(brNegative.readInt8()).toEqual(-1)
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
describe('#readUInt32LE', () => {
|
|
150
|
+
it('should return 1', () => {
|
|
151
|
+
const buf = Buffer.alloc(4)
|
|
152
|
+
buf.writeUInt32LE(1, 0)
|
|
153
|
+
const br = new ReaderUint8Array([...buf])
|
|
154
|
+
expect(br.readUInt32LE()).toEqual(1)
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
describe('#readInt32LE', () => {
|
|
159
|
+
it('should return 1', () => {
|
|
160
|
+
const buf = Buffer.alloc(4)
|
|
161
|
+
buf.writeInt32LE(1, 0)
|
|
162
|
+
const br = new ReaderUint8Array([...buf])
|
|
163
|
+
expect(br.readInt32LE()).toEqual(1)
|
|
164
|
+
expect(
|
|
165
|
+
new ReaderUint8Array([...Buffer.from('ffffffff', 'hex')]).readInt32LE()
|
|
166
|
+
).toEqual(-1)
|
|
167
|
+
})
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
describe('#readUInt64BEBn', () => {
|
|
171
|
+
it('should return 1', () => {
|
|
172
|
+
const buf = Buffer.alloc(8)
|
|
173
|
+
buf.fill(0)
|
|
174
|
+
buf.writeUInt32BE(1, 4)
|
|
175
|
+
const br = new ReaderUint8Array([...buf])
|
|
176
|
+
expect(br.readUInt64BEBn().toNumber()).toEqual(1)
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('should return 2^64', () => {
|
|
180
|
+
const buf = Buffer.alloc(8)
|
|
181
|
+
buf.fill(0xff)
|
|
182
|
+
const br = new ReaderUint8Array([...buf])
|
|
183
|
+
expect(br.readUInt64BEBn().toHex()).toEqual('ffffffffffffffff')
|
|
184
|
+
})
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
describe('#readUInt64LEBn', () => {
|
|
188
|
+
it('should return 1', () => {
|
|
189
|
+
const buf = Buffer.alloc(8)
|
|
190
|
+
buf.fill(0)
|
|
191
|
+
buf.writeUInt32LE(1, 0)
|
|
192
|
+
const br = new ReaderUint8Array([...buf])
|
|
193
|
+
expect(br.readUInt64LEBn().toNumber()).toEqual(1)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it('should return 2^30', () => {
|
|
197
|
+
const buf = Buffer.alloc(8)
|
|
198
|
+
buf.fill(0)
|
|
199
|
+
buf.writeUInt32LE(Math.pow(2, 30), 0)
|
|
200
|
+
const br = new ReaderUint8Array([...buf])
|
|
201
|
+
expect(br.readUInt64LEBn().toNumber()).toEqual(Math.pow(2, 30))
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('should return 0', () => {
|
|
205
|
+
const buf = Buffer.alloc(8)
|
|
206
|
+
buf.fill(0)
|
|
207
|
+
const br = new ReaderUint8Array([...buf])
|
|
208
|
+
expect(br.readUInt64LEBn().toNumber()).toEqual(0)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
it('should return 2^64', () => {
|
|
212
|
+
const buf = Buffer.alloc(8)
|
|
213
|
+
buf.fill(0xff)
|
|
214
|
+
const br = new ReaderUint8Array([...buf])
|
|
215
|
+
expect(br.readUInt64LEBn().toHex()).toEqual('ffffffffffffffff')
|
|
216
|
+
})
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
describe('#readVarInt', () => {
|
|
220
|
+
it('should read a 1 byte varInt', () => {
|
|
221
|
+
const buf = Buffer.from([50])
|
|
222
|
+
const br = new ReaderUint8Array([...buf])
|
|
223
|
+
expect(br.readVarInt().length).toEqual(1)
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
it('should read a 3 byte varInt', () => {
|
|
227
|
+
const buf = Buffer.from([253, 253, 0])
|
|
228
|
+
const br = new ReaderUint8Array([...buf])
|
|
229
|
+
expect(br.readVarInt().length).toEqual(3)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it('should read a 5 byte varInt', () => {
|
|
233
|
+
const buf = Buffer.from([254, 0, 0, 0, 0])
|
|
234
|
+
buf.writeUInt32LE(50000, 1)
|
|
235
|
+
const br = new ReaderUint8Array([...buf])
|
|
236
|
+
expect(br.readVarInt().length).toEqual(5)
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
it('should read a 9 byte varInt', () => {
|
|
240
|
+
const buf = new Writer()
|
|
241
|
+
.writeVarIntBn(new BigNumber(Math.pow(2, 54).toString()))
|
|
242
|
+
.toArray()
|
|
243
|
+
const br = new ReaderUint8Array([...buf])
|
|
244
|
+
expect(br.readVarInt().length).toEqual(9)
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
describe('#readVarIntNum', () => {
|
|
249
|
+
it('should read a 1 byte varInt', () => {
|
|
250
|
+
const buf = Buffer.from([50])
|
|
251
|
+
const br = new ReaderUint8Array([...buf])
|
|
252
|
+
expect(br.readVarIntNum()).toEqual(50)
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('should read a 3 byte varInt', () => {
|
|
256
|
+
const buf = Buffer.from([253, 253, 0])
|
|
257
|
+
const br = new ReaderUint8Array([...buf])
|
|
258
|
+
expect(br.readVarIntNum()).toEqual(253)
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
it('should read a 5 byte varInt', () => {
|
|
262
|
+
const buf = Buffer.from([254, 0, 0, 0, 0])
|
|
263
|
+
buf.writeUInt32LE(50000, 1)
|
|
264
|
+
const br = new ReaderUint8Array([...buf])
|
|
265
|
+
expect(br.readVarIntNum()).toEqual(50000)
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
it('should throw an error on a 9 byte varInt over the javascript uint precision limit', () => {
|
|
269
|
+
const buf = new Writer()
|
|
270
|
+
.writeVarIntBn(new BigNumber(Math.pow(2, 54).toString()))
|
|
271
|
+
.toArray()
|
|
272
|
+
const br = new ReaderUint8Array([...buf])
|
|
273
|
+
expect(() => {
|
|
274
|
+
br.readVarIntNum()
|
|
275
|
+
}).toThrow('number too large to retain precision - use readVarIntBn')
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
it('should not throw an error on a 9 byte varInt not over the javascript uint precision limit', () => {
|
|
279
|
+
const buf = new Writer()
|
|
280
|
+
.writeVarIntBn(new BigNumber(Math.pow(2, 53).toString()))
|
|
281
|
+
.toArray()
|
|
282
|
+
const br = new ReaderUint8Array([...buf])
|
|
283
|
+
expect(() => {
|
|
284
|
+
br.readVarIntNum()
|
|
285
|
+
}).not.toThrow('number too large to retain precision - use readVarIntBn')
|
|
286
|
+
})
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
describe('#readVarIntBn', () => {
|
|
290
|
+
it('should read a 1 byte varInt', () => {
|
|
291
|
+
const buf = Buffer.from([50])
|
|
292
|
+
const br = new ReaderUint8Array([...buf])
|
|
293
|
+
expect(br.readVarIntBn().toNumber()).toEqual(50)
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
it('should read a 3 byte varInt', () => {
|
|
297
|
+
const buf = Buffer.from([253, 253, 0])
|
|
298
|
+
const br = new ReaderUint8Array([...buf])
|
|
299
|
+
expect(br.readVarIntBn().toNumber()).toEqual(253)
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
it('should read a 5 byte varInt', () => {
|
|
303
|
+
const buf = Buffer.from([254, 0, 0, 0, 0])
|
|
304
|
+
buf.writeUInt32LE(50000, 1)
|
|
305
|
+
const br = new ReaderUint8Array([...buf])
|
|
306
|
+
expect(br.readVarIntBn().toNumber()).toEqual(50000)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
it('should read a 9 byte varInt', () => {
|
|
310
|
+
const buf = Buffer.concat([
|
|
311
|
+
Buffer.from([255]),
|
|
312
|
+
Buffer.from('ffffffffffffffff', 'hex')
|
|
313
|
+
])
|
|
314
|
+
const br = new ReaderUint8Array([...buf])
|
|
315
|
+
expect(br.readVarIntBn().toHex()).toEqual('ffffffffffffffff')
|
|
316
|
+
})
|
|
317
|
+
})
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import BigNumber from '../BigNumber'
|
|
2
|
+
import { Reader, encode, WriterUint8Array } from '../utils'
|
|
3
|
+
|
|
4
|
+
describe('WriterUint8Array', () => {
|
|
5
|
+
it('should create a new buffer writer', () => {
|
|
6
|
+
const bw = new WriterUint8Array()
|
|
7
|
+
expect(bw).toBeDefined()
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
describe('#getLength', () => {
|
|
11
|
+
it('should compute length correctly of two 2-byte buffers', () => {
|
|
12
|
+
const buf1 = Buffer.from('0000', 'hex')
|
|
13
|
+
const buf2 = Buffer.from('0000', 'hex')
|
|
14
|
+
const bw = new WriterUint8Array().write([...buf1]).write([...buf2])
|
|
15
|
+
expect(bw.getLength()).toEqual(4)
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
describe('#toArray', () => {
|
|
20
|
+
it('should concat these two bufs', () => {
|
|
21
|
+
const buf1 = [0]
|
|
22
|
+
const buf2 = [1]
|
|
23
|
+
const bw = new WriterUint8Array([buf1, buf2])
|
|
24
|
+
expect(encode(bw.toArray(), 'hex')).toEqual('0001')
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe('#write', () => {
|
|
29
|
+
it('should write a buffer', () => {
|
|
30
|
+
const buf = [0]
|
|
31
|
+
const bw = new WriterUint8Array()
|
|
32
|
+
bw.write(buf)
|
|
33
|
+
expect(encode(bw.toArray(), 'hex')).toEqual('00')
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe('#writeReverse', () => {
|
|
38
|
+
it('should write a buffer in reverse', () => {
|
|
39
|
+
const buf = [0, 1]
|
|
40
|
+
const bw = new WriterUint8Array()
|
|
41
|
+
bw.writeReverse(buf)
|
|
42
|
+
expect(encode(bw.toArray(), 'hex')).toEqual('0100')
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('#writeUInt8', () => {
|
|
47
|
+
it('should write 1', () => {
|
|
48
|
+
const bw = new WriterUint8Array()
|
|
49
|
+
expect(encode(bw.writeUInt8(1).toArray(), 'hex')).toEqual('01')
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('#writeInt8', () => {
|
|
54
|
+
it('should write 1', () => {
|
|
55
|
+
const bw = new WriterUint8Array()
|
|
56
|
+
expect(encode(bw.writeInt8(1).toArray(), 'hex')).toEqual('01')
|
|
57
|
+
expect(encode(new WriterUint8Array().writeInt8(-1).toArray(), 'hex')).toEqual('ff')
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
describe('#writeUInt16BE', () => {
|
|
62
|
+
it('should write 1', () => {
|
|
63
|
+
const bw = new WriterUint8Array()
|
|
64
|
+
expect(encode(bw.writeUInt16BE(1).toArray(), 'hex')).toEqual('0001')
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
describe('#writeInt16BE', () => {
|
|
69
|
+
it('should write 1', () => {
|
|
70
|
+
const bw = new WriterUint8Array()
|
|
71
|
+
expect(encode(bw.writeInt16BE(1).toArray(), 'hex')).toEqual('0001')
|
|
72
|
+
expect(encode(new WriterUint8Array().writeInt16BE(-1).toArray(), 'hex')).toEqual(
|
|
73
|
+
'ffff'
|
|
74
|
+
)
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
describe('#writeUInt16LE', () => {
|
|
79
|
+
it('should write 1', () => {
|
|
80
|
+
const bw = new WriterUint8Array()
|
|
81
|
+
expect(encode(bw.writeUInt16LE(1).toArray(), 'hex')).toEqual('0100')
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
describe('#writeInt16LE', () => {
|
|
86
|
+
it('should write 1', () => {
|
|
87
|
+
const bw = new WriterUint8Array()
|
|
88
|
+
expect(encode(bw.writeInt16LE(1).toArray(), 'hex')).toEqual('0100')
|
|
89
|
+
expect(encode(new WriterUint8Array().writeInt16LE(-1).toArray(), 'hex')).toEqual(
|
|
90
|
+
'ffff'
|
|
91
|
+
)
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('#writeUInt32BE', () => {
|
|
96
|
+
it('should write 1', () => {
|
|
97
|
+
const bw = new WriterUint8Array()
|
|
98
|
+
expect(encode(bw.writeUInt32BE(1).toArray(), 'hex')).toEqual('00000001')
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe('#writeInt32BE', () => {
|
|
103
|
+
it('should write 1', () => {
|
|
104
|
+
const bw = new WriterUint8Array()
|
|
105
|
+
expect(encode(bw.writeInt32BE(1).toArray(), 'hex')).toEqual('00000001')
|
|
106
|
+
expect(encode(new WriterUint8Array().writeInt32BE(-1).toArray(), 'hex')).toEqual(
|
|
107
|
+
'ffffffff'
|
|
108
|
+
)
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
describe('#writeUInt32LE', () => {
|
|
113
|
+
it('should write 1', () => {
|
|
114
|
+
const bw = new WriterUint8Array()
|
|
115
|
+
expect(encode(bw.writeUInt32LE(1).toArray(), 'hex')).toEqual('01000000')
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
describe('#writeInt32LE', () => {
|
|
120
|
+
it('should write 1', () => {
|
|
121
|
+
const bw = new WriterUint8Array()
|
|
122
|
+
expect(encode(bw.writeInt32LE(1).toArray(), 'hex')).toEqual('01000000')
|
|
123
|
+
expect(encode(new WriterUint8Array().writeInt32LE(-1).toArray(), 'hex')).toEqual(
|
|
124
|
+
'ffffffff'
|
|
125
|
+
)
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
describe('#writeUInt64BEBn', () => {
|
|
130
|
+
it('should write 1', () => {
|
|
131
|
+
const bw = new WriterUint8Array()
|
|
132
|
+
expect(
|
|
133
|
+
encode(bw.writeUInt64BEBn(new BigNumber(1)).toArray(), 'hex')
|
|
134
|
+
).toEqual('0000000000000001')
|
|
135
|
+
})
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
describe('#writeUInt64LEBn', () => {
|
|
139
|
+
it('should write 1', () => {
|
|
140
|
+
const bw = new WriterUint8Array()
|
|
141
|
+
expect(
|
|
142
|
+
encode(bw.writeUInt64LEBn(new BigNumber(1)).toArray(), 'hex')
|
|
143
|
+
).toEqual('0100000000000000')
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
describe('#writeVarInt', () => {
|
|
148
|
+
it('should write a 1 byte varInt', () => {
|
|
149
|
+
const bw = new WriterUint8Array()
|
|
150
|
+
bw.writeVarIntNum(1)
|
|
151
|
+
expect(bw.toArray().length).toEqual(1)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('should write a 3 byte varInt', () => {
|
|
155
|
+
const bw = new WriterUint8Array()
|
|
156
|
+
bw.writeVarIntNum(1000)
|
|
157
|
+
expect(bw.toArray().length).toEqual(3)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('should write a 5 byte varInt', () => {
|
|
161
|
+
const bw = new WriterUint8Array()
|
|
162
|
+
bw.writeVarIntNum(Math.pow(2, 16 + 1))
|
|
163
|
+
expect(bw.toArray().length).toEqual(5)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('should write a 9 byte varInt', () => {
|
|
167
|
+
const bw = new WriterUint8Array()
|
|
168
|
+
bw.writeVarIntNum(Math.pow(2, 32 + 1))
|
|
169
|
+
expect(bw.toArray().length).toEqual(9)
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('should read back the same value it wrote for a 9 byte varInt', () => {
|
|
173
|
+
const bw = new WriterUint8Array()
|
|
174
|
+
const n = Math.pow(2, 53)
|
|
175
|
+
expect(n).toEqual(n + 1) // javascript number precision limit
|
|
176
|
+
bw.writeVarIntNum(n)
|
|
177
|
+
const br = new Reader(bw.toArray())
|
|
178
|
+
expect(br.readVarIntBn().toHex()).toEqual('20000000000000')
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
describe('#writeVarIntBn', () => {
|
|
183
|
+
it('should write a 1 byte varInt', () => {
|
|
184
|
+
const bw = new WriterUint8Array()
|
|
185
|
+
bw.writeVarIntBn(new BigNumber(1))
|
|
186
|
+
expect(bw.toArray().length).toEqual(1)
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it('should write a 3 byte varInt', () => {
|
|
190
|
+
const bw = new WriterUint8Array()
|
|
191
|
+
bw.writeVarIntBn(new BigNumber(1000))
|
|
192
|
+
expect(bw.toArray().length).toEqual(3)
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
it('should write a 5 byte varInt', () => {
|
|
196
|
+
const bw = new WriterUint8Array()
|
|
197
|
+
const bn = new BigNumber(Math.pow(2, 16 + 1))
|
|
198
|
+
bw.writeVarIntBn(bn)
|
|
199
|
+
expect(bw.toArray().length).toEqual(5)
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it('should write a 9 byte varInt', () => {
|
|
203
|
+
const bw = new WriterUint8Array()
|
|
204
|
+
bw.writeVarIntBn(new BigNumber(Math.pow(2, 32 + 1)))
|
|
205
|
+
expect(bw.toArray().length).toEqual(9)
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
})
|
package/src/primitives/utils.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import BigNumber from './BigNumber.js'
|
|
2
2
|
import { hash256 } from './Hash.js'
|
|
3
3
|
import { assertValidHex } from './hex.js'
|
|
4
|
+
import { WriterUint8Array } from './WriterUint8Array.js'
|
|
5
|
+
import { ReaderUint8Array } from './ReaderUint8Array.js'
|
|
6
|
+
|
|
7
|
+
export { WriterUint8Array }
|
|
8
|
+
export { ReaderUint8Array }
|
|
4
9
|
|
|
5
10
|
const BufferCtor =
|
|
6
11
|
typeof globalThis !== 'undefined' ? (globalThis as any).Buffer : undefined
|
|
@@ -32,7 +37,7 @@ for (let i = 0; i < 256; i++) {
|
|
|
32
37
|
HEX_DIGITS[(i >> 4) & 0xf] + HEX_DIGITS[i & 0xf]
|
|
33
38
|
}
|
|
34
39
|
|
|
35
|
-
export const toHex = (msg: number[]): string => {
|
|
40
|
+
export const toHex = (msg: number[] | Uint8Array): string => {
|
|
36
41
|
if (CAN_USE_BUFFER) {
|
|
37
42
|
return BufferCtor.from(msg).toString('hex')
|
|
38
43
|
}
|
|
@@ -44,11 +49,24 @@ export const toHex = (msg: number[]): string => {
|
|
|
44
49
|
return out.join('')
|
|
45
50
|
}
|
|
46
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Converts various message formats into a Uint8Array.
|
|
54
|
+
* Supports Uint8Array, arrays of bytes, hexadecimal strings, base64 strings, and UTF-8 strings.
|
|
55
|
+
*
|
|
56
|
+
* @param {any} msg - The input message (Uint8Array, number[], Array or string).
|
|
57
|
+
* @param {('hex' | 'utf8' | 'base64')} enc - Specifies the string encoding, if applicable.
|
|
58
|
+
* @returns {Uint8Array}
|
|
59
|
+
*/
|
|
60
|
+
export const toUint8Array = (msg: any, enc?: 'hex' | 'utf8' | 'base64'): Uint8Array => {
|
|
61
|
+
if (msg instanceof Uint8Array) return msg
|
|
62
|
+
return new Uint8Array(toArray(msg, enc))
|
|
63
|
+
}
|
|
64
|
+
|
|
47
65
|
/**
|
|
48
66
|
* Converts various message formats into an array of numbers.
|
|
49
67
|
* Supports arrays, hexadecimal strings, base64 strings, and UTF-8 strings.
|
|
50
68
|
*
|
|
51
|
-
* @param {any} msg - The input message (
|
|
69
|
+
* @param {any} msg - The input message (Uint8Array, number[], Array or string).
|
|
52
70
|
* @param {('hex' | 'utf8' | 'base64')} enc - Specifies the string encoding, if applicable.
|
|
53
71
|
* @returns {any[]} - Array representation of the input.
|
|
54
72
|
*/
|