@atomiqlabs/lp-lib 16.2.0 → 16.2.1

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