@atomiqlabs/chain-evm 2.1.12 → 2.2.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.
Files changed (151) hide show
  1. package/README.md +75 -0
  2. package/dist/chains/EVMOptions.d.ts +66 -0
  3. package/dist/chains/EVMOptions.js +2 -0
  4. package/dist/chains/alpen/AlpenInitializer.d.ts +3 -30
  5. package/dist/chains/alpen/AlpenInitializer.js +3 -3
  6. package/dist/chains/botanix/BotanixInitializer.d.ts +3 -30
  7. package/dist/chains/botanix/BotanixInitializer.js +3 -3
  8. package/dist/chains/citrea/CitreaBtcRelay.d.ts +5 -0
  9. package/dist/chains/citrea/CitreaBtcRelay.js +7 -2
  10. package/dist/chains/citrea/CitreaFees.d.ts +3 -5
  11. package/dist/chains/citrea/CitreaFees.js +3 -5
  12. package/dist/chains/citrea/CitreaInitializer.d.ts +3 -29
  13. package/dist/chains/citrea/CitreaInitializer.js +3 -3
  14. package/dist/chains/citrea/CitreaSpvVaultContract.d.ts +5 -0
  15. package/dist/chains/citrea/CitreaSpvVaultContract.js +7 -2
  16. package/dist/chains/citrea/CitreaSwapContract.d.ts +7 -2
  17. package/dist/chains/citrea/CitreaSwapContract.js +10 -5
  18. package/dist/chains/citrea/CitreaTokens.d.ts +5 -0
  19. package/dist/chains/citrea/CitreaTokens.js +5 -0
  20. package/dist/chains/goat/GoatInitializer.d.ts +3 -30
  21. package/dist/chains/goat/GoatInitializer.js +3 -3
  22. package/dist/evm/btcrelay/EVMBtcRelay.d.ts +41 -10
  23. package/dist/evm/btcrelay/EVMBtcRelay.js +50 -18
  24. package/dist/evm/btcrelay/headers/EVMBtcHeader.d.ts +53 -7
  25. package/dist/evm/btcrelay/headers/EVMBtcHeader.js +43 -5
  26. package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.d.ts +53 -8
  27. package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.js +41 -1
  28. package/dist/evm/chain/EVMChainInterface.d.ts +61 -2
  29. package/dist/evm/chain/EVMChainInterface.js +14 -7
  30. package/dist/evm/chain/EVMModule.d.ts +5 -0
  31. package/dist/evm/chain/EVMModule.js +6 -1
  32. package/dist/evm/chain/modules/EVMBlocks.d.ts +7 -0
  33. package/dist/evm/chain/modules/EVMBlocks.js +2 -0
  34. package/dist/evm/chain/modules/EVMEvents.js +19 -19
  35. package/dist/evm/chain/modules/EVMFees.d.ts +41 -5
  36. package/dist/evm/chain/modules/EVMFees.js +24 -5
  37. package/dist/evm/chain/modules/EVMTokens.d.ts +1 -1
  38. package/dist/evm/chain/modules/EVMTokens.js +1 -1
  39. package/dist/evm/chain/modules/EVMTransactions.d.ts +22 -5
  40. package/dist/evm/chain/modules/EVMTransactions.js +31 -22
  41. package/dist/evm/contract/EVMContractBase.d.ts +28 -10
  42. package/dist/evm/contract/EVMContractBase.js +9 -18
  43. package/dist/evm/contract/EVMContractModule.d.ts +5 -0
  44. package/dist/evm/contract/EVMContractModule.js +5 -0
  45. package/dist/evm/contract/modules/EVMContractEvents.d.ts +7 -1
  46. package/dist/evm/contract/modules/EVMContractEvents.js +23 -3
  47. package/dist/evm/events/EVMChainEvents.d.ts +8 -0
  48. package/dist/evm/events/EVMChainEvents.js +8 -0
  49. package/dist/evm/events/EVMChainEventsBrowser.d.ts +87 -19
  50. package/dist/evm/events/EVMChainEventsBrowser.js +53 -18
  51. package/dist/evm/providers/JsonRpcProviderWithRetries.d.ts +9 -0
  52. package/dist/evm/providers/JsonRpcProviderWithRetries.js +9 -0
  53. package/dist/evm/providers/ReconnectingWebSocketProvider.d.ts +5 -0
  54. package/dist/evm/providers/ReconnectingWebSocketProvider.js +5 -0
  55. package/dist/evm/providers/WebSocketProviderWithRetries.d.ts +9 -0
  56. package/dist/evm/providers/WebSocketProviderWithRetries.js +9 -0
  57. package/dist/evm/spv_swap/EVMSpvVaultContract.d.ts +46 -21
  58. package/dist/evm/spv_swap/EVMSpvVaultContract.js +61 -23
  59. package/dist/evm/spv_swap/EVMSpvVaultData.d.ts +57 -2
  60. package/dist/evm/spv_swap/EVMSpvVaultData.js +57 -2
  61. package/dist/evm/spv_swap/EVMSpvWithdrawalData.d.ts +12 -0
  62. package/dist/evm/spv_swap/EVMSpvWithdrawalData.js +12 -0
  63. package/dist/evm/swaps/EVMSwapContract.d.ts +58 -13
  64. package/dist/evm/swaps/EVMSwapContract.js +81 -54
  65. package/dist/evm/swaps/EVMSwapData.d.ts +27 -6
  66. package/dist/evm/swaps/EVMSwapData.js +26 -0
  67. package/dist/evm/swaps/EVMSwapModule.d.ts +5 -0
  68. package/dist/evm/swaps/EVMSwapModule.js +5 -0
  69. package/dist/evm/swaps/handlers/IHandler.d.ts +5 -0
  70. package/dist/evm/swaps/handlers/claim/ClaimHandlers.d.ts +15 -0
  71. package/dist/evm/swaps/handlers/claim/ClaimHandlers.js +5 -0
  72. package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +5 -0
  73. package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +10 -0
  74. package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +5 -0
  75. package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +15 -0
  76. package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +7 -2
  77. package/dist/evm/swaps/modules/EVMLpVault.d.ts +5 -0
  78. package/dist/evm/swaps/modules/EVMLpVault.js +9 -4
  79. package/dist/evm/swaps/modules/EVMSwapClaim.d.ts +7 -2
  80. package/dist/evm/swaps/modules/EVMSwapClaim.js +11 -6
  81. package/dist/evm/swaps/modules/EVMSwapInit.d.ts +10 -0
  82. package/dist/evm/swaps/modules/EVMSwapInit.js +11 -6
  83. package/dist/evm/swaps/modules/EVMSwapRefund.d.ts +5 -0
  84. package/dist/evm/swaps/modules/EVMSwapRefund.js +9 -4
  85. package/dist/evm/wallet/EVMBrowserSigner.d.ts +22 -2
  86. package/dist/evm/wallet/EVMBrowserSigner.js +40 -2
  87. package/dist/evm/wallet/EVMPersistentSigner.d.ts +13 -2
  88. package/dist/evm/wallet/EVMPersistentSigner.js +13 -1
  89. package/dist/evm/wallet/EVMSigner.d.ts +30 -1
  90. package/dist/evm/wallet/EVMSigner.js +34 -1
  91. package/dist/index.d.ts +71 -0
  92. package/dist/index.js +70 -0
  93. package/dist/node/index.d.ts +10 -0
  94. package/dist/node/index.js +15 -0
  95. package/dist/utils/Utils.d.ts +50 -0
  96. package/dist/utils/Utils.js +45 -0
  97. package/node/index.d.ts +1 -0
  98. package/node/index.js +3 -0
  99. package/package.json +5 -3
  100. package/src/chains/EVMOptions.ts +70 -0
  101. package/src/chains/alpen/AlpenInitializer.ts +5 -27
  102. package/src/chains/botanix/BotanixChainType.ts +5 -5
  103. package/src/chains/botanix/BotanixInitializer.ts +5 -27
  104. package/src/chains/citrea/CitreaBtcRelay.ts +8 -3
  105. package/src/chains/citrea/CitreaFees.ts +3 -6
  106. package/src/chains/citrea/CitreaInitializer.ts +5 -27
  107. package/src/chains/citrea/CitreaSpvVaultContract.ts +7 -2
  108. package/src/chains/citrea/CitreaSwapContract.ts +11 -6
  109. package/src/chains/citrea/CitreaTokens.ts +6 -1
  110. package/src/chains/goat/GoatChainType.ts +5 -5
  111. package/src/chains/goat/GoatInitializer.ts +3 -25
  112. package/src/evm/btcrelay/EVMBtcRelay.ts +54 -22
  113. package/src/evm/btcrelay/headers/EVMBtcHeader.ts +60 -13
  114. package/src/evm/btcrelay/headers/EVMBtcStoredHeader.ts +55 -10
  115. package/src/evm/chain/EVMChainInterface.ts +74 -14
  116. package/src/evm/chain/EVMModule.ts +6 -1
  117. package/src/evm/chain/modules/EVMBlocks.ts +7 -0
  118. package/src/evm/chain/modules/EVMEvents.ts +19 -19
  119. package/src/evm/chain/modules/EVMFees.ts +41 -5
  120. package/src/evm/chain/modules/EVMTokens.ts +1 -1
  121. package/src/evm/chain/modules/EVMTransactions.ts +40 -17
  122. package/src/evm/contract/EVMContractBase.ts +29 -24
  123. package/src/evm/contract/EVMContractModule.ts +5 -0
  124. package/src/evm/contract/modules/EVMContractEvents.ts +27 -8
  125. package/src/evm/events/EVMChainEvents.ts +8 -0
  126. package/src/evm/events/EVMChainEventsBrowser.ts +103 -29
  127. package/src/evm/providers/JsonRpcProviderWithRetries.ts +10 -1
  128. package/src/evm/providers/ReconnectingWebSocketProvider.ts +6 -1
  129. package/src/evm/providers/WebSocketProviderWithRetries.ts +10 -1
  130. package/src/evm/spv_swap/EVMSpvVaultContract.ts +72 -32
  131. package/src/evm/spv_swap/EVMSpvVaultData.ts +57 -2
  132. package/src/evm/spv_swap/EVMSpvWithdrawalData.ts +12 -0
  133. package/src/evm/swaps/EVMSwapContract.ts +108 -63
  134. package/src/evm/swaps/EVMSwapData.ts +27 -1
  135. package/src/evm/swaps/EVMSwapModule.ts +5 -0
  136. package/src/evm/swaps/handlers/IHandler.ts +5 -0
  137. package/src/evm/swaps/handlers/claim/ClaimHandlers.ts +15 -0
  138. package/src/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +5 -0
  139. package/src/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +10 -0
  140. package/src/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +5 -0
  141. package/src/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +17 -2
  142. package/src/evm/swaps/modules/EVMLpVault.ts +10 -5
  143. package/src/evm/swaps/modules/EVMSwapClaim.ts +12 -7
  144. package/src/evm/swaps/modules/EVMSwapInit.ts +17 -7
  145. package/src/evm/swaps/modules/EVMSwapRefund.ts +9 -4
  146. package/src/evm/wallet/EVMBrowserSigner.ts +44 -5
  147. package/src/evm/wallet/EVMPersistentSigner.ts +14 -2
  148. package/src/evm/wallet/EVMSigner.ts +37 -1
  149. package/src/index.ts +72 -0
  150. package/src/node/index.ts +10 -0
  151. package/src/utils/Utils.ts +50 -1
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GoatInitializer = exports.initializeGoat = exports.GoatAssets = void 0;
3
+ exports.GoatInitializer = exports.initializeGoat = void 0;
4
4
  const base_1 = require("@atomiqlabs/base");
