@atomiqlabs/lp-lib 14.0.0-dev.21 → 14.0.0-dev.23

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 (167) hide show
  1. package/LICENSE +201 -201
  2. package/dist/fees/IBtcFeeEstimator.d.ts +3 -3
  3. package/dist/fees/IBtcFeeEstimator.js +2 -2
  4. package/dist/index.d.ts +42 -42
  5. package/dist/index.js +58 -58
  6. package/dist/info/InfoHandler.d.ts +17 -17
  7. package/dist/info/InfoHandler.js +61 -61
  8. package/dist/plugins/IPlugin.d.ts +143 -143
  9. package/dist/plugins/IPlugin.js +34 -34
  10. package/dist/plugins/PluginManager.d.ts +112 -112
  11. package/dist/plugins/PluginManager.js +259 -259
  12. package/dist/prices/BinanceSwapPrice.d.ts +26 -26
  13. package/dist/prices/BinanceSwapPrice.js +92 -92
  14. package/dist/prices/CoinGeckoSwapPrice.d.ts +30 -30
  15. package/dist/prices/CoinGeckoSwapPrice.js +64 -64
  16. package/dist/prices/ISwapPrice.d.ts +43 -43
  17. package/dist/prices/ISwapPrice.js +55 -55
  18. package/dist/prices/OKXSwapPrice.d.ts +26 -26
  19. package/dist/prices/OKXSwapPrice.js +92 -92
  20. package/dist/storage/IIntermediaryStorage.d.ts +18 -18
  21. package/dist/storage/IIntermediaryStorage.js +2 -2
  22. package/dist/storagemanager/IntermediaryStorageManager.d.ts +19 -19
  23. package/dist/storagemanager/IntermediaryStorageManager.js +111 -111
  24. package/dist/storagemanager/StorageManager.d.ts +13 -13
  25. package/dist/storagemanager/StorageManager.js +64 -64
  26. package/dist/swaps/SwapHandler.d.ts +153 -153
  27. package/dist/swaps/SwapHandler.js +160 -160
  28. package/dist/swaps/SwapHandlerSwap.d.ts +79 -79
  29. package/dist/swaps/SwapHandlerSwap.js +78 -78
  30. package/dist/swaps/assertions/AmountAssertions.d.ts +28 -28
  31. package/dist/swaps/assertions/AmountAssertions.js +72 -72
  32. package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -76
  33. package/dist/swaps/assertions/FromBtcAmountAssertions.js +180 -180
  34. package/dist/swaps/assertions/LightningAssertions.d.ts +44 -44
  35. package/dist/swaps/assertions/LightningAssertions.js +86 -86
  36. package/dist/swaps/assertions/ToBtcAmountAssertions.d.ts +53 -53
  37. package/dist/swaps/assertions/ToBtcAmountAssertions.js +150 -150
  38. package/dist/swaps/escrow/EscrowHandler.d.ts +51 -51
  39. package/dist/swaps/escrow/EscrowHandler.js +158 -158
  40. package/dist/swaps/escrow/EscrowHandlerSwap.d.ts +35 -35
  41. package/dist/swaps/escrow/EscrowHandlerSwap.js +69 -69
  42. package/dist/swaps/escrow/FromBtcBaseSwap.d.ts +14 -14
  43. package/dist/swaps/escrow/FromBtcBaseSwap.js +32 -32
  44. package/dist/swaps/escrow/FromBtcBaseSwapHandler.d.ts +102 -102
  45. package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +210 -210
  46. package/dist/swaps/escrow/ToBtcBaseSwap.d.ts +36 -36
  47. package/dist/swaps/escrow/ToBtcBaseSwap.js +67 -67
  48. package/dist/swaps/escrow/ToBtcBaseSwapHandler.d.ts +53 -53
  49. package/dist/swaps/escrow/ToBtcBaseSwapHandler.js +81 -81
  50. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.d.ts +83 -83
  51. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +318 -318
  52. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.d.ts +21 -21
  53. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.js +50 -50
  54. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.d.ts +107 -107
  55. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +675 -675
  56. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +33 -33
  57. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +91 -91
  58. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +111 -111
  59. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +684 -684
  60. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +55 -55
  61. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +120 -120
  62. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +171 -171
  63. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +706 -706
  64. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +26 -26
  65. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +62 -62
  66. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +177 -177
  67. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +863 -863
  68. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +24 -24
  69. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +58 -58
  70. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +40 -41
  71. package/dist/swaps/spv_vault_swap/SpvVault.js +111 -111
  72. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +67 -67
  73. package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +158 -158
  74. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -68
  75. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +492 -492
  76. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +52 -52
  77. package/dist/swaps/spv_vault_swap/SpvVaults.js +386 -364
  78. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +51 -51
  79. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +650 -650
  80. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.d.ts +52 -52
  81. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.js +118 -118
  82. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +76 -76
  83. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +494 -494
  84. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
  85. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
  86. package/dist/utils/BitcoinUtils.d.ts +2 -2
  87. package/dist/utils/BitcoinUtils.js +45 -45
  88. package/dist/utils/Utils.d.ts +29 -29
  89. package/dist/utils/Utils.js +89 -89
  90. package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
  91. package/dist/utils/paramcoders/IParamReader.js +2 -2
  92. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
  93. package/dist/utils/paramcoders/IParamWriter.js +2 -2
  94. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
  95. package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
  96. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
  97. package/dist/utils/paramcoders/ParamDecoder.js +222 -222
  98. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
  99. package/dist/utils/paramcoders/ParamEncoder.js +22 -22
  100. package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
  101. package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
  102. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
  103. package/dist/utils/paramcoders/server/ServerParamDecoder.js +107 -107
  104. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
  105. package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
  106. package/dist/wallets/IBitcoinWallet.d.ts +67 -67
  107. package/dist/wallets/IBitcoinWallet.js +2 -2
  108. package/dist/wallets/ILightningWallet.d.ts +117 -117
  109. package/dist/wallets/ILightningWallet.js +37 -37
  110. package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
  111. package/dist/wallets/ISpvVaultSigner.js +2 -2
  112. package/package.json +36 -36
  113. package/src/fees/IBtcFeeEstimator.ts +6 -6
  114. package/src/index.ts +53 -53
  115. package/src/info/InfoHandler.ts +106 -106
  116. package/src/plugins/IPlugin.ts +168 -168
  117. package/src/plugins/PluginManager.ts +336 -336
  118. package/src/prices/BinanceSwapPrice.ts +113 -113
  119. package/src/prices/CoinGeckoSwapPrice.ts +87 -87
  120. package/src/prices/ISwapPrice.ts +88 -88
  121. package/src/prices/OKXSwapPrice.ts +113 -113
  122. package/src/storage/IIntermediaryStorage.ts +19 -19
  123. package/src/storagemanager/IntermediaryStorageManager.ts +118 -118
  124. package/src/storagemanager/StorageManager.ts +78 -78
  125. package/src/swaps/SwapHandler.ts +277 -277
  126. package/src/swaps/SwapHandlerSwap.ts +141 -141
  127. package/src/swaps/assertions/AmountAssertions.ts +76 -76
  128. package/src/swaps/assertions/FromBtcAmountAssertions.ts +246 -246
  129. package/src/swaps/assertions/LightningAssertions.ts +103 -103
  130. package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
  131. package/src/swaps/escrow/EscrowHandler.ts +179 -179
  132. package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
  133. package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
  134. package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +286 -286
  135. package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
  136. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
  137. package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +452 -452
  138. package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
  139. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +856 -856
  140. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -141
  141. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +850 -850
  142. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -196
  143. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +879 -879
  144. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +102 -102
  145. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1112 -1112
  146. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
  147. package/src/swaps/spv_vault_swap/SpvVault.ts +143 -143
  148. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +225 -225
  149. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +628 -628
  150. package/src/swaps/spv_vault_swap/SpvVaults.ts +458 -435
  151. package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +747 -747
  152. package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
  153. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +590 -590
  154. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  155. package/src/utils/BitcoinUtils.ts +42 -42
  156. package/src/utils/Utils.ts +104 -104
  157. package/src/utils/paramcoders/IParamReader.ts +7 -7
  158. package/src/utils/paramcoders/IParamWriter.ts +8 -8
  159. package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
  160. package/src/utils/paramcoders/ParamDecoder.ts +218 -218
  161. package/src/utils/paramcoders/ParamEncoder.ts +29 -29
  162. package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
  163. package/src/utils/paramcoders/server/ServerParamDecoder.ts +118 -118
  164. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  165. package/src/wallets/IBitcoinWallet.ts +68 -68
  166. package/src/wallets/ILightningWallet.ts +178 -178
  167. package/src/wallets/ISpvVaultSigner.ts +10 -10
