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