@btc-vision/transaction 1.2.15 → 1.3.1

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.
@@ -29,39 +29,39 @@ export class BinaryWriter {
29
29
  this.buffer.setUint8(this.currentOffset++, value);
30
30
  }
31
31
 
32
- public writeU16(value: u16): void {
32
+ public writeU16(value: u16, be: boolean = true): void {
33
33
  if (value > 65535) throw new Error('u16 value is too large.');
34
34
 
35
35
  this.allocSafe(U16_BYTE_LENGTH);
36
- this.buffer.setUint16(this.currentOffset, value, true);
36
+ this.buffer.setUint16(this.currentOffset, value, !be);
37
37
  this.currentOffset += 2;
38
38
  }
39
39
 
40
- public writeU32(value: u32, le: boolean = true): void {
40
+ public writeU32(value: u32, be: boolean = true): void {
41
41
  if (value > 4294967295) throw new Error('u32 value is too large.');
42
42
 
43
43
  this.allocSafe(U32_BYTE_LENGTH);
44
- this.buffer.setUint32(this.currentOffset, value, le);
44
+ this.buffer.setUint32(this.currentOffset, value, !be);
45
45
  this.currentOffset += 4;
46
46
  }
47
47
 
48
- public writeU64(value: u64): void {
48
+ public writeU64(value: u64, be: boolean = true): void {
49
49
  if (value > 18446744073709551615n) throw new Error('u64 value is too large.');
50
50
 
51
51
  this.allocSafe(U64_BYTE_LENGTH);
52
- this.buffer.setBigUint64(this.currentOffset, value, true);
52
+ this.buffer.setBigUint64(this.currentOffset, value, !be);
53
53
  this.currentOffset += 8;
54
54
  }
55
55
 
56
56
  public writeSelector(value: Selector): void {
57
- this.writeU32(value, false);
57
+ this.writeU32(value, true);
58
58
  }
59
59
 
60
60
  public writeBoolean(value: boolean): void {
61
61
  this.writeU8(value ? 1 : 0);
62
62
  }
63
63
 
64
- public writeI128(bigIntValue: bigint): void {
64
+ public writeI128(bigIntValue: bigint, be: boolean = true): void {
65
65
  if (
66
66
  bigIntValue > 170141183460469231731687303715884105727n ||
67
67
  bigIntValue < -170141183460469231731687303715884105728n
@@ -76,17 +76,24 @@ export class BinaryWriter {
76
76
  throw new Error(`Invalid i128 value: ${bigIntValue}`);
77
77
  }
78
78
 
79
- for (let i = 0; i < bytesToHex.byteLength; i++) {
80
- this.writeU8(bytesToHex[i]);
79
+ if (be) {
80
+ for (let i = 0; i < bytesToHex.byteLength; i++) {
81
+ this.writeU8(bytesToHex[i]);
82
+ }
83
+ } else {
84
+ for (let i = bytesToHex.byteLength - 1; i >= 0; i--) {
85
+ this.writeU8(bytesToHex[i]);
86
+ }
81
87
  }
82
88
  }
83
89
 
84
- public writeU256(bigIntValue: bigint): void {
90
+ public writeU256(bigIntValue: bigint, be: boolean = true): void {
85
91
  if (
86
92
  bigIntValue >
87
- 115792089237316195423570985008687907853269984665640564039457584007913129639935n
93
+ 115792089237316195423570985008687907853269984665640564039457584007913129639935n &&
94
+ bigIntValue < 0n
88
95
  ) {
89
- throw new Error('u256 value is too large.');
96
+ throw new Error('u256 value is too large or negative.');
90
97
  }
91
98
 
92
99
  this.allocSafe(U256_BYTE_LENGTH);
@@ -96,14 +103,20 @@ export class BinaryWriter {
96
103
  throw new Error(`Invalid u256 value: ${bigIntValue}`);
97
104
  }
98
105
 
99
- for (let i = 0; i < bytesToHex.byteLength; i++) {
100
- this.writeU8(bytesToHex[i]);
106
+ if (be) {
107
+ for (let i = 0; i < bytesToHex.byteLength; i++) {
108
+ this.writeU8(bytesToHex[i]);
109
+ }
110
+ } else {
111
+ for (let i = bytesToHex.byteLength - 1; i >= 0; i--) {
112
+ this.writeU8(bytesToHex[i]);
113
+ }
101
114
  }
102
115
  }
103
116
 
104
- public writeU128(bigIntValue: bigint): void {
105
- if (bigIntValue > 340282366920938463463374607431768211455n) {
106
- throw new Error('u128 value is too large.');
117
+ public writeU128(bigIntValue: bigint, be: boolean = true): void {
118
+ if (bigIntValue > 340282366920938463463374607431768211455n && bigIntValue < 0n) {
119
+ throw new Error('u128 value is too large or negative.');
107
120
  }
108
121
 
109
122
  this.allocSafe(U128_BYTE_LENGTH);
@@ -113,8 +126,14 @@ export class BinaryWriter {
113
126
  throw new Error(`Invalid u128 value: ${bigIntValue}`);
114
127
  }
115
128
 
116
- for (let i = 0; i < bytesToHex.byteLength; i++) {
117
- this.writeU8(bytesToHex[i]);
129
+ if (be) {
130
+ for (let i = 0; i < bytesToHex.byteLength; i++) {
131
+ this.writeU8(bytesToHex[i]);
132
+ }
133
+ } else {
134
+ for (let i = bytesToHex.byteLength - 1; i >= 0; i--) {
135
+ this.writeU8(bytesToHex[i]);
136
+ }
118
137
  }
119
138
  }
120
139
 
@@ -186,15 +205,10 @@ export class BinaryWriter {
186
205
  }
187
206
  }
188
207
 
189
- public writeABISelector(name: string, selector: Selector): void {
190
- this.writeStringWithLength(name);
191
- this.writeSelector(selector);
192
- }
193
-
194
- public writeAddressValueTuple(map: AddressMap<bigint>): void {
208
+ public writeAddressValueTuple(map: AddressMap<bigint>, be: boolean = true): void {
195
209
  if (map.size > 65535) throw new Error('Map size is too large');
196
210
 
197
- this.writeU16(map.size);
211
+ this.writeU16(map.size, be);
198
212
 
199
213
  const keys = Array.from(map.keys());
200
214
  for (let i = 0; i < keys.length; i++) {
@@ -204,29 +218,7 @@ export class BinaryWriter {
204
218
  if (value === null || value === undefined) throw new Error('Value not found');
205
219
 
206
220
  this.writeAddress(key);
207
- this.writeU256(value);
208
- }
209
- }
210
-
211
- public writeLimitedAddressBytesMap(map: AddressMap<Uint8Array[]>): void {
212
- if (map.size > 8) throw new Error('Too many contract calls');
213
-
214
- this.writeU8(map.size);
215
-
216
- const keys: Address[] = Array.from(map.keys());
217
- for (let i: i32 = 0; i < keys.length; i++) {
218
- const address: Address = keys[i];
219
- const calls: Uint8Array[] | undefined = map.get(address);
220
-
221
- if (!calls) throw new Error('Calls not found');
222
- if (calls.length > 10) throw new Error('Too many calls.');
223
-
224
- this.writeAddress(address);
225
- this.writeU8(calls.length);
226
-
227
- for (let j: i32 = 0; j < calls.length; j++) {
228
- this.writeBytesWithLength(calls[j]);
229
- }
221
+ this.writeU256(value, be);
230
222
  }
231
223
  }
232
224
 
@@ -245,32 +237,32 @@ export class BinaryWriter {
245
237
  }
246
238
  }
247
239
 
248
- public writeU32Array(value: u32[]): void {
240
+ public writeU32Array(value: u32[], be: boolean = true): void {
249
241
  if (value.length > 65535) throw new Error('Array size is too large');
250
242
 
251
- this.writeU16(value.length);
243
+ this.writeU16(value.length, be);
252
244
 
253
245
  for (let i = 0; i < value.length; i++) {
254
- this.writeU32(value[i]);
246
+ this.writeU32(value[i], be);
255
247
  }
256
248
  }
257
249
 
258
- public writeU256Array(value: bigint[]): void {
250
+ public writeU256Array(value: bigint[], be: boolean = true): void {
259
251
  if (value.length > 65535) throw new Error('Array size is too large');
260
252
 
261
- this.writeU16(value.length);
253
+ this.writeU16(value.length, be);
262
254
 
263
255
  for (let i = 0; i < value.length; i++) {
264
- this.writeU256(value[i]);
256
+ this.writeU256(value[i], be);
265
257
  }
266
258
  }
267
259
 
268
- public writeU128Array(value: bigint[]): void {
260
+ public writeU128Array(value: bigint[], be: boolean = true): void {
269
261
  if (value.length > 65535) throw new Error('Array size is too large');
270
262
 
271
- this.writeU16(value.length);
263
+ this.writeU16(value.length, be);
272
264
  for (let i = 0; i < value.length; i++) {
273
- this.writeU128(value[i]);
265
+ this.writeU128(value[i], be);
274
266
  }
275
267
  }
276
268
 
@@ -284,13 +276,13 @@ export class BinaryWriter {
284
276
  }
285
277
  }
286
278
 
287
- public writeU16Array(value: u16[]): void {
279
+ public writeU16Array(value: u16[], be: boolean = true): void {
288
280
  if (value.length > 65535) throw new Error('Array size is too large');
289
281
 
290
- this.writeU16(value.length);
282
+ this.writeU16(value.length, be);
291
283
 
292
284
  for (let i = 0; i < value.length; i++) {
293
- this.writeU16(value[i]);
285
+ this.writeU16(value[i], be);
294
286
  }
295
287
  }
296
288
 
@@ -304,13 +296,13 @@ export class BinaryWriter {
304
296
  }
305
297
  }
306
298
 
307
- public writeU64Array(value: bigint[]): void {
299
+ public writeU64Array(value: bigint[], be: boolean = true): void {
308
300
  if (value.length > 65535) throw new Error('Array size is too large');
309
301
 
310
- this.writeU16(value.length);
302
+ this.writeU16(value.length, be);
311
303
 
312
304
  for (let i = 0; i < value.length; i++) {
313
- this.writeU64(value[i]);
305
+ this.writeU64(value[i], be);
314
306
  }
315
307
  }
316
308
 
@@ -324,33 +316,6 @@ export class BinaryWriter {
324
316
  }
325
317
  }
326
318
 
327
- public writeSelectorArray(value: Selector[]): void {
328
- if (value.length > 65535) throw new Error('Array size is too large');
329
-
330
- this.writeU16(value.length);
331
-
332
- for (let i = 0; i < value.length; i++) {
333
- this.writeSelector(value[i]);
334
- }
335
- }
336
-
337
- private getChecksum(): u32 {
338
- let checksum: u32 = 0;
339
- for (let i = 0; i < this.buffer.byteLength; i++) {
340
- checksum += this.buffer.getUint8(i);
341
- }
342
-
343
- return checksum % 2 ** 32;
344
- }
345
-
346
- private writeMethodSelectorMap(value: Set<Selector>): void {
347
- this.writeU16(value.size);
348
-
349
- value.forEach((selector: Selector, _value: Selector, _set: Set<Selector>): void => {
350
- this.writeSelector(selector);
351
- });
352
- }
353
-
354
319
  private verifyAddress(pubKey: Address): void {
355
320
  if (pubKey.byteLength > ADDRESS_BYTE_LENGTH) {
356
321
  throw new Error(
@@ -361,7 +326,6 @@ export class BinaryWriter {
361
326
 
362
327
  private resize(size: u32): void {
363
328
  const buf: Uint8Array = new Uint8Array(this.buffer.byteLength + size);
364
-
365
329
  for (let i: i32 = 0; i < this.buffer.byteLength; i++) {
366
330
  buf[i] = this.buffer.getUint8(i);
367
331
  }
@@ -54,6 +54,10 @@ export class Address extends Uint8Array {
54
54
  );
55
55
  }
56
56
 
57
+ public static zero(): Address {
58
+ return new Address();
59
+ }
60
+
57
61
  /**
58
62
  * Create an address from a hex string
59
63
  * @param {string} pubKey The public key