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