@atomiqlabs/lp-lib 10.3.11

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 (138) hide show
  1. package/LICENSE +201 -0
  2. package/dist/fees/IBtcFeeEstimator.d.ts +3 -0
  3. package/dist/fees/IBtcFeeEstimator.js +2 -0
  4. package/dist/fees/OneDollarFeeEstimator.d.ts +16 -0
  5. package/dist/fees/OneDollarFeeEstimator.js +71 -0
  6. package/dist/index.d.ts +33 -0
  7. package/dist/index.js +52 -0
  8. package/dist/info/InfoHandler.d.ts +17 -0
  9. package/dist/info/InfoHandler.js +70 -0
  10. package/dist/plugins/IPlugin.d.ts +118 -0
  11. package/dist/plugins/IPlugin.js +33 -0
  12. package/dist/plugins/PluginManager.d.ts +89 -0
  13. package/dist/plugins/PluginManager.js +263 -0
  14. package/dist/prices/BinanceSwapPrice.d.ts +27 -0
  15. package/dist/prices/BinanceSwapPrice.js +106 -0
  16. package/dist/prices/CoinGeckoSwapPrice.d.ts +31 -0
  17. package/dist/prices/CoinGeckoSwapPrice.js +76 -0
  18. package/dist/storage/IIntermediaryStorage.d.ts +15 -0
  19. package/dist/storage/IIntermediaryStorage.js +2 -0
  20. package/dist/storagemanager/IntermediaryStorageManager.d.ts +15 -0
  21. package/dist/storagemanager/IntermediaryStorageManager.js +113 -0
  22. package/dist/storagemanager/StorageManager.d.ts +12 -0
  23. package/dist/storagemanager/StorageManager.js +74 -0
  24. package/dist/swaps/FromBtcBaseSwap.d.ts +12 -0
  25. package/dist/swaps/FromBtcBaseSwap.js +16 -0
  26. package/dist/swaps/FromBtcBaseSwapHandler.d.ts +118 -0
  27. package/dist/swaps/FromBtcBaseSwapHandler.js +294 -0
  28. package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +25 -0
  29. package/dist/swaps/FromBtcLnBaseSwapHandler.js +55 -0
  30. package/dist/swaps/ISwapPrice.d.ts +44 -0
  31. package/dist/swaps/ISwapPrice.js +73 -0
  32. package/dist/swaps/SwapHandler.d.ts +186 -0
  33. package/dist/swaps/SwapHandler.js +292 -0
  34. package/dist/swaps/SwapHandlerSwap.d.ts +75 -0
  35. package/dist/swaps/SwapHandlerSwap.js +72 -0
  36. package/dist/swaps/ToBtcBaseSwap.d.ts +35 -0
  37. package/dist/swaps/ToBtcBaseSwap.js +61 -0
  38. package/dist/swaps/ToBtcBaseSwapHandler.d.ts +94 -0
  39. package/dist/swaps/ToBtcBaseSwapHandler.js +233 -0
  40. package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +92 -0
  41. package/dist/swaps/frombtc_abstract/FromBtcAbs.js +386 -0
  42. package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +26 -0
  43. package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +63 -0
  44. package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +55 -0
  45. package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +586 -0
  46. package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +43 -0
  47. package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +99 -0
  48. package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +105 -0
  49. package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +731 -0
  50. package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +29 -0
  51. package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +64 -0
  52. package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +79 -0
  53. package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +514 -0
  54. package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +28 -0
  55. package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +66 -0
  56. package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +290 -0
  57. package/dist/swaps/tobtc_abstract/ToBtcAbs.js +1056 -0
  58. package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +29 -0
  59. package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +70 -0
  60. package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +246 -0
  61. package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +1169 -0
  62. package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +27 -0
  63. package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +65 -0
  64. package/dist/utils/Utils.d.ts +32 -0
  65. package/dist/utils/Utils.js +109 -0
  66. package/dist/utils/coinselect2/accumulative.d.ts +6 -0
  67. package/dist/utils/coinselect2/accumulative.js +44 -0
  68. package/dist/utils/coinselect2/blackjack.d.ts +6 -0
  69. package/dist/utils/coinselect2/blackjack.js +41 -0
  70. package/dist/utils/coinselect2/index.d.ts +16 -0
  71. package/dist/utils/coinselect2/index.js +40 -0
  72. package/dist/utils/coinselect2/utils.d.ts +64 -0
  73. package/dist/utils/coinselect2/utils.js +121 -0
  74. package/dist/utils/paramcoders/IParamReader.d.ts +5 -0
  75. package/dist/utils/paramcoders/IParamReader.js +2 -0
  76. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -0
  77. package/dist/utils/paramcoders/IParamWriter.js +2 -0
  78. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -0
  79. package/dist/utils/paramcoders/LegacyParamEncoder.js +33 -0
  80. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -0
  81. package/dist/utils/paramcoders/ParamDecoder.js +234 -0
  82. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -0
  83. package/dist/utils/paramcoders/ParamEncoder.js +22 -0
  84. package/dist/utils/paramcoders/SchemaVerifier.d.ts +22 -0
  85. package/dist/utils/paramcoders/SchemaVerifier.js +85 -0
  86. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -0
  87. package/dist/utils/paramcoders/server/ServerParamDecoder.js +105 -0
  88. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -0
  89. package/dist/utils/paramcoders/server/ServerParamEncoder.js +76 -0
  90. package/package.json +43 -0
  91. package/src/fees/IBtcFeeEstimator.ts +7 -0
  92. package/src/fees/OneDollarFeeEstimator.ts +95 -0
  93. package/src/index.ts +46 -0
  94. package/src/info/InfoHandler.ts +106 -0
  95. package/src/plugins/IPlugin.ts +155 -0
  96. package/src/plugins/PluginManager.ts +310 -0
  97. package/src/prices/BinanceSwapPrice.ts +114 -0
  98. package/src/prices/CoinGeckoSwapPrice.ts +88 -0
  99. package/src/storage/IIntermediaryStorage.ts +21 -0
  100. package/src/storagemanager/IntermediaryStorageManager.ts +101 -0
  101. package/src/storagemanager/StorageManager.ts +68 -0
  102. package/src/swaps/FromBtcBaseSwap.ts +21 -0
  103. package/src/swaps/FromBtcBaseSwapHandler.ts +375 -0
  104. package/src/swaps/FromBtcLnBaseSwapHandler.ts +48 -0
  105. package/src/swaps/ISwapPrice.ts +94 -0
  106. package/src/swaps/SwapHandler.ts +404 -0
  107. package/src/swaps/SwapHandlerSwap.ts +133 -0
  108. package/src/swaps/ToBtcBaseSwap.ts +76 -0
  109. package/src/swaps/ToBtcBaseSwapHandler.ts +309 -0
  110. package/src/swaps/frombtc_abstract/FromBtcAbs.ts +484 -0
  111. package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +77 -0
  112. package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +661 -0
  113. package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +158 -0
  114. package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +864 -0
  115. package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +82 -0
  116. package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +592 -0
  117. package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +90 -0
  118. package/src/swaps/tobtc_abstract/ToBtcAbs.ts +1249 -0
  119. package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +112 -0
  120. package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +1422 -0
  121. package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +87 -0
  122. package/src/utils/Utils.ts +108 -0
  123. package/src/utils/coinselect2/accumulative.js +32 -0
  124. package/src/utils/coinselect2/accumulative.ts +58 -0
  125. package/src/utils/coinselect2/blackjack.js +29 -0
  126. package/src/utils/coinselect2/blackjack.ts +54 -0
  127. package/src/utils/coinselect2/index.js +16 -0
  128. package/src/utils/coinselect2/index.ts +50 -0
  129. package/src/utils/coinselect2/utils.js +110 -0
  130. package/src/utils/coinselect2/utils.ts +183 -0
  131. package/src/utils/paramcoders/IParamReader.ts +8 -0
  132. package/src/utils/paramcoders/IParamWriter.ts +8 -0
  133. package/src/utils/paramcoders/LegacyParamEncoder.ts +28 -0
  134. package/src/utils/paramcoders/ParamDecoder.ts +219 -0
  135. package/src/utils/paramcoders/ParamEncoder.ts +30 -0
  136. package/src/utils/paramcoders/SchemaVerifier.ts +97 -0
  137. package/src/utils/paramcoders/server/ServerParamDecoder.ts +115 -0
  138. package/src/utils/paramcoders/server/ServerParamEncoder.ts +76 -0
