opnet 1.2.17 → 1.2.18

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.
@@ -29,6 +29,7 @@ export * from './contracts/interfaces/ICallResult.js';
29
29
  export * from './contracts/interfaces/IContract.js';
30
30
  export * from './contracts/OPNetEvent.js';
31
31
  export * from './contracts/interfaces/SimulatedTransaction.js';
32
+ export * from './contracts/TypeToStr.js';
32
33
  export * from './abi/BitcoinAbiTypes.js';
33
34
  export * from './abi/BitcoinInterface.js';
34
35
  export * from './abi/interfaces/BaseContractProperties.js';
@@ -1 +1 @@
1
- export declare const version = "1.2.17";
1
+ export declare const version = "1.2.18";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.17';
1
+ export const version = '1.2.18';
@@ -45,6 +45,7 @@ export declare abstract class IBaseContract<T extends BaseContractProperties> im
45
45
  setSimulatedHeight(height: bigint | undefined): void;
46
46
  protected getFunction(name: symbol | string): CallResult | undefined | string | number | symbol | Address | Network | (() => Promise<BlockGasParameters>) | ((functionName: string, args: unknown[]) => Buffer);
47
47
  private defineInternalFunctions;
48
+ private getSelector;
48
49
  private encodeFunctionData;
49
50
  private encodeInput;
50
51
  private decodeOutput;
@@ -2,6 +2,7 @@ import { ABICoder, ABIDataTypes, AddressVerificator, BinaryReader, BinaryWriter,
2
2
  import { BitcoinAbiTypes } from '../abi/BitcoinAbiTypes.js';
3
3
  import { BitcoinInterface } from '../abi/BitcoinInterface.js';
4
4
  import { OPNetEvent } from './OPNetEvent.js';
5
+ import { AbiTypeToStr } from './TypeToStr.js';
5
6
  const internal = Symbol.for('_btc_internal');
6
7
  const bitcoinAbiCoder = new ABICoder();
7
8
  export class IBaseContract {
@@ -121,9 +122,29 @@ export class IBaseContract {
121
122
  }
122
123
  }
123
124
  }
