@btc-vision/transaction 1.2.15 → 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.
@@ -12,34 +12,34 @@ export class BinaryWriter {
12
12
  this.allocSafe(U8_BYTE_LENGTH);
13
13
  this.buffer.setUint8(this.currentOffset++, value);
14
14
  }
15
- writeU16(value) {
15
+ writeU16(value, be = true) {
16
16
  if (value > 65535)
17
17
  throw new Error('u16 value is too large.');
18
18
  this.allocSafe(U16_BYTE_LENGTH);
19
- this.buffer.setUint16(this.currentOffset, value, true);
19
+ this.buffer.setUint16(this.currentOffset, value, !be);
20
20
  this.currentOffset += 2;
21
21
  }
22
- writeU32(value, le = true) {
22
+ writeU32(value, be = true) {
23
23
  if (value > 4294967295)
24
24
  throw new Error('u32 value is too large.');
25
25
  this.allocSafe(U32_BYTE_LENGTH);
26
- this.buffer.setUint32(this.currentOffset, value, le);
26
+ this.buffer.setUint32(this.currentOffset, value, !be);
27
27
  this.currentOffset += 4;
28
28
  }
29
- writeU64(value) {
29
+ writeU64(value, be = true) {
30
30
  if (value > 18446744073709551615n)
31
31
  throw new Error('u64 value is too large.');
32
32
  this.allocSafe(U64_BYTE_LENGTH);
33
- this.buffer.setBigUint64(this.currentOffset, value, true);
33
+ this.buffer.setBigUint64(this.currentOffset, value, !be);
34
34
  this.currentOffset += 8;
35
35
  }
36
36
  writeSelector(value) {
37
- this.writeU32(value, false);
37
+ this.writeU32(value, true);
38
38
  }
39
39
  writeBoolean(value) {
40
40
  this.writeU8(value ? 1 : 0);
41
41
  }
42
- writeI128(bigIntValue) {
42
+ writeI128(bigIntValue, be = true) {
43
43
  if (bigIntValue > 170141183460469231731687303715884105727n ||
44
44
  bigIntValue < -170141183460469231731687303715884105728n) {
45
45
  throw new Error('i128 value is too large.');
@@ -49,35 +49,57 @@ export class BinaryWriter {
49
49
  if (bytesToHex.byteLength !== I128_BYTE_LENGTH) {
50
50
  throw new Error(`Invalid i128 value: ${bigIntValue}`);
51
51
  }
52
- for (let i = 0; i < bytesToHex.byteLength; i++) {
53
- this.writeU8(bytesToHex[i]);
52
+ if (be) {
53
+ for (let i = 0; i < bytesToHex.byteLength; i++) {
54
+ this.writeU8(bytesToHex[i]);
55
+ }
56
+ }
57
+ else {
58
+ for (let i = bytesToHex.byteLength - 1; i >= 0; i--) {
59
+ this.writeU8(bytesToHex[i]);
60
+ }
54
61
  }
55
62
  }
56
- writeU256(bigIntValue) {
63
+ writeU256(bigIntValue, be = true) {
57
64
  if (bigIntValue >
58
- 115792089237316195423570985008687907853269984665640564039457584007913129639935n) {
59
- throw new Error('u256 value is too large.');
65
+ 115792089237316195423570985008687907853269984665640564039457584007913129639935n &&
66
+ bigIntValue < 0n) {
67
+ throw new Error('u256 value is too large or negative.');
60
68
  }
61
69
  this.allocSafe(U256_BYTE_LENGTH);
62
70
  const bytesToHex = BufferHelper.valueToUint8Array(bigIntValue);
63
71
  if (bytesToHex.byteLength !== U256_BYTE_LENGTH) {
64
72
  throw new Error(`Invalid u256 value: ${bigIntValue}`);
65
73
  }
66
- for (let i = 0; i < bytesToHex.byteLength; i++) {
67
- this.writeU8(bytesToHex[i]);
74
+ if (be) {
75
+ for (let i = 0; i < bytesToHex.byteLength; i++) {
76
+ this.writeU8(bytesToHex[i]);
77
+ }
78
+ }
79
+ else {
80
+ for (let i = bytesToHex.byteLength - 1; i >= 0; i--) {
81
+ this.writeU8(bytesToHex[i]);
82
+ }
68
83
  }
69
84
  }
70
- writeU128(bigIntValue) {
71
- if (bigIntValue > 340282366920938463463374607431768211455n) {
72
- throw new Error('u128 value is too large.');
85
+ writeU128(bigIntValue, be = true) {
86
+ if (bigIntValue > 340282366920938463463374607431768211455n && bigIntValue < 0n) {
87
+ throw new Error('u128 value is too large or negative.');
73
88
  }
74
89
  this.allocSafe(U128_BYTE_LENGTH);
75
90
  const bytesToHex = BufferHelper.valueToUint8Array(bigIntValue, U128_BYTE_LENGTH);
76
91
  if (bytesToHex.byteLength !== U128_BYTE_LENGTH) {
77
92
  throw new Error(`Invalid u128 value: ${bigIntValue}`);
78
93
  }
79
- for (let i = 0; i < bytesToHex.byteLength; i++) {
80
- this.writeU8(bytesToHex[i]);
94
+ if (be) {
95
+ for (let i = 0; i < bytesToHex.byteLength; i++) {
96
+ this.writeU8(bytesToHex[i]);
97
+ }
98
+ }
99
+ else {
100
+ for (let i = bytesToHex.byteLength - 1; i >= 0; i--) {
101
+ this.writeU8(bytesToHex[i]);
102
+ }
81
103
  }
82
104
  }
83
105
  writeBytes(value) {
@@ -132,14 +154,10 @@ export class BinaryWriter {
132
154
  this.resize(size);
133
155
  }
134
156
  }
135
- writeABISelector(name, selector) {
136
- this.writeStringWithLength(name);
137
- this.writeSelector(selector);
138
- }
139
- writeAddressValueTuple(map) {
157
+ writeAddressValueTuple(map, be = true) {
140
158
  if (map.size > 65535)
141
159
  throw new Error('Map size is too large');
142
- this.writeU16(map.size);
160
+ this.writeU16(map.size, be);
143
161
  const keys = Array.from(map.keys());
144
162
  for (let i = 0; i < keys.length; i++) {
145
163
  const key = keys[i];
@@ -147,26 +165,7 @@ export class BinaryWriter {
147
165
  if (value === null || value === undefined)
148
166
  throw new Error('Value not found');
149
167
  this.writeAddress(key);
150
- this.writeU256(value);
151
- }
152
- }
153
- writeLimitedAddressBytesMap(map) {
154
- if (map.size > 8)
155
- throw new Error('Too many contract calls');
156
- this.writeU8(map.size);
157
- const keys = Array.from(map.keys());
158
- for (let i = 0; i < keys.length; i++) {
159
- const address = keys[i];
160
- const calls = map.get(address);
161
- if (!calls)
162
- throw new Error('Calls not found');
163
- if (calls.length > 10)
164
- throw new Error('Too many calls.');
165
- this.writeAddress(address);
166
- this.writeU8(calls.length);
167
- for (let j = 0; j < calls.length; j++) {
168
- this.writeBytesWithLength(calls[j]);
169
- }
168
+ this.writeU256(value, be);
170
169
  }
171
170
  }
172
171
  writeBytesWithLength(value) {
@@ -181,28 +180,28 @@ export class BinaryWriter {
181
180
  this.writeAddress(value[i]);
182
181
  }
183
182
  }
184
- writeU32Array(value) {
183
+ writeU32Array(value, be = true) {
185
184
  if (value.length > 65535)
186
185
  throw new Error('Array size is too large');
187
- this.writeU16(value.length);
186
+ this.writeU16(value.length, be);
188
187
  for (let i = 0; i < value.length; i++) {
189
- this.writeU32(value[i]);
188
+ this.writeU32(value[i], be);
190
189
  }
191
190
  }
192
- writeU256Array(value) {
191
+ writeU256Array(value, be = true) {
193
192
  if (value.length > 65535)
194
193
  throw new Error('Array size is too large');
195
- this.writeU16(value.length);
194
+ this.writeU16(value.length, be);
196
195
  for (let i = 0; i < value.length; i++) {
197
- this.writeU256(value[i]);
196
+ this.writeU256(value[i], be);
198
197
  }
199
198
  }
200
- writeU128Array(value) {
199
+ writeU128Array(value, be = true) {
201
200
  if (value.length > 65535)
202
201
  throw new Error('Array size is too large');
203
- this.writeU16(value.length);
202
+ this.writeU16(value.length, be);
204
203
  for (let i = 0; i < value.length; i++) {
205
- this.writeU128(value[i]);
204
+ this.writeU128(value[i], be);
206
205
  }
207
206
  }
208
207
  writeStringArray(value) {
@@ -213,12 +212,12 @@ export class BinaryWriter {
213
212
  this.writeStringWithLength(value[i]);
214
213
  }
215
214
  }
216
- writeU16Array(value) {
215
+ writeU16Array(value, be = true) {
217
216
  if (value.length > 65535)
218
217
  throw new Error('Array size is too large');
219
- this.writeU16(value.length);
218
+ this.writeU16(value.length, be);
220
219
  for (let i = 0; i < value.length; i++) {
221
- this.writeU16(value[i]);
220
+ this.writeU16(value[i], be);
222
221
  }
223
222
  }
224
223
  writeU8Array(value) {
@@ -229,12 +228,12 @@ export class BinaryWriter {
229
228
  this.writeU8(value[i]);
230
229
  }
231
230
  }
232
- writeU64Array(value) {
231
+ writeU64Array(value, be = true) {
233
232
  if (value.length > 65535)
234
233
  throw new Error('Array size is too large');
235
- this.writeU16(value.length);
234
+ this.writeU16(value.length, be);
236
235
  for (let i = 0; i < value.length; i++) {
237
- this.writeU64(value[i]);
236
+ this.writeU64(value[i], be);
238
237
  }
239
238
  }
240
239
  writeBytesArray(value) {
@@ -245,27 +244,6 @@ export class BinaryWriter {
245
244
  this.writeBytesWithLength(value[i]);
246
245
  }
247
246
  }
248
- writeSelectorArray(value) {
249
- if (value.length > 65535)
250
- throw new Error('Array size is too large');
251
- this.writeU16(value.length);
252
- for (let i = 0; i < value.length; i++) {
253
- this.writeSelector(value[i]);
254
- }
255
- }
256
- getChecksum() {
257
- let checksum = 0;
258
- for (let i = 0; i < this.buffer.byteLength; i++) {
259
- checksum += this.buffer.getUint8(i);
260
- }
261
- return checksum % 2 ** 32;
262
- }
263
- writeMethodSelectorMap(value) {
264
- this.writeU16(value.size);
265
- value.forEach((selector, _value, _set) => {
266
- this.writeSelector(selector);
267
- });
268
- }
269
247
  verifyAddress(pubKey) {
270
248
  if (pubKey.byteLength > ADDRESS_BYTE_LENGTH) {
271
249
  throw new Error(`Address is too long ${pubKey.byteLength} > ${ADDRESS_BYTE_LENGTH} bytes`);
@@ -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;
@@ -42,6 +42,9 @@ export class Address extends Uint8Array {
42
42
  static dead() {
43
43
  return Address.fromString('0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f');
44
44
  }
45
+ static zero() {
46
+ return new Address();
47
+ }
45
48
  static fromString(pubKey) {
46
49
  if (!pubKey) {
47
50
  throw new Error('Invalid public key');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/transaction",
3
3
  "type": "module",
4
- "version": "1.2.15",
4
+ "version": "1.3.0",
5
5
  "author": "BlobMaster41",
6
6
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
7
7
  "engines": {
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.15';
1
+ export const version = '1.3.0';