@btc-vision/transaction 1.2.15 → 1.3.1

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;
@@ -1 +1 @@
1
- export declare const version = "1.2.15";
1
+ export declare const version = "1.3.0";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.15';
1
+ export const version = '1.3.0';
@@ -9,31 +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
12
  readU8(): u8;
23
- readU16(): u16;
24
- readU32(le?: boolean): u32;
25
- readU64(): bigint;
26
- readAddressValueTuple(): AddressMap<bigint>;
27
- readU128(): bigint;
28
- readU256(): bigint;
29
- 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;
30
21
  readBytes(length: u32, zeroStop?: boolean): Uint8Array;
31
22
  readString(length: u16): string;
32
- readSelector(): Selector;
33
- readStringWithLength(): string;
34
- readBoolean(): boolean;
23
+ readStringWithLength(be?: boolean): string;
35
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>;
36
36
  getOffset(): u16;
37
37
  setOffset(offset: u16): void;
38
38
  verifyEnd(size: i32): void;
39
+ private reverseBytes;
40
+ private toHexString;
39
41
  }
@@ -27,173 +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();
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);
93
+ for (let i = 0; i < length; i++) {
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;
100
+ }
101
+ return bytes;
102
+ }
103
+ readString(length) {
104
+ const textDecoder = new TextDecoder();
105
+ const bytes = this.readBytes(length, false);
106
+ return textDecoder.decode(bytes);
107
+ }
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);
32
125
  const result = new Array(length);
33
126
  for (let i = 0; i < length; i++) {
34
127
  result[i] = this.readAddress();
35
128
  }
36
129
  return result;
37
130
  }
