@atomiqlabs/chain-evm 1.0.0-dev.35 → 1.0.0-dev.36

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 (161) hide show
  1. package/LICENSE +201 -201
  2. package/dist/chains/citrea/CitreaBtcRelay.d.ts +21 -21
  3. package/dist/chains/citrea/CitreaBtcRelay.js +43 -43
  4. package/dist/chains/citrea/CitreaChainType.d.ts +13 -13
  5. package/dist/chains/citrea/CitreaChainType.js +2 -2
  6. package/dist/chains/citrea/CitreaFees.d.ts +29 -29
  7. package/dist/chains/citrea/CitreaFees.js +67 -67
  8. package/dist/chains/citrea/CitreaInitializer.d.ts +30 -30
  9. package/dist/chains/citrea/CitreaInitializer.js +127 -127
  10. package/dist/chains/citrea/CitreaSpvVaultContract.d.ts +15 -15
  11. package/dist/chains/citrea/CitreaSpvVaultContract.js +74 -74
  12. package/dist/chains/citrea/CitreaSwapContract.d.ts +22 -22
  13. package/dist/chains/citrea/CitreaSwapContract.js +96 -96
  14. package/dist/chains/citrea/CitreaTokens.d.ts +9 -9
  15. package/dist/chains/citrea/CitreaTokens.js +20 -20
  16. package/dist/evm/btcrelay/BtcRelayAbi.d.ts +198 -198
  17. package/dist/evm/btcrelay/BtcRelayAbi.js +261 -261
  18. package/dist/evm/btcrelay/BtcRelayTypechain.d.ts +172 -172
  19. package/dist/evm/btcrelay/BtcRelayTypechain.js +2 -2
  20. package/dist/evm/btcrelay/EVMBtcRelay.d.ts +195 -195
  21. package/dist/evm/btcrelay/EVMBtcRelay.js +423 -423
  22. package/dist/evm/btcrelay/headers/EVMBtcHeader.d.ts +33 -33
  23. package/dist/evm/btcrelay/headers/EVMBtcHeader.js +84 -84
  24. package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.d.ts +56 -56
  25. package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.js +123 -123
  26. package/dist/evm/chain/EVMChainInterface.d.ts +51 -51
  27. package/dist/evm/chain/EVMChainInterface.js +89 -89
  28. package/dist/evm/chain/EVMModule.d.ts +9 -9
  29. package/dist/evm/chain/EVMModule.js +13 -13
  30. package/dist/evm/chain/modules/ERC20Abi.d.ts +168 -168
  31. package/dist/evm/chain/modules/ERC20Abi.js +225 -225
  32. package/dist/evm/chain/modules/EVMAddresses.d.ts +10 -10
  33. package/dist/evm/chain/modules/EVMAddresses.js +30 -30
  34. package/dist/evm/chain/modules/EVMBlocks.d.ts +20 -20
  35. package/dist/evm/chain/modules/EVMBlocks.js +64 -64
  36. package/dist/evm/chain/modules/EVMEvents.d.ts +36 -36
  37. package/dist/evm/chain/modules/EVMEvents.js +122 -122
  38. package/dist/evm/chain/modules/EVMFees.d.ts +36 -36
  39. package/dist/evm/chain/modules/EVMFees.js +73 -73
  40. package/dist/evm/chain/modules/EVMSignatures.d.ts +29 -29
  41. package/dist/evm/chain/modules/EVMSignatures.js +68 -68
  42. package/dist/evm/chain/modules/EVMTokens.d.ts +70 -51
  43. package/dist/evm/chain/modules/EVMTokens.js +142 -113
  44. package/dist/evm/chain/modules/EVMTransactions.d.ts +89 -89
  45. package/dist/evm/chain/modules/EVMTransactions.js +216 -216
  46. package/dist/evm/contract/EVMContractBase.d.ts +22 -22
  47. package/dist/evm/contract/EVMContractBase.js +34 -34
  48. package/dist/evm/contract/EVMContractModule.d.ts +8 -8
  49. package/dist/evm/contract/EVMContractModule.js +11 -11
  50. package/dist/evm/contract/modules/EVMContractEvents.d.ts +42 -42
  51. package/dist/evm/contract/modules/EVMContractEvents.js +75 -75
  52. package/dist/evm/events/EVMChainEvents.d.ts +22 -22
  53. package/dist/evm/events/EVMChainEvents.js +67 -67
  54. package/dist/evm/events/EVMChainEventsBrowser.d.ts +86 -86
  55. package/dist/evm/events/EVMChainEventsBrowser.js +294 -294
  56. package/dist/evm/spv_swap/EVMSpvVaultContract.d.ts +78 -78
  57. package/dist/evm/spv_swap/EVMSpvVaultContract.js +478 -480
  58. package/dist/evm/spv_swap/EVMSpvVaultData.d.ts +39 -39
  59. package/dist/evm/spv_swap/EVMSpvVaultData.js +180 -180
  60. package/dist/evm/spv_swap/EVMSpvWithdrawalData.d.ts +19 -19
  61. package/dist/evm/spv_swap/EVMSpvWithdrawalData.js +55 -55
  62. package/dist/evm/spv_swap/SpvVaultContractAbi.d.ts +91 -91
  63. package/dist/evm/spv_swap/SpvVaultContractAbi.js +849 -849
  64. package/dist/evm/spv_swap/SpvVaultContractTypechain.d.ts +450 -450
  65. package/dist/evm/spv_swap/SpvVaultContractTypechain.js +2 -2
  66. package/dist/evm/swaps/EVMSwapContract.d.ts +193 -193
  67. package/dist/evm/swaps/EVMSwapContract.js +374 -374
  68. package/dist/evm/swaps/EVMSwapData.d.ts +66 -66
  69. package/dist/evm/swaps/EVMSwapData.js +260 -260
  70. package/dist/evm/swaps/EVMSwapModule.d.ts +9 -9
  71. package/dist/evm/swaps/EVMSwapModule.js +11 -11
  72. package/dist/evm/swaps/EscrowManagerAbi.d.ts +120 -120
  73. package/dist/evm/swaps/EscrowManagerAbi.js +985 -985
  74. package/dist/evm/swaps/EscrowManagerTypechain.d.ts +475 -475
  75. package/dist/evm/swaps/EscrowManagerTypechain.js +2 -2
  76. package/dist/evm/swaps/handlers/IHandler.d.ts +13 -13
  77. package/dist/evm/swaps/handlers/IHandler.js +2 -2
  78. package/dist/evm/swaps/handlers/claim/ClaimHandlers.d.ts +10 -10
  79. package/dist/evm/swaps/handlers/claim/ClaimHandlers.js +13 -13
  80. package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.d.ts +20 -20
  81. package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.js +39 -39
  82. package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -24
  83. package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +59 -59
  84. package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -25
  85. package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +51 -51
  86. package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +21 -21
  87. package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +28 -28
  88. package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +48 -48
  89. package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +63 -63
  90. package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -17
  91. package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.js +28 -28
  92. package/dist/evm/swaps/modules/EVMLpVault.d.ts +69 -69
  93. package/dist/evm/swaps/modules/EVMLpVault.js +134 -131
  94. package/dist/evm/swaps/modules/EVMSwapClaim.d.ts +54 -54
  95. package/dist/evm/swaps/modules/EVMSwapClaim.js +137 -137
  96. package/dist/evm/swaps/modules/EVMSwapInit.d.ts +88 -88
  97. package/dist/evm/swaps/modules/EVMSwapInit.js +274 -275
  98. package/dist/evm/swaps/modules/EVMSwapRefund.d.ts +62 -62
  99. package/dist/evm/swaps/modules/EVMSwapRefund.js +167 -167
  100. package/dist/evm/typechain/common.d.ts +50 -50
  101. package/dist/evm/typechain/common.js +2 -2
  102. package/dist/evm/wallet/EVMSigner.d.ts +9 -9
  103. package/dist/evm/wallet/EVMSigner.js +16 -16
  104. package/dist/index.d.ts +38 -38
  105. package/dist/index.js +54 -54
  106. package/dist/utils/Utils.d.ts +15 -15
  107. package/dist/utils/Utils.js +71 -71
  108. package/package.json +37 -37
  109. package/src/chains/citrea/CitreaBtcRelay.ts +57 -57
  110. package/src/chains/citrea/CitreaChainType.ts +28 -28
  111. package/src/chains/citrea/CitreaFees.ts +77 -77
  112. package/src/chains/citrea/CitreaInitializer.ts +178 -178
  113. package/src/chains/citrea/CitreaSpvVaultContract.ts +75 -75
  114. package/src/chains/citrea/CitreaSwapContract.ts +102 -102
  115. package/src/chains/citrea/CitreaTokens.ts +21 -21
  116. package/src/evm/btcrelay/BtcRelayAbi.ts +258 -258
  117. package/src/evm/btcrelay/BtcRelayTypechain.ts +371 -371
  118. package/src/evm/btcrelay/EVMBtcRelay.ts +522 -522
  119. package/src/evm/btcrelay/headers/EVMBtcHeader.ts +109 -109
  120. package/src/evm/btcrelay/headers/EVMBtcStoredHeader.ts +152 -152
  121. package/src/evm/chain/EVMChainInterface.ts +155 -155
  122. package/src/evm/chain/EVMModule.ts +21 -21
  123. package/src/evm/chain/modules/ERC20Abi.ts +222 -222
  124. package/src/evm/chain/modules/EVMAddresses.ts +28 -28
  125. package/src/evm/chain/modules/EVMBlocks.ts +75 -75
  126. package/src/evm/chain/modules/EVMEvents.ts +139 -139
  127. package/src/evm/chain/modules/EVMFees.ts +104 -104
  128. package/src/evm/chain/modules/EVMSignatures.ts +76 -76
  129. package/src/evm/chain/modules/EVMTokens.ts +155 -126
  130. package/src/evm/chain/modules/EVMTransactions.ts +246 -246
  131. package/src/evm/contract/EVMContractBase.ts +63 -63
  132. package/src/evm/contract/EVMContractModule.ts +16 -16
  133. package/src/evm/contract/modules/EVMContractEvents.ts +102 -102
  134. package/src/evm/events/EVMChainEvents.ts +81 -81
  135. package/src/evm/events/EVMChainEventsBrowser.ts +390 -390
  136. package/src/evm/spv_swap/EVMSpvVaultContract.ts +608 -603
  137. package/src/evm/spv_swap/EVMSpvVaultData.ts +224 -224
  138. package/src/evm/spv_swap/EVMSpvWithdrawalData.ts +70 -70
  139. package/src/evm/spv_swap/SpvVaultContractAbi.ts +846 -846
  140. package/src/evm/spv_swap/SpvVaultContractTypechain.ts +685 -685
  141. package/src/evm/swaps/EVMSwapContract.ts +592 -592
  142. package/src/evm/swaps/EVMSwapData.ts +378 -378
  143. package/src/evm/swaps/EVMSwapModule.ts +16 -16
  144. package/src/evm/swaps/EscrowManagerAbi.ts +982 -982
  145. package/src/evm/swaps/EscrowManagerTypechain.ts +723 -723
  146. package/src/evm/swaps/handlers/IHandler.ts +17 -17
  147. package/src/evm/swaps/handlers/claim/ClaimHandlers.ts +20 -20
  148. package/src/evm/swaps/handlers/claim/HashlockClaimHandler.ts +46 -46
  149. package/src/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +82 -82
  150. package/src/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +76 -76
  151. package/src/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +46 -46
  152. package/src/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +115 -115
  153. package/src/evm/swaps/handlers/refund/TimelockRefundHandler.ts +37 -37
  154. package/src/evm/swaps/modules/EVMLpVault.ts +154 -152
  155. package/src/evm/swaps/modules/EVMSwapClaim.ts +172 -172
  156. package/src/evm/swaps/modules/EVMSwapInit.ts +328 -325
  157. package/src/evm/swaps/modules/EVMSwapRefund.ts +229 -229
  158. package/src/evm/typechain/common.ts +131 -131
  159. package/src/evm/wallet/EVMSigner.ts +23 -23
  160. package/src/index.ts +45 -45
  161. package/src/utils/Utils.ts +81 -81