125
+ getSelector(element) {
126
+ let name = element.name;
127
+ if (element.inputs && element.inputs.length) {
128
+ name += '(';
129
+ for (let i = 0; i < element.inputs.length; i++) {
130
+ const input = element.inputs[i];
131
+ const str = AbiTypeToStr[input.type];
132
+ if (!str) {
133
+ throw new Error(`Unsupported type: ${input.type}`);
134
+ }
135
+ if (i > 0) {
136
+ name += ',';
137
+ }
138
+ name += str;
139
+ }
140
+ name += ')';
141
+ }
142
+ return name;
143
+ }
124
144
  encodeFunctionData(element, args) {
125
145
  const writer = new BinaryWriter();
126
- const selector = Number('0x' + bitcoinAbiCoder.encodeSelector(element.name));
146
+ const selectorStr = this.getSelector(element);
147
+ const selector = Number('0x' + bitcoinAbiCoder.encodeSelector(selectorStr));
127
148
  writer.writeSelector(selector);
128
149
  if (args.length !== (element.inputs?.length ?? 0)) {
129
150
  throw new Error('Invalid number of arguments provided');
@@ -0,0 +1,4 @@
1
+ import { ABIDataTypes } from '@btc-vision/transaction';
2
+ export declare const AbiTypeToStr: {
3
+ [key in ABIDataTypes]: string;
4
+ };
@@ -0,0 +1,25 @@
1
+ import { ABIDataTypes } from '@btc-vision/transaction';
2
+ export const AbiTypeToStr = {
3
+ [ABIDataTypes.ADDRESS]: 'address',
4
+ [ABIDataTypes.BOOL]: 'bool',
5
+ [ABIDataTypes.BYTES]: 'bytes',
6
+ [ABIDataTypes.UINT256]: 'uint256',
7
+ [ABIDataTypes.UINT128]: 'uint128',
8
+ [ABIDataTypes.UINT64]: 'uint64',
9
+ [ABIDataTypes.UINT32]: 'uint32',
10
+ [ABIDataTypes.UINT16]: 'uint16',
11
+ [ABIDataTypes.UINT8]: 'uint8',
12
+ [ABIDataTypes.STRING]: 'string',
13
+ [ABIDataTypes.BYTES32]: 'bytes32',
14
+ [ABIDataTypes.ADDRESS_UINT256_TUPLE]: 'tuple(address,uint256)',
15
+ [ABIDataTypes.ARRAY_OF_ADDRESSES]: 'address[]',
16
+ [ABIDataTypes.ARRAY_OF_UINT256]: 'uint256[]',
17
+ [ABIDataTypes.ARRAY_OF_UINT128]: 'uint128[]',
18
+ [ABIDataTypes.ARRAY_OF_UINT64]: 'uint64[]',
19
+ [ABIDataTypes.ARRAY_OF_UINT32]: 'uint32[]',
20
+ [ABIDataTypes.ARRAY_OF_UINT16]: 'uint16[]',
21
+ [ABIDataTypes.ARRAY_OF_UINT8]: 'uint8[]',
22
+ [ABIDataTypes.ARRAY_OF_BYTES]: 'bytes[]',
23
+ [ABIDataTypes.ARRAY_OF_STRING]: 'string[]',
24
+ [ABIDataTypes.TUPLE]: 'tuple256',
25
+ };
package/build/opnet.d.ts CHANGED
@@ -29,6 +29,7 @@ export * from './contracts/interfaces/ICallResult.js';
29
29
  export * from './contracts/interfaces/IContract.js';
30
30
  export * from './contracts/OPNetEvent.js';
31
31
  export * from './contracts/interfaces/SimulatedTransaction.js';
32
+ export * from './contracts/TypeToStr.js';
32
33
  export * from './abi/BitcoinAbiTypes.js';
33
34
  export * from './abi/BitcoinInterface.js';
34
35
  export * from './abi/interfaces/BaseContractProperties.js';
package/build/opnet.js CHANGED
@@ -29,6 +29,7 @@ export * from './contracts/interfaces/ICallResult.js';
29
29
  export * from './contracts/interfaces/IContract.js';
30
30
  export * from './contracts/OPNetEvent.js';
31
31
  export * from './contracts/interfaces/SimulatedTransaction.js';
32
+ export * from './contracts/TypeToStr.js';
32
33
  export * from './abi/BitcoinAbiTypes.js';
33
34
  export * from './abi/BitcoinInterface.js';
34
35
  export * from './abi/interfaces/BaseContractProperties.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "opnet",
3
3
  "type": "module",
4
- "version": "1.2.17",
4
+ "version": "1.2.18",
5
5
  "author": "OP_NET",
6
6
  "description": "The perfect library for building Bitcoin-based applications.",
7
7
  "engines": {
@@ -70,6 +70,7 @@
70
70
  "@babel/preset-typescript": "^7.26.0",
71
71
  "@eslint/js": "^9.17.0",
72
72
  "@types/node": "^22.10.5",
73
+ "@types/sha.js": "^2.4.4",
73
74
  "@types/ws": "^8.5.13",
74
75
  "assert": "^2.1.0",
75
76
  "babel-loader": "^9.2.1",
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.17';
1
+ export const version = '1.2.18';
@@ -27,6 +27,7 @@ import { IAccessList } from './interfaces/IAccessList.js';
27
27
  import { IContract } from './interfaces/IContract.js';
28
28
  import { ParsedSimulatedTransaction } from './interfaces/SimulatedTransaction.js';
29
29
  import { OPNetEvent } from './OPNetEvent.js';
30
+ import { AbiTypeToStr } from './TypeToStr.js';
30
31
 
31
32
  const internal = Symbol.for('_btc_internal');
32
33
  const bitcoinAbiCoder = new ABICoder();
@@ -286,9 +287,36 @@ export abstract class IBaseContract<T extends BaseContractProperties> implements
286
287
  }
287
288
  }
288
289
 
290
+ private getSelector(element: FunctionBaseData): string {
291
+ let name = element.name;
292
+
293
+ if (element.inputs && element.inputs.length) {
294
+ name += '(';
295
+ for (let i = 0; i < element.inputs.length; i++) {
296
+ const input = element.inputs[i];
297
+ const str = AbiTypeToStr[input.type];
298
+
299
+ if (!str) {
300
+ throw new Error(`Unsupported type: ${input.type}`);
301
+ }
302
+
303
+ if (i > 0) {
304
+ name += ',';
305
+ }
306
+
307
+ name += str;
308
+ }
309
+ name += ')';
310
+ }
311
+
312
+ return name;
313
+ }
314
+
289
315
  private encodeFunctionData(element: FunctionBaseData, args: unknown[]): BinaryWriter {
290
316
  const writer = new BinaryWriter();
291
- const selector = Number('0x' + bitcoinAbiCoder.encodeSelector(element.name));
317
+
318
+ const selectorStr = this.getSelector(element);
319
+ const selector = Number('0x' + bitcoinAbiCoder.encodeSelector(selectorStr));
292
320
  writer.writeSelector(selector);
293
321
 
294
322
  if (args.length !== (element.inputs?.length ?? 0)) {
@@ -0,0 +1,26 @@
1
+ import { ABIDataTypes } from '@btc-vision/transaction';
2
+
3
+ export const AbiTypeToStr: { [key in ABIDataTypes]: string } = {
4
+ [ABIDataTypes.ADDRESS]: 'address',
5
+ [ABIDataTypes.BOOL]: 'bool',
6
+ [ABIDataTypes.BYTES]: 'bytes',
7
+ [ABIDataTypes.UINT256]: 'uint256',
8
+ [ABIDataTypes.UINT128]: 'uint128',
9
+ [ABIDataTypes.UINT64]: 'uint64',
10
+ [ABIDataTypes.UINT32]: 'uint32',
11
+ [ABIDataTypes.UINT16]: 'uint16',
12
+ [ABIDataTypes.UINT8]: 'uint8',
13
+ [ABIDataTypes.STRING]: 'string',
14
+ [ABIDataTypes.BYTES32]: 'bytes32',
15
+ [ABIDataTypes.ADDRESS_UINT256_TUPLE]: 'tuple(address,uint256)',
16
+ [ABIDataTypes.ARRAY_OF_ADDRESSES]: 'address[]',
17
+ [ABIDataTypes.ARRAY_OF_UINT256]: 'uint256[]',
18
+ [ABIDataTypes.ARRAY_OF_UINT128]: 'uint128[]',
19
+ [ABIDataTypes.ARRAY_OF_UINT64]: 'uint64[]',
20
+ [ABIDataTypes.ARRAY_OF_UINT32]: 'uint32[]',
21
+ [ABIDataTypes.ARRAY_OF_UINT16]: 'uint16[]',
22
+ [ABIDataTypes.ARRAY_OF_UINT8]: 'uint8[]',
23
+ [ABIDataTypes.ARRAY_OF_BYTES]: 'bytes[]',
24
+ [ABIDataTypes.ARRAY_OF_STRING]: 'string[]',
25
+ [ABIDataTypes.TUPLE]: 'tuple256',
26
+ };
package/src/opnet.ts CHANGED
@@ -47,6 +47,7 @@ export * from './contracts/interfaces/ICallResult.js';
47
47
  export * from './contracts/interfaces/IContract.js';
48
48
  export * from './contracts/OPNetEvent.js';
49
49
  export * from './contracts/interfaces/SimulatedTransaction.js';
50
+ export * from './contracts/TypeToStr.js';
50
51
 
51
52
  /** Abi */
52
53
  export * from './abi/BitcoinAbiTypes.js';