@atomiqlabs/lp-lib 14.0.0-dev.21 → 14.0.0-dev.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/LICENSE +201 -201
  2. package/dist/fees/IBtcFeeEstimator.d.ts +3 -3
  3. package/dist/fees/IBtcFeeEstimator.js +2 -2
  4. package/dist/index.d.ts +42 -42
  5. package/dist/index.js +58 -58
  6. package/dist/info/InfoHandler.d.ts +17 -17
  7. package/dist/info/InfoHandler.js +61 -61
  8. package/dist/plugins/IPlugin.d.ts +143 -143
  9. package/dist/plugins/IPlugin.js +34 -34
  10. package/dist/plugins/PluginManager.d.ts +112 -112
  11. package/dist/plugins/PluginManager.js +259 -259
  12. package/dist/prices/BinanceSwapPrice.d.ts +26 -26
  13. package/dist/prices/BinanceSwapPrice.js +92 -92
  14. package/dist/prices/CoinGeckoSwapPrice.d.ts +30 -30
  15. package/dist/prices/CoinGeckoSwapPrice.js +64 -64
  16. package/dist/prices/ISwapPrice.d.ts +43 -43
  17. package/dist/prices/ISwapPrice.js +55 -55
  18. package/dist/prices/OKXSwapPrice.d.ts +26 -26
  19. package/dist/prices/OKXSwapPrice.js +92 -92
  20. package/dist/storage/IIntermediaryStorage.d.ts +18 -18
  21. package/dist/storage/IIntermediaryStorage.js +2 -2
  22. package/dist/storagemanager/IntermediaryStorageManager.d.ts +19 -19
  23. package/dist/storagemanager/IntermediaryStorageManager.js +111 -111
  24. package/dist/storagemanager/StorageManager.d.ts +13 -13
  25. package/dist/storagemanager/StorageManager.js +64 -64
  26. package/dist/swaps/SwapHandler.d.ts +153 -153
  27. package/dist/swaps/SwapHandler.js +160 -160
  28. package/dist/swaps/SwapHandlerSwap.d.ts +79 -79
  29. package/dist/swaps/SwapHandlerSwap.js +78 -78
  30. package/dist/swaps/assertions/AmountAssertions.d.ts +28 -28
  31. package/dist/swaps/assertions/AmountAssertions.js +72 -72
  32. package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -76
  33. package/dist/swaps/assertions/FromBtcAmountAssertions.js +180 -180
  34. package/dist/swaps/assertions/LightningAssertions.d.ts +44 -44
  35. package/dist/swaps/assertions/LightningAssertions.js +86 -86
  36. package/dist/swaps/assertions/ToBtcAmountAssertions.d.ts +53 -53
  37. package/dist/swaps/assertions/ToBtcAmountAssertions.js +150 -150
  38. package/dist/swaps/escrow/EscrowHandler.d.ts +51 -51
  39. package/dist/swaps/escrow/EscrowHandler.js +158 -158
  40. package/dist/swaps/escrow/EscrowHandlerSwap.d.ts +35 -35
  41. package/dist/swaps/escrow/EscrowHandlerSwap.js +69 -69
  42. package/dist/swaps/escrow/FromBtcBaseSwap.d.ts +14 -14
  43. package/dist/swaps/escrow/FromBtcBaseSwap.js +32 -32
  44. package/dist/swaps/escrow/FromBtcBaseSwapHandler.d.ts +102 -102
  45. package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +210 -210
  46. package/dist/swaps/escrow/ToBtcBaseSwap.d.ts +36 -36
  47. package/dist/swaps/escrow/ToBtcBaseSwap.js +67 -67
  48. package/dist/swaps/escrow/ToBtcBaseSwapHandler.d.ts +53 -53
  49. package/dist/swaps/escrow/ToBtcBaseSwapHandler.js +81 -81
  50. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.d.ts +83 -83
  51. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +318 -318
  52. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.d.ts +21 -21
  53. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.js +50 -50
  54. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.d.ts +107 -107
  55. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +675 -675
  56. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +33 -33
  57. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +91 -91
  58. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +111 -111
  59. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +684 -684
  60. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +55 -55
  61. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +120 -120
  62. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +171 -171
  63. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +706 -706
  64. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +26 -26
  65. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +62 -62
  66. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +177 -177
  67. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +863 -863
  68. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +24 -24
  69. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +58 -58
  70. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +40 -41
  71. package/dist/swaps/spv_vault_swap/SpvVault.js +111 -111
  72. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +67 -67
  73. package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +158 -158
  74. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -68
  75. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +492 -492
  76. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +52 -52
  77. package/dist/swaps/spv_vault_swap/SpvVaults.js +386 -364
  78. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +51 -51
  79. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +650 -650
  80. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.d.ts +52 -52
  81. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.js +118 -118
  82. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +76 -76
  83. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +494 -494
  84. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
  85. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
  86. package/dist/utils/BitcoinUtils.d.ts +2 -2
  87. package/dist/utils/BitcoinUtils.js +45 -45
  88. package/dist/utils/Utils.d.ts +29 -29
  89. package/dist/utils/Utils.js +89 -89
  90. package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
  91. package/dist/utils/paramcoders/IParamReader.js +2 -2
  92. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
  93. package/dist/utils/paramcoders/IParamWriter.js +2 -2
  94. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
  95. package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
  96. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
  97. package/dist/utils/paramcoders/ParamDecoder.js +222 -222
  98. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
  99. package/dist/utils/paramcoders/ParamEncoder.js +22 -22
  100. package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
  101. package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
  102. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
  103. package/dist/utils/paramcoders/server/ServerParamDecoder.js +107 -107
  104. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
  105. package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
  106. package/dist/wallets/IBitcoinWallet.d.ts +67 -67
  107. package/dist/wallets/IBitcoinWallet.js +2 -2
  108. package/dist/wallets/ILightningWallet.d.ts +117 -117
  109. package/dist/wallets/ILightningWallet.js +37 -37
  110. package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
  111. package/dist/wallets/ISpvVaultSigner.js +2 -2
  112. package/package.json +36 -36
  113. package/src/fees/IBtcFeeEstimator.ts +6 -6
  114. package/src/index.ts +53 -53
  115. package/src/info/InfoHandler.ts +106 -106
  116. package/src/plugins/IPlugin.ts +168 -168
  117. package/src/plugins/PluginManager.ts +336 -336
  118. package/src/prices/BinanceSwapPrice.ts +113 -113
  119. package/src/prices/CoinGeckoSwapPrice.ts +87 -87
  120. package/src/prices/ISwapPrice.ts +88 -88
  121. package/src/prices/OKXSwapPrice.ts +113 -113
  122. package/src/storage/IIntermediaryStorage.ts +19 -19
  123. package/src/storagemanager/IntermediaryStorageManager.ts +118 -118
  124. package/src/storagemanager/StorageManager.ts +78 -78
  125. package/src/swaps/SwapHandler.ts +277 -277
  126. package/src/swaps/SwapHandlerSwap.ts +141 -141
  127. package/src/swaps/assertions/AmountAssertions.ts +76 -76
  128. package/src/swaps/assertions/FromBtcAmountAssertions.ts +246 -246
  129. package/src/swaps/assertions/LightningAssertions.ts +103 -103
  130. package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
  131. package/src/swaps/escrow/EscrowHandler.ts +179 -179
  132. package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
  133. package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
  134. package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +286 -286
  135. package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
  136. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
  137. package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +452 -452
  138. package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
  139. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +856 -856
  140. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -141
  141. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +850 -850
  142. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -196
  143. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +879 -879
  144. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +102 -102
  145. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1112 -1112
  146. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
  147. package/src/swaps/spv_vault_swap/SpvVault.ts +143 -143
  148. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +225 -225
  149. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +628 -628
  150. package/src/swaps/spv_vault_swap/SpvVaults.ts +458 -435
  151. package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +747 -747
  152. package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
  153. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +590 -590
  154. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  155. package/src/utils/BitcoinUtils.ts +42 -42
  156. package/src/utils/Utils.ts +104 -104
  157. package/src/utils/paramcoders/IParamReader.ts +7 -7
  158. package/src/utils/paramcoders/IParamWriter.ts +8 -8
  159. package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
  160. package/src/utils/paramcoders/ParamDecoder.ts +218 -218
  161. package/src/utils/paramcoders/ParamEncoder.ts +29 -29
  162. package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
  163. package/src/utils/paramcoders/server/ServerParamDecoder.ts +118 -118
  164. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  165. package/src/wallets/IBitcoinWallet.ts +68 -68
  166. package/src/wallets/ILightningWallet.ts +178 -178
  167. package/src/wallets/ISpvVaultSigner.ts +10 -10
