@atomiqlabs/sdk 8.1.8 → 8.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
- package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
- package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
- package/dist/enums/FeeType.d.ts +7 -0
- package/dist/enums/FeeType.js +7 -0
- package/dist/enums/SwapAmountType.d.ts +7 -0
- package/dist/enums/SwapAmountType.js +7 -0
- package/dist/enums/SwapDirection.d.ts +7 -0
- package/dist/enums/SwapDirection.js +7 -0
- package/dist/enums/SwapType.d.ts +62 -1
- package/dist/enums/SwapType.js +62 -1
- package/dist/errors/IntermediaryError.d.ts +4 -0
- package/dist/errors/IntermediaryError.js +1 -0
- package/dist/errors/RequestError.d.ts +15 -1
- package/dist/errors/RequestError.js +8 -0
- package/dist/errors/UserError.d.ts +1 -0
- package/dist/errors/UserError.js +1 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.js +3 -4
- package/dist/intermediaries/Intermediary.d.ts +57 -10
- package/dist/intermediaries/Intermediary.js +37 -10
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +55 -22
- package/dist/intermediaries/IntermediaryDiscovery.js +35 -22
- package/dist/prices/RedundantSwapPrice.d.ts +24 -3
- package/dist/prices/RedundantSwapPrice.js +21 -1
- package/dist/prices/SingleSwapPrice.d.ts +9 -6
- package/dist/prices/SingleSwapPrice.js +10 -7
- package/dist/prices/SwapPriceWithChain.d.ts +54 -16
- package/dist/prices/SwapPriceWithChain.js +58 -20
- package/dist/prices/abstract/ISwapPrice.d.ts +94 -45
- package/dist/prices/abstract/ISwapPrice.js +103 -55
- package/dist/prices/providers/BinancePriceProvider.d.ts +7 -0
- package/dist/prices/providers/BinancePriceProvider.js +7 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +6 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.js +6 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +6 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +6 -0
- package/dist/prices/providers/CustomPriceProvider.d.ts +11 -0
- package/dist/prices/providers/CustomPriceProvider.js +11 -0
- package/dist/prices/providers/KrakenPriceProvider.d.ts +9 -0
- package/dist/prices/providers/KrakenPriceProvider.js +9 -0
- package/dist/prices/providers/OKXPriceProvider.d.ts +6 -0
- package/dist/prices/providers/OKXPriceProvider.js +6 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
- package/dist/storage/IUnifiedStorage.d.ts +19 -7
- package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
- package/dist/storage/UnifiedSwapStorage.js +29 -1
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
- package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
- package/dist/storage-browser/LocalStorageManager.js +25 -1
- package/dist/swapper/Swapper.d.ts +302 -221
- package/dist/swapper/Swapper.js +376 -344
- package/dist/swapper/SwapperFactory.d.ts +41 -17
- package/dist/swapper/SwapperFactory.js +23 -2
- package/dist/swapper/SwapperUtils.d.ts +75 -28
- package/dist/swapper/SwapperUtils.js +107 -60
- package/dist/swapper/SwapperWithChain.d.ts +286 -91
- package/dist/swapper/SwapperWithChain.js +218 -64
- package/dist/swapper/SwapperWithSigner.d.ts +229 -80
- package/dist/swapper/SwapperWithSigner.js +190 -44
- package/dist/swaps/IAddressSwap.d.ts +10 -1
- package/dist/swaps/IAddressSwap.js +2 -1
- package/dist/swaps/IBTCWalletSwap.d.ts +24 -6
- package/dist/swaps/IBTCWalletSwap.js +2 -1
- package/dist/swaps/IClaimableSwap.d.ts +36 -4
- package/dist/swaps/IClaimableSwap.js +2 -1
- package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
- package/dist/swaps/IRefundableSwap.d.ts +29 -3
- package/dist/swaps/IRefundableSwap.js +2 -1
- package/dist/swaps/ISwap.d.ts +159 -21
- package/dist/swaps/ISwap.js +90 -33
- package/dist/swaps/ISwapWithGasDrop.d.ts +6 -0
- package/dist/swaps/ISwapWithGasDrop.js +1 -0
- package/dist/swaps/ISwapWrapper.d.ts +157 -48
- package/dist/swaps/ISwapWrapper.js +130 -72
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +49 -6
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +94 -29
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +90 -27
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +275 -58
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +516 -239
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +76 -25
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +131 -49
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +311 -51
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +542 -193
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +209 -53
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +449 -242
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +197 -56
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +326 -189
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +60 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +74 -31
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +36 -13
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +65 -19
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +328 -92
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +460 -219
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +244 -124
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +146 -18
- package/dist/swaps/trusted/ln/LnForGasSwap.js +173 -43
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +200 -47
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +230 -78
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
- package/dist/types/AmountData.d.ts +2 -1
- package/dist/types/CustomPriceFunction.d.ts +7 -1
- package/dist/types/SwapExecutionAction.d.ts +74 -4
- package/dist/types/SwapWithSigner.d.ts +4 -1
- package/dist/types/SwapWithSigner.js +5 -2
- package/dist/types/Token.d.ts +11 -5
- package/dist/types/Token.js +6 -3
- package/dist/types/TokenAmount.d.ts +3 -0
- package/dist/types/TokenAmount.js +2 -0
- package/dist/types/fees/Fee.d.ts +2 -1
- package/dist/types/fees/FeeBreakdown.d.ts +2 -1
- package/dist/types/fees/PercentagePPM.d.ts +2 -0
- package/dist/types/fees/PercentagePPM.js +1 -0
- package/dist/types/lnurl/LNURLPay.d.ts +14 -6
- package/dist/types/lnurl/LNURLPay.js +6 -2
- package/dist/types/lnurl/LNURLWithdraw.d.ts +12 -5
- package/dist/types/lnurl/LNURLWithdraw.js +6 -2
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +20 -0
- package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
- package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +3 -1
- package/dist/utils/BitcoinUtils.d.ts +1 -0
- package/dist/utils/BitcoinUtils.js +5 -1
- package/dist/utils/SwapUtils.d.ts +56 -1
- package/dist/utils/SwapUtils.js +53 -1
- package/dist/utils/TokenUtils.d.ts +10 -2
- package/dist/utils/TokenUtils.js +12 -4
- package/package.json +3 -3
- package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
- package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
- package/src/enums/FeeType.ts +7 -0
- package/src/enums/SwapAmountType.ts +7 -0
- package/src/enums/SwapDirection.ts +7 -0
- package/src/enums/SwapType.ts +62 -2
- package/src/errors/IntermediaryError.ts +4 -0
- package/src/errors/RequestError.ts +15 -1
- package/src/errors/UserError.ts +1 -0
- package/src/index.ts +6 -5
- package/src/intermediaries/Intermediary.ts +57 -10
- package/src/intermediaries/IntermediaryDiscovery.ts +60 -27
- package/src/prices/RedundantSwapPrice.ts +24 -4
- package/src/prices/SingleSwapPrice.ts +10 -7
- package/src/prices/SwapPriceWithChain.ts +59 -21
- package/src/prices/abstract/ISwapPrice.ts +114 -65
- package/src/prices/providers/BinancePriceProvider.ts +7 -0
- package/src/prices/providers/CoinGeckoPriceProvider.ts +6 -0
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +6 -0
- package/src/prices/providers/CustomPriceProvider.ts +11 -0
- package/src/prices/providers/KrakenPriceProvider.ts +9 -0
- package/src/prices/providers/OKXPriceProvider.ts +6 -0
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
- package/src/storage/IUnifiedStorage.ts +19 -7
- package/src/storage/UnifiedSwapStorage.ts +33 -3
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
- package/src/storage-browser/LocalStorageManager.ts +25 -1
- package/src/swapper/Swapper.ts +512 -378
- package/src/swapper/SwapperFactory.ts +44 -21
- package/src/swapper/SwapperUtils.ts +107 -60
- package/src/swapper/SwapperWithChain.ts +320 -81
- package/src/swapper/SwapperWithSigner.ts +263 -56
- package/src/swaps/IAddressSwap.ts +11 -1
- package/src/swaps/IBTCWalletSwap.ts +24 -8
- package/src/swaps/IClaimableSwap.ts +39 -4
- package/src/swaps/IClaimableSwapWrapper.ts +11 -2
- package/src/swaps/IRefundableSwap.ts +32 -3
- package/src/swaps/ISwap.ts +221 -82
- package/src/swaps/ISwapWithGasDrop.ts +6 -0
- package/src/swaps/ISwapWrapper.ts +212 -94
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +62 -18
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +120 -51
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +559 -256
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +155 -61
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +590 -226
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +470 -243
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +352 -193
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +86 -39
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +88 -33
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +534 -263
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +289 -148
- package/src/swaps/trusted/ln/LnForGasSwap.ts +184 -45
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +260 -86
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
- package/src/types/AmountData.ts +2 -1
- package/src/types/CustomPriceFunction.ts +7 -1
- package/src/types/SwapExecutionAction.ts +84 -5
- package/src/types/SwapWithSigner.ts +7 -3
- package/src/types/Token.ts +12 -5
- package/src/types/TokenAmount.ts +3 -0
- package/src/types/fees/Fee.ts +2 -1
- package/src/types/fees/FeeBreakdown.ts +2 -1
- package/src/types/fees/PercentagePPM.ts +2 -0
- package/src/types/lnurl/LNURLPay.ts +14 -6
- package/src/types/lnurl/LNURLWithdraw.ts +12 -5
- package/src/types/wallets/LightningInvoiceCreateService.ts +26 -0
- package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
- package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +3 -1
- package/src/utils/BitcoinUtils.ts +5 -0
- package/src/utils/SwapUtils.ts +61 -1
- package/src/utils/TokenUtils.ts +12 -4
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
- package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
- package/dist/bitcoin/LightningNetworkApi.js +0 -2
- package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
- package/dist/bitcoin/mempool/MempoolApi.js +0 -311
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
- package/dist/errors/PaymentAuthError.d.ts +0 -11
- package/dist/errors/PaymentAuthError.js +0 -23
- package/src/errors/PaymentAuthError.ts +0 -26
|
@@ -17,56 +17,107 @@ const Token_1 = require("../../../../types/Token");
|
|
|
17
17
|
const Logger_1 = require("../../../../utils/Logger");
|
|
18
18
|
const TimeoutUtils_1 = require("../../../../utils/TimeoutUtils");
|
|
19
19
|
const LNURLWithdraw_1 = require("../../../../types/lnurl/LNURLWithdraw");
|
|
20
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
20
21
|
/**
|
|
21
|
-
* State enum for
|
|
22
|
+
* State enum for legacy Lightning -> Smart chain swaps
|
|
22
23
|
* @category Swaps
|
|
23
24
|
*/
|
|
24
25
|
var FromBTCLNSwapState;
|
|
25
26
|
(function (FromBTCLNSwapState) {
|
|
27
|
+
/**
|
|
28
|
+
* Swap has failed as the user didn't settle the HTLC on the destination before expiration
|
|
29
|
+
*/
|
|
26
30
|
FromBTCLNSwapState[FromBTCLNSwapState["FAILED"] = -4] = "FAILED";
|
|
31
|
+
/**
|
|
32
|
+
* Swap has expired for good and there is no way how it can be executed anymore
|
|
33
|
+
*/
|
|
27
34
|
FromBTCLNSwapState[FromBTCLNSwapState["QUOTE_EXPIRED"] = -3] = "QUOTE_EXPIRED";
|
|
35
|
+
/**
|
|
36
|
+
* A swap is almost expired, and it should be presented to the user as expired, though
|
|
37
|
+
* there is still a chance that it will be processed
|
|
38
|
+
*/
|
|
28
39
|
FromBTCLNSwapState[FromBTCLNSwapState["QUOTE_SOFT_EXPIRED"] = -2] = "QUOTE_SOFT_EXPIRED";
|
|
40
|
+
/**
|
|
41
|
+
* Swap HTLC on the destination chain has expired, it is not safe anymore to settle (claim) the
|
|
42
|
+
* swap on the destination smart chain.
|
|
43
|
+
*/
|
|
29
44
|
FromBTCLNSwapState[FromBTCLNSwapState["EXPIRED"] = -1] = "EXPIRED";
|
|
45
|
+
/**
|
|
46
|
+
* Swap quote was created, use {@link FromBTCLNSwap.getAddress} or {@link FromBTCLNSwap.getHyperlink}
|
|
47
|
+
* to get the bolt11 lightning network invoice to pay to initiate the swap, then use the
|
|
48
|
+
* {@link FromBTCLNSwap.waitForPayment} to wait till the lightning network payment is received
|
|
49
|
+
* by the intermediary (LP)
|
|
50
|
+
*/
|
|
30
51
|
FromBTCLNSwapState[FromBTCLNSwapState["PR_CREATED"] = 0] = "PR_CREATED";
|
|
52
|
+
/**
|
|
53
|
+
* Lightning network payment has been received by the intermediary (LP), the user can now settle
|
|
54
|
+
* the swap on the destination smart chain side with {@link FromBTCLNSwap.commitAndClaim} (if
|
|
55
|
+
* the underlying chain supports it - check with {@link FromBTCLNSwap.canCommitAndClaimInOneShot}),
|
|
56
|
+
* or by calling {@link FromBTCLNSwap.commit} and {@link FromBTCLNSwap.claim} separately.
|
|
57
|
+
*/
|
|
31
58
|
FromBTCLNSwapState[FromBTCLNSwapState["PR_PAID"] = 1] = "PR_PAID";
|
|
59
|
+
/**
|
|
60
|
+
* Swap escrow HTLC has been created on the destination chain. Continue by claiming it with the
|
|
61
|
+
* {@link FromBTCLNSwap.claim} or {@link FromBTCLNSwap.txsClaim} function.
|
|
62
|
+
*/
|
|
32
63
|
FromBTCLNSwapState[FromBTCLNSwapState["CLAIM_COMMITED"] = 2] = "CLAIM_COMMITED";
|
|
64
|
+
/**
|
|
65
|
+
* Swap successfully settled and funds received on the destination chain
|
|
66
|
+
*/
|
|
33
67
|
FromBTCLNSwapState[FromBTCLNSwapState["CLAIM_CLAIMED"] = 3] = "CLAIM_CLAIMED";
|
|
34
68
|
})(FromBTCLNSwapState = exports.FromBTCLNSwapState || (exports.FromBTCLNSwapState = {}));
|
|
35
69
|
function isFromBTCLNSwapInit(obj) {
|
|
36
|
-
return typeof obj.pr === "string" &&
|
|
37
|
-
typeof obj.secret === "string" &&
|
|
70
|
+
return (obj.pr == null || typeof obj.pr === "string") &&
|
|
71
|
+
(obj.secret == null || typeof obj.secret === "string") &&
|
|
38
72
|
(obj.lnurl == null || typeof (obj.lnurl) === "string") &&
|
|
39
73
|
(obj.lnurlK1 == null || typeof (obj.lnurlK1) === "string") &&
|
|
40
74
|
(obj.lnurlCallback == null || typeof (obj.lnurlCallback) === "string") &&
|
|
41
75
|
(0, IEscrowSelfInitSwap_1.isIEscrowSelfInitSwapInit)(obj);
|
|
42
76
|
}
|
|
43
77
|
exports.isFromBTCLNSwapInit = isFromBTCLNSwapInit;
|
|
78
|
+
/**
|
|
79
|
+
* Legacy escrow (HTLC) based swap for Bitcoin Lightning -> Smart chains, requires manual settlement
|
|
80
|
+
* of the swap on the destination network once the lightning network payment is received by the LP.
|
|
81
|
+
*
|
|
82
|
+
* @category Swaps
|
|
83
|
+
*/
|
|
44
84
|
class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
45
|
-
|
|
46
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Sets the LNURL data for the swap
|
|
87
|
+
*
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
_setLNURLData(lnurl, lnurlK1, lnurlCallback) {
|
|
91
|
+
this.lnurl = lnurl;
|
|
92
|
+
this.lnurlK1 = lnurlK1;
|
|
93
|
+
this.lnurlCallback = lnurlCallback;
|
|
47
94
|
}
|
|
48
95
|
constructor(wrapper, initOrObject) {
|
|
49
96
|
if (isFromBTCLNSwapInit(initOrObject) && initOrObject.url != null)
|
|
50
97
|
initOrObject.url += "/frombtcln";
|
|
51
98
|
super(wrapper, initOrObject);
|
|
52
|
-
this.inputToken = Token_1.BitcoinTokens.BTCLN;
|
|
53
99
|
this.TYPE = SwapType_1.SwapType.FROM_BTCLN;
|
|
100
|
+
/**
|
|
101
|
+
* @internal
|
|
102
|
+
*/
|
|
103
|
+
this.inputToken = Token_1.BitcoinTokens.BTCLN;
|
|
54
104
|
this.lnurlFailSignal = new AbortController();
|
|
55
105
|
this.prPosted = false;
|
|
56
106
|
if (isFromBTCLNSwapInit(initOrObject)) {
|
|
57
|
-
this.
|
|
107
|
+
this._state = FromBTCLNSwapState.PR_CREATED;
|
|
58
108
|
this.pr = initOrObject.pr;
|
|
59
109
|
this.secret = initOrObject.secret;
|
|
60
110
|
this.initialSwapData = initOrObject.initialSwapData;
|
|
61
111
|
this.lnurl = initOrObject.lnurl;
|
|
62
112
|
this.lnurlK1 = initOrObject.lnurlK1;
|
|
63
113
|
this.lnurlCallback = initOrObject.lnurlCallback;
|
|
114
|
+
this.usesClaimHashAsId = true;
|
|
64
115
|
}
|
|
65
116
|
else {
|
|
66
117
|
this.pr = initOrObject.pr;
|
|
67
118
|
this.secret = initOrObject.secret;
|
|
68
119
|
if (initOrObject.initialSwapData == null) {
|
|
69
|
-
this.initialSwapData = this.
|
|
120
|
+
this.initialSwapData = this._data;
|
|
70
121
|
}
|
|
71
122
|
else {
|
|
72
123
|
this.initialSwapData = base_1.SwapData.deserialize(initOrObject.initialSwapData);
|
|
@@ -75,34 +126,46 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
75
126
|
this.lnurlK1 = initOrObject.lnurlK1;
|
|
76
127
|
this.lnurlCallback = initOrObject.lnurlCallback;
|
|
77
128
|
this.prPosted = initOrObject.prPosted;
|
|
78
|
-
if (this.
|
|
79
|
-
this.initialSwapData = this.
|
|
80
|
-
delete this.
|
|
129
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED && this._data != null) {
|
|
130
|
+
this.initialSwapData = this._data;
|
|
131
|
+
delete this._data;
|
|
81
132
|
}
|
|
133
|
+
this.usesClaimHashAsId = initOrObject.usesClaimHashAsId ?? false;
|
|
82
134
|
}
|
|
83
135
|
this.tryRecomputeSwapPrice();
|
|
84
136
|
this.logger = (0, Logger_1.getLogger)("FromBTCLN(" + this.getIdentifierHashString() + "): ");
|
|
85
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* @inheritDoc
|
|
140
|
+
* @internal
|
|
141
|
+
*/
|
|
142
|
+
getSwapData() {
|
|
143
|
+
return this._data ?? this.initialSwapData;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @inheritDoc
|
|
147
|
+
* @internal
|
|
148
|
+
*/
|
|
86
149
|
upgradeVersion() {
|
|
87
150
|
if (this.version == null) {
|
|
88
|
-
switch (this.
|
|
151
|
+
switch (this._state) {
|
|
89
152
|
case -2:
|
|
90
|
-
this.
|
|
153
|
+
this._state = FromBTCLNSwapState.QUOTE_EXPIRED;
|
|
91
154
|
break;
|
|
92
155
|
case -1:
|
|
93
|
-
this.
|
|
156
|
+
this._state = FromBTCLNSwapState.FAILED;
|
|
94
157
|
break;
|
|
95
158
|
case 0:
|
|
96
|
-
this.
|
|
159
|
+
this._state = FromBTCLNSwapState.PR_CREATED;
|
|
97
160
|
break;
|
|
98
161
|
case 1:
|
|
99
|
-
this.
|
|
162
|
+
this._state = FromBTCLNSwapState.PR_PAID;
|
|
100
163
|
break;
|
|
101
164
|
case 2:
|
|
102
|
-
this.
|
|
165
|
+
this._state = FromBTCLNSwapState.CLAIM_COMMITED;
|
|
103
166
|
break;
|
|
104
167
|
case 3:
|
|
105
|
-
this.
|
|
168
|
+
this._state = FromBTCLNSwapState.CLAIM_CLAIMED;
|
|
106
169
|
break;
|
|
107
170
|
}
|
|
108
171
|
this.version = 1;
|
|
@@ -110,125 +173,215 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
110
173
|
}
|
|
111
174
|
//////////////////////////////
|
|
112
175
|
//// Getters & utils
|
|
176
|
+
/**
|
|
177
|
+
* @inheritDoc
|
|
178
|
+
* @internal
|
|
179
|
+
*/
|
|
113
180
|
getIdentifierHash() {
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
181
|
+
const idBuffer = this.usesClaimHashAsId
|
|
182
|
+
? buffer_1.Buffer.from(this.getClaimHash(), "hex")
|
|
183
|
+
: this.getPaymentHash();
|
|
184
|
+
if (this._randomNonce == null)
|
|
185
|
+
return idBuffer;
|
|
186
|
+
return buffer_1.Buffer.concat([idBuffer, buffer_1.Buffer.from(this._randomNonce, "hex")]);
|
|
118
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns the payment hash of the swap and lightning network invoice, or `null` if not known (i.e. if
|
|
190
|
+
* the swap was recovered from on-chain data, the payment hash might not be known)
|
|
191
|
+
*
|
|
192
|
+
* @internal
|
|
193
|
+
*/
|
|
119
194
|
getPaymentHash() {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
195
|
+
if (this.pr == null)
|
|
196
|
+
return null;
|
|
197
|
+
if (this.pr.toLowerCase().startsWith("ln")) {
|
|
198
|
+
const parsed = (0, bolt11_1.decode)(this.pr);
|
|
199
|
+
if (parsed.tagsObject.payment_hash == null)
|
|
200
|
+
throw new Error("Swap invoice has no payment hash field!");
|
|
201
|
+
return buffer_1.Buffer.from(parsed.tagsObject.payment_hash, "hex");
|
|
202
|
+
}
|
|
203
|
+
return buffer_1.Buffer.from(this.pr, "hex");
|
|
124
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* @inheritDoc
|
|
207
|
+
* @internal
|
|
208
|
+
*/
|
|
125
209
|
canCommit() {
|
|
126
|
-
return this.
|
|
210
|
+
return this._state === FromBTCLNSwapState.PR_PAID;
|
|
127
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* @inheritDoc
|
|
214
|
+
*/
|
|
128
215
|
getInputAddress() {
|
|
129
|
-
return this.lnurl ?? this.pr;
|
|
216
|
+
return this.lnurl ?? this.pr ?? null;
|
|
130
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* @inheritDoc
|
|
220
|
+
*/
|
|
131
221
|
getInputTxId() {
|
|
132
|
-
|
|
222
|
+
const paymentHash = this.getPaymentHash();
|
|
223
|
+
if (paymentHash == null)
|
|
224
|
+
return null;
|
|
225
|
+
return paymentHash.toString("hex");
|
|
133
226
|
}
|
|
134
227
|
/**
|
|
135
|
-
* Returns the lightning network BOLT11 invoice that needs to be paid as an input to the swap
|
|
228
|
+
* Returns the lightning network BOLT11 invoice that needs to be paid as an input to the swap.
|
|
229
|
+
*
|
|
230
|
+
* In case the swap is recovered from on-chain data, the address returned might be just a payment hash,
|
|
231
|
+
* as it is impossible to retrieve the actual lightning network invoice paid purely from on-chain
|
|
232
|
+
* data.
|
|
136
233
|
*/
|
|
137
234
|
getAddress() {
|
|
138
|
-
return this.pr;
|
|
235
|
+
return this.pr ?? "";
|
|
139
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* In case the swap is recovered from on-chain data, the address returned might be just a payment hash,
|
|
239
|
+
* as it is impossible to retrieve the actual lightning network invoice paid purely from on-chain
|
|
240
|
+
* data.
|
|
241
|
+
*
|
|
242
|
+
* @inheritDoc
|
|
243
|
+
*/
|
|
140
244
|
getHyperlink() {
|
|
141
|
-
return "lightning:" + this.pr.toUpperCase();
|
|
245
|
+
return this.pr == null ? "" : "lightning:" + this.pr.toUpperCase();
|
|
142
246
|
}
|
|
143
247
|
/**
|
|
144
248
|
* Returns the timeout time (in UNIX milliseconds) when the swap will definitelly be considered as expired
|
|
145
249
|
* if the LP doesn't make it expired sooner
|
|
146
250
|
*/
|
|
147
251
|
getDefinitiveExpiryTime() {
|
|
252
|
+
if (this.pr == null || !this.pr.toLowerCase().startsWith("ln"))
|
|
253
|
+
return 0;
|
|
148
254
|
const decoded = (0, bolt11_1.decode)(this.pr);
|
|
149
255
|
if (decoded.timeExpireDate == null)
|
|
150
256
|
throw new Error("Swap invoice doesn't contain expiry date field!");
|
|
151
257
|
const finalCltvExpiryDelta = decoded.tagsObject.min_final_cltv_expiry ?? 144;
|
|
152
|
-
const finalCltvExpiryDelay = finalCltvExpiryDelta * this.wrapper.
|
|
258
|
+
const finalCltvExpiryDelay = finalCltvExpiryDelta * this.wrapper._options.bitcoinBlocktime * this.wrapper._options.safetyFactor;
|
|
153
259
|
return (decoded.timeExpireDate + finalCltvExpiryDelay) * 1000;
|
|
154
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Returns timeout time (in UNIX milliseconds) when the swap htlc will expire
|
|
263
|
+
*/
|
|
264
|
+
getHtlcTimeoutTime() {
|
|
265
|
+
if (this._data == null)
|
|
266
|
+
return null;
|
|
267
|
+
return Number(this.wrapper._getHtlcTimeout(this._data)) * 1000;
|
|
268
|
+
}
|
|
155
269
|
/**
|
|
156
270
|
* Returns timeout time (in UNIX milliseconds) when the LN invoice will expire
|
|
157
271
|
*/
|
|
158
272
|
getTimeoutTime() {
|
|
273
|
+
if (this.pr == null || !this.pr.toLowerCase().startsWith("ln"))
|
|
274
|
+
return 0;
|
|
159
275
|
const decoded = (0, bolt11_1.decode)(this.pr);
|
|
160
276
|
if (decoded.timeExpireDate == null)
|
|
161
277
|
throw new Error("Swap invoice doesn't contain expiry date field!");
|
|
162
278
|
return (decoded.timeExpireDate * 1000);
|
|
163
279
|
}
|
|
164
280
|
/**
|
|
165
|
-
*
|
|
281
|
+
* @inheritDoc
|
|
166
282
|
*/
|
|
167
|
-
getHtlcTimeoutTime() {
|
|
168
|
-
if (this.data == null)
|
|
169
|
-
return -1;
|
|
170
|
-
return Number(this.wrapper.getHtlcTimeout(this.data)) * 1000;
|
|
171
|
-
}
|
|
172
283
|
isFinished() {
|
|
173
|
-
return this.
|
|
284
|
+
return this._state === FromBTCLNSwapState.CLAIM_CLAIMED || this._state === FromBTCLNSwapState.QUOTE_EXPIRED || this._state === FromBTCLNSwapState.FAILED;
|
|
174
285
|
}
|
|
286
|
+
/**
|
|
287
|
+
* @inheritDoc
|
|
288
|
+
*/
|
|
175
289
|
isClaimable() {
|
|
176
|
-
return this.
|
|
290
|
+
return this._state === FromBTCLNSwapState.CLAIM_COMMITED;
|
|
177
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* @inheritDoc
|
|
294
|
+
*/
|
|
178
295
|
isSuccessful() {
|
|
179
|
-
return this.
|
|
296
|
+
return this._state === FromBTCLNSwapState.CLAIM_CLAIMED;
|
|
180
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* @inheritDoc
|
|
300
|
+
*/
|
|
181
301
|
isFailed() {
|
|
182
|
-
return this.
|
|
302
|
+
return this._state === FromBTCLNSwapState.FAILED || this._state === FromBTCLNSwapState.EXPIRED;
|
|
183
303
|
}
|
|
304
|
+
/**
|
|
305
|
+
* @inheritDoc
|
|
306
|
+
*/
|
|
184
307
|
isQuoteExpired() {
|
|
185
|
-
return this.
|
|
308
|
+
return this._state === FromBTCLNSwapState.QUOTE_EXPIRED;
|
|
186
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* @inheritDoc
|
|
312
|
+
*/
|
|
187
313
|
isQuoteSoftExpired() {
|
|
188
|
-
return this.
|
|
314
|
+
return this._state === FromBTCLNSwapState.QUOTE_EXPIRED || this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED;
|
|
189
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* @inheritDoc
|
|
318
|
+
* @internal
|
|
319
|
+
*/
|
|
190
320
|
_verifyQuoteDefinitelyExpired() {
|
|
191
|
-
if (this.
|
|
321
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData == null)) {
|
|
192
322
|
return Promise.resolve(this.getDefinitiveExpiryTime() < Date.now());
|
|
193
323
|
}
|
|
194
324
|
return super._verifyQuoteDefinitelyExpired();
|
|
195
325
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
326
|
+
/**
|
|
327
|
+
* @inheritDoc
|
|
328
|
+
* @internal
|
|
329
|
+
*/
|
|
330
|
+
_verifyQuoteValid() {
|
|
331
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED ||
|
|
332
|
+
(this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData == null)) {
|
|
199
333
|
return Promise.resolve(this.getTimeoutTime() > Date.now());
|
|
200
334
|
}
|
|
201
|
-
return super.
|
|
335
|
+
return super._verifyQuoteValid();
|
|
202
336
|
}
|
|
203
337
|
//////////////////////////////
|
|
204
338
|
//// Amounts & fees
|
|
339
|
+
/**
|
|
340
|
+
* @inheritDoc
|
|
341
|
+
*/
|
|
205
342
|
getInputToken() {
|
|
206
343
|
return Token_1.BitcoinTokens.BTCLN;
|
|
207
344
|
}
|
|
345
|
+
/**
|
|
346
|
+
* @inheritDoc
|
|
347
|
+
*/
|
|
208
348
|
getInput() {
|
|
349
|
+
if (this.pr == null || !this.pr.toLowerCase().startsWith("ln"))
|
|
350
|
+
return (0, TokenAmount_1.toTokenAmount)(null, this.inputToken, this.wrapper._prices, this.pricingInfo);
|
|
209
351
|
const parsed = (0, bolt11_1.decode)(this.pr);
|
|
210
352
|
if (parsed.millisatoshis == null)
|
|
211
353
|
throw new Error("Swap invoice doesn't contain msat amount field!");
|
|
212
354
|
const amount = (BigInt(parsed.millisatoshis) + 999n) / 1000n;
|
|
213
|
-
return (0, TokenAmount_1.toTokenAmount)(amount, this.inputToken, this.wrapper.
|
|
355
|
+
return (0, TokenAmount_1.toTokenAmount)(amount, this.inputToken, this.wrapper._prices, this.pricingInfo);
|
|
214
356
|
}
|
|
215
|
-
|
|
216
|
-
|
|
357
|
+
/**
|
|
358
|
+
* @inheritDoc
|
|
359
|
+
*/
|
|
360
|
+
getSmartChainNetworkFee() {
|
|
361
|
+
return this.getCommitAndClaimNetworkFee();
|
|
217
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* @inheritDoc
|
|
365
|
+
*/
|
|
218
366
|
async hasEnoughForTxFees() {
|
|
219
367
|
const [balance, feeRate] = await Promise.all([
|
|
220
|
-
this.wrapper.
|
|
221
|
-
this.feeRate != null ? Promise.resolve(this.feeRate) : this.wrapper.
|
|
368
|
+
this.wrapper._contract.getBalance(this._getInitiator(), this.wrapper._chain.getNativeCurrencyAddress(), false),
|
|
369
|
+
this.feeRate != null ? Promise.resolve(this.feeRate) : this.wrapper._contract.getInitFeeRate(this.getSwapData().getOfferer(), this.getSwapData().getClaimer(), this.getSwapData().getToken(), this.getSwapData().getClaimHash())
|
|
222
370
|
]);
|
|
223
|
-
const commitFee = await this.wrapper.
|
|
224
|
-
const claimFee = await this.wrapper.
|
|
371
|
+
const commitFee = await this.wrapper._contract.getCommitFee(this._getInitiator(), this.getSwapData(), feeRate);
|
|
372
|
+
const claimFee = await this.wrapper._contract.getClaimFee(this._getInitiator(), this.getSwapData(), feeRate);
|
|
225
373
|
const totalFee = commitFee + claimFee + this.getSwapData().getTotalDeposit();
|
|
226
374
|
return {
|
|
227
375
|
enoughBalance: balance >= totalFee,
|
|
228
|
-
balance: (0, TokenAmount_1.toTokenAmount)(balance, this.wrapper.
|
|
229
|
-
required: (0, TokenAmount_1.toTokenAmount)(totalFee, this.wrapper.
|
|
376
|
+
balance: (0, TokenAmount_1.toTokenAmount)(balance, this.wrapper._getNativeToken(), this.wrapper._prices, this.pricingInfo),
|
|
377
|
+
required: (0, TokenAmount_1.toTokenAmount)(totalFee, this.wrapper._getNativeToken(), this.wrapper._prices, this.pricingInfo)
|
|
230
378
|
};
|
|
231
379
|
}
|
|
380
|
+
isValidSecretPreimage(secret) {
|
|
381
|
+
const paymentHash = buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.from(secret, "hex")));
|
|
382
|
+
const claimHash = this.wrapper._contract.getHashForHtlc(paymentHash).toString("hex");
|
|
383
|
+
return this.getSwapData().getClaimHash() === claimHash;
|
|
384
|
+
}
|
|
232
385
|
//////////////////////////////
|
|
233
386
|
//// Execution
|
|
234
387
|
/**
|
|
@@ -241,19 +394,23 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
241
394
|
* link, wallet is not required and the LN invoice can be paid externally as well (just pass null or undefined here)
|
|
242
395
|
* @param callbacks Callbacks to track the progress of the swap
|
|
243
396
|
* @param options Optional options for the swap like feeRate, AbortSignal, and timeouts/intervals
|
|
397
|
+
* @param secret A swap secret to use for the claim transaction, generally only needed if the swap
|
|
398
|
+
* was recovered from on-chain data, or the pre-image was generated outside the SDK
|
|
244
399
|
*/
|
|
245
|
-
async execute(dstSigner, walletOrLnurlWithdraw, callbacks, options) {
|
|
246
|
-
if (this.
|
|
400
|
+
async execute(dstSigner, walletOrLnurlWithdraw, callbacks, options, secret) {
|
|
401
|
+
if (this._state === FromBTCLNSwapState.FAILED)
|
|
247
402
|
throw new Error("Swap failed!");
|
|
248
|
-
if (this.
|
|
403
|
+
if (this._state === FromBTCLNSwapState.EXPIRED)
|
|
249
404
|
throw new Error("Swap HTLC expired!");
|
|
250
|
-
if (this.
|
|
405
|
+
if (this._state === FromBTCLNSwapState.QUOTE_EXPIRED || this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED)
|
|
251
406
|
throw new Error("Swap quote expired!");
|
|
252
|
-
if (this.
|
|
407
|
+
if (this._state === FromBTCLNSwapState.CLAIM_CLAIMED)
|
|
253
408
|
throw new Error("Swap already settled!");
|
|
254
409
|
let abortSignal = options?.abortSignal;
|
|
255
|
-
if (this.
|
|
410
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED) {
|
|
256
411
|
if (walletOrLnurlWithdraw != null && this.lnurl == null) {
|
|
412
|
+
if (this.pr == null || !this.pr.toLowerCase().startsWith("ln"))
|
|
413
|
+
throw new Error("Input lightning network invoice not available, the swap was probably recovered!");
|
|
257
414
|
if (typeof (walletOrLnurlWithdraw) === "string" || (0, LNURLWithdraw_1.isLNURLWithdraw)(walletOrLnurlWithdraw)) {
|
|
258
415
|
await this.settleWithLNURLWithdraw(walletOrLnurlWithdraw);
|
|
259
416
|
}
|
|
@@ -270,30 +427,40 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
270
427
|
if (!paymentSuccess)
|
|
271
428
|
throw new Error("Failed to receive lightning network payment");
|
|
272
429
|
}
|
|
273
|
-
if (this.
|
|
430
|
+
if (this._state === FromBTCLNSwapState.PR_PAID || this._state === FromBTCLNSwapState.CLAIM_COMMITED) {
|
|
274
431
|
if (this.canCommitAndClaimInOneShot()) {
|
|
275
|
-
await this.commitAndClaim(dstSigner, options?.abortSignal, undefined, callbacks?.onDestinationCommitSent, callbacks?.onDestinationClaimSent);
|
|
432
|
+
await this.commitAndClaim(dstSigner, options?.abortSignal, undefined, callbacks?.onDestinationCommitSent, callbacks?.onDestinationClaimSent, secret);
|
|
276
433
|
}
|
|
277
434
|
else {
|
|
278
|
-
if (this.
|
|
435
|
+
if (this._state === FromBTCLNSwapState.PR_PAID) {
|
|
279
436
|
await this.commit(dstSigner, options?.abortSignal, undefined, callbacks?.onDestinationCommitSent);
|
|
280
437
|
if (options?.delayBetweenCommitAndClaimSeconds != null)
|
|
281
438
|
await (0, TimeoutUtils_1.timeoutPromise)(options.delayBetweenCommitAndClaimSeconds * 1000, options?.abortSignal);
|
|
282
439
|
}
|
|
283
|
-
if (this.
|
|
284
|
-
await this.claim(dstSigner, options?.abortSignal, callbacks?.onDestinationClaimSent);
|
|
440
|
+
if (this._state === FromBTCLNSwapState.CLAIM_COMMITED) {
|
|
441
|
+
await this.claim(dstSigner, options?.abortSignal, callbacks?.onDestinationClaimSent, secret);
|
|
285
442
|
}
|
|
286
443
|
}
|
|
287
444
|
}
|
|
288
445
|
// @ts-ignore
|
|
289
|
-
if (this.
|
|
446
|
+
if (this._state === FromBTCLNSwapState.CLAIM_CLAIMED) {
|
|
290
447
|
if (callbacks?.onSwapSettled != null)
|
|
291
448
|
callbacks.onSwapSettled(this.getOutputTxId());
|
|
292
449
|
}
|
|
293
450
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
451
|
+
/**
|
|
452
|
+
* @inheritDoc
|
|
453
|
+
*
|
|
454
|
+
* @param options
|
|
455
|
+
* @param options.skipChecks Skip checks like making sure init signature is still valid and swap
|
|
456
|
+
* wasn't commited yet (this is handled on swap creation, if you commit right after quoting, you
|
|
457
|
+
* can use `skipChecks=true`)
|
|
458
|
+
* @param secret A swap secret to use for the claim transaction, generally only needed if the swap
|
|
459
|
+
* was recovered from on-chain data, or the pre-image was generated outside the SDK
|
|
460
|
+
*/
|
|
461
|
+
async txsExecute(options, secret) {
|
|
462
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED) {
|
|
463
|
+
if (!await this._verifyQuoteValid())
|
|
297
464
|
throw new Error("Quote already expired or close to expiry!");
|
|
298
465
|
return [
|
|
299
466
|
{
|
|
@@ -302,18 +469,19 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
302
469
|
chain: "LIGHTNING",
|
|
303
470
|
txs: [
|
|
304
471
|
{
|
|
305
|
-
|
|
472
|
+
type: "BOLT11_PAYMENT_REQUEST",
|
|
473
|
+
address: this.getAddress(),
|
|
306
474
|
hyperlink: this.getHyperlink()
|
|
307
475
|
}
|
|
308
476
|
]
|
|
309
477
|
}
|
|
310
478
|
];
|
|
311
479
|
}
|
|
312
|
-
if (this.
|
|
313
|
-
if (!await this.
|
|
480
|
+
if (this._state === FromBTCLNSwapState.PR_PAID) {
|
|
481
|
+
if (!await this._verifyQuoteValid())
|
|
314
482
|
throw new Error("Quote already expired or close to expiry!");
|
|
315
483
|
const txsCommit = await this.txsCommit(options?.skipChecks);
|
|
316
|
-
const txsClaim = await this._txsClaim(undefined);
|
|
484
|
+
const txsClaim = await this._txsClaim(undefined, secret);
|
|
317
485
|
return [
|
|
318
486
|
{
|
|
319
487
|
name: "Commit",
|
|
@@ -329,8 +497,8 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
329
497
|
},
|
|
330
498
|
];
|
|
331
499
|
}
|
|
332
|
-
if (this.
|
|
333
|
-
const txsClaim = await this.txsClaim();
|
|
500
|
+
if (this._state === FromBTCLNSwapState.CLAIM_COMMITED) {
|
|
501
|
+
const txsClaim = await this.txsClaim(undefined, secret);
|
|
334
502
|
return [
|
|
335
503
|
{
|
|
336
504
|
name: "Claim",
|
|
@@ -348,26 +516,32 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
348
516
|
* Checks whether the LP received the LN payment and we can continue by committing & claiming the HTLC on-chain
|
|
349
517
|
*
|
|
350
518
|
* @param save If the new swap state should be saved
|
|
519
|
+
*
|
|
520
|
+
* @internal
|
|
351
521
|
*/
|
|
352
522
|
async _checkIntermediaryPaymentReceived(save = true) {
|
|
353
|
-
if (this.
|
|
354
|
-
this.
|
|
355
|
-
this.
|
|
356
|
-
this.
|
|
523
|
+
if (this._state === FromBTCLNSwapState.PR_PAID ||
|
|
524
|
+
this._state === FromBTCLNSwapState.CLAIM_COMMITED ||
|
|
525
|
+
this._state === FromBTCLNSwapState.CLAIM_CLAIMED ||
|
|
526
|
+
this._state === FromBTCLNSwapState.FAILED ||
|
|
527
|
+
this._state === FromBTCLNSwapState.EXPIRED)
|
|
357
528
|
return true;
|
|
358
|
-
if (this.
|
|
529
|
+
if (this._state === FromBTCLNSwapState.QUOTE_EXPIRED || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData != null))
|
|
359
530
|
return false;
|
|
360
531
|
if (this.url == null)
|
|
361
532
|
return false;
|
|
362
|
-
const
|
|
533
|
+
const paymentHash = this.getPaymentHash();
|
|
534
|
+
if (paymentHash == null)
|
|
535
|
+
throw new Error("Failed to check LP payment received, payment hash not known (probably recovered swap?)");
|
|
536
|
+
const resp = await IntermediaryAPI_1.IntermediaryAPI.getPaymentAuthorization(this.url, paymentHash.toString("hex"));
|
|
363
537
|
switch (resp.code) {
|
|
364
538
|
case IntermediaryAPI_1.PaymentAuthorizationResponseCodes.AUTH_DATA:
|
|
365
|
-
const data = new this.wrapper.
|
|
539
|
+
const data = new this.wrapper._swapDataDeserializer(resp.data.data);
|
|
366
540
|
try {
|
|
367
541
|
await this.checkIntermediaryReturnedAuthData(this._getInitiator(), data, resp.data);
|
|
368
|
-
this.expiry = await this.wrapper.
|
|
369
|
-
this.
|
|
370
|
-
this.
|
|
542
|
+
this.expiry = await this.wrapper._contract.getInitAuthorizationExpiry(data, resp.data);
|
|
543
|
+
this._state = FromBTCLNSwapState.PR_PAID;
|
|
544
|
+
this._data = data;
|
|
371
545
|
this.signatureData = {
|
|
372
546
|
prefix: resp.data.prefix,
|
|
373
547
|
timeout: resp.data.timeout,
|
|
@@ -381,7 +555,7 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
381
555
|
catch (e) { }
|
|
382
556
|
return null;
|
|
383
557
|
case IntermediaryAPI_1.PaymentAuthorizationResponseCodes.EXPIRED:
|
|
384
|
-
this.
|
|
558
|
+
this._state = FromBTCLNSwapState.QUOTE_EXPIRED;
|
|
385
559
|
this.initiated = true;
|
|
386
560
|
if (save)
|
|
387
561
|
await this._saveAndEmit();
|
|
@@ -396,10 +570,12 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
396
570
|
* @param signer Smart chain signer's address initiating the swap
|
|
397
571
|
* @param data Parsed swap data as returned by the intermediary
|
|
398
572
|
* @param signature Signature data as returned by the intermediary
|
|
399
|
-
*
|
|
573
|
+
*
|
|
400
574
|
* @throws {IntermediaryError} If the returned are not valid
|
|
401
575
|
* @throws {SignatureVerificationError} If the returned signature is not valid
|
|
402
576
|
* @throws {Error} If the swap is already committed on-chain
|
|
577
|
+
*
|
|
578
|
+
* @internal
|
|
403
579
|
*/
|
|
404
580
|
async checkIntermediaryReturnedAuthData(signer, data, signature) {
|
|
405
581
|
data.setClaimer(signer);
|
|
@@ -424,8 +600,8 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
424
600
|
if (data.hasSuccessAction())
|
|
425
601
|
throw new IntermediaryError_1.IntermediaryError("Invalid has success action");
|
|
426
602
|
await Promise.all([
|
|
427
|
-
this.wrapper.
|
|
428
|
-
this.wrapper.
|
|
603
|
+
this.wrapper._contract.isValidInitAuthorization(this._getInitiator(), data, signature, this.feeRate),
|
|
604
|
+
this.wrapper._contract.getCommitStatus(data.getClaimer(), data)
|
|
429
605
|
.then(status => {
|
|
430
606
|
if (status?.type !== base_1.SwapCommitStateType.NOT_COMMITED)
|
|
431
607
|
throw new Error("Swap already committed on-chain!");
|
|
@@ -433,16 +609,23 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
433
609
|
]);
|
|
434
610
|
}
|
|
435
611
|
/**
|
|
436
|
-
* Waits till
|
|
612
|
+
* Waits till a lightning network payment is received by the intermediary and client
|
|
613
|
+
* can continue by initiating (committing) & settling (claiming) the HTLC by calling
|
|
614
|
+
* either the {@link commitAndClaim} function (if the underlying chain allows commit
|
|
615
|
+
* and claim in a single transaction - check with {@link canCommitAndClaimInOneShot}).
|
|
616
|
+
* Or call {@link commit} and then {@link claim} separately.
|
|
617
|
+
*
|
|
618
|
+
* If this swap is using an LNURL-withdraw link as input, it automatically posts the
|
|
619
|
+
* generated invoice to the LNURL service to pay it.
|
|
437
620
|
*
|
|
438
621
|
* @param onPaymentReceived Callback as for when the LP reports having received the ln payment
|
|
439
622
|
* @param abortSignal Abort signal to stop waiting for payment
|
|
440
|
-
* @param checkIntervalSeconds How often to poll the intermediary for answer
|
|
623
|
+
* @param checkIntervalSeconds How often to poll the intermediary for answer (default 5 seconds)
|
|
441
624
|
*/
|
|
442
625
|
async waitForPayment(onPaymentReceived, checkIntervalSeconds, abortSignal) {
|
|
443
626
|
checkIntervalSeconds ??= 5;
|
|
444
|
-
if (this.
|
|
445
|
-
(this.
|
|
627
|
+
if (this._state !== FromBTCLNSwapState.PR_CREATED &&
|
|
628
|
+
(this._state !== FromBTCLNSwapState.QUOTE_SOFT_EXPIRED || this.signatureData != null))
|
|
446
629
|
throw new Error("Must be in PR_CREATED state!");
|
|
447
630
|
if (this.url == null)
|
|
448
631
|
throw new Error("LP URL not known, cannot await the payment!");
|
|
@@ -451,6 +634,8 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
451
634
|
abortSignal.addEventListener("abort", () => abortController.abort(abortSignal.reason));
|
|
452
635
|
let save = false;
|
|
453
636
|
if (this.lnurl != null && this.lnurlK1 != null && this.lnurlCallback != null && !this.prPosted) {
|
|
637
|
+
if (this.pr == null || !this.pr.toLowerCase().startsWith("ln"))
|
|
638
|
+
throw new Error("Input lightning network invoice not available, the swap was probably recovered!");
|
|
454
639
|
LNURL_1.LNURL.postInvoiceToLNURLWithdraw({ k1: this.lnurlK1, callback: this.lnurlCallback }, this.pr).catch(e => {
|
|
455
640
|
this.lnurlFailSignal.abort(e);
|
|
456
641
|
});
|
|
@@ -466,9 +651,12 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
466
651
|
let lnurlFailListener = () => abortController.abort(this.lnurlFailSignal.signal.reason);
|
|
467
652
|
this.lnurlFailSignal.signal.addEventListener("abort", lnurlFailListener);
|
|
468
653
|
this.lnurlFailSignal.signal.throwIfAborted();
|
|
654
|
+
const paymentHash = this.getPaymentHash();
|
|
655
|
+
if (paymentHash == null)
|
|
656
|
+
throw new Error("Swap payment hash not available, the swap was probably recovered!");
|
|
469
657
|
let resp = { code: IntermediaryAPI_1.PaymentAuthorizationResponseCodes.PENDING, msg: "" };
|
|
470
658
|
while (!abortController.signal.aborted && resp.code === IntermediaryAPI_1.PaymentAuthorizationResponseCodes.PENDING) {
|
|
471
|
-
resp = await IntermediaryAPI_1.IntermediaryAPI.getPaymentAuthorization(this.url,
|
|
659
|
+
resp = await IntermediaryAPI_1.IntermediaryAPI.getPaymentAuthorization(this.url, paymentHash.toString("hex"));
|
|
472
660
|
if (resp.code === IntermediaryAPI_1.PaymentAuthorizationResponseCodes.PENDING)
|
|
473
661
|
await (0, TimeoutUtils_1.timeoutPromise)(checkIntervalSeconds * 1000, abortController.signal);
|
|
474
662
|
}
|
|
@@ -476,13 +664,13 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
476
664
|
abortController.signal.throwIfAborted();
|
|
477
665
|
if (resp.code === IntermediaryAPI_1.PaymentAuthorizationResponseCodes.AUTH_DATA) {
|
|
478
666
|
const sigData = resp.data;
|
|
479
|
-
const swapData = new this.wrapper.
|
|
667
|
+
const swapData = new this.wrapper._swapDataDeserializer(resp.data.data);
|
|
480
668
|
await this.checkIntermediaryReturnedAuthData(this._getInitiator(), swapData, sigData);
|
|
481
|
-
this.expiry = await this.wrapper.
|
|
669
|
+
this.expiry = await this.wrapper._contract.getInitAuthorizationExpiry(swapData, sigData);
|
|
482
670
|
if (onPaymentReceived != null)
|
|
483
671
|
onPaymentReceived(this.getInputTxId());
|
|
484
|
-
if (this.
|
|
485
|
-
this.
|
|
672
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED || this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED) {
|
|
673
|
+
this._data = swapData;
|
|
486
674
|
this.signatureData = {
|
|
487
675
|
prefix: sigData.prefix,
|
|
488
676
|
timeout: sigData.timeout,
|
|
@@ -492,7 +680,7 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
492
680
|
}
|
|
493
681
|
return true;
|
|
494
682
|
}
|
|
495
|
-
if (this.
|
|
683
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED || this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED) {
|
|
496
684
|
if (resp.code === IntermediaryAPI_1.PaymentAuthorizationResponseCodes.EXPIRED) {
|
|
497
685
|
await this._saveAndEmit(FromBTCLNSwapState.QUOTE_EXPIRED);
|
|
498
686
|
}
|
|
@@ -503,36 +691,34 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
503
691
|
//////////////////////////////
|
|
504
692
|
//// Commit
|
|
505
693
|
/**
|
|
506
|
-
*
|
|
694
|
+
* @inheritDoc
|
|
507
695
|
*
|
|
508
|
-
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
509
|
-
* @param abortSignal Abort signal to stop waiting for the transaction confirmation and abort
|
|
510
|
-
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
511
|
-
* (this is handled when swap is created (quoted), if you commit right after quoting, you can use skipChecks=true)
|
|
512
|
-
* @param onBeforeTxSent
|
|
513
696
|
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
514
697
|
*/
|
|
515
698
|
async commit(_signer, abortSignal, skipChecks, onBeforeTxSent) {
|
|
516
|
-
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.
|
|
699
|
+
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
517
700
|
this.checkSigner(signer);
|
|
518
701
|
let txCount = 0;
|
|
519
702
|
const txs = await this.txsCommit(skipChecks);
|
|
520
|
-
const result = await this.wrapper.
|
|
703
|
+
const result = await this.wrapper._chain.sendAndConfirm(signer, txs, true, abortSignal, undefined, (txId) => {
|
|
521
704
|
txCount++;
|
|
522
705
|
if (onBeforeTxSent != null && txCount === txs.length)
|
|
523
706
|
onBeforeTxSent(txId);
|
|
524
707
|
return Promise.resolve();
|
|
525
708
|
});
|
|
526
|
-
this.
|
|
527
|
-
if (this.
|
|
709
|
+
this._commitTxId = result[result.length - 1];
|
|
710
|
+
if (this._state === FromBTCLNSwapState.PR_PAID || this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED) {
|
|
528
711
|
await this._saveAndEmit(FromBTCLNSwapState.CLAIM_COMMITED);
|
|
529
712
|
}
|
|
530
|
-
return this.
|
|
713
|
+
return this._commitTxId;
|
|
531
714
|
}
|
|
715
|
+
/**
|
|
716
|
+
* @inheritDoc
|
|
717
|
+
*/
|
|
532
718
|
async waitTillCommited(abortSignal) {
|
|
533
|
-
if (this.
|
|
719
|
+
if (this._state === FromBTCLNSwapState.CLAIM_COMMITED || this._state === FromBTCLNSwapState.CLAIM_CLAIMED)
|
|
534
720
|
return Promise.resolve();
|
|
535
|
-
if (this.
|
|
721
|
+
if (this._state !== FromBTCLNSwapState.PR_PAID && (this._state !== FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData != null))
|
|
536
722
|
throw new Error("Invalid state");
|
|
537
723
|
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
538
724
|
const result = await Promise.race([
|
|
@@ -546,14 +732,14 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
546
732
|
this.logger.debug("waitTillCommited(): Resolved from watchdog - commited");
|
|
547
733
|
if (result === false) {
|
|
548
734
|
this.logger.debug("waitTillCommited(): Resolved from watchdog - signature expired");
|
|
549
|
-
if (this.
|
|
550
|
-
this.
|
|
735
|
+
if (this._state === FromBTCLNSwapState.PR_PAID ||
|
|
736
|
+
this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED) {
|
|
551
737
|
await this._saveAndEmit(FromBTCLNSwapState.QUOTE_EXPIRED);
|
|
552
738
|
}
|
|
553
739
|
return;
|
|
554
740
|
}
|
|
555
|
-
if (this.
|
|
556
|
-
this.
|
|
741
|
+
if (this._state === FromBTCLNSwapState.PR_PAID ||
|
|
742
|
+
this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED) {
|
|
557
743
|
await this._saveAndEmit(FromBTCLNSwapState.CLAIM_COMMITED);
|
|
558
744
|
}
|
|
559
745
|
}
|
|
@@ -563,62 +749,72 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
563
749
|
* Unsafe txs claim getter without state checking!
|
|
564
750
|
*
|
|
565
751
|
* @param _signer
|
|
566
|
-
* @
|
|
752
|
+
* @param secret A swap secret to use for the claim transaction, generally only needed if the swap
|
|
753
|
+
* was recovered from on-chain data, or the pre-image was generated outside the SDK
|
|
754
|
+
*
|
|
755
|
+
* @internal
|
|
567
756
|
*/
|
|
568
|
-
async _txsClaim(_signer) {
|
|
569
|
-
if (this.
|
|
757
|
+
async _txsClaim(_signer, secret) {
|
|
758
|
+
if (this._data == null)
|
|
570
759
|
throw new Error("Unknown data, wrong state?");
|
|
571
|
-
|
|
760
|
+
const useSecret = secret ?? this.secret;
|
|
761
|
+
if (useSecret == null)
|
|
762
|
+
throw new Error("Swap secret pre-image not known and not provided, please provide the swap secret pre-image as an argument");
|
|
763
|
+
if (!this.isValidSecretPreimage(useSecret))
|
|
764
|
+
throw new Error("Invalid swap secret pre-image provided!");
|
|
765
|
+
return this.wrapper._contract.txsClaimWithSecret(_signer == null ?
|
|
572
766
|
this._getInitiator() :
|
|
573
|
-
((0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.
|
|
767
|
+
((0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer)), this._data, useSecret, true, true);
|
|
574
768
|
}
|
|
575
769
|
/**
|
|
576
|
-
*
|
|
577
|
-
* (hash preimage)
|
|
770
|
+
* @inheritDoc
|
|
578
771
|
*
|
|
579
772
|
* @param _signer Optional signer address to use for claiming the swap, can also be different from the initializer
|
|
580
|
-
* @
|
|
773
|
+
* @param secret A swap secret to use for the claim transaction, generally only needed if the swap
|
|
774
|
+
* was recovered from on-chain data, or the pre-image was generated outside the SDK
|
|
775
|
+
*
|
|
776
|
+
* @throws {Error} If in invalid state (must be {@link FromBTCLNSwapState.CLAIM_COMMITED})
|
|
581
777
|
*/
|
|
582
|
-
async txsClaim(_signer) {
|
|
583
|
-
if (this.
|
|
778
|
+
async txsClaim(_signer, secret) {
|
|
779
|
+
if (this._state !== FromBTCLNSwapState.CLAIM_COMMITED)
|
|
584
780
|
throw new Error("Must be in CLAIM_COMMITED state!");
|
|
585
|
-
return this._txsClaim(_signer);
|
|
781
|
+
return this._txsClaim(_signer, secret);
|
|
586
782
|
}
|
|
587
783
|
/**
|
|
588
|
-
*
|
|
784
|
+
* @inheritDoc
|
|
589
785
|
*
|
|
590
|
-
* @param _signer
|
|
591
|
-
* @param abortSignal
|
|
786
|
+
* @param _signer
|
|
787
|
+
* @param abortSignal
|
|
592
788
|
* @param onBeforeTxSent
|
|
789
|
+
* @param secret A swap secret to use for the claim transaction, generally only needed if the swap
|
|
790
|
+
* was recovered from on-chain data, or the pre-image was generated outside the SDK
|
|
593
791
|
*/
|
|
594
|
-
async claim(_signer, abortSignal, onBeforeTxSent) {
|
|
595
|
-
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.
|
|
792
|
+
async claim(_signer, abortSignal, onBeforeTxSent, secret) {
|
|
793
|
+
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
596
794
|
let txCount = 0;
|
|
597
|
-
const result = await this.wrapper.
|
|
795
|
+
const result = await this.wrapper._chain.sendAndConfirm(signer, await this.txsClaim(_signer, secret), true, abortSignal, undefined, (txId) => {
|
|
598
796
|
txCount++;
|
|
599
797
|
if (onBeforeTxSent != null && txCount === 1)
|
|
600
798
|
onBeforeTxSent(txId);
|
|
601
799
|
return Promise.resolve();
|
|
602
800
|
});
|
|
603
|
-
this.
|
|
604
|
-
if (this.
|
|
801
|
+
this._claimTxId = result[0];
|
|
802
|
+
if (this._state === FromBTCLNSwapState.CLAIM_COMMITED || this._state === FromBTCLNSwapState.EXPIRED || this._state === FromBTCLNSwapState.FAILED) {
|
|
605
803
|
await this._saveAndEmit(FromBTCLNSwapState.CLAIM_CLAIMED);
|
|
606
804
|
}
|
|
607
805
|
return result[0];
|
|
608
806
|
}
|
|
609
807
|
/**
|
|
610
|
-
*
|
|
808
|
+
* @inheritDoc
|
|
611
809
|
*
|
|
612
|
-
* @
|
|
613
|
-
* @param abortSignal AbortSignal
|
|
614
|
-
* @throws {Error} If swap is in invalid state (must be BTC_TX_CONFIRMED)
|
|
810
|
+
* @throws {Error} If swap is in invalid state (must be {@link FromBTCLNSwapState.CLAIM_COMMITED})
|
|
615
811
|
* @throws {Error} If the LP refunded sooner than we were able to claim
|
|
616
812
|
* @returns {boolean} whether the swap was claimed in time or not
|
|
617
813
|
*/
|
|
618
814
|
async waitTillClaimed(maxWaitTimeSeconds, abortSignal) {
|
|
619
|
-
if (this.
|
|
815
|
+
if (this._state === FromBTCLNSwapState.CLAIM_CLAIMED)
|
|
620
816
|
return Promise.resolve(true);
|
|
621
|
-
if (this.
|
|
817
|
+
if (this._state !== FromBTCLNSwapState.CLAIM_COMMITED)
|
|
622
818
|
throw new Error("Invalid state (not CLAIM_COMMITED)");
|
|
623
819
|
const abortController = new AbortController();
|
|
624
820
|
if (abortSignal != null)
|
|
@@ -656,16 +852,16 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
656
852
|
}
|
|
657
853
|
this.logger.debug("waitTillClaimed(): Resolved from watchdog");
|
|
658
854
|
if (res?.type === base_1.SwapCommitStateType.PAID) {
|
|
659
|
-
if (this.
|
|
660
|
-
this.
|
|
855
|
+
if (this._state !== FromBTCLNSwapState.CLAIM_CLAIMED) {
|
|
856
|
+
this._claimTxId = await res.getClaimTxId();
|
|
661
857
|
await this._saveAndEmit(FromBTCLNSwapState.CLAIM_CLAIMED);
|
|
662
858
|
}
|
|
663
859
|
}
|
|
664
860
|
if (res?.type === base_1.SwapCommitStateType.NOT_COMMITED || res?.type === base_1.SwapCommitStateType.EXPIRED) {
|
|
665
|
-
if (this.
|
|
666
|
-
this.
|
|
861
|
+
if (this._state !== FromBTCLNSwapState.CLAIM_CLAIMED &&
|
|
862
|
+
this._state !== FromBTCLNSwapState.FAILED) {
|
|
667
863
|
if (res.getRefundTxId != null)
|
|
668
|
-
this.
|
|
864
|
+
this._refundTxId = await res.getRefundTxId();
|
|
669
865
|
await this._saveAndEmit(FromBTCLNSwapState.FAILED);
|
|
670
866
|
}
|
|
671
867
|
throw new Error("Swap expired while waiting for claim!");
|
|
@@ -675,10 +871,11 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
675
871
|
//////////////////////////////
|
|
676
872
|
//// Commit & claim
|
|
677
873
|
/**
|
|
678
|
-
* Estimated transaction fee for commit & claim
|
|
874
|
+
* Estimated transaction fee for commit & claim transactions combined, required
|
|
875
|
+
* to settle the swap on the smart chain destination side.
|
|
679
876
|
*/
|
|
680
|
-
async
|
|
681
|
-
const swapContract = this.wrapper.
|
|
877
|
+
async getCommitAndClaimNetworkFee() {
|
|
878
|
+
const swapContract = this.wrapper._contract;
|
|
682
879
|
const feeRate = this.feeRate ?? await swapContract.getInitFeeRate(this.getSwapData().getOfferer(), this.getSwapData().getClaimer(), this.getSwapData().getToken(), this.getSwapData().getClaimHash());
|
|
683
880
|
const commitFee = await (swapContract.getRawCommitFee != null ?
|
|
684
881
|
swapContract.getRawCommitFee(this._getInitiator(), this.getSwapData(), feeRate) :
|
|
@@ -686,57 +883,75 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
686
883
|
const claimFee = await (swapContract.getRawClaimFee != null ?
|
|
687
884
|
swapContract.getRawClaimFee(this._getInitiator(), this.getSwapData(), feeRate) :
|
|
688
885
|
swapContract.getClaimFee(this._getInitiator(), this.getSwapData(), feeRate));
|
|
689
|
-
return commitFee + claimFee;
|
|
886
|
+
return (0, TokenAmount_1.toTokenAmount)(commitFee + claimFee, this.wrapper._getNativeToken(), this.wrapper._prices);
|
|
690
887
|
}
|
|
888
|
+
/**
|
|
889
|
+
* Returns whether the underlying chain supports calling commit and claim in a single call,
|
|
890
|
+
* such that you can use the {@link commitAndClaim} function. If not you have to manually
|
|
891
|
+
* call {@link commit} first and then {@link claim}.
|
|
892
|
+
*/
|
|
691
893
|
canCommitAndClaimInOneShot() {
|
|
692
|
-
return this.wrapper.
|
|
894
|
+
return this.wrapper._contract.initAndClaimWithSecret != null;
|
|
693
895
|
}
|
|
694
896
|
/**
|
|
695
897
|
* Returns transactions for both commit & claim operation together, such that they can be signed all at once by
|
|
696
|
-
* the wallet.
|
|
898
|
+
* the wallet. **WARNING**: transactions must be sent sequentially, such that the claim (2nd) transaction is only
|
|
697
899
|
* sent after the commit (1st) transaction confirms. Failure to do so can reveal the HTLC pre-image too soon,
|
|
698
|
-
* opening a possibility for the LP to steal funds
|
|
900
|
+
* opening a possibility for the LP to steal funds!
|
|
699
901
|
*
|
|
700
902
|
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
701
903
|
* (this is handled when swap is created (quoted), if you commit right after quoting, you can use skipChecks=true)
|
|
904
|
+
* @param secret A swap secret to use for the claim transaction, generally only needed if the swap
|
|
905
|
+
* was recovered from on-chain data, or the pre-image was generated outside the SDK
|
|
702
906
|
*
|
|
703
907
|
* @throws {Error} If in invalid state (must be PR_PAID or CLAIM_COMMITED)
|
|
704
908
|
*/
|
|
705
|
-
async txsCommitAndClaim(skipChecks) {
|
|
706
|
-
if (this.
|
|
707
|
-
return await this.txsClaim();
|
|
708
|
-
if (this.
|
|
709
|
-
(this.
|
|
909
|
+
async txsCommitAndClaim(skipChecks, secret) {
|
|
910
|
+
if (this._state === FromBTCLNSwapState.CLAIM_COMMITED)
|
|
911
|
+
return await this.txsClaim(undefined, secret);
|
|
912
|
+
if (this._state !== FromBTCLNSwapState.PR_PAID &&
|
|
913
|
+
(this._state !== FromBTCLNSwapState.QUOTE_SOFT_EXPIRED || this.signatureData == null))
|
|
710
914
|
throw new Error("Must be in PR_PAID state!");
|
|
711
|
-
if (this.
|
|
915
|
+
if (this._data == null)
|
|
712
916
|
throw new Error("Unknown data, wrong state?");
|
|
917
|
+
const useSecret = secret ?? this.secret;
|
|
918
|
+
if (useSecret == null)
|
|
919
|
+
throw new Error("Swap secret pre-image not known and not provided, please provide the swap secret pre-image as second argument");
|
|
920
|
+
if (!this.isValidSecretPreimage(useSecret))
|
|
921
|
+
throw new Error("Invalid swap secret pre-image provided!");
|
|
713
922
|
const initTxs = await this.txsCommit(skipChecks);
|
|
714
|
-
const claimTxs = await this.wrapper.
|
|
923
|
+
const claimTxs = await this.wrapper._contract.txsClaimWithSecret(this._getInitiator(), this._data, useSecret, true, true, undefined, true);
|
|
715
924
|
return initTxs.concat(claimTxs);
|
|
716
925
|
}
|
|
717
926
|
/**
|
|
718
|
-
* Commits and claims the swap, in a way that the transactions can be signed together by the
|
|
719
|
-
* then sent sequentially
|
|
927
|
+
* Commits and claims the swap, in a way that the transactions can be signed together by the provided signer and
|
|
928
|
+
* then automatically sent sequentially by the SDK. To check if the underlying chain supports this flow check
|
|
929
|
+
* the {@link canCommitAndClaimInOneShot} function.
|
|
720
930
|
*
|
|
721
931
|
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
722
932
|
* @param abortSignal Abort signal to stop waiting for the transaction confirmation and abort
|
|
723
933
|
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
724
934
|
* (this is handled when swap is created (quoted), if you commit right after quoting, you can use skipChecks=true)
|
|
725
|
-
* @param onBeforeCommitTxSent
|
|
726
|
-
*
|
|
935
|
+
* @param onBeforeCommitTxSent Optional callback called before the initialization (commit) transaction is
|
|
936
|
+
* broadcasted
|
|
937
|
+
* @param onBeforeClaimTxSent Optional callback called before the settlement (claim) transaction is
|
|
938
|
+
* broadcasted
|
|
939
|
+
* @param secret A swap secret to use for the claim transaction, generally only needed if the swap
|
|
940
|
+
* was recovered from on-chain data, or the pre-image was generated outside the SDK
|
|
941
|
+
*
|
|
727
942
|
* @throws {Error} If in invalid state (must be PR_PAID or CLAIM_COMMITED)
|
|
728
943
|
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
729
944
|
*/
|
|
730
|
-
async commitAndClaim(_signer, abortSignal, skipChecks, onBeforeCommitTxSent, onBeforeClaimTxSent) {
|
|
731
|
-
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.
|
|
945
|
+
async commitAndClaim(_signer, abortSignal, skipChecks, onBeforeCommitTxSent, onBeforeClaimTxSent, secret) {
|
|
946
|
+
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
732
947
|
if (!this.canCommitAndClaimInOneShot())
|
|
733
948
|
throw new Error("Cannot commitAndClaim in single action, please run commit and claim separately!");
|
|
734
949
|
this.checkSigner(signer);
|
|
735
|
-
if (this.
|
|
736
|
-
return [await this.claim(signer, abortSignal, onBeforeClaimTxSent)];
|
|
950
|
+
if (this._state === FromBTCLNSwapState.CLAIM_COMMITED)
|
|
951
|
+
return [await this.claim(signer, abortSignal, onBeforeClaimTxSent, secret)];
|
|
737
952
|
let txCount = 0;
|
|
738
|
-
const txs = await this.txsCommitAndClaim(skipChecks);
|
|
739
|
-
const result = await this.wrapper.
|
|
953
|
+
const txs = await this.txsCommitAndClaim(skipChecks, secret);
|
|
954
|
+
const result = await this.wrapper._chain.sendAndConfirm(signer, txs, true, abortSignal, undefined, (txId) => {
|
|
740
955
|
txCount++;
|
|
741
956
|
if (onBeforeCommitTxSent != null && txCount === 1)
|
|
742
957
|
onBeforeCommitTxSent(txId);
|
|
@@ -744,9 +959,9 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
744
959
|
onBeforeClaimTxSent(txId);
|
|
745
960
|
return Promise.resolve();
|
|
746
961
|
});
|
|
747
|
-
this.
|
|
748
|
-
this.
|
|
749
|
-
if (this.
|
|
962
|
+
this._commitTxId = result[0] ?? this._commitTxId;
|
|
963
|
+
this._claimTxId = result[result.length - 1] ?? this._claimTxId;
|
|
964
|
+
if (this._state !== FromBTCLNSwapState.CLAIM_CLAIMED) {
|
|
750
965
|
await this._saveAndEmit(FromBTCLNSwapState.CLAIM_CLAIMED);
|
|
751
966
|
}
|
|
752
967
|
return result;
|
|
@@ -754,21 +969,29 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
754
969
|
//////////////////////////////
|
|
755
970
|
//// LNURL
|
|
756
971
|
/**
|
|
757
|
-
*
|
|
972
|
+
* Whether this swap uses an LNURL-withdraw link
|
|
758
973
|
*/
|
|
759
974
|
isLNURL() {
|
|
760
975
|
return this.lnurl != null;
|
|
761
976
|
}
|
|
762
977
|
/**
|
|
763
|
-
* Gets the used LNURL or null if this is not an LNURL-withdraw swap
|
|
978
|
+
* Gets the used LNURL or `null` if this is not an LNURL-withdraw swap
|
|
764
979
|
*/
|
|
765
980
|
getLNURL() {
|
|
766
981
|
return this.lnurl ?? null;
|
|
767
982
|
}
|
|
768
983
|
/**
|
|
769
|
-
* Pay the generated lightning network invoice with LNURL-withdraw
|
|
984
|
+
* Pay the generated lightning network invoice with an LNURL-withdraw link, this
|
|
985
|
+
* is useful when you want to display a lightning payment QR code and also want to
|
|
986
|
+
* allow payments using LNURL-withdraw NFC cards.
|
|
987
|
+
*
|
|
988
|
+
* Note that the swap needs to be created **without** an LNURL to begin with for this function
|
|
989
|
+
* to work. If this swap is already using an LNURL-withdraw link, this function throws.
|
|
770
990
|
*/
|
|
771
991
|
async settleWithLNURLWithdraw(lnurl) {
|
|
992
|
+
if (this._state !== FromBTCLNSwapState.PR_CREATED &&
|
|
993
|
+
(this._state !== FromBTCLNSwapState.QUOTE_SOFT_EXPIRED || this.signatureData != null))
|
|
994
|
+
throw new Error("Must be in PR_CREATED state!");
|
|
772
995
|
if (this.lnurl != null)
|
|
773
996
|
throw new Error("Cannot settle LNURL-withdraw swap with different LNURL");
|
|
774
997
|
let lnurlParams;
|
|
@@ -781,6 +1004,8 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
781
1004
|
else {
|
|
782
1005
|
lnurlParams = lnurl.params;
|
|
783
1006
|
}
|
|
1007
|
+
if (this.pr == null || !this.pr.toLowerCase().startsWith("ln"))
|
|
1008
|
+
throw new Error("Input lightning network invoice not available, the swap was probably recovered!");
|
|
784
1009
|
LNURL_1.LNURL.useLNURLWithdraw(lnurlParams, this.pr).catch(e => this.lnurlFailSignal.abort(e));
|
|
785
1010
|
this.lnurl = lnurlParams.url;
|
|
786
1011
|
this.lnurlCallback = lnurlParams.callback;
|
|
@@ -790,6 +1015,9 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
790
1015
|
}
|
|
791
1016
|
//////////////////////////////
|
|
792
1017
|
//// Storage
|
|
1018
|
+
/**
|
|
1019
|
+
* @inheritDoc
|
|
1020
|
+
*/
|
|
793
1021
|
serialize() {
|
|
794
1022
|
return {
|
|
795
1023
|
...super.serialize(),
|
|
@@ -799,7 +1027,8 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
799
1027
|
lnurlK1: this.lnurlK1,
|
|
800
1028
|
lnurlCallback: this.lnurlCallback,
|
|
801
1029
|
prPosted: this.prPosted,
|
|
802
|
-
initialSwapData: this.initialSwapData.serialize()
|
|
1030
|
+
initialSwapData: this.initialSwapData.serialize(),
|
|
1031
|
+
usesClaimHashAsId: this.usesClaimHashAsId
|
|
803
1032
|
};
|
|
804
1033
|
}
|
|
805
1034
|
//////////////////////////////
|
|
@@ -811,70 +1040,61 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
811
1040
|
* @private
|
|
812
1041
|
*/
|
|
813
1042
|
async syncStateFromChain(quoteDefinitelyExpired, commitStatus) {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
if (commitStatus?.type === base_1.SwapCommitStateType.PAID) {
|
|
823
|
-
if (this.claimTxId == null)
|
|
824
|
-
this.claimTxId = await commitStatus.getClaimTxId();
|
|
825
|
-
this.state = FromBTCLNSwapState.CLAIM_CLAIMED;
|
|
826
|
-
return true;
|
|
1043
|
+
if (this._state === FromBTCLNSwapState.PR_PAID ||
|
|
1044
|
+
(this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData != null) ||
|
|
1045
|
+
this._state === FromBTCLNSwapState.CLAIM_COMMITED ||
|
|
1046
|
+
this._state === FromBTCLNSwapState.EXPIRED) {
|
|
1047
|
+
//Check for expiry before the getCommitStatus to prevent race conditions
|
|
1048
|
+
let quoteExpired = false;
|
|
1049
|
+
if (this._state === FromBTCLNSwapState.PR_PAID || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData != null)) {
|
|
1050
|
+
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
|
|
827
1051
|
}
|
|
828
|
-
if
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
this.state = FromBTCLNSwapState.FAILED;
|
|
1052
|
+
//Check if it's already successfully paid
|
|
1053
|
+
commitStatus ??= await this.wrapper._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
1054
|
+
if (commitStatus != null && await this._forciblySetOnchainState(commitStatus))
|
|
832
1055
|
return true;
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
commitStatus ??= await this.wrapper.contract.getCommitStatus(this._getInitiator(), this.data);
|
|
838
|
-
switch (commitStatus?.type) {
|
|
839
|
-
case base_1.SwapCommitStateType.COMMITED:
|
|
840
|
-
this.state = FromBTCLNSwapState.CLAIM_COMMITED;
|
|
841
|
-
return true;
|
|
842
|
-
case base_1.SwapCommitStateType.EXPIRED:
|
|
843
|
-
if (this.refundTxId == null && commitStatus.getRefundTxId)
|
|
844
|
-
this.refundTxId = await commitStatus.getRefundTxId();
|
|
845
|
-
this.state = FromBTCLNSwapState.QUOTE_EXPIRED;
|
|
1056
|
+
//Set the state on expiry here
|
|
1057
|
+
if (this._state === FromBTCLNSwapState.PR_PAID || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData != null)) {
|
|
1058
|
+
if (quoteExpired) {
|
|
1059
|
+
this._state = FromBTCLNSwapState.QUOTE_EXPIRED;
|
|
846
1060
|
return true;
|
|
847
|
-
|
|
848
|
-
if (this.claimTxId == null && commitStatus.getClaimTxId)
|
|
849
|
-
this.claimTxId = await commitStatus.getClaimTxId();
|
|
850
|
-
this.state = FromBTCLNSwapState.CLAIM_CLAIMED;
|
|
851
|
-
return true;
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
//Set the state on expiry here
|
|
855
|
-
if (this.state === FromBTCLNSwapState.PR_PAID || (this.state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData != null)) {
|
|
856
|
-
if (quoteExpired) {
|
|
857
|
-
this.state = FromBTCLNSwapState.QUOTE_EXPIRED;
|
|
858
|
-
return true;
|
|
1061
|
+
}
|
|
859
1062
|
}
|
|
860
1063
|
}
|
|
861
1064
|
return false;
|
|
862
1065
|
}
|
|
1066
|
+
/**
|
|
1067
|
+
* @inheritDoc
|
|
1068
|
+
* @internal
|
|
1069
|
+
*/
|
|
863
1070
|
_shouldFetchExpiryStatus() {
|
|
864
|
-
return this.
|
|
1071
|
+
return this._state === FromBTCLNSwapState.PR_PAID || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData != null);
|
|
865
1072
|
}
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
1073
|
+
/**
|
|
1074
|
+
* @inheritDoc
|
|
1075
|
+
* @internal
|
|
1076
|
+
*/
|
|
1077
|
+
_shouldFetchOnchainState() {
|
|
1078
|
+
return this._state === FromBTCLNSwapState.PR_PAID || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData != null) ||
|
|
1079
|
+
this._state === FromBTCLNSwapState.CLAIM_COMMITED || this._state === FromBTCLNSwapState.EXPIRED;
|
|
869
1080
|
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Whether an intermediary (LP) should be contacted to get the state of this swap.
|
|
1083
|
+
*
|
|
1084
|
+
* @internal
|
|
1085
|
+
*/
|
|
870
1086
|
_shouldCheckIntermediary() {
|
|
871
|
-
return this.
|
|
1087
|
+
return this._state === FromBTCLNSwapState.PR_CREATED || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData == null);
|
|
872
1088
|
}
|
|
1089
|
+
/**
|
|
1090
|
+
* @inheritDoc
|
|
1091
|
+
* @internal
|
|
1092
|
+
*/
|
|
873
1093
|
async _sync(save, quoteDefinitelyExpired, commitStatus, skipLpCheck) {
|
|
874
1094
|
let changed = false;
|
|
875
|
-
if (this.
|
|
876
|
-
if (this.
|
|
877
|
-
this.
|
|
1095
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData == null)) {
|
|
1096
|
+
if (this._state != FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.getTimeoutTime() < Date.now()) {
|
|
1097
|
+
this._state = FromBTCLNSwapState.QUOTE_SOFT_EXPIRED;
|
|
878
1098
|
changed ||= true;
|
|
879
1099
|
}
|
|
880
1100
|
if (!skipLpCheck)
|
|
@@ -886,24 +1106,70 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
886
1106
|
catch (e) {
|
|
887
1107
|
this.logger.error("_sync(): Failed to synchronize swap, error: ", e);
|
|
888
1108
|
}
|
|
889
|
-
if (this.
|
|
1109
|
+
if (this._state === FromBTCLNSwapState.PR_CREATED || (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.signatureData == null)) {
|
|
890
1110
|
if (await this._verifyQuoteDefinitelyExpired()) {
|
|
891
|
-
this.
|
|
1111
|
+
this._state = FromBTCLNSwapState.QUOTE_EXPIRED;
|
|
892
1112
|
changed ||= true;
|
|
893
1113
|
}
|
|
894
1114
|
}
|
|
895
1115
|
}
|
|
896
1116
|
if (await this.syncStateFromChain(quoteDefinitelyExpired, commitStatus))
|
|
897
1117
|
changed = true;
|
|
1118
|
+
if (this._state === FromBTCLNSwapState.CLAIM_COMMITED) {
|
|
1119
|
+
const expired = await this.wrapper._contract.isExpired(this._getInitiator(), this._data);
|
|
1120
|
+
if (expired) {
|
|
1121
|
+
this._state = FromBTCLNSwapState.EXPIRED;
|
|
1122
|
+
changed = true;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
898
1125
|
if (save && changed)
|
|
899
1126
|
await this._saveAndEmit();
|
|
900
1127
|
return changed;
|
|
901
1128
|
}
|
|
1129
|
+
/**
|
|
1130
|
+
* @inheritDoc
|
|
1131
|
+
* @internal
|
|
1132
|
+
*/
|
|
1133
|
+
async _forciblySetOnchainState(commitStatus) {
|
|
1134
|
+
switch (commitStatus.type) {
|
|
1135
|
+
case base_1.SwapCommitStateType.PAID:
|
|
1136
|
+
if (this._claimTxId == null)
|
|
1137
|
+
this._claimTxId = await commitStatus.getClaimTxId();
|
|
1138
|
+
if (this.secret == null || this.pr == null)
|
|
1139
|
+
this._setSwapSecret(await commitStatus.getClaimResult());
|
|
1140
|
+
this._state = FromBTCLNSwapState.CLAIM_CLAIMED;
|
|
1141
|
+
return true;
|
|
1142
|
+
case base_1.SwapCommitStateType.NOT_COMMITED:
|
|
1143
|
+
if (this._refundTxId == null && commitStatus.getRefundTxId)
|
|
1144
|
+
this._refundTxId = await commitStatus.getRefundTxId();
|
|
1145
|
+
if (this._refundTxId != null) {
|
|
1146
|
+
this._state = FromBTCLNSwapState.FAILED;
|
|
1147
|
+
return true;
|
|
1148
|
+
}
|
|
1149
|
+
break;
|
|
1150
|
+
case base_1.SwapCommitStateType.EXPIRED:
|
|
1151
|
+
if (this._refundTxId == null && commitStatus.getRefundTxId)
|
|
1152
|
+
this._refundTxId = await commitStatus.getRefundTxId();
|
|
1153
|
+
this._state = this._refundTxId == null ? FromBTCLNSwapState.QUOTE_EXPIRED : FromBTCLNSwapState.FAILED;
|
|
1154
|
+
return true;
|
|
1155
|
+
case base_1.SwapCommitStateType.COMMITED:
|
|
1156
|
+
if (this._state !== FromBTCLNSwapState.CLAIM_COMMITED && this._state !== FromBTCLNSwapState.EXPIRED) {
|
|
1157
|
+
this._state = FromBTCLNSwapState.CLAIM_COMMITED;
|
|
1158
|
+
return true;
|
|
1159
|
+
}
|
|
1160
|
+
break;
|
|
1161
|
+
}
|
|
1162
|
+
return false;
|
|
1163
|
+
}
|
|
1164
|
+
/**
|
|
1165
|
+
* @inheritDoc
|
|
1166
|
+
* @internal
|
|
1167
|
+
*/
|
|
902
1168
|
async _tick(save) {
|
|
903
|
-
switch (this.
|
|
1169
|
+
switch (this._state) {
|
|
904
1170
|
case FromBTCLNSwapState.PR_CREATED:
|
|
905
1171
|
if (this.getTimeoutTime() < Date.now()) {
|
|
906
|
-
this.
|
|
1172
|
+
this._state = FromBTCLNSwapState.QUOTE_SOFT_EXPIRED;
|
|
907
1173
|
if (save)
|
|
908
1174
|
await this._saveAndEmit();
|
|
909
1175
|
return true;
|
|
@@ -911,16 +1177,16 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
911
1177
|
break;
|
|
912
1178
|
case FromBTCLNSwapState.PR_PAID:
|
|
913
1179
|
if (this.expiry < Date.now()) {
|
|
914
|
-
this.
|
|
1180
|
+
this._state = FromBTCLNSwapState.QUOTE_SOFT_EXPIRED;
|
|
915
1181
|
if (save)
|
|
916
1182
|
await this._saveAndEmit();
|
|
917
1183
|
return true;
|
|
918
1184
|
}
|
|
919
1185
|
break;
|
|
920
1186
|
case FromBTCLNSwapState.CLAIM_COMMITED:
|
|
921
|
-
const expired = await this.wrapper.
|
|
1187
|
+
const expired = await this.wrapper._contract.isExpired(this._getInitiator(), this._data);
|
|
922
1188
|
if (expired) {
|
|
923
|
-
this.
|
|
1189
|
+
this._state = FromBTCLNSwapState.EXPIRED;
|
|
924
1190
|
if (save)
|
|
925
1191
|
await this._saveAndEmit();
|
|
926
1192
|
return true;
|
|
@@ -929,5 +1195,16 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
929
1195
|
}
|
|
930
1196
|
return false;
|
|
931
1197
|
}
|
|
1198
|
+
/**
|
|
1199
|
+
* Forcibly sets the swap secret pre-image from on-chain data
|
|
1200
|
+
*
|
|
1201
|
+
* @internal
|
|
1202
|
+
*/
|
|
1203
|
+
_setSwapSecret(secret) {
|
|
1204
|
+
this.secret = secret;
|
|
1205
|
+
if (this.pr == null) {
|
|
1206
|
+
this.pr = buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.from(secret, "hex"))).toString("hex");
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
932
1209
|
}
|
|
933
1210
|
exports.FromBTCLNSwap = FromBTCLNSwap;
|