@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.
@@ -5,6 +5,7 @@ export declare class Address extends Uint8Array {
5
5
  get originalPublicKey(): Uint8Array | undefined;
6
6
  private get keyPair();
7
7
  static dead(): Address;
8
+ static zero(): Address;
8
9
  static fromString(pubKey: string): Address;
9
10
  static wrap(bytes: ArrayLike<number>): Address;
10
11
  static uncompressedToCompressed(publicKey: ArrayLike<number>): Buffer;
@@ -11,6 +11,6 @@ export declare abstract class CustomKeypair implements Signer {
11
11
  abstract getPublicKey(): Buffer;
12
12
  abstract sign(hash: Buffer, lowR?: boolean): Buffer;
13
13
  abstract signSchnorr(hash: Buffer): Buffer;
14
- abstract verify(hash: Buffer, signature: Buffer): boolean;
14
+ abstract verify(hash: Buffer, signature: Buffer): boolean | Buffer;
15
15
  abstract init(): Promise<void>;
16
16
  }
@@ -1 +1 @@
1
- export declare const version = "1.2.14";
1
+ export declare const version = "1.3.0";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.14';
1
+ export const version = '1.3.0';
@@ -10,7 +10,6 @@ export declare enum ABIDataTypes {
10
10
  ADDRESS = "ADDRESS",
11
11
  STRING = "STRING",
12
12
  BYTES32 = "BYTES32",
13
- TUPLE = "TUPLE",
14
13
  BYTES = "BYTES",
15
14
  ADDRESS_UINT256_TUPLE = "ADDRESS_UINT256_TUPLE",
16
15
  ARRAY_OF_ADDRESSES = "ARRAY_OF_ADDRESSES",
@@ -14,7 +14,6 @@ export var ABIDataTypes;
14
14
  ABIDataTypes["ADDRESS"] = "ADDRESS";
15
15
  ABIDataTypes["STRING"] = "STRING";
16
16
  ABIDataTypes["BYTES32"] = "BYTES32";
17
- ABIDataTypes["TUPLE"] = "TUPLE";
18
17
  ABIDataTypes["BYTES"] = "BYTES";
19
18
  ABIDataTypes["ADDRESS_UINT256_TUPLE"] = "ADDRESS_UINT256_TUPLE";
20
19
  ABIDataTypes["ARRAY_OF_ADDRESSES"] = "ARRAY_OF_ADDRESSES";
@@ -64,9 +63,6 @@ export class ABICoder {
64
63
  case ABIDataTypes.INT128:
65
64
  result.push(byteReader.readI128());
66
65
  break;
67
- case ABIDataTypes.TUPLE:
68
- result.push(byteReader.readTuple());
69
- break;
70
66
  case ABIDataTypes.ADDRESS_UINT256_TUPLE:
71
67
  result.push(byteReader.readAddressValueTuple());
72
68
  break;
@@ -9,32 +9,33 @@ export declare class BinaryReader {
9
9
  static bigintCompare(a: bigint, b: bigint): number;
10
10
  static numberCompare(a: number, b: number): number;
11
11
  setBuffer(bytes: BufferLike): void;
12
- readAddressArray(): Address[];
13
- readU256Array(): bigint[];
14
- readU128Array(): bigint[];
15
- readU64Array(): bigint[];
16
- readU32Array(): u32[];
17
- readU16Array(): u16[];
18
- readU8Array(): u8[];
19
- readStringArray(): string[];
20
- readBytesArray(): Uint8Array[];
21
- readBytesWithLength(maxLength?: number): Uint8Array;
22
- readTuple(): bigint[];
23
12
  readU8(): u8;
24
- readU16(): u16;
25
- readU32(le?: boolean): u32;
26
- readU64(): bigint;
27
- readAddressValueTuple(): AddressMap<bigint>;
28
- readU128(): bigint;
29
- readU256(): bigint;
30
- readI128(): bigint;
13
+ readU16(be?: boolean): u16;
14
+ readU32(be?: boolean): u32;
15
+ readU64(be?: boolean): bigint;
16
+ readU128(be?: boolean): bigint;
17
+ readU256(be?: boolean): bigint;
18
+ readI128(be?: boolean): bigint;
19
+ readBoolean(): boolean;
20
+ readSelector(): Selector;
31
21
  readBytes(length: u32, zeroStop?: boolean): Uint8Array;
32
22
  readString(length: u16): string;
33
- readSelector(): Selector;
34
- readStringWithLength(): string;
35
- readBoolean(): boolean;
23
+ readStringWithLength(be?: boolean): string;
36
24
  readAddress(): Address;
25
+ readBytesWithLength(maxLength?: number, be?: boolean): Uint8Array;
26
+ readAddressArray(be?: boolean): Address[];
27
+ readU256Array(be?: boolean): bigint[];
28
+ readU128Array(be?: boolean): bigint[];
29
+ readU64Array(be?: boolean): bigint[];
30
+ readU32Array(be?: boolean): u32[];
31
+ readU16Array(be?: boolean): u16[];
32
+ readU8Array(): u8[];
33
+ readStringArray(be?: boolean): string[];
34
+ readBytesArray(be?: boolean): Uint8Array[];
35
+ readAddressValueTuple(be?: boolean): AddressMap<bigint>;
37
36
  getOffset(): u16;
38
37
  setOffset(offset: u16): void;
39
38
  verifyEnd(size: i32): void;
39
+ private reverseBytes;
40
+ private toHexString;
40
41
  }
@@ -27,181 +27,184 @@ export class BinaryReader {
27
27
  this.buffer = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
28
28
  this.currentOffset = 0;
29
29
  }
30
- readAddressArray() {
31
- const length = this.readU16();
32
- const result = new Array(length);
30
+ readU8() {
31
+ this.verifyEnd(this.currentOffset + U8_BYTE_LENGTH);
32
+ const value = this.buffer.getUint8(this.currentOffset);
33
+ this.currentOffset += U8_BYTE_LENGTH;
34
+ return value;
35
+ }
36
+ readU16(be = true) {
37
+ this.verifyEnd(this.currentOffset + U16_BYTE_LENGTH);
38
+ const value = this.buffer.getUint16(this.currentOffset, !be);
39
+ this.currentOffset += U16_BYTE_LENGTH;
40
+ return value;
41
+ }
42
+ readU32(be = true) {
43
+ this.verifyEnd(this.currentOffset + U32_BYTE_LENGTH);
44
+ const value = this.buffer.getUint32(this.currentOffset, !be);
45
+ this.currentOffset += U32_BYTE_LENGTH;
46
+ return value;
47
+ }
48
+ readU64(be = true) {
49
+ this.verifyEnd(this.currentOffset + U64_BYTE_LENGTH);
50
+ const value = this.buffer.getBigUint64(this.currentOffset, !be);
51
+ this.currentOffset += U64_BYTE_LENGTH;
52
+ return value;
53
+ }
54
+ readU128(be = true) {
55
+ const raw = this.readBytes(U128_BYTE_LENGTH);
56
+ let bytes = raw;
57
+ if (!be) {
58
+ bytes = this.reverseBytes(raw);
59
+ }
60
+ return BigInt('0x' + this.toHexString(bytes));
61
+ }
62
+ readU256(be = true) {
63
+ const raw = this.readBytes(U256_BYTE_LENGTH);
64
+ let bytes = raw;
65
+ if (!be) {
66
+ bytes = this.reverseBytes(raw);
67
+ }
68
+ return BigInt('0x' + this.toHexString(bytes));
69
+ }
70
+ readI128(be = true) {
71
+ const raw = this.readBytes(I128_BYTE_LENGTH);
72
+ let bytes = raw;
73
+ if (!be) {
74
+ bytes = this.reverseBytes(raw);
75
+ }
76
+ let value = BigInt('0x' + this.toHexString(bytes));
77
+ const signBitMask = 0x80;
78
+ if (bytes[0] & signBitMask) {
79
+ const twoTo128 = BigInt(1) << BigInt(128);
80
+ value = value - twoTo128;
81
+ }
82
+ return value;
83
+ }
84
+ readBoolean() {
85
+ return this.readU8() !== 0;
86
+ }
87
+ readSelector() {
88
+ return this.readU32(true);
89
+ }
90
+ readBytes(length, zeroStop = false) {
91
+ this.verifyEnd(this.currentOffset + length);
92
+ let bytes = new Uint8Array(length);
33
93
  for (let i = 0; i < length; i++) {
34
- result[i] = this.readAddress();
94
+ const b = this.buffer.getUint8(this.currentOffset++);
95
+ if (zeroStop && b === 0) {
96
+ bytes = bytes.subarray(0, i);
97
+ break;
98
+ }
99
+ bytes[i] = b;
35
100
  }
36
- return result;
101
+ return bytes;
102
+ }
103
+ readString(length) {
104
+ const textDecoder = new TextDecoder();
105
+ const bytes = this.readBytes(length, false);
106
+ return textDecoder.decode(bytes);
37
107
  }
38
- readU256Array() {
39
- const length = this.readU16();
108
+ readStringWithLength(be = true) {
109
+ const length = this.readU16(be);
110
+ return this.readString(length);
111
+ }
112
+ readAddress() {
113
+ const bytes = Array.from(this.readBytes(ADDRESS_BYTE_LENGTH));
114
+ return new Address(bytes);
115
+ }
116
+ readBytesWithLength(maxLength = 0, be = true) {
117
+ const length = this.readU32(be);
118
+ if (maxLength > 0 && length > maxLength) {
119
+ throw new Error('Data length exceeds maximum length.');
120
+ }
121
+ return this.readBytes(length);
122
+ }
123
+ readAddressArray(be = true) {
124
+ const length = this.readU16(be);
40
125
  const result = new Array(length);
41
126
  for (let i = 0; i < length; i++) {
42
- result[i] = this.readU256();
127
+ result[i] = this.readAddress();
43
128
  }
44
129
  return result;
45
130
  }
46
- readU128Array() {
47
- const length = this.readU16();
131
+ readU256Array(be = true) {
132
+ const length = this.readU16(be);
48
133
  const result = new Array(length);
49
134
  for (let i = 0; i < length; i++) {
50
- result[i] = this.readU128();
135
+ result[i] = this.readU256(be);
51
136
  }
52
137
  return result;
53
138
  }
54
- readU64Array() {
55
- const length = this.readU16();
139
+ readU128Array(be = true) {
140
+ const length = this.readU16(be);
56
141
  const result = new Array(length);
57
142
  for (let i = 0; i < length; i++) {
58
- result[i] = this.readU64();
143
+ result[i] = this.readU128(be);
59
144
  }
60
145
  return result;
61
146
  }
62
- readU32Array() {
63
- const length = this.readU16();
147
+ readU64Array(be = true) {
148
+ const length = this.readU16(be);
64
149
  const result = new Array(length);
65
150
  for (let i = 0; i < length; i++) {
66
- result[i] = this.readU32();
151
+ result[i] = this.readU64(be);
67
152
  }
68
153
  return result;
69
154
  }
70
- readU16Array() {
71
- const length = this.readU16();
155
+ readU32Array(be = true) {
156
+ const length = this.readU16(be);
72
157
  const result = new Array(length);
73
158
  for (let i = 0; i < length; i++) {
74
- result[i] = this.readU16();
159
+ result[i] = this.readU32(be);
75
160
  }
76
161
  return result;
77
162
  }
78
- readU8Array() {
79
- const length = this.readU16();
163
+ readU16Array(be = true) {
164
+ const length = this.readU16(be);
80
165
  const result = new Array(length);
81
166
  for (let i = 0; i < length; i++) {
82
- result[i] = this.readU8();
167
+ result[i] = this.readU16(be);
83
168
  }
84
169
  return result;
85
170
  }
86
- readStringArray() {
87
- const length = this.readU16();
171
+ readU8Array() {
172
+ const length = this.readU16(true);
88
173
  const result = new Array(length);
89
174
  for (let i = 0; i < length; i++) {
90
- result[i] = this.readStringWithLength();
175
+ result[i] = this.readU8();
91
176
  }
92
177
  return result;
93
178
  }
94
- readBytesArray() {
95
- const length = this.readU16();
179
+ readStringArray(be = true) {
180
+ const length = this.readU16(be);
96
181
  const result = new Array(length);
97
182
  for (let i = 0; i < length; i++) {
98
- result[i] = this.readBytesWithLength();
183
+ result[i] = this.readStringWithLength(be);
99
184
  }
100
185
  return result;
101
186
  }
102
- readBytesWithLength(maxLength = 0) {
103
- const length = this.readU32();
104
- if (maxLength > 0 && length > maxLength) {
105
- throw new Error('Data length exceeds maximum length.');
106
- }
107
- return this.readBytes(length);
108
- }
109
- readTuple() {
110
- const length = this.readU32();
187
+ readBytesArray(be = true) {
188
+ const length = this.readU16(be);
111
189
  const result = new Array(length);
112
190
  for (let i = 0; i < length; i++) {
113
- result[i] = this.readU256();
191
+ result[i] = this.readBytesWithLength(0, be);
114
192
  }
115
193
  return result;
116
194
  }
117
- readU8() {
118
- this.verifyEnd(this.currentOffset + U8_BYTE_LENGTH);
119
- const value = this.buffer.getUint8(this.currentOffset);
120
- this.currentOffset += U8_BYTE_LENGTH;
121
- return value;
122
- }
123
- readU16() {
124
- this.verifyEnd(this.currentOffset + U16_BYTE_LENGTH);
125
- const value = this.buffer.getUint16(this.currentOffset, true);
126
- this.currentOffset += U16_BYTE_LENGTH;
127
- return value;
128
- }
129
- readU32(le = true) {
130
- this.verifyEnd(this.currentOffset + U32_BYTE_LENGTH);
131
- const value = this.buffer.getUint32(this.currentOffset, le);
132
- this.currentOffset += U32_BYTE_LENGTH;
133
- return value;
134
- }
135
- readU64() {
136
- this.verifyEnd(this.currentOffset + U64_BYTE_LENGTH);
137
- const value = this.buffer.getBigUint64(this.currentOffset, true);
138
- this.currentOffset += U64_BYTE_LENGTH;
139
- return value;
140
- }
141
- readAddressValueTuple() {
142
- const length = this.readU16();
195
+ readAddressValueTuple(be = true) {
196
+ const length = this.readU16(be);
143
197
  const result = new AddressMap();
144
198
  for (let i = 0; i < length; i++) {
145
199
  const address = this.readAddress();
146
- const value = this.readU256();
147
- if (result.has(address))
200
+ const value = this.readU256(be);
201
+ if (result.has(address)) {
148
202
  throw new Error('Duplicate address found in map');
203
+ }
149
204
  result.set(address, value);
150
205
  }
151
206
  return result;
152
207
  }
153
- readU128() {
154
- const next16Bytes = this.readBytes(U128_BYTE_LENGTH);
155
- return BigInt('0x' + next16Bytes.reduce((acc, byte) => acc + byte.toString(16).padStart(2, '0'), ''));
156
- }
157
- readU256() {
158
- const next32Bytes = this.readBytes(U256_BYTE_LENGTH);
159
- return BigInt('0x' + next32Bytes.reduce((acc, byte) => acc + byte.toString(16).padStart(2, '0'), ''));
160
- }
161
- readI128() {
162
- const next16Bytes = this.readBytes(I128_BYTE_LENGTH);
163
- let value = BigInt('0x' + next16Bytes.reduce((acc, byte) => acc + byte.toString(16).padStart(2, '0'), ''));
164
- if (next16Bytes[0] & 0x80) {
165
- const mask = (BigInt(1) << BigInt(128)) - BigInt(1);
166
- value = (value ^ mask) + BigInt(1);
167
- value = -value;
168
- }
169
- return value;
170
- }
171
- readBytes(length, zeroStop = false) {
172
- let bytes = new Uint8Array(length);
173
- for (let i = 0; i < length; i++) {
174
- const byte = this.readU8();
175
- if (zeroStop && byte === 0) {
176
- bytes = bytes.slice(0, i);
177
- break;
178
- }
179
- bytes[i] = byte;
180
- }
181
- return bytes;
182
- }
183
- readString(length) {
184
- const textDecoder = new TextDecoder();
185
- const bytes = this.readBytes(length, true);
186
- return textDecoder.decode(bytes);
187
- }
188
- readSelector() {
189
- return this.readU32(false);
190
- }
191
- readStringWithLength() {
192
- const length = this.readU16();
193
- return this.readString(length);
194
- }
195
- readBoolean() {
196
- return this.readU8() !== 0;
197
- }
198
- readAddress() {
199
- const bytes = new Array(ADDRESS_BYTE_LENGTH);
200
- for (let i = 0; i < ADDRESS_BYTE_LENGTH; i++) {
201
- bytes[i] = this.readU8();
202
- }
203
- return new Address(bytes);
204
- }
205
208
  getOffset() {
206
209
  return this.currentOffset;
207
210
  }
@@ -209,8 +212,18 @@ export class BinaryReader {
209
212
  this.currentOffset = offset;
210
213
  }
211
214
  verifyEnd(size) {
212
- if (this.currentOffset > this.buffer.byteLength) {
213
- throw new Error(`Expected to read ${size} bytes but read ${this.currentOffset} bytes`);
215
+ if (size > this.buffer.byteLength) {
216
+ throw new Error(`Attempt to read beyond buffer length: requested up to byte offset ${size}, but buffer is only ${this.buffer.byteLength} bytes.`);
214
217
  }
215
218
  }
219
+ reverseBytes(bytes) {
220
+ const out = new Uint8Array(bytes.length);
221
+ for (let i = 0; i < bytes.length; i++) {
222
+ out[i] = bytes[bytes.length - 1 - i];
223
+ }
224
+ return out;
225
+ }
226
+ toHexString(bytes) {
227
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
228
+ }
216
229
  }
@@ -7,42 +7,36 @@ export declare class BinaryWriter {
7
7
  private buffer;
8
8
  constructor(length?: number);
9
9
  writeU8(value: u8): void;
10
- writeU16(value: u16): void;
11
- writeU32(value: u32, le?: boolean): void;
12
- writeU64(value: u64): void;
10
+ writeU16(value: u16, be?: boolean): void;
11
+ writeU32(value: u32, be?: boolean): void;
12
+ writeU64(value: u64, be?: boolean): void;
13
13
  writeSelector(value: Selector): void;
14
14
  writeBoolean(value: boolean): void;
15
- writeI128(bigIntValue: bigint): void;
16
- writeU256(bigIntValue: bigint): void;
17
- writeU128(bigIntValue: bigint): void;
15
+ writeI128(bigIntValue: bigint, be?: boolean): void;
16
+ writeU256(bigIntValue: bigint, be?: boolean): void;
17
+ writeU128(bigIntValue: bigint, be?: boolean): void;
18
18
  writeBytes(value: Uint8Array | Buffer): void;
19
19
  writeString(value: string): void;
20
20
  writeAddress(value: Address): void;
21
21
  writeStringWithLength(value: string): void;
22
22
  getBuffer(clear?: boolean): Uint8Array;
23
23
  reset(): void;
24
- writeTuple(values: bigint[]): void;
25
24
  toBytesReader(): BinaryReader;
26
25
  getOffset(): u32;
27
26
  setOffset(offset: u32): void;
28
27
  clear(): void;
29
28
  allocSafe(size: u32): void;
30
- writeABISelector(name: string, selector: Selector): void;
31
- writeAddressValueTupleMap(map: AddressMap<bigint>): void;
32
- writeLimitedAddressBytesMap(map: AddressMap<Uint8Array[]>): void;
29
+ writeAddressValueTuple(map: AddressMap<bigint>, be?: boolean): void;
33
30
  writeBytesWithLength(value: Uint8Array): void;
34
31
  writeAddressArray(value: Address[]): void;
35
- writeU32Array(value: u32[]): void;
36
- writeU256Array(value: bigint[]): void;
37
- writeU128Array(value: bigint[]): void;
32
+ writeU32Array(value: u32[], be?: boolean): void;
33
+ writeU256Array(value: bigint[], be?: boolean): void;
34
+ writeU128Array(value: bigint[], be?: boolean): void;
38
35
  writeStringArray(value: string[]): void;
39
- writeU16Array(value: u16[]): void;
36
+ writeU16Array(value: u16[], be?: boolean): void;
40
37
  writeU8Array(value: u8[]): void;
41
- writeU64Array(value: bigint[]): void;
38
+ writeU64Array(value: bigint[], be?: boolean): void;
42
39
  writeBytesArray(value: Uint8Array[]): void;
43
- writeSelectorArray(value: Selector[]): void;
44
- private getChecksum;
45
- private writeMethodSelectorMap;
46
40
  private verifyAddress;
47
41
  private resize;
48
42
  private getDefaultBuffer;