38
- readU256Array() {
39
- const length = this.readU16();
131
+ readU256Array(be = true) {
132
+ const length = this.readU16(be);
40
133
  const result = new Array(length);
41
134
  for (let i = 0; i < length; i++) {
42
- result[i] = this.readU256();
135
+ result[i] = this.readU256(be);
43
136
  }
44
137
  return result;
45
138
  }
46
- readU128Array() {
47
- const length = this.readU16();
139
+ readU128Array(be = true) {
140
+ const length = this.readU16(be);
48
141
  const result = new Array(length);
49
142
  for (let i = 0; i < length; i++) {
50
- result[i] = this.readU128();
143
+ result[i] = this.readU128(be);
51
144
  }
52
145
  return result;
53
146
  }
54
- readU64Array() {
55
- const length = this.readU16();
147
+ readU64Array(be = true) {
148
+ const length = this.readU16(be);
56
149
  const result = new Array(length);
57
150
  for (let i = 0; i < length; i++) {
58
- result[i] = this.readU64();
151
+ result[i] = this.readU64(be);
59
152
  }
60
153
  return result;
61
154
  }
62
- readU32Array() {
63
- const length = this.readU16();
155
+ readU32Array(be = true) {
156
+ const length = this.readU16(be);
64
157
  const result = new Array(length);
65
158
  for (let i = 0; i < length; i++) {
66
- result[i] = this.readU32();
159
+ result[i] = this.readU32(be);
67
160
  }
68
161
  return result;
69
162
  }
70
- readU16Array() {
71
- const length = this.readU16();
163
+ readU16Array(be = true) {
164
+ const length = this.readU16(be);
72
165
  const result = new Array(length);
73
166
  for (let i = 0; i < length; i++) {
74
- result[i] = this.readU16();
167
+ result[i] = this.readU16(be);
75
168
  }
76
169
  return result;
77
170
  }
78
171
  readU8Array() {
79
- const length = this.readU16();
172
+ const length = this.readU16(true);
80
173
  const result = new Array(length);
81
174
  for (let i = 0; i < length; i++) {
82
175
  result[i] = this.readU8();
83
176
  }
84
177
  return result;
85
178
  }
86
- readStringArray() {
87
- const length = this.readU16();
179
+ readStringArray(be = true) {
180
+ const length = this.readU16(be);
88
181
  const result = new Array(length);
89
182
  for (let i = 0; i < length; i++) {
90
- result[i] = this.readStringWithLength();
183
+ result[i] = this.readStringWithLength(be);
91
184
  }
92
185
  return result;
93
186
  }
94
- readBytesArray() {
95
- const length = this.readU16();
187
+ readBytesArray(be = true) {
188
+ const length = this.readU16(be);
96
189
  const result = new Array(length);
97
190
  for (let i = 0; i < length; i++) {
98
- result[i] = this.readBytesWithLength();
191
+ result[i] = this.readBytesWithLength(0, be);
99
192
  }
100
193
  return result;
101
194
  }
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
- readU8() {
110
- this.verifyEnd(this.currentOffset + U8_BYTE_LENGTH);
111
- const value = this.buffer.getUint8(this.currentOffset);
112
- this.currentOffset += U8_BYTE_LENGTH;
113
- return value;
114
- }
115
- readU16() {
116
- this.verifyEnd(this.currentOffset + U16_BYTE_LENGTH);
117
- const value = this.buffer.getUint16(this.currentOffset, true);
118
- this.currentOffset += U16_BYTE_LENGTH;
119
- return value;
120
- }
121
- readU32(le = true) {
122
- this.verifyEnd(this.currentOffset + U32_BYTE_LENGTH);
123
- const value = this.buffer.getUint32(this.currentOffset, le);
124
- this.currentOffset += U32_BYTE_LENGTH;
125
- return value;
126
- }
127
- readU64() {
128
- this.verifyEnd(this.currentOffset + U64_BYTE_LENGTH);
129
- const value = this.buffer.getBigUint64(this.currentOffset, true);
130
- this.currentOffset += U64_BYTE_LENGTH;
131
- return value;
132
- }
133
- readAddressValueTuple() {
134
- const length = this.readU16();
195
+ readAddressValueTuple(be = true) {
196
+ const length = this.readU16(be);
135
197
  const result = new AddressMap();
136
198
  for (let i = 0; i < length; i++) {
137
199
  const address = this.readAddress();
138
- const value = this.readU256();
139
- if (result.has(address))
200
+ const value = this.readU256(be);
201
+ if (result.has(address)) {
140
202
  throw new Error('Duplicate address found in map');
203
+ }
141
204
  result.set(address, value);
142
205
  }
143
206
  return result;
144
207
  }
145
- readU128() {
146
- const next16Bytes = this.readBytes(U128_BYTE_LENGTH);
147
- return BigInt('0x' + next16Bytes.reduce((acc, byte) => acc + byte.toString(16).padStart(2, '0'), ''));
148
- }
149
- readU256() {
150
- const next32Bytes = this.readBytes(U256_BYTE_LENGTH);
151
- return BigInt('0x' + next32Bytes.reduce((acc, byte) => acc + byte.toString(16).padStart(2, '0'), ''));
152
- }
153
- readI128() {
154
- const next16Bytes = this.readBytes(I128_BYTE_LENGTH);
155
- let value = BigInt('0x' + next16Bytes.reduce((acc, byte) => acc + byte.toString(16).padStart(2, '0'), ''));
156
- if (next16Bytes[0] & 0x80) {
157
- const mask = (BigInt(1) << BigInt(128)) - BigInt(1);
158
- value = (value ^ mask) + BigInt(1);
159
- value = -value;
160
- }
161
- return value;
162
- }
163
- readBytes(length, zeroStop = false) {
164
- let bytes = new Uint8Array(length);
165
- for (let i = 0; i < length; i++) {
166
- const byte = this.readU8();
167
- if (zeroStop && byte === 0) {
168
- bytes = bytes.slice(0, i);
169
- break;
170
- }
171
- bytes[i] = byte;
172
- }
173
- return bytes;
174
- }
175
- readString(length) {
176
- const textDecoder = new TextDecoder();
177
- const bytes = this.readBytes(length, true);
178
- return textDecoder.decode(bytes);
179
- }
180
- readSelector() {
181
- return this.readU32(false);
182
- }
183
- readStringWithLength() {
184
- const length = this.readU16();
185
- return this.readString(length);
186
- }
187
- readBoolean() {
188
- return this.readU8() !== 0;
189
- }
190
- readAddress() {
191
- const bytes = new Array(ADDRESS_BYTE_LENGTH);
192
- for (let i = 0; i < ADDRESS_BYTE_LENGTH; i++) {
193
- bytes[i] = this.readU8();
194
- }
195
- return new Address(bytes);
196
- }
197
208
  getOffset() {
198
209
  return this.currentOffset;
199
210
  }
@@ -201,8 +212,18 @@ export class BinaryReader {
201
212
  this.currentOffset = offset;
202
213
  }
203
214
  verifyEnd(size) {
204
- if (this.currentOffset > this.buffer.byteLength) {
205
- 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.`);
206
217
  }
207
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
+ }
208
229
  }
@@ -7,14 +7,14 @@ 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;
@@ -26,22 +26,17 @@ export declare class BinaryWriter {
26
26
  setOffset(offset: u32): void;
27
27
  clear(): void;
28
28
  allocSafe(size: u32): void;
29
- writeABISelector(name: string, selector: Selector): void;
30
- writeAddressValueTuple(map: AddressMap<bigint>): void;
31
- writeLimitedAddressBytesMap(map: AddressMap<Uint8Array[]>): void;
29
+ writeAddressValueTuple(map: AddressMap<bigint>, be?: boolean): void;
32
30
  writeBytesWithLength(value: Uint8Array): void;
33
31
  writeAddressArray(value: Address[]): void;
34
- writeU32Array(value: u32[]): void;
35
- writeU256Array(value: bigint[]): void;
36
- 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;
37
35
  writeStringArray(value: string[]): void;
38
- writeU16Array(value: u16[]): void;
36
+ writeU16Array(value: u16[], be?: boolean): void;
39
37
  writeU8Array(value: u8[]): void;
40
- writeU64Array(value: bigint[]): void;
38
+ writeU64Array(value: bigint[], be?: boolean): void;
41
39
  writeBytesArray(value: Uint8Array[]): void;
42
- writeSelectorArray(value: Selector[]): void;
43
- private getChecksum;
44
- private writeMethodSelectorMap;
45
40
  private verifyAddress;
46
41
  private resize;
47
42
  private getDefaultBuffer;