@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.
@@ -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) {
@@ -114,13 +136,6 @@ export class BinaryWriter {
114
136
  this.currentOffset = 0;
115
137
  this.buffer = this.getDefaultBuffer(4);
116
138
  }
117
- writeTuple(values) {
118
- this.allocSafe(U32_BYTE_LENGTH + values.length * U256_BYTE_LENGTH);
119
- this.writeU32(values.length);
120
- for (let i = 0; i < values.length; i++) {
121
- this.writeU256(values[i]);
122
- }
123
- }
124
139
  toBytesReader() {
125
140
  return new BinaryReader(this.getBuffer());
126
141
  }
@@ -139,14 +154,10 @@ export class BinaryWriter {
139
154
  this.resize(size);
140
155
  }
141
156
  }
142
- writeABISelector(name, selector) {
143
- this.writeStringWithLength(name);
144
- this.writeSelector(selector);
145
- }
146
- writeAddressValueTupleMap(map) {
157
+ writeAddressValueTuple(map, be = true) {
147
158
  if (map.size > 65535)
148
159
  throw new Error('Map size is too large');
149
- this.writeU16(map.size);
160
+ this.writeU16(map.size, be);
150
161
  const keys = Array.from(map.keys());
151
162
  for (let i = 0; i < keys.length; i++) {
152
163
  const key = keys[i];
@@ -154,26 +165,7 @@ export class BinaryWriter {
154
165
  if (value === null || value === undefined)
155
166
  throw new Error('Value not found');
156
167
  this.writeAddress(key);
157
- this.writeU256(value);
158
- }
159
- }
160
- writeLimitedAddressBytesMap(map) {
161
- if (map.size > 8)
162
- throw new Error('Too many contract calls');
163
- this.writeU8(map.size);
164
- const keys = Array.from(map.keys());
165
- for (let i = 0; i < keys.length; i++) {
166
- const address = keys[i];
167
- const calls = map.get(address);
168
- if (!calls)
169
- throw new Error('Calls not found');
170
- if (calls.length > 10)
171
- throw new Error('Too many calls.');
172
- this.writeAddress(address);
173
- this.writeU8(calls.length);
174
- for (let j = 0; j < calls.length; j++) {
175
- this.writeBytesWithLength(calls[j]);
176
- }
168
+ this.writeU256(value, be);
177
169
  }
178
170
  }
179
171
  writeBytesWithLength(value) {
@@ -188,28 +180,28 @@ export class BinaryWriter {
188
180
  this.writeAddress(value[i]);
189
181
  }
190
182
  }
191
- writeU32Array(value) {
183
+ writeU32Array(value, be = true) {
192
184
  if (value.length > 65535)
193
185
  throw new Error('Array size is too large');
194
- this.writeU16(value.length);
186
+ this.writeU16(value.length, be);
195
187
  for (let i = 0; i < value.length; i++) {
196
- this.writeU32(value[i]);
188
+ this.writeU32(value[i], be);
197
189
  }
198
190
  }
199
- writeU256Array(value) {
191
+ writeU256Array(value, be = true) {
200
192
  if (value.length > 65535)
201
193
  throw new Error('Array size is too large');
202
- this.writeU16(value.length);
194
+ this.writeU16(value.length, be);
203
195
  for (let i = 0; i < value.length; i++) {
204
- this.writeU256(value[i]);
196
+ this.writeU256(value[i], be);
205
197
  }
206
198
  }
207
- writeU128Array(value) {
199
+ writeU128Array(value, be = true) {
208
200
  if (value.length > 65535)
209
201
  throw new Error('Array size is too large');
210
- this.writeU16(value.length);
202
+ this.writeU16(value.length, be);
211
203
  for (let i = 0; i < value.length; i++) {
212
- this.writeU128(value[i]);
204
+ this.writeU128(value[i], be);
213
205
  }
214
206
  }
215
207
  writeStringArray(value) {
@@ -220,12 +212,12 @@ export class BinaryWriter {
220
212
  this.writeStringWithLength(value[i]);
221
213
  }
222
214
  }
223
- writeU16Array(value) {
215
+ writeU16Array(value, be = true) {
224
216
  if (value.length > 65535)
225
217
  throw new Error('Array size is too large');
226
- this.writeU16(value.length);
218
+ this.writeU16(value.length, be);
227
219
  for (let i = 0; i < value.length; i++) {
228
- this.writeU16(value[i]);
220
+ this.writeU16(value[i], be);
229
221
  }
230
222
  }
231
223
  writeU8Array(value) {
@@ -236,12 +228,12 @@ export class BinaryWriter {
236
228
  this.writeU8(value[i]);
237
229
  }
238
230
  }
239
- writeU64Array(value) {
231
+ writeU64Array(value, be = true) {
240
232
  if (value.length > 65535)
241
233
  throw new Error('Array size is too large');
242
- this.writeU16(value.length);
234
+ this.writeU16(value.length, be);
243
235
  for (let i = 0; i < value.length; i++) {
244
- this.writeU64(value[i]);
236
+ this.writeU64(value[i], be);
245
237
  }
246
238
  }
247
239
  writeBytesArray(value) {
@@ -252,27 +244,6 @@ export class BinaryWriter {
252
244
  this.writeBytesWithLength(value[i]);
253
245
  }
254
246
  }
255
- writeSelectorArray(value) {
256
- if (value.length > 65535)
257
- throw new Error('Array size is too large');
258
- this.writeU16(value.length);
259
- for (let i = 0; i < value.length; i++) {
260
- this.writeSelector(value[i]);
261
- }
262
- }
263
- getChecksum() {
264
- let checksum = 0;
265
- for (let i = 0; i < this.buffer.byteLength; i++) {
266
- checksum += this.buffer.getUint8(i);
267
- }
268
- return checksum % 2 ** 32;
269
- }
270
- writeMethodSelectorMap(value) {
271
- this.writeU16(value.size);
272
- value.forEach((selector, _value, _set) => {
273
- this.writeSelector(selector);
274
- });
275
- }
276
247
  verifyAddress(pubKey) {
277
248
  if (pubKey.byteLength > ADDRESS_BYTE_LENGTH) {
278
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');
@@ -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,4 +1,4 @@
1
- import { crypto as bitCrypto, script as bitScript, networks, Psbt, toXOnly, } from '@btc-vision/bitcoin';
1
+ import { crypto as bitCrypto, networks, Psbt, script as bitScript, toXOnly, } from '@btc-vision/bitcoin';
2
2
  import { EcKeyPair } from '../../../keypair/EcKeyPair.js';
3
3
  import { canSignNonTaprootInput, isTaprootInput } from '../../../signer/SignerUtils.js';
4
4
  import { CustomKeypair } from '../BrowserSignerBase.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/transaction",
3
3
  "type": "module",
4
- "version": "1.2.14",
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.14';
1
+ export const version = '1.3.0';
@@ -15,7 +15,6 @@ export enum ABIDataTypes {
15
15
  ADDRESS = 'ADDRESS',
16
16
  STRING = 'STRING',
17
17
  BYTES32 = 'BYTES32',
18
- TUPLE = 'TUPLE',
19
18
  BYTES = 'BYTES',
20
19
  ADDRESS_UINT256_TUPLE = 'ADDRESS_UINT256_TUPLE',
21
20
  ARRAY_OF_ADDRESSES = 'ARRAY_OF_ADDRESSES',
@@ -67,9 +66,6 @@ export class ABICoder {
67
66
  case ABIDataTypes.INT128:
68
67
  result.push(byteReader.readI128());
69
68
  break;
70
- case ABIDataTypes.TUPLE: // very basic for now, only contains uint256
71
- result.push(byteReader.readTuple());
72
- break;
73
69
  case ABIDataTypes.ADDRESS_UINT256_TUPLE:
74
70
  result.push(byteReader.readAddressValueTuple());
75
71
  break;