@atomiqlabs/lp-lib 15.0.14 → 15.0.16

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