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.
Files changed (253) hide show
  1. package/CONTRIBUTING.md +71 -0
  2. package/README.md +6 -7
  3. package/browser/_version.d.ts +1 -1
  4. package/browser/abi/BaseContractProperty.d.ts +3 -0
  5. package/browser/abi/BitcoinAbiTypes.d.ts +4 -0
  6. package/browser/abi/BitcoinInterface.d.ts +9 -0
  7. package/browser/abi/interfaces/BaseContractProperties.d.ts +4 -0
  8. package/browser/abi/interfaces/BitcoinAbiValue.d.ts +5 -0
  9. package/browser/abi/interfaces/BitcoinInterfaceAbi.d.ts +11 -0
  10. package/browser/bitcoin/UTXOs.d.ts +1 -6
  11. package/browser/bitcoin/interfaces/IUTXO.d.ts +7 -0
  12. package/browser/common/CommonTypes.d.ts +1 -0
  13. package/browser/contracts/CallResult.d.ts +6 -3
  14. package/browser/contracts/Contract.d.ts +28 -0
  15. package/browser/contracts/interfaces/ICallResult.d.ts +2 -3
  16. package/browser/contracts/interfaces/IContract.d.ts +4 -0
  17. package/browser/crypto/crypto-browser.d.ts +7 -0
  18. package/browser/index.js +1 -1
  19. package/browser/opnet.d.ts +11 -0
  20. package/browser/scripts/testContract.d.ts +1 -0
  21. package/browser/scripts/testMethods.d.ts +1 -0
  22. package/browser/utils/BitcoinUtils.d.ts +4 -0
  23. package/build/_version.d.ts +1 -1
  24. package/build/_version.js +1 -1
  25. package/build/abi/BaseContractProperty.d.ts +3 -0
  26. package/build/abi/BaseContractProperty.js +1 -0
  27. package/build/abi/BitcoinAbiTypes.d.ts +4 -0
  28. package/build/abi/BitcoinAbiTypes.js +5 -0
  29. package/build/abi/BitcoinInterface.d.ts +9 -0
  30. package/build/abi/BitcoinInterface.js +45 -0
  31. package/build/abi/BitcoinInterfaceAbi.d.ts +3 -0
  32. package/build/abi/BitcoinInterfaceAbi.js +1 -0
  33. package/build/abi/enums/ParamTypes.d.ts +0 -0
  34. package/build/abi/enums/ParamTypes.js +1 -0
  35. package/build/abi/interface/BitcoinInterface.d.ts +6 -0
  36. package/build/abi/interface/BitcoinInterface.js +12 -0
  37. package/build/abi/interface/BitcoinInterfaceAbi.d.ts +3 -0
  38. package/build/abi/interface/BitcoinInterfaceAbi.js +1 -0
  39. package/build/abi/interfaces/BaseContractProperties.d.ts +4 -0
  40. package/build/abi/interfaces/BaseContractProperties.js +1 -0
  41. package/build/abi/interfaces/BaseContractProperty.d.ts +2 -0
  42. package/build/abi/interfaces/BaseContractProperty.js +1 -0
  43. package/build/abi/interfaces/BitcoinAbiInput.d.ts +5 -0
  44. package/build/abi/interfaces/BitcoinAbiInput.js +1 -0
  45. package/build/abi/interfaces/BitcoinAbiValue.d.ts +5 -0
  46. package/build/abi/interfaces/BitcoinAbiValue.js +1 -0
  47. package/build/abi/interfaces/BitcoinInterfaceAbi.d.ts +11 -0
  48. package/build/abi/interfaces/BitcoinInterfaceAbi.js +1 -0
  49. package/build/bitcoin/UTXOs.d.ts +1 -6
  50. package/build/bitcoin/interfaces/IUTXO.d.ts +7 -0
  51. package/build/bitcoin/interfaces/IUTXO.js +1 -0
  52. package/build/common/CommonTypes.d.ts +1 -0
  53. package/build/contracts/CallResult.d.ts +6 -3
  54. package/build/contracts/CallResult.js +9 -2
  55. package/build/contracts/Contract.d.ts +28 -0
  56. package/build/contracts/Contract.js +206 -0
  57. package/build/contracts/interfaces/ICallResult.d.ts +2 -3
  58. package/build/contracts/interfaces/IContract.d.ts +4 -0
  59. package/build/contracts/interfaces/IContract.js +1 -0
  60. package/build/interface/BitcoinInterface.d.ts +6 -0
  61. package/build/interface/BitcoinInterface.js +12 -0
  62. package/build/interface/BitcoinInterfaceAbi.d.ts +3 -0
  63. package/build/interface/BitcoinInterfaceAbi.js +1 -0
  64. package/build/interface/Interface.d.ts +2 -0
  65. package/build/interface/Interface.js +2 -0
  66. package/build/interface.d.ts +0 -0
  67. package/build/interface.js +1 -0
  68. package/build/opnet.d.ts +11 -0
  69. package/build/opnet.js +11 -0
  70. package/build/scripts/test.js +12 -0
  71. package/build/scripts/testContract.d.ts +1 -0
  72. package/build/scripts/testContract.js +28 -0
  73. package/build/scripts/testMethods.d.ts +1 -0
  74. package/build/scripts/testMethods.js +24 -0
  75. package/build/utils/BitcoinUtils.d.ts +4 -0
  76. package/build/utils/BitcoinUtils.js +7 -0
  77. package/cjs/_version.d.ts +1 -0
  78. package/cjs/_version.js +4 -0
  79. package/cjs/abi/BaseContractProperty.d.ts +3 -0
  80. package/cjs/abi/BaseContractProperty.js +2 -0
  81. package/cjs/abi/BitcoinAbiTypes.d.ts +4 -0
  82. package/cjs/abi/BitcoinAbiTypes.js +8 -0
  83. package/cjs/abi/BitcoinInterface.d.ts +9 -0
  84. package/cjs/abi/BitcoinInterface.js +49 -0
  85. package/cjs/abi/interfaces/BaseContractProperties.d.ts +4 -0
  86. package/cjs/abi/interfaces/BaseContractProperties.js +2 -0
  87. package/cjs/abi/interfaces/BitcoinAbiValue.d.ts +5 -0
  88. package/cjs/abi/interfaces/BitcoinAbiValue.js +2 -0
  89. package/cjs/abi/interfaces/BitcoinInterfaceAbi.d.ts +11 -0
  90. package/cjs/abi/interfaces/BitcoinInterfaceAbi.js +2 -0
  91. package/cjs/bitcoin/BitcoinAddress.d.ts +20 -0
  92. package/cjs/bitcoin/BitcoinAddress.js +116 -0
  93. package/cjs/bitcoin/UTXOs.d.ts +15 -0
  94. package/cjs/bitcoin/UTXOs.js +16 -0
  95. package/cjs/block/Block.d.ts +2 -3
  96. package/cjs/block/Block.js +3 -28
  97. package/cjs/block/interfaces/IBlock.d.ts +28 -0
  98. package/cjs/block/interfaces/IBlock.js +2 -0
  99. package/cjs/common/CommonTypes.d.ts +4 -0
  100. package/cjs/common/CommonTypes.js +2 -0
  101. package/cjs/contracts/CallResult.d.ts +13 -0
  102. package/cjs/contracts/CallResult.js +25 -0
  103. package/cjs/contracts/Contract.d.ts +28 -0
  104. package/cjs/contracts/Contract.js +212 -0
  105. package/cjs/contracts/ContractData.d.ts +15 -0
  106. package/cjs/contracts/ContractData.js +36 -0
  107. package/cjs/contracts/interfaces/IAccessList.d.ts +6 -0
  108. package/cjs/contracts/interfaces/IAccessList.js +2 -0
  109. package/cjs/contracts/interfaces/ICallResult.d.ts +11 -0
  110. package/cjs/contracts/interfaces/ICallResult.js +2 -0
  111. package/cjs/contracts/interfaces/IContract.d.ts +4 -0
  112. package/cjs/contracts/interfaces/IContract.js +2 -0
  113. package/cjs/contracts/interfaces/IRawContract.d.ts +13 -0
  114. package/cjs/contracts/interfaces/IRawContract.js +2 -0
  115. package/cjs/crypto/crypto.d.ts +2 -0
  116. package/cjs/crypto/crypto.js +8 -0
  117. package/cjs/index.d.ts +3 -11
  118. package/cjs/index.js +16 -11
  119. package/cjs/opnet.d.ts +36 -0
  120. package/cjs/opnet.js +54 -0
  121. package/cjs/providers/AbstractRpcProvider.d.ts +34 -0
  122. package/cjs/providers/AbstractRpcProvider.js +161 -0
  123. package/cjs/providers/JSONRpcProvider.d.ts +5 -9
  124. package/cjs/providers/JSONRpcProvider.js +4 -29
  125. package/cjs/providers/WebsocketRpcProvider.d.ts +8 -0
  126. package/cjs/providers/WebsocketRpcProvider.js +27 -0
  127. package/cjs/scripts/testContract.d.ts +1 -0
  128. package/cjs/scripts/testContract.js +30 -0
  129. package/cjs/scripts/testMethods.d.ts +1 -0
  130. package/cjs/scripts/testMethods.js +26 -0
  131. package/cjs/serialize/BigInt.d.ts +6 -0
  132. package/cjs/serialize/BigInt.js +5 -0
  133. package/cjs/storage/StoredValue.d.ts +10 -0
  134. package/cjs/storage/StoredValue.js +28 -0
  135. package/cjs/storage/interfaces/IStorageValue.d.ts +9 -0
  136. package/cjs/storage/interfaces/IStorageValue.js +2 -0
  137. package/cjs/transactions/DeploymentTransaction.d.ts +10 -1
  138. package/cjs/transactions/DeploymentTransaction.js +16 -0
  139. package/cjs/transactions/GenericTransaction.d.ts +1 -1
  140. package/cjs/transactions/InteractionTransaction.d.ts +1 -1
  141. package/cjs/transactions/Transaction.d.ts +1 -1
  142. package/cjs/transactions/Transaction.js +1 -1
  143. package/cjs/transactions/TransactionParser.d.ts +7 -0
  144. package/cjs/transactions/TransactionParser.js +32 -0
  145. package/cjs/transactions/TransactionReceipt.d.ts +10 -0
  146. package/cjs/transactions/TransactionReceipt.js +16 -0
  147. package/cjs/transactions/interfaces/ITransaction.d.ts +39 -0
  148. package/cjs/transactions/interfaces/ITransaction.js +2 -0
  149. package/cjs/transactions/interfaces/ITransactionReceipt.d.ts +8 -0
  150. package/cjs/transactions/interfaces/ITransactionReceipt.js +2 -0
  151. package/cjs/utils/BitcoinUtils.d.ts +4 -0
  152. package/cjs/utils/BitcoinUtils.js +14 -0
  153. package/docs/assets/navigation.js +1 -1
  154. package/docs/assets/search.js +1 -1
  155. package/docs/classes/AbstractRpcProvider.html +15 -15
  156. package/docs/classes/BaseContract.html +187 -0
  157. package/docs/classes/BitcoinAddress.html +5 -2
  158. package/docs/classes/BitcoinInterface.html +182 -0
  159. package/docs/classes/BitcoinUtils.html +183 -0
  160. package/docs/classes/Block.html +5 -3
  161. package/docs/classes/CallResult.html +6 -2
  162. package/docs/classes/ContractData.html +4 -2
  163. package/docs/classes/DeploymentTransaction.html +4 -2
  164. package/docs/classes/GenericTransaction.html +4 -2
  165. package/docs/classes/IBaseContract.html +192 -0
  166. package/docs/classes/InteractionTransaction.html +3 -2
  167. package/docs/classes/JSONRpcProvider.html +14 -14
  168. package/docs/classes/StoredValue.html +4 -2
  169. package/docs/classes/TransactionBase.html +4 -2
  170. package/docs/classes/TransactionInput.html +3 -2
  171. package/docs/classes/TransactionOutput.html +3 -2
  172. package/docs/classes/TransactionParser.html +3 -2
  173. package/docs/classes/TransactionReceipt.html +3 -2
  174. package/docs/classes/UTXO.html +4 -2
  175. package/docs/classes/WebSocketRpcProvider.html +14 -14
  176. package/docs/enums/ABIDataTypes.html +183 -0
  177. package/docs/enums/BitcoinAbiTypes.html +178 -0
  178. package/docs/enums/OPNetTransactionTypes.html +2 -2
  179. package/docs/functions/getContract.html +180 -0
  180. package/docs/hierarchy.html +1 -1
  181. package/docs/index.html +4 -5
  182. package/docs/interfaces/BaseContractProperties.html +176 -0
  183. package/docs/interfaces/BitcoinAbiValue.html +180 -0
  184. package/docs/interfaces/BitcoinInterfaceAbiBase.html +182 -0
  185. package/docs/interfaces/IAccessList.html +3 -1
  186. package/docs/interfaces/IAccessListItem.html +3 -1
  187. package/docs/interfaces/IBlock.html +5 -3
  188. package/docs/interfaces/IBlockCommon.html +5 -3
  189. package/docs/interfaces/ICallRequestError.html +4 -2
  190. package/docs/interfaces/ICallResultData.html +4 -2
  191. package/docs/interfaces/IContract.html +177 -0
  192. package/docs/interfaces/IDeploymentTransaction.html +4 -2
  193. package/docs/interfaces/IGenericTransaction.html +4 -2
  194. package/docs/interfaces/IInteractionTransaction.html +4 -2
  195. package/docs/interfaces/IRawContract.html +5 -2
  196. package/docs/interfaces/IStorageValue.html +4 -2
  197. package/docs/interfaces/ITransactionBase.html +4 -2
  198. package/docs/interfaces/ITransactionInput.html +3 -2
  199. package/docs/interfaces/ITransactionOutput.html +3 -2
  200. package/docs/interfaces/ITransactionReceipt.html +4 -2
  201. package/docs/interfaces/IUTXO.html +4 -2
  202. package/docs/modules.html +38 -24
  203. package/docs/types/BaseContractProperty.html +176 -0
  204. package/docs/types/BitcoinAddressLike.html +1 -1
  205. package/docs/types/BitcoinInterfaceAbi.html +174 -0
  206. package/docs/types/BlockHeaderChecksumProof.html +1 -1
  207. package/docs/types/DecodedCallResult.html +174 -0
  208. package/docs/types/ICallResult.html +2 -1
  209. package/docs/types/ITransaction.html +2 -1
  210. package/docs/types/PointerLike.html +1 -1
  211. package/docs/types/UTXOs.html +3 -1
  212. package/docs/variables/version.html +1 -1
  213. package/package.json +6 -4
  214. package/src/_version.ts +1 -1
  215. package/src/abi/BaseContractProperty.ts +8 -0
  216. package/src/abi/BitcoinAbiTypes.ts +9 -0
  217. package/src/abi/BitcoinInterface.ts +63 -0
  218. package/src/abi/interfaces/BaseContractProperties.ts +9 -0
  219. package/src/abi/interfaces/BitcoinAbiValue.ts +18 -0
  220. package/src/abi/interfaces/BitcoinInterfaceAbi.ts +19 -0
  221. package/src/bitcoin/BitcoinAddress.ts +5 -0
  222. package/src/bitcoin/UTXOs.ts +28 -26
  223. package/src/bitcoin/interfaces/IUTXO.ts +12 -0
  224. package/src/block/Block.ts +5 -0
  225. package/src/block/interfaces/IBlock.ts +46 -38
  226. package/src/common/CommonTypes.ts +16 -4
  227. package/src/contracts/CallResult.ts +34 -20
  228. package/src/contracts/Contract.ts +321 -0
  229. package/src/contracts/ContractData.ts +46 -41
  230. package/src/contracts/interfaces/IAccessList.ts +17 -7
  231. package/src/contracts/interfaces/ICallResult.ts +29 -15
  232. package/src/contracts/interfaces/IContract.ts +10 -0
  233. package/src/contracts/interfaces/IRawContract.ts +17 -12
  234. package/src/crypto/crypto-browser.js +71 -60
  235. package/src/opnet.ts +17 -0
  236. package/src/providers/AbstractRpcProvider.ts +352 -351
  237. package/src/scripts/testContract.ts +51 -0
  238. package/src/scripts/{test.ts → testMethods.ts} +52 -54
  239. package/src/scripts/test_abi.json +31 -0
  240. package/src/storage/StoredValue.ts +35 -30
  241. package/src/storage/interfaces/IStorageValue.ts +15 -10
  242. package/src/transactions/DeploymentTransaction.ts +41 -36
  243. package/src/transactions/GenericTransaction.ts +19 -12
  244. package/src/transactions/InteractionTransaction.ts +44 -40
  245. package/src/transactions/Transaction.ts +8 -0
  246. package/src/transactions/TransactionInput.ts +8 -0
  247. package/src/transactions/TransactionOutput.ts +8 -0
  248. package/src/transactions/TransactionParser.ts +47 -43
  249. package/src/transactions/TransactionReceipt.ts +22 -18
  250. package/src/transactions/interfaces/ITransaction.ts +78 -52
  251. package/src/transactions/interfaces/ITransactionReceipt.ts +14 -9
  252. package/src/utils/BitcoinUtils.ts +22 -0
  253. package/webpack.config.js +71 -75
