@atomiqlabs/lp-lib 14.0.0-dev.21 → 14.0.0-dev.24
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.
- package/LICENSE +201 -201
- package/dist/fees/IBtcFeeEstimator.d.ts +3 -3
- package/dist/fees/IBtcFeeEstimator.js +2 -2
- package/dist/index.d.ts +42 -42
- package/dist/index.js +58 -58
- package/dist/info/InfoHandler.d.ts +17 -17
- package/dist/info/InfoHandler.js +61 -61
- package/dist/plugins/IPlugin.d.ts +143 -143
- package/dist/plugins/IPlugin.js +34 -34
- package/dist/plugins/PluginManager.d.ts +112 -112
- package/dist/plugins/PluginManager.js +259 -259
- package/dist/prices/BinanceSwapPrice.d.ts +26 -26
- package/dist/prices/BinanceSwapPrice.js +92 -92
- package/dist/prices/CoinGeckoSwapPrice.d.ts +30 -30
- package/dist/prices/CoinGeckoSwapPrice.js +64 -64
- package/dist/prices/ISwapPrice.d.ts +43 -43
- package/dist/prices/ISwapPrice.js +55 -55
- package/dist/prices/OKXSwapPrice.d.ts +26 -26
- package/dist/prices/OKXSwapPrice.js +92 -92
- package/dist/storage/IIntermediaryStorage.d.ts +18 -18
- package/dist/storage/IIntermediaryStorage.js +2 -2
- package/dist/storagemanager/IntermediaryStorageManager.d.ts +19 -19
- package/dist/storagemanager/IntermediaryStorageManager.js +111 -111
- package/dist/storagemanager/StorageManager.d.ts +13 -13
- package/dist/storagemanager/StorageManager.js +64 -64
- package/dist/swaps/SwapHandler.d.ts +153 -153
- package/dist/swaps/SwapHandler.js +160 -160
- package/dist/swaps/SwapHandlerSwap.d.ts +79 -79
- package/dist/swaps/SwapHandlerSwap.js +78 -78
- package/dist/swaps/assertions/AmountAssertions.d.ts +28 -28
- package/dist/swaps/assertions/AmountAssertions.js +72 -72
- package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -76
- package/dist/swaps/assertions/FromBtcAmountAssertions.js +180 -180
- package/dist/swaps/assertions/LightningAssertions.d.ts +44 -44
- package/dist/swaps/assertions/LightningAssertions.js +86 -86
- package/dist/swaps/assertions/ToBtcAmountAssertions.d.ts +53 -53
- package/dist/swaps/assertions/ToBtcAmountAssertions.js +150 -150
- package/dist/swaps/escrow/EscrowHandler.d.ts +51 -51
- package/dist/swaps/escrow/EscrowHandler.js +158 -158
- package/dist/swaps/escrow/EscrowHandlerSwap.d.ts +35 -35
- package/dist/swaps/escrow/EscrowHandlerSwap.js +69 -69
- package/dist/swaps/escrow/FromBtcBaseSwap.d.ts +14 -14
- package/dist/swaps/escrow/FromBtcBaseSwap.js +32 -32
- package/dist/swaps/escrow/FromBtcBaseSwapHandler.d.ts +102 -102
- package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +210 -210
- package/dist/swaps/escrow/ToBtcBaseSwap.d.ts +36 -36
- package/dist/swaps/escrow/ToBtcBaseSwap.js +67 -67
- package/dist/swaps/escrow/ToBtcBaseSwapHandler.d.ts +53 -53
- package/dist/swaps/escrow/ToBtcBaseSwapHandler.js +81 -81
- package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.d.ts +83 -83
- package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +318 -318
- package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.d.ts +21 -21
- package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.js +50 -50
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.d.ts +107 -107
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +675 -675
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +33 -33
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +91 -91
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +111 -111
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +684 -684
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +55 -55
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +120 -120
- package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +171 -171
- package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +706 -706
- package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +26 -26
- package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +62 -62
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +177 -177
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +863 -863
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +24 -24
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +58 -58
- package/dist/swaps/spv_vault_swap/SpvVault.d.ts +40 -41
- package/dist/swaps/spv_vault_swap/SpvVault.js +111 -111
- package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +67 -67
- package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +158 -158
- package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -68
- package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +492 -492
- package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +52 -52
- package/dist/swaps/spv_vault_swap/SpvVaults.js +394 -364
- package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +51 -51
- package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +650 -650
- package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.d.ts +52 -52
- package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.js +118 -118
- package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +76 -76
- package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +494 -494
- package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
- package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
- package/dist/utils/BitcoinUtils.d.ts +2 -2
- package/dist/utils/BitcoinUtils.js +45 -45
- package/dist/utils/Utils.d.ts +29 -29
- package/dist/utils/Utils.js +89 -89
- package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
- package/dist/utils/paramcoders/IParamReader.js +2 -2
- package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
- package/dist/utils/paramcoders/IParamWriter.js +2 -2
- package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
- package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
- package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
- package/dist/utils/paramcoders/ParamDecoder.js +222 -222
- package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
- package/dist/utils/paramcoders/ParamEncoder.js +22 -22
- package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
- package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
- package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
- package/dist/utils/paramcoders/server/ServerParamDecoder.js +107 -107
- package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
- package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
- package/dist/wallets/IBitcoinWallet.d.ts +67 -67
- package/dist/wallets/IBitcoinWallet.js +2 -2
- package/dist/wallets/ILightningWallet.d.ts +117 -117
- package/dist/wallets/ILightningWallet.js +37 -37
- package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
- package/dist/wallets/ISpvVaultSigner.js +2 -2
- package/package.json +36 -36
- package/src/fees/IBtcFeeEstimator.ts +6 -6
- package/src/index.ts +53 -53
- package/src/info/InfoHandler.ts +106 -106
- package/src/plugins/IPlugin.ts +168 -168
- package/src/plugins/PluginManager.ts +336 -336
- package/src/prices/BinanceSwapPrice.ts +113 -113
- package/src/prices/CoinGeckoSwapPrice.ts +87 -87
- package/src/prices/ISwapPrice.ts +88 -88
- package/src/prices/OKXSwapPrice.ts +113 -113
- package/src/storage/IIntermediaryStorage.ts +19 -19
- package/src/storagemanager/IntermediaryStorageManager.ts +118 -118
- package/src/storagemanager/StorageManager.ts +78 -78
- package/src/swaps/SwapHandler.ts +277 -277
- package/src/swaps/SwapHandlerSwap.ts +141 -141
- package/src/swaps/assertions/AmountAssertions.ts +76 -76
- package/src/swaps/assertions/FromBtcAmountAssertions.ts +246 -246
- package/src/swaps/assertions/LightningAssertions.ts +103 -103
- package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
- package/src/swaps/escrow/EscrowHandler.ts +179 -179
- package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
- package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
- package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +286 -286
- package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
- package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
- package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +452 -452
- package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
- package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +856 -856
- package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -141
- package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +850 -850
- package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -196
- package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +879 -879
- package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +102 -102
- package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1112 -1112
- package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
- package/src/swaps/spv_vault_swap/SpvVault.ts +143 -143
- package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +225 -225
- package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +628 -628
- package/src/swaps/spv_vault_swap/SpvVaults.ts +469 -435
- package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +747 -747
- package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
- package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +590 -590
- package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
- package/src/utils/BitcoinUtils.ts +42 -42
- package/src/utils/Utils.ts +104 -104
- package/src/utils/paramcoders/IParamReader.ts +7 -7
- package/src/utils/paramcoders/IParamWriter.ts +8 -8
- package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
- package/src/utils/paramcoders/ParamDecoder.ts +218 -218
- package/src/utils/paramcoders/ParamEncoder.ts +29 -29
- package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
- package/src/utils/paramcoders/server/ServerParamDecoder.ts +118 -118
- package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
- package/src/wallets/IBitcoinWallet.ts +68 -68
- package/src/wallets/ILightningWallet.ts +178 -178
- package/src/wallets/ISpvVaultSigner.ts +10 -10
|
@@ -1,1112 +1,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
|
-
|
|
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 ?? this.chains.default;
|
|
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
|
+
|
|
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 ?? this.chains.default;
|
|
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
|
+
}
|