@btc-vision/transaction 1.8.0-alpha.1 → 1.8.0-beta.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.
Files changed (43) hide show
  1. package/browser/_version.d.ts +1 -1
  2. package/browser/abi/ABICoder.d.ts +8 -0
  3. package/browser/btc-vision-bitcoin.js +423 -422
  4. package/browser/buffer/BinaryReader.d.ts +16 -1
  5. package/browser/buffer/BinaryWriter.d.ts +11 -1
  6. package/browser/deterministic/ExtendedAddressMap.d.ts +3 -1
  7. package/browser/index.js +1102 -861
  8. package/browser/keypair/Address.d.ts +1 -0
  9. package/browser/noble-hashes.js +2 -2
  10. package/browser/utils/lengths.d.ts +3 -1
  11. package/browser/utils/types.d.ts +3 -0
  12. package/browser/vendors.js +3 -3
  13. package/build/_version.d.ts +1 -1
  14. package/build/_version.js +1 -1
  15. package/build/abi/ABICoder.d.ts +8 -0
  16. package/build/abi/ABICoder.js +32 -0
  17. package/build/buffer/BinaryReader.d.ts +16 -1
  18. package/build/buffer/BinaryReader.js +66 -1
  19. package/build/buffer/BinaryWriter.d.ts +11 -1
  20. package/build/buffer/BinaryWriter.js +66 -1
  21. package/build/deterministic/ExtendedAddressMap.d.ts +3 -1
  22. package/build/deterministic/ExtendedAddressMap.js +44 -17
  23. package/build/keypair/Address.d.ts +1 -0
  24. package/build/keypair/Address.js +19 -4
  25. package/build/transaction/builders/MultiSignTransaction.js +2 -2
  26. package/build/transaction/shared/TweakedTransaction.js +3 -3
  27. package/build/tsconfig.build.tsbuildinfo +1 -1
  28. package/build/utils/lengths.d.ts +3 -1
  29. package/build/utils/lengths.js +3 -1
  30. package/build/utils/types.d.ts +3 -0
  31. package/package.json +2 -2
  32. package/src/_version.ts +1 -1
  33. package/src/abi/ABICoder.ts +43 -0
  34. package/src/buffer/BinaryReader.ts +158 -2
  35. package/src/buffer/BinaryWriter.ts +143 -1
  36. package/src/deterministic/ExtendedAddressMap.ts +59 -20
  37. package/src/keypair/Address.ts +27 -6
  38. package/src/transaction/builders/MultiSignTransaction.ts +2 -2
  39. package/src/transaction/shared/TweakedTransaction.ts +3 -3
  40. package/src/utils/lengths.ts +3 -1
  41. package/src/utils/types.ts +4 -1
  42. package/test/binary-reader-writer.test.ts +457 -0
  43. package/test/derivePath.test.ts +4 -4