@@ -1,351 +1,352 @@
1
- import '../serialize/BigInt.js';
2
- import { BufferHelper } from '@btc-vision/bsi-binary';
3
- import { Network, networks } from 'bitcoinjs-lib';
4
-
5
- import {
6
- BigNumberish,
7
- BlockTag,
8
- JsonRpcApiProvider,
9
- JsonRpcError,
10
- JsonRpcPayload,
11
- JsonRpcResult,
12
- } from 'ethers';
13
-
14
- import { IUTXO, UTXO } from '../bitcoin/UTXOs.js';
15
- import { Block } from '../block/Block.js';
16
- import { IBlock } from '../block/interfaces/IBlock.js';
17
- import { BitcoinAddressLike } from '../common/CommonTypes.js';
18
- import { CallResult } from '../contracts/CallResult.js';
19
- import { ContractData } from '../contracts/ContractData.js';
20
- import { ICallRequestError, ICallResult } from '../contracts/interfaces/ICallResult.js';
21
- import { IRawContract } from '../contracts/interfaces/IRawContract.js';
22
- import { OPNetTransactionTypes } from '../interfaces/opnet/OPNetTransactionTypes.js';
23
- import { IStorageValue } from '../storage/interfaces/IStorageValue.js';
24
- import { StoredValue } from '../storage/StoredValue.js';
25
- import { ITransaction } from '../transactions/interfaces/ITransaction.js';
26
- import { ITransactionReceipt } from '../transactions/interfaces/ITransactionReceipt.js';
27
- import { TransactionBase } from '../transactions/Transaction.js';
28
- import { TransactionParser } from '../transactions/TransactionParser.js';
29
- import { TransactionReceipt } from '../transactions/TransactionReceipt.js';
30
-
31
- type JsonRpcCallResult = (JsonRpcResult | JsonRpcError)[];
32
-
33
- /**
34
- * @description This class is used to provide an abstract RPC provider.
35
- * @abstract
36
- * @class AbstractRpcProvider
37
- * @category Providers
38
- */
39
- export abstract class AbstractRpcProvider {
40
- protected abstract readonly provider: JsonRpcApiProvider;
41
-
42
- private nextId: number = 0;
43
-
44
- protected constructor() {}
45
-
46
- /**
47
- * Get the latest block number.
48
- * @description This method is used to get the latest block number.
49
- * @returns {Promise<number>} The latest block number
50
- * @example await getBlockNumber();
51
- */
52
- public async getBlockNumber(): Promise<number> {
53
- return await this.provider.getBlockNumber();
54
- }
55
-
56
- /**
57
- * Get block by number or hash.
58
- * @param {BlockTag} blockNumberOrHash The block number or hash
59
- * @param {boolean} prefetchTxs Whether to prefetch transactions
60
- * @description This method is used to get a block by its number or hash.
61
- * @returns {Promise<Block>} The requested block
62
- * @throws {Error} If the block is not found
63
- * @example await getBlock(123456);
64
- */
65
- public async getBlock(
66
- blockNumberOrHash: BlockTag,
67
- prefetchTxs: boolean = false,
68
- ): Promise<Block> {
69
- const method =
70
- typeof blockNumberOrHash === 'string' ? 'btc_getBlockByHash' : 'btc_getBlockByNumber';
71
-
72
- const payload: JsonRpcPayload = this.buildJsonRpcPayload(method, [
73
- blockNumberOrHash,
74
- prefetchTxs,
75
- ]);
76
-
77
- const block: JsonRpcResult = await this.callPayloadSingle(payload);
78
-
79
- const result: IBlock = block.result;
80
- return new Block(result);
81
- }
82
-
83
- /**
84
- * Get block by hash. This is the same method as getBlock.
85
- * @param {string} blockHash The block hash
86
- * @description This method is used to get a block by its hash. Note that this method is the same as getBlock.
87
- * @returns {Promise<Block>} The requested block
88
- * @throws {Error} If the block is not found
89
- */
90
- public async getBlockByHash(blockHash: string): Promise<Block> {
91
- return await this.getBlock(blockHash);
92
- }
93
-
94
- /**
95
- * Get the bitcoin balance of an address.
96
- * @param {BitcoinAddressLike} addressLike The address to get the balance of
97
- * @description This method is used to get the balance of a bitcoin address.
98
- * @returns {Promise<bigint>} The balance of the address
99
- * @example await getBalance('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq');
100
- */
101
- public async getBalance(addressLike: BitcoinAddressLike): Promise<bigint> {
102
- const address: string = addressLike.toString();
103
- const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getBalance', [address]);
104
- const rawBalance: JsonRpcResult = await this.callPayloadSingle(payload);
105
-
106
- const result: string = rawBalance.result;
107
- if (!result || (result && !result.startsWith('0x'))) {
108
- throw new Error(`Invalid balance returned from provider: ${result}`);
109
- }
110
-
111
- return BigInt(result);
112
- }
113
-
114
- /**
115
- * Get the UTXOs (Unspent Transaction Outputs) of an address.
116
- * @description This method is used to get the UTXOs of a bitcoin address.
117
- * @param {BitcoinAddressLike} address The address to get the UTXOs of
118
- * @param {boolean} optimize Whether to optimize the UTXOs
119
- * @returns {Promise<UTXOs>} The UTXOs of the address
120
- * @example await getUXTOsOf('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq');
121
- * @throws {Error} If something went wrong while fetching the UTXOs
122
- */
123
- public async getUXTOs(
124
- address: BitcoinAddressLike,
125
- optimize: boolean = false,
126
- ): Promise<unknown> {
127
- const addressStr: string = address.toString();
128
- const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getUTXOs', [
129
- addressStr,
130
- optimize,
131
- ]);
132
-
133
- const rawUXTOs: JsonRpcResult = await this.callPayloadSingle(payload);
134
- const result: IUTXO[] = rawUXTOs.result || [];
135
-
136
- return result.map((utxo: IUTXO) => {
137
- return new UTXO(utxo);
138
- });
139
- }
140
-
141
- /**
142
- * Get a transaction by its hash or hash id.
143
- * @description This method is used to get a transaction by its hash or hash id.
144
- * @param {string} txHash The transaction hash
145
- * @returns {Promise<TransactionBase<OPNetTransactionTypes>>} The requested transaction
146
- * @example await getTransaction('63e77ba9fa4262b3d4d0d9d97fa8a7359534606c3f3af096284662e3f619f374');
147
- * @throws {Error} If something went wrong while fetching the transaction
148
- */
149
- public async getTransaction(txHash: string): Promise<TransactionBase<OPNetTransactionTypes>> {
150
- const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getTransactionByHash', [
151
- txHash,
152
- ]);
153
-
154
- const rawTransaction: JsonRpcResult = await this.callPayloadSingle(payload);
155
- const result: ITransaction = rawTransaction.result;
156
-
157
- return TransactionParser.parseTransaction(result);
158
- }
159
-
160
- /**
161
- * Get a transaction receipt by its hash.
162
- * @description This method is used to get a transaction receipt by its hash.
163
- * @param {string} txHash The transaction hash
164
- * @returns {Promise<ITransactionReceipt>} The requested transaction receipt
165
- * @example await getTransactionReceipt('63e77ba9fa4262b3d4d0d9d97fa8a7359534606c3f3af096284662e3f619f374');
166
- * @throws {Error} Something went wrong while fetching the transaction receipt
167
- */
168
- public async getTransactionReceipt(txHash: string): Promise<ITransactionReceipt> {
169
- const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getTransactionReceipt', [
170
- txHash,
171
- ]);
172
-
173
- const rawTransaction: JsonRpcResult = await this.callPayloadSingle(payload);
174
- return new TransactionReceipt(rawTransaction.result);
175
- }
176
-
177
- /**
178
- * Get the current connected network type.
179
- * @description This method is used to get the current connected network type.
180
- * @returns {Promise<Network>} The current connected network type
181
- * @throws {Error} If the chain id is invalid
182
- */
183
- public async getNetwork(): Promise<Network> {
184
- const network = await this.provider.getNetwork();
185
-
186
- switch (network.chainId) {
187
- case 1n:
188
- return networks.bitcoin;
189
- case 2n:
190
- return networks.testnet;
191
- case 3n:
192
- return networks.regtest;
193
-
194
- default:
195
- throw new Error(`Invalid chain id: ${network.chainId}`);
196
- }
197
- }
198
-
199
- /**
200
- * Get the contract code of an address.
201
- * @description This method is used to get the contract code of an address.
202
- * @param {BitcoinAddressLike} address The address of the contract
203
- * @param {boolean} [onlyBytecode] Whether to return only the bytecode
204
- * @returns {Promise<ContractData | Buffer>} The contract data or bytecode
205
- * @example await getCode('tb1pth90usc4f528aqphpjrfkkdm4vy8hxnt5gps6aau2nva6pxeshtqqzlt3a');
206
- * @throws {Error} If something went wrong while fetching the contract code
207
- */
208
- public async getCode(
209
- address: BitcoinAddressLike,
210
- onlyBytecode: boolean = false,
211
- ): Promise<ContractData | Buffer> {
212
- const addressStr: string = address.toString();
213
- const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getCode', [
214
- addressStr,
215
- onlyBytecode,
216
- ]);
217
-
218
- const rawCode: JsonRpcResult = await this.callPayloadSingle(payload);
219
- const result: IRawContract | { bytecode: string } = rawCode.result;
220
-
221
- if ('contractAddress' in result) {
222
- return new ContractData(result);
223
- } else {
224
- return Buffer.from(result.bytecode, 'base64');
225
- }
226
- }
227
-
228
- /**
229
- * Get the storage at a specific address and pointer.
230
- * @description This method is used to get the storage at a specific address and pointer.
231
- * @param {BitcoinAddressLike} address The address to get the storage from
232
- * @param {BigNumberish} rawPointer The pointer to get the storage from as base64 or bigint
233
- * @param {boolean} proofs Whether to send proofs or not
234
- * @param {BigNumberish} [height] The height to get the storage from
235
- * @returns {Promise<StoredValue>} The storage value
236
- * @example await getStorageAt('tb1pth90usc4f528aqphpjrfkkdm4vy8hxnt5gps6aau2nva6pxeshtqqzlt3a', 'EXLK/QhEQMI5d9DrthLvozT+UcDQ7WuSPaz7g8GV3AQ=');
237
- * @throws {Error} If something went wrong while fetching the storage
238
- */
239
- public async getStorageAt(
240
- address: BitcoinAddressLike,
241
- rawPointer: bigint | string,
242
- proofs: boolean = true,
243
- height?: BigNumberish,
244
- ): Promise<StoredValue> {
245
- const addressStr: string = address.toString();
246
- const pointer: string =
247
- typeof rawPointer === 'string' ? rawPointer : this.bigintToBase64(rawPointer);
248
-
249
- const params: [string, string, boolean, string?] = [addressStr, pointer, proofs];
250
-
251
- if (height) {
252
- params.push(height.toString());
253
- }
254
-
255
- const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getStorageAt', params);
256
-
257
- const rawStorage: JsonRpcResult = await this.callPayloadSingle(payload);
258
- const result: IStorageValue = rawStorage.result;
259
-
260
- return new StoredValue(result);
261
- }
262
-
263
- /**
264
- * Call a contract function with a given calldata.
265
- * @description This method is used to call a contract function with a given calldata.
266
- * @param {BitcoinAddressLike} to The address of the contract
267
- * @param {Buffer} data The calldata of the contract function
268
- * @param {BitcoinAddressLike} [from] The address to call the contract from
269
- * @param {BigNumberish} [height] The height to call the contract from
270
- * @returns {Promise<CallResult>} The result of the contract function call
271
- * @example await call('tb1pth90usc4f528aqphpjrfkkdm4vy8hxnt5gps6aau2nva6pxeshtqqzlt3a', Buffer.from('0x12345678'));
272
- * @throws {Error} If something went wrong while calling the contract
273
- */
274
- public async call(
275
- to: BitcoinAddressLike,
276
- data: Buffer | string,
277
- from?: BitcoinAddressLike,
278
- height?: BigNumberish,
279
- ): Promise<CallResult | ICallRequestError> {
280
- const toStr: string = to.toString();
281
- const fromStr: string | null = from ? from.toString() : null;
282
-
283
- let dataStr: string = Buffer.isBuffer(data) ? this.bufferToHex(data) : data;
284
- if (dataStr.startsWith('0x')) {
285
- dataStr = dataStr.slice(2);
286
- }
287
-
288
- const params: [string, string, string?, string?] = [toStr, dataStr];
289
- if (fromStr) {
290
- params.push(fromStr);
291
- }
292
-
293
- if (height) {
294
- params.push(height.toString());
295
- }
296
-
297
- const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_call', params);
298
- const rawCall: JsonRpcResult = await this.callPayloadSingle(payload);
299
-
300
- const result: ICallResult = rawCall.result;
301
- if ('error' in result) {
302
- return result;
303
- }
304
-
305
- return new CallResult(result);
306
- }
307
-
308
- protected abstract providerUrl(url: string): string;
309
-
310
- private bufferToHex(buffer: Buffer): string {
311
- return buffer.toString('hex');
312
- }
313
-
314
- private bigintToBase64(bigint: bigint): string {
315
- return Buffer.from(BufferHelper.pointerToUint8Array(bigint)).toString('base64');
316
- }
317
-
318
- /**
319
- * Send a single payload. This method is used to send a single payload.
320
- * @param {JsonRpcPayload} payload The payload to send
321
- * @returns {Promise<JsonRpcResult>} The result of the payload
322
- * @throws {Error} If no data is returned
323
- * @private
324
- */
325
- private async callPayloadSingle(payload: JsonRpcPayload): Promise<JsonRpcResult> {
326
- const rawData: JsonRpcCallResult = await this.provider._send(payload);
327
- if (rawData.length !== 1) {
328
- throw new Error(`Unexpected response length for get block request: ${rawData.length}`);
329
- }
330
-
331
- const data = rawData.shift();
332
- if (!data) {
333
- throw new Error('Block not found');
334
- }
335
-
336
- if ('error' in data) {
337
- throw new Error(`Something went wrong while fetching: ${data.error.message}`);
338
- }
339
-
340
- return data;
341
- }
342
-
343
- private buildJsonRpcPayload(method: string, params: unknown[]): JsonRpcPayload {
344
- return {
345
- method: method,
346
- params: params,
347
- id: this.nextId++,
348
- jsonrpc: '2.0',
349
- };
350
- }
351
- }
1
+ import '../serialize/BigInt.js';
2
+ import { BufferHelper } from '@btc-vision/bsi-binary';
3
+ import { Network, networks } from 'bitcoinjs-lib';
4
+
5
+ import {
6
+ BigNumberish,
7
+ BlockTag,
8
+ JsonRpcApiProvider,
9
+ JsonRpcError,
10
+ JsonRpcPayload,
11
+ JsonRpcResult,
12
+ } from 'ethers';
13
+ import { IUTXO } from '../bitcoin/interfaces/IUTXO.js';
14
+
15
+ import { UTXO, UTXOs } from '../bitcoin/UTXOs.js';
16
+ import { Block } from '../block/Block.js';
17
+ import { IBlock } from '../block/interfaces/IBlock.js';
18
+ import { BitcoinAddressLike } from '../common/CommonTypes.js';
19
+ import { CallResult } from '../contracts/CallResult.js';
20
+ import { ContractData } from '../contracts/ContractData.js';
21
+ import { ICallRequestError, ICallResult } from '../contracts/interfaces/ICallResult.js';
22
+ import { IRawContract } from '../contracts/interfaces/IRawContract.js';
23
+ import { OPNetTransactionTypes } from '../interfaces/opnet/OPNetTransactionTypes.js';
24
+ import { IStorageValue } from '../storage/interfaces/IStorageValue.js';
25
+ import { StoredValue } from '../storage/StoredValue.js';
26
+ import { ITransaction } from '../transactions/interfaces/ITransaction.js';
27
+ import { ITransactionReceipt } from '../transactions/interfaces/ITransactionReceipt.js';
28
+ import { TransactionBase } from '../transactions/Transaction.js';
29
+ import { TransactionParser } from '../transactions/TransactionParser.js';
30
+ import { TransactionReceipt } from '../transactions/TransactionReceipt.js';
31
+
32
+ type JsonRpcCallResult = (JsonRpcResult | JsonRpcError)[];
33
+
34
+ /**
35
+ * @description This class is used to provide an abstract RPC provider.
36
+ * @abstract
37
+ * @class AbstractRpcProvider
38
+ * @category Providers
39
+ */
40
+ export abstract class AbstractRpcProvider {
41
+ protected abstract readonly provider: JsonRpcApiProvider;
42
+
43
+ private nextId: number = 0;
44
+
45
+ protected constructor() {}
46
+
47
+ /**
48
+ * Get the latest block number.
49
+ * @description This method is used to get the latest block number.
50
+ * @returns {Promise<number>} The latest block number
51
+ * @example await getBlockNumber();
52
+ */
53
+ public async getBlockNumber(): Promise<number> {
54
+ return await this.provider.getBlockNumber();
55
+ }
56
+
57
+ /**
58
+ * Get block by number or hash.
59
+ * @param {BlockTag} blockNumberOrHash The block number or hash
60
+ * @param {boolean} prefetchTxs Whether to prefetch transactions
61
+ * @description This method is used to get a block by its number or hash.
62
+ * @returns {Promise<Block>} The requested block
63
+ * @throws {Error} If the block is not found
64
+ * @example await getBlock(123456);
65
+ */
66
+ public async getBlock(
67
+ blockNumberOrHash: BlockTag,
68
+ prefetchTxs: boolean = false,
69
+ ): Promise<Block> {
70
+ const method =
71
+ typeof blockNumberOrHash === 'string' ? 'btc_getBlockByHash' : 'btc_getBlockByNumber';
72
+
73
+ const payload: JsonRpcPayload = this.buildJsonRpcPayload(method, [
74
+ blockNumberOrHash,
75
+ prefetchTxs,
76
+ ]);
77
+
78
+ const block: JsonRpcResult = await this.callPayloadSingle(payload);
79
+
80
+ const result: IBlock = block.result;
81
+ return new Block(result);
82
+ }
83
+
84
+ /**
85
+ * Get block by hash. This is the same method as getBlock.
86
+ * @param {string} blockHash The block hash
87
+ * @description This method is used to get a block by its hash. Note that this method is the same as getBlock.
88
+ * @returns {Promise<Block>} The requested block
89
+ * @throws {Error} If the block is not found
90
+ */
91
+ public async getBlockByHash(blockHash: string): Promise<Block> {
92
+ return await this.getBlock(blockHash);
93
+ }
94
+
95
+ /**
96
+ * Get the bitcoin balance of an address.
97
+ * @param {BitcoinAddressLike} addressLike The address to get the balance of
98
+ * @description This method is used to get the balance of a bitcoin address.
99
+ * @returns {Promise<bigint>} The balance of the address
100
+ * @example await getBalance('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq');
101
+ */
102
+ public async getBalance(addressLike: BitcoinAddressLike): Promise<bigint> {
103
+ const address: string = addressLike.toString();
104
+ const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getBalance', [address]);
105
+ const rawBalance: JsonRpcResult = await this.callPayloadSingle(payload);
106
+
107
+ const result: string = rawBalance.result;
108
+ if (!result || (result && !result.startsWith('0x'))) {
109
+ throw new Error(`Invalid balance returned from provider: ${result}`);
110
+ }
111
+
112
+ return BigInt(result);
113
+ }
114
+
115
+ /**
116
+ * Get the UTXOs (Unspent Transaction Outputs) of an address.
117
+ * @description This method is used to get the UTXOs of a bitcoin address.
118
+ * @param {BitcoinAddressLike} address The address to get the UTXOs of
119
+ * @param {boolean} optimize Whether to optimize the UTXOs
120
+ * @returns {Promise<UTXOs>} The UTXOs of the address
121
+ * @example await getUXTOs('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq');
122
+ * @throws {Error} If something went wrong while fetching the UTXOs
123
+ */
124
+ public async getUXTOs(
125
+ address: BitcoinAddressLike,
126
+ optimize: boolean = false,
127
+ ): Promise<unknown> {
128
+ const addressStr: string = address.toString();
129
+ const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getUTXOs', [
130
+ addressStr,
131
+ optimize,
132
+ ]);
133
+
134
+ const rawUXTOs: JsonRpcResult = await this.callPayloadSingle(payload);
135
+ const result: UTXOs = rawUXTOs.result || [];
136
+
137
+ return result.map((utxo: IUTXO) => {
138
+ return new UTXO(utxo);
139
+ });
140
+ }
141
+
142
+ /**
143
+ * Get a transaction by its hash or hash id.
144
+ * @description This method is used to get a transaction by its hash or hash id.
145
+ * @param {string} txHash The transaction hash
146
+ * @returns {Promise<TransactionBase<OPNetTransactionTypes>>} The requested transaction
147
+ * @example await getTransaction('63e77ba9fa4262b3d4d0d9d97fa8a7359534606c3f3af096284662e3f619f374');
148
+ * @throws {Error} If something went wrong while fetching the transaction
149
+ */
150
+ public async getTransaction(txHash: string): Promise<TransactionBase<OPNetTransactionTypes>> {
151
+ const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getTransactionByHash', [
152
+ txHash,
153
+ ]);
154
+
155
+ const rawTransaction: JsonRpcResult = await this.callPayloadSingle(payload);
156
+ const result: ITransaction = rawTransaction.result;
157
+
158
+ return TransactionParser.parseTransaction(result);
159
+ }
160
+
161
+ /**
162
+ * Get a transaction receipt by its hash.
163
+ * @description This method is used to get a transaction receipt by its hash.
164
+ * @param {string} txHash The transaction hash
165
+ * @returns {Promise<ITransactionReceipt>} The requested transaction receipt
166
+ * @example await getTransactionReceipt('63e77ba9fa4262b3d4d0d9d97fa8a7359534606c3f3af096284662e3f619f374');
167
+ * @throws {Error} Something went wrong while fetching the transaction receipt
168
+ */
169
+ public async getTransactionReceipt(txHash: string): Promise<ITransactionReceipt> {
170
+ const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getTransactionReceipt', [
171
+ txHash,
172
+ ]);
173
+
174
+ const rawTransaction: JsonRpcResult = await this.callPayloadSingle(payload);
175
+ return new TransactionReceipt(rawTransaction.result);
176
+ }
177
+
178
+ /**
179
+ * Get the current connected network type.
180
+ * @description This method is used to get the current connected network type.
181
+ * @returns {Promise<Network>} The current connected network type
182
+ * @throws {Error} If the chain id is invalid
183
+ */
184
+ public async getNetwork(): Promise<Network> {
185
+ const network = await this.provider.getNetwork();
186
+
187
+ switch (network.chainId) {
188
+ case 1n:
189
+ return networks.bitcoin;
190
+ case 2n:
191
+ return networks.testnet;
192
+ case 3n:
193
+ return networks.regtest;
194
+
195
+ default:
196
+ throw new Error(`Invalid chain id: ${network.chainId}`);
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Get the contract code of an address.
202
+ * @description This method is used to get the contract code of an address.
203
+ * @param {BitcoinAddressLike} address The address of the contract
204
+ * @param {boolean} [onlyBytecode] Whether to return only the bytecode
205
+ * @returns {Promise<ContractData | Buffer>} The contract data or bytecode
206
+ * @example await getCode('tb1pth90usc4f528aqphpjrfkkdm4vy8hxnt5gps6aau2nva6pxeshtqqzlt3a');
207
+ * @throws {Error} If something went wrong while fetching the contract code
208
+ */
209
+ public async getCode(
210
+ address: BitcoinAddressLike,
211
+ onlyBytecode: boolean = false,
212
+ ): Promise<ContractData | Buffer> {
213
+ const addressStr: string = address.toString();
214
+ const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getCode', [
215
+ addressStr,
216
+ onlyBytecode,
217
+ ]);
218
+
219
+ const rawCode: JsonRpcResult = await this.callPayloadSingle(payload);
220
+ const result: IRawContract | { bytecode: string } = rawCode.result;
221
+
222
+ if ('contractAddress' in result) {
223
+ return new ContractData(result);
224
+ } else {
225
+ return Buffer.from(result.bytecode, 'base64');
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Get the storage at a specific address and pointer.
231
+ * @description This method is used to get the storage at a specific address and pointer.
232
+ * @param {BitcoinAddressLike} address The address to get the storage from
233
+ * @param {BigNumberish} rawPointer The pointer to get the storage from as base64 or bigint
234
+ * @param {boolean} proofs Whether to send proofs or not
235
+ * @param {BigNumberish} [height] The height to get the storage from
236
+ * @returns {Promise<StoredValue>} The storage value
237
+ * @example await getStorageAt('tb1pth90usc4f528aqphpjrfkkdm4vy8hxnt5gps6aau2nva6pxeshtqqzlt3a', 'EXLK/QhEQMI5d9DrthLvozT+UcDQ7WuSPaz7g8GV3AQ=');
238
+ * @throws {Error} If something went wrong while fetching the storage
239
+ */
240
+ public async getStorageAt(
241
+ address: BitcoinAddressLike,
242
+ rawPointer: bigint | string,
243
+ proofs: boolean = true,
244
+ height?: BigNumberish,
245
+ ): Promise<StoredValue> {
246
+ const addressStr: string = address.toString();
247
+ const pointer: string =
248
+ typeof rawPointer === 'string' ? rawPointer : this.bigintToBase64(rawPointer);
249
+
250
+ const params: [string, string, boolean, string?] = [addressStr, pointer, proofs];
251
+
252
+ if (height) {
253
+ params.push(height.toString());
254
+ }
255
+
256
+ const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_getStorageAt', params);
257
+
258
+ const rawStorage: JsonRpcResult = await this.callPayloadSingle(payload);
259
+ const result: IStorageValue = rawStorage.result;
260
+
261
+ return new StoredValue(result);
262
+ }
263
+
264
+ /**
265
+ * Call a contract function with a given calldata.
266
+ * @description This method is used to call a contract function with a given calldata.
267
+ * @param {BitcoinAddressLike} to The address of the contract
268
+ * @param {Buffer} data The calldata of the contract function
269
+ * @param {BitcoinAddressLike} [from] The address to call the contract from
270
+ * @param {BigNumberish} [height] The height to call the contract from
271
+ * @returns {Promise<CallResult>} The result of the contract function call
272
+ * @example await call('tb1pth90usc4f528aqphpjrfkkdm4vy8hxnt5gps6aau2nva6pxeshtqqzlt3a', Buffer.from('0x12345678'));
273
+ * @throws {Error} If something went wrong while calling the contract
274
+ */
275
+ public async call(
276
+ to: BitcoinAddressLike,
277
+ data: Buffer | string,
278
+ from?: BitcoinAddressLike,
279
+ height?: BigNumberish,
280
+ ): Promise<CallResult | ICallRequestError> {
281
+ const toStr: string = to.toString();
282
+ const fromStr: string | null = from ? from.toString() : null;
283
+
284
+ let dataStr: string = Buffer.isBuffer(data) ? this.bufferToHex(data) : data;
285
+ if (dataStr.startsWith('0x')) {
286
+ dataStr = dataStr.slice(2);
287
+ }
288
+
289
+ const params: [string, string, string?, string?] = [toStr, dataStr];
290
+ if (fromStr) {
291
+ params.push(fromStr);
292
+ }
293
+
294
+ if (height) {
295
+ params.push(height.toString());
296
+ }
297
+
298
+ const payload: JsonRpcPayload = this.buildJsonRpcPayload('btc_call', params);
299
+ const rawCall: JsonRpcResult = await this.callPayloadSingle(payload);
300
+
301
+ const result: ICallResult = rawCall.result;
302
+ if ('error' in result) {
303
+ return result;
304
+ }
305
+
306
+ return new CallResult(result);
307
+ }
308
+
309
+ protected abstract providerUrl(url: string): string;
310
+
311
+ private bufferToHex(buffer: Buffer): string {
312
+ return buffer.toString('hex');
313
+ }
314
+
315
+ private bigintToBase64(bigint: bigint): string {
316
+ return Buffer.from(BufferHelper.pointerToUint8Array(bigint)).toString('base64');
317
+ }
318
+
319
+ /**
320
+ * Send a single payload. This method is used to send a single payload.
321
+ * @param {JsonRpcPayload} payload The payload to send
322
+ * @returns {Promise<JsonRpcResult>} The result of the payload
323
+ * @throws {Error} If no data is returned
324
+ * @private
325
+ */
326
+ private async callPayloadSingle(payload: JsonRpcPayload): Promise<JsonRpcResult> {
327
+ const rawData: JsonRpcCallResult = await this.provider._send(payload);
328
+ if (rawData.length !== 1) {
329
+ throw new Error(`Unexpected response length for get block request: ${rawData.length}`);
330
+ }
331
+
332
+ const data = rawData.shift();
333
+ if (!data) {
334
+ throw new Error('Block not found');
335
+ }
336
+
337
+ if ('error' in data) {
338
+ throw new Error(`Something went wrong while fetching: ${data.error.message}`);
339
+ }
340
+
341
+ return data;
342
+ }
343
+
344
+ private buildJsonRpcPayload(method: string, params: unknown[]): JsonRpcPayload {
345
+ return {
346
+ method: method,
347
+ params: params,
348
+ id: this.nextId++,
349
+ jsonrpc: '2.0',
350
+ };
351
+ }
352
+ }