@atomiqlabs/lp-lib 14.0.0-dev.11 → 14.0.0-dev.13

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