opnet 1.0.13 → 1.0.15
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.
- package/CONTRIBUTING.md +71 -0
- package/README.md +6 -7
- package/browser/_version.d.ts +1 -1
- package/browser/abi/BaseContractProperty.d.ts +3 -0
- package/browser/abi/BitcoinAbiTypes.d.ts +4 -0
- package/browser/abi/BitcoinInterface.d.ts +9 -0
- package/browser/abi/interfaces/BaseContractProperties.d.ts +4 -0
- package/browser/abi/interfaces/BitcoinAbiValue.d.ts +5 -0
- package/browser/abi/interfaces/BitcoinInterfaceAbi.d.ts +11 -0
- package/browser/bitcoin/UTXOs.d.ts +1 -6
- package/browser/bitcoin/interfaces/IUTXO.d.ts +7 -0
- package/browser/common/CommonTypes.d.ts +1 -0
- package/browser/contracts/CallResult.d.ts +6 -3
- package/browser/contracts/Contract.d.ts +28 -0
- package/browser/contracts/interfaces/ICallResult.d.ts +2 -3
- package/browser/contracts/interfaces/IContract.d.ts +4 -0
- package/browser/crypto/crypto-browser.d.ts +7 -0
- package/browser/index.js +1 -1
- package/browser/opnet.d.ts +11 -0
- package/browser/scripts/testContract.d.ts +1 -0
- package/browser/scripts/testMethods.d.ts +1 -0
- package/browser/utils/BitcoinUtils.d.ts +4 -0
- package/build/_version.d.ts +1 -1
- package/build/_version.js +1 -1
- package/build/abi/BaseContractProperty.d.ts +3 -0
- package/build/abi/BaseContractProperty.js +1 -0
- package/build/abi/BitcoinAbiTypes.d.ts +4 -0
- package/build/abi/BitcoinAbiTypes.js +5 -0
- package/build/abi/BitcoinInterface.d.ts +9 -0
- package/build/abi/BitcoinInterface.js +45 -0
- package/build/abi/BitcoinInterfaceAbi.d.ts +3 -0
- package/build/abi/BitcoinInterfaceAbi.js +1 -0
- package/build/abi/enums/ParamTypes.d.ts +0 -0
- package/build/abi/enums/ParamTypes.js +1 -0
- package/build/abi/interface/BitcoinInterface.d.ts +6 -0
- package/build/abi/interface/BitcoinInterface.js +12 -0
- package/build/abi/interface/BitcoinInterfaceAbi.d.ts +3 -0
- package/build/abi/interface/BitcoinInterfaceAbi.js +1 -0
- package/build/abi/interfaces/BaseContractProperties.d.ts +4 -0
- package/build/abi/interfaces/BaseContractProperties.js +1 -0
- package/build/abi/interfaces/BaseContractProperty.d.ts +2 -0
- package/build/abi/interfaces/BaseContractProperty.js +1 -0
- package/build/abi/interfaces/BitcoinAbiInput.d.ts +5 -0
- package/build/abi/interfaces/BitcoinAbiInput.js +1 -0
- package/build/abi/interfaces/BitcoinAbiValue.d.ts +5 -0
- package/build/abi/interfaces/BitcoinAbiValue.js +1 -0
- package/build/abi/interfaces/BitcoinInterfaceAbi.d.ts +11 -0
- package/build/abi/interfaces/BitcoinInterfaceAbi.js +1 -0
- package/build/bitcoin/UTXOs.d.ts +1 -6
- package/build/bitcoin/interfaces/IUTXO.d.ts +7 -0
- package/build/bitcoin/interfaces/IUTXO.js +1 -0
- package/build/common/CommonTypes.d.ts +1 -0
- package/build/contracts/CallResult.d.ts +6 -3
- package/build/contracts/CallResult.js +9 -2
- package/build/contracts/Contract.d.ts +28 -0
- package/build/contracts/Contract.js +206 -0
- package/build/contracts/interfaces/ICallResult.d.ts +2 -3
- package/build/contracts/interfaces/IContract.d.ts +4 -0
- package/build/contracts/interfaces/IContract.js +1 -0
- package/build/interface/BitcoinInterface.d.ts +6 -0
- package/build/interface/BitcoinInterface.js +12 -0
- package/build/interface/BitcoinInterfaceAbi.d.ts +3 -0
- package/build/interface/BitcoinInterfaceAbi.js +1 -0
- package/build/interface/Interface.d.ts +2 -0
- package/build/interface/Interface.js +2 -0
- package/build/interface.d.ts +0 -0
- package/build/interface.js +1 -0
- package/build/opnet.d.ts +11 -0
- package/build/opnet.js +11 -0
- package/build/scripts/test.js +12 -0
- package/build/scripts/testContract.d.ts +1 -0
- package/build/scripts/testContract.js +28 -0
- package/build/scripts/testMethods.d.ts +1 -0
- package/build/scripts/testMethods.js +24 -0
- package/build/utils/BitcoinUtils.d.ts +4 -0
- package/build/utils/BitcoinUtils.js +7 -0
- package/cjs/_version.d.ts +1 -0
- package/cjs/_version.js +4 -0
- package/cjs/abi/BaseContractProperty.d.ts +3 -0
- package/cjs/abi/BaseContractProperty.js +2 -0
- package/cjs/abi/BitcoinAbiTypes.d.ts +4 -0
- package/cjs/abi/BitcoinAbiTypes.js +8 -0
- package/cjs/abi/BitcoinInterface.d.ts +9 -0
- package/cjs/abi/BitcoinInterface.js +49 -0
- package/cjs/abi/interfaces/BaseContractProperties.d.ts +4 -0
- package/cjs/abi/interfaces/BaseContractProperties.js +2 -0
- package/cjs/abi/interfaces/BitcoinAbiValue.d.ts +5 -0
- package/cjs/abi/interfaces/BitcoinAbiValue.js +2 -0
- package/cjs/abi/interfaces/BitcoinInterfaceAbi.d.ts +11 -0
- package/cjs/abi/interfaces/BitcoinInterfaceAbi.js +2 -0
- package/cjs/bitcoin/BitcoinAddress.d.ts +20 -0
- package/cjs/bitcoin/BitcoinAddress.js +116 -0
- package/cjs/bitcoin/UTXOs.d.ts +15 -0
- package/cjs/bitcoin/UTXOs.js +16 -0
- package/cjs/block/Block.d.ts +2 -3
- package/cjs/block/Block.js +3 -28
- package/cjs/block/interfaces/IBlock.d.ts +28 -0
- package/cjs/block/interfaces/IBlock.js +2 -0
- package/cjs/common/CommonTypes.d.ts +4 -0
- package/cjs/common/CommonTypes.js +2 -0
- package/cjs/contracts/CallResult.d.ts +13 -0
- package/cjs/contracts/CallResult.js +25 -0
- package/cjs/contracts/Contract.d.ts +28 -0
- package/cjs/contracts/Contract.js +212 -0
- package/cjs/contracts/ContractData.d.ts +15 -0
- package/cjs/contracts/ContractData.js +36 -0
- package/cjs/contracts/interfaces/IAccessList.d.ts +6 -0
- package/cjs/contracts/interfaces/IAccessList.js +2 -0
- package/cjs/contracts/interfaces/ICallResult.d.ts +11 -0
- package/cjs/contracts/interfaces/ICallResult.js +2 -0
- package/cjs/contracts/interfaces/IContract.d.ts +4 -0
- package/cjs/contracts/interfaces/IContract.js +2 -0
- package/cjs/contracts/interfaces/IRawContract.d.ts +13 -0
- package/cjs/contracts/interfaces/IRawContract.js +2 -0
- package/cjs/crypto/crypto.d.ts +2 -0
- package/cjs/crypto/crypto.js +8 -0
- package/cjs/index.d.ts +3 -11
- package/cjs/index.js +16 -11
- package/cjs/opnet.d.ts +36 -0
- package/cjs/opnet.js +54 -0
- package/cjs/providers/AbstractRpcProvider.d.ts +34 -0
- package/cjs/providers/AbstractRpcProvider.js +161 -0
- package/cjs/providers/JSONRpcProvider.d.ts +5 -9
- package/cjs/providers/JSONRpcProvider.js +4 -29
- package/cjs/providers/WebsocketRpcProvider.d.ts +8 -0
- package/cjs/providers/WebsocketRpcProvider.js +27 -0
- package/cjs/scripts/testContract.d.ts +1 -0
- package/cjs/scripts/testContract.js +30 -0
- package/cjs/scripts/testMethods.d.ts +1 -0
- package/cjs/scripts/testMethods.js +26 -0
- package/cjs/serialize/BigInt.d.ts +6 -0
- package/cjs/serialize/BigInt.js +5 -0
- package/cjs/storage/StoredValue.d.ts +10 -0
- package/cjs/storage/StoredValue.js +28 -0
- package/cjs/storage/interfaces/IStorageValue.d.ts +9 -0
- package/cjs/storage/interfaces/IStorageValue.js +2 -0
- package/cjs/transactions/DeploymentTransaction.d.ts +10 -1
- package/cjs/transactions/DeploymentTransaction.js +16 -0
- package/cjs/transactions/GenericTransaction.d.ts +1 -1
- package/cjs/transactions/InteractionTransaction.d.ts +1 -1
- package/cjs/transactions/Transaction.d.ts +1 -1
- package/cjs/transactions/Transaction.js +1 -1
- package/cjs/transactions/TransactionParser.d.ts +7 -0
- package/cjs/transactions/TransactionParser.js +32 -0
- package/cjs/transactions/TransactionReceipt.d.ts +10 -0
- package/cjs/transactions/TransactionReceipt.js +16 -0
- package/cjs/transactions/interfaces/ITransaction.d.ts +39 -0
- package/cjs/transactions/interfaces/ITransaction.js +2 -0
- package/cjs/transactions/interfaces/ITransactionReceipt.d.ts +8 -0
- package/cjs/transactions/interfaces/ITransactionReceipt.js +2 -0
- package/cjs/utils/BitcoinUtils.d.ts +4 -0
- package/cjs/utils/BitcoinUtils.js +14 -0
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/AbstractRpcProvider.html +15 -15
- package/docs/classes/BaseContract.html +187 -0
- package/docs/classes/BitcoinAddress.html +5 -2
- package/docs/classes/BitcoinInterface.html +182 -0
- package/docs/classes/BitcoinUtils.html +183 -0
- package/docs/classes/Block.html +5 -3
- package/docs/classes/CallResult.html +6 -2
- package/docs/classes/ContractData.html +4 -2
- package/docs/classes/DeploymentTransaction.html +4 -2
- package/docs/classes/GenericTransaction.html +4 -2
- package/docs/classes/IBaseContract.html +192 -0
- package/docs/classes/InteractionTransaction.html +3 -2
- package/docs/classes/JSONRpcProvider.html +14 -14
- package/docs/classes/StoredValue.html +4 -2
- package/docs/classes/TransactionBase.html +4 -2
- package/docs/classes/TransactionInput.html +3 -2
- package/docs/classes/TransactionOutput.html +3 -2
- package/docs/classes/TransactionParser.html +3 -2
- package/docs/classes/TransactionReceipt.html +3 -2
- package/docs/classes/UTXO.html +4 -2
- package/docs/classes/WebSocketRpcProvider.html +14 -14
- package/docs/enums/ABIDataTypes.html +183 -0
- package/docs/enums/BitcoinAbiTypes.html +178 -0
- package/docs/enums/OPNetTransactionTypes.html +2 -2
- package/docs/functions/getContract.html +180 -0
- package/docs/hierarchy.html +1 -1
- package/docs/index.html +4 -5
- package/docs/interfaces/BaseContractProperties.html +176 -0
- package/docs/interfaces/BitcoinAbiValue.html +180 -0
- package/docs/interfaces/BitcoinInterfaceAbiBase.html +182 -0
- package/docs/interfaces/IAccessList.html +3 -1
- package/docs/interfaces/IAccessListItem.html +3 -1
- package/docs/interfaces/IBlock.html +5 -3
- package/docs/interfaces/IBlockCommon.html +5 -3
- package/docs/interfaces/ICallRequestError.html +4 -2
- package/docs/interfaces/ICallResultData.html +4 -2
- package/docs/interfaces/IContract.html +177 -0
- package/docs/interfaces/IDeploymentTransaction.html +4 -2
- package/docs/interfaces/IGenericTransaction.html +4 -2
- package/docs/interfaces/IInteractionTransaction.html +4 -2
- package/docs/interfaces/IRawContract.html +5 -2
- package/docs/interfaces/IStorageValue.html +4 -2
- package/docs/interfaces/ITransactionBase.html +4 -2
- package/docs/interfaces/ITransactionInput.html +3 -2
- package/docs/interfaces/ITransactionOutput.html +3 -2
- package/docs/interfaces/ITransactionReceipt.html +4 -2
- package/docs/interfaces/IUTXO.html +4 -2
- package/docs/modules.html +38 -24
- package/docs/types/BaseContractProperty.html +176 -0
- package/docs/types/BitcoinAddressLike.html +1 -1
- package/docs/types/BitcoinInterfaceAbi.html +174 -0
- package/docs/types/BlockHeaderChecksumProof.html +1 -1
- package/docs/types/DecodedCallResult.html +174 -0
- package/docs/types/ICallResult.html +2 -1
- package/docs/types/ITransaction.html +2 -1
- package/docs/types/PointerLike.html +1 -1
- package/docs/types/UTXOs.html +3 -1
- package/docs/variables/version.html +1 -1
- package/package.json +6 -4
- package/src/_version.ts +1 -1
- package/src/abi/BaseContractProperty.ts +8 -0
- package/src/abi/BitcoinAbiTypes.ts +9 -0
- package/src/abi/BitcoinInterface.ts +63 -0
- package/src/abi/interfaces/BaseContractProperties.ts +9 -0
- package/src/abi/interfaces/BitcoinAbiValue.ts +18 -0
- package/src/abi/interfaces/BitcoinInterfaceAbi.ts +19 -0
- package/src/bitcoin/BitcoinAddress.ts +5 -0
- package/src/bitcoin/UTXOs.ts +28 -26
- package/src/bitcoin/interfaces/IUTXO.ts +12 -0
- package/src/block/Block.ts +5 -0
- package/src/block/interfaces/IBlock.ts +46 -38
- package/src/common/CommonTypes.ts +16 -4
- package/src/contracts/CallResult.ts +34 -20
- package/src/contracts/Contract.ts +321 -0
- package/src/contracts/ContractData.ts +46 -41
- package/src/contracts/interfaces/IAccessList.ts +17 -7
- package/src/contracts/interfaces/ICallResult.ts +29 -15
- package/src/contracts/interfaces/IContract.ts +10 -0
- package/src/contracts/interfaces/IRawContract.ts +17 -12
- package/src/crypto/crypto-browser.js +71 -60
- package/src/opnet.ts +17 -0
- package/src/providers/AbstractRpcProvider.ts +352 -351
- package/src/scripts/testContract.ts +51 -0
- package/src/scripts/{test.ts → testMethods.ts} +52 -54
- package/src/scripts/test_abi.json +31 -0
- package/src/storage/StoredValue.ts +35 -30
- package/src/storage/interfaces/IStorageValue.ts +15 -10
- package/src/transactions/DeploymentTransaction.ts +41 -36
- package/src/transactions/GenericTransaction.ts +19 -12
- package/src/transactions/InteractionTransaction.ts +44 -40
- package/src/transactions/Transaction.ts +8 -0
- package/src/transactions/TransactionInput.ts +8 -0
- package/src/transactions/TransactionOutput.ts +8 -0
- package/src/transactions/TransactionParser.ts +47 -43
- package/src/transactions/TransactionReceipt.ts +22 -18
- package/src/transactions/interfaces/ITransaction.ts +78 -52
- package/src/transactions/interfaces/ITransactionReceipt.ts +14 -9
- package/src/utils/BitcoinUtils.ts +22 -0
- package/webpack.config.js +71 -75
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { ABICoder, ABIDataTypes, BinaryReader, BinaryWriter } from '@btc-vision/bsi-binary';
|
|
2
|
+
import { BaseContractProperty } from '../abi/BaseContractProperty.js';
|
|
3
|
+
import { BitcoinAbiTypes } from '../abi/BitcoinAbiTypes.js';
|
|
4
|
+
import { BitcoinInterface } from '../abi/BitcoinInterface.js';
|
|
5
|
+
import { BaseContractProperties } from '../abi/interfaces/BaseContractProperties.js';
|
|
6
|
+
import { BitcoinAbiValue } from '../abi/interfaces/BitcoinAbiValue.js';
|
|
7
|
+
import {
|
|
8
|
+
BitcoinInterfaceAbi,
|
|
9
|
+
BitcoinInterfaceAbiBase,
|
|
10
|
+
} from '../abi/interfaces/BitcoinInterfaceAbi.js';
|
|
11
|
+
import { BitcoinAddressLike, DecodedCallResult } from '../common/CommonTypes.js';
|
|
12
|
+
import { AbstractRpcProvider } from '../providers/AbstractRpcProvider.js';
|
|
13
|
+
import { IContract } from './interfaces/IContract.js';
|
|
14
|
+
|
|
15
|
+
const internal = Symbol.for('_btc_internal');
|
|
16
|
+
const bitcoinAbiCoder = new ABICoder();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Represents the base contract class.
|
|
20
|
+
* @category Contracts
|
|
21
|
+
* @abstract
|
|
22
|
+
*/
|
|
23
|
+
export abstract class IBaseContract<T extends BaseContractProperties> implements IContract {
|
|
24
|
+
/**
|
|
25
|
+
* The address of the contract.
|
|
26
|
+
*/
|
|
27
|
+
public readonly address: BitcoinAddressLike;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The interface of the contract.
|
|
31
|
+
*/
|
|
32
|
+
public readonly interface!: BitcoinInterface;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A generic provider for the contract.
|
|
36
|
+
*/
|
|
37
|
+
public readonly provider: AbstractRpcProvider;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The internal functions of the contract.
|
|
41
|
+
* @protected
|
|
42
|
+
*/
|
|
43
|
+
readonly [internal]: keyof T | undefined;
|
|
44
|
+
|
|
45
|
+
private events: object[] = [];
|
|
46
|
+
|
|
47
|
+
protected constructor(
|
|
48
|
+
address: BitcoinAddressLike,
|
|
49
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
50
|
+
provider: AbstractRpcProvider,
|
|
51
|
+
) {
|
|
52
|
+
this.address = address;
|
|
53
|
+
this.provider = provider;
|
|
54
|
+
this.interface = BitcoinInterface.from(abi);
|
|
55
|
+
|
|
56
|
+
Object.defineProperty(this, internal, { value: {} });
|
|
57
|
+
|
|
58
|
+
this.defineInternalFunctions();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected getFunction(
|
|
62
|
+
name: symbol,
|
|
63
|
+
): BaseContractProperty | undefined | string | number | symbol {
|
|
64
|
+
const key = name as keyof Omit<IBaseContract<T>, 'address' | 'provider' | 'interface'>;
|
|
65
|
+
|
|
66
|
+
// @ts-ignore
|
|
67
|
+
return this[key];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Defines the internal functions of the contract. These functions are generated for the ABI provided.
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
private defineInternalFunctions(): void {
|
|
75
|
+
for (const element of this.interface.abi) {
|
|
76
|
+
switch (element.type) {
|
|
77
|
+
case BitcoinAbiTypes.Function:
|
|
78
|
+
Object.defineProperty(this, element.name, {
|
|
79
|
+
value: this.callFunction(element).bind(this),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
break;
|
|
83
|
+
case BitcoinAbiTypes.Event:
|
|
84
|
+
this.events.push(element);
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private encodeFunctionData(element: BitcoinInterfaceAbiBase, args: unknown[]): BinaryWriter {
|
|
93
|
+
const writer = new BinaryWriter();
|
|
94
|
+
const selector = Number('0x' + bitcoinAbiCoder.encodeSelector(element.name));
|
|
95
|
+
writer.writeSelector(selector);
|
|
96
|
+
|
|
97
|
+
if (args.length !== element.inputs!.length) {
|
|
98
|
+
throw new Error('Invalid number of arguments provided');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!element.inputs || (element.inputs && element.inputs.length === 0)) {
|
|
102
|
+
return writer;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < element.inputs.length; i++) {
|
|
106
|
+
this.encodeInput(writer, element.inputs[i], args[i]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return writer;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private encodeInput(writer: BinaryWriter, abi: BitcoinAbiValue, value: unknown): void {
|
|
113
|
+
const type = abi.type;
|
|
114
|
+
const name = abi.name;
|
|
115
|
+
|
|
116
|
+
switch (type) {
|
|
117
|
+
case ABIDataTypes.UINT256:
|
|
118
|
+
if (typeof value !== 'bigint') {
|
|
119
|
+
throw new Error(`Expected value to be of type bigint (${name})`);
|
|
120
|
+
}
|
|
121
|
+
writer.writeU256(value as bigint);
|
|
122
|
+
break;
|
|
123
|
+
case ABIDataTypes.BOOL:
|
|
124
|
+
if (typeof value !== 'boolean') {
|
|
125
|
+
throw new Error(`Expected value to be of type boolean (${name})`);
|
|
126
|
+
}
|
|
127
|
+
writer.writeBoolean(value as boolean);
|
|
128
|
+
break;
|
|
129
|
+
case ABIDataTypes.STRING:
|
|
130
|
+
if (typeof value !== 'string') {
|
|
131
|
+
throw new Error(`Expected value to be of type string (${name})`);
|
|
132
|
+
}
|
|
133
|
+
writer.writeStringWithLength(value as string);
|
|
134
|
+
break;
|
|
135
|
+
case ABIDataTypes.ADDRESS:
|
|
136
|
+
const address = value as BitcoinAddressLike;
|
|
137
|
+
writer.writeAddress(address.toString());
|
|
138
|
+
break;
|
|
139
|
+
case ABIDataTypes.TUPLE:
|
|
140
|
+
if (!(value instanceof Array)) {
|
|
141
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
writer.writeTuple(value as bigint[]);
|
|
145
|
+
break;
|
|
146
|
+
case ABIDataTypes.UINT8:
|
|
147
|
+
if (typeof value !== 'number') {
|
|
148
|
+
throw new Error(`Expected value to be of type number (${name})`);
|
|
149
|
+
}
|
|
150
|
+
writer.writeU8(value as number);
|
|
151
|
+
break;
|
|
152
|
+
case ABIDataTypes.UINT16:
|
|
153
|
+
if (typeof value !== 'number') {
|
|
154
|
+
throw new Error(`Expected value to be of type number (${name})`);
|
|
155
|
+
}
|
|
156
|
+
writer.writeU16(value as number);
|
|
157
|
+
break;
|
|
158
|
+
case ABIDataTypes.UINT32:
|
|
159
|
+
if (typeof value !== 'number') {
|
|
160
|
+
throw new Error(`Expected value to be of type number (${name})`);
|
|
161
|
+
}
|
|
162
|
+
writer.writeU32(value as number);
|
|
163
|
+
break;
|
|
164
|
+
case ABIDataTypes.BYTES32:
|
|
165
|
+
if (!(value instanceof Uint8Array)) {
|
|
166
|
+
throw new Error(`Expected value to be of type Uint8Array (${name})`);
|
|
167
|
+
}
|
|
168
|
+
writer.writeBytes(value as Uint8Array);
|
|
169
|
+
break;
|
|
170
|
+
default:
|
|
171
|
+
throw new Error(`Unsupported type: ${type} (${name})`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private decodeOutput(abi: BitcoinAbiValue[], reader: BinaryReader): Array<DecodedCallResult> {
|
|
176
|
+
const result: Array<DecodedCallResult> = [];
|
|
177
|
+
|
|
178
|
+
for (let i = 0; i < abi.length; i++) {
|
|
179
|
+
const type = abi[i].type;
|
|
180
|
+
const name = abi[i].name;
|
|
181
|
+
|
|
182
|
+
switch (type) {
|
|
183
|
+
case ABIDataTypes.UINT256:
|
|
184
|
+
result.push(reader.readU256());
|
|
185
|
+
break;
|
|
186
|
+
case ABIDataTypes.BOOL:
|
|
187
|
+
result.push(reader.readBoolean());
|
|
188
|
+
break;
|
|
189
|
+
case ABIDataTypes.STRING:
|
|
190
|
+
result.push(reader.readStringWithLength());
|
|
191
|
+
break;
|
|
192
|
+
case ABIDataTypes.ADDRESS:
|
|
193
|
+
result.push(reader.readAddress());
|
|
194
|
+
break;
|
|
195
|
+
case ABIDataTypes.TUPLE:
|
|
196
|
+
result.push(reader.readTuple());
|
|
197
|
+
break;
|
|
198
|
+
case ABIDataTypes.UINT8:
|
|
199
|
+
result.push(reader.readU8());
|
|
200
|
+
break;
|
|
201
|
+
case ABIDataTypes.UINT16:
|
|
202
|
+
result.push(reader.readU16());
|
|
203
|
+
break;
|
|
204
|
+
case ABIDataTypes.UINT32:
|
|
205
|
+
result.push(reader.readU32());
|
|
206
|
+
break;
|
|
207
|
+
case ABIDataTypes.BYTES32:
|
|
208
|
+
result.push(reader.readBytes(32));
|
|
209
|
+
break;
|
|
210
|
+
default:
|
|
211
|
+
throw new Error(`Unsupported type: ${type} (${name})`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private callFunction(
|
|
219
|
+
element: BitcoinInterfaceAbiBase,
|
|
220
|
+
): (...args: unknown[]) => Promise<BaseContractProperty> {
|
|
221
|
+
return async (...args: unknown[]): Promise<BaseContractProperty> => {
|
|
222
|
+
const data = this.encodeFunctionData(element, args);
|
|
223
|
+
const response = await this.provider.call(this.address, Buffer.from(data.getBuffer()));
|
|
224
|
+
|
|
225
|
+
if ('error' in response) {
|
|
226
|
+
return response;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const decoded = element.outputs
|
|
230
|
+
? this.decodeOutput(element.outputs, response.result)
|
|
231
|
+
: [];
|
|
232
|
+
response.setDecoded(decoded);
|
|
233
|
+
|
|
234
|
+
return response;
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Represents the base contract class.
|
|
241
|
+
* @category Contracts
|
|
242
|
+
*/
|
|
243
|
+
export class BaseContract<T extends BaseContractProperties> extends IBaseContract<T> {
|
|
244
|
+
constructor(
|
|
245
|
+
address: BitcoinAddressLike,
|
|
246
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
247
|
+
provider: AbstractRpcProvider,
|
|
248
|
+
) {
|
|
249
|
+
super(address, abi, provider);
|
|
250
|
+
|
|
251
|
+
return this.proxify();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Proxifies the contract to allow for type checking.
|
|
256
|
+
* @private
|
|
257
|
+
*/
|
|
258
|
+
private proxify(): T & BaseContract<T> {
|
|
259
|
+
return new Proxy(this, {
|
|
260
|
+
get: (target: BaseContract<T>, prop: typeof internal, receiver) => {
|
|
261
|
+
if (typeof prop === 'symbol' || prop in target) {
|
|
262
|
+
return Reflect.get(target, prop, receiver);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Undefined properties should return undefined
|
|
266
|
+
try {
|
|
267
|
+
return this.getFunction(prop);
|
|
268
|
+
} catch (error: unknown) {
|
|
269
|
+
if (!(error instanceof Error)) {
|
|
270
|
+
throw new Error(
|
|
271
|
+
`Something went wrong when trying to get the function: ${error}`,
|
|
272
|
+
);
|
|
273
|
+
} else {
|
|
274
|
+
throw error;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
has: (target, prop) => {
|
|
279
|
+
if (typeof prop === 'symbol' || prop in target) {
|
|
280
|
+
return Reflect.has(target, prop);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return target.interface.hasFunction(prop);
|
|
284
|
+
},
|
|
285
|
+
}) as BaseContract<T> & T;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Creates a new contract class.
|
|
291
|
+
*/
|
|
292
|
+
function contractBase<T extends BaseContractProperties>(): new (
|
|
293
|
+
address: BitcoinAddressLike,
|
|
294
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
295
|
+
provider: AbstractRpcProvider,
|
|
296
|
+
) => BaseContract<T> & Omit<T, keyof BaseContract<T>> {
|
|
297
|
+
return BaseContract as new (
|
|
298
|
+
address: BitcoinAddressLike,
|
|
299
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
300
|
+
provider: AbstractRpcProvider,
|
|
301
|
+
) => BaseContract<T> & Omit<T, keyof BaseContract<T>>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Creates a new contract instance.
|
|
306
|
+
* @param {BitcoinAddressLike} address The address of the contract.
|
|
307
|
+
* @param {BitcoinInterface | BitcoinInterfaceAbi} abi The ABI of the contract.
|
|
308
|
+
* @param {AbstractRpcProvider} provider The provider for the contract.
|
|
309
|
+
* @returns {BaseContract<T> & Omit<T, keyof BaseContract<T>>} The contract instance.
|
|
310
|
+
* @template T The properties of the contract.
|
|
311
|
+
* @category Contracts
|
|
312
|
+
*/
|
|
313
|
+
export function getContract<T extends BaseContractProperties>(
|
|
314
|
+
address: BitcoinAddressLike,
|
|
315
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
316
|
+
provider: AbstractRpcProvider,
|
|
317
|
+
): BaseContract<T> & Omit<T, keyof BaseContract<T>> {
|
|
318
|
+
const base = contractBase<T>();
|
|
319
|
+
|
|
320
|
+
return new base(address, abi, provider);
|
|
321
|
+
}
|
|
@@ -1,41 +1,46 @@
|
|
|
1
|
-
import { IRawContract } from './interfaces/IRawContract.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
public readonly
|
|
10
|
-
public readonly
|
|
11
|
-
public readonly
|
|
12
|
-
public readonly
|
|
13
|
-
public readonly
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
this.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
import { IRawContract } from './interfaces/IRawContract.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description This class is used to store contract data.
|
|
5
|
+
* @class ContractData
|
|
6
|
+
* @category Bitcoin
|
|
7
|
+
*/
|
|
8
|
+
export class ContractData implements IRawContract {
|
|
9
|
+
public readonly contractAddress: string;
|
|
10
|
+
public readonly virtualAddress: string;
|
|
11
|
+
public readonly bytecode: Buffer;
|
|
12
|
+
public readonly wasCompressed: boolean;
|
|
13
|
+
public readonly deployedTransactionId: string;
|
|
14
|
+
public readonly deployedTransactionHash: string;
|
|
15
|
+
public readonly deployerPubKey: Buffer;
|
|
16
|
+
public readonly contractSeed: Buffer;
|
|
17
|
+
public readonly contractSaltHash: Buffer;
|
|
18
|
+
public readonly deployerAddress: string;
|
|
19
|
+
|
|
20
|
+
constructor(raw: IRawContract) {
|
|
21
|
+
this.contractAddress = raw.contractAddress;
|
|
22
|
+
this.virtualAddress = raw.virtualAddress;
|
|
23
|
+
|
|
24
|
+
this.bytecode = Buffer.isBuffer(raw.bytecode)
|
|
25
|
+
? raw.bytecode
|
|
26
|
+
: Buffer.from(raw.bytecode, 'base64');
|
|
27
|
+
|
|
28
|
+
this.wasCompressed = raw.wasCompressed;
|
|
29
|
+
this.deployedTransactionId = raw.deployedTransactionId;
|
|
30
|
+
this.deployedTransactionHash = raw.deployedTransactionHash;
|
|
31
|
+
|
|
32
|
+
this.deployerPubKey = Buffer.isBuffer(raw.deployerPubKey)
|
|
33
|
+
? raw.deployerPubKey
|
|
34
|
+
: Buffer.from(raw.deployerPubKey, 'base64');
|
|
35
|
+
|
|
36
|
+
this.contractSeed = Buffer.isBuffer(raw.contractSeed)
|
|
37
|
+
? raw.contractSeed
|
|
38
|
+
: Buffer.from(raw.contractSeed, 'base64');
|
|
39
|
+
|
|
40
|
+
this.contractSaltHash = Buffer.isBuffer(raw.contractSaltHash)
|
|
41
|
+
? raw.contractSaltHash
|
|
42
|
+
: Buffer.from(raw.contractSaltHash, 'base64');
|
|
43
|
+
|
|
44
|
+
this.deployerAddress = raw.deployerAddress;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @description This interface represents an access list item.
|
|
3
|
+
* @interface IAccessListItem
|
|
4
|
+
* @category AccessList
|
|
5
|
+
*/
|
|
6
|
+
export interface IAccessListItem {
|
|
7
|
+
[key: string]: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @description This interface represents an access list.
|
|
12
|
+
* @interface IAccessList
|
|
13
|
+
* @category AccessList
|
|
14
|
+
*/
|
|
15
|
+
export interface IAccessList {
|
|
16
|
+
[key: string]: IAccessListItem;
|
|
17
|
+
}
|
|
@@ -1,15 +1,29 @@
|
|
|
1
|
-
import { NetEvent } from '@btc-vision/bsi-binary';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export interface
|
|
10
|
-
readonly
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import { BinaryReader, NetEvent } from '@btc-vision/bsi-binary';
|
|
2
|
+
import { IAccessList } from './IAccessList.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description This interface is used to define the call request error.
|
|
6
|
+
* @interface ICallRequestError
|
|
7
|
+
* @category Interfaces
|
|
8
|
+
*/
|
|
9
|
+
export interface ICallRequestError {
|
|
10
|
+
readonly error: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @description This interface is used to define the call result data.
|
|
15
|
+
* @interface ICallResultData
|
|
16
|
+
* @category Interfaces
|
|
17
|
+
*/
|
|
18
|
+
export interface ICallResultData {
|
|
19
|
+
readonly result: string | BinaryReader;
|
|
20
|
+
readonly events: NetEvent[];
|
|
21
|
+
readonly accessList: IAccessList;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @description This type is used to define the call result.
|
|
26
|
+
* @type ICallResult
|
|
27
|
+
* @category Interfaces
|
|
28
|
+
*/
|
|
29
|
+
export type ICallResult = ICallRequestError | ICallResultData;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BitcoinAddressLike } from '../../common/CommonTypes.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description This interface is used to define a contract.
|
|
5
|
+
* @interface IContract
|
|
6
|
+
* @category Contracts
|
|
7
|
+
*/
|
|
8
|
+
export interface IContract {
|
|
9
|
+
readonly address: BitcoinAddressLike;
|
|
10
|
+
}
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
readonly
|
|
8
|
-
readonly
|
|
9
|
-
readonly
|
|
10
|
-
readonly
|
|
11
|
-
readonly
|
|
12
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Interface for raw contract data.
|
|
3
|
+
* @interface IRawContract
|
|
4
|
+
* @cathegory Raw
|
|
5
|
+
*/
|
|
6
|
+
export interface IRawContract {
|
|
7
|
+
readonly contractAddress: string;
|
|
8
|
+
readonly virtualAddress: string;
|
|
9
|
+
readonly bytecode: Buffer | string;
|
|
10
|
+
readonly wasCompressed: boolean;
|
|
11
|
+
readonly deployedTransactionId: string;
|
|
12
|
+
readonly deployedTransactionHash: string;
|
|
13
|
+
readonly deployerPubKey: Buffer | string;
|
|
14
|
+
readonly contractSeed: Buffer | string;
|
|
15
|
+
readonly contractSaltHash: Buffer | string;
|
|
16
|
+
readonly deployerAddress: string;
|
|
17
|
+
}
|
|
@@ -1,60 +1,71 @@
|
|
|
1
|
-
/* Browser Crypto Shims */
|
|
2
|
-
import { hmac } from '@noble/hashes/hmac';
|
|
3
|
-
import { pbkdf2 } from '@noble/hashes/pbkdf2';
|
|
4
|
-
import { sha256 } from '@noble/hashes/sha256';
|
|
5
|
-
import { sha512 } from '@noble/hashes/sha512';
|
|
6
|
-
import { assertArgument } from 'ethers';
|
|
7
|
-
|
|
8
|
-
function getGlobal() {
|
|
9
|
-
if (typeof self !== 'undefined') {
|
|
10
|
-
return self;
|
|
11
|
-
}
|
|
12
|
-
if (typeof window !== 'undefined') {
|
|
13
|
-
return window;
|
|
14
|
-
}
|
|
15
|
-
if (typeof global !== 'undefined') {
|
|
16
|
-
return global;
|
|
17
|
-
}
|
|
18
|
-
throw new Error('unable to locate global object');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const anyGlobal = getGlobal();
|
|
22
|
-
const crypto = anyGlobal.crypto || anyGlobal.msCrypto;
|
|
23
|
-
|
|
24
|
-
export function createHash(algo) {
|
|
25
|
-
switch (algo) {
|
|
26
|
-
case 'sha256':
|
|
27
|
-
return sha256.create();
|
|
28
|
-
case 'sha512':
|
|
29
|
-
return sha512.create();
|
|
30
|
-
}
|
|
31
|
-
assertArgument(false, 'invalid hashing algorithm name', 'algorithm', algo);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function createHmac(_algo, key) {
|
|
35
|
-
const algo =
|
|
36
|
-
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
1
|
+
/* Browser Crypto Shims */
|
|
2
|
+
import { hmac } from '@noble/hashes/hmac';
|
|
3
|
+
import { pbkdf2 } from '@noble/hashes/pbkdf2';
|
|
4
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
5
|
+
import { sha512 } from '@noble/hashes/sha512';
|
|
6
|
+
import { assertArgument } from 'ethers';
|
|
7
|
+
|
|
8
|
+
function getGlobal() {
|
|
9
|
+
if (typeof self !== 'undefined') {
|
|
10
|
+
return self;
|
|
11
|
+
}
|
|
12
|
+
if (typeof window !== 'undefined') {
|
|
13
|
+
return window;
|
|
14
|
+
}
|
|
15
|
+
if (typeof global !== 'undefined') {
|
|
16
|
+
return global;
|
|
17
|
+
}
|
|
18
|
+
throw new Error('unable to locate global object');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const anyGlobal = getGlobal();
|
|
22
|
+
const crypto = anyGlobal.crypto || anyGlobal.msCrypto;
|
|
23
|
+
|
|
24
|
+
export function createHash(algo) {
|
|
25
|
+
switch (algo) {
|
|
26
|
+
case 'sha256':
|
|
27
|
+
return sha256.create();
|
|
28
|
+
case 'sha512':
|
|
29
|
+
return sha512.create();
|
|
30
|
+
}
|
|
31
|
+
assertArgument(false, 'invalid hashing algorithm name', 'algorithm', algo);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function createHmac(_algo, key) {
|
|
35
|
+
const algo = { sha256, sha512 }[_algo];
|
|
36
|
+
assertArgument(algo != null, 'invalid hmac algorithm', 'algorithm', _algo);
|
|
37
|
+
return hmac.create(algo, key);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function pbkdf2Sync(password, salt, iterations, keylen, _algo) {
|
|
41
|
+
const algo = { sha256, sha512 }[_algo];
|
|
42
|
+
assertArgument(algo != null, 'invalid pbkdf2 algorithm', 'algorithm', _algo);
|
|
43
|
+
return pbkdf2(algo, password, salt, { c: iterations, dkLen: keylen });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function randomBytes(length) {
|
|
47
|
+
assert(
|
|
48
|
+
crypto != null,
|
|
49
|
+
'platform does not support secure random numbers',
|
|
50
|
+
'UNSUPPORTED_OPERATION',
|
|
51
|
+
{
|
|
52
|
+
operation: 'randomBytes',
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
assertArgument(
|
|
56
|
+
Number.isInteger(length) && length > 0 && length <= 1024,
|
|
57
|
+
'invalid length',
|
|
58
|
+
'length',
|
|
59
|
+
length,
|
|
60
|
+
);
|
|
61
|
+
const result = new Uint8Array(length);
|
|
62
|
+
crypto.getRandomValues(result);
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default {
|
|
67
|
+
createHash,
|
|
68
|
+
createHmac,
|
|
69
|
+
pbkdf2Sync,
|
|
70
|
+
randomBytes,
|
|
71
|
+
};
|