@btc-vision/transaction 1.7.1 → 1.7.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/browser/index.js +1 -1
- package/browser/src/buffer/BinaryReader.d.ts +2 -2
- package/browser/src/deterministic/AddressMap.d.ts +12 -5
- package/browser/src/deterministic/AddressSet.d.ts +2 -1
- package/browser/src/deterministic/DeterministicMap.d.ts +2 -0
- package/browser/src/deterministic/Map.d.ts +4 -3
- package/browser/src/keypair/Address.d.ts +4 -0
- package/browser/src/opnet.d.ts +1 -0
- package/browser/src/utils/lengths.d.ts +15 -16
- package/build/buffer/BinaryReader.d.ts +2 -2
- package/build/deterministic/AddressMap.d.ts +12 -5
- package/build/deterministic/AddressMap.js +52 -30
- package/build/deterministic/AddressSet.d.ts +2 -1
- package/build/deterministic/AddressSet.js +17 -21
- package/build/deterministic/DeterministicMap.d.ts +2 -0
- package/build/deterministic/DeterministicMap.js +12 -11
- package/build/deterministic/Map.d.ts +4 -3
- package/build/deterministic/Map.js +11 -8
- package/build/keypair/Address.d.ts +4 -0
- package/build/keypair/Address.js +46 -2
- package/build/opnet.d.ts +1 -0
- package/build/opnet.js +1 -0
- package/build/utils/lengths.d.ts +15 -16
- package/build/utils/lengths.js +1 -1
- package/documentation/quantum-support/02-mnemonic-and-wallet.md +22 -10
- package/package.json +1 -1
- package/src/buffer/BinaryReader.ts +3 -3
- package/src/buffer/BinaryWriter.ts +2 -2
- package/src/deterministic/AddressMap.ts +66 -35
- package/src/deterministic/AddressSet.ts +20 -26
- package/src/deterministic/DeterministicMap.ts +14 -12
- package/src/deterministic/Map.ts +12 -8
- package/src/keypair/Address.ts +136 -0
- package/src/opnet.ts +2 -0
- package/src/utils/lengths.ts +15 -17
|
@@ -404,16 +404,21 @@ console.log(wallet1.p2tr === wallet2.p2tr); // true - deterministic!
|
|
|
404
404
|
## Complete Example
|
|
405
405
|
|
|
406
406
|
```typescript
|
|
407
|
-
import {
|
|
407
|
+
import {
|
|
408
|
+
AddressTypes,
|
|
409
|
+
MLDSASecurityLevel,
|
|
410
|
+
Mnemonic,
|
|
411
|
+
MnemonicStrength,
|
|
412
|
+
} from '@btc-vision/transaction';
|
|
408
413
|
import { networks } from '@btc-vision/bitcoin';
|
|
409
414
|
|
|
410
415
|
// Step 1: Generate mnemonic
|
|
411
416
|
console.log('Generating quantum-resistant wallet...');
|
|
412
417
|
const mnemonic = Mnemonic.generate(
|
|
413
|
-
MnemonicStrength.MAXIMUM,
|
|
414
|
-
'
|
|
415
|
-
networks.
|
|
416
|
-
MLDSASecurityLevel.LEVEL2
|
|
418
|
+
MnemonicStrength.MAXIMUM, // 24 words
|
|
419
|
+
'', // BIP39 passphrase
|
|
420
|
+
networks.regtest, // Network
|
|
421
|
+
MLDSASecurityLevel.LEVEL2, // Security level
|
|
417
422
|
);
|
|
418
423
|
|
|
419
424
|
// Step 2: Securely store mnemonic phrase
|
|
@@ -423,11 +428,12 @@ console.log(phrase);
|
|
|
423
428
|
|
|
424
429
|
// Step 3: Derive wallets
|
|
425
430
|
const wallet = mnemonic.derive(0);
|
|
431
|
+
const walletUnisat = mnemonic.deriveUnisat(AddressTypes.P2TR, 0);
|
|
426
432
|
|
|
427
|
-
|
|
428
|
-
console.log('
|
|
429
|
-
console.log('P2TR:',
|
|
430
|
-
console.log('P2WPKH:',
|
|
433
|
+
console.log('\nDerived Wallets:');
|
|
434
|
+
console.log('Classical Wallet (Unisat):');
|
|
435
|
+
console.log(' P2TR Address:', walletUnisat.p2tr);
|
|
436
|
+
console.log(' P2WPKH Address:', walletUnisat.p2wpkh);
|
|
431
437
|
|
|
432
438
|
console.log('\nQuantum Address:');
|
|
433
439
|
console.log('Public Key:', wallet.address.toHex());
|
|
@@ -435,8 +441,14 @@ console.log('Public Key:', wallet.address.toHex());
|
|
|
435
441
|
// Step 5: Display keys (for demonstration only!)
|
|
436
442
|
console.log('\nKey Information:');
|
|
437
443
|
console.log('Classical Public Key:', wallet.toPublicKeyHex());
|
|
438
|
-
console.log('Quantum Public Key
|
|
444
|
+
console.log('Quantum Public Key:', wallet.quantumPublicKeyHex);
|
|
439
445
|
console.log('Security Level:', wallet.securityLevel);
|
|
446
|
+
|
|
447
|
+
// Step 4: Get addresses
|
|
448
|
+
console.log('\nClassical Addresses:');
|
|
449
|
+
console.log('P2TR:', wallet.p2tr);
|
|
450
|
+
console.log('P2WPKH:', wallet.p2wpkh);
|
|
451
|
+
|
|
440
452
|
```
|
|
441
453
|
|
|
442
454
|
## Next Steps
|
package/package.json
CHANGED
|
@@ -10,11 +10,11 @@ import {
|
|
|
10
10
|
U64_BYTE_LENGTH,
|
|
11
11
|
U8_BYTE_LENGTH,
|
|
12
12
|
} from '../utils/lengths.js';
|
|
13
|
-
import { BufferLike,
|
|
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:
|
|
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:
|
|
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 {
|
|
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:
|
|
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,94 @@
|
|
|
1
|
-
import { i32 } from '../utils/types.js';
|
|
2
1
|
import { Address } from '../keypair/Address.js';
|
|
3
2
|
import { Map } from './Map.js';
|
|
4
3
|
|
|
5
|
-
export class AddressMap<V>
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
export class AddressMap<V> {
|
|
5
|
+
private items: Map<bigint, V>;
|
|
6
|
+
private keyOrder: Address[];
|
|
7
|
+
|
|
8
|
+
constructor(iterable?: ReadonlyArray<readonly [Address, V]> | null) {
|
|
9
|
+
this.items = new Map();
|
|
10
|
+
this.keyOrder = [];
|
|
11
|
+
|
|
12
|
+
if (iterable) {
|
|
13
|
+
for (const [key, value] of iterable) {
|
|
14
|
+
this.set(key, value);
|
|
15
|
+
}
|
|
13
16
|
}
|
|
14
17
|
}
|
|
15
18
|
|
|
16
|
-
public
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
public get size(): number {
|
|
20
|
+
return this.keyOrder.length;
|
|
21
|
+
}
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
public set(key: Address, value: V): void {
|
|
24
|
+
const keyBigInt = key.toBigInt();
|
|
25
|
+
if (!this.items.has(keyBigInt)) {
|
|
26
|
+
this.keyOrder.push(key);
|
|
23
27
|
}
|
|
28
|
+
this.items.set(keyBigInt, value);
|
|
29
|
+
}
|
|
24
30
|
|
|
25
|
-
|
|
31
|
+
public get(key: Address): V | undefined {
|
|
32
|
+
return this.items.get(key.toBigInt());
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
public has(key: Address): boolean {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
36
|
+
return this.items.has(key.toBigInt());
|
|
37
|
+
}
|
|
34
38
|
|
|
39
|
+
public delete(key: Address): boolean {
|
|
40
|
+
const keyBigInt = key.toBigInt();
|
|
41
|
+
if (this.items.delete(keyBigInt)) {
|
|
42
|
+
this.keyOrder = this.keyOrder.filter((k) => k.toBigInt() !== keyBigInt);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
35
45
|
return false;
|
|
36
46
|
}
|
|
37
47
|
|
|
38
|
-
public
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
48
|
+
public clear(): void {
|
|
49
|
+
this.items.clear();
|
|
50
|
+
this.keyOrder = [];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public indexOf(address: Address): number {
|
|
54
|
+
const addressBigInt = address.toBigInt();
|
|
55
|
+
for (let i: number = 0; i < this.keyOrder.length; i++) {
|
|
56
|
+
if (this.keyOrder[i].toBigInt() === addressBigInt) {
|
|
57
|
+
return i;
|
|
58
|
+
}
|
|
42
59
|
}
|
|
43
|
-
return
|
|
60
|
+
return -1;
|
|
44
61
|
}
|
|
45
62
|
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
return false;
|
|
63
|
+
*entries(): IterableIterator<[Address, V]> {
|
|
64
|
+
for (const key of this.keyOrder) {
|
|
65
|
+
yield [key, this.items.get(key.toBigInt()) as V];
|
|
50
66
|
}
|
|
67
|
+
}
|
|
51
68
|
|
|
52
|
-
|
|
53
|
-
this.
|
|
69
|
+
*keys(): IterableIterator<Address> {
|
|
70
|
+
yield* this.keyOrder;
|
|
71
|
+
}
|
|
54
72
|
|
|
55
|
-
|
|
73
|
+
*values(): IterableIterator<V> {
|
|
74
|
+
for (const key of this.keyOrder) {
|
|
75
|
+
yield this.items.get(key.toBigInt()) as V;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
forEach(
|
|
80
|
+
callback: (value: V, key: Address, map: AddressMap<V>) => void,
|
|
81
|
+
thisArg?: unknown,
|
|
82
|
+
): void {
|
|
83
|
+
for (const key of this.keyOrder) {
|
|
84
|
+
callback.call(thisArg, this.items.get(key.toBigInt()) as V, key, this);
|
|
85
|
+
}
|
|
56
86
|
}
|
|
57
87
|
|
|
58
88
|
*[Symbol.iterator](): IterableIterator<[Address, V]> {
|
|
59
|
-
for (
|
|
60
|
-
|
|
89
|
+
for (let i = 0; i < this.keyOrder.length; i++) {
|
|
90
|
+
const key = this.keyOrder[i];
|
|
91
|
+
yield [key, this.items.get(key.toBigInt()) as V];
|
|
61
92
|
}
|
|
62
93
|
}
|
|
63
94
|
}
|
|
@@ -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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
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 (
|
|
56
|
-
clone.add(
|
|
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
|
-
|
|
64
|
-
yield this.keys[i];
|
|
65
|
-
}
|
|
58
|
+
*[Symbol.iterator](): IterableIterator<Address> {
|
|
59
|
+
yield* this.keys;
|
|
66
60
|
}
|
|
67
61
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Map } from './Map.js';
|
|
2
|
+
|
|
1
3
|
export class DeterministicMap<K, V> {
|
|
2
4
|
private map: Map<K, V>;
|
|
3
5
|
#keys: K[];
|
|
@@ -34,25 +36,25 @@ export class DeterministicMap<K, V> {
|
|
|
34
36
|
return this.map.get(key);
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
public
|
|
38
|
-
|
|
39
|
+
public *entries(): IterableIterator<[K, V]> {
|
|
40
|
+
for (const key of this.#keys) {
|
|
41
|
+
yield [key, this.map.get(key) as V];
|
|
42
|
+
}
|
|
39
43
|
}
|
|
40
44
|
|
|
41
|
-
public
|
|
42
|
-
|
|
45
|
+
public *keys(): IterableIterator<K> {
|
|
46
|
+
yield* this.#keys;
|
|
47
|
+
}
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
public *values(): IterableIterator<V> {
|
|
50
|
+
for (const key of this.#keys) {
|
|
46
51
|
const value = this.map.get(key);
|
|
47
|
-
|
|
48
|
-
if (value) {
|
|
49
|
-
values.push(value);
|
|
50
|
-
} else {
|
|
52
|
+
if (value === undefined && !this.map.has(key)) {
|
|
51
53
|
throw new Error('Value not found');
|
|
52
54
|
}
|
|
53
|
-
}
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
yield value as V;
|
|
57
|
+
}
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
public has(key: K): boolean {
|
package/src/deterministic/Map.ts
CHANGED
|
@@ -8,20 +8,18 @@ export class Map<K, V> {
|
|
|
8
8
|
return this._keys.length;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
public keys(): K
|
|
12
|
-
|
|
11
|
+
public *keys(): IterableIterator<K> {
|
|
12
|
+
yield* this._keys;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
public values(): V
|
|
16
|
-
|
|
15
|
+
public *values(): IterableIterator<V> {
|
|
16
|
+
yield* this._values;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
public entries(): [K, V]
|
|
20
|
-
const result: [K, V][] = [];
|
|
19
|
+
public *entries(): IterableIterator<[K, V]> {
|
|
21
20
|
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
22
|
-
|
|
21
|
+
yield [this._keys[i], this._values[i]];
|
|
23
22
|
}
|
|
24
|
-
return result;
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
public set(key: K, value: V): void {
|
|
@@ -71,4 +69,10 @@ export class Map<K, V> {
|
|
|
71
69
|
this._keys = [];
|
|
72
70
|
this._values = [];
|
|
73
71
|
}
|
|
72
|
+
|
|
73
|
+
*[Symbol.iterator](): IterableIterator<[K, V]> {
|
|
74
|
+
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
75
|
+
yield [this._keys[i], this._values[i]];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
74
78
|
}
|
package/src/keypair/Address.ts
CHANGED
|
@@ -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
|
|
package/src/opnet.ts
CHANGED
|
@@ -125,6 +125,8 @@ export * from './transaction/browser/Web3Provider.js';
|
|
|
125
125
|
export * from './keypair/Secp256k1PointDeriver.js';
|
|
126
126
|
export * from './transaction/ContractAddress.js';
|
|
127
127
|
|
|
128
|
+
export * from './deterministic/Map.js';
|
|
129
|
+
|
|
128
130
|
declare global {
|
|
129
131
|
interface Window {
|
|
130
132
|
unisat?: Unisat;
|
package/src/utils/lengths.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
export const ADDRESS_BYTE_LENGTH: number = 32;
|
|
2
|
+
export const SELECTOR_BYTE_LENGTH: number = 4;
|
|
2
3
|
|
|
3
|
-
export const
|
|
4
|
-
export const
|
|
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
|
|
7
|
-
export const
|
|
8
|
-
export const
|
|
9
|
-
export const
|
|
10
|
-
export const
|
|
11
|
-
export const
|
|
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
|
|
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;
|