@@ -0,0 +1,1422 @@
1
+ import * as BN from "bn.js";
2
+ import {Express, Request, Response} from "express";
3
+ import * as bolt11 from "@atomiqlabs/bolt11";
4
+ import * as lncli from "ln-service";
5
+ import {ToBtcLnSwapAbs, ToBtcLnSwapState} from "./ToBtcLnSwapAbs";
6
+ import {MultichainData, SwapHandlerType} from "../SwapHandler";
7
+ import {ISwapPrice} from "../ISwapPrice";
8
+ import {
9
+ ChainSwapType,
10
+ ClaimEvent,
11
+ InitializeEvent,
12
+ RefundEvent,
13
+ SwapCommitStatus,
14
+ SwapData
15
+ } from "@atomiqlabs/base";
16
+ import {AuthenticatedLnd} from "lightning";
17
+ import {expressHandlerWrapper, handleLndError, HEX_REGEX, isDefinedRuntimeError} from "../../utils/Utils";
18
+ import {PluginManager} from "../../plugins/PluginManager";
19
+ import {IIntermediaryStorage} from "../../storage/IIntermediaryStorage";
20
+ import {randomBytes} from "crypto";
21
+ import {serverParamDecoder} from "../../utils/paramcoders/server/ServerParamDecoder";
22
+ import {IParamReader} from "../../utils/paramcoders/IParamReader";
23
+ import {FieldTypeEnum, verifySchema} from "../../utils/paramcoders/SchemaVerifier";
24
+ import {ServerParamEncoder} from "../../utils/paramcoders/server/ServerParamEncoder";
25
+ import {ToBtcBaseConfig, ToBtcBaseSwapHandler} from "../ToBtcBaseSwapHandler";
26
+ import {BlindedPayInfo} from "@atomiqlabs/bolt11";
27
+
28
+ export type ToBtcLnConfig = ToBtcBaseConfig & {
29
+ routingFeeMultiplier: BN,
30
+
31
+ minSendCltv: BN,
32
+
33
+ allowProbeFailedSwaps: boolean,
34
+ allowShortExpiry: boolean,
35
+
36
+ minLnRoutingFeePPM?: BN,
37
+ minLnBaseFee?: BN,
38
+
39
+ exactInExpiry?: number
40
+ };
41
+
42
+ const SNOWFLAKE_LIST: Set<string> = new Set([
43
+ "038f8f113c580048d847d6949371726653e02b928196bad310e3eda39ff61723f6"
44
+ ]);
45
+
46
+ type ProbeAndRouteResponse = {
47
+ confidence: number,
48
+ fee: number,
49
+ fee_mtokens: string,
50
+ mtokens: string,
51
+ payment: string,
52
+ safe_fee: number,
53
+ safe_tokens: number,
54
+ timeout: number,
55
+ tokens: number
56
+ };
57
+
58
+ type LNRoutes = {
59
+ public_key: string,
60
+ fee_rate?: number,
61
+ cltv_delta?: number,
62
+ channel?: string,
63
+ base_fee_mtokens?: string
64
+ }[][];
65
+
66
+ function routesMatch(routesA: LNRoutes, routesB: LNRoutes) {
67
+ if(routesA===routesB) return true;
68
+ if(routesA==null || routesB==null) {
69
+ return false;
70
+ }
71
+ if(routesA.length!==routesB.length) return false;
72
+ for(let i=0;i<routesA.length;i++) {
73
+ if(routesA[i]===routesB[i]) continue;
74
+ if(routesA[i]==null || routesB[i]==null) {
75
+ return false;
76
+ }
77
+ if(routesA[i].length!==routesB[i].length) return false;
78
+ for(let e=0;e<routesA[i].length;e++) {
79
+ if(routesA[i][e]===routesB[i][e]) continue;
80
+ if(routesA[i][e]==null || routesB[i][e]==null) {
81
+ return false;
82
+ }
83
+ if(
84
+ routesA[i][e].public_key!==routesB[i][e].public_key ||
85
+ routesA[i][e].base_fee_mtokens!==routesB[i][e].base_fee_mtokens ||
86
+ routesA[i][e].channel!==routesB[i][e].channel ||
87
+ routesA[i][e].cltv_delta!==routesB[i][e].cltv_delta ||
88
+ routesA[i][e].fee_rate!==routesB[i][e].fee_rate
89
+ ) {
90
+ return false;
91
+ }
92
+ }
93
+ }
94
+
95
+ return true;
96
+ }
97
+
98
+ type ExactInAuthorization = {
99
+ chainIdentifier: string,
100
+ reqId: string,
101
+ expiry: number,
102
+
103
+ amount: BN,
104
+ destination: string,
105
+ cltvDelta: number,
106
+ routes: LNRoutes,
107
+
108
+ quotedNetworkFeeInToken: BN,
109
+ swapFeeInToken: BN,
110
+ total: BN,
111
+ confidence: number,
112
+ quotedNetworkFee: BN,
113
+ swapFee: BN,
114
+
115
+ token: string,
116
+ swapExpiry: BN,
117
+ offerer: string,
118
+
119
+ preFetchSignData: any,
120
+ metadata: {
121
+ request: any,
122
+ probeRequest?: any,
123
+ probeResponse?: any,
124
+ routeResponse?: any,
125
+ times: {[key: string]: number}
126
+ }
127
+ }
128
+
129
+ export type ToBtcLnRequestType = {
130
+ pr: string,
131
+ maxFee: BN,
132
+ expiryTimestamp: BN,
133
+ token: string,
134
+ offerer: string,
135
+ exactIn?: boolean,
136
+ amount?: BN
137
+ };
138
+
139
+ /**
140
+ * Swap handler handling to BTCLN swaps using submarine swaps
141
+ */
142
+ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwapState> {
143
+ protected readonly LIGHTNING_LIQUIDITY_CACHE_TIMEOUT = 5*1000;
144
+
145
+ activeSubscriptions: Set<string> = new Set<string>();
146
+ lightningLiquidityCache: {
147
+ liquidityMTokens: BN,
148
+ timestamp: number
149
+ };
150
+
151
+ readonly type = SwapHandlerType.TO_BTCLN;
152
+
153
+ readonly config: ToBtcLnConfig & {minTsSendCltv: BN};
154
+
155
+ readonly exactInAuths: {
156
+ [reqId: string]: ExactInAuthorization
157
+ } = {};
158
+
159
+ constructor(
160
+ storageDirectory: IIntermediaryStorage<ToBtcLnSwapAbs>,
161
+ path: string,
162
+ chainData: MultichainData,
163
+ lnd: AuthenticatedLnd,
164
+ swapPricing: ISwapPrice,
165
+ config: ToBtcLnConfig
166
+ ) {
167
+ super(storageDirectory, path, chainData, lnd, swapPricing);
168
+ const anyConfig = config as any;
169
+ anyConfig.minTsSendCltv = config.gracePeriod.add(config.bitcoinBlocktime.mul(config.minSendCltv).mul(config.safetyFactor));
170
+ this.config = anyConfig;
171
+ this.config.minLnRoutingFeePPM = this.config.minLnRoutingFeePPM || new BN(1000);
172
+ this.config.minLnBaseFee = this.config.minLnBaseFee || new BN(5);
173
+ this.config.exactInExpiry = this.config.exactInExpiry || 10*1000;
174
+ }
175
+
176
+ /**
177
+ * Fetches the payment info, returns null if payment not found
178
+ *
179
+ * @param paymentHash
180
+ * @private
181
+ */
182
+ private async getPayment(paymentHash: string): Promise<any> {
183
+ try {
184
+ return await lncli.getPayment({
185
+ id: paymentHash,
186
+ lnd: this.LND
187
+ });
188
+ } catch (e) {
189
+ if (Array.isArray(e) && e[0] === 404 && e[1] === "SentPaymentNotFound") return null;
190
+ throw e;
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Cleans up exactIn authorization that are already past their expiry
196
+ *
197
+ * @protected
198
+ */
199
+ private cleanExpiredExactInAuthorizations() {
200
+ for(let key in this.exactInAuths) {
201
+ const obj = this.exactInAuths[key];
202
+ if(obj.expiry<Date.now()) {
203
+ this.logger.info("cleanExpiredExactInAuthorizations(): remove expired authorization, reqId: "+key);
204
+ delete this.exactInAuths[key];
205
+ }
206
+ }
207
+ }
208
+
209
+ protected async processPastSwap(swap: ToBtcLnSwapAbs): Promise<void> {
210
+ //Current timestamp plus maximum allowed on-chain time skew
211
+ const timestamp = new BN(Math.floor(Date.now()/1000)).sub(new BN(this.config.maxSkew));
212
+
213
+ if (swap.state === ToBtcLnSwapState.SAVED) {
214
+ //Cancel the swaps where signature is expired
215
+ const isSignatureExpired = swap.signatureExpiry!=null && swap.signatureExpiry.lt(timestamp);
216
+ if(isSignatureExpired) {
217
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): signature expired, cancel uncommited swap, invoice: "+swap.pr);
218
+ await this.removeSwapData(swap, ToBtcLnSwapState.CANCELED);
219
+ return;
220
+ }
221
+
222
+ //Cancel the swaps where lightning invoice is expired
223
+ const decodedPR = bolt11.decode(swap.pr);
224
+ const isInvoiceExpired = decodedPR.timeExpireDate < Date.now() / 1000;
225
+ if (isInvoiceExpired) {
226
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): invoice expired, cancel uncommited swap, invoice: "+swap.pr);
227
+ await this.removeSwapData(swap, ToBtcLnSwapState.CANCELED);
228
+ return;
229
+ }
230
+ }
231
+
232
+ if (swap.state === ToBtcLnSwapState.COMMITED || swap.state === ToBtcLnSwapState.PAID) {
233
+ //Process swaps in commited & paid state
234
+ await this.processInitialized(swap);
235
+ }
236
+
237
+ if (swap.state === ToBtcLnSwapState.NON_PAYABLE) {
238
+ //Remove expired swaps (as these can already be unilaterally refunded by the client), so we don't need
239
+ // to be able to cooperatively refund them
240
+ const isSwapExpired = swap.data.getExpiry().lt(timestamp);
241
+ if(isSwapExpired) {
242
+ this.swapLogger.info(swap, "processPastSwap(state=NON_PAYABLE): swap expired, removing swap data, invoice: "+swap.pr);
243
+ await this.removeSwapData(swap);
244
+ }
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Checks past swaps, deletes ones that are already expired, and tries to process ones that are committed.
250
+ */
251
+ protected async processPastSwaps() {
252
+ this.cleanExpiredExactInAuthorizations();
253
+
254
+ const queriedData = await this.storageManager.query([
255
+ {
256
+ key: "state",
257
+ value: [
258
+ ToBtcLnSwapState.SAVED,
259
+ ToBtcLnSwapState.COMMITED,
260
+ ToBtcLnSwapState.PAID,
261
+ ToBtcLnSwapState.NON_PAYABLE
262
+ ]
263
+ }
264
+ ]);
265
+
266
+ for(let swap of queriedData) {
267
+ await this.processPastSwap(swap);
268
+ }
269
+ }
270
+
271
+ /**
272
+ * Tries to claim the swap funds on the SC side, returns false if the swap is already locked (claim tx is already being sent)
273
+ *
274
+ * @param swap
275
+ * @private
276
+ * @returns Whether the transaction was successfully sent
277
+ */
278
+ private async tryClaimSwap(swap: ToBtcLnSwapAbs): Promise<boolean> {
279
+ if(swap.secret==null) throw new Error("Invalid swap state, needs payment pre-image!");
280
+
281
+ const {swapContract, signer} = this.getChain(swap.chainIdentifier);
282
+
283
+ //Set flag that we are sending the transaction already, so we don't end up with race condition
284
+ const unlock: () => boolean = swap.lock(swapContract.claimWithSecretTimeout);
285
+ if(unlock==null) return false;
286
+
287
+ try {
288
+ this.swapLogger.debug(swap, "tryClaimSwap(): initiate claim of swap, secret: "+swap.secret);
289
+ const success = await swapContract.claimWithSecret(signer, swap.data, swap.secret, false, false, {
290
+ waitForConfirmation: true
291
+ });
292
+ this.swapLogger.info(swap, "tryClaimSwap(): swap claimed successfully, secret: "+swap.secret+" invoice: "+swap.pr);
293
+ if(swap.metadata!=null) swap.metadata.times.txClaimed = Date.now();
294
+ unlock();
295
+ return true;
296
+ } catch (e) {
297
+ this.swapLogger.error(swap, "tryClaimSwap(): error occurred claiming swap, secret: "+swap.secret+" invoice: "+swap.pr, e);
298
+ return false;
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Process the result of attempted lightning network payment
304
+ *
305
+ * @param swap
306
+ * @param lnPaymentStatus
307
+ */
308
+ private async processPaymentResult(swap: ToBtcLnSwapAbs, lnPaymentStatus: {is_confirmed?: boolean, is_failed?: boolean, is_pending?: boolean, payment?: any}) {
309
+ if(lnPaymentStatus.is_pending) {
310
+ return;
311
+ }
312
+
313
+ if(lnPaymentStatus.is_failed) {
314
+ this.swapLogger.info(swap, "processPaymentResult(): invoice payment failed, cancelling swap, invoice: "+swap.pr);
315
+ await swap.setState(ToBtcLnSwapState.NON_PAYABLE);
316
+ await this.storageManager.saveData(swap.data.getHash(), swap.data.getSequence(), swap);
317
+ return;
318
+ }
319
+
320
+ const {swapContract, signer} = this.getChain(swap.chainIdentifier);
321
+
322
+ if(lnPaymentStatus.is_confirmed) {
323
+ //Save pre-image & real network fee
324
+ swap.secret = lnPaymentStatus.payment.secret;
325
+ swap.setRealNetworkFee(new BN(lnPaymentStatus.payment.fee_mtokens).div(new BN(1000)));
326
+ this.swapLogger.info(swap, "processPaymentResult(): invoice paid, secret: "+swap.secret+" realRoutingFee: "+swap.realNetworkFee.toString(10)+" invoice: "+swap.pr);
327
+ await swap.setState(ToBtcLnSwapState.PAID);
328
+ await this.storageManager.saveData(swap.data.getHash(), swap.data.getSequence(), swap);
329
+
330
+ //Check if escrow state exists
331
+ const isCommited = await swapContract.isCommited(swap.data);
332
+ if(!isCommited) {
333
+ const status = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
334
+ if(status===SwapCommitStatus.PAID) {
335
+ //This is alright, we got the money
336
+ await this.removeSwapData(swap, ToBtcLnSwapState.CLAIMED);
337
+ return;
338
+ } else if(status===SwapCommitStatus.EXPIRED) {
339
+ //This means the user was able to refund before we were able to claim, no good
340
+ await this.removeSwapData(swap, ToBtcLnSwapState.REFUNDED);
341
+ }
342
+ this.swapLogger.warn(swap, "processPaymentResult(): tried to claim but escrow doesn't exist anymore,"+
343
+ " status: "+status+
344
+ " invoice: "+swap.pr);
345
+ return;
346
+ }
347
+
348
+ const success = await this.tryClaimSwap(swap);
349
+ if(success) this.swapLogger.info(swap, "processPaymentResult(): swap claimed successfully, invoice: "+swap.pr);
350
+ return;
351
+ }
352
+
353
+ //This should never happen
354
+ throw new Error("Invalid lnPaymentStatus");
355
+ }
356
+
357
+ /**
358
+ * Subscribe to a pending lightning network payment attempt
359
+ *
360
+ * @param invoiceData
361
+ */
362
+ private subscribeToPayment(invoiceData: ToBtcLnSwapAbs): boolean {
363
+ const paymentHash = invoiceData.data.getHash();
364
+ if(this.activeSubscriptions.has(paymentHash)) return false;
365
+
366
+ const subscription = lncli.subscribeToPastPayment({id: paymentHash, lnd: this.LND});
367
+
368
+ const onResult = (lnPaymentStatus: {is_confirmed?: boolean, is_failed?: boolean, payment?: any, error?: any}) => {
369
+ const outcome = lnPaymentStatus.is_confirmed ? "success" : lnPaymentStatus.is_failed ? "failure" : null;
370
+ this.swapLogger.info(invoiceData, "subscribeToPayment(): result callback, outcome: "+outcome+" invoice: "+invoiceData.pr);
371
+ this.processPaymentResult(invoiceData, lnPaymentStatus).catch(e => this.swapLogger.error(invoiceData, "subscribeToPayment(): process payment result", e));
372
+ subscription.removeAllListeners();
373
+ this.activeSubscriptions.delete(paymentHash);
374
+ };
375
+
376
+ subscription.on('confirmed', (payment) => onResult({
377
+ is_confirmed: true,
378
+ payment
379
+ }));
380
+ subscription.on('failed', (data) => onResult({
381
+ is_failed: true,
382
+ error: data
383
+ }));
384
+
385
+ this.swapLogger.info(invoiceData, "subscribeToPayment(): subscribe to payment outcome, invoice: "+invoiceData.pr);
386
+
387
+ this.activeSubscriptions.add(paymentHash);
388
+ return true;
389
+ }
390
+
391
+ private async sendLightningPayment(swap: ToBtcLnSwapAbs): Promise<void> {
392
+ const decodedPR = bolt11.decode(swap.pr);
393
+ const expiryTimestamp: BN = swap.data.getExpiry();
394
+ const currentTimestamp: BN = new BN(Math.floor(Date.now()/1000));
395
+
396
+ //Run checks
397
+ const hasEnoughTimeToPay = expiryTimestamp.sub(currentTimestamp).gte(this.config.minTsSendCltv);
398
+ if(!hasEnoughTimeToPay) throw {
399
+ code: 90005,
400
+ msg: "Not enough time to reliably pay the invoice"
401
+ }
402
+
403
+ const isInvoiceExpired = decodedPR.timeExpireDate < Date.now() / 1000;
404
+ if (isInvoiceExpired) throw {
405
+ code: 90006,
406
+ msg: "Invoice already expired"
407
+ };
408
+
409
+ //Compute max cltv delta
410
+ const maxFee = swap.quotedNetworkFee;
411
+ const maxUsableCLTVdelta = expiryTimestamp.sub(currentTimestamp).sub(this.config.gracePeriod).div(this.config.bitcoinBlocktime.mul(this.config.safetyFactor));
412
+
413
+ await swap.setState(ToBtcLnSwapState.COMMITED);
414
+ await this.storageManager.saveData(decodedPR.tagsObject.payment_hash, swap.data.getSequence(), swap);
415
+
416
+ //Initiate payment
417
+ const { current_block_height } = await lncli.getHeight({lnd: this.LND});
418
+ const obj: any = {
419
+ request: swap.pr,
420
+ max_fee: maxFee.toString(10),
421
+ max_timeout_height: new BN(current_block_height).add(maxUsableCLTVdelta).toString(10),
422
+ lnd: this.LND
423
+ };
424
+ this.swapLogger.info(swap, "sendLightningPayment(): paying lightning network invoice,"+
425
+ " cltvDelta: "+maxUsableCLTVdelta.toString(10)+
426
+ " maxFee: "+maxFee.toString(10)+
427
+ " invoice: "+swap.pr);
428
+
429
+ try {
430
+ await lncli.pay(obj)
431
+ } catch (e) {
432
+ throw {
433
+ code: 90007,
434
+ msg: "Failed to initiate invoice payment",
435
+ data: {
436
+ error: JSON.stringify(e)
437
+ }
438
+ }
439
+ }
440
+ if(swap.metadata!=null) swap.metadata.times.payComplete = Date.now();
441
+ }
442
+
443
+ /**
444
+ * Begins a lightning network payment attempt, if not attempted already
445
+ *
446
+ * @param swap
447
+ */
448
+ private async processInitialized(swap: ToBtcLnSwapAbs) {
449
+ //Check if payment was already made
450
+ let lnPaymentStatus = await this.getPayment(swap.getHash());
451
+ if(swap.metadata!=null) swap.metadata.times.payPaymentChecked = Date.now();
452
+
453
+ const paymentExists = lnPaymentStatus!=null;
454
+ if(!paymentExists) {
455
+ try {
456
+ await this.sendLightningPayment(swap);
457
+ } catch (e) {
458
+ this.swapLogger.error(swap, "processInitialized(): lightning payment error", e);
459
+ if(isDefinedRuntimeError(e)) {
460
+ if(swap.metadata!=null) swap.metadata.payError = e;
461
+ await swap.setState(ToBtcLnSwapState.NON_PAYABLE);
462
+ await this.storageManager.saveData(swap.data.getHash(), swap.data.getSequence(), swap);
463
+ return;
464
+ } else throw e;
465
+ }
466
+ this.subscribeToPayment(swap);
467
+ return;
468
+ }
469
+
470
+ if(lnPaymentStatus.is_pending) {
471
+ this.subscribeToPayment(swap);
472
+ return;
473
+ }
474
+
475
+ //Payment has already concluded, process the result
476
+ await this.processPaymentResult(swap, lnPaymentStatus);
477
+ }
478
+
479
+ protected async processInitializeEvent(chainIdentifier: string, event: InitializeEvent<SwapData>): Promise<void> {
480
+ if(event.swapType!==ChainSwapType.HTLC) return;
481
+
482
+ const paymentHash = event.paymentHash;
483
+
484
+ const swap = await this.storageManager.getData(paymentHash, event.sequence);
485
+ if(swap==null || swap.chainIdentifier!==chainIdentifier) return;
486
+
487
+ swap.txIds.init = (event as any).meta?.txId;
488
+ if(swap.metadata!=null) swap.metadata.times.txReceived = Date.now();
489
+
490
+ this.swapLogger.info(swap, "SC: InitializeEvent: swap initialized by the client, invoice: "+swap.pr);
491
+
492
+ //Only process swaps in SAVED state
493
+ if(swap.state!==ToBtcLnSwapState.SAVED) return;
494
+ await this.processInitialized(swap);
495
+ }
496
+
497
+ protected async processClaimEvent(chainIdentifier: string, event: ClaimEvent<SwapData>): Promise<void> {
498
+ const paymentHash = event.paymentHash;
499
+
500
+ const swap = await this.storageManager.getData(paymentHash, event.sequence);
501
+ if(swap==null || swap.chainIdentifier!==chainIdentifier) return;
502
+
503
+ swap.txIds.claim = (event as any).meta?.txId;
504
+
505
+ this.swapLogger.info(swap, "SC: ClaimEvent: swap claimed to us, secret: "+event.secret+" invoice: "+swap.pr);
506
+
507
+ await this.removeSwapData(swap, ToBtcLnSwapState.CLAIMED);
508
+ }
509
+
510
+ protected async processRefundEvent(chainIdentifier: string, event: RefundEvent<SwapData>): Promise<void> {
511
+ const paymentHash = event.paymentHash;
512
+
513
+ const swap = await this.storageManager.getData(paymentHash, event.sequence);
514
+ if(swap==null || swap.chainIdentifier!==chainIdentifier) return;
515
+
516
+ swap.txIds.refund = (event as any).meta?.txId;
517
+
518
+ this.swapLogger.info(swap, "SC: RefundEvent: swap refunded back to the client, invoice: "+swap.pr);
519
+
520
+ await this.removeSwapData(swap, ToBtcLnSwapState.REFUNDED);
521
+ }
522
+
523
+ /**
524
+ * Checks if the amount was supplied in the exactIn request
525
+ *
526
+ * @param amount
527
+ * @param exactIn
528
+ * @throws {DefinedRuntimeError} will throw an error if the swap was exactIn, but amount not specified
529
+ */
530
+ private checkAmount(amount: BN, exactIn: boolean): void {
531
+ if(exactIn) {
532
+ if(amount==null) {
533
+ throw {
534
+ code: 20040,
535
+ msg: "Invalid request body (amount not specified)!"
536
+ };
537
+ }
538
+ }
539
+ }
540
+
541
+ /**
542
+ * Checks if the maxFee parameter is in valid range (>0)
543
+ *
544
+ * @param maxFee
545
+ * @throws {DefinedRuntimeError} will throw an error if the maxFee is zero or negative
546
+ */
547
+ private checkMaxFee(maxFee: BN): void {
548
+ if(maxFee.isNeg() || maxFee.isZero()) {
549
+ throw {
550
+ code: 20030,
551
+ msg: "Invalid request body (maxFee too low)!"
552
+ };
553
+ }
554
+ }
555
+
556
+ /**
557
+ * Checks and parses a payment request (bolt11 invoice), additionally also checks expiration time of the invoice
558
+ *
559
+ * @param pr
560
+ * @throws {DefinedRuntimeError} will throw an error if the pr is invalid, without amount or expired
561
+ */
562
+ private checkPaymentRequest(pr: string): {
563
+ parsedPR: bolt11.PaymentRequestObject & { tagsObject: bolt11.TagsObject },
564
+ halfConfidence: boolean
565
+ } {
566
+ let parsedPR: bolt11.PaymentRequestObject & { tagsObject: bolt11.TagsObject };
567
+
568
+ try {
569
+ parsedPR = bolt11.decode(pr);
570
+ } catch (e) {
571
+ throw {
572
+ code: 20021,
573
+ msg: "Invalid request body (pr - cannot be parsed)"
574
+ };
575
+ }
576
+
577
+ if(parsedPR.millisatoshis==null) throw {
578
+ code: 20022,
579
+ msg: "Invalid request body (pr - needs to have amount)"
580
+ };
581
+
582
+ let halfConfidence = false;
583
+ if(parsedPR.timeExpireDate < ((Date.now()/1000)+(this.config.authorizationTimeout+(2*60)))) {
584
+ if(!this.config.allowShortExpiry) {
585
+ throw {
586
+ code: 20020,
587
+ msg: "Invalid request body (pr - expired)"
588
+ };
589
+ } else if(parsedPR.timeExpireDate < Date.now()/1000) {
590
+ throw {
591
+ code: 20020,
592
+ msg: "Invalid request body (pr - expired)"
593
+ };
594
+ }
595
+ halfConfidence = true;
596
+ }
597
+
598
+ return {parsedPR, halfConfidence};
599
+ }
600
+
601
+ /**
602
+ * Checks if the request specified too short of an expiry
603
+ *
604
+ * @param expiryTimestamp
605
+ * @param currentTimestamp
606
+ * @throws {DefinedRuntimeError} will throw an error if the expiry time is too short
607
+ */
608
+ private checkExpiry(expiryTimestamp: BN, currentTimestamp: BN): void {
609
+ const expiresTooSoon = expiryTimestamp.sub(currentTimestamp).lt(this.config.minTsSendCltv);
610
+ if(expiresTooSoon) {
611
+ throw {
612
+ code: 20001,
613
+ msg: "Expiry time too low!"
614
+ };
615
+ }
616
+ }
617
+
618
+ /**
619
+ * Checks if the prior payment with the same paymentHash exists
620
+ *
621
+ * @param paymentHash
622
+ * @param abortSignal
623
+ * @throws {DefinedRuntimeError} will throw an error if payment already exists
624
+ */
625
+ private async checkPriorPayment(paymentHash: string, abortSignal: AbortSignal): Promise<void> {
626
+ const payment = await this.getPayment(paymentHash);
627
+ if(payment!=null) throw {
628
+ code: 20010,
629
+ msg: "Already processed"
630
+ };
631
+ abortSignal.throwIfAborted();
632
+ }
633
+
634
+ /**
635
+ * Checks if the underlying LND backend has enough liquidity in channels to honor the swap
636
+ *
637
+ * @param amount
638
+ * @param abortSignal
639
+ * @param useCached Whether to use cached liquidity values
640
+ * @throws {DefinedRuntimeError} will throw an error if there isn't enough liquidity
641
+ */
642
+ private async checkLiquidity(amount: BN, abortSignal: AbortSignal, useCached: boolean = false): Promise<void> {
643
+ const amountBDMtokens = amount.mul(new BN(1000));
644
+ if(!useCached || this.lightningLiquidityCache==null || this.lightningLiquidityCache.timestamp<Date.now()-this.LIGHTNING_LIQUIDITY_CACHE_TIMEOUT) {
645
+ const channelBalances = await lncli.getChannelBalance({lnd: this.LND});
646
+ this.lightningLiquidityCache = {
647
+ liquidityMTokens: new BN(channelBalances.channel_balance_mtokens),
648
+ timestamp: Date.now()
649
+ }
650
+ }
651
+ if(amountBDMtokens.gt(this.lightningLiquidityCache.liquidityMTokens)) {
652
+ throw {
653
+ code: 20002,
654
+ msg: "Not enough liquidity"
655
+ };
656
+ }
657
+ abortSignal.throwIfAborted();
658
+ }
659
+
660
+ /**
661
+ * Computes the route paying to the specified bolt11 invoice, estimating the fee, uses bLIP-39 blinded paths
662
+ *
663
+ * @param amountSats
664
+ * @param maxFee
665
+ * @param parsedRequest
666
+ * @param maxTimeoutBlockheight
667
+ * @param metadata
668
+ * @param maxUsableCLTV
669
+ * @private
670
+ */
671
+ private async getRoutesInvoiceBLIP39(
672
+ amountSats: BN,
673
+ maxFee: BN,
674
+ parsedRequest: {destination: string, cltv_delta: number, payment: string, routes: LNRoutes, blindedPaths?: BlindedPayInfo[]},
675
+ maxTimeoutBlockheight: BN,
676
+ metadata: any,
677
+ maxUsableCLTV: BN
678
+ ): Promise<ProbeAndRouteResponse> {
679
+ metadata.routeReq = [];
680
+ const routeReqs = parsedRequest.blindedPaths.map(async (blindedPath) => {
681
+ if(new BN(blindedPath.cltv_expiry_delta+10).gt(maxUsableCLTV)) return null;
682
+
683
+ const originalMsatAmount = amountSats.mul(new BN(1000));
684
+ const blindedFeeTotalMsat = new BN(blindedPath.fee_base_msat)
685
+ .add(originalMsatAmount.mul(new BN(blindedPath.fee_proportional_millionths)).div(new BN(1000000)));
686
+
687
+ const routeReq = {
688
+ destination: blindedPath.introduction_node,
689
+ cltv_delta: Math.max(blindedPath.cltv_expiry_delta, parsedRequest.cltv_delta),
690
+ mtokens: originalMsatAmount.add(blindedFeeTotalMsat).toString(10),
691
+ max_fee_mtokens: maxFee.mul(new BN(1000)).sub(blindedFeeTotalMsat).toString(10),
692
+ max_timeout_height: maxTimeoutBlockheight.toString(10),
693
+ // total_mtokens: amountSats.mul(new BN(1000)).toString(10),
694
+ routes: parsedRequest.routes,
695
+ is_ignoring_past_failures: true,
696
+ lnd: null
697
+ };
698
+ metadata.routeReq.push({...routeReq});
699
+ routeReq.lnd = this.LND;
700
+
701
+ let resp;
702
+ try {
703
+ resp = await lncli.getRouteToDestination(routeReq);
704
+ } catch (e) {
705
+ handleLndError(e);
706
+ }
707
+
708
+ if(resp==null || resp.route==null) return null;
709
+
710
+ const adjustedFeeMsats = new BN(resp.route.fee_mtokens).add(blindedFeeTotalMsat);
711
+ resp.route.fee_mtokens = adjustedFeeMsats.toString(10);
712
+ resp.route.fee = adjustedFeeMsats.div(new BN(1000)).toNumber();
713
+ resp.route.safe_fee = adjustedFeeMsats.add(new BN(999)).div(new BN(1000)).toNumber();
714
+ const totalAdjustedMsats = new BN(routeReq.mtokens).add(blindedFeeTotalMsat);
715
+ resp.route.mtokens = totalAdjustedMsats.toString(10);
716
+ resp.route.tokens = totalAdjustedMsats.div(new BN(1000)).toNumber();
717
+ resp.route.safe_tokens = totalAdjustedMsats.add(new BN(999)).div(new BN(1000)).toNumber();
718
+
719
+ return resp.route as ProbeAndRouteResponse;
720
+ });
721
+
722
+ const responses = await Promise.all(routeReqs);
723
+
724
+ metadata.routeResponsesBLIP39 = responses.map(resp => {return {...resp}});
725
+
726
+ return responses.reduce((prev, current) => {
727
+ if(prev==null) return current;
728
+ if(current==null) return prev;
729
+ current.fee_mtokens = BN.max(new BN(prev.fee_mtokens), new BN(current.fee_mtokens)).toString(10);
730
+ current.fee = Math.max(prev.fee, current.fee);
731
+ current.safe_fee = Math.max(prev.safe_fee, current.safe_fee);
732
+ current.mtokens = BN.max(new BN(prev.mtokens), new BN(current.mtokens)).toString(10);
733
+ current.tokens = Math.max(prev.tokens, current.tokens);
734
+ current.safe_tokens = Math.max(prev.safe_tokens, current.safe_tokens);
735
+ current.timeout = Math.max(prev.timeout, current.timeout);
736
+ return current;
737
+ });
738
+ }
739
+
740
+ /**
741
+ * Computes the route paying to the specified bolt11 invoice, estimating the fee
742
+ *
743
+ * @param amountSats
744
+ * @param maxFee
745
+ * @param parsedRequest
746
+ * @param maxTimeoutBlockheight
747
+ * @param metadata
748
+ * @param maxUsableCLTV
749
+ * @private
750
+ */
751
+ private async getRoutesInvoice(
752
+ amountSats: BN,
753
+ maxFee: BN,
754
+ parsedRequest: {destination: string, cltv_delta: number, payment: string, routes: LNRoutes, blindedPaths?: BlindedPayInfo[]},
755
+ maxTimeoutBlockheight: BN,
756
+ metadata: any,
757
+ maxUsableCLTV: BN
758
+ ): Promise<ProbeAndRouteResponse> {
759
+ if(parsedRequest.blindedPaths!=null && parsedRequest.blindedPaths.length>0)
760
+ return await this.getRoutesInvoiceBLIP39(amountSats, maxFee, parsedRequest, maxTimeoutBlockheight, metadata, maxUsableCLTV);
761
+
762
+ const routesReq: any = {
763
+ destination: parsedRequest.destination,
764
+ cltv_delta: parsedRequest.cltv_delta,
765
+ mtokens: amountSats.mul(new BN(1000)).toString(10),
766
+ max_fee_mtokens: maxFee.mul(new BN(1000)).toString(10),
767
+ payment: parsedRequest.payment,
768
+ max_timeout_height: maxTimeoutBlockheight.toString(10),
769
+ total_mtokens: amountSats.mul(new BN(1000)).toString(10),
770
+ routes: parsedRequest.routes,
771
+ is_ignoring_past_failures: true
772
+ };
773
+ metadata.routeReq = {...routesReq};
774
+ routesReq.lnd = this.LND;
775
+
776
+ let obj;
777
+ try {
778
+ obj = await lncli.getRouteToDestination(routesReq);
779
+ } catch (e) {
780
+ handleLndError(e);
781
+ }
782
+ return obj?.route==null ? null : obj.route;
783
+ }
784
+
785
+ /**
786
+ * Sends a probe payment to the specified bolt11 invoice to check if it is reachable
787
+ *
788
+ * @param amountSats
789
+ * @param maxFee
790
+ * @param parsedRequest
791
+ * @param maxTimeoutBlockheight
792
+ * @param metadata
793
+ * @private
794
+ */
795
+ private async probeInvoice(
796
+ amountSats: BN,
797
+ maxFee: BN,
798
+ parsedRequest: {destination: string, cltv_delta: number, payment: string, routes: LNRoutes},
799
+ maxTimeoutBlockheight: BN,
800
+ metadata: any
801
+ ): Promise<ProbeAndRouteResponse> {
802
+ const probeReq: any = {
803
+ destination: parsedRequest.destination,
804
+ cltv_delta: parsedRequest.cltv_delta,
805
+ mtokens: amountSats.mul(new BN(1000)).toString(10),
806
+ max_fee_mtokens: maxFee.mul(new BN(1000)).toString(10),
807
+ max_timeout_height: maxTimeoutBlockheight.toString(10),
808
+ payment: parsedRequest.payment,
809
+ total_mtokens: amountSats.mul(new BN(1000)).toString(10),
810
+ routes: parsedRequest.routes
811
+ };
812
+ metadata.probeRequest = {...probeReq};
813
+ probeReq.lnd = this.LND;
814
+
815
+ let is_snowflake: boolean = false;
816
+ if(parsedRequest.routes!=null) {
817
+ for(let route of parsedRequest.routes) {
818
+ if(SNOWFLAKE_LIST.has(route[0].public_key) || SNOWFLAKE_LIST.has(route[1].public_key)) {
819
+ is_snowflake = true;
820
+ }
821
+ }
822
+ }
823
+
824
+ let obj;
825
+ if(!is_snowflake) try {
826
+ obj = await lncli.probeForRoute(probeReq);
827
+ } catch (e) {
828
+ handleLndError(e);
829
+ }
830
+ return obj?.route==null ? null : obj.route;
831
+ }
832
+
833
+ /**
834
+ * Estimates the routing fee & confidence by either probing or routing (if probing fails), the fee is also adjusted
835
+ * according to routing fee multiplier, and subject to minimums set in config
836
+ *
837
+ * @param amountBD
838
+ * @param maxFee
839
+ * @param expiryTimestamp
840
+ * @param currentTimestamp
841
+ * @param pr
842
+ * @param metadata
843
+ * @param abortSignal
844
+ * @throws {DefinedRuntimeError} will throw an error if the destination is unreachable
845
+ */
846
+ private async checkAndGetNetworkFee(amountBD: BN, maxFee: BN, expiryTimestamp: BN, currentTimestamp: BN, pr: string, metadata: any, abortSignal: AbortSignal): Promise<{
847
+ confidence: number,
848
+ networkFee: BN,
849
+ routes: LNRoutes
850
+ }> {
851
+ const maxUsableCLTV: BN = expiryTimestamp.sub(currentTimestamp).sub(this.config.gracePeriod).div(this.config.bitcoinBlocktime.mul(this.config.safetyFactor));
852
+
853
+ const { current_block_height } = await lncli.getHeight({lnd: this.LND});
854
+ abortSignal.throwIfAborted();
855
+ metadata.times.blockheightFetched = Date.now();
856
+
857
+ const maxTimeoutBlockheight = new BN(current_block_height).add(maxUsableCLTV);
858
+ const parsedRequest = lncli.parsePaymentRequest({request: pr});
859
+ const bolt11Parsed = bolt11.decode(pr);
860
+ if(bolt11Parsed.tagsObject.blinded_payinfo!=null && bolt11Parsed.tagsObject.blinded_payinfo.length>0) {
861
+ parsedRequest.blindedPaths = bolt11Parsed.tagsObject.blinded_payinfo;
862
+ }
863
+
864
+ let probeOrRouteResp: ProbeAndRouteResponse;
865
+
866
+ if(parsedRequest.blindedPaths==null) {
867
+ probeOrRouteResp = await this.probeInvoice(amountBD, maxFee, parsedRequest, maxTimeoutBlockheight, metadata);
868
+ metadata.times.probeResult = Date.now();
869
+ metadata.probeResponse = {...probeOrRouteResp};
870
+ abortSignal.throwIfAborted();
871
+ }
872
+
873
+ if(probeOrRouteResp==null) {
874
+ if(!this.config.allowProbeFailedSwaps) throw {
875
+ code: 20002,
876
+ msg: "Cannot route the payment!"
877
+ };
878
+
879
+ const routeResp = await this.getRoutesInvoice(amountBD, maxFee, parsedRequest, maxTimeoutBlockheight, metadata, maxUsableCLTV);
880
+ metadata.times.routingResult = Date.now();
881
+ metadata.routeResponse = {...routeResp};
882
+ abortSignal.throwIfAborted();
883
+
884
+ if(routeResp==null) throw {
885
+ code: 20002,
886
+ msg: "Cannot route the payment!"
887
+ };
888
+
889
+ this.logger.info("checkAndGetNetworkFee(): routing result,"+
890
+ " destination: "+parsedRequest.destination+
891
+ " confidence: "+routeResp.confidence+
892
+ " safe fee: "+routeResp.safe_fee);
893
+
894
+ probeOrRouteResp = routeResp;
895
+ if(parsedRequest.blindedPaths==null) probeOrRouteResp.confidence = 0;
896
+ } else {
897
+ this.logger.info("checkAndGetNetworkFee(): route probed,"+
898
+ " destination: "+parsedRequest.destination+
899
+ " confidence: "+probeOrRouteResp.confidence+
900
+ " safe fee: "+probeOrRouteResp.safe_fee);
901
+ }
902
+
903
+ let actualRoutingFee: BN = new BN(probeOrRouteResp.safe_fee).mul(this.config.routingFeeMultiplier);
904
+
905
+ const minRoutingFee: BN = amountBD.mul(this.config.minLnRoutingFeePPM).div(new BN(1000000)).add(this.config.minLnBaseFee);
906
+ if(actualRoutingFee.lt(minRoutingFee)) {
907
+ actualRoutingFee = minRoutingFee;
908
+ if(actualRoutingFee.gt(maxFee)) {
909
+ probeOrRouteResp.confidence = 0;
910
+ }
911
+ }
912
+
913
+ if(actualRoutingFee.gt(maxFee)) {
914
+ actualRoutingFee = maxFee;
915
+ }
916
+
917
+ return {
918
+ networkFee: actualRoutingFee,
919
+ confidence: probeOrRouteResp.confidence,
920
+ routes: parsedRequest.routes
921
+ };
922
+ }
923
+
924
+ /**
925
+ * Checks and consumes (deletes & returns) exactIn authorizaton with a specific reqId
926
+ *
927
+ * @param reqId
928
+ * @throws {DefinedRuntimeError} will throw an error if the authorization doesn't exist
929
+ */
930
+ private checkExactInAuthorization(reqId: string): ExactInAuthorization {
931
+ const parsedAuth = this.exactInAuths[reqId];
932
+ if (parsedAuth==null) {
933
+ throw {
934
+ code: 20070,
935
+ msg: "Invalid reqId"
936
+ };
937
+ }
938
+ delete this.exactInAuths[reqId];
939
+ if(parsedAuth.expiry<Date.now()) {
940
+ throw {
941
+ code: 20200,
942
+ msg: "Authorization already expired!"
943
+ };
944
+ }
945
+ return parsedAuth;
946
+ }
947
+
948
+ /**
949
+ * Checks if the newly submitted PR has the same parameters (destination, cltv_delta, routes) as the initial dummy
950
+ * invoice sent for exactIn swap quote
951
+ *
952
+ * @param pr
953
+ * @param parsedAuth
954
+ * @throws {DefinedRuntimeError} will throw an error if the details don't match
955
+ */
956
+ private async checkPaymentRequestMatchesInitial(pr: string, parsedAuth: ExactInAuthorization): Promise<void> {
957
+ const parsedRequest = await lncli.parsePaymentRequest({
958
+ request: pr
959
+ });
960
+
961
+ if(
962
+ parsedRequest.destination!==parsedAuth.destination ||
963
+ parsedRequest.cltv_delta!==parsedAuth.cltvDelta ||
964
+ !new BN(parsedRequest.mtokens).eq(parsedAuth.amount.mul(new BN(1000)))
965
+ ) {
966
+ throw {
967
+ code: 20102,
968
+ msg: "Provided PR doesn't match initial!"
969
+ };
970
+ }
971
+
972
+ if(!routesMatch(parsedRequest.routes, parsedAuth.routes)) {
973
+ throw {
974
+ code: 20102,
975
+ msg: "Provided PR doesn't match initial (routes)!"
976
+ };
977
+ }
978
+ }
979
+
980
+ startRestServer(restServer: Express) {
981
+
982
+ restServer.use(this.path+"/payInvoiceExactIn", serverParamDecoder(10*1000));
983
+ restServer.post(this.path+"/payInvoiceExactIn", expressHandlerWrapper(async (req: Request & {paramReader: IParamReader}, res: Response & {responseStream: ServerParamEncoder}) => {
984
+ /**
985
+ * pr: string bolt11 lightning invoice
986
+ * reqId: string Identifier of the swap
987
+ * feeRate: string Fee rate to use for the init tx
988
+ */
989
+ const parsedBody = await req.paramReader.getParams({
990
+ pr: FieldTypeEnum.String,
991
+ reqId: FieldTypeEnum.String,
992
+ feeRate: FieldTypeEnum.String
993
+ });
994
+ if (parsedBody==null) {
995
+ throw {
996
+ code: 20100,
997
+ msg: "Invalid request body"
998
+ };
999
+ }
1000
+
1001
+ const responseStream = res.responseStream;
1002
+ const abortSignal = responseStream.getAbortSignal();
1003
+
1004
+ //Check request params
1005
+ const parsedAuth = this.checkExactInAuthorization(parsedBody.reqId);
1006
+ const {parsedPR, halfConfidence} = this.checkPaymentRequest(parsedBody.pr);
1007
+ await this.checkPaymentRequestMatchesInitial(parsedBody.pr, parsedAuth);
1008
+
1009
+ const metadata = parsedAuth.metadata;
1010
+
1011
+ const sequence = new BN(randomBytes(8));
1012
+
1013
+ const {swapContract, signer} = this.getChain(parsedAuth.chainIdentifier);
1014
+
1015
+ //Create swap data
1016
+ const payObject: SwapData = await swapContract.createSwapData(
1017
+ ChainSwapType.HTLC,
1018
+ parsedAuth.offerer,
1019
+ signer.getAddress(),
1020
+ parsedAuth.token,
1021
+ parsedAuth.total,
1022
+ parsedPR.tagsObject.payment_hash,
1023
+ sequence,
1024
+ parsedAuth.swapExpiry,
1025
+ new BN(0),
1026
+ 0,
1027
+ true,
1028
+ false,
1029
+ new BN(0),
1030
+ new BN(0)
1031
+ );
1032
+ metadata.times.swapCreated = Date.now();
1033
+
1034
+ //Sign swap data
1035
+ const prefetchedSignData = parsedAuth.preFetchSignData;
1036
+ const sigData = await this.getToBtcSignatureData(parsedAuth.chainIdentifier, payObject, req, abortSignal, prefetchedSignData);
1037
+ metadata.times.swapSigned = Date.now();
1038
+
1039
+ //Create swap
1040
+ const createdSwap = new ToBtcLnSwapAbs(
1041
+ parsedAuth.chainIdentifier,
1042
+ parsedBody.pr,
1043
+ parsedAuth.swapFee,
1044
+ parsedAuth.swapFeeInToken,
1045
+ parsedAuth.quotedNetworkFee,
1046
+ parsedAuth.quotedNetworkFeeInToken,
1047
+ new BN(sigData.timeout)
1048
+ );
1049
+ createdSwap.data = payObject;
1050
+ createdSwap.metadata = metadata;
1051
+
1052
+ await PluginManager.swapCreate(createdSwap);
1053
+ await this.storageManager.saveData(parsedPR.tagsObject.payment_hash, sequence, createdSwap);
1054
+
1055
+ this.swapLogger.info(createdSwap, "REST: /payInvoiceExactIn: created exact in swap,"+
1056
+ " reqId: "+parsedBody.reqId+
1057
+ " amount: "+new BN(parsedPR.millisatoshis).div(new BN(1000)).toString(10)+
1058
+ " invoice: "+createdSwap.pr);
1059
+
1060
+ await responseStream.writeParamsAndEnd({
1061
+ code: 20000,
1062
+ msg: "Success",
1063
+ data: {
1064
+ maxFee: parsedAuth.quotedNetworkFeeInToken.toString(10),
1065
+ swapFee: parsedAuth.swapFeeInToken.toString(10),
1066
+ total: parsedAuth.total.toString(10),
1067
+ confidence: halfConfidence ? parsedAuth.confidence/2000000 : parsedAuth.confidence/1000000,
1068
+ address: signer.getAddress(),
1069
+
1070
+ routingFeeSats: parsedAuth.quotedNetworkFee.toString(10),
1071
+
1072
+ data: payObject.serialize(),
1073
+
1074
+ prefix: sigData.prefix,
1075
+ timeout: sigData.timeout,
1076
+ signature: sigData.signature
1077
+ }
1078
+ });
1079
+
1080
+ }));
1081
+
1082
+ restServer.use(this.path+"/payInvoice", serverParamDecoder(10*1000));
1083
+ restServer.post(this.path+"/payInvoice", expressHandlerWrapper(async (req: Request & {paramReader: IParamReader}, res: Response & {responseStream: ServerParamEncoder}) => {
1084
+ const metadata: {
1085
+ request: any,
1086
+ probeRequest?: any,
1087
+ probeResponse?: any,
1088
+ routeResponse?: any,
1089
+ times: {[key: string]: number}
1090
+ } = {request: {}, times: {}};
1091
+
1092
+ const chainIdentifier = req.query.chain as string ?? this.chains.default;
1093
+ const {swapContract, signer} = this.getChain(chainIdentifier);
1094
+
1095
+ metadata.times.requestReceived = Date.now();
1096
+ /**
1097
+ *Sent initially:
1098
+ * pr: string bolt11 lightning invoice
1099
+ * maxFee: string maximum routing fee
1100
+ * expiryTimestamp: string expiry timestamp of the to be created HTLC, determines how many LN paths can be considered
1101
+ * token: string Desired token to use
1102
+ * offerer: string Address of the caller
1103
+ * exactIn: boolean Whether to do an exact in swap instead of exact out
1104
+ * amount: string Input amount for exactIn swaps
1105
+ *
1106
+ *Sent later:
1107
+ * feeRate: string Fee rate to use for the init signature
1108
+ */
1109
+ const parsedBody: ToBtcLnRequestType = await req.paramReader.getParams({
1110
+ pr: FieldTypeEnum.String,
1111
+ maxFee: FieldTypeEnum.BN,
1112
+ expiryTimestamp: FieldTypeEnum.BN,
1113
+ token: (val: string) => val!=null &&
1114
+ typeof(val)==="string" &&
1115
+ this.isTokenSupported(chainIdentifier, val) ? val : null,
1116
+ offerer: (val: string) => val!=null &&
1117
+ typeof(val)==="string" &&
1118
+ swapContract.isValidAddress(val) ? val : null,
1119
+ exactIn: FieldTypeEnum.BooleanOptional,
1120
+ amount: FieldTypeEnum.BNOptional
1121
+ });
1122
+ if (parsedBody==null) {
1123
+ throw {
1124
+ code: 20100,
1125
+ msg: "Invalid request body"
1126
+ };
1127
+ }
1128
+ metadata.request = parsedBody;
1129
+
1130
+ const request = {
1131
+ chainIdentifier,
1132
+ raw: req,
1133
+ parsed: parsedBody,
1134
+ metadata
1135
+ };
1136
+ const useToken = parsedBody.token;
1137
+
1138
+ const responseStream = res.responseStream;
1139
+
1140
+ const currentTimestamp: BN = new BN(Math.floor(Date.now()/1000));
1141
+
1142
+ //Check request params
1143
+ this.checkAmount(parsedBody.amount, parsedBody.exactIn);
1144
+ this.checkMaxFee(parsedBody.maxFee);
1145
+ this.checkExpiry(parsedBody.expiryTimestamp, currentTimestamp);
1146
+ await this.checkVaultInitialized(chainIdentifier, parsedBody.token);
1147
+ const {parsedPR, halfConfidence} = this.checkPaymentRequest(parsedBody.pr);
1148
+ const requestedAmount = {
1149
+ input: !!parsedBody.exactIn,
1150
+ amount: !!parsedBody.exactIn ? parsedBody.amount : new BN(parsedPR.millisatoshis).add(new BN(999)).div(new BN(1000))
1151
+ };
1152
+ const fees = await this.preCheckAmounts(request, requestedAmount, useToken);
1153
+ metadata.times.requestChecked = Date.now();
1154
+
1155
+ //Create abort controller for parallel pre-fetches
1156
+ const abortController = this.getAbortController(responseStream);
1157
+
1158
+ //Pre-fetch
1159
+ const {pricePrefetchPromise, signDataPrefetchPromise} = this.getToBtcPrefetches(chainIdentifier, useToken, responseStream, abortController);
1160
+
1161
+ //Check if prior payment has been made
1162
+ await this.checkPriorPayment(parsedPR.tagsObject.payment_hash, abortController.signal);
1163
+ metadata.times.priorPaymentChecked = Date.now();
1164
+
1165
+ //Check amounts
1166
+ const {
1167
+ amountBD,
1168
+ networkFeeData,
1169
+ totalInToken,
1170
+ swapFee,
1171
+ swapFeeInToken,
1172
+ networkFeeInToken
1173
+ } = await this.checkToBtcAmount(request, requestedAmount, fees, useToken, async (amountBD: BN) => {
1174
+ //Check if we have enough liquidity to process the swap
1175
+ await this.checkLiquidity(amountBD, abortController.signal, true);
1176
+ metadata.times.liquidityChecked = Date.now();
1177
+
1178
+ const maxFee = parsedBody.exactIn ?
1179
+ await this.swapPricing.getToBtcSwapAmount(parsedBody.maxFee, useToken, chainIdentifier, null, pricePrefetchPromise) :
1180
+ parsedBody.maxFee;
1181
+
1182
+ return await this.checkAndGetNetworkFee(amountBD, maxFee, parsedBody.expiryTimestamp, currentTimestamp, parsedBody.pr, metadata, abortController.signal);
1183
+ }, abortController.signal, pricePrefetchPromise);
1184
+ metadata.times.priceCalculated = Date.now();
1185
+
1186
+ //For exactIn swap, just save and wait for the actual invoice to be submitted
1187
+ if(parsedBody.exactIn) {
1188
+ const reqId = randomBytes(32).toString("hex");
1189
+ this.exactInAuths[reqId] = {
1190
+ chainIdentifier,
1191
+ reqId,
1192
+ expiry: Date.now() + this.config.exactInExpiry,
1193
+
1194
+ amount: amountBD,
1195
+ destination: parsedPR.payeeNodeKey,
1196
+ cltvDelta: parsedPR.tagsObject.min_final_cltv_expiry,
1197
+ routes: networkFeeData.routes,
1198
+
1199
+ quotedNetworkFeeInToken: networkFeeInToken,
1200
+ swapFeeInToken,
1201
+ total: totalInToken,
1202
+ confidence: networkFeeData.confidence,
1203
+ quotedNetworkFee: networkFeeData.networkFee,
1204
+ swapFee,
1205
+
1206
+ token: useToken,
1207
+ swapExpiry: parsedBody.expiryTimestamp,
1208
+ offerer: parsedBody.offerer,
1209
+
1210
+ preFetchSignData: signDataPrefetchPromise != null ? await signDataPrefetchPromise : null,
1211
+ metadata
1212
+ };
1213
+
1214
+ this.logger.info("REST: /payInvoice: created exact in swap,"+
1215
+ " reqId: "+reqId+
1216
+ " amount: "+amountBD.toString(10)+
1217
+ " destination: "+parsedPR.payeeNodeKey);
1218
+
1219
+ await responseStream.writeParamsAndEnd({
1220
+ code: 20000,
1221
+ msg: "Success",
1222
+ data: {
1223
+ amount: amountBD.toString(10),
1224
+ reqId
1225
+ }
1226
+ });
1227
+ return;
1228
+ }
1229
+
1230
+ const sequence = new BN(randomBytes(8));
1231
+
1232
+ //Create swap data
1233
+ const payObject: SwapData = await swapContract.createSwapData(
1234
+ ChainSwapType.HTLC,
1235
+ parsedBody.offerer,
1236
+ signer.getAddress(),
1237
+ useToken,
1238
+ totalInToken,
1239
+ parsedPR.tagsObject.payment_hash,
1240
+ sequence,
1241
+ parsedBody.expiryTimestamp,
1242
+ new BN(0),
1243
+ 0,
1244
+ true,
1245
+ false,
1246
+ new BN(0),
1247
+ new BN(0)
1248
+ );
1249
+ abortController.signal.throwIfAborted();
1250
+ metadata.times.swapCreated = Date.now();
1251
+
1252
+ //Sign swap data
1253
+ const sigData = await this.getToBtcSignatureData(chainIdentifier, payObject, req, abortController.signal, signDataPrefetchPromise);
1254
+ metadata.times.swapSigned = Date.now();
1255
+
1256
+ //Create swap
1257
+ const createdSwap = new ToBtcLnSwapAbs(chainIdentifier, parsedBody.pr, swapFee, swapFeeInToken, networkFeeData.networkFee, networkFeeInToken, new BN(sigData.timeout));
1258
+ createdSwap.data = payObject;
1259
+ createdSwap.metadata = metadata;
1260
+
1261
+ await PluginManager.swapCreate(createdSwap);
1262
+ await this.storageManager.saveData(parsedPR.tagsObject.payment_hash, sequence, createdSwap);
1263
+
1264
+ this.swapLogger.info(createdSwap, "REST: /payInvoice: created swap,"+
1265
+ " amount: "+amountBD.toString(10)+
1266
+ " invoice: "+createdSwap.pr);
1267
+
1268
+ await responseStream.writeParamsAndEnd({
1269
+ code: 20000,
1270
+ msg: "Success",
1271
+ data: {
1272
+ maxFee: networkFeeInToken.toString(10),
1273
+ swapFee: swapFeeInToken.toString(10),
1274
+ total: totalInToken.toString(10),
1275
+ confidence: halfConfidence ? networkFeeData.confidence/2000000 : networkFeeData.confidence/1000000,
1276
+ address: signer.getAddress(),
1277
+
1278
+ routingFeeSats: networkFeeData.networkFee.toString(10),
1279
+
1280
+ data: payObject.serialize(),
1281
+
1282
+ prefix: sigData.prefix,
1283
+ timeout: sigData.timeout,
1284
+ signature: sigData.signature
1285
+ }
1286
+ });
1287
+ }));
1288
+
1289
+ const getRefundAuthorization = expressHandlerWrapper(async (req, res) => {
1290
+ /**
1291
+ * paymentHash: string Identifier of the swap
1292
+ * sequence: BN Sequence identifier of the swap
1293
+ */
1294
+ const parsedBody = verifySchema({...req.body, ...req.query}, {
1295
+ paymentHash: (val: string) => val!=null &&
1296
+ typeof(val)==="string" &&
1297
+ val.length===64 &&
1298
+ HEX_REGEX.test(val) ? val: null,
1299
+ sequence: FieldTypeEnum.BN
1300
+ });
1301
+ if (parsedBody==null) throw {
1302
+ code: 20100,
1303
+ msg: "Invalid request body/query (paymentHash/sequence)"
1304
+ };
1305
+
1306
+ this.checkSequence(parsedBody.sequence);
1307
+
1308
+ const data = await this.storageManager.getData(parsedBody.paymentHash, parsedBody.sequence);
1309
+
1310
+ const isSwapFound = data!=null;
1311
+ if(isSwapFound) {
1312
+ const isExpired = data.data.getExpiry().lt(new BN(Math.floor(Date.now()/1000)).sub(new BN(this.config.maxSkew)));
1313
+ if(isExpired) throw {
1314
+ _httpStatus: 200,
1315
+ code: 20010,
1316
+ msg: "Payment expired"
1317
+ };
1318
+
1319
+ const {signer, swapContract} = this.getChain(data.chainIdentifier);
1320
+ if(data.state===ToBtcLnSwapState.NON_PAYABLE) {
1321
+ const refundSigData = await swapContract.getRefundSignature(signer, data.data, this.config.authorizationTimeout);
1322
+
1323
+ //Double check the state after promise result
1324
+ if (data.state !== ToBtcLnSwapState.NON_PAYABLE) throw {
1325
+ code: 20005,
1326
+ msg: "Not committed"
1327
+ };
1328
+
1329
+ this.swapLogger.info(data, "REST: /getRefundAuthorization: returning refund authorization, because invoice in NON_PAYABLE state, invoice: "+data.pr);
1330
+
1331
+ res.status(200).json({
1332
+ code: 20000,
1333
+ msg: "Success",
1334
+ data: {
1335
+ address: signer.getAddress(),
1336
+ prefix: refundSigData.prefix,
1337
+ timeout: refundSigData.timeout,
1338
+ signature: refundSigData.signature
1339
+ }
1340
+ });
1341
+ return;
1342
+ }
1343
+ }
1344
+
1345
+ const payment = await this.getPayment(parsedBody.paymentHash);
1346
+
1347
+ if(payment==null) throw {
1348
+ _httpStatus: 200,
1349
+ code: 20007,
1350
+ msg: "Payment not found"
1351
+ };
1352
+
1353
+ if(payment.is_pending) throw {
1354
+ _httpStatus: 200,
1355
+ code: 20008,
1356
+ msg: "Payment in-flight"
1357
+ };
1358
+
1359
+ if(payment.is_confirmed) throw {
1360
+ _httpStatus: 200,
1361
+ code: 20006,
1362
+ msg: "Already paid",
1363
+ data: {
1364
+ secret: payment.payment.secret
1365
+ }
1366
+ };
1367
+
1368
+ if(payment.is_failed) throw {
1369
+ _httpStatus: 200,
1370
+ code: 20010,
1371
+ msg: "Payment expired"
1372
+ };
1373
+
1374
+ // NOTE: Fixed by not removing swap data until the HTLC is either expired, claimed or refunded.
1375
+ // //TODO_old: Fix this by providing chain identifier as part of the invoice description, or maybe just do it the proper
1376
+ // // way and just keep storing the data until the HTLC expiry
1377
+ // if(payment.is_failed) {
1378
+ // //TODO_old: This might not be the best idea with EVM chains
1379
+ // const commitedData = await this.swapContract.getCommitedData(parsedBody.paymentHash);
1380
+ //
1381
+ // if(commitedData==null) throw {
1382
+ // code: 20005,
1383
+ // msg: "Not committed"
1384
+ // };
1385
+ //
1386
+ // const refundSigData = await this.swapContract.getRefundSignature(commitedData, this.config.authorizationTimeout);
1387
+ //
1388
+ // this.swapLogger.info(commitedData, "REST: /getRefundAuthorization: returning refund authorization, because invoice payment failed");
1389
+ //
1390
+ // res.status(200).json({
1391
+ // code: 20000,
1392
+ // msg: "Success",
1393
+ // data: {
1394
+ // address: this.swapContract.getAddress(),
1395
+ // prefix: refundSigData.prefix,
1396
+ // timeout: refundSigData.timeout,
1397
+ // signature: refundSigData.signature
1398
+ // }
1399
+ // });
1400
+ // }
1401
+ });
1402
+
1403
+ restServer.post(this.path+'/getRefundAuthorization', getRefundAuthorization);
1404
+ restServer.get(this.path+'/getRefundAuthorization', getRefundAuthorization);
1405
+
1406
+ this.logger.info("started at path: ", this.path);
1407
+ }
1408
+
1409
+ async init() {
1410
+ await this.storageManager.loadData(ToBtcLnSwapAbs);
1411
+ this.subscribeToEvents();
1412
+ await PluginManager.serviceInitialize(this);
1413
+ }
1414
+
1415
+ getInfoData(): any {
1416
+ return {
1417
+ minCltv: this.config.minSendCltv.toNumber(),
1418
+ minTimestampCltv: this.config.minTsSendCltv.toNumber()
1419
+ };
1420
+ }
1421
+
1422
+ }