@@ -50,6 +50,7 @@ export declare class Address extends Uint8Array {
50
50
  p2op(network: Network): string;
51
51
  toTweakedHybridPublicKeyHex(): string;
52
52
  toTweakedHybridPublicKeyBuffer(): Buffer;
53
+ private ensureTweakedUncompressed;
53
54
  private setMldsaKey;
54
55
  private ensureLegacyProcessed;
55
56
  private autoFormat;
@@ -1647,8 +1647,8 @@ export {
1647
1647
  ys as a,
1648
1648
  Pe as b,
1649
1649
  gs as c,
1650
- It as d,
1651
- ws as e,
1650
+ ws as d,
1651
+ It as e,
1652
1652
  _s as f,
1653
1653
  Ss as g,
1654
1654
  Is as h,
@@ -1,4 +1,6 @@
1
1
  export declare const ADDRESS_BYTE_LENGTH: number;
2
+ export declare const EXTENDED_ADDRESS_BYTE_LENGTH: number;
3
+ export declare const SCHNORR_SIGNATURE_BYTE_LENGTH: number;
2
4
  export declare const SELECTOR_BYTE_LENGTH: number;
3
5
  export declare const U256_BYTE_LENGTH: number;
4
6
  export declare const U128_BYTE_LENGTH: number;
@@ -9,7 +11,7 @@ export declare const U8_BYTE_LENGTH: number;
9
11
  export declare const I256_BYTE_LENGTH: number;
10
12
  export declare const I128_BYTE_LENGTH: number;
11
13
  export declare const I64_BYTE_LENGTH: number;
12
- export declare const number_BYTE_LENGTH: number;
14
+ export declare const I32_BYTE_LENGTH: number;
13
15
  export declare const I16_BYTE_LENGTH: number;
14
16
  export declare const I8_BYTE_LENGTH: number;
15
17
  export declare const BOOLEAN_BYTE_LENGTH: number;
@@ -4,7 +4,10 @@ export type BufferLike = Uint8Array | Buffer;
4
4
  export type MemorySlotData<T> = T;
5
5
  export type PointerStorage = DeterministicMap<MemorySlotPointer, MemorySlotData<bigint>>;
6
6
  export type BlockchainStorage = DeterministicMap<string, PointerStorage>;
7
+ export type i8 = number;
8
+ export type i16 = number;
7
9
  export type i32 = number;
10
+ export type i64 = bigint;
8
11
  export type u8 = number;
9
12
  export type u16 = number;
10
13
  export type u32 = number;
@@ -1,4 +1,4 @@
1
- import { s as fn, r as th, a as kn, b as nh, c as hf, d as ar, e as ah, f as ts, i as oh, g as ih, h as sr, j as ns, k as sh, p as ch, l as uh, m as lh } from "./noble-hashes.js";
1
+ import { s as fn, r as th, a as kn, b as nh, c as hf, d as ah, e as ar, f as ts, i as oh, g as ih, h as sr, j as ns, k as sh, p as ch, l as uh, m as lh } from "./noble-hashes.js";
2
2
  import { r as fh, a as dh, b as hh, F as Rs, c as ph, d as gh } from "./noble-curves.js";
3
3
  import { r as as } from "./valibot.js";
4
4
  function bh(e, t) {
@@ -12021,10 +12021,10 @@ export {
12021
12021
  cv as d,
12022
12022
  fv as e,
12023
12023
  sv as f,
12024
- mv as g,
12024
+ bv as g,
12025
12025
  _m as h,
12026
12026
  uv as i,
12027
- bv as j,
12027
+ mv as j,
12028
12028
  jd as k,
12029
12029
  nd as l,
12030
12030
  _v as m,
@@ -1 +1 @@
1
- export declare const version = "1.7.31";
1
+ export declare const version = "1.8.0-beta.0";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.7.31';
1
+ export const version = '1.8.0-beta.0';
@@ -5,15 +5,23 @@ export declare enum ABIDataTypes {
5
5
  UINT64 = "UINT64",
6
6
  UINT128 = "UINT128",
7
7
  UINT256 = "UINT256",
8
+ INT8 = "INT8",
9
+ INT16 = "INT16",
10
+ INT32 = "INT32",
11
+ INT64 = "INT64",
8
12
  INT128 = "INT128",
9
13
  BOOL = "BOOL",
10
14
  ADDRESS = "ADDRESS",
15
+ EXTENDED_ADDRESS = "EXTENDED_ADDRESS",
11
16
  STRING = "STRING",
12
17
  BYTES4 = "BYTES4",
13
18
  BYTES32 = "BYTES32",
14
19
  BYTES = "BYTES",
15
20
  ADDRESS_UINT256_TUPLE = "ADDRESS_UINT256_TUPLE",
21
+ EXTENDED_ADDRESS_UINT256_TUPLE = "EXTENDED_ADDRESS_UINT256_TUPLE",
22
+ SCHNORR_SIGNATURE = "SCHNORR_SIGNATURE",
16
23
  ARRAY_OF_ADDRESSES = "ARRAY_OF_ADDRESSES",
24
+ ARRAY_OF_EXTENDED_ADDRESSES = "ARRAY_OF_EXTENDED_ADDRESSES",
17
25
  ARRAY_OF_UINT256 = "ARRAY_OF_UINT256",
18
26
  ARRAY_OF_UINT128 = "ARRAY_OF_UINT128",
19
27
  ARRAY_OF_UINT64 = "ARRAY_OF_UINT64",
@@ -9,15 +9,23 @@ export var ABIDataTypes;
9
9
  ABIDataTypes["UINT64"] = "UINT64";
10
10
  ABIDataTypes["UINT128"] = "UINT128";
11
11
  ABIDataTypes["UINT256"] = "UINT256";
12
+ ABIDataTypes["INT8"] = "INT8";
13
+ ABIDataTypes["INT16"] = "INT16";
14
+ ABIDataTypes["INT32"] = "INT32";
15
+ ABIDataTypes["INT64"] = "INT64";
12
16
  ABIDataTypes["INT128"] = "INT128";
13
17
  ABIDataTypes["BOOL"] = "BOOL";
14
18
  ABIDataTypes["ADDRESS"] = "ADDRESS";
19
+ ABIDataTypes["EXTENDED_ADDRESS"] = "EXTENDED_ADDRESS";
15
20
  ABIDataTypes["STRING"] = "STRING";
16
21
  ABIDataTypes["BYTES4"] = "BYTES4";
17
22
  ABIDataTypes["BYTES32"] = "BYTES32";
18
23
  ABIDataTypes["BYTES"] = "BYTES";
19
24
  ABIDataTypes["ADDRESS_UINT256_TUPLE"] = "ADDRESS_UINT256_TUPLE";
25
+ ABIDataTypes["EXTENDED_ADDRESS_UINT256_TUPLE"] = "EXTENDED_ADDRESS_UINT256_TUPLE";
26
+ ABIDataTypes["SCHNORR_SIGNATURE"] = "SCHNORR_SIGNATURE";
20
27
  ABIDataTypes["ARRAY_OF_ADDRESSES"] = "ARRAY_OF_ADDRESSES";
28
+ ABIDataTypes["ARRAY_OF_EXTENDED_ADDRESSES"] = "ARRAY_OF_EXTENDED_ADDRESSES";
21
29
  ABIDataTypes["ARRAY_OF_UINT256"] = "ARRAY_OF_UINT256";
22
30
  ABIDataTypes["ARRAY_OF_UINT128"] = "ARRAY_OF_UINT128";
23
31
  ABIDataTypes["ARRAY_OF_UINT64"] = "ARRAY_OF_UINT64";
@@ -65,12 +73,33 @@ export class ABICoder {
65
73
  case ABIDataTypes.UINT256:
66
74
  result.push(byteReader.readU256());
67
75
  break;
76
+ case ABIDataTypes.INT8:
77
+ result.push(byteReader.readI8());
78
+ break;
79
+ case ABIDataTypes.INT16:
80
+ result.push(byteReader.readI16());
81
+ break;
82
+ case ABIDataTypes.INT32:
83
+ result.push(byteReader.readI32());
84
+ break;
85
+ case ABIDataTypes.INT64:
86
+ result.push(byteReader.readI64());
87
+ break;
68
88
  case ABIDataTypes.INT128:
69
89
  result.push(byteReader.readI128());
70
90
  break;
91
+ case ABIDataTypes.EXTENDED_ADDRESS:
92
+ result.push(byteReader.readExtendedAddress());
93
+ break;
71
94
  case ABIDataTypes.ADDRESS_UINT256_TUPLE:
72
95
  result.push(byteReader.readAddressValueTuple());
73
96
  break;
97
+ case ABIDataTypes.EXTENDED_ADDRESS_UINT256_TUPLE:
98
+ result.push(byteReader.readExtendedAddressMapU256());
99
+ break;
100
+ case ABIDataTypes.SCHNORR_SIGNATURE:
101
+ result.push(byteReader.readSchnorrSignature());
102
+ break;
74
103
  case ABIDataTypes.BYTES:
75
104
  result.push(byteReader.readBytesWithLength());
76
105
  break;
@@ -80,6 +109,9 @@ export class ABICoder {
80
109
  case ABIDataTypes.ARRAY_OF_ADDRESSES:
81
110
  result.push(byteReader.readAddressArray());
82
111
  break;
112
+ case ABIDataTypes.ARRAY_OF_EXTENDED_ADDRESSES:
113
+ result.push(byteReader.readExtendedAddressArray());
114
+ break;
83
115
  case ABIDataTypes.ARRAY_OF_UINT256:
84
116
  result.push(byteReader.readU256Array());
85
117
  break;
@@ -1,6 +1,11 @@
1
1
  import { AddressMap } from '../deterministic/AddressMap.js';
2
+ import { ExtendedAddressMap } from '../deterministic/ExtendedAddressMap.js';
2
3
  import { Address } from '../keypair/Address.js';
3
- import { BufferLike, Selector, u16, u32, u8 } from '../utils/types.js';
4
+ import { BufferLike, i16, i32, i64, i8, Selector, u16, u32, u8 } from '../utils/types.js';
5
+ export interface SchnorrSignature {
6
+ readonly address: Address;
7
+ readonly signature: Uint8Array;
8
+ }
4
9
  export declare class BinaryReader {
5
10
  private buffer;
6
11
  private currentOffset;
@@ -9,8 +14,13 @@ export declare class BinaryReader {
9
14
  static bigintCompare(a: bigint, b: bigint): number;
10
15
  static numberCompare(a: number, b: number): number;
11
16
  setBuffer(bytes: BufferLike): void;
17
+ get byteLength(): number;
12
18
  length(): number;
13
19
  bytesLeft(): number;
20
+ readI8(): i8;
21
+ readI16(be?: boolean): i16;
22
+ readI32(be?: boolean): i32;
23
+ readI64(be?: boolean): i64;
14
24
  readU8(): u8;
15
25
  readU16(be?: boolean): u16;
16
26
  readU32(be?: boolean): u32;
@@ -24,6 +34,9 @@ export declare class BinaryReader {
24
34
  readString(length: u32): string;
25
35
  readStringWithLength(be?: boolean): string;
26
36
  readAddress(): Address;
37
+ readTweakedPublicKey(): Uint8Array;
38
+ readExtendedAddress(): Address;
39
+ readSchnorrSignature(): SchnorrSignature;
27
40
  readBytesWithLength(maxLength?: number, be?: boolean): Uint8Array;
28
41
  readArrayOfBuffer(be?: boolean): Uint8Array[];
29
42
  readAddressArray(be?: boolean): Address[];
@@ -36,6 +49,8 @@ export declare class BinaryReader {
36
49
  readStringArray(be?: boolean): string[];
37
50
  readBytesArray(be?: boolean): Uint8Array[];
38
51
  readAddressValueTuple(be?: boolean): AddressMap<bigint>;
52
+ readExtendedAddressArray(be?: boolean): Address[];
53
+ readExtendedAddressMapU256(be?: boolean): ExtendedAddressMap<bigint>;
39
54
  getOffset(): u16;
40
55
  setOffset(offset: u16): void;
41
56
  verifyEnd(size: number): void;
@@ -1,6 +1,7 @@
1
1
  import { AddressMap } from '../deterministic/AddressMap.js';
2
+ import { ExtendedAddressMap } from '../deterministic/ExtendedAddressMap.js';
2
3
  import { Address } from '../keypair/Address.js';
3
- import { ADDRESS_BYTE_LENGTH, I128_BYTE_LENGTH, U128_BYTE_LENGTH, U16_BYTE_LENGTH, U256_BYTE_LENGTH, U32_BYTE_LENGTH, U64_BYTE_LENGTH, U8_BYTE_LENGTH, } from '../utils/lengths.js';
4
+ import { ADDRESS_BYTE_LENGTH, EXTENDED_ADDRESS_BYTE_LENGTH, I128_BYTE_LENGTH, I16_BYTE_LENGTH, I32_BYTE_LENGTH, I64_BYTE_LENGTH, I8_BYTE_LENGTH, SCHNORR_SIGNATURE_BYTE_LENGTH, U128_BYTE_LENGTH, U16_BYTE_LENGTH, U256_BYTE_LENGTH, U32_BYTE_LENGTH, U64_BYTE_LENGTH, U8_BYTE_LENGTH, } from '../utils/lengths.js';
4
5
  export class BinaryReader {
5
6
  constructor(bytes) {
6
7
  this.currentOffset = 0;
@@ -27,12 +28,39 @@ export class BinaryReader {
27
28
  this.buffer = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
28
29
  this.currentOffset = 0;
29
30
  }
31
+ get byteLength() {
32
+ return this.buffer.byteLength;
33
+ }
30
34
  length() {
31
35
  return this.buffer.byteLength;
32
36
  }
33
37
  bytesLeft() {
34
38
  return this.buffer.byteLength - this.currentOffset;
35
39
  }
40
+ readI8() {
41
+ this.verifyEnd(this.currentOffset + I8_BYTE_LENGTH);
42
+ const value = this.buffer.getInt8(this.currentOffset);
43
+ this.currentOffset += I8_BYTE_LENGTH;
44
+ return value;
45
+ }
46
+ readI16(be = true) {
47
+ this.verifyEnd(this.currentOffset + I16_BYTE_LENGTH);
48
+ const value = this.buffer.getInt16(this.currentOffset, !be);
49
+ this.currentOffset += I16_BYTE_LENGTH;
50
+ return value;
51
+ }
52
+ readI32(be = true) {
53
+ this.verifyEnd(this.currentOffset + I32_BYTE_LENGTH);
54
+ const value = this.buffer.getInt32(this.currentOffset, !be);
55
+ this.currentOffset += I32_BYTE_LENGTH;
56
+ return value;
57
+ }
58
+ readI64(be = true) {
59
+ this.verifyEnd(this.currentOffset + I64_BYTE_LENGTH);
60
+ const value = this.buffer.getBigInt64(this.currentOffset, !be);
61
+ this.currentOffset += I64_BYTE_LENGTH;
62
+ return value;
63
+ }
36
64
  readU8() {
37
65
  this.verifyEnd(this.currentOffset + U8_BYTE_LENGTH);
38
66
  const value = this.buffer.getUint8(this.currentOffset);
@@ -119,6 +147,22 @@ export class BinaryReader {
119
147
  const bytes = Array.from(this.readBytes(ADDRESS_BYTE_LENGTH));
120
148
  return new Address(bytes);
121
149
  }
150
+ readTweakedPublicKey() {
151
+ this.verifyEnd(this.currentOffset + ADDRESS_BYTE_LENGTH);
152
+ return this.readBytes(ADDRESS_BYTE_LENGTH);
153
+ }
154
+ readExtendedAddress() {
155
+ this.verifyEnd(this.currentOffset + EXTENDED_ADDRESS_BYTE_LENGTH);
156
+ const tweakedPublicKey = Array.from(this.readBytes(ADDRESS_BYTE_LENGTH));
157
+ const mldsaKeyHash = Array.from(this.readBytes(ADDRESS_BYTE_LENGTH));
158
+ return new Address(mldsaKeyHash, tweakedPublicKey);
159
+ }
160
+ readSchnorrSignature() {
161
+ this.verifyEnd(this.currentOffset + EXTENDED_ADDRESS_BYTE_LENGTH + SCHNORR_SIGNATURE_BYTE_LENGTH);
162
+ const address = this.readExtendedAddress();
163
+ const signature = this.readBytes(SCHNORR_SIGNATURE_BYTE_LENGTH);
164
+ return { address, signature };
165
+ }
122
166
  readBytesWithLength(maxLength = 0, be = true) {
123
167
  const length = this.readU32(be);
124
168
  if (maxLength > 0 && length > maxLength) {
@@ -219,6 +263,27 @@ export class BinaryReader {
219
263
  }
220
264
  return result;
221
265
  }
266
+ readExtendedAddressArray(be = true) {
267
+ const length = this.readU16(be);
268
+ const result = new Array(length);
269
+ for (let i = 0; i < length; i++) {
270
+ result[i] = this.readExtendedAddress();
271
+ }
272
+ return result;
273
+ }
274
+ readExtendedAddressMapU256(be = true) {
275
+ const length = this.readU16(be);
276
+ const result = new ExtendedAddressMap();
277
+ for (let i = 0; i < length; i++) {
278
+ const address = this.readExtendedAddress();
279
+ const value = this.readU256(be);
280
+ if (result.has(address)) {
281
+ throw new Error('Duplicate tweaked address found in map');
282
+ }
283
+ result.set(address, value);
284
+ }
285
+ return result;
286
+ }
222
287
  getOffset() {
223
288
  return this.currentOffset;
224
289
  }
@@ -1,6 +1,7 @@
1
1
  import { AddressMap } from '../deterministic/AddressMap.js';
2
+ import { ExtendedAddressMap } from '../deterministic/ExtendedAddressMap.js';
2
3
  import { Address } from '../keypair/Address.js';
3
- import { Selector, u16, u32, u64, u8 } from '../utils/types.js';
4
+ import { i16, i32, i64, i8, Selector, u16, u32, u64, u8 } from '../utils/types.js';
4
5
  import { BinaryReader } from './BinaryReader.js';
5
6
  export declare class BinaryWriter {
6
7
  private currentOffset;
@@ -11,6 +12,10 @@ export declare class BinaryWriter {
11
12
  writeU16(value: u16, be?: boolean): void;
12
13
  writeU32(value: u32, be?: boolean): void;
13
14
  writeU64(value: u64, be?: boolean): void;
15
+ writeI8(value: i8): void;
16
+ writeI16(value: i16, be?: boolean): void;
17
+ writeI32(value: i32, be?: boolean): void;
18
+ writeI64(value: i64, be?: boolean): void;
14
19
  writeSelector(value: Selector): void;
15
20
  writeBoolean(value: boolean): void;
16
21
  writeI128(bigIntValue: bigint, be?: boolean): void;
@@ -20,6 +25,9 @@ export declare class BinaryWriter {
20
25
  writeString(value: string): void;
21
26
  writeStringWithLength(value: string): void;
22
27
  writeAddress(value: Address): void;
28
+ writeTweakedPublicKey(value: Address): void;
29
+ writeExtendedAddress(value: Address): void;
30
+ writeSchnorrSignature(address: Address, signature: Uint8Array): void;
23
31
  getBuffer(clear?: boolean): Uint8Array;
24
32
  reset(): void;
25
33
  toBytesReader(): BinaryReader;
@@ -28,9 +36,11 @@ export declare class BinaryWriter {
28
36
  clear(): void;
29
37
  allocSafe(size: u32): void;
30
38
  writeAddressValueTuple(map: AddressMap<bigint>, be?: boolean): void;
39
+ writeExtendedAddressMapU256(map: ExtendedAddressMap<bigint>, be?: boolean): void;
31
40
  writeBytesWithLength(value: Uint8Array): void;
32
41
  writeArrayOfBuffer(values: Uint8Array[], be?: boolean): void;
33
42
  writeAddressArray(value: Address[]): void;
43
+ writeExtendedAddressArray(value: Address[]): void;
34
44
  writeU32Array(value: u32[], be?: boolean): void;
35
45
  writeU256Array(value: bigint[], be?: boolean): void;
36
46
  writeU128Array(value: bigint[], be?: boolean): void;
@@ -1,5 +1,5 @@
1
1
  import { BufferHelper } from '../utils/BufferHelper.js';
2
- import { ADDRESS_BYTE_LENGTH, I128_BYTE_LENGTH, U128_BYTE_LENGTH, U16_BYTE_LENGTH, U256_BYTE_LENGTH, U32_BYTE_LENGTH, U64_BYTE_LENGTH, U8_BYTE_LENGTH, } from '../utils/lengths.js';
2
+ import { ADDRESS_BYTE_LENGTH, EXTENDED_ADDRESS_BYTE_LENGTH, I128_BYTE_LENGTH, I16_BYTE_LENGTH, I32_BYTE_LENGTH, I64_BYTE_LENGTH, I8_BYTE_LENGTH, SCHNORR_SIGNATURE_BYTE_LENGTH, U128_BYTE_LENGTH, U16_BYTE_LENGTH, U256_BYTE_LENGTH, U32_BYTE_LENGTH, U64_BYTE_LENGTH, U8_BYTE_LENGTH, } from '../utils/lengths.js';
3
3
  import { BinaryReader } from './BinaryReader.js';
4
4
  export class BinaryWriter {
5
5
  constructor(length = 0) {
@@ -42,6 +42,35 @@ export class BinaryWriter {
42
42
  this.buffer.setBigUint64(this.currentOffset, value, !be);
43
43
  this.currentOffset += 8;
44
44
  }
45
+ writeI8(value) {
46
+ if (value < -128 || value > 127)
47
+ throw new Error('i8 value is out of range.');
48
+ this.allocSafe(I8_BYTE_LENGTH);
49
+ this.buffer.setInt8(this.currentOffset, value);
50
+ this.currentOffset += I8_BYTE_LENGTH;
51
+ }
52
+ writeI16(value, be = true) {
53
+ if (value < -32768 || value > 32767)
54
+ throw new Error('i16 value is out of range.');
55
+ this.allocSafe(I16_BYTE_LENGTH);
56
+ this.buffer.setInt16(this.currentOffset, value, !be);
57
+ this.currentOffset += I16_BYTE_LENGTH;
58
+ }
59
+ writeI32(value, be = true) {
60
+ if (value < -2147483648 || value > 2147483647)
61
+ throw new Error('i32 value is out of range.');
62
+ this.allocSafe(I32_BYTE_LENGTH);
63
+ this.buffer.setInt32(this.currentOffset, value, !be);
64
+ this.currentOffset += I32_BYTE_LENGTH;
65
+ }
66
+ writeI64(value, be = true) {
67
+ if (value < -9223372036854775808n || value > 9223372036854775807n) {
68
+ throw new Error('i64 value is out of range.');
69
+ }
70
+ this.allocSafe(I64_BYTE_LENGTH);
71
+ this.buffer.setBigInt64(this.currentOffset, value, !be);
72
+ this.currentOffset += I64_BYTE_LENGTH;
73
+ }
45
74
  writeSelector(value) {
46
75
  this.writeU32(value, true);
47
76
  }
@@ -134,6 +163,24 @@ export class BinaryWriter {
134
163
  this.verifyAddress(value);
135
164
  this.writeBytes(value);
136
165
  }
166
+ writeTweakedPublicKey(value) {
167
+ const tweakedKey = value.tweakedPublicKeyToBuffer();
168
+ this.allocSafe(ADDRESS_BYTE_LENGTH);
169
+ this.writeBytes(tweakedKey);
170
+ }
171
+ writeExtendedAddress(value) {
172
+ this.allocSafe(EXTENDED_ADDRESS_BYTE_LENGTH);
173
+ this.writeTweakedPublicKey(value);
174
+ this.writeBytes(value);
175
+ }
176
+ writeSchnorrSignature(address, signature) {
177
+ if (signature.length !== SCHNORR_SIGNATURE_BYTE_LENGTH) {
178
+ throw new Error(`Invalid Schnorr signature length: expected ${SCHNORR_SIGNATURE_BYTE_LENGTH}, got ${signature.length}`);
179
+ }
180
+ this.allocSafe(EXTENDED_ADDRESS_BYTE_LENGTH + SCHNORR_SIGNATURE_BYTE_LENGTH);
181
+ this.writeExtendedAddress(address);
182
+ this.writeBytes(signature);
183
+ }
137
184
  getBuffer(clear = true) {
138
185
  const buf = new Uint8Array(this.buffer.byteLength);
139
186
  for (let i = 0; i < this.buffer.byteLength; i++) {
@@ -179,6 +226,15 @@ export class BinaryWriter {
179
226
  this.writeU256(value, be);
180
227
  }
181
228
  }
229
+ writeExtendedAddressMapU256(map, be = true) {
230
+ if (map.size > 65535)
231
+ throw new Error('Map size is too large');
232
+ this.writeU16(map.size, be);
233
+ for (const [key, value] of map.entries()) {
234
+ this.writeExtendedAddress(key);
235
+ this.writeU256(value, be);
236
+ }
237
+ }
182
238
  writeBytesWithLength(value) {
183
239
  this.writeU32(value.length);
184
240
  this.writeBytes(value);
@@ -200,6 +256,15 @@ export class BinaryWriter {
200
256
  this.writeAddress(value[i]);
201
257
  }
202
258
  }
259
+ writeExtendedAddressArray(value) {
260
+ if (value.length > 65535)
261
+ throw new Error('Array size is too large');
262
+ this.allocSafe(U16_BYTE_LENGTH + value.length * EXTENDED_ADDRESS_BYTE_LENGTH);
263
+ this.writeU16(value.length);
264
+ for (let i = 0; i < value.length; i++) {
265
+ this.writeExtendedAddress(value[i]);
266
+ }
267
+ }
203
268
  writeU32Array(value, be = true) {
204
269
  if (value.length > 65535)
205
270
  throw new Error('Array size is too large');
@@ -1,6 +1,8 @@
1
1
  import { Address } from '../keypair/Address.js';
2
2
  export declare class ExtendedAddressMap<V> {
3
- private items;
3
+ private indexMap;
4
+ private _keys;
5
+ private _values;
4
6
  constructor(iterable?: ReadonlyArray<readonly [Address, V]> | null);
5
7
  get size(): number;
6
8
  set(key: Address, value: V): this;
@@ -1,8 +1,9 @@
1
- import { Address } from '../keypair/Address.js';
2
1
  import { FastMap } from './FastMap.js';
3
2
  export class ExtendedAddressMap {
4
3
  constructor(iterable) {
5
- this.items = new FastMap();
4
+ this._keys = [];
5
+ this._values = [];
6
+ this.indexMap = new FastMap();
6
7
  if (iterable) {
7
8
  for (const [key, value] of iterable) {
8
9
  this.set(key, value);
@@ -10,48 +11,74 @@ export class ExtendedAddressMap {
10
11
  }
11
12
  }
12
13
  get size() {
13
- return this.items.size;
14
+ return this._keys.length;
14
15
  }
15
16
  set(key, value) {
16
17
  const keyBigInt = key.tweakedToBigInt();
17
- this.items.set(keyBigInt, value);
18
+ const existingIndex = this.indexMap.get(keyBigInt);
19
+ if (existingIndex !== undefined) {
20
+ this._values[existingIndex] = value;
21
+ }
22
+ else {
23
+ const newIndex = this._keys.length;
24
+ this._keys.push(key);
25
+ this._values.push(value);
26
+ this.indexMap.set(keyBigInt, newIndex);
27
+ }
18
28
  return this;
19
29
  }
20
30
  get(key) {
21
- return this.items.get(key.tweakedToBigInt());
31
+ const keyBigInt = key.tweakedToBigInt();
32
+ const index = this.indexMap.get(keyBigInt);
33
+ if (index === undefined) {
34
+ return undefined;
35
+ }
36
+ return this._values[index];
22
37
  }
23
38
  has(key) {
24
- return this.items.has(key.tweakedToBigInt());
39
+ return this.indexMap.has(key.tweakedToBigInt());
25
40
  }
26
41
  delete(key) {
27
42
  const keyBigInt = key.tweakedToBigInt();
28
- return this.items.delete(keyBigInt);
43
+ const index = this.indexMap.get(keyBigInt);
44
+ if (index === undefined) {
45
+ return false;
46
+ }
47
+ this._keys.splice(index, 1);
48
+ this._values.splice(index, 1);
49
+ this.indexMap.clear();
50
+ for (let i = 0; i < this._keys.length; i++) {
51
+ this.indexMap.set(this._keys[i].tweakedToBigInt(), i);
52
+ }
53
+ return true;
29
54
  }
30
55
  clear() {
31
- this.items.clear();
56
+ this.indexMap.clear();
57
+ this._keys = [];
58
+ this._values = [];
32
59
  }
33
60
  indexOf(address) {
34
- return this.items.indexOf(address.tweakedToBigInt());
61
+ const index = this.indexMap.get(address.tweakedToBigInt());
62
+ return index !== undefined ? index : -1;
35
63
  }
36
64
  *entries() {
37
- for (const [keyBigInt, value] of this.items.entries()) {
38
- yield [Address.fromBigInt(keyBigInt), value];
65
+ for (let i = 0; i < this._keys.length; i++) {
66
+ yield [this._keys[i], this._values[i]];
39
67
  }
40
68
  }
41
69
  *keys() {
42
- for (const keyBigInt of this.items.keys()) {
43
- yield Address.fromBigInt(keyBigInt);
70
+ for (const key of this._keys) {
71
+ yield key;
44
72
  }
45
73
  }
46
74
  *values() {
47
- for (const value of this.items.values()) {
75
+ for (const value of this._values) {
48
76
  yield value;
49
77
  }
50
78
  }
51
79
  forEach(callback, thisArg) {
52
- for (const [keyBigInt, value] of this.items.entries()) {
53
- const key = Address.fromBigInt(0n, keyBigInt);
54
- callback.call(thisArg, value, key, this);
80
+ for (let i = 0; i < this._keys.length; i++) {
81
+ callback.call(thisArg, this._values[i], this._keys[i], this);
55
82
  }
56
83
  }
57
84
  *[Symbol.iterator]() {
@@ -50,6 +50,7 @@ export declare class Address extends Uint8Array {
50
50
  p2op(network: Network): string;
51
51
  toTweakedHybridPublicKeyHex(): string;
52
52
  toTweakedHybridPublicKeyBuffer(): Buffer;
53
+ private ensureTweakedUncompressed;
53
54
  private setMldsaKey;
54
55
  private ensureLegacyProcessed;
55
56
  private autoFormat;
@@ -121,7 +121,7 @@ export class Address extends Uint8Array {
121
121
  }
122
122
  static fromBigInt(value, tweakedValue) {
123
123
  const address = Address.bigintToUint8Array(value);
124
- const legacyAddress = tweakedValue ? Address.bigintToUint8Array(tweakedValue) : undefined;
124
+ const legacyAddress = tweakedValue !== undefined ? Address.bigintToUint8Array(tweakedValue) : undefined;
125
125
  return new Address(address, legacyAddress);
126
126
  }
127
127
  static fromUint64Array(w0, w1, w2, w3) {
@@ -389,6 +389,7 @@ export class Address extends Uint8Array {
389
389
  }
390
390
  toTweakedHybridPublicKeyHex() {
391
391
  this.ensureLegacyProcessed();
392
+ this.ensureTweakedUncompressed();
392
393
  if (!__classPrivateFieldGet(this, _Address_tweakedUncompressed, "f")) {
393
394
  throw new Error('Legacy public key not set');
394
395
  }
@@ -396,11 +397,28 @@ export class Address extends Uint8Array {
396
397
  }
397
398
  toTweakedHybridPublicKeyBuffer() {
398
399
  this.ensureLegacyProcessed();
400
+ this.ensureTweakedUncompressed();
399
401
  if (!__classPrivateFieldGet(this, _Address_tweakedUncompressed, "f")) {
400
402
  throw new Error('Legacy public key not set');
401
403
  }
402
404
  return __classPrivateFieldGet(this, _Address_tweakedUncompressed, "f");
403
405
  }
406
+ ensureTweakedUncompressed() {
407
+ if (__classPrivateFieldGet(this, _Address_tweakedUncompressed, "f"))
408
+ return;
409
+ const key = __classPrivateFieldGet(this, _Address_legacyPublicKey, "f");
410
+ if (!key)
411
+ return;
412
+ if (key.length === ADDRESS_BYTE_LENGTH && !__classPrivateFieldGet(this, _Address_originalPublicKey, "f")) {
413
+ try {
414
+ const buf = Buffer.alloc(ADDRESS_BYTE_LENGTH);
415
+ buf.set(key);
416
+ __classPrivateFieldSet(this, _Address_tweakedUncompressed, ContractAddress.generateHybridKeyFromHash(buf), "f");
417
+ }
418
+ catch {
419
+ }
420
+ }
421
+ }
404
422
  setMldsaKey(mldsaPublicKey) {
405
423
  if (mldsaPublicKey.length === ADDRESS_BYTE_LENGTH) {
406
424
  const buf = new Uint8Array(ADDRESS_BYTE_LENGTH);
@@ -429,9 +447,6 @@ export class Address extends Uint8Array {
429
447
  if (!pending)
430
448
  return;
431
449
  if (pending.length === ADDRESS_BYTE_LENGTH) {
432
- const buf = Buffer.alloc(ADDRESS_BYTE_LENGTH);
433
- buf.set(pending);
434
- __classPrivateFieldSet(this, _Address_tweakedUncompressed, ContractAddress.generateHybridKeyFromHash(buf), "f");
435
450
  __classPrivateFieldSet(this, _Address_legacyPublicKey, pending, "f");
436
451
  }
437
452
  else {
@@ -109,7 +109,7 @@ export class MultiSignTransaction extends TransactionBuilder {
109
109
  }
110
110
  input.tapScriptSig = (input.tapScriptSig || []).concat(partialSignatures);
111
111
  }
112
- delete input.finalScriptWitness;
112
+ Reflect.deleteProperty(input, 'finalScriptWitness');
113
113
  const signHashTypes = MultiSignTransaction.signHashTypesArray
114
114
  ? [MultiSignTransaction.calculateSignHash(MultiSignTransaction.signHashTypesArray)]
115
115
  : [];
@@ -172,7 +172,7 @@ export class MultiSignTransaction extends TransactionBuilder {
172
172
  ];
173
173
  input.tapScriptSig = MultiSignTransaction.dedupeSignatures(input.tapScriptSig || [], partialSignatures);
174
174
  }
175
- delete input.finalScriptWitness;
175
+ Reflect.deleteProperty(input, 'finalScriptWitness');
176
176
  psbt.finalizeInput(i, (inputIndex, input) => {
177
177
  return MultiSignTransaction.partialFinalizer(inputIndex, input, [], orderedPubKeys[i - startIndex], isFinal);
178
178
  });