@atomiqlabs/lp-lib 13.0.0-beta.9 → 13.0.0

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