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