@btc-vision/transaction 1.7.1 → 1.7.2

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.
@@ -10,11 +10,11 @@ import {
10
10
  U64_BYTE_LENGTH,
11
11
  U8_BYTE_LENGTH,
12
12
  } from '../utils/lengths.js';
13
- import { BufferLike, i32, Selector, u16, u32, u8 } from '../utils/types.js';
13
+ import { BufferLike, Selector, u16, u32, u8 } from '../utils/types.js';
14
14
 
15
15
  export class BinaryReader {
16
16
  private buffer: DataView;
17
- private currentOffset: i32 = 0;
17
+ private currentOffset: number = 0;
18
18
 
19
19
  constructor(bytes: BufferLike) {
20
20
  this.buffer = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
@@ -342,7 +342,7 @@ export class BinaryReader {
342
342
  /**
343
343
  * Verifies we have enough bytes in the buffer to read up to `size`.
344
344
  */
345
- public verifyEnd(size: i32): void {
345
+ public verifyEnd(size: number): void {
346
346
  if (size > this.buffer.byteLength) {
347
347
  throw new Error(
348
348
  `Attempt to read beyond buffer length: requested up to byte offset ${size}, but buffer is only ${this.buffer.byteLength} bytes.`,
@@ -11,7 +11,7 @@ import {
11
11
  U64_BYTE_LENGTH,
12
12
  U8_BYTE_LENGTH,
13
13
  } from '../utils/lengths.js';
14
- import { i32, Selector, u16, u32, u64, u8 } from '../utils/types.js';
14
+ import { Selector, u16, u32, u64, u8 } from '../utils/types.js';
15
15
  import { BinaryReader } from './BinaryReader.js';
16
16
 
17
17
  export class BinaryWriter {
@@ -351,7 +351,7 @@ export class BinaryWriter {
351
351
 
352
352
  private resize(size: u32): void {
353
353
  const buf: Uint8Array = new Uint8Array(this.buffer.byteLength + size);
354
- for (let i: i32 = 0; i < this.buffer.byteLength; i++) {
354
+ for (let i: number = 0; i < this.buffer.byteLength; i++) {
355
355
  buf[i] = this.buffer.getUint8(i);
356
356
  }
357
357
 
@@ -1,63 +1,90 @@
1
- import { i32 } from '../utils/types.js';
2
1
  import { Address } from '../keypair/Address.js';
3
- import { Map } from './Map.js';
4
2
 
5
- export class AddressMap<V> extends Map<Address, V> {
6
- public set(key: Address, value: V): void {
7
- const index: i32 = this.indexOf(key);
8
- if (index == -1) {
9
- this._keys.push(key);
10
- this._values.push(value);
11
- } else {
12
- this._values[index] = value;
3
+ export class AddressMap<V> {
4
+ private items: Map<bigint, V>;
5
+ private keyOrder: Address[];
6
+
7
+ constructor(iterable?: ReadonlyArray<readonly [Address, V]> | null) {
8
+ this.items = new Map();
9
+ this.keyOrder = [];
10
+
11
+ if (iterable) {
12
+ for (const [key, value] of iterable) {
13
+ this.set(key, value);
14
+ }
13
15
  }
14
16
  }
15
17
 
16
- public indexOf(address: Address): i32 {
17
- for (let i: i32 = 0; i < this._keys.length; i++) {
18
- const key = this._keys[i];
18
+ get size(): number {
19
+ return this.keyOrder.length;
20
+ }
19
21
 
20
- if (address.equals(key)) {
21
- return i;
22
- }
22
+ public set(key: Address, value: V): void {
23
+ const keyBigInt = key.toBigInt();
24
+ if (!this.items.has(keyBigInt)) {
25
+ this.keyOrder.push(key);
23
26
  }
27
+ this.items.set(keyBigInt, value);
28
+ }
24
29
 
25
- return -1;
30
+ public get(key: Address): V | undefined {
31
+ return this.items.get(key.toBigInt());
26
32
  }
27
33
 
28
34
  public has(key: Address): boolean {
29
- for (let i: i32 = 0; i < this._keys.length; i++) {
30
- if (key.equals(this._keys[i])) {
31
- return true;
32
- }
33
- }
35
+ return this.items.has(key.toBigInt());
36
+ }
34
37
 
38
+ public delete(key: Address): boolean {
39
+ const keyBigInt = key.toBigInt();
40
+ if (this.items.delete(keyBigInt)) {
41
+ this.keyOrder = this.keyOrder.filter((k) => k.toBigInt() !== keyBigInt);
42
+ return true;
43
+ }
35
44
  return false;
36
45
  }
37
46
 
38
- public get(key: Address): V | undefined {
39
- const index: i32 = this.indexOf(key);
40
- if (index == -1) {
41
- return;
47
+ public clear(): void {
48
+ this.items.clear();
49
+ this.keyOrder = [];
50
+ }
51
+
52
+ public indexOf(address: Address): number {
53
+ const addressBigInt = address.toBigInt();
54
+ for (let i: number = 0; i < this.keyOrder.length; i++) {
55
+ if (this.keyOrder[i].toBigInt() === addressBigInt) {
56
+ return i;
57
+ }
42
58
  }
43
- return this._values[index];
59
+ return -1;
44
60
  }
45
61
 
46
- public delete(key: Address): boolean {
47
- const index: i32 = this.indexOf(key);
48
- if (index == -1) {
49
- return false;
62
+ *entries(): IterableIterator<[Address, V]> {
63
+ for (const key of this.keyOrder) {
64
+ yield [key, this.items.get(key.toBigInt()) as V];
50
65
  }
66
+ }
51
67
 
52
- this._keys.splice(index, 1);
53
- this._values.splice(index, 1);
68
+ *keys(): IterableIterator<Address> {
69
+ yield* this.keyOrder;
70
+ }
54
71
 
55
- return true;
72
+ *values(): IterableIterator<V> {
73
+ for (const key of this.keyOrder) {
74
+ yield this.items.get(key.toBigInt()) as V;
75
+ }
56
76
  }
57
77
 
58
- *[Symbol.iterator](): IterableIterator<[Address, V]> {
59
- for (const key of this._keys) {
60
- yield [key, this.get(key) as V];
78
+ forEach(
79
+ callback: (value: V, key: Address, map: AddressMap<V>) => void,
80
+ thisArg?: unknown,
81
+ ): void {
82
+ for (const key of this.keyOrder) {
83
+ callback.call(thisArg, this.items.get(key.toBigInt()) as V, key, this);
61
84
  }
62
85
  }
86
+
87
+ [Symbol.iterator](): IterableIterator<[Address, V]> {
88
+ return this.entries();
89
+ }
63
90
  }
@@ -1,10 +1,16 @@
1
1
  import { Address } from '../keypair/Address.js';
2
2
 
3
3
  export class AddressSet {
4
+ private items: Set<bigint>;
4
5
  private keys: Address[];
5
6
 
6
7
  public constructor(keys: Address[] = []) {
7
- this.keys = keys;
8
+ this.items = new Set();
9
+ this.keys = [];
10
+
11
+ for (const key of keys) {
12
+ this.add(key);
13
+ }
8
14
  }
9
15
 
10
16
  public get size(): number {
@@ -12,56 +18,44 @@ export class AddressSet {
12
18
  }
13
19
 
14
20
  public add(address: Address): void {
15
- if (!this.has(address)) {
21
+ const addressBigInt = address.toBigInt();
22
+ if (!this.items.has(addressBigInt)) {
23
+ this.items.add(addressBigInt);
16
24
  this.keys.push(address);
17
25
  }
18
26
  }
19
27
 
20
28
  public has(address: Address): boolean {
21
- for (let i = 0; i < this.keys.length; i++) {
22
- if (this.keys[i].equals(address)) {
23
- return true;
24
- }
25
- }
26
-
27
- return false;
29
+ return this.items.has(address.toBigInt());
28
30
  }
29
31
 
30
32
  public remove(address: Address): void {
31
- const index = this.keys.findIndex((key) => key.equals(address));
32
-
33
- if (index !== -1) {
34
- this.keys.splice(index, 1);
33
+ const addressBigInt = address.toBigInt();
34
+ if (this.items.delete(addressBigInt)) {
35
+ this.keys = this.keys.filter((k) => k.toBigInt() !== addressBigInt);
35
36
  }
36
37
  }
37
38
 
38
39
  public clone(): AddressSet {
39
- const clone = new AddressSet();
40
-
41
- for (let i = 0; i < this.keys.length; i++) {
42
- clone.add(this.keys[i]);
43
- }
44
-
45
- return clone;
40
+ return new AddressSet(this.keys);
46
41
  }
47
42
 
48
43
  public clear(): void {
44
+ this.items.clear();
49
45
  this.keys = [];
50
46
  }
51
47
 
52
48
  public combine(set: AddressSet): AddressSet {
53
49
  const clone = this.clone();
54
50
 
55
- for (let i = 0; i < set.keys.length; i++) {
56
- clone.add(set.keys[i]);
51
+ for (const key of set.keys) {
52
+ clone.add(key);
57
53
  }
58
54
 
59
55
  return clone;
60
56
  }
61
57
 
62
- *[Symbol.iterator]() {
63
- for (let i = 0; i < this.keys.length; i++) {
64
- yield this.keys[i];
65
- }
58
+ *[Symbol.iterator](): IterableIterator<Address> {
59
+ yield* this.keys;
66
60
  }
67
61
  }
@@ -1,10 +1,13 @@
1
1
  export class DeterministicMap<K, V> {
2
2
  private map: Map<K, V>;
3
- #keys: K[];
3
+ private readonly compareFn: (a: K, b: K) => number;
4
4
 
5
- constructor(private compareFn: (a: K, b: K) => number) {
5
+ #keyOrder: K[];
6
+
7
+ constructor(compareFn: (a: K, b: K) => number) {
6
8
  this.map = new Map<K, V>();
7
- this.#keys = [];
9
+ this.#keyOrder = [];
10
+ this.compareFn = compareFn;
8
11
  }
9
12
 
10
13
  public get size(): number {
@@ -24,8 +27,8 @@ export class DeterministicMap<K, V> {
24
27
 
25
28
  public set(key: K, value: V): void {
26
29
  if (!this.map.has(key)) {
27
- this.#keys.push(key);
28
- this.#keys.sort(this.compareFn);
30
+ this.#keyOrder.push(key);
31
+ this.#keyOrder.sort(this.compareFn);
29
32
  }
30
33
  this.map.set(key, value);
31
34
  }
@@ -35,17 +38,17 @@ export class DeterministicMap<K, V> {
35
38
  }
36
39
 
37
40
  public keys(): IterableIterator<K> {
38
- return this.#keys.values();
41
+ return this.#keyOrder.values();
39
42
  }
40
43
 
41
44
  public values(): IterableIterator<V> {
42
45
  const values: V[] = [];
43
46
 
44
- for (let i = 0; i < this.#keys.length; i++) {
45
- const key = this.#keys[i];
47
+ for (let i = 0; i < this.#keyOrder.length; i++) {
48
+ const key = this.#keyOrder[i];
46
49
  const value = this.map.get(key);
47
50
 
48
- if (value) {
51
+ if (value !== undefined) {
49
52
  values.push(value);
50
53
  } else {
51
54
  throw new Error('Value not found');
@@ -62,7 +65,7 @@ export class DeterministicMap<K, V> {
62
65
  public delete(key: K): boolean {
63
66
  if (this.map.has(key)) {
64
67
  this.map.delete(key);
65
- this.#keys = this.#keys.filter((k) => k !== key);
68
+ this.#keyOrder = this.#keyOrder.filter((k) => k !== key);
66
69
  return true;
67
70
  }
68
71
  return false;
@@ -70,18 +73,18 @@ export class DeterministicMap<K, V> {
70
73
 
71
74
  public clear(): void {
72
75
  this.map.clear();
73
- this.#keys = [];
76
+ this.#keyOrder = [];
74
77
  }
75
78
 
76
79
  public forEach(callback: (value: V, key: K, map: DeterministicMap<K, V>) => void): void {
77
- for (const key of this.#keys) {
80
+ for (const key of this.#keyOrder) {
78
81
  const value = this.map.get(key) as V;
79
82
  callback(value, key, this);
80
83
  }
81
84
  }
82
85
 
83
86
  *[Symbol.iterator](): IterableIterator<[K, V]> {
84
- for (const key of this.#keys) {
87
+ for (const key of this.#keyOrder) {
85
88
  yield [key, this.map.get(key) as V];
86
89
  }
87
90
  }
@@ -30,6 +30,8 @@ export class Address extends Uint8Array {
30
30
  #tweakedUncompressed: Buffer | undefined;
31
31
  #p2wda: IP2WSHAddress | undefined;
32
32
  #mldsaPublicKey: Uint8Array | undefined;
33
+ #cachedBigInt: bigint | undefined;
34
+ #cachedUint64Array: [bigint, bigint, bigint, bigint] | undefined;
33
35
 
34
36
  private classicPublicKey: Uint8Array | undefined;
35
37
 
@@ -140,6 +142,109 @@ export class Address extends Uint8Array {
140
142
  return compressed;
141
143
  }
142
144
 
145
+ /**
146
+ * Creates an Address instance from a BigInt value.
147
+ *
148
+ * Converts a 256-bit unsigned integer into a 32-byte address by splitting it
149
+ * into four 64-bit chunks and writing them in big-endian format using DataView.
150
+ * This is the inverse operation of toBigInt().
151
+ *
152
+ * @param {bigint} value - The 256-bit unsigned integer to convert (0 to 2^256-1)
153
+ * @returns {Address} A new Address instance containing the converted value
154
+ *
155
+ * @throws {RangeError} If the value is negative or exceeds 2^256-1
156
+ *
157
+ * @example
158
+ * ```typescript
159
+ * const bigIntValue = 12345678901234567890n;
160
+ * const address = Address.fromBigInt(bigIntValue);
161
+ * console.log(address.toHex()); // 0x0000000000000000000000000000000000000000000000000000abc123...
162
+ * ```
163
+ */
164
+ public static fromBigInt(value: bigint): Address {
165
+ const buffer = new Uint8Array(32);
166
+ const view = new DataView(buffer.buffer);
167
+
168
+ view.setBigUint64(0, (value >> 192n) & 0xffffffffffffffffn, false);
169
+ view.setBigUint64(8, (value >> 128n) & 0xffffffffffffffffn, false);
170
+ view.setBigUint64(16, (value >> 64n) & 0xffffffffffffffffn, false);
171
+ view.setBigUint64(24, value & 0xffffffffffffffffn, false);
172
+
173
+ return new Address(buffer);
174
+ }
175
+
176
+ /**
177
+ * Creates an Address instance from four 64-bit unsigned integers.
178
+ *
179
+ * Constructs a 32-byte address by combining four 64-bit big-endian unsigned integers.
180
+ * This is the inverse operation of toUint64Array() and provides an efficient way
181
+ * to create addresses from word-aligned data.
182
+ *
183
+ * @param {bigint} w0 - Most significant 64 bits (bytes 0-7)
184
+ * @param {bigint} w1 - Second 64 bits (bytes 8-15)
185
+ * @param {bigint} w2 - Third 64 bits (bytes 16-23)
186
+ * @param {bigint} w3 - Least significant 64 bits (bytes 24-31)
187
+ * @returns {Address} A new Address instance containing the combined value
188
+ *
189
+ * @throws {RangeError} If any value exceeds 64 bits (2^64-1)
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * const address = Address.fromUint64Array(
194
+ * 0x0123456789abcdefn,
195
+ * 0xfedcba9876543210n,
196
+ * 0x1111222233334444n,
197
+ * 0x5555666677778888n
198
+ * );
199
+ * console.log(address.toHex());
200
+ * ```
201
+ */
202
+ public static fromUint64Array(w0: bigint, w1: bigint, w2: bigint, w3: bigint): Address {
203
+ const buffer = new Uint8Array(32);
204
+ const view = new DataView(buffer.buffer);
205
+
206
+ view.setBigUint64(0, w0, false);
207
+ view.setBigUint64(8, w1, false);
208
+ view.setBigUint64(16, w2, false);
209
+ view.setBigUint64(24, w3, false);
210
+
211
+ return new Address(buffer);
212
+ }
213
+
214
+ /**
215
+ * Converts the address to four 64-bit unsigned integers.
216
+ *
217
+ * Splits the 32-byte (256-bit) address into four 64-bit big-endian unsigned integers.
218
+ * This representation is useful for efficient storage, comparison operations, or
219
+ * interfacing with systems that work with 64-bit word sizes.
220
+ *
221
+ * @returns {[bigint, bigint, bigint, bigint]} An array of four 64-bit unsigned integers
222
+ * representing the address from most significant to least significant bits
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * const address = Address.fromString('0x0123456789abcdef...');
227
+ * const [w0, w1, w2, w3] = address.toUint64Array();
228
+ * console.log(w0); // Most significant 64 bits
229
+ * console.log(w3); // Least significant 64 bits
230
+ * ```
231
+ */
232
+ public toUint64Array(): [bigint, bigint, bigint, bigint] {
233
+ if (this.#cachedUint64Array !== undefined) {
234
+ return this.#cachedUint64Array;
235
+ }
236
+
237
+ const view = new DataView(this.buffer, this.byteOffset, 32);
238
+ this.#cachedUint64Array = [
239
+ view.getBigUint64(0, false),
240
+ view.getBigUint64(8, false),
241
+ view.getBigUint64(16, false),
242
+ view.getBigUint64(24, false),
243
+ ];
244
+
245
+ return this.#cachedUint64Array;
246
+ }
247
+
143
248
  /**
144
249
  * Converts the address to a hex string
145
250
  * @returns {string} The hex string
@@ -220,6 +325,37 @@ export class Address extends Uint8Array {
220
325
  return Buffer.from(this.#originalPublicKey);
221
326
  }
222
327
 
328
+ /**
329
+ * Converts the address to a BigInt representation.
330
+ *
331
+ * This method uses an optimized DataView approach to read the 32-byte address
332
+ * as four 64-bit big-endian unsigned integers, then combines them using bitwise
333
+ * operations. This is approximately 10-20x faster than string-based conversion.
334
+ *
335
+ * @returns {bigint} The address as a 256-bit unsigned integer
336
+ *
337
+ * @example
338
+ * ```typescript
339
+ * const address = Address.fromString('0x0123456789abcdef...');
340
+ * const bigIntValue = address.toBigInt();
341
+ * console.log(bigIntValue); // 123456789...n
342
+ * ```
343
+ */
344
+ public toBigInt(): bigint {
345
+ if (this.#cachedBigInt !== undefined) {
346
+ return this.#cachedBigInt;
347
+ }
348
+
349
+ const view = new DataView(this.buffer, this.byteOffset, 32);
350
+ this.#cachedBigInt =
351
+ (view.getBigUint64(0, false) << 192n) |
352
+ (view.getBigUint64(8, false) << 128n) |
353
+ (view.getBigUint64(16, false) << 64n) |
354
+ view.getBigUint64(24, false);
355
+
356
+ return this.#cachedBigInt;
357
+ }
358
+
223
359
  public equals(a: Address): boolean {
224
360
  const b: Address = this as Address;
225
361
 
@@ -1,20 +1,18 @@
1
- import { i32 } from './types';
1
+ export const ADDRESS_BYTE_LENGTH: number = 32;
2
+ export const SELECTOR_BYTE_LENGTH: number = 4;
2
3
 
3
- export const ADDRESS_BYTE_LENGTH: i32 = 32;
4
- export const SELECTOR_BYTE_LENGTH: i32 = 4;
4
+ export const U256_BYTE_LENGTH: number = 32;
5
+ export const U128_BYTE_LENGTH: number = 16;
6
+ export const U64_BYTE_LENGTH: number = 8;
7
+ export const U32_BYTE_LENGTH: number = 4;
8
+ export const U16_BYTE_LENGTH: number = 2;
9
+ export const U8_BYTE_LENGTH: number = 1;
5
10
 
6
- export const U256_BYTE_LENGTH: i32 = 32;
7
- export const U128_BYTE_LENGTH: i32 = 16;
8
- export const U64_BYTE_LENGTH: i32 = 8;
9
- export const U32_BYTE_LENGTH: i32 = 4;
10
- export const U16_BYTE_LENGTH: i32 = 2;
11
- export const U8_BYTE_LENGTH: i32 = 1;
11
+ export const I256_BYTE_LENGTH: number = 32;
12
+ export const I128_BYTE_LENGTH: number = 16;
13
+ export const I64_BYTE_LENGTH: number = 8;
14
+ export const number_BYTE_LENGTH: number = 4;
15
+ export const I16_BYTE_LENGTH: number = 2;
16
+ export const I8_BYTE_LENGTH: number = 1;
12
17
 
13
- export const I256_BYTE_LENGTH: i32 = 32;
14
- export const I128_BYTE_LENGTH: i32 = 16;
15
- export const I64_BYTE_LENGTH: i32 = 8;
16
- export const I32_BYTE_LENGTH: i32 = 4;
17
- export const I16_BYTE_LENGTH: i32 = 2;
18
- export const I8_BYTE_LENGTH: i32 = 1;
19
-
20
- export const BOOLEAN_BYTE_LENGTH: i32 = 1;
18
+ export const BOOLEAN_BYTE_LENGTH: number = 1;
@@ -1,15 +0,0 @@
1
- import { i32 } from '../utils/types.js';
2
- export declare class Map<K, V> {
3
- protected _keys: K[];
4
- protected _values: V[];
5
- get size(): i32;
6
- keys(): K[];
7
- values(): V[];
8
- entries(): [K, V][];
9
- set(key: K, value: V): void;
10
- indexOf(key: K): i32;
11
- get(key: K): V | undefined;
12
- has(key: K): boolean;
13
- delete(key: K): boolean;
14
- clear(): void;
15
- }
@@ -1,15 +0,0 @@
1
- import { i32 } from '../utils/types.js';
2
- export declare class Map<K, V> {
3
- protected _keys: K[];
4
- protected _values: V[];
5
- get size(): i32;
6
- keys(): K[];
7
- values(): V[];
8
- entries(): [K, V][];
9
- set(key: K, value: V): void;
10
- indexOf(key: K): i32;
11
- get(key: K): V | undefined;
12
- has(key: K): boolean;
13
- delete(key: K): boolean;
14
- clear(): void;
15
- }
@@ -1,63 +0,0 @@
1
- export class Map {
2
- constructor() {
3
- this._keys = [];
4
- this._values = [];
5
- }
6
- get size() {
7
- return this._keys.length;
8
- }
9
- keys() {
10
- return this._keys;
11
- }
12
- values() {
13
- return this._values;
14
- }
15
- entries() {
16
- const result = [];
17
- for (let i = 0; i < this._keys.length; i++) {
18
- result.push([this._keys[i], this._values[i]]);
19
- }
20
- return result;
21
- }
22
- set(key, value) {
23
- const index = this.indexOf(key);
24
- if (index == -1) {
25
- this._keys.push(key);
26
- this._values.push(value);
27
- }
28
- else {
29
- this._values[index] = value;
30
- }
31
- }
32
- indexOf(key) {
33
- for (let i = 0; i < this._keys.length; i++) {
34
- if (this._keys[i] == key) {
35
- return i;
36
- }
37
- }
38
- return -1;
39
- }
40
- get(key) {
41
- const index = this.indexOf(key);
42
- if (index == -1) {
43
- return undefined;
44
- }
45
- return this._values[index];
46
- }
47
- has(key) {
48
- return this.indexOf(key) != -1;
49
- }
50
- delete(key) {
51
- const index = this.indexOf(key);
52
- if (index == -1) {
53
- return false;
54
- }
55
- this._keys.splice(index, 1);
56
- this._values.splice(index, 1);
57
- return true;
58
- }
59
- clear() {
60
- this._keys = [];
61
- this._values = [];
62
- }
63
- }