@@ -1,850 +1,850 @@
1
- import {Express, Request, Response} from "express";
2
- import {createHash} from "crypto";
3
- import {FromBtcLnAutoSwap, FromBtcLnAutoSwapState} from "./FromBtcLnAutoSwap";
4
- import {MultichainData, SwapHandlerType} from "../../SwapHandler";
5
- import {ISwapPrice} from "../../../prices/ISwapPrice";
6
- import {ChainSwapType, ClaimEvent, InitializeEvent, RefundEvent, SwapCommitStateType, SwapData} from "@atomiqlabs/base";
7
- import {expressHandlerWrapper, getAbortController, HEX_REGEX} from "../../../utils/Utils";
8
- import {PluginManager} from "../../../plugins/PluginManager";
9
- import {IIntermediaryStorage} from "../../../storage/IIntermediaryStorage";
10
- import {FieldTypeEnum, verifySchema} from "../../../utils/paramcoders/SchemaVerifier";
11
- import {serverParamDecoder} from "../../../utils/paramcoders/server/ServerParamDecoder";
12
- import {ServerParamEncoder} from "../../../utils/paramcoders/server/ServerParamEncoder";
13
- import {IParamReader} from "../../../utils/paramcoders/IParamReader";
14
- import {FromBtcBaseConfig, FromBtcBaseSwapHandler} from "../FromBtcBaseSwapHandler";
15
- import {
16
- HodlInvoiceInit,
17
- ILightningWallet,
18
- LightningNetworkChannel,
19
- LightningNetworkInvoice
20
- } from "../../../wallets/ILightningWallet";
21
- import {LightningAssertions} from "../../assertions/LightningAssertions";
22
- import {FromBtcLnSwapState} from "../frombtcln_abstract/FromBtcLnSwapAbs";
23
- import {ToBtcLnSwapAbs} from "../tobtcln_abstract/ToBtcLnSwapAbs";
24
-
25
- export type FromBtcLnAutoConfig = FromBtcBaseConfig & {
26
- invoiceTimeoutSeconds?: number,
27
- minCltv: bigint,
28
- gracePeriod: bigint,
29
- gasTokenMax: {[chainId: string]: bigint}
30
- }
31
-
32
- export type FromBtcLnAutoRequestType = {
33
- address: string,
34
- paymentHash: string,
35
- amount: bigint,
36
- token: string,
37
- gasToken: string,
38
- gasAmount: bigint,
39
- claimerBounty: bigint,
40
- descriptionHash?: string,
41
- exactOut?: boolean
42
- }
43
-
44
- /**
45
- * Swap handler handling from BTCLN swaps using submarine swaps
46
- */
47
- export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, FromBtcLnAutoSwapState> {
48
- readonly type = SwapHandlerType.FROM_BTCLN_AUTO;
49
- readonly swapType = ChainSwapType.HTLC;
50
-
51
- activeSubscriptions: Set<string> = new Set<string>();
52
-
53
- readonly config: FromBtcLnAutoConfig;
54
- readonly lightning: ILightningWallet;
55
- readonly LightningAssertions: LightningAssertions;
56
-
57
- constructor(
58
- storageDirectory: IIntermediaryStorage<FromBtcLnAutoSwap>,
59
- path: string,
60
- chains: MultichainData,
61
- lightning: ILightningWallet,
62
- swapPricing: ISwapPrice,
63
- config: FromBtcLnAutoConfig
64
- ) {
65
- super(storageDirectory, path, chains, swapPricing, config);
66
- this.config = config;
67
- this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
68
- this.lightning = lightning;
69
- this.LightningAssertions = new LightningAssertions(this.logger, lightning);
70
- }
71
-
72
- protected async processPastSwap(swap: FromBtcLnAutoSwap): Promise<"REFUND" | "SETTLE" | null> {
73
- const {swapContract, signer} = this.getChain(swap.chainIdentifier);
74
- if(swap.state===FromBtcLnAutoSwapState.CREATED) {
75
- //Check if already paid
76
- const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
77
- const invoice = await this.lightning.getInvoice(parsedPR.id);
78
-
79
- const isBeingPaid = invoice.status==="held";
80
- if(!isBeingPaid) {
81
- //Not paid
82
- const isInvoiceExpired = parsedPR.expiryEpochMillis<Date.now();
83
- if(isInvoiceExpired) {
84
- this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap LN invoice expired, cancelling, invoice: "+swap.pr);
85
- await this.cancelSwapAndInvoice(swap);
86
- return null;
87
- }
88
- this.subscribeToInvoice(swap);
89
- return null;
90
- }
91
-
92
- //Adjust the state of the swap and expiry
93
- try {
94
- await this.htlcReceived(swap, invoice);
95
- //Result is either FromBtcLnSwapState.RECEIVED or FromBtcLnSwapState.CANCELED
96
- } catch (e) {
97
- this.swapLogger.error(swap, "processPastSwap(state=CREATED): htlcReceived error", e);
98
- }
99
-
100
- return null;
101
- }
102
-
103
- if(swap.state===FromBtcLnAutoSwapState.RECEIVED) {
104
- try {
105
- if(!await this.offerHtlc(swap)) {
106
- //Expired
107
- if(swap.state===FromBtcLnAutoSwapState.RECEIVED) {
108
- this.swapLogger.info(swap, "processPastSwap(state=RECEIVED): offer HTLC expired, cancelling invoice: "+swap.pr);
109
- await this.cancelSwapAndInvoice(swap);
110
- }
111
- }
112
- } catch (e) {
113
- this.swapLogger.error(swap, "processPastSwap(state=RECEIVED): offerHtlc error", e);
114
- }
115
-
116
- return null;
117
- }
118
-
119
- if(swap.state===FromBtcLnAutoSwapState.TXS_SENT || swap.state===FromBtcLnAutoSwapState.COMMITED) {
120
- const onchainStatus = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
121
- const state: FromBtcLnAutoSwapState = swap.state as FromBtcLnAutoSwapState;
122
- if(onchainStatus.type===SwapCommitStateType.PAID) {
123
- //Extract the swap secret
124
- if(state!==FromBtcLnAutoSwapState.CLAIMED && state!==FromBtcLnAutoSwapState.SETTLED) {
125
- const secretHex = await onchainStatus.getClaimResult();
126
- const secret: Buffer = Buffer.from(secretHex, "hex");
127
- const paymentHash: Buffer = createHash("sha256").update(secret).digest();
128
- const paymentHashHex = paymentHash.toString("hex");
129
-
130
- if (swap.lnPaymentHash!==paymentHashHex) {
131
- //TODO: Possibly fatal failure
132
- this.swapLogger.error(swap, "processPastSwap(state=TXS_SENT|COMMITED): onchainStatus=PAID, Invalid swap secret specified: "+secretHex+" for paymentHash: "+paymentHashHex);
133
- return null;
134
- }
135
-
136
- swap.secret = secretHex;
137
- await swap.setState(FromBtcLnAutoSwapState.CLAIMED);
138
- await this.saveSwapData(swap);
139
-
140
- this.swapLogger.warn(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap settled (detected from processPastSwap), invoice: "+swap.pr);
141
-
142
- return "SETTLE";
143
- }
144
- return null;
145
- }
146
- if(onchainStatus.type===SwapCommitStateType.COMMITED) {
147
- if(state===FromBtcLnAutoSwapState.TXS_SENT) {
148
- await swap.setState(FromBtcLnAutoSwapState.COMMITED);
149
- await this.saveSwapData(swap);
150
-
151
- this.swapLogger.info(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap committed (detected from processPastSwap), invoice: "+swap.pr);
152
- }
153
- return null;
154
- }
155
- if(onchainStatus.type===SwapCommitStateType.NOT_COMMITED || onchainStatus.type===SwapCommitStateType.EXPIRED) {
156
- if(swap.state===FromBtcLnAutoSwapState.TXS_SENT) {
157
- const isAuthorizationExpired = await swapContract.isInitAuthorizationExpired(swap.data, swap);
158
- if(isAuthorizationExpired) {
159
- this.swapLogger.info(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap not committed before authorization expiry, cancelling the LN invoice, invoice: "+swap.pr);
160
- await this.cancelSwapAndInvoice(swap);
161
- return null;
162
- }
163
- } else {
164
- if(await swapContract.isExpired(signer.getAddress(), swap.data)) {
165
- this.swapLogger.info(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap timed out, refunding to self, invoice: "+swap.pr);
166
- return "REFUND";
167
- }
168
- }
169
- }
170
- if(onchainStatus.type===SwapCommitStateType.REFUNDABLE) {
171
- this.swapLogger.info(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap timed out, refunding to self, invoice: "+swap.pr);
172
- return "REFUND";
173
- }
174
- }
175
-
176
- if(swap.state===FromBtcLnAutoSwapState.CLAIMED) return "SETTLE";
177
- if(swap.state===FromBtcLnAutoSwapState.CANCELED) await this.cancelSwapAndInvoice(swap);
178
- }
179
-
180
- protected async refundSwaps(refundSwaps: FromBtcLnAutoSwap[]) {
181
- for(let refundSwap of refundSwaps) {
182
- const {swapContract, signer} = this.getChain(refundSwap.chainIdentifier);
183
- const unlock = refundSwap.lock(swapContract.refundTimeout);
184
- if(unlock==null) continue;
185
-
186
- this.swapLogger.debug(refundSwap, "refundSwaps(): initiate refund of swap");
187
- await swapContract.refund(signer, refundSwap.data, true, false, {waitForConfirmation: true});
188
- this.swapLogger.info(refundSwap, "refundsSwaps(): swap refunded, invoice: "+refundSwap.pr);
189
-
190
- await refundSwap.setState(FromBtcLnAutoSwapState.REFUNDED);
191
- unlock();
192
- }
193
- }
194
-
195
- protected async settleInvoices(swaps: FromBtcLnAutoSwap[]) {
196
- for(let swap of swaps) {
197
- try {
198
- await this.lightning.settleHodlInvoice(swap.secret);
199
- if(swap.metadata!=null) swap.metadata.times.htlcSettled = Date.now();
200
- await this.removeSwapData(swap, FromBtcLnAutoSwapState.SETTLED);
201
-
202
- this.swapLogger.info(swap, "settleInvoices(): invoice settled, secret: "+swap.secret);
203
- } catch (e) {
204
- this.swapLogger.error(swap, "settleInvoices(): cannot settle invoice", e);
205
- }
206
- }
207
- }
208
-
209
- /**
210
- * Checks past swaps, refunds and deletes ones that are already expired.
211
- */
212
- protected async processPastSwaps() {
213
-
214
- const settleInvoices: FromBtcLnAutoSwap[] = [];
215
- const refundSwaps: FromBtcLnAutoSwap[] = [];
216
-
217
- const queriedData = await this.storageManager.query([
218
- {
219
- key: "state",
220
- value: [
221
- FromBtcLnAutoSwapState.CREATED,
222
- FromBtcLnAutoSwapState.RECEIVED,
223
- FromBtcLnAutoSwapState.TXS_SENT,
224
- FromBtcLnAutoSwapState.COMMITED,
225
- FromBtcLnAutoSwapState.CLAIMED,
226
- FromBtcLnAutoSwapState.CANCELED,
227
- ]
228
- }
229
- ]);
230
-
231
- for(let {obj: swap} of queriedData) {
232
- switch(await this.processPastSwap(swap)) {
233
- case "SETTLE":
234
- settleInvoices.push(swap);
235
- break;
236
- case "REFUND":
237
- refundSwaps.push(swap);
238
- break;
239
- }
240
- }
241
-
242
- await this.refundSwaps(refundSwaps);
243
- await this.settleInvoices(settleInvoices);
244
- }
245
-
246
- protected async processInitializeEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: InitializeEvent<SwapData>): Promise<void> {
247
- this.swapLogger.info(savedSwap, "SC: InitializeEvent: HTLC initialized by the client, invoice: "+savedSwap.pr);
248
-
249
- if(savedSwap.state===FromBtcLnAutoSwapState.TXS_SENT) {
250
- await savedSwap.setState(FromBtcLnAutoSwapState.COMMITED);
251
- await this.saveSwapData(savedSwap);
252
- }
253
- }
254
-
255
- protected async processClaimEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: ClaimEvent<SwapData>): Promise<void> {
256
- //Claim
257
- //This is the important part, we need to catch the claim TX, else we may lose money
258
- const secret: Buffer = Buffer.from(event.result, "hex");
259
- const paymentHash: Buffer = createHash("sha256").update(secret).digest();
260
- const secretHex = secret.toString("hex");
261
- const paymentHashHex = paymentHash.toString("hex");
262
-
263
- if (savedSwap.lnPaymentHash!==paymentHashHex) return;
264
-
265
- this.swapLogger.info(savedSwap, "SC: ClaimEvent: swap HTLC successfully claimed by the client, invoice: "+savedSwap.pr);
266
-
267
- try {
268
- await this.lightning.settleHodlInvoice(secretHex);
269
- this.swapLogger.info(savedSwap, "SC: ClaimEvent: invoice settled, secret: "+secretHex);
270
- savedSwap.secret = secretHex;
271
- if(savedSwap.metadata!=null) savedSwap.metadata.times.htlcSettled = Date.now();
272
- await this.removeSwapData(savedSwap, FromBtcLnAutoSwapState.SETTLED);
273
- } catch (e) {
274
- this.swapLogger.error(savedSwap, "SC: ClaimEvent: cannot settle invoice", e);
275
- savedSwap.secret = secretHex;
276
- await savedSwap.setState(FromBtcLnAutoSwapState.CLAIMED);
277
- await this.saveSwapData(savedSwap);
278
- }
279
-
280
- }
281
-
282
- protected async processRefundEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: RefundEvent<SwapData>): Promise<void> {
283
- this.swapLogger.info(savedSwap, "SC: RefundEvent: swap refunded to us, invoice: "+savedSwap.pr);
284
-
285
- //We don't cancel the incoming invoice, to make the offender pay for this with locked liquidity
286
- // await this.lightning.cancelHodlInvoice(savedSwap.lnPaymentHash);
287
- await this.removeSwapData(savedSwap, FromBtcLnAutoSwapState.REFUNDED)
288
- }
289
-
290
- /**
291
- * Subscribe to a lightning network invoice
292
- *
293
- * @param swap
294
- */
295
- private subscribeToInvoice(swap: FromBtcLnAutoSwap): boolean {
296
- const paymentHash = swap.lnPaymentHash;
297
- if(this.activeSubscriptions.has(paymentHash)) return false;
298
-
299
- this.lightning.waitForInvoice(paymentHash).then(result => {
300
- this.swapLogger.info(swap, "subscribeToInvoice(): result callback, outcome: "+result.status+" invoice: "+swap.pr);
301
- if(result.status==="held")
302
- this.htlcReceived(swap, result).catch(e => this.swapLogger.error(swap, "subscribeToInvoice(): HTLC received result", e));
303
- this.activeSubscriptions.delete(paymentHash);
304
- });
305
- this.swapLogger.info(swap, "subscribeToInvoice(): subscribe to invoice: "+swap.pr);
306
-
307
- this.activeSubscriptions.add(paymentHash);
308
- return true;
309
- }
310
-
311
- /**
312
- * Called when lightning HTLC is received, also signs an init transaction on the smart chain side, expiry of the
313
- * smart chain authorization starts ticking as soon as this HTLC is received
314
- *
315
- * @param invoiceData
316
- * @param invoice
317
- */
318
- private async htlcReceived(invoiceData: FromBtcLnAutoSwap, invoice: LightningNetworkInvoice) {
319
- if(invoiceData.state!==FromBtcLnAutoSwapState.CREATED) return;
320
- this.swapLogger.debug(invoiceData, "htlcReceived(): invoice: ", invoice);
321
- if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcReceived = Date.now();
322
-
323
- const useToken = invoiceData.token;
324
- const gasToken = invoiceData.gasToken;
325
-
326
- let expiryTimeout: bigint;
327
- try {
328
- //Check if HTLC expiry is long enough
329
- expiryTimeout = await this.checkHtlcExpiry(invoice);
330
- if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcTimeoutCalculated = Date.now();
331
- } catch (e) {
332
- if(invoiceData.state===FromBtcLnAutoSwapState.CREATED) await this.cancelSwapAndInvoice(invoiceData);
333
- throw e;
334
- }
335
-
336
- const {swapContract, signer} = this.getChain(invoiceData.chainIdentifier);
337
-
338
- //Create real swap data
339
- const swapData: SwapData = await swapContract.createSwapData(
340
- ChainSwapType.HTLC,
341
- signer.getAddress(),
342
- invoiceData.claimer,
343
- useToken,
344
- invoiceData.amountToken,
345
- invoiceData.claimHash,
346
- 0n,
347
- BigInt(Math.floor(Date.now() / 1000)) + expiryTimeout,
348
- false,
349
- true,
350
- invoiceData.amountGasToken + invoiceData.claimerBounty,
351
- invoiceData.claimerBounty,
352
- invoiceData.gasToken
353
- );
354
- if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcSwapCreated = Date.now();
355
-
356
- //Important to prevent race condition and issuing 2 signed init messages at the same time
357
- if(invoiceData.state===FromBtcLnAutoSwapState.CREATED) {
358
- invoiceData.data = swapData;
359
- invoiceData.signature = null;
360
- invoiceData.timeout = (BigInt(Math.floor(Date.now() / 1000)) + 120n).toString(10);
361
-
362
- //Setting the state variable is done outside the promise, so is done synchronously
363
- await invoiceData.setState(FromBtcLnAutoSwapState.RECEIVED);
364
-
365
- await this.saveSwapData(invoiceData);
366
-
367
- await this.offerHtlc(invoiceData);
368
- }
369
- }
370
-
371
- private async offerHtlc(invoiceData: FromBtcLnAutoSwap) {
372
- if(invoiceData.state!==FromBtcLnAutoSwapState.RECEIVED) return;
373
-
374
- this.swapLogger.debug(invoiceData, "offerHtlc(): invoice: ", invoiceData.pr);
375
- if(invoiceData.metadata!=null) invoiceData.metadata.times.offerHtlc = Date.now();
376
-
377
- const useToken = invoiceData.token;
378
- const gasToken = invoiceData.gasToken;
379
-
380
- const {swapContract, signer, chainInterface} = this.getChain(invoiceData.chainIdentifier);
381
-
382
- //Create abort controller for parallel fetches
383
- const abortController = new AbortController();
384
-
385
- //Pre-fetch data
386
- const balancePrefetch: Promise<bigint> = this.getBalancePrefetch(invoiceData.chainIdentifier, useToken, abortController);
387
- const gasTokenBalancePrefetch: Promise<bigint> = invoiceData.getTotalOutputGasAmount()===0n || useToken===gasToken ?
388
- null : this.getBalancePrefetch(invoiceData.chainIdentifier, gasToken, abortController);
389
-
390
- if(await swapContract.getInitAuthorizationExpiry(invoiceData.data, invoiceData) < Date.now()) {
391
- if(invoiceData.state===FromBtcLnAutoSwapState.RECEIVED) {
392
- await this.cancelSwapAndInvoice(invoiceData);
393
- }
394
- return false;
395
- }
396
-
397
- try {
398
- //Check if we have enough liquidity to proceed
399
- if(useToken===gasToken) {
400
- await this.checkBalance(invoiceData.getTotalOutputAmount() + invoiceData.getTotalOutputGasAmount(), balancePrefetch, abortController.signal);
401
- } else {
402
- await this.checkBalance(invoiceData.getTotalOutputAmount(), balancePrefetch, abortController.signal);
403
- await this.checkBalance(invoiceData.getTotalOutputGasAmount(), gasTokenBalancePrefetch, abortController.signal);
404
- }
405
- if(invoiceData.metadata!=null) invoiceData.metadata.times.offerHtlcChecked = Date.now();
406
- } catch (e) {
407
- if(!abortController.signal.aborted) {
408
- if(invoiceData.state===FromBtcLnAutoSwapState.RECEIVED) await this.cancelSwapAndInvoice(invoiceData);
409
- }
410
- throw e;
411
- }
412
-
413
- const txWithdraw = await swapContract.txsWithdraw(signer.getAddress(), gasToken, invoiceData.data.getTotalDeposit());
414
- const txInit = await swapContract.txsInit(signer.getAddress(), invoiceData.data, {
415
- prefix: invoiceData.prefix,
416
- timeout: invoiceData.timeout,
417
- signature: invoiceData.signature
418
- }, true);
419
-
420
- if(invoiceData.state===FromBtcLnAutoSwapState.RECEIVED) {
421
- //Setting the state variable is done outside the promise, so is done synchronously
422
- await invoiceData.setState(FromBtcLnAutoSwapState.TXS_SENT);
423
- await this.saveSwapData(invoiceData);
424
- await chainInterface.sendAndConfirm(signer, [...txWithdraw, ...txInit], true, undefined, true);
425
- }
426
-
427
- return true;
428
- }
429
-
430
- /**
431
- * Checks invoice description hash
432
- *
433
- * @param descriptionHash
434
- * @throws {DefinedRuntimeError} will throw an error if the description hash is invalid
435
- */
436
- private checkDescriptionHash(descriptionHash: string) {
437
- if(descriptionHash!=null) {
438
- if(typeof(descriptionHash)!=="string" || !HEX_REGEX.test(descriptionHash) || descriptionHash.length!==64) {
439
- throw {
440
- code: 20100,
441
- msg: "Invalid request body (descriptionHash)"
442
- };
443
- }
444
- }
445
- }
446
-
447
- /**
448
- * Asynchronously sends the LN node's public key to the client, so he can pre-fetch the node's channels from 1ml api
449
- *
450
- * @param responseStream
451
- */
452
- private sendPublicKeyAsync(responseStream: ServerParamEncoder) {
453
- this.lightning.getIdentityPublicKey().then(publicKey => responseStream.writeParams({
454
- lnPublicKey: publicKey
455
- })).catch(e => {
456
- this.logger.error("sendPublicKeyAsync(): error", e);
457
- });
458
- }
459
-
460
- /**
461
- * Returns the CLTV timeout (blockheight) of the received HTLC corresponding to the invoice. If multiple HTLCs are
462
- * received (MPP) it returns the lowest of the timeouts
463
- *
464
- * @param invoice
465
- */
466
- private getInvoicePaymentsTimeout(invoice: LightningNetworkInvoice): number | null {
467
- let timeout: number = null;
468
- invoice.payments.forEach((curr) => {
469
- if (timeout == null || timeout > curr.timeout) timeout = curr.timeout;
470
- });
471
- return timeout;
472
- }
473
-
474
- /**
475
- * Checks if the received HTLC's CLTV timeout is large enough to still process the swap
476
- *
477
- * @param invoice
478
- * @throws {DefinedRuntimeError} Will throw if HTLC expires too soon and therefore cannot be processed
479
- * @returns expiry timeout in seconds
480
- */
481
- private async checkHtlcExpiry(invoice: LightningNetworkInvoice): Promise<bigint> {
482
- const timeout: number = this.getInvoicePaymentsTimeout(invoice);
483
- const current_block_height = await this.lightning.getBlockheight();
484
-
485
- const blockDelta = BigInt(timeout - current_block_height);
486
-
487
- const htlcExpiresTooSoon = blockDelta < this.config.minCltv;
488
- if(htlcExpiresTooSoon) {
489
- throw {
490
- code: 20002,
491
- msg: "Not enough time to reliably process the swap",
492
- data: {
493
- requiredDelta: this.config.minCltv.toString(10),
494
- actualDelta: blockDelta.toString(10)
495
- }
496
- };
497
- }
498
-
499
- return (this.config.minCltv * this.config.bitcoinBlocktime / this.config.safetyFactor) - this.config.gracePeriod;
500
- }
501
-
502
- /**
503
- * Cancels the swap (CANCELED state) & also cancels the LN invoice (including all pending HTLCs)
504
- *
505
- * @param invoiceData
506
- */
507
- private async cancelSwapAndInvoice(invoiceData: FromBtcLnAutoSwap): Promise<void> {
508
- await invoiceData.setState(FromBtcLnAutoSwapState.CANCELED);
509
- await this.lightning.cancelHodlInvoice(invoiceData.lnPaymentHash);
510
- await this.removeSwapData(invoiceData);
511
- this.swapLogger.info(invoiceData, "cancelSwapAndInvoice(): swap removed & invoice cancelled, invoice: ", invoiceData.pr);
512
- };
513
-
514
- /**
515
- *
516
- * Checks if the lightning invoice is in HELD state (htlcs received but yet unclaimed)
517
- *
518
- * @param paymentHash
519
- * @throws {DefinedRuntimeError} Will throw if the lightning invoice is not found, or if it isn't in the HELD state
520
- * @returns the fetched lightning invoice
521
- */
522
- private async checkInvoiceStatus(paymentHash: string): Promise<any> {
523
- const invoice = await this.lightning.getInvoice(paymentHash);
524
- if(invoice==null) throw {
525
- _httpStatus: 200,
526
- code: 10001,
527
- msg: "Invoice expired/canceled"
528
- };
529
-
530
- const arr = invoice.description.split("-");
531
- let chainIdentifier: string;
532
- let address: string;
533
- if(arr.length>1) {
534
- chainIdentifier = arr[0];
535
- address = arr[1];
536
- } else {
537
- chainIdentifier = this.chains.default;
538
- address = invoice.description;
539
- }
540
- const {chainInterface} = this.getChain(chainIdentifier);
541
- if(!chainInterface.isValidAddress(address)) throw {
542
- _httpStatus: 200,
543
- code: 10001,
544
- msg: "Invoice expired/canceled"
545
- };
546
-
547
- switch(invoice.status) {
548
- case "canceled":
549
- throw {
550
- _httpStatus: 200,
551
- code: 10001,
552
- msg: "Invoice expired/canceled"
553
- }
554
- case "confirmed":
555
- throw {
556
- _httpStatus: 200,
557
- code: 10002,
558
- msg: "Invoice already paid"
559
- };
560
- case "unpaid":
561
- throw {
562
- _httpStatus: 200,
563
- code: 10003,
564
- msg: "Invoice yet unpaid"
565
- };
566
- default:
567
- return invoice;
568
- }
569
- }
570
-
571
- startRestServer(restServer: Express) {
572
-
573
- restServer.use(this.path+"/createInvoice", serverParamDecoder(10*1000));
574
- restServer.post(this.path+"/createInvoice", expressHandlerWrapper(async (req: Request & {paramReader: IParamReader}, res: Response & {responseStream: ServerParamEncoder}) => {
575
- const metadata: {
576
- request: any,
577
- invoiceRequest?: any,
578
- invoiceResponse?: any,
579
- times: {[key: string]: number}
580
- } = {request: {}, times: {}};
581
-
582
- const chainIdentifier = req.query.chain as string ?? this.chains.default;
583
- const {swapContract, signer, chainInterface} = this.getChain(chainIdentifier);
584
- if(!swapContract.supportsInitWithoutClaimer) throw {
585
- code: 20299,
586
- msg: "Not supported for "+chainIdentifier
587
- };
588
-
589
- metadata.times.requestReceived = Date.now();
590
-
591
- /**
592
- * address: string smart chain address of the recipient
593
- * paymentHash: string payment hash of the to-be-created invoice
594
- * amount: string amount (in sats) of the invoice
595
- * token: string Desired token to swap
596
- * exactOut: boolean Whether the swap should be an exact out instead of exact in swap
597
- * descriptionHash: string Description hash of the invoice
598
- * gasAmount: string Desired amount in gas token to also get
599
- * gasToken: string
600
- * claimerBounty: string Desired amount to be left out as a claimer bounty
601
- */
602
- const parsedBody: FromBtcLnAutoRequestType = await req.paramReader.getParams({
603
- address: (val: string) => val!=null &&
604
- typeof(val)==="string" &&
605
- chainInterface.isValidAddress(val) ? val : null,
606
- paymentHash: (val: string) => val!=null &&
607
- typeof(val)==="string" &&
608
- val.length===64 &&
609
- HEX_REGEX.test(val) ? val: null,
610
- amount: FieldTypeEnum.BigInt,
611
- token: (val: string) => val!=null &&
612
- typeof(val)==="string" &&
613
- this.isTokenSupported(chainIdentifier, val) ? val : null,
614
- descriptionHash: FieldTypeEnum.StringOptional,
615
- exactOut: FieldTypeEnum.BooleanOptional,
616
- gasToken: (val: string) => val!=null &&
617
- typeof(val)==="string" &&
618
- chainInterface.isValidToken(val) ? val : null,
619
- gasAmount: FieldTypeEnum.BigInt,
620
- claimerBounty: FieldTypeEnum.BigInt
621
- });
622
- if(parsedBody==null) throw {
623
- code: 20100,
624
- msg: "Invalid request body"
625
- };
626
-
627
- if(parsedBody.gasToken!==chainInterface.getNativeCurrencyAddress()) throw {
628
- code: 20290,
629
- msg: "Unsupported gas token"
630
- };
631
-
632
- if(parsedBody.gasAmount < 0) throw {
633
- code: 20291,
634
- msg: "Invalid gas amount, negative"
635
- };
636
- if(parsedBody.claimerBounty < 0) throw {
637
- code: 20292,
638
- msg: "Invalid claimer bounty, negative"
639
- };
640
- metadata.request = parsedBody;
641
-
642
- const requestedAmount = {input: !parsedBody.exactOut, amount: parsedBody.amount, token: parsedBody.token};
643
- const gasTokenAmount = {
644
- input: false,
645
- amount: parsedBody.gasAmount + parsedBody.claimerBounty,
646
- token: parsedBody.gasToken
647
- } as const;
648
- const request = {
649
- chainIdentifier,
650
- raw: req,
651
- parsed: parsedBody,
652
- metadata
653
- };
654
- const useToken = parsedBody.token;
655
- const gasToken = parsedBody.gasToken;
656
-
657
- //Check request params
658
- this.checkDescriptionHash(parsedBody.descriptionHash);
659
- const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount, gasTokenAmount);
660
- metadata.times.requestChecked = Date.now();
661
-
662
- //Create abortController for parallel prefetches
663
- const responseStream = res.responseStream;
664
- const abortController = getAbortController(responseStream);
665
-
666
- //Pre-fetch data
667
- const {
668
- pricePrefetchPromise,
669
- gasTokenPricePrefetchPromise
670
- } = this.getFromBtcPricePrefetches(chainIdentifier, useToken, gasToken, abortController);
671
- const balancePrefetch: Promise<bigint> = this.getBalancePrefetch(chainIdentifier, useToken, abortController);
672
- const gasTokenBalancePrefetch: Promise<bigint> = gasTokenAmount.amount===0n || useToken===gasToken ?
673
- null : this.getBalancePrefetch(chainIdentifier, gasToken, abortController);
674
- const channelsPrefetch: Promise<LightningNetworkChannel[]> = this.LightningAssertions.getChannelsPrefetch(abortController);
675
-
676
- //Asynchronously send the node's public key to the client
677
- this.sendPublicKeyAsync(responseStream);
678
-
679
- //Check valid amount specified (min/max)
680
- let {
681
- amountBD,
682
- swapFee,
683
- swapFeeInToken,
684
- totalInToken,
685
- amountBDgas,
686
- gasSwapFee,
687
- gasSwapFeeInToken,
688
- totalInGasToken
689
- } = await this.AmountAssertions.checkFromBtcAmount(
690
- this.type, request,
691
- {...requestedAmount, pricePrefetch: pricePrefetchPromise},
692
- fees, abortController.signal,
693
- {...gasTokenAmount, pricePrefetch: gasTokenPricePrefetchPromise}
694
- );
695
- metadata.times.priceCalculated = Date.now();
696
-
697
- const totalBtcInput = amountBD + amountBDgas;
698
-
699
- //Check if we have enough funds to honor the request
700
- if(useToken===gasToken) {
701
- await this.checkBalance(totalInToken + totalInGasToken, balancePrefetch, abortController.signal);
702
- } else {
703
- await this.checkBalance(totalInToken, balancePrefetch, abortController.signal);
704
- await this.checkBalance(totalInGasToken, gasTokenBalancePrefetch, abortController.signal);
705
- }
706
- await this.LightningAssertions.checkInboundLiquidity(totalBtcInput, channelsPrefetch, abortController.signal);
707
- metadata.times.balanceChecked = Date.now();
708
-
709
- //Create swap
710
- const hodlInvoiceObj: HodlInvoiceInit = {
711
- description: chainIdentifier+"-"+parsedBody.address,
712
- cltvDelta: Number(this.config.minCltv) + 5,
713
- expiresAt: Date.now()+(this.config.invoiceTimeoutSeconds*1000),
714
- id: parsedBody.paymentHash,
715
- mtokens: totalBtcInput * 1000n,
716
- descriptionHash: parsedBody.descriptionHash
717
- };
718
- metadata.invoiceRequest = hodlInvoiceObj;
719
-
720
- const hodlInvoice = await this.lightning.createHodlInvoice(hodlInvoiceObj);
721
- abortController.signal.throwIfAborted();
722
- metadata.times.invoiceCreated = Date.now();
723
- metadata.invoiceResponse = {...hodlInvoice};
724
-
725
- totalInGasToken -= parsedBody.claimerBounty;
726
-
727
- const createdSwap = new FromBtcLnAutoSwap(
728
- chainIdentifier,
729
- hodlInvoice.request,
730
- parsedBody.paymentHash,
731
- swapContract.getHashForHtlc(Buffer.from(parsedBody.paymentHash, "hex")).toString("hex"),
732
- hodlInvoice.mtokens,
733
- parsedBody.address,
734
- useToken,
735
- gasToken,
736
- totalInToken,
737
- totalInGasToken,
738
- swapFee,
739
- swapFeeInToken,
740
- gasSwapFee,
741
- gasSwapFeeInToken,
742
- parsedBody.claimerBounty
743
- );
744
- metadata.times.swapCreated = Date.now();
745
-
746
- createdSwap.metadata = metadata;
747
-
748
- await PluginManager.swapCreate(createdSwap);
749
- await this.saveSwapData(createdSwap);
750
-
751
- this.swapLogger.info(createdSwap, "REST: /createInvoice: Created swap invoice: "+hodlInvoice.request+" amount: "+totalBtcInput.toString(10));
752
- this.subscribeToInvoice(createdSwap);
753
-
754
- await responseStream.writeParamsAndEnd({
755
- code: 20000,
756
- msg: "Success",
757
- data: {
758
- intermediaryKey: signer.getAddress(),
759
- pr: hodlInvoice.request,
760
-
761
- btcAmountSwap: amountBD.toString(10),
762
- btcAmountGas: amountBDgas.toString(10),
763
-
764
- total: totalInToken.toString(10),
765
- totalGas: totalInGasToken.toString(10),
766
-
767
- totalFeeBtc: (swapFee + gasSwapFee).toString(10),
768
-
769
- swapFeeBtc: swapFee.toString(10),
770
- swapFee: swapFeeInToken.toString(10),
771
-
772
- gasSwapFeeBtc: gasSwapFee.toString(10),
773
- gasSwapFee: gasSwapFeeInToken.toString(10),
774
-
775
- claimerBounty: parsedBody.claimerBounty.toString(10)
776
- }
777
- });
778
-
779
- }));
780
-
781
- const getInvoiceStatus = expressHandlerWrapper(async (req, res) => {
782
- /**
783
- * paymentHash: string payment hash of the invoice
784
- */
785
- const parsedBody = verifySchema({...req.body, ...req.query}, {
786
- paymentHash: (val: string) => val!=null &&
787
- typeof(val)==="string" &&
788
- val.length===64 &&
789
- HEX_REGEX.test(val) ? val: null,
790
- });
791
-
792
- await this.checkInvoiceStatus(parsedBody.paymentHash);
793
-
794
- const swap: FromBtcLnAutoSwap = await this.storageManager.getData(parsedBody.paymentHash, null);
795
- if (swap==null) throw {
796
- _httpStatus: 200,
797
- code: 10001,
798
- msg: "Invoice expired/canceled"
799
- };
800
-
801
- if (
802
- swap.state === FromBtcLnAutoSwapState.RECEIVED ||
803
- swap.state === FromBtcLnAutoSwapState.TXS_SENT ||
804
- swap.state === FromBtcLnAutoSwapState.COMMITED
805
- ) {
806
- res.status(200).json({
807
- code: 10000,
808
- msg: "Success",
809
- data: {
810
- data: swap.data.serialize()
811
- }
812
- });
813
- } else {
814
- res.status(200).json({
815
- code: 10003,
816
- msg: "Invoice yet unpaid"
817
- });
818
- }
819
-
820
- });
821
-
822
- restServer.post(this.path+"/getInvoiceStatus", getInvoiceStatus);
823
- restServer.get(this.path+"/getInvoiceStatus", getInvoiceStatus);
824
-
825
- this.logger.info("started at path: ", this.path);
826
- }
827
-
828
- async init() {
829
- await this.loadData(FromBtcLnAutoSwap);
830
- this.subscribeToEvents();
831
- await PluginManager.serviceInitialize(this);
832
- }
833
-
834
- getInfoData(): any {
835
- const mappedDict = {};
836
- for(let chainId in this.config.gasTokenMax) {
837
- mappedDict[chainId] = {
838
- gasToken: this.getChain(chainId).chainInterface.getNativeCurrencyAddress(),
839
- max: this.config.gasTokenMax[chainId].toString(10)
840
- };
841
- }
842
- return {
843
- minCltv: Number(this.config.minCltv),
844
- invoiceTimeoutSeconds: this.config.invoiceTimeoutSeconds,
845
- gasTokens: mappedDict
846
- };
847
- }
848
-
849
- }
850
-
1
+ import {Express, Request, Response} from "express";
2
+ import {createHash} from "crypto";
3
+ import {FromBtcLnAutoSwap, FromBtcLnAutoSwapState} from "./FromBtcLnAutoSwap";
4
+ import {MultichainData, SwapHandlerType} from "../../SwapHandler";
5
+ import {ISwapPrice} from "../../../prices/ISwapPrice";
6
+ import {ChainSwapType, ClaimEvent, InitializeEvent, RefundEvent, SwapCommitStateType, SwapData} from "@atomiqlabs/base";
7
+ import {expressHandlerWrapper, getAbortController, HEX_REGEX} from "../../../utils/Utils";
8
+ import {PluginManager} from "../../../plugins/PluginManager";
9
+ import {IIntermediaryStorage} from "../../../storage/IIntermediaryStorage";
10
+ import {FieldTypeEnum, verifySchema} from "../../../utils/paramcoders/SchemaVerifier";
11
+ import {serverParamDecoder} from "../../../utils/paramcoders/server/ServerParamDecoder";
12
+ import {ServerParamEncoder} from "../../../utils/paramcoders/server/ServerParamEncoder";
13
+ import {IParamReader} from "../../../utils/paramcoders/IParamReader";
14
+ import {FromBtcBaseConfig, FromBtcBaseSwapHandler} from "../FromBtcBaseSwapHandler";
15
+ import {
16
+ HodlInvoiceInit,
17
+ ILightningWallet,
18
+ LightningNetworkChannel,
19
+ LightningNetworkInvoice
20
+ } from "../../../wallets/ILightningWallet";
21
+ import {LightningAssertions} from "../../assertions/LightningAssertions";
22
+ import {FromBtcLnSwapState} from "../frombtcln_abstract/FromBtcLnSwapAbs";
23
+ import {ToBtcLnSwapAbs} from "../tobtcln_abstract/ToBtcLnSwapAbs";
24
+
25
+ export type FromBtcLnAutoConfig = FromBtcBaseConfig & {
26
+ invoiceTimeoutSeconds?: number,
27
+ minCltv: bigint,
28
+ gracePeriod: bigint,
29
+ gasTokenMax: {[chainId: string]: bigint}
30
+ }
31
+
32
+ export type FromBtcLnAutoRequestType = {
33
+ address: string,
34
+ paymentHash: string,
35
+ amount: bigint,
36
+ token: string,
37
+ gasToken: string,
38
+ gasAmount: bigint,
39
+ claimerBounty: bigint,
40
+ descriptionHash?: string,
41
+ exactOut?: boolean
42
+ }
43
+
44
+ /**
45
+ * Swap handler handling from BTCLN swaps using submarine swaps
46
+ */
47
+ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, FromBtcLnAutoSwapState> {
48
+ readonly type = SwapHandlerType.FROM_BTCLN_AUTO;
49
+ readonly swapType = ChainSwapType.HTLC;
50
+
51
+ activeSubscriptions: Set<string> = new Set<string>();
52
+
53
+ readonly config: FromBtcLnAutoConfig;
54
+ readonly lightning: ILightningWallet;
55
+ readonly LightningAssertions: LightningAssertions;
56
+
57
+ constructor(
58
+ storageDirectory: IIntermediaryStorage<FromBtcLnAutoSwap>,
59
+ path: string,
60
+ chains: MultichainData,
61
+ lightning: ILightningWallet,
62
+ swapPricing: ISwapPrice,
63
+ config: FromBtcLnAutoConfig
64
+ ) {
65
+ super(storageDirectory, path, chains, swapPricing, config);
66
+ this.config = config;
67
+ this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
68
+ this.lightning = lightning;
69
+ this.LightningAssertions = new LightningAssertions(this.logger, lightning);
70
+ }
71
+
72
+ protected async processPastSwap(swap: FromBtcLnAutoSwap): Promise<"REFUND" | "SETTLE" | null> {
73
+ const {swapContract, signer} = this.getChain(swap.chainIdentifier);
74
+ if(swap.state===FromBtcLnAutoSwapState.CREATED) {
75
+ //Check if already paid
76
+ const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
77
+ const invoice = await this.lightning.getInvoice(parsedPR.id);
78
+
79
+ const isBeingPaid = invoice.status==="held";
80
+ if(!isBeingPaid) {
81
+ //Not paid
82
+ const isInvoiceExpired = parsedPR.expiryEpochMillis<Date.now();
83
+ if(isInvoiceExpired) {
84
+ this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap LN invoice expired, cancelling, invoice: "+swap.pr);
85
+ await this.cancelSwapAndInvoice(swap);
86
+ return null;
87
+ }
88
+ this.subscribeToInvoice(swap);
89
+ return null;
90
+ }
91
+
92
+ //Adjust the state of the swap and expiry
93
+ try {
94
+ await this.htlcReceived(swap, invoice);
95
+ //Result is either FromBtcLnSwapState.RECEIVED or FromBtcLnSwapState.CANCELED
96
+ } catch (e) {
97
+ this.swapLogger.error(swap, "processPastSwap(state=CREATED): htlcReceived error", e);
98
+ }
99
+
100
+ return null;
101
+ }
102
+
103
+ if(swap.state===FromBtcLnAutoSwapState.RECEIVED) {
104
+ try {
105
+ if(!await this.offerHtlc(swap)) {
106
+ //Expired
107
+ if(swap.state===FromBtcLnAutoSwapState.RECEIVED) {
108
+ this.swapLogger.info(swap, "processPastSwap(state=RECEIVED): offer HTLC expired, cancelling invoice: "+swap.pr);
109
+ await this.cancelSwapAndInvoice(swap);
110
+ }
111
+ }
112
+ } catch (e) {
113
+ this.swapLogger.error(swap, "processPastSwap(state=RECEIVED): offerHtlc error", e);
114
+ }
115
+
116
+ return null;
117
+ }
118
+
119
+ if(swap.state===FromBtcLnAutoSwapState.TXS_SENT || swap.state===FromBtcLnAutoSwapState.COMMITED) {
120
+ const onchainStatus = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
121
+ const state: FromBtcLnAutoSwapState = swap.state as FromBtcLnAutoSwapState;
122
+ if(onchainStatus.type===SwapCommitStateType.PAID) {
123
+ //Extract the swap secret
124
+ if(state!==FromBtcLnAutoSwapState.CLAIMED && state!==FromBtcLnAutoSwapState.SETTLED) {
125
+ const secretHex = await onchainStatus.getClaimResult();
126
+ const secret: Buffer = Buffer.from(secretHex, "hex");
127
+ const paymentHash: Buffer = createHash("sha256").update(secret).digest();
128
+ const paymentHashHex = paymentHash.toString("hex");
129
+
130
+ if (swap.lnPaymentHash!==paymentHashHex) {
131
+ //TODO: Possibly fatal failure
132
+ this.swapLogger.error(swap, "processPastSwap(state=TXS_SENT|COMMITED): onchainStatus=PAID, Invalid swap secret specified: "+secretHex+" for paymentHash: "+paymentHashHex);
133
+ return null;
134
+ }
135
+
136
+ swap.secret = secretHex;
137
+ await swap.setState(FromBtcLnAutoSwapState.CLAIMED);
138
+ await this.saveSwapData(swap);
139
+
140
+ this.swapLogger.warn(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap settled (detected from processPastSwap), invoice: "+swap.pr);
141
+
142
+ return "SETTLE";
143
+ }
144
+ return null;
145
+ }
146
+ if(onchainStatus.type===SwapCommitStateType.COMMITED) {
147
+ if(state===FromBtcLnAutoSwapState.TXS_SENT) {
148
+ await swap.setState(FromBtcLnAutoSwapState.COMMITED);
149
+ await this.saveSwapData(swap);
150
+
151
+ this.swapLogger.info(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap committed (detected from processPastSwap), invoice: "+swap.pr);
152
+ }
153
+ return null;
154
+ }
155
+ if(onchainStatus.type===SwapCommitStateType.NOT_COMMITED || onchainStatus.type===SwapCommitStateType.EXPIRED) {
156
+ if(swap.state===FromBtcLnAutoSwapState.TXS_SENT) {
157
+ const isAuthorizationExpired = await swapContract.isInitAuthorizationExpired(swap.data, swap);
158
+ if(isAuthorizationExpired) {
159
+ this.swapLogger.info(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap not committed before authorization expiry, cancelling the LN invoice, invoice: "+swap.pr);
160
+ await this.cancelSwapAndInvoice(swap);
161
+ return null;
162
+ }
163
+ } else {
164
+ if(await swapContract.isExpired(signer.getAddress(), swap.data)) {
165
+ this.swapLogger.info(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap timed out, refunding to self, invoice: "+swap.pr);
166
+ return "REFUND";
167
+ }
168
+ }
169
+ }
170
+ if(onchainStatus.type===SwapCommitStateType.REFUNDABLE) {
171
+ this.swapLogger.info(swap, "processPastSwap(state=TXS_SENT|COMMITED): swap timed out, refunding to self, invoice: "+swap.pr);
172
+ return "REFUND";
173
+ }
174
+ }
175
+
176
+ if(swap.state===FromBtcLnAutoSwapState.CLAIMED) return "SETTLE";
177
+ if(swap.state===FromBtcLnAutoSwapState.CANCELED) await this.cancelSwapAndInvoice(swap);
178
+ }
179
+
180
+ protected async refundSwaps(refundSwaps: FromBtcLnAutoSwap[]) {
181
+ for(let refundSwap of refundSwaps) {
182
+ const {swapContract, signer} = this.getChain(refundSwap.chainIdentifier);
183
+ const unlock = refundSwap.lock(swapContract.refundTimeout);
184
+ if(unlock==null) continue;
185
+
186
+ this.swapLogger.debug(refundSwap, "refundSwaps(): initiate refund of swap");
187
+ await swapContract.refund(signer, refundSwap.data, true, false, {waitForConfirmation: true});
188
+ this.swapLogger.info(refundSwap, "refundsSwaps(): swap refunded, invoice: "+refundSwap.pr);
189
+
190
+ await refundSwap.setState(FromBtcLnAutoSwapState.REFUNDED);
191
+ unlock();
192
+ }
193
+ }
194
+
195
+ protected async settleInvoices(swaps: FromBtcLnAutoSwap[]) {
196
+ for(let swap of swaps) {
197
+ try {
198
+ await this.lightning.settleHodlInvoice(swap.secret);
199
+ if(swap.metadata!=null) swap.metadata.times.htlcSettled = Date.now();
200
+ await this.removeSwapData(swap, FromBtcLnAutoSwapState.SETTLED);
201
+
202
+ this.swapLogger.info(swap, "settleInvoices(): invoice settled, secret: "+swap.secret);
203
+ } catch (e) {
204
+ this.swapLogger.error(swap, "settleInvoices(): cannot settle invoice", e);
205
+ }
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Checks past swaps, refunds and deletes ones that are already expired.
211
+ */
212
+ protected async processPastSwaps() {
213
+
214
+ const settleInvoices: FromBtcLnAutoSwap[] = [];
215
+ const refundSwaps: FromBtcLnAutoSwap[] = [];
216
+
217
+ const queriedData = await this.storageManager.query([
218
+ {
219
+ key: "state",
220
+ value: [
221
+ FromBtcLnAutoSwapState.CREATED,
222
+ FromBtcLnAutoSwapState.RECEIVED,
223
+ FromBtcLnAutoSwapState.TXS_SENT,
224
+ FromBtcLnAutoSwapState.COMMITED,
225
+ FromBtcLnAutoSwapState.CLAIMED,
226
+ FromBtcLnAutoSwapState.CANCELED,
227
+ ]
228
+ }
229
+ ]);
230
+
231
+ for(let {obj: swap} of queriedData) {
232
+ switch(await this.processPastSwap(swap)) {
233
+ case "SETTLE":
234
+ settleInvoices.push(swap);
235
+ break;
236
+ case "REFUND":
237
+ refundSwaps.push(swap);
238
+ break;
239
+ }
240
+ }
241
+
242
+ await this.refundSwaps(refundSwaps);
243
+ await this.settleInvoices(settleInvoices);
244
+ }
245
+
246
+ protected async processInitializeEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: InitializeEvent<SwapData>): Promise<void> {
247
+ this.swapLogger.info(savedSwap, "SC: InitializeEvent: HTLC initialized by the client, invoice: "+savedSwap.pr);
248
+
249
+ if(savedSwap.state===FromBtcLnAutoSwapState.TXS_SENT) {
250
+ await savedSwap.setState(FromBtcLnAutoSwapState.COMMITED);
251
+ await this.saveSwapData(savedSwap);
252
+ }
253
+ }
254
+
255
+ protected async processClaimEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: ClaimEvent<SwapData>): Promise<void> {
256
+ //Claim
257
+ //This is the important part, we need to catch the claim TX, else we may lose money
258
+ const secret: Buffer = Buffer.from(event.result, "hex");
259
+ const paymentHash: Buffer = createHash("sha256").update(secret).digest();
260
+ const secretHex = secret.toString("hex");
261
+ const paymentHashHex = paymentHash.toString("hex");
262
+
263
+ if (savedSwap.lnPaymentHash!==paymentHashHex) return;
264
+
265
+ this.swapLogger.info(savedSwap, "SC: ClaimEvent: swap HTLC successfully claimed by the client, invoice: "+savedSwap.pr);
266
+
267
+ try {
268
+ await this.lightning.settleHodlInvoice(secretHex);
269
+ this.swapLogger.info(savedSwap, "SC: ClaimEvent: invoice settled, secret: "+secretHex);
270
+ savedSwap.secret = secretHex;
271
+ if(savedSwap.metadata!=null) savedSwap.metadata.times.htlcSettled = Date.now();
272
+ await this.removeSwapData(savedSwap, FromBtcLnAutoSwapState.SETTLED);
273
+ } catch (e) {
274
+ this.swapLogger.error(savedSwap, "SC: ClaimEvent: cannot settle invoice", e);
275
+ savedSwap.secret = secretHex;
276
+ await savedSwap.setState(FromBtcLnAutoSwapState.CLAIMED);
277
+ await this.saveSwapData(savedSwap);
278
+ }
279
+
280
+ }
281
+
282
+ protected async processRefundEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: RefundEvent<SwapData>): Promise<void> {
283
+ this.swapLogger.info(savedSwap, "SC: RefundEvent: swap refunded to us, invoice: "+savedSwap.pr);
284
+
285
+ //We don't cancel the incoming invoice, to make the offender pay for this with locked liquidity
286
+ // await this.lightning.cancelHodlInvoice(savedSwap.lnPaymentHash);
287
+ await this.removeSwapData(savedSwap, FromBtcLnAutoSwapState.REFUNDED)
288
+ }
289
+
290
+ /**
291
+ * Subscribe to a lightning network invoice
292
+ *
293
+ * @param swap
294
+ */
295
+ private subscribeToInvoice(swap: FromBtcLnAutoSwap): boolean {
296
+ const paymentHash = swap.lnPaymentHash;
297
+ if(this.activeSubscriptions.has(paymentHash)) return false;
298
+
299
+ this.lightning.waitForInvoice(paymentHash).then(result => {
300
+ this.swapLogger.info(swap, "subscribeToInvoice(): result callback, outcome: "+result.status+" invoice: "+swap.pr);
301
+ if(result.status==="held")
302
+ this.htlcReceived(swap, result).catch(e => this.swapLogger.error(swap, "subscribeToInvoice(): HTLC received result", e));
303
+ this.activeSubscriptions.delete(paymentHash);
304
+ });
305
+ this.swapLogger.info(swap, "subscribeToInvoice(): subscribe to invoice: "+swap.pr);
306
+
307
+ this.activeSubscriptions.add(paymentHash);
308
+ return true;
309
+ }
310
+
311
+ /**
312
+ * Called when lightning HTLC is received, also signs an init transaction on the smart chain side, expiry of the
313
+ * smart chain authorization starts ticking as soon as this HTLC is received
314
+ *
315
+ * @param invoiceData
316
+ * @param invoice
317
+ */
318
+ private async htlcReceived(invoiceData: FromBtcLnAutoSwap, invoice: LightningNetworkInvoice) {
319
+ if(invoiceData.state!==FromBtcLnAutoSwapState.CREATED) return;
320
+ this.swapLogger.debug(invoiceData, "htlcReceived(): invoice: ", invoice);
321
+ if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcReceived = Date.now();
322
+
323
+ const useToken = invoiceData.token;
324
+ const gasToken = invoiceData.gasToken;
325
+
326
+ let expiryTimeout: bigint;
327
+ try {
328
+ //Check if HTLC expiry is long enough
329
+ expiryTimeout = await this.checkHtlcExpiry(invoice);
330
+ if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcTimeoutCalculated = Date.now();
331
+ } catch (e) {
332
+ if(invoiceData.state===FromBtcLnAutoSwapState.CREATED) await this.cancelSwapAndInvoice(invoiceData);
333
+ throw e;
334
+ }
335
+
336
+ const {swapContract, signer} = this.getChain(invoiceData.chainIdentifier);
337
+
338
+ //Create real swap data
339
+ const swapData: SwapData = await swapContract.createSwapData(
340
+ ChainSwapType.HTLC,
341
+ signer.getAddress(),
342
+ invoiceData.claimer,
343
+ useToken,
344
+ invoiceData.amountToken,
345
+ invoiceData.claimHash,
346
+ 0n,
347
+ BigInt(Math.floor(Date.now() / 1000)) + expiryTimeout,
348
+ false,
349
+ true,
350
+ invoiceData.amountGasToken + invoiceData.claimerBounty,
351
+ invoiceData.claimerBounty,
352
+ invoiceData.gasToken
353
+ );
354
+ if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcSwapCreated = Date.now();
355
+
356
+ //Important to prevent race condition and issuing 2 signed init messages at the same time
357
+ if(invoiceData.state===FromBtcLnAutoSwapState.CREATED) {
358
+ invoiceData.data = swapData;
359
+ invoiceData.signature = null;
360
+ invoiceData.timeout = (BigInt(Math.floor(Date.now() / 1000)) + 120n).toString(10);
361
+
362
+ //Setting the state variable is done outside the promise, so is done synchronously
363
+ await invoiceData.setState(FromBtcLnAutoSwapState.RECEIVED);
364
+
365
+ await this.saveSwapData(invoiceData);
366
+
367
+ await this.offerHtlc(invoiceData);
368
+ }
369
+ }
370
+
371
+ private async offerHtlc(invoiceData: FromBtcLnAutoSwap) {
372
+ if(invoiceData.state!==FromBtcLnAutoSwapState.RECEIVED) return;
373
+
374
+ this.swapLogger.debug(invoiceData, "offerHtlc(): invoice: ", invoiceData.pr);
375
+ if(invoiceData.metadata!=null) invoiceData.metadata.times.offerHtlc = Date.now();
376
+
377
+ const useToken = invoiceData.token;
378
+ const gasToken = invoiceData.gasToken;
379
+
380
+ const {swapContract, signer, chainInterface} = this.getChain(invoiceData.chainIdentifier);
381
+
382
+ //Create abort controller for parallel fetches
383
+ const abortController = new AbortController();
384
+
385
+ //Pre-fetch data
386
+ const balancePrefetch: Promise<bigint> = this.getBalancePrefetch(invoiceData.chainIdentifier, useToken, abortController);
387
+ const gasTokenBalancePrefetch: Promise<bigint> = invoiceData.getTotalOutputGasAmount()===0n || useToken===gasToken ?
388
+ null : this.getBalancePrefetch(invoiceData.chainIdentifier, gasToken, abortController);
389
+
390
+ if(await swapContract.getInitAuthorizationExpiry(invoiceData.data, invoiceData) < Date.now()) {
391
+ if(invoiceData.state===FromBtcLnAutoSwapState.RECEIVED) {
392
+ await this.cancelSwapAndInvoice(invoiceData);
393
+ }
394
+ return false;
395
+ }
396
+
397
+ try {
398
+ //Check if we have enough liquidity to proceed
399
+ if(useToken===gasToken) {
400
+ await this.checkBalance(invoiceData.getTotalOutputAmount() + invoiceData.getTotalOutputGasAmount(), balancePrefetch, abortController.signal);
401
+ } else {
402
+ await this.checkBalance(invoiceData.getTotalOutputAmount(), balancePrefetch, abortController.signal);
403
+ await this.checkBalance(invoiceData.getTotalOutputGasAmount(), gasTokenBalancePrefetch, abortController.signal);
404
+ }
405
+ if(invoiceData.metadata!=null) invoiceData.metadata.times.offerHtlcChecked = Date.now();
406
+ } catch (e) {
407
+ if(!abortController.signal.aborted) {
408
+ if(invoiceData.state===FromBtcLnAutoSwapState.RECEIVED) await this.cancelSwapAndInvoice(invoiceData);
409
+ }
410
+ throw e;
411
+ }
412
+
413
+ const txWithdraw = await swapContract.txsWithdraw(signer.getAddress(), gasToken, invoiceData.data.getTotalDeposit());
414
+ const txInit = await swapContract.txsInit(signer.getAddress(), invoiceData.data, {
415
+ prefix: invoiceData.prefix,
416
+ timeout: invoiceData.timeout,
417
+ signature: invoiceData.signature
418
+ }, true);
419
+
420
+ if(invoiceData.state===FromBtcLnAutoSwapState.RECEIVED) {
421
+ //Setting the state variable is done outside the promise, so is done synchronously
422
+ await invoiceData.setState(FromBtcLnAutoSwapState.TXS_SENT);
423
+ await this.saveSwapData(invoiceData);
424
+ await chainInterface.sendAndConfirm(signer, [...txWithdraw, ...txInit], true, undefined, true);
425
+ }
426
+
427
+ return true;
428
+ }
429
+
430
+ /**
431
+ * Checks invoice description hash
432
+ *
433
+ * @param descriptionHash
434
+ * @throws {DefinedRuntimeError} will throw an error if the description hash is invalid
435
+ */
436
+ private checkDescriptionHash(descriptionHash: string) {
437
+ if(descriptionHash!=null) {
438
+ if(typeof(descriptionHash)!=="string" || !HEX_REGEX.test(descriptionHash) || descriptionHash.length!==64) {
439
+ throw {
440
+ code: 20100,
441
+ msg: "Invalid request body (descriptionHash)"
442
+ };
443
+ }
444
+ }
445
+ }
446
+
447
+ /**
448
+ * Asynchronously sends the LN node's public key to the client, so he can pre-fetch the node's channels from 1ml api
449
+ *
450
+ * @param responseStream
451
+ */
452
+ private sendPublicKeyAsync(responseStream: ServerParamEncoder) {
453
+ this.lightning.getIdentityPublicKey().then(publicKey => responseStream.writeParams({
454
+ lnPublicKey: publicKey
455
+ })).catch(e => {
456
+ this.logger.error("sendPublicKeyAsync(): error", e);
457
+ });
458
+ }
459
+
460
+ /**
461
+ * Returns the CLTV timeout (blockheight) of the received HTLC corresponding to the invoice. If multiple HTLCs are
462
+ * received (MPP) it returns the lowest of the timeouts
463
+ *
464
+ * @param invoice
465
+ */
466
+ private getInvoicePaymentsTimeout(invoice: LightningNetworkInvoice): number | null {
467
+ let timeout: number = null;
468
+ invoice.payments.forEach((curr) => {
469
+ if (timeout == null || timeout > curr.timeout) timeout = curr.timeout;
470
+ });
471
+ return timeout;
472
+ }
473
+
474
+ /**
475
+ * Checks if the received HTLC's CLTV timeout is large enough to still process the swap
476
+ *
477
+ * @param invoice
478
+ * @throws {DefinedRuntimeError} Will throw if HTLC expires too soon and therefore cannot be processed
479
+ * @returns expiry timeout in seconds
480
+ */
481
+ private async checkHtlcExpiry(invoice: LightningNetworkInvoice): Promise<bigint> {
482
+ const timeout: number = this.getInvoicePaymentsTimeout(invoice);
483
+ const current_block_height = await this.lightning.getBlockheight();
484
+
485
+ const blockDelta = BigInt(timeout - current_block_height);
486
+
487
+ const htlcExpiresTooSoon = blockDelta < this.config.minCltv;
488
+ if(htlcExpiresTooSoon) {
489
+ throw {
490
+ code: 20002,
491
+ msg: "Not enough time to reliably process the swap",
492
+ data: {
493
+ requiredDelta: this.config.minCltv.toString(10),
494
+ actualDelta: blockDelta.toString(10)
495
+ }
496
+ };
497
+ }
498
+
499
+ return (this.config.minCltv * this.config.bitcoinBlocktime / this.config.safetyFactor) - this.config.gracePeriod;
500
+ }
501
+
502
+ /**
503
+ * Cancels the swap (CANCELED state) & also cancels the LN invoice (including all pending HTLCs)
504
+ *
505
+ * @param invoiceData
506
+ */
507
+ private async cancelSwapAndInvoice(invoiceData: FromBtcLnAutoSwap): Promise<void> {
508
+ await invoiceData.setState(FromBtcLnAutoSwapState.CANCELED);
509
+ await this.lightning.cancelHodlInvoice(invoiceData.lnPaymentHash);
510
+ await this.removeSwapData(invoiceData);
511
+ this.swapLogger.info(invoiceData, "cancelSwapAndInvoice(): swap removed & invoice cancelled, invoice: ", invoiceData.pr);
512
+ };
513
+
514
+ /**
515
+ *
516
+ * Checks if the lightning invoice is in HELD state (htlcs received but yet unclaimed)
517
+ *
518
+ * @param paymentHash
519
+ * @throws {DefinedRuntimeError} Will throw if the lightning invoice is not found, or if it isn't in the HELD state
520
+ * @returns the fetched lightning invoice
521
+ */
522
+ private async checkInvoiceStatus(paymentHash: string): Promise<any> {
523
+ const invoice = await this.lightning.getInvoice(paymentHash);
524
+ if(invoice==null) throw {
525
+ _httpStatus: 200,
526
+ code: 10001,
527
+ msg: "Invoice expired/canceled"
528
+ };
529
+
530
+ const arr = invoice.description.split("-");
531
+ let chainIdentifier: string;
532
+ let address: string;
533
+ if(arr.length>1) {
534
+ chainIdentifier = arr[0];
535
+ address = arr[1];
536
+ } else {
537
+ chainIdentifier = this.chains.default;
538
+ address = invoice.description;
539
+ }
540
+ const {chainInterface} = this.getChain(chainIdentifier);
541
+ if(!chainInterface.isValidAddress(address)) throw {
542
+ _httpStatus: 200,
543
+ code: 10001,
544
+ msg: "Invoice expired/canceled"
545
+ };
546
+
547
+ switch(invoice.status) {
548
+ case "canceled":
549
+ throw {
550
+ _httpStatus: 200,
551
+ code: 10001,
552
+ msg: "Invoice expired/canceled"
553
+ }
554
+ case "confirmed":
555
+ throw {
556
+ _httpStatus: 200,
557
+ code: 10002,
558
+ msg: "Invoice already paid"
559
+ };
560
+ case "unpaid":
561
+ throw {
562
+ _httpStatus: 200,
563
+ code: 10003,
564
+ msg: "Invoice yet unpaid"
565
+ };
566
+ default:
567
+ return invoice;
568
+ }
569
+ }
570
+
571
+ startRestServer(restServer: Express) {
572
+
573
+ restServer.use(this.path+"/createInvoice", serverParamDecoder(10*1000));
574
+ restServer.post(this.path+"/createInvoice", expressHandlerWrapper(async (req: Request & {paramReader: IParamReader}, res: Response & {responseStream: ServerParamEncoder}) => {
575
+ const metadata: {
576
+ request: any,
577
+ invoiceRequest?: any,
578
+ invoiceResponse?: any,
579
+ times: {[key: string]: number}
580
+ } = {request: {}, times: {}};
581
+
582
+ const chainIdentifier = req.query.chain as string ?? this.chains.default;
583
+ const {swapContract, signer, chainInterface} = this.getChain(chainIdentifier);
584
+ if(!swapContract.supportsInitWithoutClaimer) throw {
585
+ code: 20299,
586
+ msg: "Not supported for "+chainIdentifier
587
+ };
588
+
589
+ metadata.times.requestReceived = Date.now();
590
+
591
+ /**
592
+ * address: string smart chain address of the recipient
593
+ * paymentHash: string payment hash of the to-be-created invoice
594
+ * amount: string amount (in sats) of the invoice
595
+ * token: string Desired token to swap
596
+ * exactOut: boolean Whether the swap should be an exact out instead of exact in swap
597
+ * descriptionHash: string Description hash of the invoice
598
+ * gasAmount: string Desired amount in gas token to also get
599
+ * gasToken: string
600
+ * claimerBounty: string Desired amount to be left out as a claimer bounty
601
+ */
602
+ const parsedBody: FromBtcLnAutoRequestType = await req.paramReader.getParams({
603
+ address: (val: string) => val!=null &&
604
+ typeof(val)==="string" &&
605
+ chainInterface.isValidAddress(val) ? val : null,
606
+ paymentHash: (val: string) => val!=null &&
607
+ typeof(val)==="string" &&
608
+ val.length===64 &&
609
+ HEX_REGEX.test(val) ? val: null,
610
+ amount: FieldTypeEnum.BigInt,
611
+ token: (val: string) => val!=null &&
612
+ typeof(val)==="string" &&
613
+ this.isTokenSupported(chainIdentifier, val) ? val : null,
614
+ descriptionHash: FieldTypeEnum.StringOptional,
615
+ exactOut: FieldTypeEnum.BooleanOptional,
616
+ gasToken: (val: string) => val!=null &&
617
+ typeof(val)==="string" &&
618
+ chainInterface.isValidToken(val) ? val : null,
619
+ gasAmount: FieldTypeEnum.BigInt,
620
+ claimerBounty: FieldTypeEnum.BigInt
621
+ });
622
+ if(parsedBody==null) throw {
623
+ code: 20100,
624
+ msg: "Invalid request body"
625
+ };
626
+
627
+ if(parsedBody.gasToken!==chainInterface.getNativeCurrencyAddress()) throw {
628
+ code: 20290,
629
+ msg: "Unsupported gas token"
630
+ };
631
+
632
+ if(parsedBody.gasAmount < 0) throw {
633
+ code: 20291,
634
+ msg: "Invalid gas amount, negative"
635
+ };
636
+ if(parsedBody.claimerBounty < 0) throw {
637
+ code: 20292,
638
+ msg: "Invalid claimer bounty, negative"
639
+ };
640
+ metadata.request = parsedBody;
641
+
642
+ const requestedAmount = {input: !parsedBody.exactOut, amount: parsedBody.amount, token: parsedBody.token};
643
+ const gasTokenAmount = {
644
+ input: false,
645
+ amount: parsedBody.gasAmount + parsedBody.claimerBounty,
646
+ token: parsedBody.gasToken
647
+ } as const;
648
+ const request = {
649
+ chainIdentifier,
650
+ raw: req,
651
+ parsed: parsedBody,
652
+ metadata
653
+ };
654
+ const useToken = parsedBody.token;
655
+ const gasToken = parsedBody.gasToken;
656
+
657
+ //Check request params
658
+ this.checkDescriptionHash(parsedBody.descriptionHash);
659
+ const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount, gasTokenAmount);
660
+ metadata.times.requestChecked = Date.now();
661
+
662
+ //Create abortController for parallel prefetches
663
+ const responseStream = res.responseStream;
664
+ const abortController = getAbortController(responseStream);
665
+
666
+ //Pre-fetch data
667
+ const {
668
+ pricePrefetchPromise,
669
+ gasTokenPricePrefetchPromise
670
+ } = this.getFromBtcPricePrefetches(chainIdentifier, useToken, gasToken, abortController);
671
+ const balancePrefetch: Promise<bigint> = this.getBalancePrefetch(chainIdentifier, useToken, abortController);
672
+ const gasTokenBalancePrefetch: Promise<bigint> = gasTokenAmount.amount===0n || useToken===gasToken ?
673
+ null : this.getBalancePrefetch(chainIdentifier, gasToken, abortController);
674
+ const channelsPrefetch: Promise<LightningNetworkChannel[]> = this.LightningAssertions.getChannelsPrefetch(abortController);
675
+
676
+ //Asynchronously send the node's public key to the client
677
+ this.sendPublicKeyAsync(responseStream);
678
+
679
+ //Check valid amount specified (min/max)
680
+ let {
681
+ amountBD,
682
+ swapFee,
683
+ swapFeeInToken,
684
+ totalInToken,
685
+ amountBDgas,
686
+ gasSwapFee,
687
+ gasSwapFeeInToken,
688
+ totalInGasToken
689
+ } = await this.AmountAssertions.checkFromBtcAmount(
690
+ this.type, request,
691
+ {...requestedAmount, pricePrefetch: pricePrefetchPromise},
692
+ fees, abortController.signal,
693
+ {...gasTokenAmount, pricePrefetch: gasTokenPricePrefetchPromise}
694
+ );
695
+ metadata.times.priceCalculated = Date.now();
696
+
697
+ const totalBtcInput = amountBD + amountBDgas;
698
+
699
+ //Check if we have enough funds to honor the request
700
+ if(useToken===gasToken) {
701
+ await this.checkBalance(totalInToken + totalInGasToken, balancePrefetch, abortController.signal);
702
+ } else {
703
+ await this.checkBalance(totalInToken, balancePrefetch, abortController.signal);
704
+ await this.checkBalance(totalInGasToken, gasTokenBalancePrefetch, abortController.signal);
705
+ }
706
+ await this.LightningAssertions.checkInboundLiquidity(totalBtcInput, channelsPrefetch, abortController.signal);
707
+ metadata.times.balanceChecked = Date.now();
708
+
709
+ //Create swap
710
+ const hodlInvoiceObj: HodlInvoiceInit = {
711
+ description: chainIdentifier+"-"+parsedBody.address,
712
+ cltvDelta: Number(this.config.minCltv) + 5,
713
+ expiresAt: Date.now()+(this.config.invoiceTimeoutSeconds*1000),
714
+ id: parsedBody.paymentHash,
715
+ mtokens: totalBtcInput * 1000n,
716
+ descriptionHash: parsedBody.descriptionHash
717
+ };
718
+ metadata.invoiceRequest = hodlInvoiceObj;
719
+
720
+ const hodlInvoice = await this.lightning.createHodlInvoice(hodlInvoiceObj);
721
+ abortController.signal.throwIfAborted();
722
+ metadata.times.invoiceCreated = Date.now();
723
+ metadata.invoiceResponse = {...hodlInvoice};
724
+
725
+ totalInGasToken -= parsedBody.claimerBounty;
726
+
727
+ const createdSwap = new FromBtcLnAutoSwap(
728
+ chainIdentifier,
729
+ hodlInvoice.request,
730
+ parsedBody.paymentHash,
731
+ swapContract.getHashForHtlc(Buffer.from(parsedBody.paymentHash, "hex")).toString("hex"),
732
+ hodlInvoice.mtokens,
733
+ parsedBody.address,
734
+ useToken,
735
+ gasToken,
736
+ totalInToken,
737
+ totalInGasToken,
738
+ swapFee,
739
+ swapFeeInToken,
740
+ gasSwapFee,
741
+ gasSwapFeeInToken,
742
+ parsedBody.claimerBounty
743
+ );
744
+ metadata.times.swapCreated = Date.now();
745
+
746
+ createdSwap.metadata = metadata;
747
+
748
+ await PluginManager.swapCreate(createdSwap);
749
+ await this.saveSwapData(createdSwap);
750
+
751
+ this.swapLogger.info(createdSwap, "REST: /createInvoice: Created swap invoice: "+hodlInvoice.request+" amount: "+totalBtcInput.toString(10));
752
+ this.subscribeToInvoice(createdSwap);
753
+
754
+ await responseStream.writeParamsAndEnd({
755
+ code: 20000,
756
+ msg: "Success",
757
+ data: {
758
+ intermediaryKey: signer.getAddress(),
759
+ pr: hodlInvoice.request,
760
+
761
+ btcAmountSwap: amountBD.toString(10),
762
+ btcAmountGas: amountBDgas.toString(10),
763
+
764
+ total: totalInToken.toString(10),
765
+ totalGas: totalInGasToken.toString(10),
766
+
767
+ totalFeeBtc: (swapFee + gasSwapFee).toString(10),
768
+
769
+ swapFeeBtc: swapFee.toString(10),
770
+ swapFee: swapFeeInToken.toString(10),
771
+
772
+ gasSwapFeeBtc: gasSwapFee.toString(10),
773
+ gasSwapFee: gasSwapFeeInToken.toString(10),
774
+
775
+ claimerBounty: parsedBody.claimerBounty.toString(10)
776
+ }
777
+ });
778
+
779
+ }));
780
+
781
+ const getInvoiceStatus = expressHandlerWrapper(async (req, res) => {
782
+ /**
783
+ * paymentHash: string payment hash of the invoice
784
+ */
785
+ const parsedBody = verifySchema({...req.body, ...req.query}, {
786
+ paymentHash: (val: string) => val!=null &&
787
+ typeof(val)==="string" &&
788
+ val.length===64 &&
789
+ HEX_REGEX.test(val) ? val: null,
790
+ });
791
+
792
+ await this.checkInvoiceStatus(parsedBody.paymentHash);
793
+
794
+ const swap: FromBtcLnAutoSwap = await this.storageManager.getData(parsedBody.paymentHash, null);
795
+ if (swap==null) throw {
796
+ _httpStatus: 200,
797
+ code: 10001,
798
+ msg: "Invoice expired/canceled"
799
+ };
800
+
801
+ if (
802
+ swap.state === FromBtcLnAutoSwapState.RECEIVED ||
803
+ swap.state === FromBtcLnAutoSwapState.TXS_SENT ||
804
+ swap.state === FromBtcLnAutoSwapState.COMMITED
805
+ ) {
806
+ res.status(200).json({
807
+ code: 10000,
808
+ msg: "Success",
809
+ data: {
810
+ data: swap.data.serialize()
811
+ }
812
+ });
813
+ } else {
814
+ res.status(200).json({
815
+ code: 10003,
816
+ msg: "Invoice yet unpaid"
817
+ });
818
+ }
819
+
820
+ });
821
+
822
+ restServer.post(this.path+"/getInvoiceStatus", getInvoiceStatus);
823
+ restServer.get(this.path+"/getInvoiceStatus", getInvoiceStatus);
824
+
825
+ this.logger.info("started at path: ", this.path);
826
+ }
827
+
828
+ async init() {
829
+ await this.loadData(FromBtcLnAutoSwap);
830
+ this.subscribeToEvents();
831
+ await PluginManager.serviceInitialize(this);
832
+ }
833
+
834
+ getInfoData(): any {
835
+ const mappedDict = {};
836
+ for(let chainId in this.config.gasTokenMax) {
837
+ mappedDict[chainId] = {
838
+ gasToken: this.getChain(chainId).chainInterface.getNativeCurrencyAddress(),
839
+ max: this.config.gasTokenMax[chainId].toString(10)
840
+ };
841
+ }
842
+ return {
843
+ minCltv: Number(this.config.minCltv),
844
+ invoiceTimeoutSeconds: this.config.invoiceTimeoutSeconds,
845
+ gasTokens: mappedDict
846
+ };
847
+ }
848
+
849
+ }
850
+