@atomiqlabs/lp-lib 16.2.0 → 17.0.0

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