@atomiqlabs/lp-lib 16.2.0 → 16.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/LICENSE +201 -201
  2. package/dist/fees/IBtcFeeEstimator.d.ts +3 -3
  3. package/dist/fees/IBtcFeeEstimator.js +2 -2
  4. package/dist/index.d.ts +42 -42
  5. package/dist/index.js +58 -58
  6. package/dist/info/InfoHandler.d.ts +17 -17
  7. package/dist/info/InfoHandler.js +60 -60
  8. package/dist/plugins/IPlugin.d.ts +144 -144
  9. package/dist/plugins/IPlugin.js +34 -34
  10. package/dist/plugins/PluginManager.d.ts +113 -113
  11. package/dist/plugins/PluginManager.js +274 -274
  12. package/dist/prices/BinanceSwapPrice.d.ts +29 -29
  13. package/dist/prices/BinanceSwapPrice.js +79 -79
  14. package/dist/prices/CoinGeckoSwapPrice.d.ts +33 -33
  15. package/dist/prices/CoinGeckoSwapPrice.js +51 -51
  16. package/dist/prices/ISwapPrice.d.ts +43 -43
  17. package/dist/prices/ISwapPrice.js +55 -55
  18. package/dist/prices/OKXSwapPrice.d.ts +29 -29
  19. package/dist/prices/OKXSwapPrice.js +79 -79
  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 +171 -171
  27. package/dist/swaps/SwapHandler.js +217 -217
  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 +74 -74
  32. package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -76
  33. package/dist/swaps/assertions/FromBtcAmountAssertions.js +185 -185
  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 +50 -50
  39. package/dist/swaps/escrow/EscrowHandler.js +151 -151
  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 +84 -84
  51. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +322 -322
  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 +108 -108
  55. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +695 -695
  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 +112 -112
  59. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +708 -708
  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 +170 -170
  63. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +745 -745
  64. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +28 -28
  65. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +64 -64
  66. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +178 -178
  67. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +900 -899
  68. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +24 -24
  69. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +58 -58
  70. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +44 -44
  71. package/dist/swaps/spv_vault_swap/SpvVault.js +145 -145
  72. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +68 -68
  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 +561 -561
  76. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +63 -63
  77. package/dist/swaps/spv_vault_swap/SpvVaults.js +491 -491
  78. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +52 -52
  79. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +662 -662
  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 +77 -77
  83. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +504 -504
  84. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
  85. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
  86. package/dist/utils/BitcoinUtils.d.ts +4 -4
  87. package/dist/utils/BitcoinUtils.js +61 -61
  88. package/dist/utils/Utils.d.ts +32 -32
  89. package/dist/utils/Utils.js +129 -129
  90. package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
  91. package/dist/utils/paramcoders/IParamReader.js +2 -2
  92. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
  93. package/dist/utils/paramcoders/IParamWriter.js +2 -2
  94. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
  95. package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
  96. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
  97. package/dist/utils/paramcoders/ParamDecoder.js +222 -222
  98. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
  99. package/dist/utils/paramcoders/ParamEncoder.js +22 -22
  100. package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
  101. package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
  102. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
  103. package/dist/utils/paramcoders/server/ServerParamDecoder.js +107 -107
  104. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
  105. package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
  106. package/dist/wallets/IBitcoinWallet.d.ts +149 -149
  107. package/dist/wallets/IBitcoinWallet.js +97 -97
  108. package/dist/wallets/ILightningWallet.d.ts +136 -136
  109. package/dist/wallets/ILightningWallet.js +37 -37
  110. package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
  111. package/dist/wallets/ISpvVaultSigner.js +2 -2
  112. package/package.json +36 -36
  113. package/src/fees/IBtcFeeEstimator.ts +6 -6
  114. package/src/index.ts +53 -53
  115. package/src/info/InfoHandler.ts +103 -103
  116. package/src/plugins/IPlugin.ts +174 -174
  117. package/src/plugins/PluginManager.ts +354 -354
  118. package/src/prices/BinanceSwapPrice.ts +101 -101
  119. package/src/prices/CoinGeckoSwapPrice.ts +75 -75
  120. package/src/prices/ISwapPrice.ts +88 -88
  121. package/src/prices/OKXSwapPrice.ts +101 -101
  122. package/src/storage/IIntermediaryStorage.ts +19 -19
  123. package/src/storagemanager/IntermediaryStorageManager.ts +118 -118
  124. package/src/storagemanager/StorageManager.ts +78 -78
  125. package/src/swaps/SwapHandler.ts +323 -323
  126. package/src/swaps/SwapHandlerSwap.ts +141 -141
  127. package/src/swaps/assertions/AmountAssertions.ts +77 -77
  128. package/src/swaps/assertions/FromBtcAmountAssertions.ts +251 -251
  129. package/src/swaps/assertions/LightningAssertions.ts +103 -103
  130. package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
  131. package/src/swaps/escrow/EscrowHandler.ts +172 -172
  132. package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
  133. package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
  134. package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +286 -286
  135. package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
  136. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
  137. package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +457 -457
  138. package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
  139. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +873 -873
  140. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -141
  141. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +866 -866
  142. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -196
  143. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +920 -920
  144. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +108 -108
  145. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1150 -1149
  146. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
  147. package/src/swaps/spv_vault_swap/SpvVault.ts +178 -178
  148. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +228 -228
  149. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +718 -718
  150. package/src/swaps/spv_vault_swap/SpvVaults.ts +567 -567
  151. package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +762 -762
  152. package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
  153. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +603 -603
  154. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  155. package/src/utils/BitcoinUtils.ts +59 -59
  156. package/src/utils/Utils.ts +150 -150
  157. package/src/utils/paramcoders/IParamReader.ts +7 -7
  158. package/src/utils/paramcoders/IParamWriter.ts +8 -8
  159. package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
  160. package/src/utils/paramcoders/ParamDecoder.ts +218 -218
  161. package/src/utils/paramcoders/ParamEncoder.ts +29 -29
  162. package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
  163. package/src/utils/paramcoders/server/ServerParamDecoder.ts +118 -118
  164. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  165. package/src/wallets/IBitcoinWallet.ts +237 -237
  166. package/src/wallets/ILightningWallet.ts +200 -200
  167. package/src/wallets/ISpvVaultSigner.ts +10 -10
