@atomiqlabs/lp-lib 14.0.0-dev.11 → 14.0.0-dev.13

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