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