@@ -1,592 +1,592 @@
1
- import {
2
- BigIntBufferUtils,
3
- ChainSwapType,
4
- IntermediaryReputationType,
5
- RelaySynchronizer,
6
- SignatureData, SwapCommitState, SwapCommitStateType,
7
- SwapContract,
8
- TransactionConfirmationOptions
9
- } from "@atomiqlabs/base";
10
- import {Buffer} from "buffer";
11
- import {TimelockRefundHandler} from "./handlers/refund/TimelockRefundHandler";
12
- import {claimHandlersList, IClaimHandler} from "./handlers/claim/ClaimHandlers"
13
- import {IHandler} from "./handlers/IHandler";
14
- import {sha256} from "@noble/hashes/sha2";
15
- import { EVMContractBase } from "../contract/EVMContractBase";
16
- import {EscrowManager} from "./EscrowManagerTypechain";
17
- import { EVMSwapData } from "./EVMSwapData";
18
- import {EVMTx} from "../chain/modules/EVMTransactions";
19
- import { EVMSigner } from "../wallet/EVMSigner";
20
- import {EVMChainInterface} from "../chain/EVMChainInterface";
21
- import { EVMBtcRelay } from "../btcrelay/EVMBtcRelay";
22
- import {EscrowManagerAbi} from "./EscrowManagerAbi";
23
- import {hexlify} from "ethers";
24
- import {EVMBtcStoredHeader} from "../btcrelay/headers/EVMBtcStoredHeader";
25
- import {EVMLpVault} from "./modules/EVMLpVault";
26
- import {EVMPreFetchVerification, EVMSwapInit} from "./modules/EVMSwapInit";
27
- import { EVMSwapRefund } from "./modules/EVMSwapRefund";
28
- import {EVMSwapClaim} from "./modules/EVMSwapClaim";
29
-
30
- const ESCROW_STATE_COMMITTED = 1;
31
- const ESCROW_STATE_CLAIMED = 2;
32
- const ESCROW_STATE_REFUNDED = 3;
33
-
34
- export class EVMSwapContract<ChainId extends string = string>
35
- extends EVMContractBase<EscrowManager>
36
- implements SwapContract<
37
- EVMSwapData,
38
- EVMTx,
39
- never,
40
- EVMPreFetchVerification,
41
- EVMSigner,
42
- ChainId
43
- > {
44
-
45
- readonly supportsInitWithoutClaimer = true;
46
-
47
- ////////////////////////
48
- //// Constants
49
- readonly chainId: ChainId;
50
-
51
- ////////////////////////
52
- //// Timeouts
53
- readonly claimWithSecretTimeout: number = 180;
54
- readonly claimWithTxDataTimeout: number = 180;
55
- readonly refundTimeout: number = 180;
56
- readonly claimGracePeriod: number = 10*60;
57
- readonly refundGracePeriod: number = 10*60;
58
- readonly authGracePeriod: number = 30;
59
-
60
- ////////////////////////
61
- //// Services
62
- readonly Init: EVMSwapInit;
63
- readonly Refund: EVMSwapRefund;
64
- readonly Claim: EVMSwapClaim;
65
- readonly LpVault: EVMLpVault;
66
-
67
- ////////////////////////
68
- //// Handlers
69
- readonly claimHandlersByAddress: {[address: string]: IClaimHandler<any, any>} = {};
70
- readonly claimHandlersBySwapType: {[type in ChainSwapType]?: IClaimHandler<any, any>} = {};
71
-
72
- readonly refundHandlersByAddress: {[address: string]: IHandler<any, any>} = {};
73
- readonly timelockRefundHandler: IHandler<any, any>;
74
-
75
- readonly btcRelay: EVMBtcRelay<any>;
76
-
77
- constructor(
78
- chainInterface: EVMChainInterface<ChainId>,
79
- btcRelay: EVMBtcRelay<any>,
80
- contractAddress: string,
81
- handlerAddresses: {
82
- refund: {
83
- timelock: string
84
- },
85
- claim: {
86
- [type in ChainSwapType]: string
87
- }
88
- }
89
- ) {
90
- super(chainInterface, contractAddress, EscrowManagerAbi);
91
- this.chainId = chainInterface.chainId;
92
- this.Init = new EVMSwapInit(chainInterface, this);
93
- this.Refund = new EVMSwapRefund(chainInterface, this);
94
- this.Claim = new EVMSwapClaim(chainInterface, this);
95
- this.LpVault = new EVMLpVault(chainInterface, this);
96
-
97
- this.btcRelay = btcRelay;
98
-
99
- claimHandlersList.forEach(handlerCtor => {
100
- const handler = new handlerCtor(handlerAddresses.claim[handlerCtor.type]);
101
- this.claimHandlersByAddress[handler.address.toLowerCase()] = handler;
102
- this.claimHandlersBySwapType[handlerCtor.type] = handler;
103
- });
104
-
105
- this.timelockRefundHandler = new TimelockRefundHandler(handlerAddresses.refund.timelock);
106
- this.refundHandlersByAddress[this.timelockRefundHandler.address.toLowerCase()] = this.timelockRefundHandler;
107
- }
108
-
109
- async start(): Promise<void> {
110
- }
111
-
112
- ////////////////////////////////////////////
113
- //// Signatures
114
- preFetchForInitSignatureVerification(): Promise<EVMPreFetchVerification> {
115
- return this.Init.preFetchForInitSignatureVerification();
116
- }
117
-
118
- getInitSignature(signer: EVMSigner, swapData: EVMSwapData, authorizationTimeout: number, preFetchedBlockData?: never, feeRate?: string): Promise<SignatureData> {
119
- return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout);
120
- }
121
-
122
- isValidInitAuthorization(sender: string, swapData: EVMSwapData, {timeout, prefix, signature}, feeRate?: string, preFetchedData?: EVMPreFetchVerification): Promise<Buffer> {
123
- return this.Init.isSignatureValid(sender, swapData, timeout, prefix, signature, preFetchedData);
124
- }
125
-
126
- getInitAuthorizationExpiry(swapData: EVMSwapData, {timeout, prefix, signature}, preFetchedData?: EVMPreFetchVerification): Promise<number> {
127
- return this.Init.getSignatureExpiry(timeout);
128
- }
129
-
130
- isInitAuthorizationExpired(swapData: EVMSwapData, {timeout, prefix, signature}): Promise<boolean> {
131
- return this.Init.isSignatureExpired(timeout);
132
- }
133
-
134
- getRefundSignature(signer: EVMSigner, swapData: EVMSwapData, authorizationTimeout: number): Promise<SignatureData> {
135
- return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
136
- }
137
-
138
- isValidRefundAuthorization(swapData: EVMSwapData, {timeout, prefix, signature}): Promise<Buffer> {
139
- return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
140
- }
141
-
142
- getDataSignature(signer: EVMSigner, data: Buffer): Promise<string> {
143
- return this.Chain.Signatures.getDataSignature(signer, data);
144
- }
145
-
146
- isValidDataSignature(data: Buffer, signature: string, publicKey: string): Promise<boolean> {
147
- return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
148
- }
149
-
150
- ////////////////////////////////////////////
151
- //// Swap data utils
152
- /**
153
- * Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
154
- *
155
- * @param signer
156
- * @param data
157
- */
158
- async isClaimable(signer: string, data: EVMSwapData): Promise<boolean> {
159
- if(!data.isClaimer(signer)) return false;
160
- if(await this.isExpired(signer, data)) return false;
161
- return await this.isCommited(data);
162
- }
163
-
164
- /**
165
- * Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
166
- *
167
- * @param swapData
168
- */
169
- async isCommited(swapData: EVMSwapData): Promise<boolean> {
170
- const data = await this.contract.getHashState("0x"+swapData.getEscrowHash());
171
- return Number(data.state)===ESCROW_STATE_COMMITTED;
172
- }
173
-
174
- /**
175
- * Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
176
- * the swap expires a bit sooner than it should've & for the offerer it expires a bit later
177
- *
178
- * @param signer
179
- * @param data
180
- */
181
- isExpired(signer: string, data: EVMSwapData): Promise<boolean> {
182
- let currentTimestamp: bigint = BigInt(Math.floor(Date.now()/1000));
183
- if(data.isClaimer(signer)) currentTimestamp = currentTimestamp + BigInt(this.claimGracePeriod);
184
- if(data.isOfferer(signer)) currentTimestamp = currentTimestamp - BigInt(this.refundGracePeriod);
185
- return Promise.resolve(data.getExpiry() < currentTimestamp);
186
- }
187
-
188
- /**
189
- * Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
190
- * is still commited
191
- *
192
- * @param signer
193
- * @param data
194
- */
195
- async isRequestRefundable(signer: string, data: EVMSwapData): Promise<boolean> {
196
- //Swap can only be refunded by the offerer
197
- if(!data.isOfferer(signer)) return false;
198
- if(!(await this.isExpired(signer, data))) return false;
199
- return await this.isCommited(data);
200
- }
201
-
202
- getHashForTxId(txId: string, confirmations: number) {
203
- return Buffer.from(this.claimHandlersBySwapType[ChainSwapType.CHAIN_TXID].getCommitment({
204
- txId,
205
- confirmations,
206
- btcRelay: this.btcRelay
207
- }).slice(2), "hex");
208
- }
209
-
210
- /**
211
- * Get the swap payment hash to be used for an on-chain swap, uses poseidon hash of the value
212
- *
213
- * @param outputScript output script required to claim the swap
214
- * @param amount sats sent required to claim the swap
215
- * @param confirmations
216
- * @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
217
- */
218
- getHashForOnchain(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
219
- let result: string;
220
- if(nonce==null || nonce === 0n) {
221
- result = this.claimHandlersBySwapType[ChainSwapType.CHAIN].getCommitment({
222
- output: outputScript,
223
- amount,
224
- confirmations,
225
- btcRelay: this.btcRelay
226
- });
227
- } else {
228
- result = this.claimHandlersBySwapType[ChainSwapType.CHAIN_NONCED].getCommitment({
229
- output: outputScript,
230
- amount,
231
- nonce,
232
- confirmations,
233
- btcRelay: this.btcRelay
234
- });
235
- }
236
- return Buffer.from(result.slice(2), "hex");
237
- }
238
-
239
- /**
240
- * Get the swap payment hash to be used for a lightning htlc swap, uses poseidon hash of the sha256 hash of the preimage
241
- *
242
- * @param paymentHash payment hash of the HTLC
243
- */
244
- getHashForHtlc(paymentHash: Buffer): Buffer {
245
- return Buffer.from(this.claimHandlersBySwapType[ChainSwapType.HTLC].getCommitment(paymentHash).slice(2), "hex");
246
- }
247
-
248
- getExtraData(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
249
- if(nonce==null) nonce = 0n;
250
- const txoHash = Buffer.from(sha256(Buffer.concat([
251
- BigIntBufferUtils.toBuffer(amount, "le", 8),
252
- outputScript
253
- ])));
254
- return Buffer.concat([
255
- txoHash,
256
- BigIntBufferUtils.toBuffer(nonce, "be", 8),
257
- BigIntBufferUtils.toBuffer(BigInt(confirmations), "be", 2)
258
- ]);
259
- }
260
-
261
-
262
- ////////////////////////////////////////////
263
- //// Swap data getters
264
- /**
265
- * Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
266
- * if swap is refundable)
267
- *
268
- * @param signer
269
- * @param data
270
- */
271
- async getCommitStatus(signer: string, data: EVMSwapData): Promise<SwapCommitState> {
272
- const escrowHash = data.getEscrowHash();
273
- const stateData = await this.contract.getHashState("0x"+escrowHash);
274
- const state = Number(stateData.state);
275
- const blockHeight = Number(stateData.finishBlockheight);
276
- switch(state) {
277
- case ESCROW_STATE_COMMITTED:
278
- if(data.isOfferer(signer) && await this.isExpired(signer,data)) return {type: SwapCommitStateType.REFUNDABLE};
279
- return {type: SwapCommitStateType.COMMITED};
280
- case ESCROW_STATE_CLAIMED:
281
- return {
282
- type: SwapCommitStateType.PAID,
283
- getTxBlock: async () => {
284
- return {
285
- blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
286
- blockHeight: blockHeight
287
- };
288
- },
289
- getClaimTxId: async () => {
290
- const events = await this.Events.getContractBlockEvents(
291
- ["Claim"],
292
- [null, null, "0x"+escrowHash],
293
- blockHeight, blockHeight
294
- );
295
- return events.length===0 ? null : events[0].transactionHash;
296
- }
297
- };
298
- default:
299
- return {
300
- type: await this.isExpired(signer, data) ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
301
- getTxBlock: async () => {
302
- return {
303
- blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
304
- blockHeight: blockHeight
305
- };
306
- },
307
- getRefundTxId: async () => {
308
- const events = await this.Events.getContractBlockEvents(
309
- ["Refund"],
310
- [null, null, "0x"+escrowHash],
311
- blockHeight, blockHeight
312
- );
313
- return events.length===0 ? null : events[0].transactionHash;
314
- }
315
- };
316
- }
317
- }
318
-
319
- /**
320
- * Returns the data committed for a specific payment hash, or null if no data is currently commited for
321
- * the specific swap
322
- *
323
- * @param paymentHashHex
324
- */
325
- async getCommitedData(paymentHashHex: string): Promise<EVMSwapData> {
326
- //TODO: Noop
327
- return null;
328
- }
329
-
330
- ////////////////////////////////////////////
331
- //// Swap data initializer
332
- createSwapData(
333
- type: ChainSwapType,
334
- offerer: string,
335
- claimer: string,
336
- token: string,
337
- amount: bigint,
338
- paymentHash: string,
339
- sequence: bigint,
340
- expiry: bigint,
341
- payIn: boolean,
342
- payOut: boolean,
343
- securityDeposit: bigint,
344
- claimerBounty: bigint,
345
- depositToken: string = this.Chain.Tokens.getNativeCurrencyAddress()
346
- ): Promise<EVMSwapData> {
347
- return Promise.resolve(new EVMSwapData(
348
- offerer,
349
- claimer,
350
- token,
351
- this.timelockRefundHandler.address,
352
- this.claimHandlersBySwapType?.[type]?.address,
353
- payOut,
354
- payIn,
355
- payIn, //For now track reputation for all payIn swaps
356
- sequence,
357
- "0x"+paymentHash,
358
- hexlify(BigIntBufferUtils.toBuffer(expiry, "be", 32)),
359
- amount,
360
- depositToken,
361
- securityDeposit,
362
- claimerBounty,
363
- type,
364
- null
365
- ));
366
- }
367
-
368
- ////////////////////////////////////////////
369
- //// Utils
370
- async getBalance(signer: string, tokenAddress: string, inContract?: boolean): Promise<bigint> {
371
- if(inContract) return await this.getIntermediaryBalance(signer, tokenAddress);
372
-
373
- return await this.Chain.getBalance(signer, tokenAddress);
374
- }
375
-
376
- getIntermediaryData(address: string, token: string): Promise<{
377
- balance: bigint,
378
- reputation: IntermediaryReputationType
379
- }> {
380
- return this.LpVault.getIntermediaryData(address, token);
381
- }
382
-
383
- getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType> {
384
- return this.LpVault.getIntermediaryReputation(address, token);
385
- }
386
-
387
- getIntermediaryBalance(address: string, token: string): Promise<bigint> {
388
- return this.LpVault.getIntermediaryBalance(address, token);
389
- }
390
-
391
- ////////////////////////////////////////////
392
- //// Transaction initializers
393
- async txsClaimWithSecret(
394
- signer: string | EVMSigner,
395
- swapData: EVMSwapData,
396
- secret: string,
397
- checkExpiry?: boolean,
398
- initAta?: boolean,
399
- feeRate?: string,
400
- skipAtaCheck?: boolean
401
- ): Promise<EVMTx[]> {
402
- return this.Claim.txsClaimWithSecret(typeof(signer)==="string" ? signer : signer.getAddress(), swapData, secret, checkExpiry, feeRate)
403
- }
404
-
405
- async txsClaimWithTxData(
406
- signer: string | EVMSigner,
407
- swapData: EVMSwapData,
408
- tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
409
- requiredConfirmations: number,
410
- vout: number,
411
- commitedHeader?: EVMBtcStoredHeader,
412
- synchronizer?: RelaySynchronizer<EVMBtcStoredHeader, EVMTx, any>,
413
- initAta?: boolean,
414
- feeRate?: string
415
- ): Promise<EVMTx[] | null> {
416
- return this.Claim.txsClaimWithTxData(
417
- typeof(signer)==="string" ? signer : signer.getAddress(),
418
- swapData,
419
- tx,
420
- requiredConfirmations,
421
- vout,
422
- commitedHeader,
423
- synchronizer,
424
- feeRate
425
- );
426
- }
427
-
428
- txsRefund(signer: string, swapData: EVMSwapData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<EVMTx[]> {
429
- return this.Refund.txsRefund(signer, swapData, check, feeRate);
430
- }
431
-
432
- txsRefundWithAuthorization(signer: string, swapData: EVMSwapData, {timeout, prefix, signature}, check?: boolean, initAta?: boolean, feeRate?: string): Promise<EVMTx[]> {
433
- return this.Refund.txsRefundWithAuthorization(signer, swapData, timeout, prefix,signature, check, feeRate);
434
- }
435
-
436
- txsInit(signer: string, swapData: EVMSwapData, {timeout, prefix, signature}, skipChecks?: boolean, feeRate?: string): Promise<EVMTx[]> {
437
- return this.Init.txsInit(signer, swapData, timeout, prefix, signature, skipChecks, feeRate);
438
- }
439
-
440
- txsWithdraw(signer: string, token: string, amount: bigint, feeRate?: string): Promise<EVMTx[]> {
441
- return this.LpVault.txsWithdraw(signer, token, amount, feeRate);
442
- }
443
-
444
- txsDeposit(signer: string, token: string, amount: bigint, feeRate?: string): Promise<EVMTx[]> {
445
- return this.LpVault.txsDeposit(signer, token, amount, feeRate);
446
- }
447
-
448
- ////////////////////////////////////////////
449
- //// Executors
450
- async claimWithSecret(
451
- signer: EVMSigner,
452
- swapData: EVMSwapData,
453
- secret: string,
454
- checkExpiry?: boolean,
455
- initAta?: boolean,
456
- txOptions?: TransactionConfirmationOptions
457
- ): Promise<string> {
458
- const result = await this.Claim.txsClaimWithSecret(signer.getAddress(), swapData, secret, checkExpiry, txOptions?.feeRate);
459
- const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
460
- return signature;
461
- }
462
-
463
- async claimWithTxData(
464
- signer: EVMSigner,
465
- swapData: EVMSwapData,
466
- tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
467
- requiredConfirmations: number,
468
- vout: number,
469
- commitedHeader?: EVMBtcStoredHeader,
470
- synchronizer?: RelaySynchronizer<EVMBtcStoredHeader, EVMTx, any>,
471
- initAta?: boolean,
472
- txOptions?: TransactionConfirmationOptions
473
- ): Promise<string> {
474
- const txs = await this.Claim.txsClaimWithTxData(
475
- signer.getAddress(), swapData, tx, requiredConfirmations, vout,
476
- commitedHeader, synchronizer, txOptions?.feeRate
477
- );
478
- if(txs===null) throw new Error("Btc relay not synchronized to required blockheight!");
479
-
480
- const txHashes = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
481
-
482
- return txHashes[txHashes.length - 1];
483
- }
484
-
485
- async refund(
486
- signer: EVMSigner,
487
- swapData: EVMSwapData,
488
- check?: boolean,
489
- initAta?: boolean,
490
- txOptions?: TransactionConfirmationOptions
491
- ): Promise<string> {
492
- let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
493
-
494
- const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
495
-
496
- return signature;
497
- }
498
-
499
- async refundWithAuthorization(
500
- signer: EVMSigner,
501
- swapData: EVMSwapData,
502
- signature: SignatureData,
503
- check?: boolean,
504
- initAta?: boolean,
505
- txOptions?: TransactionConfirmationOptions
506
- ): Promise<string> {
507
- let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
508
-
509
- const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
510
-
511
- return txSignature;
512
- }
513
-
514
- async init(
515
- signer: EVMSigner,
516
- swapData: EVMSwapData,
517
- signature: SignatureData,
518
- skipChecks?: boolean,
519
- txOptions?: TransactionConfirmationOptions
520
- ): Promise<string> {
521
- if(swapData.isPayIn()) {
522
- if(!swapData.isOfferer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress())) throw new Error("Invalid signer provided!");
523
- } else {
524
- if(!swapData.isClaimer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress())) throw new Error("Invalid signer provided!");
525
- }
526
-
527
- let result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
528
-
529
- const txHashes = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
530
-
531
- return txHashes[txHashes.length - 1];
532
- }
533
-
534
- async withdraw(
535
- signer: EVMSigner,
536
- token: string,
537
- amount: bigint,
538
- txOptions?: TransactionConfirmationOptions
539
- ): Promise<string> {
540
- const txs = await this.LpVault.txsWithdraw(signer.getAddress(), token, amount, txOptions?.feeRate);
541
- const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
542
- return txId;
543
- }
544
-
545
- async deposit(
546
- signer: EVMSigner,
547
- token: string,
548
- amount: bigint,
549
- txOptions?: TransactionConfirmationOptions
550
- ): Promise<string> {
551
- const txs = await this.LpVault.txsDeposit(signer.getAddress(), token, amount, txOptions?.feeRate);
552
- const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
553
- return txId;
554
- }
555
-
556
- ////////////////////////////////////////////
557
- //// Fees
558
- getInitPayInFeeRate(offerer?: string, claimer?: string, token?: string, paymentHash?: string): Promise<string> {
559
- return this.Chain.Fees.getFeeRate();
560
- }
561
-
562
- getInitFeeRate(offerer?: string, claimer?: string, token?: string, paymentHash?: string): Promise<string> {
563
- return this.Chain.Fees.getFeeRate();
564
- }
565
-
566
- getRefundFeeRate(swapData: EVMSwapData): Promise<string> {
567
- return this.Chain.Fees.getFeeRate();
568
- }
569
-
570
- getClaimFeeRate(signer: string, swapData: EVMSwapData): Promise<string> {
571
- return this.Chain.Fees.getFeeRate();
572
- }
573
-
574
- getClaimFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
575
- return this.Claim.getClaimFee(swapData, feeRate);
576
- }
577
-
578
- /**
579
- * Get the estimated fee of the commit transaction
580
- */
581
- getCommitFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
582
- return this.Init.getInitFee(swapData, feeRate);
583
- }
584
-
585
- /**
586
- * Get the estimated transaction fee of the refund transaction
587
- */
588
- getRefundFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
589
- return this.Refund.getRefundFee(swapData, feeRate);
590
- }
591
-
592
- }
1
+ import {
2
+ BigIntBufferUtils,
3
+ ChainSwapType,
4
+ IntermediaryReputationType,
5
+ RelaySynchronizer,
6
+ SignatureData, SwapCommitState, SwapCommitStateType,
7
+ SwapContract,
8
+ TransactionConfirmationOptions
9
+ } from "@atomiqlabs/base";
10
+ import {Buffer} from "buffer";
11
+ import {TimelockRefundHandler} from "./handlers/refund/TimelockRefundHandler";
12
+ import {claimHandlersList, IClaimHandler} from "./handlers/claim/ClaimHandlers"
13
+ import {IHandler} from "./handlers/IHandler";
14
+ import {sha256} from "@noble/hashes/sha2";
15
+ import { EVMContractBase } from "../contract/EVMContractBase";
16
+ import {EscrowManager} from "./EscrowManagerTypechain";
17
+ import { EVMSwapData } from "./EVMSwapData";
18
+ import {EVMTx} from "../chain/modules/EVMTransactions";
19
+ import { EVMSigner } from "../wallet/EVMSigner";
20
+ import {EVMChainInterface} from "../chain/EVMChainInterface";
21
+ import { EVMBtcRelay } from "../btcrelay/EVMBtcRelay";
22
+ import {EscrowManagerAbi} from "./EscrowManagerAbi";
23
+ import {hexlify} from "ethers";
24
+ import {EVMBtcStoredHeader} from "../btcrelay/headers/EVMBtcStoredHeader";
25
+ import {EVMLpVault} from "./modules/EVMLpVault";
26
+ import {EVMPreFetchVerification, EVMSwapInit} from "./modules/EVMSwapInit";
27
+ import { EVMSwapRefund } from "./modules/EVMSwapRefund";
28
+ import {EVMSwapClaim} from "./modules/EVMSwapClaim";
29
+
30
+ const ESCROW_STATE_COMMITTED = 1;
31
+ const ESCROW_STATE_CLAIMED = 2;
32
+ const ESCROW_STATE_REFUNDED = 3;
33
+
34
+ export class EVMSwapContract<ChainId extends string = string>
35
+ extends EVMContractBase<EscrowManager>
36
+ implements SwapContract<
37
+ EVMSwapData,
38
+ EVMTx,
39
+ never,
40
+ EVMPreFetchVerification,
41
+ EVMSigner,
42
+ ChainId
43
+ > {
44
+
45
+ readonly supportsInitWithoutClaimer = true;
46
+
47
+ ////////////////////////
48
+ //// Constants
49
+ readonly chainId: ChainId;
50
+
51
+ ////////////////////////
52
+ //// Timeouts
53
+ readonly claimWithSecretTimeout: number = 180;
54
+ readonly claimWithTxDataTimeout: number = 180;
55
+ readonly refundTimeout: number = 180;
56
+ readonly claimGracePeriod: number = 10*60;
57
+ readonly refundGracePeriod: number = 10*60;
58
+ readonly authGracePeriod: number = 30;
59
+
60
+ ////////////////////////
61
+ //// Services
62
+ readonly Init: EVMSwapInit;
63
+ readonly Refund: EVMSwapRefund;
64
+ readonly Claim: EVMSwapClaim;
65
+ readonly LpVault: EVMLpVault;
66
+
67
+ ////////////////////////
68
+ //// Handlers
69
+ readonly claimHandlersByAddress: {[address: string]: IClaimHandler<any, any>} = {};
70
+ readonly claimHandlersBySwapType: {[type in ChainSwapType]?: IClaimHandler<any, any>} = {};
71
+
72
+ readonly refundHandlersByAddress: {[address: string]: IHandler<any, any>} = {};
73
+ readonly timelockRefundHandler: IHandler<any, any>;
74
+
75
+ readonly btcRelay: EVMBtcRelay<any>;
76
+
77
+ constructor(
78
+ chainInterface: EVMChainInterface<ChainId>,
79
+ btcRelay: EVMBtcRelay<any>,
80
+ contractAddress: string,
81
+ handlerAddresses: {
82
+ refund: {
83
+ timelock: string
84
+ },
85
+ claim: {
86
+ [type in ChainSwapType]: string
87
+ }
88
+ }
89
+ ) {
90
+ super(chainInterface, contractAddress, EscrowManagerAbi);
91
+ this.chainId = chainInterface.chainId;
92
+ this.Init = new EVMSwapInit(chainInterface, this);
93
+ this.Refund = new EVMSwapRefund(chainInterface, this);
94
+ this.Claim = new EVMSwapClaim(chainInterface, this);
95
+ this.LpVault = new EVMLpVault(chainInterface, this);
96
+
97
+ this.btcRelay = btcRelay;
98
+
99
+ claimHandlersList.forEach(handlerCtor => {
100
+ const handler = new handlerCtor(handlerAddresses.claim[handlerCtor.type]);
101
+ this.claimHandlersByAddress[handler.address.toLowerCase()] = handler;
102
+ this.claimHandlersBySwapType[handlerCtor.type] = handler;
103
+ });
104
+
105
+ this.timelockRefundHandler = new TimelockRefundHandler(handlerAddresses.refund.timelock);
106
+ this.refundHandlersByAddress[this.timelockRefundHandler.address.toLowerCase()] = this.timelockRefundHandler;
107
+ }
108
+
109
+ async start(): Promise<void> {
110
+ }
111
+
112
+ ////////////////////////////////////////////
113
+ //// Signatures
114
+ preFetchForInitSignatureVerification(): Promise<EVMPreFetchVerification> {
115
+ return this.Init.preFetchForInitSignatureVerification();
116
+ }
117
+
118
+ getInitSignature(signer: EVMSigner, swapData: EVMSwapData, authorizationTimeout: number, preFetchedBlockData?: never, feeRate?: string): Promise<SignatureData> {
119
+ return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout);
120
+ }
121
+
122
+ isValidInitAuthorization(sender: string, swapData: EVMSwapData, {timeout, prefix, signature}, feeRate?: string, preFetchedData?: EVMPreFetchVerification): Promise<Buffer> {
123
+ return this.Init.isSignatureValid(sender, swapData, timeout, prefix, signature, preFetchedData);
124
+ }
125
+
126
+ getInitAuthorizationExpiry(swapData: EVMSwapData, {timeout, prefix, signature}, preFetchedData?: EVMPreFetchVerification): Promise<number> {
127
+ return this.Init.getSignatureExpiry(timeout);
128
+ }
129
+
130
+ isInitAuthorizationExpired(swapData: EVMSwapData, {timeout, prefix, signature}): Promise<boolean> {
131
+ return this.Init.isSignatureExpired(timeout);
132
+ }
133
+
134
+ getRefundSignature(signer: EVMSigner, swapData: EVMSwapData, authorizationTimeout: number): Promise<SignatureData> {
135
+ return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
136
+ }
137
+
138
+ isValidRefundAuthorization(swapData: EVMSwapData, {timeout, prefix, signature}): Promise<Buffer> {
139
+ return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
140
+ }
141
+
142
+ getDataSignature(signer: EVMSigner, data: Buffer): Promise<string> {
143
+ return this.Chain.Signatures.getDataSignature(signer, data);
144
+ }
145
+
146
+ isValidDataSignature(data: Buffer, signature: string, publicKey: string): Promise<boolean> {
147
+ return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
148
+ }
149
+
150
+ ////////////////////////////////////////////
151
+ //// Swap data utils
152
+ /**
153
+ * Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
154
+ *
155
+ * @param signer
156
+ * @param data
157
+ */
158
+ async isClaimable(signer: string, data: EVMSwapData): Promise<boolean> {
159
+ if(!data.isClaimer(signer)) return false;
160
+ if(await this.isExpired(signer, data)) return false;
161
+ return await this.isCommited(data);
162
+ }
163
+
164
+ /**
165
+ * Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
166
+ *
167
+ * @param swapData
168
+ */
169
+ async isCommited(swapData: EVMSwapData): Promise<boolean> {
170
+ const data = await this.contract.getHashState("0x"+swapData.getEscrowHash());
171
+ return Number(data.state)===ESCROW_STATE_COMMITTED;
172
+ }
173
+
174
+ /**
175
+ * Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
176
+ * the swap expires a bit sooner than it should've & for the offerer it expires a bit later
177
+ *
178
+ * @param signer
179
+ * @param data
180
+ */
181
+ isExpired(signer: string, data: EVMSwapData): Promise<boolean> {
182
+ let currentTimestamp: bigint = BigInt(Math.floor(Date.now()/1000));
183
+ if(data.isClaimer(signer)) currentTimestamp = currentTimestamp + BigInt(this.claimGracePeriod);
184
+ if(data.isOfferer(signer)) currentTimestamp = currentTimestamp - BigInt(this.refundGracePeriod);
185
+ return Promise.resolve(data.getExpiry() < currentTimestamp);
186
+ }
187
+
188
+ /**
189
+ * Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
190
+ * is still commited
191
+ *
192
+ * @param signer
193
+ * @param data
194
+ */
195
+ async isRequestRefundable(signer: string, data: EVMSwapData): Promise<boolean> {
196
+ //Swap can only be refunded by the offerer
197
+ if(!data.isOfferer(signer)) return false;
198
+ if(!(await this.isExpired(signer, data))) return false;
199
+ return await this.isCommited(data);
200
+ }
201
+
202
+ getHashForTxId(txId: string, confirmations: number) {
203
+ return Buffer.from(this.claimHandlersBySwapType[ChainSwapType.CHAIN_TXID].getCommitment({
204
+ txId,
205
+ confirmations,
206
+ btcRelay: this.btcRelay
207
+ }).slice(2), "hex");
208
+ }
209
+
210
+ /**
211
+ * Get the swap payment hash to be used for an on-chain swap, uses poseidon hash of the value
212
+ *
213
+ * @param outputScript output script required to claim the swap
214
+ * @param amount sats sent required to claim the swap
215
+ * @param confirmations
216
+ * @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
217
+ */
218
+ getHashForOnchain(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
219
+ let result: string;
220
+ if(nonce==null || nonce === 0n) {
221
+ result = this.claimHandlersBySwapType[ChainSwapType.CHAIN].getCommitment({
222
+ output: outputScript,
223
+ amount,
224
+ confirmations,
225
+ btcRelay: this.btcRelay
226
+ });
227
+ } else {
228
+ result = this.claimHandlersBySwapType[ChainSwapType.CHAIN_NONCED].getCommitment({
229
+ output: outputScript,
230
+ amount,
231
+ nonce,
232
+ confirmations,
233
+ btcRelay: this.btcRelay
234
+ });
235
+ }
236
+ return Buffer.from(result.slice(2), "hex");
237
+ }
238
+
239
+ /**
240
+ * Get the swap payment hash to be used for a lightning htlc swap, uses poseidon hash of the sha256 hash of the preimage
241
+ *
242
+ * @param paymentHash payment hash of the HTLC
243
+ */
244
+ getHashForHtlc(paymentHash: Buffer): Buffer {
245
+ return Buffer.from(this.claimHandlersBySwapType[ChainSwapType.HTLC].getCommitment(paymentHash).slice(2), "hex");
246
+ }
247
+
248
+ getExtraData(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
249
+ if(nonce==null) nonce = 0n;
250
+ const txoHash = Buffer.from(sha256(Buffer.concat([
251
+ BigIntBufferUtils.toBuffer(amount, "le", 8),
252
+ outputScript
253
+ ])));
254
+ return Buffer.concat([
255
+ txoHash,
256
+ BigIntBufferUtils.toBuffer(nonce, "be", 8),
257
+ BigIntBufferUtils.toBuffer(BigInt(confirmations), "be", 2)
258
+ ]);
259
+ }
260
+
261
+
262
+ ////////////////////////////////////////////
263
+ //// Swap data getters
264
+ /**
265
+ * Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
266
+ * if swap is refundable)
267
+ *
268
+ * @param signer
269
+ * @param data
270
+ */
271
+ async getCommitStatus(signer: string, data: EVMSwapData): Promise<SwapCommitState> {
272
+ const escrowHash = data.getEscrowHash();
273
+ const stateData = await this.contract.getHashState("0x"+escrowHash);
274
+ const state = Number(stateData.state);
275
+ const blockHeight = Number(stateData.finishBlockheight);
276
+ switch(state) {
277
+ case ESCROW_STATE_COMMITTED:
278
+ if(data.isOfferer(signer) && await this.isExpired(signer,data)) return {type: SwapCommitStateType.REFUNDABLE};
279
+ return {type: SwapCommitStateType.COMMITED};
280
+ case ESCROW_STATE_CLAIMED:
281
+ return {
282
+ type: SwapCommitStateType.PAID,
283
+ getTxBlock: async () => {
284
+ return {
285
+ blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
286
+ blockHeight: blockHeight
287
+ };
288
+ },
289
+ getClaimTxId: async () => {
290
+ const events = await this.Events.getContractBlockEvents(
291
+ ["Claim"],
292
+ [null, null, "0x"+escrowHash],
293
+ blockHeight, blockHeight
294
+ );
295
+ return events.length===0 ? null : events[0].transactionHash;
296
+ }
297
+ };
298
+ default:
299
+ return {
300
+ type: await this.isExpired(signer, data) ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
301
+ getTxBlock: async () => {
302
+ return {
303
+ blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
304
+ blockHeight: blockHeight
305
+ };
306
+ },
307
+ getRefundTxId: async () => {
308
+ const events = await this.Events.getContractBlockEvents(
309
+ ["Refund"],
310
+ [null, null, "0x"+escrowHash],
311
+ blockHeight, blockHeight
312
+ );
313
+ return events.length===0 ? null : events[0].transactionHash;
314
+ }
315
+ };
316
+ }
317
+ }
318
+
319
+ /**
320
+ * Returns the data committed for a specific payment hash, or null if no data is currently commited for
321
+ * the specific swap
322
+ *
323
+ * @param paymentHashHex
324
+ */
325
+ async getCommitedData(paymentHashHex: string): Promise<EVMSwapData> {
326
+ //TODO: Noop
327
+ return null;
328
+ }
329
+
330
+ ////////////////////////////////////////////
331
+ //// Swap data initializer
332
+ createSwapData(
333
+ type: ChainSwapType,
334
+ offerer: string,
335
+ claimer: string,
336
+ token: string,
337
+ amount: bigint,
338
+ paymentHash: string,
339
+ sequence: bigint,
340
+ expiry: bigint,
341
+ payIn: boolean,
342
+ payOut: boolean,
343
+ securityDeposit: bigint,
344
+ claimerBounty: bigint,
345
+ depositToken: string = this.Chain.Tokens.getNativeCurrencyAddress()
346
+ ): Promise<EVMSwapData> {
347
+ return Promise.resolve(new EVMSwapData(
348
+ offerer,
349
+ claimer,
350
+ token,
351
+ this.timelockRefundHandler.address,
352
+ this.claimHandlersBySwapType?.[type]?.address,
353
+ payOut,
354
+ payIn,
355
+ payIn, //For now track reputation for all payIn swaps
356
+ sequence,
357
+ "0x"+paymentHash,
358
+ hexlify(BigIntBufferUtils.toBuffer(expiry, "be", 32)),
359
+ amount,
360
+ depositToken,
361
+ securityDeposit,
362
+ claimerBounty,
363
+ type,
364
+ null
365
+ ));
366
+ }
367
+
368
+ ////////////////////////////////////////////
369
+ //// Utils
370
+ async getBalance(signer: string, tokenAddress: string, inContract?: boolean): Promise<bigint> {
371
+ if(inContract) return await this.getIntermediaryBalance(signer, tokenAddress);
372
+
373
+ return await this.Chain.getBalance(signer, tokenAddress);
374
+ }
375
+
376
+ getIntermediaryData(address: string, token: string): Promise<{
377
+ balance: bigint,
378
+ reputation: IntermediaryReputationType
379
+ }> {
380
+ return this.LpVault.getIntermediaryData(address, token);
381
+ }
382
+
383
+ getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType> {
384
+ return this.LpVault.getIntermediaryReputation(address, token);
385
+ }
386
+
387
+ getIntermediaryBalance(address: string, token: string): Promise<bigint> {
388
+ return this.LpVault.getIntermediaryBalance(address, token);
389
+ }
390
+
391
+ ////////////////////////////////////////////
392
+ //// Transaction initializers
393
+ async txsClaimWithSecret(
394
+ signer: string | EVMSigner,
395
+ swapData: EVMSwapData,
396
+ secret: string,
397
+ checkExpiry?: boolean,
398
+ initAta?: boolean,
399
+ feeRate?: string,
400
+ skipAtaCheck?: boolean
401
+ ): Promise<EVMTx[]> {
402
+ return this.Claim.txsClaimWithSecret(typeof(signer)==="string" ? signer : signer.getAddress(), swapData, secret, checkExpiry, feeRate)
403
+ }
404
+
405
+ async txsClaimWithTxData(
406
+ signer: string | EVMSigner,
407
+ swapData: EVMSwapData,
408
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
409
+ requiredConfirmations: number,
410
+ vout: number,
411
+ commitedHeader?: EVMBtcStoredHeader,
412
+ synchronizer?: RelaySynchronizer<EVMBtcStoredHeader, EVMTx, any>,
413
+ initAta?: boolean,
414
+ feeRate?: string
415
+ ): Promise<EVMTx[] | null> {
416
+ return this.Claim.txsClaimWithTxData(
417
+ typeof(signer)==="string" ? signer : signer.getAddress(),
418
+ swapData,
419
+ tx,
420
+ requiredConfirmations,
421
+ vout,
422
+ commitedHeader,
423
+ synchronizer,
424
+ feeRate
425
+ );
426
+ }
427
+
428
+ txsRefund(signer: string, swapData: EVMSwapData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<EVMTx[]> {
429
+ return this.Refund.txsRefund(signer, swapData, check, feeRate);
430
+ }
431
+
432
+ txsRefundWithAuthorization(signer: string, swapData: EVMSwapData, {timeout, prefix, signature}, check?: boolean, initAta?: boolean, feeRate?: string): Promise<EVMTx[]> {
433
+ return this.Refund.txsRefundWithAuthorization(signer, swapData, timeout, prefix,signature, check, feeRate);
434
+ }
435
+
436
+ txsInit(signer: string, swapData: EVMSwapData, {timeout, prefix, signature}, skipChecks?: boolean, feeRate?: string): Promise<EVMTx[]> {
437
+ return this.Init.txsInit(signer, swapData, timeout, prefix, signature, skipChecks, feeRate);
438
+ }
439
+
440
+ txsWithdraw(signer: string, token: string, amount: bigint, feeRate?: string): Promise<EVMTx[]> {
441
+ return this.LpVault.txsWithdraw(signer, token, amount, feeRate);
442
+ }
443
+
444
+ txsDeposit(signer: string, token: string, amount: bigint, feeRate?: string): Promise<EVMTx[]> {
445
+ return this.LpVault.txsDeposit(signer, token, amount, feeRate);
446
+ }
447
+
448
+ ////////////////////////////////////////////
449
+ //// Executors
450
+ async claimWithSecret(
451
+ signer: EVMSigner,
452
+ swapData: EVMSwapData,
453
+ secret: string,
454
+ checkExpiry?: boolean,
455
+ initAta?: boolean,
456
+ txOptions?: TransactionConfirmationOptions
457
+ ): Promise<string> {
458
+ const result = await this.Claim.txsClaimWithSecret(signer.getAddress(), swapData, secret, checkExpiry, txOptions?.feeRate);
459
+ const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
460
+ return signature;
461
+ }
462
+
463
+ async claimWithTxData(
464
+ signer: EVMSigner,
465
+ swapData: EVMSwapData,
466
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
467
+ requiredConfirmations: number,
468
+ vout: number,
469
+ commitedHeader?: EVMBtcStoredHeader,
470
+ synchronizer?: RelaySynchronizer<EVMBtcStoredHeader, EVMTx, any>,
471
+ initAta?: boolean,
472
+ txOptions?: TransactionConfirmationOptions
473
+ ): Promise<string> {
474
+ const txs = await this.Claim.txsClaimWithTxData(
475
+ signer.getAddress(), swapData, tx, requiredConfirmations, vout,
476
+ commitedHeader, synchronizer, txOptions?.feeRate
477
+ );
478
+ if(txs===null) throw new Error("Btc relay not synchronized to required blockheight!");
479
+
480
+ const txHashes = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
481
+
482
+ return txHashes[txHashes.length - 1];
483
+ }
484
+
485
+ async refund(
486
+ signer: EVMSigner,
487
+ swapData: EVMSwapData,
488
+ check?: boolean,
489
+ initAta?: boolean,
490
+ txOptions?: TransactionConfirmationOptions
491
+ ): Promise<string> {
492
+ let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
493
+
494
+ const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
495
+
496
+ return signature;
497
+ }
498
+
499
+ async refundWithAuthorization(
500
+ signer: EVMSigner,
501
+ swapData: EVMSwapData,
502
+ signature: SignatureData,
503
+ check?: boolean,
504
+ initAta?: boolean,
505
+ txOptions?: TransactionConfirmationOptions
506
+ ): Promise<string> {
507
+ let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
508
+
509
+ const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
510
+
511
+ return txSignature;
512
+ }
513
+
514
+ async init(
515
+ signer: EVMSigner,
516
+ swapData: EVMSwapData,
517
+ signature: SignatureData,
518
+ skipChecks?: boolean,
519
+ txOptions?: TransactionConfirmationOptions
520
+ ): Promise<string> {
521
+ if(swapData.isPayIn()) {
522
+ if(!swapData.isOfferer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress())) throw new Error("Invalid signer provided!");
523
+ } else {
524
+ if(!swapData.isClaimer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress())) throw new Error("Invalid signer provided!");
525
+ }
526
+
527
+ let result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
528
+
529
+ const txHashes = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
530
+
531
+ return txHashes[txHashes.length - 1];
532
+ }
533
+
534
+ async withdraw(
535
+ signer: EVMSigner,
536
+ token: string,
537
+ amount: bigint,
538
+ txOptions?: TransactionConfirmationOptions
539
+ ): Promise<string> {
540
+ const txs = await this.LpVault.txsWithdraw(signer.getAddress(), token, amount, txOptions?.feeRate);
541
+ const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
542
+ return txId;
543
+ }
544
+
545
+ async deposit(
546
+ signer: EVMSigner,
547
+ token: string,
548
+ amount: bigint,
549
+ txOptions?: TransactionConfirmationOptions
550
+ ): Promise<string> {
551
+ const txs = await this.LpVault.txsDeposit(signer.getAddress(), token, amount, txOptions?.feeRate);
552
+ const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
553
+ return txId;
554
+ }
555
+
556
+ ////////////////////////////////////////////
557
+ //// Fees
558
+ getInitPayInFeeRate(offerer?: string, claimer?: string, token?: string, paymentHash?: string): Promise<string> {
559
+ return this.Chain.Fees.getFeeRate();
560
+ }
561
+
562
+ getInitFeeRate(offerer?: string, claimer?: string, token?: string, paymentHash?: string): Promise<string> {
563
+ return this.Chain.Fees.getFeeRate();
564
+ }
565
+
566
+ getRefundFeeRate(swapData: EVMSwapData): Promise<string> {
567
+ return this.Chain.Fees.getFeeRate();
568
+ }
569
+
570
+ getClaimFeeRate(signer: string, swapData: EVMSwapData): Promise<string> {
571
+ return this.Chain.Fees.getFeeRate();
572
+ }
573
+
574
+ getClaimFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
575
+ return this.Claim.getClaimFee(swapData, feeRate);
576
+ }
577
+
578
+ /**
579
+ * Get the estimated fee of the commit transaction
580
+ */
581
+ getCommitFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
582
+ return this.Init.getInitFee(swapData, feeRate);
583
+ }
584
+
585
+ /**
586
+ * Get the estimated transaction fee of the refund transaction
587
+ */
588
+ getRefundFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
589
+ return this.Refund.getRefundFee(swapData, feeRate);
590
+ }
591
+
592
+ }