5
5
  const ethers_1 = require("ethers");
6
6
  const EVMChainInterface_1 = require("../../evm/chain/EVMChainInterface");
@@ -88,7 +88,7 @@ const chainTypeMapping = {
88
88
  * Default GOAT Network token assets configuration
89
89
  * @category Networks/GOAT
90
90
  */
91
- exports.GoatAssets = {
91
+ const GoatAssets = {
92
92
  BTC: {
93
93
  address: "0x0000000000000000000000000000000000000000",
94
94
  decimals: 18,
@@ -166,6 +166,6 @@ exports.GoatInitializer = {
166
166
  chainId: "GOAT",
167
167
  chainType: null,
168
168
  initializer: initializeGoat,
169
- tokens: exports.GoatAssets,
169
+ tokens: GoatAssets,
170
170
  options: null
171
171
  };
@@ -1,7 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import { BitcoinNetwork, BitcoinRpc, BtcBlock, BtcRelay, RelaySynchronizer } from "@atomiqlabs/base";
4
- import { EVMBtcHeader } from "./headers/EVMBtcHeader";
5
4
  import { EVMContractBase } from "../contract/EVMContractBase";
6
5
  import { BtcRelay as BtcRelayTypechain } from "./BtcRelayTypechain";
7
6
  import { EVMBtcStoredHeader } from "./headers/EVMBtcStoredHeader";
@@ -9,27 +8,59 @@ import { EVMSigner } from "../wallet/EVMSigner";
9
8
  import { EVMTx } from "../chain/modules/EVMTransactions";
10
9
  import { EVMChainInterface } from "../chain/EVMChainInterface";
11
10
  /**
11
+ * EVM BTC Relay bitcoin light client contract representation.
12
+ *
12
13
  * @category BTC Relay
13
14
  */
14
15
  export declare class EVMBtcRelay<B extends BtcBlock> extends EVMContractBase<BtcRelayTypechain> implements BtcRelay<EVMBtcStoredHeader, EVMTx, B, EVMSigner> {
15
- static GasCosts: {
16
+ /**
17
+ * @internal
18
+ */
19
+ static _GasCosts: {
16
20
  GAS_PER_BLOCKHEADER: number;
17
21
  GAS_BASE_MAIN: number;
18
22
  GAS_PER_BLOCKHEADER_FORK: number;
19
23
  GAS_PER_BLOCKHEADER_FORKED: number;
20
24
  GAS_BASE_FORK: number;
21
25
  };
22
- SaveMainHeaders(signer: string, mainHeaders: EVMBtcHeader[], storedHeader: EVMBtcStoredHeader, feeRate: string): Promise<EVMTx>;
23
- SaveShortForkHeaders(signer: string, forkHeaders: EVMBtcHeader[], storedHeader: EVMBtcStoredHeader, feeRate: string): Promise<EVMTx>;
24
- SaveLongForkHeaders(signer: string, forkId: number, forkHeaders: EVMBtcHeader[], storedHeader: EVMBtcStoredHeader, feeRate: string, totalForkHeaders?: number): Promise<EVMTx>;
25
- bitcoinRpc: BitcoinRpc<B>;
26
+ /**
27
+ * Returns a transaction that submits new main-chain bitcoin blockheaders to the light client.
28
+ *
29
+ * @param signer EVM signer address
30
+ * @param mainHeaders New bitcoin blockheaders to submit
31
+ * @param storedHeader Current latest committed and stored bitcoin blockheader in the light client
32
+ * @param feeRate Fee rate to apply to the transaction
33
+ */
34
+ private SaveMainHeaders;
35
+ /**
36
+ * Returns a transaction that submits a short competing branch.
37
+ * If the submitted chain has higher total chainwork than the current canonical chain, it becomes canonical.
38
+ *
39
+ * @param signer EVM signer address
40
+ * @param forkHeaders Fork bitcoin blockheaders to submit
41
+ * @param storedHeader Committed and stored bitcoin blockheader from which to fork the light client
42
+ * @param feeRate Fee rate to apply to the transaction
43
+ */
44
+ private SaveShortForkHeaders;
45
+ /**
46
+ * Returns a transaction that submits blockheaders to an existing long fork.
47
+ *
48
+ * @param signer EVM signer address
49
+ * @param forkId Fork ID to submit the fork blockheaders to
50
+ * @param forkHeaders Fork bitcoin blockheaders to submit
51
+ * @param storedHeader Either a committed and stored blockheader from which to fork, or the current fork tip
52
+ * @param feeRate Fee rate to apply to the transaction
53
+ * @param totalForkHeaders Total blockheaders in the fork, used for gas estimation when reorg happens
54
+ */
55
+ private SaveLongForkHeaders;
56
+ readonly _bitcoinRpc: BitcoinRpc<B>;
26
57
  readonly maxHeadersPerTx: number;
27
58
  readonly maxForkHeadersPerTx: number;
28
59
  readonly maxShortForkHeadersPerTx: number;
29
60
  constructor(chainInterface: EVMChainInterface<any>, bitcoinRpc: BitcoinRpc<B>, bitcoinNetwork: BitcoinNetwork, contractAddress: string, contractDeploymentHeight?: number);
30
61
  /**
31
- * Computes subsequent commited headers as they will appear on the blockchain when transactions
32
- * are submitted & confirmed
62
+ * Computes subsequent committed headers as they will appear on-chain once transactions
63
+ * are submitted and confirmed.
33
64
  *
34
65
  * @param initialStoredHeader
35
66
  * @param syncedHeaders
@@ -149,8 +180,8 @@ export declare class EVMBtcRelay<B extends BtcBlock> extends EVMContractBase<Btc
149
180
  * @param signer
150
181
  * @param btcRelay
151
182
  * @param btcTxs
152
- * @param txs solana transaction array, in case we need to synchronize the btc relay ourselves the synchronization
153
- * txns are added here
183
+ * @param txs EVM transaction array. If BTC relay synchronization is needed, synchronization
184
+ * transactions are appended here.
154
185
  * @param synchronizer optional synchronizer to use to synchronize the btc relay in case it is not yet synchronized
155
186
  * to the required blockheight
156
187
  * @param feeRate Fee rate to use for synchronization transactions
@@ -23,34 +23,63 @@ function serializeBlockHeader(e) {
23
23
  }
24
24
  const logger = (0, Utils_1.getLogger)("EVMBtcRelay: ");
25
25
  /**
26
+ * EVM BTC Relay bitcoin light client contract representation.
27
+ *
26
28
  * @category BTC Relay
27
29
  */
28
30
  class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
31
+ /**
32
+ * Returns a transaction that submits new main-chain bitcoin blockheaders to the light client.
33
+ *
34
+ * @param signer EVM signer address
35
+ * @param mainHeaders New bitcoin blockheaders to submit
36
+ * @param storedHeader Current latest committed and stored bitcoin blockheader in the light client
37
+ * @param feeRate Fee rate to apply to the transaction
38
+ */
29
39
  async SaveMainHeaders(signer, mainHeaders, storedHeader, feeRate) {
30
40
  const tx = await this.contract.submitMainBlockheaders.populateTransaction(Buffer.concat([
31
41
  storedHeader.serialize(),
32
42
  Buffer.concat(mainHeaders.map(header => header.serializeCompact()))
33
43
  ]));
34
44
  tx.from = signer;
35
- EVMFees_1.EVMFees.applyFeeRate(tx, EVMBtcRelay.GasCosts.GAS_BASE_MAIN + (EVMBtcRelay.GasCosts.GAS_PER_BLOCKHEADER * mainHeaders.length), feeRate);
45
+ EVMFees_1.EVMFees.applyFeeRate(tx, EVMBtcRelay._GasCosts.GAS_BASE_MAIN + (EVMBtcRelay._GasCosts.GAS_PER_BLOCKHEADER * mainHeaders.length), feeRate);
36
46
  return tx;
37
47
  }
48
+ /**
49
+ * Returns a transaction that submits a short competing branch.
50
+ * If the submitted chain has higher total chainwork than the current canonical chain, it becomes canonical.
51
+ *
52
+ * @param signer EVM signer address
53
+ * @param forkHeaders Fork bitcoin blockheaders to submit
54
+ * @param storedHeader Committed and stored bitcoin blockheader from which to fork the light client
55
+ * @param feeRate Fee rate to apply to the transaction
56
+ */
38
57
  async SaveShortForkHeaders(signer, forkHeaders, storedHeader, feeRate) {
39
58
  const tx = await this.contract.submitShortForkBlockheaders.populateTransaction(Buffer.concat([
40
59
  storedHeader.serialize(),
41
60
  Buffer.concat(forkHeaders.map(header => header.serializeCompact()))
42
61
  ]));
43
62
  tx.from = signer;
44
- EVMFees_1.EVMFees.applyFeeRate(tx, EVMBtcRelay.GasCosts.GAS_BASE_MAIN + (EVMBtcRelay.GasCosts.GAS_PER_BLOCKHEADER * forkHeaders.length), feeRate);
63
+ EVMFees_1.EVMFees.applyFeeRate(tx, EVMBtcRelay._GasCosts.GAS_BASE_MAIN + (EVMBtcRelay._GasCosts.GAS_PER_BLOCKHEADER * forkHeaders.length), feeRate);
45
64
  return tx;
46
65
  }
66
+ /**
67
+ * Returns a transaction that submits blockheaders to an existing long fork.
68
+ *
69
+ * @param signer EVM signer address
70
+ * @param forkId Fork ID to submit the fork blockheaders to
71
+ * @param forkHeaders Fork bitcoin blockheaders to submit
72
+ * @param storedHeader Either a committed and stored blockheader from which to fork, or the current fork tip
73
+ * @param feeRate Fee rate to apply to the transaction
74
+ * @param totalForkHeaders Total blockheaders in the fork, used for gas estimation when reorg happens
75
+ */
47
76
  async SaveLongForkHeaders(signer, forkId, forkHeaders, storedHeader, feeRate, totalForkHeaders = 100) {
48
77
  const tx = await this.contract.submitForkBlockheaders.populateTransaction(forkId, Buffer.concat([
49
78
  storedHeader.serialize(),
50
79
  Buffer.concat(forkHeaders.map(header => header.serializeCompact()))
51
80
  ]));
52
81
  tx.from = signer;
53
- EVMFees_1.EVMFees.applyFeeRate(tx, EVMBtcRelay.GasCosts.GAS_BASE_FORK + (EVMBtcRelay.GasCosts.GAS_PER_BLOCKHEADER_FORK * forkHeaders.length) + (EVMBtcRelay.GasCosts.GAS_PER_BLOCKHEADER_FORKED * totalForkHeaders), feeRate);
82
+ EVMFees_1.EVMFees.applyFeeRate(tx, EVMBtcRelay._GasCosts.GAS_BASE_FORK + (EVMBtcRelay._GasCosts.GAS_PER_BLOCKHEADER_FORK * forkHeaders.length) + (EVMBtcRelay._GasCosts.GAS_PER_BLOCKHEADER_FORKED * totalForkHeaders), feeRate);
54
83
  return tx;
55
84
  }
56
85
  constructor(chainInterface, bitcoinRpc, bitcoinNetwork, contractAddress, contractDeploymentHeight) {
@@ -60,11 +89,11 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
60
89
  this.maxShortForkHeadersPerTx = 100;
61
90
  this.commitHashCache = new promise_cache_ts_1.PromiseLruCache(1000);
62
91
  this.blockHashCache = new promise_cache_ts_1.PromiseLruCache(1000);
63
- this.bitcoinRpc = bitcoinRpc;
92
+ this._bitcoinRpc = bitcoinRpc;
64
93
  }
65
94
  /**
66
- * Computes subsequent commited headers as they will appear on the blockchain when transactions
67
- * are submitted & confirmed
95
+ * Computes subsequent committed headers as they will appear on-chain once transactions
96
+ * are submitted and confirmed.
68
97
  *
69
98
  * @param initialStoredHeader
70
99
  * @param syncedHeaders
@@ -153,7 +182,7 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
153
182
  }
154
183
  getBlock(commitHash, blockHash) {
155
184
  const blockHashString = blockHash == null ? null : "0x" + Buffer.from([...blockHash]).reverse().toString("hex");
156
- const generator = () => this.Events.findInContractEvents(["StoreHeader", "StoreForkHeader"], [
185
+ const generator = () => this._Events.findInContractEvents(["StoreHeader", "StoreForkHeader"], [
157
186
  commitHash ?? null,
158
187
  blockHashString
159
188
  ], async (event) => {
@@ -206,7 +235,7 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
206
235
  return null;
207
236
  const [storedBlockHeader, commitHash] = result;
208
237
  //Check if block is part of the main chain
209
- const chainCommitment = await this.contract.getCommitHash(storedBlockHeader.blockHeight);
238
+ const chainCommitment = await this.contract.getCommitHash(storedBlockHeader.getBlockheight());
210
239
  if (chainCommitment !== commitHash)
211
240
  return null;
212
241
  logger.debug("retrieveLogAndBlockheight(): block found," +
@@ -222,24 +251,24 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
222
251
  return null;
223
252
  const [storedBlockHeader, commitHash] = result;
224
253
  //Check if block is part of the main chain
225
- const chainCommitment = await this.contract.getCommitHash(storedBlockHeader.blockHeight);
254
+ const chainCommitment = await this.contract.getCommitHash(storedBlockHeader.getBlockheight());
226
255
  if (chainCommitment !== commitHash)
227
256
  return null;
228
257
  logger.debug("retrieveLogByCommitHash(): block found," +
229
- " commit hash: " + commitmentHashStr + " blockhash: " + blockData.blockhash + " height: " + storedBlockHeader.blockHeight);
258
+ " commit hash: " + commitmentHashStr + " blockhash: " + blockData.blockhash + " height: " + storedBlockHeader.getBlockheight());
230
259
  return storedBlockHeader;
231
260
  }
232
261
  /**
233
262
  * @inheritDoc
234
263
  */
235
264
  async retrieveLatestKnownBlockLog() {
236
- const data = await this.Events.findInContractEvents(["StoreHeader", "StoreForkHeader"], null, async (event) => {
265
+ const data = await this._Events.findInContractEvents(["StoreHeader", "StoreForkHeader"], null, async (event) => {
237
266
  const blockHashHex = Buffer.from(event.args.blockHash.substring(2), "hex").reverse().toString("hex");
238
267
  const commitHash = event.args.commitHash;
239
- const isInBtcMainChain = await this.bitcoinRpc.isInMainChain(blockHashHex).catch(() => false);
268
+ const isInBtcMainChain = await this._bitcoinRpc.isInMainChain(blockHashHex).catch(() => false);
240
269
  if (!isInBtcMainChain)
241
270
  return null;
242
- const blockHeader = await this.bitcoinRpc.getBlockHeader(blockHashHex);
271
+ const blockHeader = await this._bitcoinRpc.getBlockHeader(blockHashHex);
243
272
  if (blockHeader == null)
244
273
  return null;
245
274
  if (commitHash !== await this.contract.getCommitHash(blockHeader.getHeight()))
@@ -324,7 +353,7 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
324
353
  if (blockheightDelta <= 0)
325
354
  return 0n;
326
355
  const synchronizationFee = (BigInt(blockheightDelta) * await this.getFeePerBlock(feeRate))
327
- + EVMFees_1.EVMFees.getGasFee(EVMBtcRelay.GasCosts.GAS_BASE_MAIN * Math.ceil(blockheightDelta / this.maxHeadersPerTx), feeRate);
356
+ + EVMFees_1.EVMFees.getGasFee(EVMBtcRelay._GasCosts.GAS_BASE_MAIN * Math.ceil(blockheightDelta / this.maxHeadersPerTx), feeRate);
328
357
  logger.debug("estimateSynchronizeFee(): required blockheight: " + requiredBlockheight +
329
358
  " blockheight delta: " + blockheightDelta + " fee: " + synchronizationFee.toString(10));
330
359
  return synchronizationFee;
@@ -334,7 +363,7 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
334
363
  */
335
364
  async getFeePerBlock(feeRate) {
336
365
  feeRate ?? (feeRate = await this.Chain.Fees.getFeeRate());
337
- return EVMFees_1.EVMFees.getGasFee(EVMBtcRelay.GasCosts.GAS_PER_BLOCKHEADER, feeRate);
366
+ return EVMFees_1.EVMFees.getGasFee(EVMBtcRelay._GasCosts.GAS_PER_BLOCKHEADER, feeRate);
338
367
  }
339
368
  /**
340
369
  * @inheritDoc
@@ -363,8 +392,8 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
363
392
  * @param signer
364
393
  * @param btcRelay
365
394
  * @param btcTxs
366
- * @param txs solana transaction array, in case we need to synchronize the btc relay ourselves the synchronization
367
- * txns are added here
395
+ * @param txs EVM transaction array. If BTC relay synchronization is needed, synchronization
396
+ * transactions are appended here.
368
397
  * @param synchronizer optional synchronizer to use to synchronize the btc relay in case it is not yet synchronized
369
398
  * to the required blockheight
370
399
  * @param feeRate Fee rate to use for synchronization transactions
@@ -410,7 +439,10 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
410
439
  }
411
440
  }
412
441
  exports.EVMBtcRelay = EVMBtcRelay;
413
- EVMBtcRelay.GasCosts = {
442
+ /**
443
+ * @internal
444
+ */
445
+ EVMBtcRelay._GasCosts = {
414
446
  GAS_PER_BLOCKHEADER: 30000,
415
447
  GAS_BASE_MAIN: 15000 + 21000,
416
448
  GAS_PER_BLOCKHEADER_FORK: 65000,
@@ -2,6 +2,11 @@
2
2
  /// <reference types="node" />
3
3
  import { BtcHeader } from "@atomiqlabs/base";
4
4
  import { Buffer } from "buffer";
5
+ /**
6
+ * Constructor payload for EVM bitcoin blockheader representation.
7
+ *
8
+ * @category BTC Relay
9
+ */
5
10
  export type EVMBtcHeaderType = {
6
11
  version: number;
7
12
  previousBlockhash?: Buffer;
@@ -12,25 +17,66 @@ export type EVMBtcHeaderType = {
12
17
  hash?: Buffer;
13
18
  };
14
19
  /**
20
+ * Representation of a bitcoin blockheader submitted to EVM BTC relay contracts.
21
+ *
15
22
  * @category BTC Relay
16
23
  */
17
24
  export declare class EVMBtcHeader implements BtcHeader {
18
- version: number;
19
- previousBlockhash?: Buffer;
20
- merkleRoot: Buffer;
21
- timestamp: number;
22
- nbits: number;
23
- nonce: number;
24
- hash?: Buffer;
25
+ private readonly version;
26
+ private readonly merkleRoot;
27
+ private readonly timestamp;
28
+ private readonly nbits;
29
+ private readonly nonce;
30
+ private readonly hash?;
31
+ /**
32
+ * @internal
33
+ */
34
+ _previousBlockhash?: Buffer;
35
+ /**
36
+ * @internal
37
+ */
25
38
  constructor(data: EVMBtcHeaderType);
39
+ /**
40
+ * @inheritDoc
41
+ */
26
42
  getMerkleRoot(): Buffer;
43
+ /**
44
+ * @inheritDoc
45
+ */
27
46
  getNbits(): number;
47
+ /**
48
+ * @inheritDoc
49
+ */
28
50
  getNonce(): number;
51
+ /**
52
+ * @inheritDoc
53
+ */
29
54
  getReversedPrevBlockhash(): Buffer;
55
+ /**
56
+ * @inheritDoc
57
+ */
30
58
  getTimestamp(): number;
59
+ /**
60
+ * @inheritDoc
61
+ */
31
62
  getVersion(): number;
63
+ /**
64
+ * @inheritDoc
65
+ */
32
66
  getHash(): Buffer;
67
+ /**
68
+ * Serializes the bitcoin blockheader into compact 48-byte representation
69
+ * (without previous blockhash).
70
+ */
33
71
  serializeCompact(): Buffer;
72
+ /**
73
+ * Serializes the bitcoin blockheader into full 80-byte representation.
74
+ */
34
75
  serialize(): Buffer;
76
+ /**
77
+ * Deserializes a bitcoin blockheader from 80-byte full or 48-byte compact representation.
78
+ *
79
+ * @param rawData Serialized blockheader bytes
80
+ */
35
81
  static deserialize(rawData: Buffer): EVMBtcHeader;
36
82
  }
@@ -4,41 +4,71 @@ exports.EVMBtcHeader = void 0;
4
4
  const buffer_1 = require("buffer");
5
5
  const sha2_1 = require("@noble/hashes/sha2");
6
6
  /**
7
+ * Representation of a bitcoin blockheader submitted to EVM BTC relay contracts.
8
+ *
7
9
  * @category BTC Relay
8
10
  */
9
11
  class EVMBtcHeader {
12
+ /**
13
+ * @internal
14
+ */
10
15
  constructor(data) {
11
16
  this.version = data.version;
12
- this.previousBlockhash = data.previousBlockhash;
17
+ this._previousBlockhash = data.previousBlockhash;
13
18
  this.merkleRoot = data.merkleRoot;
14
19
  this.timestamp = data.timestamp;
15
20
  this.nbits = data.nbits;
16
21
  this.nonce = data.nonce;
17
22
  this.hash = data.hash;
18
23
  }
24
+ /**
25
+ * @inheritDoc
26
+ */
19
27
  getMerkleRoot() {
20
28
  return this.merkleRoot;
21
29
  }
30
+ /**
31
+ * @inheritDoc
32
+ */
22
33
  getNbits() {
23
34
  return this.nbits;
24
35
  }
36
+ /**
37
+ * @inheritDoc
38
+ */
25
39
  getNonce() {
26
40
  return this.nonce;
27
41
  }
42
+ /**
43
+ * @inheritDoc
44
+ */
28
45
  getReversedPrevBlockhash() {
29
- if (this.previousBlockhash == null)
46
+ if (this._previousBlockhash == null)
30
47
  throw new Error("Previous blockhash is not known from compact blockheader!");
31
- return this.previousBlockhash;
48
+ return this._previousBlockhash;
32
49
  }
50
+ /**
51
+ * @inheritDoc
52
+ */
33
53
  getTimestamp() {
34
54
  return this.timestamp;
35
55
  }
56
+ /**
57
+ * @inheritDoc
58
+ */
36
59
  getVersion() {
37
60
  return this.version;
38
61
  }
62
+ /**
63
+ * @inheritDoc
64
+ */
39
65
  getHash() {
40
66
  return buffer_1.Buffer.from((0, sha2_1.sha256)((0, sha2_1.sha256)(this.serialize())));
41
67
  }
68
+ /**
69
+ * Serializes the bitcoin blockheader into compact 48-byte representation
70
+ * (without previous blockhash).
71
+ */
42
72
  serializeCompact() {
43
73
  const buffer = buffer_1.Buffer.alloc(48);
44
74
  buffer.writeUInt32LE(this.version, 0);
@@ -48,18 +78,26 @@ class EVMBtcHeader {
48
78
  buffer.writeUInt32LE(this.nonce, 44);
49
79
  return buffer;
50
80
  }
81
+ /**
82
+ * Serializes the bitcoin blockheader into full 80-byte representation.
83
+ */
51
84
  serialize() {
52
- if (this.previousBlockhash == null)
85
+ if (this._previousBlockhash == null)
53
86
  throw new Error("Cannot serialize compact blockheader without previous blockhash!");
54
87
  const buffer = buffer_1.Buffer.alloc(80);
55
88
  buffer.writeUInt32LE(this.version, 0);
56
- this.previousBlockhash.copy(buffer, 4);
89
+ this._previousBlockhash.copy(buffer, 4);
57
90
  this.merkleRoot.copy(buffer, 36);
58
91
  buffer.writeUInt32LE(this.timestamp, 68);
59
92
  buffer.writeUInt32LE(this.nbits, 72);
60
93
  buffer.writeUInt32LE(this.nonce, 76);
61
94
  return buffer;
62
95
  }
96
+ /**
97
+ * Deserializes a bitcoin blockheader from 80-byte full or 48-byte compact representation.
98
+ *
99
+ * @param rawData Serialized blockheader bytes
100
+ */
63
101
  static deserialize(rawData) {
64
102
  if (rawData.length === 80) {
65
103
  //Regular blockheader
@@ -3,7 +3,12 @@
3
3
  import { BtcStoredHeader } from "@atomiqlabs/base";
4
4
  import { EVMBtcHeader, EVMBtcHeaderType } from "./EVMBtcHeader";
5
5
  import { Buffer } from "buffer";
6
- export type StarknetBtcStoredHeaderType = {
6
+ /**
7
+ * Constructor payload for a stored bitcoin header committed in EVM BTC relay contract state.
8
+ *
9
+ * @category BTC Relay
10
+ */
11
+ export type EVMBtcStoredHeaderType = {
7
12
  blockheader: EVMBtcHeader | EVMBtcHeaderType;
8
13
  blockHash: Buffer;
9
14
  chainWork: bigint;
@@ -12,21 +17,44 @@ export type StarknetBtcStoredHeaderType = {
12
17
  prevBlockTimestamps: number[];
13
18
  };
14
19
  /**
20
+ * Represents a bitcoin header already committed inside EVM BTC relay contract state.
21
+ *
15
22
  * @category BTC Relay
16
23
  */
17
24
  export declare class EVMBtcStoredHeader implements BtcStoredHeader<EVMBtcHeader> {
18
- blockheader: EVMBtcHeader;
19
- blockHash: Buffer;
20
- chainWork: bigint;
21
- blockHeight: number;
22
- lastDiffAdjustment: number;
23
- prevBlockTimestamps: number[];
24
- constructor(obj: StarknetBtcStoredHeaderType);
25
+ private readonly blockheader;
26
+ private readonly blockHash;
27
+ private readonly chainWork;
28
+ private readonly blockHeight;
29
+ private readonly lastDiffAdjustment;
30
+ private readonly prevBlockTimestamps;
31
+ /**
32
+ * @internal
33
+ */
34
+ constructor(obj: EVMBtcStoredHeaderType);
35
+ /**
36
+ * @inheritDoc
37
+ */
25
38
  getBlockheight(): number;
39
+ /**
40
+ * @inheritDoc
41
+ */
26
42
  getChainWork(): Buffer;
43
+ /**
44
+ * @inheritDoc
45
+ */
27
46
  getHeader(): EVMBtcHeader;
47
+ /**
48
+ * @inheritDoc
49
+ */
28
50
  getLastDiffAdjustment(): number;
51
+ /**
52
+ * @inheritDoc
53
+ */
29
54
  getPrevBlockTimestamps(): number[];
55
+ /**
56
+ * @inheritDoc
57
+ */
30
58
  getBlockHash(): Buffer;
31
59
  /**
32
60
  * Computes prevBlockTimestamps for a next block, shifting the old block timestamps to the left & appending
@@ -49,11 +77,28 @@ export declare class EVMBtcStoredHeader implements BtcStoredHeader<EVMBtcHeader>
49
77
  * @private
50
78
  */
51
79
  private computeNextLastDiffAdjustment;
80
+ /**
81
+ * @inheritDoc
82
+ */
52
83
  computeNext(header: EVMBtcHeader): EVMBtcStoredHeader;
84
+ /**
85
+ * Returns the commitment of this stored head (keccak256 hash), this is what's actually stored on-chain
86
+ */
53
87
  getCommitHash(): string;
88
+ /**
89
+ * Serializes the stored blockheader into the 160-byte binary layout used by the EVM contracts.
90
+ */
54
91
  serialize(): Buffer;
92
+ /**
93
+ * Serializes the stored blockheader into the contract tuple form (`bytes32[5]` payload).
94
+ */
55
95
  serializeToStruct(): {
56
96
  data: [string, string, string, string, string];
57
97
  };
98
+ /**
99
+ * Deserializes a stored blockheader from the 160-byte binary representation.
100
+ *
101
+ * @param data Serialized stored blockheader bytes
102
+ */
58
103
  static deserialize(data: Buffer): EVMBtcStoredHeader;
59
104
  }
@@ -6,9 +6,14 @@ const EVMBtcHeader_1 = require("./EVMBtcHeader");
6
6
  const buffer_1 = require("buffer");
7
7
  const ethers_1 = require("ethers");
8
8
  /**
9
+ * Represents a bitcoin header already committed inside EVM BTC relay contract state.
10
+ *
9
11
  * @category BTC Relay
10
12
  */
11
13
  class EVMBtcStoredHeader {
14
+ /**
15
+ * @internal
16
+ */
12
17
  constructor(obj) {
13
18
  this.blockheader = obj.blockheader instanceof EVMBtcHeader_1.EVMBtcHeader ? obj.blockheader : new EVMBtcHeader_1.EVMBtcHeader(obj.blockheader);
14
19
  this.blockHash = obj.blockHash;
@@ -17,21 +22,39 @@ class EVMBtcStoredHeader {
17
22
  this.lastDiffAdjustment = obj.lastDiffAdjustment;
18
23
  this.prevBlockTimestamps = obj.prevBlockTimestamps;
19
24
  }
25
+ /**
26
+ * @inheritDoc
27
+ */
20
28
  getBlockheight() {
21
29
  return this.blockHeight;
22
30
  }
31
+ /**
32
+ * @inheritDoc
33
+ */
23
34
  getChainWork() {
24
35
  return buffer_1.Buffer.from(this.chainWork.toString(16).padStart(64, "0"), "hex");
25
36
  }
37
+ /**
38
+ * @inheritDoc
39
+ */
26
40
  getHeader() {
27
41
  return this.blockheader;
28
42
  }
43
+ /**
44
+ * @inheritDoc
45
+ */
29
46
  getLastDiffAdjustment() {
30
47
  return this.lastDiffAdjustment;
31
48
  }
49
+ /**
50
+ * @inheritDoc
51
+ */
32
52
  getPrevBlockTimestamps() {
33
53
  return this.prevBlockTimestamps;
34
54
  }
55
+ /**
56
+ * @inheritDoc
57
+ */
35
58
  getBlockHash() {
36
59
  return buffer_1.Buffer.from([...this.blockHash]).reverse();
37
60
  }
@@ -72,8 +95,11 @@ class EVMBtcStoredHeader {
72
95
  }
73
96
  return lastDiffAdjustment;
74
97
  }
98
+ /**
99
+ * @inheritDoc
100
+ */
75
101
  computeNext(header) {
76
- header.previousBlockhash = this.blockHash;
102
+ header._previousBlockhash = this.blockHash;
77
103
  return new EVMBtcStoredHeader({
78
104
  chainWork: this.computeNextChainWork(header.getNbits()),
79
105
  prevBlockTimestamps: this.computeNextBlockTimestamps(),
@@ -83,9 +109,15 @@ class EVMBtcStoredHeader {
83
109
  blockheader: header
84
110
  });
85
111
  }
112
+ /**
113
+ * Returns the commitment of this stored head (keccak256 hash), this is what's actually stored on-chain
114
+ */
86
115
  getCommitHash() {
87
116
  return (0, ethers_1.keccak256)(this.serialize());
88
117
  }
118
+ /**
119
+ * Serializes the stored blockheader into the 160-byte binary layout used by the EVM contracts.
120
+ */
89
121
  serialize() {
90
122
  const buffer = buffer_1.Buffer.alloc(160);
91
123
  this.blockheader.serialize().copy(buffer, 0, 0, 80);
@@ -97,6 +129,9 @@ class EVMBtcStoredHeader {
97
129
  }
98
130
  return buffer;
99
131
  }
132
+ /**
133
+ * Serializes the stored blockheader into the contract tuple form (`bytes32[5]` payload).
134
+ */
100
135
  serializeToStruct() {
101
136
  const buffer = this.serialize();
102
137
  const result = [];
@@ -105,6 +140,11 @@ class EVMBtcStoredHeader {
105
140
  }
106
141
  return { data: result };
107
142
  }
143
+ /**
144
+ * Deserializes a stored blockheader from the 160-byte binary representation.
145
+ *
146
+ * @param data Serialized stored blockheader bytes
147
+ */
108
148
  static deserialize(data) {
109
149
  if (data.length !== 160)
110
150
  throw new Error(`Invalid size Expected 160, got: ${data.length}!`);