@btc-vision/transaction 1.2.14 → 1.3.0

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
 
@@ -163,15 +182,6 @@ export class BinaryWriter {
163
182
  this.buffer = this.getDefaultBuffer(4);
164
183
  }
165
184
 
166
- public writeTuple(values: bigint[]): void {
167
- this.allocSafe(U32_BYTE_LENGTH + values.length * U256_BYTE_LENGTH);
168
- this.writeU32(values.length);
169
-
170
- for (let i = 0; i < values.length; i++) {
171
- this.writeU256(values[i]);
172
- }
173
- }
174
-
175
185
  public toBytesReader(): BinaryReader {
176
186
  return new BinaryReader(this.getBuffer());
177
187
  }
@@ -195,15 +205,10 @@ export class BinaryWriter {
195
205
  }
196
206
  }
197
207
 
198
- public writeABISelector(name: string, selector: Selector): void {
199
- this.writeStringWithLength(name);
200
- this.writeSelector(selector);
201
- }
202
-
203
- public writeAddressValueTupleMap(map: AddressMap<bigint>): void {
208
+ public writeAddressValueTuple(map: AddressMap<bigint>, be: boolean = true): void {
204
209
  if (map.size > 65535) throw new Error('Map size is too large');
205
210
 
206
- this.writeU16(map.size);
211
+ this.writeU16(map.size, be);
207
212
 
208
213
  const keys = Array.from(map.keys());
209
214
  for (let i = 0; i < keys.length; i++) {
@@ -213,29 +218,7 @@ export class BinaryWriter {
213
218
  if (value === null || value === undefined) throw new Error('Value not found');
214
219
 
215
220
  this.writeAddress(key);
216
- this.writeU256(value);
217
- }
218
- }
219
-
220
- public writeLimitedAddressBytesMap(map: AddressMap<Uint8Array[]>): void {
221
- if (map.size > 8) throw new Error('Too many contract calls');
222
-
223
- this.writeU8(map.size);
224
-
225
- const keys: Address[] = Array.from(map.keys());
226
- for (let i: i32 = 0; i < keys.length; i++) {
227
- const address: Address = keys[i];
228
- const calls: Uint8Array[] | undefined = map.get(address);
229
-
230
- if (!calls) throw new Error('Calls not found');
231
- if (calls.length > 10) throw new Error('Too many calls.');
232
-
233
- this.writeAddress(address);
234
- this.writeU8(calls.length);
235
-
236
- for (let j: i32 = 0; j < calls.length; j++) {
237
- this.writeBytesWithLength(calls[j]);
238
- }
221
+ this.writeU256(value, be);
239
222
  }
240
223
  }
241
224
 
@@ -254,32 +237,32 @@ export class BinaryWriter {
254
237
  }
255
238
  }
256
239
 
257
- public writeU32Array(value: u32[]): void {
240
+ public writeU32Array(value: u32[], be: boolean = true): void {
258
241
  if (value.length > 65535) throw new Error('Array size is too large');
259
242
 
260
- this.writeU16(value.length);
243
+ this.writeU16(value.length, be);
261
244
 
262
245
  for (let i = 0; i < value.length; i++) {
263
- this.writeU32(value[i]);
246
+ this.writeU32(value[i], be);
264
247
  }
265
248
  }
266
249
 
267
- public writeU256Array(value: bigint[]): void {
250
+ public writeU256Array(value: bigint[], be: boolean = true): void {
268
251
  if (value.length > 65535) throw new Error('Array size is too large');
269
252
 
270
- this.writeU16(value.length);
253
+ this.writeU16(value.length, be);
271
254
 
272
255
  for (let i = 0; i < value.length; i++) {
273
- this.writeU256(value[i]);
256
+ this.writeU256(value[i], be);
274
257
  }
275
258
  }
276
259
 
277
- public writeU128Array(value: bigint[]): void {
260
+ public writeU128Array(value: bigint[], be: boolean = true): void {
278
261
  if (value.length > 65535) throw new Error('Array size is too large');
279
262
 
280
- this.writeU16(value.length);
263
+ this.writeU16(value.length, be);
281
264
  for (let i = 0; i < value.length; i++) {
282
- this.writeU128(value[i]);
265
+ this.writeU128(value[i], be);
283
266
  }
284
267
  }
285
268
 
@@ -293,13 +276,13 @@ export class BinaryWriter {
293
276
  }
294
277
  }
295
278
 
296
- public writeU16Array(value: u16[]): void {
279
+ public writeU16Array(value: u16[], be: boolean = true): void {
297
280
  if (value.length > 65535) throw new Error('Array size is too large');
298
281
 
299
- this.writeU16(value.length);
282
+ this.writeU16(value.length, be);
300
283
 
301
284
  for (let i = 0; i < value.length; i++) {
302
- this.writeU16(value[i]);
285
+ this.writeU16(value[i], be);
303
286
  }
304
287
  }
305
288
 
@@ -313,13 +296,13 @@ export class BinaryWriter {
313
296
  }
314
297
  }
315
298
 
316
- public writeU64Array(value: bigint[]): void {
299
+ public writeU64Array(value: bigint[], be: boolean = true): void {
317
300
  if (value.length > 65535) throw new Error('Array size is too large');
318
301
 
319
- this.writeU16(value.length);
302
+ this.writeU16(value.length, be);
320
303
 
321
304
  for (let i = 0; i < value.length; i++) {
322
- this.writeU64(value[i]);
305
+ this.writeU64(value[i], be);
323
306
  }
324
307
  }
325
308
 
@@ -333,33 +316,6 @@ export class BinaryWriter {
333
316
  }
334
317
  }
335
318
 
336
- public writeSelectorArray(value: Selector[]): void {
337
- if (value.length > 65535) throw new Error('Array size is too large');
338
-
339
- this.writeU16(value.length);
340
-
341
- for (let i = 0; i < value.length; i++) {
342
- this.writeSelector(value[i]);
343
- }
344
- }
345
-
346
- private getChecksum(): u32 {
347
- let checksum: u32 = 0;
348
- for (let i = 0; i < this.buffer.byteLength; i++) {
349
- checksum += this.buffer.getUint8(i);
350
- }
351
-
352
- return checksum % 2 ** 32;
353
- }
354
-
355
- private writeMethodSelectorMap(value: Set<Selector>): void {
356
- this.writeU16(value.size);
357
-
358
- value.forEach((selector: Selector, _value: Selector, _set: Set<Selector>): void => {
359
- this.writeSelector(selector);
360
- });
361
- }
362
-
363
319
  private verifyAddress(pubKey: Address): void {
364
320
  if (pubKey.byteLength > ADDRESS_BYTE_LENGTH) {
365
321
  throw new Error(
@@ -370,7 +326,6 @@ export class BinaryWriter {
370
326
 
371
327
  private resize(size: u32): void {
372
328
  const buf: Uint8Array = new Uint8Array(this.buffer.byteLength + size);
373
-
374
329
  for (let i: i32 = 0; i < this.buffer.byteLength; i++) {
375
330
  buf[i] = this.buffer.getUint8(i);
376
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
@@ -30,7 +30,7 @@ export abstract class CustomKeypair implements Signer {
30
30
 
31
31
  public abstract signSchnorr(hash: Buffer): Buffer;
32
32
 
33
- public abstract verify(hash: Buffer, signature: Buffer): boolean;
33
+ public abstract verify(hash: Buffer, signature: Buffer): boolean | Buffer;
34
34
 
35
35
  public abstract init(): Promise<void>;
36
36
  }
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  crypto as bitCrypto,
3
- script as bitScript,
4
3
  Network,
5
4
  networks,
6
5
  Psbt,
6
+ script as bitScript,
7
7
  TapScriptSig,
8
8
  toXOnly,
9
9
  } from '@btc-vision/bitcoin';