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

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 +180 -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 -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 +104 -104
  59. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +659 -659
  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 -491
  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 +246 -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 -856
  138. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -141
  139. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +822 -822
  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 -627
  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,822 +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, 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
-
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
+