@@ -1,603 +1,603 @@
1
- import {Express, Request, Response} from "express";
2
- import {createHash, randomBytes} from "crypto";
3
- import {FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState} from "./FromBtcLnTrustedSwap";
4
- import {ISwapPrice} from "../../../prices/ISwapPrice";
5
- import {MultichainData, SwapBaseConfig, SwapHandler, SwapHandlerType} from "../../SwapHandler";
6
- import {IIntermediaryStorage} from "../../../storage/IIntermediaryStorage";
7
- import {expressHandlerWrapper, getAbortController, HEX_REGEX} from "../../../utils/Utils";
8
- import {IParamReader} from "../../../utils/paramcoders/IParamReader";
9
- import {ServerParamEncoder} from "../../../utils/paramcoders/server/ServerParamEncoder";
10
- import {FieldTypeEnum, verifySchema} from "../../../utils/paramcoders/SchemaVerifier";
11
- import {PluginManager} from "../../../plugins/PluginManager";
12
- import {
13
- HodlInvoiceInit,
14
- ILightningWallet,
15
- LightningNetworkChannel,
16
- LightningNetworkInvoice
17
- } from "../../../wallets/ILightningWallet";
18
- import {FromBtcAmountAssertions} from "../../assertions/FromBtcAmountAssertions";
19
- import { LightningAssertions } from "../../assertions/LightningAssertions";
20
-
21
- export type SwapForGasServerConfig = SwapBaseConfig & {
22
- minCltv: bigint,
23
-
24
- invoiceTimeoutSeconds?: number
25
- }
26
-
27
- export type FromBtcLnTrustedRequestType = {
28
- address: string,
29
- amount: bigint,
30
- exactIn?: boolean,
31
- token?: string
32
- };
33
-
34
- /**
35
- * Swap handler handling from BTCLN swaps using submarine swaps
36
- */
37
- export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState> {
38
- readonly type = SwapHandlerType.FROM_BTCLN_TRUSTED;
39
- readonly inflightSwapStates = new Set([FromBtcLnTrustedSwapState.RECEIVED, FromBtcLnTrustedSwapState.SENT, FromBtcLnTrustedSwapState.CONFIRMED]);
40
-
41
- activeSubscriptions: Map<string, AbortController> = new Map<string, AbortController>();
42
- processedTxIds: Map<string, string> = new Map<string, string>();
43
-
44
- readonly config: SwapForGasServerConfig;
45
- readonly lightning: ILightningWallet;
46
- readonly LightningAssertions: LightningAssertions;
47
- readonly AmountAssertions: FromBtcAmountAssertions;
48
-
49
- constructor(
50
- storageDirectory: IIntermediaryStorage<FromBtcLnTrustedSwap>,
51
- path: string,
52
- chains: MultichainData,
53
- lightning: ILightningWallet,
54
- swapPricing: ISwapPrice,
55
- config: SwapForGasServerConfig
56
- ) {
57
- super(storageDirectory, path, chains, swapPricing);
58
- this.lightning = lightning;
59
- this.LightningAssertions = new LightningAssertions(this.logger, lightning);
60
- this.AmountAssertions = new FromBtcAmountAssertions(config, swapPricing);
61
- this.config = config;
62
- this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
63
- }
64
-
65
- /**
66
- * Unsubscribe from the pending lightning network invoice
67
- *
68
- * @param paymentHash
69
- * @private
70
- */
71
- private unsubscribeInvoice(paymentHash: string): boolean {
72
- const controller = this.activeSubscriptions.get(paymentHash);
73
- if(controller==null) return false;
74
- controller.abort("Unsubscribed");
75
- this.activeSubscriptions.delete(paymentHash);
76
- return true;
77
- }
78
-
79
- /**
80
- * Subscribe to a pending lightning network invoice
81
- *
82
- * @param invoiceData
83
- */
84
- private subscribeToInvoice(invoiceData: FromBtcLnTrustedSwap) {
85
- const hash = invoiceData.getIdentifierHash();
86
-
87
- //Already subscribed
88
- if(this.activeSubscriptions.has(hash)) return;
89
-
90
- const abortController = new AbortController();
91
- this.lightning.waitForInvoice(hash, abortController.signal).then(invoice => {
92
- this.swapLogger.debug(invoiceData, "subscribeToInvoice(): invoice_updated: ", invoice);
93
- if(invoice.status!=="held") return;
94
- this.htlcReceived(invoiceData, invoice).catch(e => this.swapLogger.error(invoiceData, "subscribeToInvoice(): Error calling htlcReceived(): ", e));
95
- this.activeSubscriptions.delete(hash);
96
- });
97
-
98
- this.swapLogger.debug(invoiceData, "subscribeToInvoice(): Subscribed to invoice payment");
99
- this.activeSubscriptions.set(hash, abortController);
100
- }
101
-
102
- /**
103
- *
104
- * @param swap
105
- * @protected
106
- * @returns {Promise<boolean>} Whether the invoice should be cancelled
107
- */
108
- protected async processPastSwap(swap: FromBtcLnTrustedSwap): Promise<boolean> {
109
- if(swap.state===FromBtcLnTrustedSwapState.CANCELED) return true;
110
- if(swap.state===FromBtcLnTrustedSwapState.REFUNDED) return true;
111
-
112
- const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
113
- const invoice = await this.lightning.getInvoice(parsedPR.id);
114
-
115
- switch (invoice.status) {
116
- case "held":
117
- try {
118
- await this.htlcReceived(swap, invoice);
119
- //Result is either FromBtcLnTrustedSwapState.RECEIVED or FromBtcLnTrustedSwapState.CANCELED
120
- } catch (e) {
121
- this.swapLogger.error(swap, "processPastSwap(): Error calling htlcReceived(): ", e);
122
- }
123
- return false;
124
- case "confirmed":
125
- return false;
126
- default:
127
- const isInvoiceExpired = parsedPR.expiryEpochMillis<Date.now();
128
- if(isInvoiceExpired) {
129
- await swap.setState(FromBtcLnTrustedSwapState.CANCELED);
130
- return true;
131
- }
132
- this.subscribeToInvoice(swap);
133
- return false;
134
- }
135
- }
136
-
137
- protected async cancelInvoices(swaps: FromBtcLnTrustedSwap[]) {
138
- for(let swap of swaps) {
139
- //Cancel invoices
140
- try {
141
- const paymentHash = swap.getIdentifierHash();
142
- await this.lightning.cancelHodlInvoice(paymentHash);
143
- this.unsubscribeInvoice(paymentHash);
144
- this.swapLogger.info(swap, "cancelInvoices(): invoice cancelled!");
145
- await this.removeSwapData(swap);
146
- } catch (e) {
147
- this.swapLogger.error(swap, "cancelInvoices(): cannot cancel hodl invoice id", e);
148
- }
149
- }
150
- }
151
-
152
- /**
153
- * Checks past swaps, refunds and deletes ones that are already expired.
154
- */
155
- protected async processPastSwaps(): Promise<void> {
156
- const cancelInvoices: FromBtcLnTrustedSwap[] = [];
157
-
158
- const queriedData = await this.storageManager.query([
159
- {
160
- key: "state",
161
- value: [
162
- FromBtcLnTrustedSwapState.CREATED,
163
- FromBtcLnTrustedSwapState.RECEIVED,
164
- FromBtcLnTrustedSwapState.SENT,
165
- FromBtcLnTrustedSwapState.CONFIRMED,
166
- FromBtcLnTrustedSwapState.CANCELED,
167
- FromBtcLnTrustedSwapState.REFUNDED,
168
- ]
169
- }
170
- ]);
171
-
172
- for(let {obj: swap} of queriedData) {
173
- if(await this.processPastSwap(swap)) cancelInvoices.push(swap);
174
- }
175
-
176
- await this.cancelInvoices(cancelInvoices);
177
- }
178
-
179
- private async cancelSwapAndInvoice(swap: FromBtcLnTrustedSwap): Promise<void> {
180
- if(swap.state!==FromBtcLnTrustedSwapState.RECEIVED) return;
181
- await swap.setState(FromBtcLnTrustedSwapState.CANCELED);
182
- const paymentHash = swap.getIdentifierHash();
183
- await this.lightning.cancelHodlInvoice(paymentHash);
184
- this.unsubscribeInvoice(paymentHash);
185
- await this.removeSwapData(swap);
186
- this.swapLogger.info(swap, "cancelSwapAndInvoice(): swap removed & invoice cancelled, invoice: ", swap.pr);
187
- }
188
-
189
- /**
190
- * Saves the state of received HTLC of the lightning payment
191
- *
192
- * @param invoiceData
193
- * @param invoice
194
- */
195
- private async htlcReceived(invoiceData: FromBtcLnTrustedSwap, invoice: { id: string }) {
196
-
197
- const { signer, chainInterface} = this.getChain(invoiceData.chainIdentifier);
198
-
199
- //Important to prevent race condition and issuing 2 signed init messages at the same time
200
- if(invoiceData.state===FromBtcLnTrustedSwapState.CREATED) {
201
- try {
202
- this.checkTooManyInflightSwaps();
203
- } catch (e) {
204
- await this.cancelSwapAndInvoice(invoiceData);
205
- throw e;
206
- }
207
-
208
- if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcReceived = Date.now();
209
- await invoiceData.setState(FromBtcLnTrustedSwapState.RECEIVED);
210
- await this.storageManager.saveData(invoice.id, null, invoiceData);
211
- }
212
-
213
- if(invoiceData.state===FromBtcLnTrustedSwapState.RECEIVED) {
214
- const balance: Promise<bigint> = chainInterface.getBalance(signer.getAddress(), invoiceData.token);
215
- try {
216
- await this.checkBalance(invoiceData.output, balance, null);
217
- if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcBalanceChecked = Date.now();
218
- } catch (e) {
219
- await this.cancelSwapAndInvoice(invoiceData);
220
- throw e;
221
- }
222
-
223
- if(invoiceData.state!==FromBtcLnTrustedSwapState.RECEIVED) return;
224
-
225
- const txns = await chainInterface.txsTransfer(signer.getAddress(), invoiceData.token, invoiceData.output, invoiceData.dstAddress);
226
-
227
- let unlock = invoiceData.lock(Infinity);
228
- if(unlock==null) return;
229
-
230
- const result = await chainInterface.sendAndConfirm(signer, txns, true, null, false, async (txId: string, rawTx: string) => {
231
- invoiceData.txIds = {init: txId};
232
- invoiceData.scRawTx = rawTx;
233
- if(invoiceData.state===FromBtcLnTrustedSwapState.RECEIVED) {
234
- await invoiceData.setState(FromBtcLnTrustedSwapState.SENT);
235
- await this.storageManager.saveData(invoice.id, null, invoiceData);
236
- }
237
- }).catch(e => this.swapLogger.error(invoiceData, "htlcReceived(): Error sending transfer txns", e));
238
-
239
- if(result==null) {
240
- //Cancel invoice
241
- await invoiceData.setState(FromBtcLnTrustedSwapState.REFUNDED);
242
- await this.storageManager.saveData(invoice.id, null, invoiceData);
243
- await this.lightning.cancelHodlInvoice(invoice.id);
244
- this.unsubscribeInvoice(invoice.id);
245
- await this.removeSwapData(invoiceData);
246
- this.swapLogger.info(invoiceData, "htlcReceived(): transaction sending failed, refunding lightning: ", invoiceData.pr);
247
- throw {
248
- code: 20002,
249
- msg: "Transaction sending failed"
250
- };
251
- } else {
252
- //Successfully paid
253
- await invoiceData.setState(FromBtcLnTrustedSwapState.CONFIRMED);
254
- await this.storageManager.saveData(invoice.id, null, invoiceData);
255
- }
256
-
257
- unlock();
258
- unlock = null;
259
- }
260
-
261
- if(invoiceData.state===FromBtcLnTrustedSwapState.SENT) {
262
- if(invoiceData.isLocked()) return;
263
-
264
- const txStatus = await chainInterface.getTxStatus(invoiceData.scRawTx);
265
- if(txStatus==="not_found") {
266
- //Retry
267
- invoiceData.txIds = {init: null};
268
- invoiceData.scRawTx = null;
269
- await invoiceData.setState(FromBtcLnTrustedSwapState.RECEIVED);
270
- await this.storageManager.saveData(invoice.id, null, invoiceData);
271
- }
272
- if(txStatus==="reverted") {
273
- //Cancel invoice
274
- await invoiceData.setState(FromBtcLnTrustedSwapState.REFUNDED);
275
- await this.storageManager.saveData(invoice.id, null, invoiceData);
276
- await this.lightning.cancelHodlInvoice(invoice.id);
277
- this.unsubscribeInvoice(invoice.id);
278
- await this.removeSwapData(invoiceData);
279
- this.swapLogger.info(invoiceData, "htlcReceived(): transaction reverted, refunding lightning: ", invoiceData.pr);
280
- throw {
281
- code: 20002,
282
- msg: "Transaction reverted"
283
- };
284
- }
285
- if(txStatus==="success") {
286
- //Successfully paid
287
- await invoiceData.setState(FromBtcLnTrustedSwapState.CONFIRMED);
288
- await this.storageManager.saveData(invoice.id, null, invoiceData);
289
- }
290
- }
291
-
292
- if(invoiceData.state===FromBtcLnTrustedSwapState.CONFIRMED) {
293
- await this.lightning.settleHodlInvoice(invoiceData.secret);
294
-
295
- if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcSettled = Date.now();
296
-
297
- const paymentHash = invoiceData.getIdentifierHash();
298
- this.processedTxIds.set(paymentHash, invoiceData.txIds.init);
299
- await invoiceData.setState(FromBtcLnTrustedSwapState.SETTLED);
300
-
301
- this.unsubscribeInvoice(paymentHash);
302
- this.swapLogger.info(invoiceData, "htlcReceived(): invoice settled, invoice: "+invoiceData.pr+" scTxId: "+invoiceData.txIds.init);
303
- await this.removeSwapData(invoiceData);
304
- }
305
- }
306
-
307
- /**
308
- *
309
- * Checks if the lightning invoice is in HELD state (htlcs received but yet unclaimed)
310
- *
311
- * @param paymentHash
312
- * @throws {DefinedRuntimeError} Will throw if the lightning invoice is not found, or if it isn't in the HELD state
313
- * @returns the fetched lightning invoice
314
- */
315
- private async checkInvoiceStatus(paymentHash: string): Promise<LightningNetworkInvoice> {
316
- const invoice = await this.lightning.getInvoice(paymentHash);
317
-
318
- const isInvoiceFound = invoice!=null;
319
- if (!isInvoiceFound) throw {
320
- _httpStatus: 200,
321
- code: 10001,
322
- msg: "Invoice expired/canceled"
323
- }
324
-
325
- const arr = invoice.description.split("-");
326
- if(arr.length<3 || arr[1]!=="GAS") throw {
327
- _httpStatus: 200,
328
- code: 10001,
329
- msg: "Invoice expired/canceled"
330
- }
331
- const chainIdentifier = arr[0];
332
- const address = arr[2];
333
-
334
- const { chainInterface} = this.getChain(chainIdentifier);
335
- if(!chainInterface.isValidAddress(address, true)) throw {
336
- _httpStatus: 200,
337
- code: 10001,
338
- msg: "Invoice expired/canceled"
339
- };
340
-
341
- switch(invoice.status) {
342
- case "held":
343
- return invoice;
344
- case "canceled":
345
- throw {
346
- _httpStatus: 200,
347
- code: 10001,
348
- msg: "Invoice expired/canceled"
349
- };
350
- case "confirmed":
351
- throw {
352
- _httpStatus: 200,
353
- code: 10000,
354
- msg: "Invoice already paid",
355
- data: {
356
- txId: this.processedTxIds.get(paymentHash)
357
- }
358
- };
359
- case "unpaid":
360
- throw {
361
- _httpStatus: 200,
362
- code: 10010,
363
- msg: "Invoice yet unpaid"
364
- };
365
- default:
366
- throw new Error("Lightning invoice invalid state!");
367
- }
368
- }
369
-
370
- startRestServer(restServer: Express) {
371
-
372
- const createInvoice = expressHandlerWrapper(async (req: Request & {paramReader: IParamReader}, res: Response & {responseStream: ServerParamEncoder}) => {
373
- const metadata: {
374
- request: any,
375
- invoiceRequest?: any,
376
- invoiceResponse?: any,
377
- times: {[key: string]: number}
378
- } = {request: {}, times: {}};
379
-
380
- const chainIdentifier = req.query.chain as string;
381
- const {signer, chainInterface} = this.getChain(chainIdentifier);
382
-
383
- metadata.times.requestReceived = Date.now();
384
- /**
385
- * address: string solana address of the recipient
386
- * amount: string amount (in lamports/smart chain base units) of the invoice
387
- */
388
-
389
- req.query.token ??= chainInterface.getNativeCurrencyAddress();
390
-
391
- const parsedBody: FromBtcLnTrustedRequestType = verifySchema(req.query,{
392
- address: (val: string) => val!=null &&
393
- typeof(val)==="string" &&
394
- chainInterface.isValidAddress(val, true) ? val : null,
395
- token: (val: string) => val!=null &&
396
- typeof(val)==="string" &&
397
- this.isTokenSupported(chainIdentifier, val) ? val : null,
398
- amount: FieldTypeEnum.BigInt,
399
- exactIn: (val: string) => val==="true" ? true :
400
- (val==="false" || val===undefined) ? false : null
401
- });
402
- if(parsedBody==null) throw {
403
- code: 20100,
404
- msg: "Invalid request body"
405
- };
406
- metadata.request = parsedBody;
407
-
408
- const requestedAmount = {input: parsedBody.exactIn, amount: parsedBody.amount, token: parsedBody.token};
409
- const request = {
410
- chainIdentifier,
411
- raw: req,
412
- parsed: parsedBody,
413
- metadata
414
- };
415
- const useToken = parsedBody.token;
416
-
417
- this.checkTooManyInflightSwaps();
418
-
419
- //Check request params
420
- const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount);
421
- metadata.times.requestChecked = Date.now();
422
-
423
- //Create abortController for parallel prefetches
424
- const responseStream = res.responseStream;
425
- const abortController = getAbortController(responseStream);
426
-
427
- //Pre-fetch data
428
- const pricePrefetchPromise: Promise<bigint> = this.swapPricing.preFetchPrice(useToken, chainIdentifier).catch(e => {
429
- this.logger.error("pricePrefetchPromise(): pricePrefetch error: ", e);
430
- abortController.abort(e);
431
- return null;
432
- });
433
- const balancePrefetch: Promise<bigint> = chainInterface.getBalance(signer.getAddress(), useToken).catch(e => {
434
- this.logger.error("getBalancePrefetch(): balancePrefetch error: ", e);
435
- abortController.abort(e);
436
- return null;
437
- });
438
- const nativeBalancePrefetch: Promise<bigint> = useToken===chainInterface.getNativeCurrencyAddress() ?
439
- balancePrefetch : this.prefetchNativeBalanceIfNeeded(chainIdentifier, abortController);
440
- const channelsPrefetch: Promise<LightningNetworkChannel[]> = this.LightningAssertions.getChannelsPrefetch(abortController);
441
-
442
- await this.checkNativeBalance(chainIdentifier, nativeBalancePrefetch, abortController.signal);
443
-
444
- //Check valid amount specified (min/max)
445
- const {
446
- amountBD,
447
- swapFee,
448
- swapFeeInToken,
449
- totalInToken
450
- } = await this.AmountAssertions.checkFromBtcAmount(this.type, request, {...requestedAmount, pricePrefetch: pricePrefetchPromise}, fees, abortController.signal);
451
- metadata.times.priceCalculated = Date.now();
452
-
453
- //Check if we have enough funds to honor the request
454
- await this.checkBalance(totalInToken, balancePrefetch, abortController.signal)
455
- await this.LightningAssertions.checkInboundLiquidity(amountBD, channelsPrefetch, abortController.signal);
456
- metadata.times.balanceChecked = Date.now();
457
-
458
- const secret = randomBytes(32);
459
- const hash = createHash("sha256").update(secret).digest();
460
-
461
- const hodlInvoiceObj: HodlInvoiceInit = {
462
- description: chainIdentifier+"-GAS-"+parsedBody.address,
463
- cltvDelta: Number(this.config.minCltv) + 5,
464
- expiresAt: Date.now()+(this.config.invoiceTimeoutSeconds*1000),
465
- id: hash.toString("hex"),
466
- mtokens: amountBD * 1000n
467
- };
468
- metadata.invoiceRequest = hodlInvoiceObj;
469
-
470
- const hodlInvoice = await this.lightning.createHodlInvoice(hodlInvoiceObj);
471
- abortController.signal.throwIfAborted();
472
- metadata.times.invoiceCreated = Date.now();
473
- metadata.invoiceResponse = {...hodlInvoice};
474
-
475
- const createdSwap = new FromBtcLnTrustedSwap(
476
- chainIdentifier,
477
- hodlInvoice.request,
478
- hodlInvoice.mtokens,
479
- swapFee,
480
- swapFeeInToken,
481
- totalInToken,
482
- secret.toString("hex"),
483
- parsedBody.address,
484
- useToken
485
- );
486
- metadata.times.swapCreated = Date.now();
487
- createdSwap.metadata = metadata;
488
-
489
- await PluginManager.swapCreate(createdSwap);
490
- await this.storageManager.saveData(hash.toString("hex"), null, createdSwap);
491
- this.subscribeToInvoice(createdSwap);
492
-
493
- this.swapLogger.info(createdSwap, "REST: /createInvoice: Created swap invoice: "+hodlInvoice.request+" amount: "+amountBD.toString(10));
494
-
495
- res.status(200).json({
496
- msg: "Success",
497
- code: 10000,
498
- data: {
499
- pr: hodlInvoice.request,
500
- amountSats: amountBD.toString(10),
501
- swapFeeSats: swapFee.toString(10),
502
- swapFee: swapFeeInToken.toString(10),
503
- total: totalInToken.toString(10),
504
- intermediaryKey: signer.getAddress()
505
- }
506
- });
507
- });
508
-
509
- restServer.get(this.path+"/createInvoice", createInvoice);
510
-
511
- const getInvoiceStatus = expressHandlerWrapper(async (req, res) => {
512
- /**
513
- * paymentHash: string payment hash of the invoice
514
- */
515
- const parsedBody = verifySchema({...req.body, ...req.query}, {
516
- paymentHash: (val: string) => val!=null &&
517
- typeof(val)==="string" &&
518
- val.length===64 &&
519
- HEX_REGEX.test(val) ? val: null,
520
- });
521
-
522
- await this.checkInvoiceStatus(parsedBody.paymentHash);
523
-
524
- const invoiceData: FromBtcLnTrustedSwap = await this.storageManager.getData(parsedBody.paymentHash, null);
525
- if (invoiceData==null) throw {
526
- _httpStatus: 200,
527
- code: 10001,
528
- msg: "Invoice expired/canceled"
529
- };
530
-
531
- if (
532
- invoiceData.state === FromBtcLnTrustedSwapState.CANCELED ||
533
- invoiceData.state === FromBtcLnTrustedSwapState.REFUNDED
534
- ) throw {
535
- _httpStatus: 200,
536
- code: 10001,
537
- msg: "Invoice expired/canceled"
538
- };
539
-
540
- if (invoiceData.state === FromBtcLnTrustedSwapState.CREATED) throw {
541
- _httpStatus: 200,
542
- code: 10010,
543
- msg: "Invoice yet unpaid"
544
- };
545
-
546
- if (invoiceData.state === FromBtcLnTrustedSwapState.RECEIVED) throw {
547
- _httpStatus: 200,
548
- code: 10011,
549
- msg: "Invoice received, payment processing"
550
- };
551
-
552
- if (invoiceData.state === FromBtcLnTrustedSwapState.SENT) throw {
553
- _httpStatus: 200,
554
- code: 10012,
555
- msg: "Tx sent",
556
- data: {
557
- txId: invoiceData.txIds.init
558
- }
559
- };
560
-
561
- if (invoiceData.state === FromBtcLnTrustedSwapState.CONFIRMED) throw {
562
- _httpStatus: 200,
563
- code: 10000,
564
- msg: "Success, tx confirmed",
565
- data: {
566
- txId: invoiceData.txIds.init
567
- }
568
- };
569
-
570
- if (invoiceData.state === FromBtcLnTrustedSwapState.SETTLED) throw {
571
- _httpStatus: 200,
572
- code: 10000,
573
- msg: "Success, tx confirmed - invoice settled",
574
- data: {
575
- txId: invoiceData.txIds.init
576
- }
577
- };
578
- });
579
- restServer.get(this.path+"/getInvoiceStatus", getInvoiceStatus);
580
-
581
- this.logger.info("started at path: ", this.path);
582
- }
583
-
584
- async init() {
585
- await this.storageManager.loadData(FromBtcLnTrustedSwap);
586
- //Check if all swaps contain a valid amount
587
- for(let {obj: swap} of await this.storageManager.query([])) {
588
- if(swap.amount==null) {
589
- const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
590
- swap.amount = (parsedPR.mtokens + 999n) / 1000n;
591
- }
592
- }
593
- await PluginManager.serviceInitialize(this);
594
- }
595
-
596
- getInfoData(): any {
597
- return {
598
- minCltv: Number(this.config.minCltv)
599
- };
600
- }
601
-
602
- }
603
-
1
+ import {Express, Request, Response} from "express";
2
+ import {createHash, randomBytes} from "crypto";
3
+ import {FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState} from "./FromBtcLnTrustedSwap";
4
+ import {ISwapPrice} from "../../../prices/ISwapPrice";
5
+ import {MultichainData, SwapBaseConfig, SwapHandler, SwapHandlerType} from "../../SwapHandler";
6
+ import {IIntermediaryStorage} from "../../../storage/IIntermediaryStorage";
7
+ import {expressHandlerWrapper, getAbortController, HEX_REGEX} from "../../../utils/Utils";
8
+ import {IParamReader} from "../../../utils/paramcoders/IParamReader";
9
+ import {ServerParamEncoder} from "../../../utils/paramcoders/server/ServerParamEncoder";
10
+ import {FieldTypeEnum, verifySchema} from "../../../utils/paramcoders/SchemaVerifier";
11
+ import {PluginManager} from "../../../plugins/PluginManager";
12
+ import {
13
+ HodlInvoiceInit,
14
+ ILightningWallet,
15
+ LightningNetworkChannel,
16
+ LightningNetworkInvoice
17
+ } from "../../../wallets/ILightningWallet";
18
+ import {FromBtcAmountAssertions} from "../../assertions/FromBtcAmountAssertions";
19
+ import { LightningAssertions } from "../../assertions/LightningAssertions";
20
+
21
+ export type SwapForGasServerConfig = SwapBaseConfig & {
22
+ minCltv: bigint,
23
+
24
+ invoiceTimeoutSeconds?: number
25
+ }
26
+
27
+ export type FromBtcLnTrustedRequestType = {
28
+ address: string,
29
+ amount: bigint,
30
+ exactIn?: boolean,
31
+ token?: string
32
+ };
33
+
34
+ /**
35
+ * Swap handler handling from BTCLN swaps using submarine swaps
36
+ */
37
+ export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState> {
38
+ readonly type = SwapHandlerType.FROM_BTCLN_TRUSTED;
39
+ readonly inflightSwapStates = new Set([FromBtcLnTrustedSwapState.RECEIVED, FromBtcLnTrustedSwapState.SENT, FromBtcLnTrustedSwapState.CONFIRMED]);
40
+
41
+ activeSubscriptions: Map<string, AbortController> = new Map<string, AbortController>();
42
+ processedTxIds: Map<string, string> = new Map<string, string>();
43
+
44
+ readonly config: SwapForGasServerConfig;
45
+ readonly lightning: ILightningWallet;
46
+ readonly LightningAssertions: LightningAssertions;
47
+ readonly AmountAssertions: FromBtcAmountAssertions;
48
+
49
+ constructor(
50
+ storageDirectory: IIntermediaryStorage<FromBtcLnTrustedSwap>,
51
+ path: string,
52
+ chains: MultichainData,
53
+ lightning: ILightningWallet,
54
+ swapPricing: ISwapPrice,
55
+ config: SwapForGasServerConfig
56
+ ) {
57
+ super(storageDirectory, path, chains, swapPricing);
58
+ this.lightning = lightning;
59
+ this.LightningAssertions = new LightningAssertions(this.logger, lightning);
60
+ this.AmountAssertions = new FromBtcAmountAssertions(config, swapPricing);
61
+ this.config = config;
62
+ this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
63
+ }
64
+
65
+ /**
66
+ * Unsubscribe from the pending lightning network invoice
67
+ *
68
+ * @param paymentHash
69
+ * @private
70
+ */
71
+ private unsubscribeInvoice(paymentHash: string): boolean {
72
+ const controller = this.activeSubscriptions.get(paymentHash);
73
+ if(controller==null) return false;
74
+ controller.abort("Unsubscribed");
75
+ this.activeSubscriptions.delete(paymentHash);
76
+ return true;
77
+ }
78
+
79
+ /**
80
+ * Subscribe to a pending lightning network invoice
81
+ *
82
+ * @param invoiceData
83
+ */
84
+ private subscribeToInvoice(invoiceData: FromBtcLnTrustedSwap) {
85
+ const hash = invoiceData.getIdentifierHash();
86
+
87
+ //Already subscribed
88
+ if(this.activeSubscriptions.has(hash)) return;
89
+
90
+ const abortController = new AbortController();
91
+ this.lightning.waitForInvoice(hash, abortController.signal).then(invoice => {
92
+ this.swapLogger.debug(invoiceData, "subscribeToInvoice(): invoice_updated: ", invoice);
93
+ if(invoice.status!=="held") return;
94
+ this.htlcReceived(invoiceData, invoice).catch(e => this.swapLogger.error(invoiceData, "subscribeToInvoice(): Error calling htlcReceived(): ", e));
95
+ this.activeSubscriptions.delete(hash);
96
+ });
97
+
98
+ this.swapLogger.debug(invoiceData, "subscribeToInvoice(): Subscribed to invoice payment");
99
+ this.activeSubscriptions.set(hash, abortController);
100
+ }
101
+
102
+ /**
103
+ *
104
+ * @param swap
105
+ * @protected
106
+ * @returns {Promise<boolean>} Whether the invoice should be cancelled
107
+ */
108
+ protected async processPastSwap(swap: FromBtcLnTrustedSwap): Promise<boolean> {
109
+ if(swap.state===FromBtcLnTrustedSwapState.CANCELED) return true;
110
+ if(swap.state===FromBtcLnTrustedSwapState.REFUNDED) return true;
111
+
112
+ const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
113
+ const invoice = await this.lightning.getInvoice(parsedPR.id);
114
+
115
+ switch (invoice.status) {
116
+ case "held":
117
+ try {
118
+ await this.htlcReceived(swap, invoice);
119
+ //Result is either FromBtcLnTrustedSwapState.RECEIVED or FromBtcLnTrustedSwapState.CANCELED
120
+ } catch (e) {
121
+ this.swapLogger.error(swap, "processPastSwap(): Error calling htlcReceived(): ", e);
122
+ }
123
+ return false;
124
+ case "confirmed":
125
+ return false;
126
+ default:
127
+ const isInvoiceExpired = parsedPR.expiryEpochMillis<Date.now();
128
+ if(isInvoiceExpired) {
129
+ await swap.setState(FromBtcLnTrustedSwapState.CANCELED);
130
+ return true;
131
+ }
132
+ this.subscribeToInvoice(swap);
133
+ return false;
134
+ }
135
+ }
136
+
137
+ protected async cancelInvoices(swaps: FromBtcLnTrustedSwap[]) {
138
+ for(let swap of swaps) {
139
+ //Cancel invoices
140
+ try {
141
+ const paymentHash = swap.getIdentifierHash();
142
+ await this.lightning.cancelHodlInvoice(paymentHash);
143
+ this.unsubscribeInvoice(paymentHash);
144
+ this.swapLogger.info(swap, "cancelInvoices(): invoice cancelled!");
145
+ await this.removeSwapData(swap);
146
+ } catch (e) {
147
+ this.swapLogger.error(swap, "cancelInvoices(): cannot cancel hodl invoice id", e);
148
+ }
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Checks past swaps, refunds and deletes ones that are already expired.
154
+ */
155
+ protected async processPastSwaps(): Promise<void> {
156
+ const cancelInvoices: FromBtcLnTrustedSwap[] = [];
157
+
158
+ const queriedData = await this.storageManager.query([
159
+ {
160
+ key: "state",
161
+ value: [
162
+ FromBtcLnTrustedSwapState.CREATED,
163
+ FromBtcLnTrustedSwapState.RECEIVED,
164
+ FromBtcLnTrustedSwapState.SENT,
165
+ FromBtcLnTrustedSwapState.CONFIRMED,
166
+ FromBtcLnTrustedSwapState.CANCELED,
167
+ FromBtcLnTrustedSwapState.REFUNDED,
168
+ ]
169
+ }
170
+ ]);
171
+
172
+ for(let {obj: swap} of queriedData) {
173
+ if(await this.processPastSwap(swap)) cancelInvoices.push(swap);
174
+ }
175
+
176
+ await this.cancelInvoices(cancelInvoices);
177
+ }
178
+
179
+ private async cancelSwapAndInvoice(swap: FromBtcLnTrustedSwap): Promise<void> {
180
+ if(swap.state!==FromBtcLnTrustedSwapState.RECEIVED) return;
181
+ await swap.setState(FromBtcLnTrustedSwapState.CANCELED);
182
+ const paymentHash = swap.getIdentifierHash();
183
+ await this.lightning.cancelHodlInvoice(paymentHash);
184
+ this.unsubscribeInvoice(paymentHash);
185
+ await this.removeSwapData(swap);
186
+ this.swapLogger.info(swap, "cancelSwapAndInvoice(): swap removed & invoice cancelled, invoice: ", swap.pr);
187
+ }
188
+
189
+ /**
190
+ * Saves the state of received HTLC of the lightning payment
191
+ *
192
+ * @param invoiceData
193
+ * @param invoice
194
+ */
195
+ private async htlcReceived(invoiceData: FromBtcLnTrustedSwap, invoice: { id: string }) {
196
+
197
+ const { signer, chainInterface} = this.getChain(invoiceData.chainIdentifier);
198
+
199
+ //Important to prevent race condition and issuing 2 signed init messages at the same time
200
+ if(invoiceData.state===FromBtcLnTrustedSwapState.CREATED) {
201
+ try {
202
+ this.checkTooManyInflightSwaps();
203
+ } catch (e) {
204
+ await this.cancelSwapAndInvoice(invoiceData);
205
+ throw e;
206
+ }
207
+
208
+ if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcReceived = Date.now();
209
+ await invoiceData.setState(FromBtcLnTrustedSwapState.RECEIVED);
210
+ await this.storageManager.saveData(invoice.id, null, invoiceData);
211
+ }
212
+
213
+ if(invoiceData.state===FromBtcLnTrustedSwapState.RECEIVED) {
214
+ const balance: Promise<bigint> = chainInterface.getBalance(signer.getAddress(), invoiceData.token);
215
+ try {
216
+ await this.checkBalance(invoiceData.output, balance, null);
217
+ if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcBalanceChecked = Date.now();
218
+ } catch (e) {
219
+ await this.cancelSwapAndInvoice(invoiceData);
220
+ throw e;
221
+ }
222
+
223
+ if(invoiceData.state!==FromBtcLnTrustedSwapState.RECEIVED) return;
224
+
225
+ const txns = await chainInterface.txsTransfer(signer.getAddress(), invoiceData.token, invoiceData.output, invoiceData.dstAddress);
226
+
227
+ let unlock = invoiceData.lock(Infinity);
228
+ if(unlock==null) return;
229
+
230
+ const result = await chainInterface.sendAndConfirm(signer, txns, true, null, false, async (txId: string, rawTx: string) => {
231
+ invoiceData.txIds = {init: txId};
232
+ invoiceData.scRawTx = rawTx;
233
+ if(invoiceData.state===FromBtcLnTrustedSwapState.RECEIVED) {
234
+ await invoiceData.setState(FromBtcLnTrustedSwapState.SENT);
235
+ await this.storageManager.saveData(invoice.id, null, invoiceData);
236
+ }
237
+ }).catch(e => this.swapLogger.error(invoiceData, "htlcReceived(): Error sending transfer txns", e));
238
+
239
+ if(result==null) {
240
+ //Cancel invoice
241
+ await invoiceData.setState(FromBtcLnTrustedSwapState.REFUNDED);
242
+ await this.storageManager.saveData(invoice.id, null, invoiceData);
243
+ await this.lightning.cancelHodlInvoice(invoice.id);
244
+ this.unsubscribeInvoice(invoice.id);
245
+ await this.removeSwapData(invoiceData);
246
+ this.swapLogger.info(invoiceData, "htlcReceived(): transaction sending failed, refunding lightning: ", invoiceData.pr);
247
+ throw {
248
+ code: 20002,
249
+ msg: "Transaction sending failed"
250
+ };
251
+ } else {
252
+ //Successfully paid
253
+ await invoiceData.setState(FromBtcLnTrustedSwapState.CONFIRMED);
254
+ await this.storageManager.saveData(invoice.id, null, invoiceData);
255
+ }
256
+
257
+ unlock();
258
+ unlock = null;
259
+ }
260
+
261
+ if(invoiceData.state===FromBtcLnTrustedSwapState.SENT) {
262
+ if(invoiceData.isLocked()) return;
263
+
264
+ const txStatus = await chainInterface.getTxStatus(invoiceData.scRawTx);
265
+ if(txStatus==="not_found") {
266
+ //Retry
267
+ invoiceData.txIds = {init: null};
268
+ invoiceData.scRawTx = null;
269
+ await invoiceData.setState(FromBtcLnTrustedSwapState.RECEIVED);
270
+ await this.storageManager.saveData(invoice.id, null, invoiceData);
271
+ }
272
+ if(txStatus==="reverted") {
273
+ //Cancel invoice
274
+ await invoiceData.setState(FromBtcLnTrustedSwapState.REFUNDED);
275
+ await this.storageManager.saveData(invoice.id, null, invoiceData);
276
+ await this.lightning.cancelHodlInvoice(invoice.id);
277
+ this.unsubscribeInvoice(invoice.id);
278
+ await this.removeSwapData(invoiceData);
279
+ this.swapLogger.info(invoiceData, "htlcReceived(): transaction reverted, refunding lightning: ", invoiceData.pr);
280
+ throw {
281
+ code: 20002,
282
+ msg: "Transaction reverted"
283
+ };
284
+ }
285
+ if(txStatus==="success") {
286
+ //Successfully paid
287
+ await invoiceData.setState(FromBtcLnTrustedSwapState.CONFIRMED);
288
+ await this.storageManager.saveData(invoice.id, null, invoiceData);
289
+ }
290
+ }
291
+
292
+ if(invoiceData.state===FromBtcLnTrustedSwapState.CONFIRMED) {
293
+ await this.lightning.settleHodlInvoice(invoiceData.secret);
294
+
295
+ if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcSettled = Date.now();
296
+
297
+ const paymentHash = invoiceData.getIdentifierHash();
298
+ this.processedTxIds.set(paymentHash, invoiceData.txIds.init);
299
+ await invoiceData.setState(FromBtcLnTrustedSwapState.SETTLED);
300
+
301
+ this.unsubscribeInvoice(paymentHash);
302
+ this.swapLogger.info(invoiceData, "htlcReceived(): invoice settled, invoice: "+invoiceData.pr+" scTxId: "+invoiceData.txIds.init);
303
+ await this.removeSwapData(invoiceData);
304
+ }
305
+ }
306
+
307
+ /**
308
+ *
309
+ * Checks if the lightning invoice is in HELD state (htlcs received but yet unclaimed)
310
+ *
311
+ * @param paymentHash
312
+ * @throws {DefinedRuntimeError} Will throw if the lightning invoice is not found, or if it isn't in the HELD state
313
+ * @returns the fetched lightning invoice
314
+ */
315
+ private async checkInvoiceStatus(paymentHash: string): Promise<LightningNetworkInvoice> {
316
+ const invoice = await this.lightning.getInvoice(paymentHash);
317
+
318
+ const isInvoiceFound = invoice!=null;
319
+ if (!isInvoiceFound) throw {
320
+ _httpStatus: 200,
321
+ code: 10001,
322
+ msg: "Invoice expired/canceled"
323
+ }
324
+
325
+ const arr = invoice.description.split("-");
326
+ if(arr.length<3 || arr[1]!=="GAS") throw {
327
+ _httpStatus: 200,
328
+ code: 10001,
329
+ msg: "Invoice expired/canceled"
330
+ }
331
+ const chainIdentifier = arr[0];
332
+ const address = arr[2];
333
+
334
+ const { chainInterface} = this.getChain(chainIdentifier);
335
+ if(!chainInterface.isValidAddress(address, true)) throw {
336
+ _httpStatus: 200,
337
+ code: 10001,
338
+ msg: "Invoice expired/canceled"
339
+ };
340
+
341
+ switch(invoice.status) {
342
+ case "held":
343
+ return invoice;
344
+ case "canceled":
345
+ throw {
346
+ _httpStatus: 200,
347
+ code: 10001,
348
+ msg: "Invoice expired/canceled"
349
+ };
350
+ case "confirmed":
351
+ throw {
352
+ _httpStatus: 200,
353
+ code: 10000,
354
+ msg: "Invoice already paid",
355
+ data: {
356
+ txId: this.processedTxIds.get(paymentHash)
357
+ }
358
+ };
359
+ case "unpaid":
360
+ throw {
361
+ _httpStatus: 200,
362
+ code: 10010,
363
+ msg: "Invoice yet unpaid"
364
+ };
365
+ default:
366
+ throw new Error("Lightning invoice invalid state!");
367
+ }
368
+ }
369
+
370
+ startRestServer(restServer: Express) {
371
+
372
+ const createInvoice = expressHandlerWrapper(async (req: Request & {paramReader: IParamReader}, res: Response & {responseStream: ServerParamEncoder}) => {
373
+ const metadata: {
374
+ request: any,
375
+ invoiceRequest?: any,
376
+ invoiceResponse?: any,
377
+ times: {[key: string]: number}
378
+ } = {request: {}, times: {}};
379
+
380
+ const chainIdentifier = req.query.chain as string;
381
+ const {signer, chainInterface} = this.getChain(chainIdentifier);
382
+
383
+ metadata.times.requestReceived = Date.now();
384
+ /**
385
+ * address: string solana address of the recipient
386
+ * amount: string amount (in lamports/smart chain base units) of the invoice
387
+ */
388
+
389
+ req.query.token ??= chainInterface.getNativeCurrencyAddress();
390
+
391
+ const parsedBody: FromBtcLnTrustedRequestType = verifySchema(req.query,{
392
+ address: (val: string) => val!=null &&
393
+ typeof(val)==="string" &&
394
+ chainInterface.isValidAddress(val, true) ? val : null,
395
+ token: (val: string) => val!=null &&
396
+ typeof(val)==="string" &&
397
+ this.isTokenSupported(chainIdentifier, val) ? val : null,
398
+ amount: FieldTypeEnum.BigInt,
399
+ exactIn: (val: string) => val==="true" ? true :
400
+ (val==="false" || val===undefined) ? false : null
401
+ });
402
+ if(parsedBody==null) throw {
403
+ code: 20100,
404
+ msg: "Invalid request body"
405
+ };
406
+ metadata.request = parsedBody;
407
+
408
+ const requestedAmount = {input: parsedBody.exactIn, amount: parsedBody.amount, token: parsedBody.token};
409
+ const request = {
410
+ chainIdentifier,
411
+ raw: req,
412
+ parsed: parsedBody,
413
+ metadata
414
+ };
415
+ const useToken = parsedBody.token;
416
+
417
+ this.checkTooManyInflightSwaps();
418
+
419
+ //Check request params
420
+ const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount);
421
+ metadata.times.requestChecked = Date.now();
422
+
423
+ //Create abortController for parallel prefetches
424
+ const responseStream = res.responseStream;
425
+ const abortController = getAbortController(responseStream);
426
+
427
+ //Pre-fetch data
428
+ const pricePrefetchPromise: Promise<bigint> = this.swapPricing.preFetchPrice(useToken, chainIdentifier).catch(e => {
429
+ this.logger.error("pricePrefetchPromise(): pricePrefetch error: ", e);
430
+ abortController.abort(e);
431
+ return null;
432
+ });
433
+ const balancePrefetch: Promise<bigint> = chainInterface.getBalance(signer.getAddress(), useToken).catch(e => {
434
+ this.logger.error("getBalancePrefetch(): balancePrefetch error: ", e);
435
+ abortController.abort(e);
436
+ return null;
437
+ });
438
+ const nativeBalancePrefetch: Promise<bigint> = useToken===chainInterface.getNativeCurrencyAddress() ?
439
+ balancePrefetch : this.prefetchNativeBalanceIfNeeded(chainIdentifier, abortController);
440
+ const channelsPrefetch: Promise<LightningNetworkChannel[]> = this.LightningAssertions.getChannelsPrefetch(abortController);
441
+
442
+ await this.checkNativeBalance(chainIdentifier, nativeBalancePrefetch, abortController.signal);
443
+
444
+ //Check valid amount specified (min/max)
445
+ const {
446
+ amountBD,
447
+ swapFee,
448
+ swapFeeInToken,
449
+ totalInToken
450
+ } = await this.AmountAssertions.checkFromBtcAmount(this.type, request, {...requestedAmount, pricePrefetch: pricePrefetchPromise}, fees, abortController.signal);
451
+ metadata.times.priceCalculated = Date.now();
452
+
453
+ //Check if we have enough funds to honor the request
454
+ await this.checkBalance(totalInToken, balancePrefetch, abortController.signal)
455
+ await this.LightningAssertions.checkInboundLiquidity(amountBD, channelsPrefetch, abortController.signal);
456
+ metadata.times.balanceChecked = Date.now();
457
+
458
+ const secret = randomBytes(32);
459
+ const hash = createHash("sha256").update(secret).digest();
460
+
461
+ const hodlInvoiceObj: HodlInvoiceInit = {
462
+ description: chainIdentifier+"-GAS-"+parsedBody.address,
463
+ cltvDelta: Number(this.config.minCltv) + 5,
464
+ expiresAt: Date.now()+(this.config.invoiceTimeoutSeconds*1000),
465
+ id: hash.toString("hex"),
466
+ mtokens: amountBD * 1000n
467
+ };
468
+ metadata.invoiceRequest = hodlInvoiceObj;
469
+
470
+ const hodlInvoice = await this.lightning.createHodlInvoice(hodlInvoiceObj);
471
+ abortController.signal.throwIfAborted();
472
+ metadata.times.invoiceCreated = Date.now();
473
+ metadata.invoiceResponse = {...hodlInvoice};
474
+
475
+ const createdSwap = new FromBtcLnTrustedSwap(
476
+ chainIdentifier,
477
+ hodlInvoice.request,
478
+ hodlInvoice.mtokens,
479
+ swapFee,
480
+ swapFeeInToken,
481
+ totalInToken,
482
+ secret.toString("hex"),
483
+ parsedBody.address,
484
+ useToken
485
+ );
486
+ metadata.times.swapCreated = Date.now();
487
+ createdSwap.metadata = metadata;
488
+
489
+ await PluginManager.swapCreate(createdSwap);
490
+ await this.storageManager.saveData(hash.toString("hex"), null, createdSwap);
491
+ this.subscribeToInvoice(createdSwap);
492
+
493
+ this.swapLogger.info(createdSwap, "REST: /createInvoice: Created swap invoice: "+hodlInvoice.request+" amount: "+amountBD.toString(10));
494
+
495
+ res.status(200).json({
496
+ msg: "Success",
497
+ code: 10000,
498
+ data: {
499
+ pr: hodlInvoice.request,
500
+ amountSats: amountBD.toString(10),
501
+ swapFeeSats: swapFee.toString(10),
502
+ swapFee: swapFeeInToken.toString(10),
503
+ total: totalInToken.toString(10),
504
+ intermediaryKey: signer.getAddress()
505
+ }
506
+ });
507
+ });
508
+
509
+ restServer.get(this.path+"/createInvoice", createInvoice);
510
+
511
+ const getInvoiceStatus = expressHandlerWrapper(async (req, res) => {
512
+ /**
513
+ * paymentHash: string payment hash of the invoice
514
+ */
515
+ const parsedBody = verifySchema({...req.body, ...req.query}, {
516
+ paymentHash: (val: string) => val!=null &&
517
+ typeof(val)==="string" &&
518
+ val.length===64 &&
519
+ HEX_REGEX.test(val) ? val: null,
520
+ });
521
+
522
+ await this.checkInvoiceStatus(parsedBody.paymentHash);
523
+
524
+ const invoiceData: FromBtcLnTrustedSwap = await this.storageManager.getData(parsedBody.paymentHash, null);
525
+ if (invoiceData==null) throw {
526
+ _httpStatus: 200,
527
+ code: 10001,
528
+ msg: "Invoice expired/canceled"
529
+ };
530
+
531
+ if (
532
+ invoiceData.state === FromBtcLnTrustedSwapState.CANCELED ||
533
+ invoiceData.state === FromBtcLnTrustedSwapState.REFUNDED
534
+ ) throw {
535
+ _httpStatus: 200,
536
+ code: 10001,
537
+ msg: "Invoice expired/canceled"
538
+ };
539
+
540
+ if (invoiceData.state === FromBtcLnTrustedSwapState.CREATED) throw {
541
+ _httpStatus: 200,
542
+ code: 10010,
543
+ msg: "Invoice yet unpaid"
544
+ };
545
+
546
+ if (invoiceData.state === FromBtcLnTrustedSwapState.RECEIVED) throw {
547
+ _httpStatus: 200,
548
+ code: 10011,
549
+ msg: "Invoice received, payment processing"
550
+ };
551
+
552
+ if (invoiceData.state === FromBtcLnTrustedSwapState.SENT) throw {
553
+ _httpStatus: 200,
554
+ code: 10012,
555
+ msg: "Tx sent",
556
+ data: {
557
+ txId: invoiceData.txIds.init
558
+ }
559
+ };
560
+
561
+ if (invoiceData.state === FromBtcLnTrustedSwapState.CONFIRMED) throw {
562
+ _httpStatus: 200,
563
+ code: 10000,
564
+ msg: "Success, tx confirmed",
565
+ data: {
566
+ txId: invoiceData.txIds.init
567
+ }
568
+ };
569
+
570
+ if (invoiceData.state === FromBtcLnTrustedSwapState.SETTLED) throw {
571
+ _httpStatus: 200,
572
+ code: 10000,
573
+ msg: "Success, tx confirmed - invoice settled",
574
+ data: {
575
+ txId: invoiceData.txIds.init
576
+ }
577
+ };
578
+ });
579
+ restServer.get(this.path+"/getInvoiceStatus", getInvoiceStatus);
580
+
581
+ this.logger.info("started at path: ", this.path);
582
+ }
583
+
584
+ async init() {
585
+ await this.storageManager.loadData(FromBtcLnTrustedSwap);
586
+ //Check if all swaps contain a valid amount
587
+ for(let {obj: swap} of await this.storageManager.query([])) {
588
+ if(swap.amount==null) {
589
+ const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
590
+ swap.amount = (parsedPR.mtokens + 999n) / 1000n;
591
+ }
592
+ }
593
+ await PluginManager.serviceInitialize(this);
594
+ }
595
+
596
+ getInfoData(): any {
597
+ return {
598
+ minCltv: Number(this.config.minCltv)
599
+ };
600
+ }
601
+
602
+ }
603
+