@@ -1,863 +1,863 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ToBtcLnAbs = void 0;
4
- const ToBtcLnSwapAbs_1 = require("./ToBtcLnSwapAbs");
5
- const SwapHandler_1 = require("../../SwapHandler");
6
- const base_1 = require("@atomiqlabs/base");
7
- const Utils_1 = require("../../../utils/Utils");
8
- const PluginManager_1 = require("../../../plugins/PluginManager");
9
- const crypto_1 = require("crypto");
10
- const ServerParamDecoder_1 = require("../../../utils/paramcoders/server/ServerParamDecoder");
11
- const SchemaVerifier_1 = require("../../../utils/paramcoders/SchemaVerifier");
12
- const ToBtcBaseSwapHandler_1 = require("../ToBtcBaseSwapHandler");
13
- const ILightningWallet_1 = require("../../../wallets/ILightningWallet");
14
- const LightningAssertions_1 = require("../../assertions/LightningAssertions");
15
- /**
16
- * Swap handler handling to BTCLN swaps using submarine swaps
17
- */
18
- class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
19
- constructor(storageDirectory, path, chainData, lightning, swapPricing, config) {
20
- super(storageDirectory, path, chainData, swapPricing, config);
21
- this.activeSubscriptions = new Set();
22
- this.type = SwapHandler_1.SwapHandlerType.TO_BTCLN;
23
- this.swapType = base_1.ChainSwapType.HTLC;
24
- this.exactInAuths = {};
25
- this.lightning = lightning;
26
- this.LightningAssertions = new LightningAssertions_1.LightningAssertions(this.logger, lightning);
27
- const anyConfig = config;
28
- anyConfig.minTsSendCltv = config.gracePeriod + (config.bitcoinBlocktime * config.minSendCltv * config.safetyFactor);
29
- this.config = anyConfig;
30
- this.config.minLnRoutingFeePPM = this.config.minLnRoutingFeePPM || 1000n;
31
- this.config.minLnBaseFee = this.config.minLnBaseFee || 5n;
32
- this.config.exactInExpiry = this.config.exactInExpiry || 10 * 1000;
33
- }
34
- /**
35
- * Cleans up exactIn authorization that are already past their expiry
36
- *
37
- * @protected
38
- */
39
- cleanExpiredExactInAuthorizations() {
40
- for (let key in this.exactInAuths) {
41
- const obj = this.exactInAuths[key];
42
- if (obj.expiry < Date.now()) {
43
- this.logger.info("cleanExpiredExactInAuthorizations(): remove expired authorization, reqId: " + key);
44
- delete this.exactInAuths[key];
45
- }
46
- }
47
- }
48
- async processPastSwap(swap) {
49
- const { swapContract, signer } = this.getChain(swap.chainIdentifier);
50
- if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED) {
51
- //Cancel the swaps where signature is expired
52
- const isSignatureExpired = await swapContract.isInitAuthorizationExpired(swap.data, swap);
53
- if (isSignatureExpired) {
54
- const isCommitted = await swapContract.isCommited(swap.data);
55
- if (!isCommitted) {
56
- this.swapLogger.info(swap, "processPastSwap(state=SAVED): authorization expired & swap not committed, cancelling swap, invoice: " + swap.pr);
57
- await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CANCELED);
58
- return;
59
- }
60
- else {
61
- this.swapLogger.info(swap, "processPastSwap(state=SAVED): swap committed (detected from processPastSwap), invoice: " + swap.pr);
62
- await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED);
63
- await this.saveSwapData(swap);
64
- }
65
- }
66
- //Cancel the swaps where lightning invoice is expired
67
- const decodedPR = await this.lightning.parsePaymentRequest(swap.pr);
68
- const isInvoiceExpired = decodedPR.expiryEpochMillis < Date.now();
69
- if (isInvoiceExpired) {
70
- this.swapLogger.info(swap, "processPastSwap(state=SAVED): invoice expired, cancel uncommited swap, invoice: " + swap.pr);
71
- await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CANCELED);
72
- return;
73
- }
74
- }
75
- if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED || swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.PAID) {
76
- //Process swaps in commited & paid state
77
- await this.processInitialized(swap);
78
- }
79
- if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE) {
80
- //Remove expired swaps (as these can already be unilaterally refunded by the client), so we don't need
81
- // to be able to cooperatively refund them
82
- if (await swapContract.isExpired(signer.getAddress(), swap.data)) {
83
- this.swapLogger.info(swap, "processPastSwap(state=NON_PAYABLE): swap expired, removing swap data, invoice: " + swap.pr);
84
- await this.removeSwapData(swap);
85
- }
86
- }
87
- }
88
- /**
89
- * Checks past swaps, deletes ones that are already expired, and tries to process ones that are committed.
90
- */
91
- async processPastSwaps() {
92
- this.cleanExpiredExactInAuthorizations();
93
- const queriedData = await this.storageManager.query([
94
- {
95
- key: "state",
96
- value: [
97
- ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED,
98
- ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED,
99
- ToBtcLnSwapAbs_1.ToBtcLnSwapState.PAID,
100
- ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE
101
- ]
102
- }
103
- ]);
104
- for (let { obj: swap } of queriedData) {
105
- await this.processPastSwap(swap);
106
- }
107
- }
108
- /**
109
- * Tries to claim the swap funds on the SC side, returns false if the swap is already locked (claim tx is already being sent)
110
- *
111
- * @param swap
112
- * @private
113
- * @returns Whether the transaction was successfully sent
114
- */
115
- async tryClaimSwap(swap) {
116
- if (swap.secret == null)
117
- throw new Error("Invalid swap state, needs payment pre-image!");
118
- const { swapContract, signer } = this.getChain(swap.chainIdentifier);
119
- //Check if escrow state exists
120
- const isCommited = await swapContract.isCommited(swap.data);
121
- if (!isCommited) {
122
- const status = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
123
- if (status?.type === base_1.SwapCommitStateType.PAID) {
124
- //This is alright, we got the money
125
- swap.txIds ?? (swap.txIds = {});
126
- swap.txIds.claim = await status.getClaimTxId();
127
- await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CLAIMED);
128
- return true;
129
- }
130
- else if (status?.type === base_1.SwapCommitStateType.EXPIRED) {
131
- //This means the user was able to refund before we were able to claim, no good
132
- swap.txIds ?? (swap.txIds = {});
133
- swap.txIds.refund = status.getRefundTxId == null ? null : await status.getRefundTxId();
134
- await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.REFUNDED);
135
- }
136
- this.swapLogger.warn(swap, "processPaymentResult(): tried to claim but escrow doesn't exist anymore," +
137
- " status: " + status +
138
- " invoice: " + swap.pr);
139
- return false;
140
- }
141
- //Set flag that we are sending the transaction already, so we don't end up with race condition
142
- const unlock = swap.lock(swapContract.claimWithSecretTimeout);
143
- if (unlock == null)
144
- return false;
145
- try {
146
- this.swapLogger.debug(swap, "tryClaimSwap(): initiate claim of swap, secret: " + swap.secret);
147
- const success = await swapContract.claimWithSecret(signer, swap.data, swap.secret, false, false, {
148
- waitForConfirmation: true
149
- });
150
- this.swapLogger.info(swap, "tryClaimSwap(): swap claimed successfully, secret: " + swap.secret + " invoice: " + swap.pr);
151
- if (swap.metadata != null)
152
- swap.metadata.times.txClaimed = Date.now();
153
- unlock();
154
- return true;
155
- }
156
- catch (e) {
157
- this.swapLogger.error(swap, "tryClaimSwap(): error occurred claiming swap, secret: " + swap.secret + " invoice: " + swap.pr, e);
158
- return false;
159
- }
160
- }
161
- /**
162
- * Process the result of attempted lightning network payment
163
- *
164
- * @param swap
165
- * @param lnPaymentStatus
166
- */
167
- async processPaymentResult(swap, lnPaymentStatus) {
168
- switch (lnPaymentStatus.status) {
169
- case "pending":
170
- return;
171
- case "failed":
172
- this.swapLogger.info(swap, "processPaymentResult(): invoice payment failed, cancelling swap, invoice: " + swap.pr);
173
- await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE);
174
- await this.saveSwapData(swap);
175
- return;
176
- case "confirmed":
177
- swap.secret = lnPaymentStatus.secret;
178
- swap.setRealNetworkFee(lnPaymentStatus.feeMtokens / 1000n);
179
- this.swapLogger.info(swap, "processPaymentResult(): invoice paid, secret: " + swap.secret + " realRoutingFee: " + swap.realNetworkFee.toString(10) + " invoice: " + swap.pr);
180
- await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.PAID);
181
- await this.saveSwapData(swap);
182
- const success = await this.tryClaimSwap(swap);
183
- if (success)
184
- this.swapLogger.info(swap, "processPaymentResult(): swap claimed successfully, invoice: " + swap.pr);
185
- return;
186
- default:
187
- throw new Error("Invalid lnPaymentStatus");
188
- }
189
- }
190
- /**
191
- * Subscribe to a pending lightning network payment attempt
192
- *
193
- * @param invoiceData
194
- */
195
- subscribeToPayment(invoiceData) {
196
- const paymentHash = invoiceData.lnPaymentHash;
197
- if (this.activeSubscriptions.has(paymentHash))
198
- return false;
199
- this.lightning.waitForPayment(paymentHash).then(result => {
200
- this.swapLogger.info(invoiceData, "subscribeToPayment(): result callback, outcome: " + result.status + " invoice: " + invoiceData.pr);
201
- this.processPaymentResult(invoiceData, result).catch(e => this.swapLogger.error(invoiceData, "subscribeToPayment(): process payment result", e));
202
- this.activeSubscriptions.delete(paymentHash);
203
- });
204
- this.swapLogger.info(invoiceData, "subscribeToPayment(): subscribe to payment outcome, invoice: " + invoiceData.pr);
205
- this.activeSubscriptions.add(paymentHash);
206
- return true;
207
- }
208
- async sendLightningPayment(swap) {
209
- const decodedPR = await this.lightning.parsePaymentRequest(swap.pr);
210
- const expiryTimestamp = swap.data.getExpiry();
211
- const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
212
- //Run checks
213
- const hasEnoughTimeToPay = (expiryTimestamp - currentTimestamp) >= this.config.minTsSendCltv;
214
- if (!hasEnoughTimeToPay)
215
- throw {
216
- code: 90005,
217
- msg: "Not enough time to reliably pay the invoice"
218
- };
219
- const isInvoiceExpired = decodedPR.expiryEpochMillis < Date.now();
220
- if (isInvoiceExpired)
221
- throw {
222
- code: 90006,
223
- msg: "Invoice already expired"
224
- };
225
- //Compute max cltv delta
226
- const maxFee = swap.quotedNetworkFee;
227
- const maxUsableCLTVdelta = (expiryTimestamp - currentTimestamp - this.config.gracePeriod)
228
- / (this.config.bitcoinBlocktime * this.config.safetyFactor);
229
- //Initiate payment
230
- this.swapLogger.info(swap, "sendLightningPayment(): paying lightning network invoice," +
231
- " cltvDelta: " + maxUsableCLTVdelta.toString(10) +
232
- " maxFee: " + maxFee.toString(10) +
233
- " invoice: " + swap.pr);
234
- const blockHeight = await this.lightning.getBlockheight();
235
- swap.payInitiated = true;
236
- await this.saveSwapData(swap);
237
- try {
238
- await this.lightning.pay({
239
- request: swap.pr,
240
- maxFeeMtokens: maxFee * 1000n,
241
- maxTimeoutHeight: blockHeight + Number(maxUsableCLTVdelta)
242
- });
243
- }
244
- catch (e) {
245
- throw {
246
- code: 90007,
247
- msg: "Failed to initiate invoice payment",
248
- data: {
249
- error: JSON.stringify(e)
250
- }
251
- };
252
- }
253
- if (swap.metadata != null)
254
- swap.metadata.times.payComplete = Date.now();
255
- }
256
- /**
257
- * Begins a lightning network payment attempt, if not attempted already
258
- *
259
- * @param swap
260
- */
261
- async processInitialized(swap) {
262
- //Check if payment was already made
263
- if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.PAID) {
264
- const success = await this.tryClaimSwap(swap);
265
- if (success)
266
- this.swapLogger.info(swap, "processInitialized(): swap claimed successfully, invoice: " + swap.pr);
267
- return;
268
- }
269
- if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED) {
270
- if (swap.metadata != null)
271
- swap.metadata.times.payPaymentChecked = Date.now();
272
- let lnPaymentStatus = await this.lightning.getPayment(swap.lnPaymentHash);
273
- if (lnPaymentStatus != null) {
274
- if (lnPaymentStatus.status === "pending") {
275
- //Payment still ongoing, process the result
276
- this.subscribeToPayment(swap);
277
- return;
278
- }
279
- else {
280
- //Payment has already concluded, process the result
281
- await this.processPaymentResult(swap, lnPaymentStatus);
282
- return;
283
- }
284
- }
285
- else {
286
- //Payment not founds, try to process again
287
- await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED);
288
- }
289
- }
290
- if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED) {
291
- await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED);
292
- await this.saveSwapData(swap);
293
- try {
294
- await this.sendLightningPayment(swap);
295
- }
296
- catch (e) {
297
- this.swapLogger.error(swap, "processInitialized(): lightning payment error", e);
298
- if ((0, Utils_1.isDefinedRuntimeError)(e)) {
299
- if (swap.metadata != null)
300
- swap.metadata.payError = e;
301
- await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE);
302
- await this.saveSwapData(swap);
303
- return;
304
- }
305
- else
306
- throw e;
307
- }
308
- this.subscribeToPayment(swap);
309
- return;
310
- }
311
- }
312
- async processInitializeEvent(chainIdentifier, swap, event) {
313
- this.swapLogger.info(swap, "SC: InitializeEvent: swap initialized by the client, invoice: " + swap.pr);
314
- //Only process swaps in SAVED state
315
- if (swap.state !== ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED)
316
- return;
317
- await this.processInitialized(swap);
318
- }
319
- async processClaimEvent(chainIdentifier, swap, event) {
320
- this.swapLogger.info(swap, "SC: ClaimEvent: swap claimed to us, secret: " + event.result + " invoice: " + swap.pr);
321
- await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CLAIMED);
322
- }
323
- async processRefundEvent(chainIdentifier, swap, event) {
324
- this.swapLogger.info(swap, "SC: RefundEvent: swap refunded back to the client, invoice: " + swap.pr);
325
- await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.REFUNDED);
326
- }
327
- /**
328
- * Checks if the amount was supplied in the exactIn request
329
- *
330
- * @param amount
331
- * @param exactIn
332
- * @throws {DefinedRuntimeError} will throw an error if the swap was exactIn, but amount not specified
333
- */
334
- checkAmount(amount, exactIn) {
335
- if (exactIn) {
336
- if (amount == null) {
337
- throw {
338
- code: 20040,
339
- msg: "Invalid request body (amount not specified)!"
340
- };
341
- }
342
- }
343
- }
344
- /**
345
- * Checks if the maxFee parameter is in valid range (>0)
346
- *
347
- * @param maxFee
348
- * @throws {DefinedRuntimeError} will throw an error if the maxFee is zero or negative
349
- */
350
- checkMaxFee(maxFee) {
351
- if (maxFee <= 0) {
352
- throw {
353
- code: 20030,
354
- msg: "Invalid request body (maxFee too low)!"
355
- };
356
- }
357
- }
358
- /**
359
- * Checks and parses a payment request (bolt11 invoice), additionally also checks expiration time of the invoice
360
- *
361
- * @param chainIdentifier
362
- * @param pr
363
- * @throws {DefinedRuntimeError} will throw an error if the pr is invalid, without amount or expired
364
- */
365
- async checkPaymentRequest(chainIdentifier, pr) {
366
- let parsedPR;
367
- try {
368
- parsedPR = await this.lightning.parsePaymentRequest(pr);
369
- }
370
- catch (e) {
371
- throw {
372
- code: 20021,
373
- msg: "Invalid request body (pr - cannot be parsed)"
374
- };
375
- }
376
- if (parsedPR.mtokens == null)
377
- throw {
378
- code: 20022,
379
- msg: "Invalid request body (pr - needs to have amount)"
380
- };
381
- let halfConfidence = false;
382
- if (parsedPR.expiryEpochMillis < Date.now() + ((this.getInitAuthorizationTimeout(chainIdentifier) + (2 * 60)) * 1000)) {
383
- if (!this.config.allowShortExpiry) {
384
- throw {
385
- code: 20020,
386
- msg: "Invalid request body (pr - expired)"
387
- };
388
- }
389
- else if (parsedPR.expiryEpochMillis < Date.now()) {
390
- throw {
391
- code: 20020,
392
- msg: "Invalid request body (pr - expired)"
393
- };
394
- }
395
- halfConfidence = true;
396
- }
397
- return { parsedPR, halfConfidence };
398
- }
399
- /**
400
- * Checks if the request specified too short of an expiry
401
- *
402
- * @param expiryTimestamp
403
- * @param currentTimestamp
404
- * @throws {DefinedRuntimeError} will throw an error if the expiry time is too short
405
- */
406
- checkExpiry(expiryTimestamp, currentTimestamp) {
407
- const expiresTooSoon = (expiryTimestamp - currentTimestamp) < this.config.minTsSendCltv;
408
- if (expiresTooSoon) {
409
- throw {
410
- code: 20001,
411
- msg: "Expiry time too low!"
412
- };
413
- }
414
- }
415
- /**
416
- * Estimates the routing fee & confidence by either probing or routing (if probing fails), the fee is also adjusted
417
- * according to routing fee multiplier, and subject to minimums set in config
418
- *
419
- * @param amountBD
420
- * @param maxFee
421
- * @param expiryTimestamp
422
- * @param currentTimestamp
423
- * @param pr
424
- * @param metadata
425
- * @param abortSignal
426
- * @throws {DefinedRuntimeError} will throw an error if the destination is unreachable
427
- */
428
- async checkAndGetNetworkFee(amountBD, maxFee, expiryTimestamp, currentTimestamp, pr, metadata, abortSignal) {
429
- const maxUsableCLTV = (expiryTimestamp - currentTimestamp - this.config.gracePeriod) / (this.config.bitcoinBlocktime * this.config.safetyFactor);
430
- const blockHeight = await this.lightning.getBlockheight();
431
- abortSignal.throwIfAborted();
432
- metadata.times.blockheightFetched = Date.now();
433
- const maxTimeoutBlockheight = BigInt(blockHeight) + maxUsableCLTV;
434
- const req = {
435
- request: pr,
436
- amountMtokens: amountBD * 1000n,
437
- maxFeeMtokens: maxFee * 1000n,
438
- maxTimeoutHeight: Number(maxTimeoutBlockheight)
439
- };
440
- let probeOrRouteResp = await this.lightning.probe(req);
441
- metadata.times.probeResult = Date.now();
442
- metadata.probeResponse = { ...probeOrRouteResp };
443
- abortSignal.throwIfAborted();
444
- if (probeOrRouteResp == null) {
445
- if (!this.config.allowProbeFailedSwaps)
446
- throw {
447
- code: 20002,
448
- msg: "Cannot route the payment!"
449
- };
450
- const routeResp = await this.lightning.route(req);
451
- metadata.times.routingResult = Date.now();
452
- metadata.routeResponse = { ...routeResp };
453
- abortSignal.throwIfAborted();
454
- if (routeResp == null)
455
- throw {
456
- code: 20002,
457
- msg: "Cannot route the payment!"
458
- };
459
- this.logger.info("checkAndGetNetworkFee(): routing result," +
460
- " destination: " + routeResp.destination +
461
- " confidence: " + routeResp.confidence +
462
- " fee mtokens: " + routeResp.feeMtokens.toString(10));
463
- probeOrRouteResp = routeResp;
464
- }
465
- else {
466
- this.logger.info("checkAndGetNetworkFee(): route probed," +
467
- " destination: " + probeOrRouteResp.destination +
468
- " confidence: " + probeOrRouteResp.confidence +
469
- " fee mtokens: " + probeOrRouteResp.feeMtokens.toString(10));
470
- }
471
- const safeFeeTokens = (probeOrRouteResp.feeMtokens + 999n) / 1000n;
472
- let actualRoutingFee = safeFeeTokens * this.config.routingFeeMultiplier;
473
- const minRoutingFee = (amountBD * this.config.minLnRoutingFeePPM / 1000000n) + this.config.minLnBaseFee;
474
- if (actualRoutingFee < minRoutingFee) {
475
- actualRoutingFee = minRoutingFee;
476
- if (actualRoutingFee > maxFee) {
477
- probeOrRouteResp.confidence = 0;
478
- }
479
- }
480
- if (actualRoutingFee > maxFee) {
481
- actualRoutingFee = maxFee;
482
- }
483
- this.logger.debug("checkAndGetNetworkFee(): network fee calculated, amount: " + amountBD.toString(10) + " fee: " + actualRoutingFee.toString(10));
484
- return {
485
- networkFee: actualRoutingFee,
486
- confidence: probeOrRouteResp.confidence
487
- };
488
- }
489
- /**
490
- * Checks and consumes (deletes & returns) exactIn authorizaton with a specific reqId
491
- *
492
- * @param reqId
493
- * @throws {DefinedRuntimeError} will throw an error if the authorization doesn't exist
494
- */
495
- checkExactInAuthorization(reqId) {
496
- const parsedAuth = this.exactInAuths[reqId];
497
- if (parsedAuth == null) {
498
- throw {
499
- code: 20070,
500
- msg: "Invalid reqId"
501
- };
502
- }
503
- delete this.exactInAuths[reqId];
504
- if (parsedAuth.expiry < Date.now()) {
505
- throw {
506
- code: 20200,
507
- msg: "Authorization already expired!"
508
- };
509
- }
510
- return parsedAuth;
511
- }
512
- /**
513
- * Checks if the newly submitted PR has the same parameters (destination, cltv_delta, routes) as the initial dummy
514
- * invoice sent for exactIn swap quote
515
- *
516
- * @param pr
517
- * @param parsedAuth
518
- * @throws {DefinedRuntimeError} will throw an error if the details don't match
519
- */
520
- async checkPaymentRequestMatchesInitial(pr, parsedAuth) {
521
- const parsedRequest = await this.lightning.parsePaymentRequest(pr);
522
- if (parsedRequest.destination !== parsedAuth.initialInvoice.destination ||
523
- parsedRequest.cltvDelta !== parsedAuth.initialInvoice.cltvDelta ||
524
- parsedRequest.mtokens !== parsedAuth.amount * 1000n) {
525
- throw {
526
- code: 20102,
527
- msg: "Provided PR doesn't match initial!"
528
- };
529
- }
530
- if (!(0, ILightningWallet_1.routesMatch)(parsedRequest.routes, parsedAuth.initialInvoice.routes)) {
531
- throw {
532
- code: 20102,
533
- msg: "Provided PR doesn't match initial (routes)!"
534
- };
535
- }
536
- }
537
- startRestServer(restServer) {
538
- restServer.use(this.path + "/payInvoiceExactIn", (0, ServerParamDecoder_1.serverParamDecoder)(10 * 1000));
539
- restServer.post(this.path + "/payInvoiceExactIn", (0, Utils_1.expressHandlerWrapper)(async (req, res) => {
540
- /**
541
- * pr: string bolt11 lightning invoice
542
- * reqId: string Identifier of the swap
543
- * feeRate: string Fee rate to use for the init tx
544
- */
545
- const parsedBody = await req.paramReader.getParams({
546
- pr: SchemaVerifier_1.FieldTypeEnum.String,
547
- reqId: SchemaVerifier_1.FieldTypeEnum.String,
548
- feeRate: SchemaVerifier_1.FieldTypeEnum.String
549
- });
550
- if (parsedBody == null) {
551
- throw {
552
- code: 20100,
553
- msg: "Invalid request body"
554
- };
555
- }
556
- const responseStream = res.responseStream;
557
- const abortSignal = responseStream.getAbortSignal();
558
- //Check request params
559
- const parsedAuth = this.checkExactInAuthorization(parsedBody.reqId);
560
- const { parsedPR, halfConfidence } = await this.checkPaymentRequest(parsedAuth.chainIdentifier, parsedBody.pr);
561
- await this.checkPaymentRequestMatchesInitial(parsedBody.pr, parsedAuth);
562
- const metadata = parsedAuth.metadata;
563
- const sequence = base_1.BigIntBufferUtils.fromBuffer((0, crypto_1.randomBytes)(8));
564
- const { swapContract, signer } = this.getChain(parsedAuth.chainIdentifier);
565
- const claimHash = swapContract.getHashForHtlc(Buffer.from(parsedPR.id, "hex"));
566
- //Create swap data
567
- const payObject = await swapContract.createSwapData(base_1.ChainSwapType.HTLC, parsedAuth.offerer, signer.getAddress(), parsedAuth.token, parsedAuth.total, claimHash.toString("hex"), sequence, parsedAuth.swapExpiry, true, false, 0n, 0n);
568
- metadata.times.swapCreated = Date.now();
569
- //Sign swap data
570
- const prefetchedSignData = parsedAuth.preFetchSignData;
571
- const sigData = await this.getToBtcSignatureData(parsedAuth.chainIdentifier, payObject, req, abortSignal, prefetchedSignData);
572
- metadata.times.swapSigned = Date.now();
573
- //Create swap
574
- const createdSwap = new ToBtcLnSwapAbs_1.ToBtcLnSwapAbs(parsedAuth.chainIdentifier, parsedPR.id, parsedBody.pr, parsedPR.mtokens, parsedAuth.swapFee, parsedAuth.swapFeeInToken, parsedAuth.quotedNetworkFee, parsedAuth.quotedNetworkFeeInToken);
575
- createdSwap.data = payObject;
576
- createdSwap.metadata = metadata;
577
- createdSwap.prefix = sigData.prefix;
578
- createdSwap.timeout = sigData.timeout;
579
- createdSwap.signature = sigData.signature;
580
- createdSwap.feeRate = sigData.feeRate;
581
- await PluginManager_1.PluginManager.swapCreate(createdSwap);
582
- await this.saveSwapData(createdSwap);
583
- this.swapLogger.info(createdSwap, "REST: /payInvoiceExactIn: created exact in swap," +
584
- " reqId: " + parsedBody.reqId +
585
- " mtokens: " + parsedPR.mtokens.toString(10) +
586
- " invoice: " + createdSwap.pr);
587
- await responseStream.writeParamsAndEnd({
588
- code: 20000,
589
- msg: "Success",
590
- data: {
591
- maxFee: parsedAuth.quotedNetworkFeeInToken.toString(10),
592
- swapFee: parsedAuth.swapFeeInToken.toString(10),
593
- total: parsedAuth.total.toString(10),
594
- confidence: halfConfidence ? parsedAuth.confidence / 2000000 : parsedAuth.confidence / 1000000,
595
- address: signer.getAddress(),
596
- routingFeeSats: parsedAuth.quotedNetworkFee.toString(10),
597
- data: payObject.serialize(),
598
- prefix: sigData.prefix,
599
- timeout: sigData.timeout,
600
- signature: sigData.signature
601
- }
602
- });
603
- }));
604
- restServer.use(this.path + "/payInvoice", (0, ServerParamDecoder_1.serverParamDecoder)(10 * 1000));
605
- restServer.post(this.path + "/payInvoice", (0, Utils_1.expressHandlerWrapper)(async (req, res) => {
606
- const metadata = { request: {}, times: {} };
607
- const chainIdentifier = req.query.chain ?? this.chains.default;
608
- const { swapContract, signer, chainInterface } = this.getChain(chainIdentifier);
609
- metadata.times.requestReceived = Date.now();
610
- /**
611
- *Sent initially:
612
- * pr: string bolt11 lightning invoice
613
- * maxFee: string maximum routing fee
614
- * expiryTimestamp: string expiry timestamp of the to be created HTLC, determines how many LN paths can be considered
615
- * token: string Desired token to use
616
- * offerer: string Address of the caller
617
- * exactIn: boolean Whether to do an exact in swap instead of exact out
618
- * amount: string Input amount for exactIn swaps
619
- *
620
- *Sent later:
621
- * feeRate: string Fee rate to use for the init signature
622
- */
623
- const parsedBody = await req.paramReader.getParams({
624
- pr: SchemaVerifier_1.FieldTypeEnum.String,
625
- maxFee: SchemaVerifier_1.FieldTypeEnum.BigInt,
626
- expiryTimestamp: SchemaVerifier_1.FieldTypeEnum.BigInt,
627
- token: (val) => val != null &&
628
- typeof (val) === "string" &&
629
- this.isTokenSupported(chainIdentifier, val) ? val : null,
630
- offerer: (val) => val != null &&
631
- typeof (val) === "string" &&
632
- chainInterface.isValidAddress(val) ? val : null,
633
- exactIn: SchemaVerifier_1.FieldTypeEnum.BooleanOptional,
634
- amount: SchemaVerifier_1.FieldTypeEnum.BigIntOptional
635
- });
636
- if (parsedBody == null) {
637
- throw {
638
- code: 20100,
639
- msg: "Invalid request body"
640
- };
641
- }
642
- metadata.request = parsedBody;
643
- const request = {
644
- chainIdentifier,
645
- raw: req,
646
- parsed: parsedBody,
647
- metadata
648
- };
649
- const useToken = parsedBody.token;
650
- const responseStream = res.responseStream;
651
- const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
652
- //Check request params
653
- this.checkAmount(parsedBody.amount, parsedBody.exactIn);
654
- this.checkMaxFee(parsedBody.maxFee);
655
- this.checkExpiry(parsedBody.expiryTimestamp, currentTimestamp);
656
- await this.checkVaultInitialized(chainIdentifier, parsedBody.token);
657
- const { parsedPR, halfConfidence } = await this.checkPaymentRequest(chainIdentifier, parsedBody.pr);
658
- const requestedAmount = {
659
- input: !!parsedBody.exactIn,
660
- amount: !!parsedBody.exactIn ? parsedBody.amount : (parsedPR.mtokens + 999n) / 1000n,
661
- token: useToken
662
- };
663
- const fees = await this.AmountAssertions.preCheckToBtcAmounts(this.type, request, requestedAmount);
664
- metadata.times.requestChecked = Date.now();
665
- //Create abort controller for parallel pre-fetches
666
- const abortController = (0, Utils_1.getAbortController)(responseStream);
667
- //Pre-fetch
668
- const { pricePrefetchPromise, signDataPrefetchPromise } = this.getToBtcPrefetches(chainIdentifier, useToken, responseStream, abortController);
669
- //Check if prior payment has been made
670
- await this.LightningAssertions.checkPriorPayment(parsedPR.id, abortController.signal);
671
- metadata.times.priorPaymentChecked = Date.now();
672
- //Check amounts
673
- const { amountBD, networkFeeData, totalInToken, swapFee, swapFeeInToken, networkFeeInToken } = await this.AmountAssertions.checkToBtcAmount(this.type, request, { ...requestedAmount, pricePrefetch: pricePrefetchPromise }, fees, async (amountBD) => {
674
- //Check if we have enough liquidity to process the swap
675
- await this.LightningAssertions.checkLiquidity(amountBD, abortController.signal, true);
676
- metadata.times.liquidityChecked = Date.now();
677
- const maxFee = parsedBody.exactIn ?
678
- await this.swapPricing.getToBtcSwapAmount(parsedBody.maxFee, useToken, chainIdentifier, null, pricePrefetchPromise) :
679
- parsedBody.maxFee;
680
- return await this.checkAndGetNetworkFee(amountBD, maxFee, parsedBody.expiryTimestamp, currentTimestamp, parsedBody.pr, metadata, abortController.signal);
681
- }, abortController.signal);
682
- metadata.times.priceCalculated = Date.now();
683
- //For exactIn swap, just save and wait for the actual invoice to be submitted
684
- if (parsedBody.exactIn) {
685
- const reqId = (0, crypto_1.randomBytes)(32).toString("hex");
686
- this.exactInAuths[reqId] = {
687
- chainIdentifier,
688
- reqId,
689
- expiry: Date.now() + this.config.exactInExpiry,
690
- amount: amountBD,
691
- initialInvoice: parsedPR,
692
- quotedNetworkFeeInToken: networkFeeInToken,
693
- swapFeeInToken,
694
- total: totalInToken,
695
- confidence: networkFeeData.confidence,
696
- quotedNetworkFee: networkFeeData.networkFee,
697
- swapFee,
698
- token: useToken,
699
- swapExpiry: parsedBody.expiryTimestamp,
700
- offerer: parsedBody.offerer,
701
- preFetchSignData: signDataPrefetchPromise != null ? await signDataPrefetchPromise : null,
702
- metadata
703
- };
704
- this.logger.info("REST: /payInvoice: created exact in swap," +
705
- " reqId: " + reqId +
706
- " amount: " + amountBD.toString(10) +
707
- " destination: " + parsedPR.destination);
708
- await responseStream.writeParamsAndEnd({
709
- code: 20000,
710
- msg: "Success",
711
- data: {
712
- amount: amountBD.toString(10),
713
- reqId
714
- }
715
- });
716
- return;
717
- }
718
- const sequence = base_1.BigIntBufferUtils.fromBuffer((0, crypto_1.randomBytes)(8));
719
- const claimHash = swapContract.getHashForHtlc(Buffer.from(parsedPR.id, "hex"));
720
- //Create swap data
721
- const payObject = await swapContract.createSwapData(base_1.ChainSwapType.HTLC, parsedBody.offerer, signer.getAddress(), useToken, totalInToken, claimHash.toString("hex"), sequence, parsedBody.expiryTimestamp, true, false, 0n, 0n);
722
- abortController.signal.throwIfAborted();
723
- metadata.times.swapCreated = Date.now();
724
- //Sign swap data
725
- const sigData = await this.getToBtcSignatureData(chainIdentifier, payObject, req, abortController.signal, signDataPrefetchPromise);
726
- metadata.times.swapSigned = Date.now();
727
- //Create swap
728
- const createdSwap = new ToBtcLnSwapAbs_1.ToBtcLnSwapAbs(chainIdentifier, parsedPR.id, parsedBody.pr, parsedPR.mtokens, swapFee, swapFeeInToken, networkFeeData.networkFee, networkFeeInToken);
729
- createdSwap.data = payObject;
730
- createdSwap.metadata = metadata;
731
- createdSwap.prefix = sigData.prefix;
732
- createdSwap.timeout = sigData.timeout;
733
- createdSwap.signature = sigData.signature;
734
- createdSwap.feeRate = sigData.feeRate;
735
- await PluginManager_1.PluginManager.swapCreate(createdSwap);
736
- await this.saveSwapData(createdSwap);
737
- this.swapLogger.info(createdSwap, "REST: /payInvoice: created swap," +
738
- " amount: " + amountBD.toString(10) +
739
- " invoice: " + createdSwap.pr);
740
- await responseStream.writeParamsAndEnd({
741
- code: 20000,
742
- msg: "Success",
743
- data: {
744
- maxFee: networkFeeInToken.toString(10),
745
- swapFee: swapFeeInToken.toString(10),
746
- total: totalInToken.toString(10),
747
- confidence: halfConfidence ? networkFeeData.confidence / 2000000 : networkFeeData.confidence / 1000000,
748
- address: signer.getAddress(),
749
- routingFeeSats: networkFeeData.networkFee.toString(10),
750
- data: payObject.serialize(),
751
- prefix: sigData.prefix,
752
- timeout: sigData.timeout,
753
- signature: sigData.signature
754
- }
755
- });
756
- }));
757
- const getRefundAuthorization = (0, Utils_1.expressHandlerWrapper)(async (req, res) => {
758
- /**
759
- * paymentHash: string Identifier of the swap
760
- * sequence: BN Sequence identifier of the swap
761
- */
762
- const parsedBody = (0, SchemaVerifier_1.verifySchema)({ ...req.body, ...req.query }, {
763
- paymentHash: (val) => val != null &&
764
- typeof (val) === "string" &&
765
- val.length === 64 &&
766
- Utils_1.HEX_REGEX.test(val) ? val : null,
767
- sequence: SchemaVerifier_1.FieldTypeEnum.BigInt
768
- });
769
- if (parsedBody == null)
770
- throw {
771
- code: 20100,
772
- msg: "Invalid request body/query (paymentHash/sequence)"
773
- };
774
- this.checkSequence(parsedBody.sequence);
775
- const data = await this.storageManager.getData(parsedBody.paymentHash, parsedBody.sequence);
776
- const isSwapFound = data != null;
777
- if (isSwapFound) {
778
- const { signer, swapContract } = this.getChain(data.chainIdentifier);
779
- if (await swapContract.isExpired(signer.getAddress(), data.data))
780
- throw {
781
- _httpStatus: 200,
782
- code: 20010,
783
- msg: "Payment expired"
784
- };
785
- if (data.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE) {
786
- const refundSigData = await swapContract.getRefundSignature(signer, data.data, this.config.refundAuthorizationTimeout);
787
- //Double check the state after promise result
788
- if (data.state !== ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE)
789
- throw {
790
- code: 20005,
791
- msg: "Not committed"
792
- };
793
- this.swapLogger.info(data, "REST: /getRefundAuthorization: returning refund authorization, because invoice in NON_PAYABLE state, invoice: " + data.pr);
794
- res.status(200).json({
795
- code: 20000,
796
- msg: "Success",
797
- data: {
798
- address: signer.getAddress(),
799
- prefix: refundSigData.prefix,
800
- timeout: refundSigData.timeout,
801
- signature: refundSigData.signature
802
- }
803
- });
804
- return;
805
- }
806
- }
807
- const payment = await this.lightning.getPayment(parsedBody.paymentHash);
808
- if (payment == null)
809
- throw {
810
- _httpStatus: 200,
811
- code: 20007,
812
- msg: "Payment not found"
813
- };
814
- if (payment.status === "pending")
815
- throw {
816
- _httpStatus: 200,
817
- code: 20008,
818
- msg: "Payment in-flight"
819
- };
820
- if (payment.status === "confirmed")
821
- throw {
822
- _httpStatus: 200,
823
- code: 20006,
824
- msg: "Already paid",
825
- data: {
826
- secret: payment.secret
827
- }
828
- };
829
- if (payment.status === "failed")
830
- throw {
831
- _httpStatus: 200,
832
- code: 20010,
833
- msg: "Payment expired",
834
- data: {
835
- reason: payment.failedReason
836
- }
837
- };
838
- });
839
- restServer.post(this.path + '/getRefundAuthorization', getRefundAuthorization);
840
- restServer.get(this.path + '/getRefundAuthorization', getRefundAuthorization);
841
- this.logger.info("started at path: ", this.path);
842
- }
843
- async init() {
844
- await this.loadData(ToBtcLnSwapAbs_1.ToBtcLnSwapAbs);
845
- //Check if all swaps contain a valid amount
846
- for (let { obj: swap } of await this.storageManager.query([])) {
847
- if (swap.amount == null || swap.lnPaymentHash == null) {
848
- const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
849
- swap.amount = (parsedPR.mtokens + 999n) / 1000n;
850
- swap.lnPaymentHash = parsedPR.id;
851
- }
852
- }
853
- this.subscribeToEvents();
854
- await PluginManager_1.PluginManager.serviceInitialize(this);
855
- }
856
- getInfoData() {
857
- return {
858
- minCltv: Number(this.config.minSendCltv),
859
- minTimestampCltv: Number(this.config.minTsSendCltv)
860
- };
861
- }
862
- }
863
- exports.ToBtcLnAbs = ToBtcLnAbs;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ToBtcLnAbs = void 0;
4
+ const ToBtcLnSwapAbs_1 = require("./ToBtcLnSwapAbs");
5
+ const SwapHandler_1 = require("../../SwapHandler");
6
+ const base_1 = require("@atomiqlabs/base");
7
+ const Utils_1 = require("../../../utils/Utils");
8
+ const PluginManager_1 = require("../../../plugins/PluginManager");
9
+ const crypto_1 = require("crypto");
10
+ const ServerParamDecoder_1 = require("../../../utils/paramcoders/server/ServerParamDecoder");
11
+ const SchemaVerifier_1 = require("../../../utils/paramcoders/SchemaVerifier");
12
+ const ToBtcBaseSwapHandler_1 = require("../ToBtcBaseSwapHandler");
13
+ const ILightningWallet_1 = require("../../../wallets/ILightningWallet");
14
+ const LightningAssertions_1 = require("../../assertions/LightningAssertions");
15
+ /**
16
+ * Swap handler handling to BTCLN swaps using submarine swaps
17
+ */
18
+ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
19
+ constructor(storageDirectory, path, chainData, lightning, swapPricing, config) {
20
+ super(storageDirectory, path, chainData, swapPricing, config);
21
+ this.activeSubscriptions = new Set();
22
+ this.type = SwapHandler_1.SwapHandlerType.TO_BTCLN;
23
+ this.swapType = base_1.ChainSwapType.HTLC;
24
+ this.exactInAuths = {};
25
+ this.lightning = lightning;
26
+ this.LightningAssertions = new LightningAssertions_1.LightningAssertions(this.logger, lightning);
27
+ const anyConfig = config;
28
+ anyConfig.minTsSendCltv = config.gracePeriod + (config.bitcoinBlocktime * config.minSendCltv * config.safetyFactor);
29
+ this.config = anyConfig;
30
+ this.config.minLnRoutingFeePPM = this.config.minLnRoutingFeePPM || 1000n;
31
+ this.config.minLnBaseFee = this.config.minLnBaseFee || 5n;
32
+ this.config.exactInExpiry = this.config.exactInExpiry || 10 * 1000;
33
+ }
34
+ /**
35
+ * Cleans up exactIn authorization that are already past their expiry
36
+ *
37
+ * @protected
38
+ */
39
+ cleanExpiredExactInAuthorizations() {
40
+ for (let key in this.exactInAuths) {
41
+ const obj = this.exactInAuths[key];
42
+ if (obj.expiry < Date.now()) {
43
+ this.logger.info("cleanExpiredExactInAuthorizations(): remove expired authorization, reqId: " + key);
44
+ delete this.exactInAuths[key];
45
+ }
46
+ }
47
+ }
48
+ async processPastSwap(swap) {
49
+ const { swapContract, signer } = this.getChain(swap.chainIdentifier);
50
+ if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED) {
51
+ //Cancel the swaps where signature is expired
52
+ const isSignatureExpired = await swapContract.isInitAuthorizationExpired(swap.data, swap);
53
+ if (isSignatureExpired) {
54
+ const isCommitted = await swapContract.isCommited(swap.data);
55
+ if (!isCommitted) {
56
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): authorization expired & swap not committed, cancelling swap, invoice: " + swap.pr);
57
+ await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CANCELED);
58
+ return;
59
+ }
60
+ else {
61
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): swap committed (detected from processPastSwap), invoice: " + swap.pr);
62
+ await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED);
63
+ await this.saveSwapData(swap);
64
+ }
65
+ }
66
+ //Cancel the swaps where lightning invoice is expired
67
+ const decodedPR = await this.lightning.parsePaymentRequest(swap.pr);
68
+ const isInvoiceExpired = decodedPR.expiryEpochMillis < Date.now();
69
+ if (isInvoiceExpired) {
70
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): invoice expired, cancel uncommited swap, invoice: " + swap.pr);
71
+ await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CANCELED);
72
+ return;
73
+ }
74
+ }
75
+ if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED || swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.PAID) {
76
+ //Process swaps in commited & paid state
77
+ await this.processInitialized(swap);
78
+ }
79
+ if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE) {
80
+ //Remove expired swaps (as these can already be unilaterally refunded by the client), so we don't need
81
+ // to be able to cooperatively refund them
82
+ if (await swapContract.isExpired(signer.getAddress(), swap.data)) {
83
+ this.swapLogger.info(swap, "processPastSwap(state=NON_PAYABLE): swap expired, removing swap data, invoice: " + swap.pr);
84
+ await this.removeSwapData(swap);
85
+ }
86
+ }
87
+ }
88
+ /**
89
+ * Checks past swaps, deletes ones that are already expired, and tries to process ones that are committed.
90
+ */
91
+ async processPastSwaps() {
92
+ this.cleanExpiredExactInAuthorizations();
93
+ const queriedData = await this.storageManager.query([
94
+ {
95
+ key: "state",
96
+ value: [
97
+ ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED,
98
+ ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED,
99
+ ToBtcLnSwapAbs_1.ToBtcLnSwapState.PAID,
100
+ ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE
101
+ ]
102
+ }
103
+ ]);
104
+ for (let { obj: swap } of queriedData) {
105
+ await this.processPastSwap(swap);
106
+ }
107
+ }
108
+ /**
109
+ * Tries to claim the swap funds on the SC side, returns false if the swap is already locked (claim tx is already being sent)
110
+ *
111
+ * @param swap
112
+ * @private
113
+ * @returns Whether the transaction was successfully sent
114
+ */
115
+ async tryClaimSwap(swap) {
116
+ if (swap.secret == null)
117
+ throw new Error("Invalid swap state, needs payment pre-image!");
118
+ const { swapContract, signer } = this.getChain(swap.chainIdentifier);
119
+ //Check if escrow state exists
120
+ const isCommited = await swapContract.isCommited(swap.data);
121
+ if (!isCommited) {
122
+ const status = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
123
+ if (status?.type === base_1.SwapCommitStateType.PAID) {
124
+ //This is alright, we got the money
125
+ swap.txIds ?? (swap.txIds = {});
126
+ swap.txIds.claim = await status.getClaimTxId();
127
+ await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CLAIMED);
128
+ return true;
129
+ }
130
+ else if (status?.type === base_1.SwapCommitStateType.EXPIRED) {
131
+ //This means the user was able to refund before we were able to claim, no good
132
+ swap.txIds ?? (swap.txIds = {});
133
+ swap.txIds.refund = status.getRefundTxId == null ? null : await status.getRefundTxId();
134
+ await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.REFUNDED);
135
+ }
136
+ this.swapLogger.warn(swap, "processPaymentResult(): tried to claim but escrow doesn't exist anymore," +
137
+ " status: " + status +
138
+ " invoice: " + swap.pr);
139
+ return false;
140
+ }
141
+ //Set flag that we are sending the transaction already, so we don't end up with race condition
142
+ const unlock = swap.lock(swapContract.claimWithSecretTimeout);
143
+ if (unlock == null)
144
+ return false;
145
+ try {
146
+ this.swapLogger.debug(swap, "tryClaimSwap(): initiate claim of swap, secret: " + swap.secret);
147
+ const success = await swapContract.claimWithSecret(signer, swap.data, swap.secret, false, false, {
148
+ waitForConfirmation: true
149
+ });
150
+ this.swapLogger.info(swap, "tryClaimSwap(): swap claimed successfully, secret: " + swap.secret + " invoice: " + swap.pr);
151
+ if (swap.metadata != null)
152
+ swap.metadata.times.txClaimed = Date.now();
153
+ unlock();
154
+ return true;
155
+ }
156
+ catch (e) {
157
+ this.swapLogger.error(swap, "tryClaimSwap(): error occurred claiming swap, secret: " + swap.secret + " invoice: " + swap.pr, e);
158
+ return false;
159
+ }
160
+ }
161
+ /**
162
+ * Process the result of attempted lightning network payment
163
+ *
164
+ * @param swap
165
+ * @param lnPaymentStatus
166
+ */
167
+ async processPaymentResult(swap, lnPaymentStatus) {
168
+ switch (lnPaymentStatus.status) {
169
+ case "pending":
170
+ return;
171
+ case "failed":
172
+ this.swapLogger.info(swap, "processPaymentResult(): invoice payment failed, cancelling swap, invoice: " + swap.pr);
173
+ await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE);
174
+ await this.saveSwapData(swap);
175
+ return;
176
+ case "confirmed":
177
+ swap.secret = lnPaymentStatus.secret;
178
+ swap.setRealNetworkFee(lnPaymentStatus.feeMtokens / 1000n);
179
+ this.swapLogger.info(swap, "processPaymentResult(): invoice paid, secret: " + swap.secret + " realRoutingFee: " + swap.realNetworkFee.toString(10) + " invoice: " + swap.pr);
180
+ await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.PAID);
181
+ await this.saveSwapData(swap);
182
+ const success = await this.tryClaimSwap(swap);
183
+ if (success)
184
+ this.swapLogger.info(swap, "processPaymentResult(): swap claimed successfully, invoice: " + swap.pr);
185
+ return;
186
+ default:
187
+ throw new Error("Invalid lnPaymentStatus");
188
+ }
189
+ }
190
+ /**
191
+ * Subscribe to a pending lightning network payment attempt
192
+ *
193
+ * @param invoiceData
194
+ */
195
+ subscribeToPayment(invoiceData) {
196
+ const paymentHash = invoiceData.lnPaymentHash;
197
+ if (this.activeSubscriptions.has(paymentHash))
198
+ return false;
199
+ this.lightning.waitForPayment(paymentHash).then(result => {
200
+ this.swapLogger.info(invoiceData, "subscribeToPayment(): result callback, outcome: " + result.status + " invoice: " + invoiceData.pr);
201
+ this.processPaymentResult(invoiceData, result).catch(e => this.swapLogger.error(invoiceData, "subscribeToPayment(): process payment result", e));
202
+ this.activeSubscriptions.delete(paymentHash);
203
+ });
204
+ this.swapLogger.info(invoiceData, "subscribeToPayment(): subscribe to payment outcome, invoice: " + invoiceData.pr);
205
+ this.activeSubscriptions.add(paymentHash);
206
+ return true;
207
+ }
208
+ async sendLightningPayment(swap) {
209
+ const decodedPR = await this.lightning.parsePaymentRequest(swap.pr);
210
+ const expiryTimestamp = swap.data.getExpiry();
211
+ const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
212
+ //Run checks
213
+ const hasEnoughTimeToPay = (expiryTimestamp - currentTimestamp) >= this.config.minTsSendCltv;
214
+ if (!hasEnoughTimeToPay)
215
+ throw {
216
+ code: 90005,
217
+ msg: "Not enough time to reliably pay the invoice"
218
+ };
219
+ const isInvoiceExpired = decodedPR.expiryEpochMillis < Date.now();
220
+ if (isInvoiceExpired)
221
+ throw {
222
+ code: 90006,
223
+ msg: "Invoice already expired"
224
+ };
225
+ //Compute max cltv delta
226
+ const maxFee = swap.quotedNetworkFee;
227
+ const maxUsableCLTVdelta = (expiryTimestamp - currentTimestamp - this.config.gracePeriod)
228
+ / (this.config.bitcoinBlocktime * this.config.safetyFactor);
229
+ //Initiate payment
230
+ this.swapLogger.info(swap, "sendLightningPayment(): paying lightning network invoice," +
231
+ " cltvDelta: " + maxUsableCLTVdelta.toString(10) +
232
+ " maxFee: " + maxFee.toString(10) +
233
+ " invoice: " + swap.pr);
234
+ const blockHeight = await this.lightning.getBlockheight();
235
+ swap.payInitiated = true;
236
+ await this.saveSwapData(swap);
237
+ try {
238
+ await this.lightning.pay({
239
+ request: swap.pr,
240
+ maxFeeMtokens: maxFee * 1000n,
241
+ maxTimeoutHeight: blockHeight + Number(maxUsableCLTVdelta)
242
+ });
243
+ }
244
+ catch (e) {
245
+ throw {
246
+ code: 90007,
247
+ msg: "Failed to initiate invoice payment",
248
+ data: {
249
+ error: JSON.stringify(e)
250
+ }
251
+ };
252
+ }
253
+ if (swap.metadata != null)
254
+ swap.metadata.times.payComplete = Date.now();
255
+ }
256
+ /**
257
+ * Begins a lightning network payment attempt, if not attempted already
258
+ *
259
+ * @param swap
260
+ */
261
+ async processInitialized(swap) {
262
+ //Check if payment was already made
263
+ if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.PAID) {
264
+ const success = await this.tryClaimSwap(swap);
265
+ if (success)
266
+ this.swapLogger.info(swap, "processInitialized(): swap claimed successfully, invoice: " + swap.pr);
267
+ return;
268
+ }
269
+ if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED) {
270
+ if (swap.metadata != null)
271
+ swap.metadata.times.payPaymentChecked = Date.now();
272
+ let lnPaymentStatus = await this.lightning.getPayment(swap.lnPaymentHash);
273
+ if (lnPaymentStatus != null) {
274
+ if (lnPaymentStatus.status === "pending") {
275
+ //Payment still ongoing, process the result
276
+ this.subscribeToPayment(swap);
277
+ return;
278
+ }
279
+ else {
280
+ //Payment has already concluded, process the result
281
+ await this.processPaymentResult(swap, lnPaymentStatus);
282
+ return;
283
+ }
284
+ }
285
+ else {
286
+ //Payment not founds, try to process again
287
+ await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED);
288
+ }
289
+ }
290
+ if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED) {
291
+ await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED);
292
+ await this.saveSwapData(swap);
293
+ try {
294
+ await this.sendLightningPayment(swap);
295
+ }
296
+ catch (e) {
297
+ this.swapLogger.error(swap, "processInitialized(): lightning payment error", e);
298
+ if ((0, Utils_1.isDefinedRuntimeError)(e)) {
299
+ if (swap.metadata != null)
300
+ swap.metadata.payError = e;
301
+ await swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE);
302
+ await this.saveSwapData(swap);
303
+ return;
304
+ }
305
+ else
306
+ throw e;
307
+ }
308
+ this.subscribeToPayment(swap);
309
+ return;
310
+ }
311
+ }
312
+ async processInitializeEvent(chainIdentifier, swap, event) {
313
+ this.swapLogger.info(swap, "SC: InitializeEvent: swap initialized by the client, invoice: " + swap.pr);
314
+ //Only process swaps in SAVED state
315
+ if (swap.state !== ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED)
316
+ return;
317
+ await this.processInitialized(swap);
318
+ }
319
+ async processClaimEvent(chainIdentifier, swap, event) {
320
+ this.swapLogger.info(swap, "SC: ClaimEvent: swap claimed to us, secret: " + event.result + " invoice: " + swap.pr);
321
+ await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CLAIMED);
322
+ }
323
+ async processRefundEvent(chainIdentifier, swap, event) {
324
+ this.swapLogger.info(swap, "SC: RefundEvent: swap refunded back to the client, invoice: " + swap.pr);
325
+ await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.REFUNDED);
326
+ }
327
+ /**
328
+ * Checks if the amount was supplied in the exactIn request
329
+ *
330
+ * @param amount
331
+ * @param exactIn
332
+ * @throws {DefinedRuntimeError} will throw an error if the swap was exactIn, but amount not specified
333
+ */
334
+ checkAmount(amount, exactIn) {
335
+ if (exactIn) {
336
+ if (amount == null) {
337
+ throw {
338
+ code: 20040,
339
+ msg: "Invalid request body (amount not specified)!"
340
+ };
341
+ }
342
+ }
343
+ }
344
+ /**
345
+ * Checks if the maxFee parameter is in valid range (>0)
346
+ *
347
+ * @param maxFee
348
+ * @throws {DefinedRuntimeError} will throw an error if the maxFee is zero or negative
349
+ */
350
+ checkMaxFee(maxFee) {
351
+ if (maxFee <= 0) {
352
+ throw {
353
+ code: 20030,
354
+ msg: "Invalid request body (maxFee too low)!"
355
+ };
356
+ }
357
+ }
358
+ /**
359
+ * Checks and parses a payment request (bolt11 invoice), additionally also checks expiration time of the invoice
360
+ *
361
+ * @param chainIdentifier
362
+ * @param pr
363
+ * @throws {DefinedRuntimeError} will throw an error if the pr is invalid, without amount or expired
364
+ */
365
+ async checkPaymentRequest(chainIdentifier, pr) {
366
+ let parsedPR;
367
+ try {
368
+ parsedPR = await this.lightning.parsePaymentRequest(pr);
369
+ }
370
+ catch (e) {
371
+ throw {
372
+ code: 20021,
373
+ msg: "Invalid request body (pr - cannot be parsed)"
374
+ };
375
+ }
376
+ if (parsedPR.mtokens == null)
377
+ throw {
378
+ code: 20022,
379
+ msg: "Invalid request body (pr - needs to have amount)"
380
+ };
381
+ let halfConfidence = false;
382
+ if (parsedPR.expiryEpochMillis < Date.now() + ((this.getInitAuthorizationTimeout(chainIdentifier) + (2 * 60)) * 1000)) {
383
+ if (!this.config.allowShortExpiry) {
384
+ throw {
385
+ code: 20020,
386
+ msg: "Invalid request body (pr - expired)"
387
+ };
388
+ }
389
+ else if (parsedPR.expiryEpochMillis < Date.now()) {
390
+ throw {
391
+ code: 20020,
392
+ msg: "Invalid request body (pr - expired)"
393
+ };
394
+ }
395
+ halfConfidence = true;
396
+ }
397
+ return { parsedPR, halfConfidence };
398
+ }
399
+ /**
400
+ * Checks if the request specified too short of an expiry
401
+ *
402
+ * @param expiryTimestamp
403
+ * @param currentTimestamp
404
+ * @throws {DefinedRuntimeError} will throw an error if the expiry time is too short
405
+ */
406
+ checkExpiry(expiryTimestamp, currentTimestamp) {
407
+ const expiresTooSoon = (expiryTimestamp - currentTimestamp) < this.config.minTsSendCltv;
408
+ if (expiresTooSoon) {
409
+ throw {
410
+ code: 20001,
411
+ msg: "Expiry time too low!"
412
+ };
413
+ }
414
+ }
415
+ /**
416
+ * Estimates the routing fee & confidence by either probing or routing (if probing fails), the fee is also adjusted
417
+ * according to routing fee multiplier, and subject to minimums set in config
418
+ *
419
+ * @param amountBD
420
+ * @param maxFee
421
+ * @param expiryTimestamp
422
+ * @param currentTimestamp
423
+ * @param pr
424
+ * @param metadata
425
+ * @param abortSignal
426
+ * @throws {DefinedRuntimeError} will throw an error if the destination is unreachable
427
+ */
428
+ async checkAndGetNetworkFee(amountBD, maxFee, expiryTimestamp, currentTimestamp, pr, metadata, abortSignal) {
429
+ const maxUsableCLTV = (expiryTimestamp - currentTimestamp - this.config.gracePeriod) / (this.config.bitcoinBlocktime * this.config.safetyFactor);
430
+ const blockHeight = await this.lightning.getBlockheight();
431
+ abortSignal.throwIfAborted();
432
+ metadata.times.blockheightFetched = Date.now();
433
+ const maxTimeoutBlockheight = BigInt(blockHeight) + maxUsableCLTV;
434
+ const req = {
435
+ request: pr,
436
+ amountMtokens: amountBD * 1000n,
437
+ maxFeeMtokens: maxFee * 1000n,
438
+ maxTimeoutHeight: Number(maxTimeoutBlockheight)
439
+ };
440
+ let probeOrRouteResp = await this.lightning.probe(req);
441
+ metadata.times.probeResult = Date.now();
442
+ metadata.probeResponse = { ...probeOrRouteResp };
443
+ abortSignal.throwIfAborted();
444
+ if (probeOrRouteResp == null) {
445
+ if (!this.config.allowProbeFailedSwaps)
446
+ throw {
447
+ code: 20002,
448
+ msg: "Cannot route the payment!"
449
+ };
450
+ const routeResp = await this.lightning.route(req);
451
+ metadata.times.routingResult = Date.now();
452
+ metadata.routeResponse = { ...routeResp };
453
+ abortSignal.throwIfAborted();
454
+ if (routeResp == null)
455
+ throw {
456
+ code: 20002,
457
+ msg: "Cannot route the payment!"
458
+ };
459
+ this.logger.info("checkAndGetNetworkFee(): routing result," +
460
+ " destination: " + routeResp.destination +
461
+ " confidence: " + routeResp.confidence +
462
+ " fee mtokens: " + routeResp.feeMtokens.toString(10));
463
+ probeOrRouteResp = routeResp;
464
+ }
465
+ else {
466
+ this.logger.info("checkAndGetNetworkFee(): route probed," +
467
+ " destination: " + probeOrRouteResp.destination +
468
+ " confidence: " + probeOrRouteResp.confidence +
469
+ " fee mtokens: " + probeOrRouteResp.feeMtokens.toString(10));
470
+ }
471
+ const safeFeeTokens = (probeOrRouteResp.feeMtokens + 999n) / 1000n;
472
+ let actualRoutingFee = safeFeeTokens * this.config.routingFeeMultiplier;
473
+ const minRoutingFee = (amountBD * this.config.minLnRoutingFeePPM / 1000000n) + this.config.minLnBaseFee;
474
+ if (actualRoutingFee < minRoutingFee) {
475
+ actualRoutingFee = minRoutingFee;
476
+ if (actualRoutingFee > maxFee) {
477
+ probeOrRouteResp.confidence = 0;
478
+ }
479
+ }
480
+ if (actualRoutingFee > maxFee) {
481
+ actualRoutingFee = maxFee;
482
+ }
483
+ this.logger.debug("checkAndGetNetworkFee(): network fee calculated, amount: " + amountBD.toString(10) + " fee: " + actualRoutingFee.toString(10));
484
+ return {
485
+ networkFee: actualRoutingFee,
486
+ confidence: probeOrRouteResp.confidence
487
+ };
488
+ }
489
+ /**
490
+ * Checks and consumes (deletes & returns) exactIn authorizaton with a specific reqId
491
+ *
492
+ * @param reqId
493
+ * @throws {DefinedRuntimeError} will throw an error if the authorization doesn't exist
494
+ */
495
+ checkExactInAuthorization(reqId) {
496
+ const parsedAuth = this.exactInAuths[reqId];
497
+ if (parsedAuth == null) {
498
+ throw {
499
+ code: 20070,
500
+ msg: "Invalid reqId"
501
+ };
502
+ }
503
+ delete this.exactInAuths[reqId];
504
+ if (parsedAuth.expiry < Date.now()) {
505
+ throw {
506
+ code: 20200,
507
+ msg: "Authorization already expired!"
508
+ };
509
+ }
510
+ return parsedAuth;
511
+ }
512
+ /**
513
+ * Checks if the newly submitted PR has the same parameters (destination, cltv_delta, routes) as the initial dummy
514
+ * invoice sent for exactIn swap quote
515
+ *
516
+ * @param pr
517
+ * @param parsedAuth
518
+ * @throws {DefinedRuntimeError} will throw an error if the details don't match
519
+ */
520
+ async checkPaymentRequestMatchesInitial(pr, parsedAuth) {
521
+ const parsedRequest = await this.lightning.parsePaymentRequest(pr);
522
+ if (parsedRequest.destination !== parsedAuth.initialInvoice.destination ||
523
+ parsedRequest.cltvDelta !== parsedAuth.initialInvoice.cltvDelta ||
524
+ parsedRequest.mtokens !== parsedAuth.amount * 1000n) {
525
+ throw {
526
+ code: 20102,
527
+ msg: "Provided PR doesn't match initial!"
528
+ };
529
+ }
530
+ if (!(0, ILightningWallet_1.routesMatch)(parsedRequest.routes, parsedAuth.initialInvoice.routes)) {
531
+ throw {
532
+ code: 20102,
533
+ msg: "Provided PR doesn't match initial (routes)!"
534
+ };
535
+ }
536
+ }
537
+ startRestServer(restServer) {
538
+ restServer.use(this.path + "/payInvoiceExactIn", (0, ServerParamDecoder_1.serverParamDecoder)(10 * 1000));
539
+ restServer.post(this.path + "/payInvoiceExactIn", (0, Utils_1.expressHandlerWrapper)(async (req, res) => {
540
+ /**
541
+ * pr: string bolt11 lightning invoice
542
+ * reqId: string Identifier of the swap
543
+ * feeRate: string Fee rate to use for the init tx
544
+ */
545
+ const parsedBody = await req.paramReader.getParams({
546
+ pr: SchemaVerifier_1.FieldTypeEnum.String,
547
+ reqId: SchemaVerifier_1.FieldTypeEnum.String,
548
+ feeRate: SchemaVerifier_1.FieldTypeEnum.String
549
+ });
550
+ if (parsedBody == null) {
551
+ throw {
552
+ code: 20100,
553
+ msg: "Invalid request body"
554
+ };
555
+ }
556
+ const responseStream = res.responseStream;
557
+ const abortSignal = responseStream.getAbortSignal();
558
+ //Check request params
559
+ const parsedAuth = this.checkExactInAuthorization(parsedBody.reqId);
560
+ const { parsedPR, halfConfidence } = await this.checkPaymentRequest(parsedAuth.chainIdentifier, parsedBody.pr);
561
+ await this.checkPaymentRequestMatchesInitial(parsedBody.pr, parsedAuth);
562
+ const metadata = parsedAuth.metadata;
563
+ const sequence = base_1.BigIntBufferUtils.fromBuffer((0, crypto_1.randomBytes)(8));
564
+ const { swapContract, signer } = this.getChain(parsedAuth.chainIdentifier);
565
+ const claimHash = swapContract.getHashForHtlc(Buffer.from(parsedPR.id, "hex"));
566
+ //Create swap data
567
+ const payObject = await swapContract.createSwapData(base_1.ChainSwapType.HTLC, parsedAuth.offerer, signer.getAddress(), parsedAuth.token, parsedAuth.total, claimHash.toString("hex"), sequence, parsedAuth.swapExpiry, true, false, 0n, 0n);
568
+ metadata.times.swapCreated = Date.now();
569
+ //Sign swap data
570
+ const prefetchedSignData = parsedAuth.preFetchSignData;
571
+ const sigData = await this.getToBtcSignatureData(parsedAuth.chainIdentifier, payObject, req, abortSignal, prefetchedSignData);
572
+ metadata.times.swapSigned = Date.now();
573
+ //Create swap
574
+ const createdSwap = new ToBtcLnSwapAbs_1.ToBtcLnSwapAbs(parsedAuth.chainIdentifier, parsedPR.id, parsedBody.pr, parsedPR.mtokens, parsedAuth.swapFee, parsedAuth.swapFeeInToken, parsedAuth.quotedNetworkFee, parsedAuth.quotedNetworkFeeInToken);
575
+ createdSwap.data = payObject;
576
+ createdSwap.metadata = metadata;
577
+ createdSwap.prefix = sigData.prefix;
578
+ createdSwap.timeout = sigData.timeout;
579
+ createdSwap.signature = sigData.signature;
580
+ createdSwap.feeRate = sigData.feeRate;
581
+ await PluginManager_1.PluginManager.swapCreate(createdSwap);
582
+ await this.saveSwapData(createdSwap);
583
+ this.swapLogger.info(createdSwap, "REST: /payInvoiceExactIn: created exact in swap," +
584
+ " reqId: " + parsedBody.reqId +
585
+ " mtokens: " + parsedPR.mtokens.toString(10) +
586
+ " invoice: " + createdSwap.pr);
587
+ await responseStream.writeParamsAndEnd({
588
+ code: 20000,
589
+ msg: "Success",
590
+ data: {
591
+ maxFee: parsedAuth.quotedNetworkFeeInToken.toString(10),
592
+ swapFee: parsedAuth.swapFeeInToken.toString(10),
593
+ total: parsedAuth.total.toString(10),
594
+ confidence: halfConfidence ? parsedAuth.confidence / 2000000 : parsedAuth.confidence / 1000000,
595
+ address: signer.getAddress(),
596
+ routingFeeSats: parsedAuth.quotedNetworkFee.toString(10),
597
+ data: payObject.serialize(),
598
+ prefix: sigData.prefix,
599
+ timeout: sigData.timeout,
600
+ signature: sigData.signature
601
+ }
602
+ });
603
+ }));
604
+ restServer.use(this.path + "/payInvoice", (0, ServerParamDecoder_1.serverParamDecoder)(10 * 1000));
605
+ restServer.post(this.path + "/payInvoice", (0, Utils_1.expressHandlerWrapper)(async (req, res) => {
606
+ const metadata = { request: {}, times: {} };
607
+ const chainIdentifier = req.query.chain ?? this.chains.default;
608
+ const { swapContract, signer, chainInterface } = this.getChain(chainIdentifier);
609
+ metadata.times.requestReceived = Date.now();
610
+ /**
611
+ *Sent initially:
612
+ * pr: string bolt11 lightning invoice
613
+ * maxFee: string maximum routing fee
614
+ * expiryTimestamp: string expiry timestamp of the to be created HTLC, determines how many LN paths can be considered
615
+ * token: string Desired token to use
616
+ * offerer: string Address of the caller
617
+ * exactIn: boolean Whether to do an exact in swap instead of exact out
618
+ * amount: string Input amount for exactIn swaps
619
+ *
620
+ *Sent later:
621
+ * feeRate: string Fee rate to use for the init signature
622
+ */
623
+ const parsedBody = await req.paramReader.getParams({
624
+ pr: SchemaVerifier_1.FieldTypeEnum.String,
625
+ maxFee: SchemaVerifier_1.FieldTypeEnum.BigInt,
626
+ expiryTimestamp: SchemaVerifier_1.FieldTypeEnum.BigInt,
627
+ token: (val) => val != null &&
628
+ typeof (val) === "string" &&
629
+ this.isTokenSupported(chainIdentifier, val) ? val : null,
630
+ offerer: (val) => val != null &&
631
+ typeof (val) === "string" &&
632
+ chainInterface.isValidAddress(val) ? val : null,
633
+ exactIn: SchemaVerifier_1.FieldTypeEnum.BooleanOptional,
634
+ amount: SchemaVerifier_1.FieldTypeEnum.BigIntOptional
635
+ });
636
+ if (parsedBody == null) {
637
+ throw {
638
+ code: 20100,
639
+ msg: "Invalid request body"
640
+ };
641
+ }
642
+ metadata.request = parsedBody;
643
+ const request = {
644
+ chainIdentifier,
645
+ raw: req,
646
+ parsed: parsedBody,
647
+ metadata
648
+ };
649
+ const useToken = parsedBody.token;
650
+ const responseStream = res.responseStream;
651
+ const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
652
+ //Check request params
653
+ this.checkAmount(parsedBody.amount, parsedBody.exactIn);
654
+ this.checkMaxFee(parsedBody.maxFee);
655
+ this.checkExpiry(parsedBody.expiryTimestamp, currentTimestamp);
656
+ await this.checkVaultInitialized(chainIdentifier, parsedBody.token);
657
+ const { parsedPR, halfConfidence } = await this.checkPaymentRequest(chainIdentifier, parsedBody.pr);
658
+ const requestedAmount = {
659
+ input: !!parsedBody.exactIn,
660
+ amount: !!parsedBody.exactIn ? parsedBody.amount : (parsedPR.mtokens + 999n) / 1000n,
661
+ token: useToken
662
+ };
663
+ const fees = await this.AmountAssertions.preCheckToBtcAmounts(this.type, request, requestedAmount);
664
+ metadata.times.requestChecked = Date.now();
665
+ //Create abort controller for parallel pre-fetches
666
+ const abortController = (0, Utils_1.getAbortController)(responseStream);
667
+ //Pre-fetch
668
+ const { pricePrefetchPromise, signDataPrefetchPromise } = this.getToBtcPrefetches(chainIdentifier, useToken, responseStream, abortController);
669
+ //Check if prior payment has been made
670
+ await this.LightningAssertions.checkPriorPayment(parsedPR.id, abortController.signal);
671
+ metadata.times.priorPaymentChecked = Date.now();
672
+ //Check amounts
673
+ const { amountBD, networkFeeData, totalInToken, swapFee, swapFeeInToken, networkFeeInToken } = await this.AmountAssertions.checkToBtcAmount(this.type, request, { ...requestedAmount, pricePrefetch: pricePrefetchPromise }, fees, async (amountBD) => {
674
+ //Check if we have enough liquidity to process the swap
675
+ await this.LightningAssertions.checkLiquidity(amountBD, abortController.signal, true);
676
+ metadata.times.liquidityChecked = Date.now();
677
+ const maxFee = parsedBody.exactIn ?
678
+ await this.swapPricing.getToBtcSwapAmount(parsedBody.maxFee, useToken, chainIdentifier, null, pricePrefetchPromise) :
679
+ parsedBody.maxFee;
680
+ return await this.checkAndGetNetworkFee(amountBD, maxFee, parsedBody.expiryTimestamp, currentTimestamp, parsedBody.pr, metadata, abortController.signal);
681
+ }, abortController.signal);
682
+ metadata.times.priceCalculated = Date.now();
683
+ //For exactIn swap, just save and wait for the actual invoice to be submitted
684
+ if (parsedBody.exactIn) {
685
+ const reqId = (0, crypto_1.randomBytes)(32).toString("hex");
686
+ this.exactInAuths[reqId] = {
687
+ chainIdentifier,
688
+ reqId,
689
+ expiry: Date.now() + this.config.exactInExpiry,
690
+ amount: amountBD,
691
+ initialInvoice: parsedPR,
692
+ quotedNetworkFeeInToken: networkFeeInToken,
693
+ swapFeeInToken,
694
+ total: totalInToken,
695
+ confidence: networkFeeData.confidence,
696
+ quotedNetworkFee: networkFeeData.networkFee,
697
+ swapFee,
698
+ token: useToken,
699
+ swapExpiry: parsedBody.expiryTimestamp,
700
+ offerer: parsedBody.offerer,
701
+ preFetchSignData: signDataPrefetchPromise != null ? await signDataPrefetchPromise : null,
702
+ metadata
703
+ };
704
+ this.logger.info("REST: /payInvoice: created exact in swap," +
705
+ " reqId: " + reqId +
706
+ " amount: " + amountBD.toString(10) +
707
+ " destination: " + parsedPR.destination);
708
+ await responseStream.writeParamsAndEnd({
709
+ code: 20000,
710
+ msg: "Success",
711
+ data: {
712
+ amount: amountBD.toString(10),
713
+ reqId
714
+ }
715
+ });
716
+ return;
717
+ }
718
+ const sequence = base_1.BigIntBufferUtils.fromBuffer((0, crypto_1.randomBytes)(8));
719
+ const claimHash = swapContract.getHashForHtlc(Buffer.from(parsedPR.id, "hex"));
720
+ //Create swap data
721
+ const payObject = await swapContract.createSwapData(base_1.ChainSwapType.HTLC, parsedBody.offerer, signer.getAddress(), useToken, totalInToken, claimHash.toString("hex"), sequence, parsedBody.expiryTimestamp, true, false, 0n, 0n);
722
+ abortController.signal.throwIfAborted();
723
+ metadata.times.swapCreated = Date.now();
724
+ //Sign swap data
725
+ const sigData = await this.getToBtcSignatureData(chainIdentifier, payObject, req, abortController.signal, signDataPrefetchPromise);
726
+ metadata.times.swapSigned = Date.now();
727
+ //Create swap
728
+ const createdSwap = new ToBtcLnSwapAbs_1.ToBtcLnSwapAbs(chainIdentifier, parsedPR.id, parsedBody.pr, parsedPR.mtokens, swapFee, swapFeeInToken, networkFeeData.networkFee, networkFeeInToken);
729
+ createdSwap.data = payObject;
730
+ createdSwap.metadata = metadata;
731
+ createdSwap.prefix = sigData.prefix;
732
+ createdSwap.timeout = sigData.timeout;
733
+ createdSwap.signature = sigData.signature;
734
+ createdSwap.feeRate = sigData.feeRate;
735
+ await PluginManager_1.PluginManager.swapCreate(createdSwap);
736
+ await this.saveSwapData(createdSwap);
737
+ this.swapLogger.info(createdSwap, "REST: /payInvoice: created swap," +
738
+ " amount: " + amountBD.toString(10) +
739
+ " invoice: " + createdSwap.pr);
740
+ await responseStream.writeParamsAndEnd({
741
+ code: 20000,
742
+ msg: "Success",
743
+ data: {
744
+ maxFee: networkFeeInToken.toString(10),
745
+ swapFee: swapFeeInToken.toString(10),
746
+ total: totalInToken.toString(10),
747
+ confidence: halfConfidence ? networkFeeData.confidence / 2000000 : networkFeeData.confidence / 1000000,
748
+ address: signer.getAddress(),
749
+ routingFeeSats: networkFeeData.networkFee.toString(10),
750
+ data: payObject.serialize(),
751
+ prefix: sigData.prefix,
752
+ timeout: sigData.timeout,
753
+ signature: sigData.signature
754
+ }
755
+ });
756
+ }));
757
+ const getRefundAuthorization = (0, Utils_1.expressHandlerWrapper)(async (req, res) => {
758
+ /**
759
+ * paymentHash: string Identifier of the swap
760
+ * sequence: BN Sequence identifier of the swap
761
+ */
762
+ const parsedBody = (0, SchemaVerifier_1.verifySchema)({ ...req.body, ...req.query }, {
763
+ paymentHash: (val) => val != null &&
764
+ typeof (val) === "string" &&
765
+ val.length === 64 &&
766
+ Utils_1.HEX_REGEX.test(val) ? val : null,
767
+ sequence: SchemaVerifier_1.FieldTypeEnum.BigInt
768
+ });
769
+ if (parsedBody == null)
770
+ throw {
771
+ code: 20100,
772
+ msg: "Invalid request body/query (paymentHash/sequence)"
773
+ };
774
+ this.checkSequence(parsedBody.sequence);
775
+ const data = await this.storageManager.getData(parsedBody.paymentHash, parsedBody.sequence);
776
+ const isSwapFound = data != null;
777
+ if (isSwapFound) {
778
+ const { signer, swapContract } = this.getChain(data.chainIdentifier);
779
+ if (await swapContract.isExpired(signer.getAddress(), data.data))
780
+ throw {
781
+ _httpStatus: 200,
782
+ code: 20010,
783
+ msg: "Payment expired"
784
+ };
785
+ if (data.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE) {
786
+ const refundSigData = await swapContract.getRefundSignature(signer, data.data, this.config.refundAuthorizationTimeout);
787
+ //Double check the state after promise result
788
+ if (data.state !== ToBtcLnSwapAbs_1.ToBtcLnSwapState.NON_PAYABLE)
789
+ throw {
790
+ code: 20005,
791
+ msg: "Not committed"
792
+ };
793
+ this.swapLogger.info(data, "REST: /getRefundAuthorization: returning refund authorization, because invoice in NON_PAYABLE state, invoice: " + data.pr);
794
+ res.status(200).json({
795
+ code: 20000,
796
+ msg: "Success",
797
+ data: {
798
+ address: signer.getAddress(),
799
+ prefix: refundSigData.prefix,
800
+ timeout: refundSigData.timeout,
801
+ signature: refundSigData.signature
802
+ }
803
+ });
804
+ return;
805
+ }
806
+ }
807
+ const payment = await this.lightning.getPayment(parsedBody.paymentHash);
808
+ if (payment == null)
809
+ throw {
810
+ _httpStatus: 200,
811
+ code: 20007,
812
+ msg: "Payment not found"
813
+ };
814
+ if (payment.status === "pending")
815
+ throw {
816
+ _httpStatus: 200,
817
+ code: 20008,
818
+ msg: "Payment in-flight"
819
+ };
820
+ if (payment.status === "confirmed")
821
+ throw {
822
+ _httpStatus: 200,
823
+ code: 20006,
824
+ msg: "Already paid",
825
+ data: {
826
+ secret: payment.secret
827
+ }
828
+ };
829
+ if (payment.status === "failed")
830
+ throw {
831
+ _httpStatus: 200,
832
+ code: 20010,
833
+ msg: "Payment expired",
834
+ data: {
835
+ reason: payment.failedReason
836
+ }
837
+ };
838
+ });
839
+ restServer.post(this.path + '/getRefundAuthorization', getRefundAuthorization);
840
+ restServer.get(this.path + '/getRefundAuthorization', getRefundAuthorization);
841
+ this.logger.info("started at path: ", this.path);
842
+ }
843
+ async init() {
844
+ await this.loadData(ToBtcLnSwapAbs_1.ToBtcLnSwapAbs);
845
+ //Check if all swaps contain a valid amount
846
+ for (let { obj: swap } of await this.storageManager.query([])) {
847
+ if (swap.amount == null || swap.lnPaymentHash == null) {
848
+ const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
849
+ swap.amount = (parsedPR.mtokens + 999n) / 1000n;
850
+ swap.lnPaymentHash = parsedPR.id;
851
+ }
852
+ }
853
+ this.subscribeToEvents();
854
+ await PluginManager_1.PluginManager.serviceInitialize(this);
855
+ }
856
+ getInfoData() {
857
+ return {
858
+ minCltv: Number(this.config.minSendCltv),
859
+ minTimestampCltv: Number(this.config.minTsSendCltv)
860
+ };
861
+ }
862
+ }
863
+ exports.ToBtcLnAbs = ToBtcLnAbs;