@atomiqlabs/sdk 7.0.12 → 8.0.7
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/README.md +45 -29
- package/dist/SmartChainAssets.d.ts +11 -3
- package/dist/SmartChainAssets.js +7 -3
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +68 -0
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +2 -0
- package/dist/bitcoin/LightningNetworkApi.d.ts +12 -0
- package/dist/bitcoin/LightningNetworkApi.js +2 -0
- package/dist/bitcoin/coinselect2/accumulative.d.ts +6 -0
- package/dist/bitcoin/coinselect2/accumulative.js +52 -0
- package/dist/bitcoin/coinselect2/blackjack.d.ts +6 -0
- package/dist/bitcoin/coinselect2/blackjack.js +38 -0
- package/dist/bitcoin/coinselect2/index.d.ts +19 -0
- package/dist/bitcoin/coinselect2/index.js +69 -0
- package/dist/bitcoin/coinselect2/utils.d.ts +71 -0
- package/dist/bitcoin/coinselect2/utils.js +123 -0
- package/dist/bitcoin/mempool/MempoolApi.d.ts +350 -0
- package/dist/bitcoin/mempool/MempoolApi.js +311 -0
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +44 -0
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +48 -0
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +119 -0
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +361 -0
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +22 -0
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +105 -0
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +93 -0
- package/dist/bitcoin/wallet/BitcoinWallet.js +273 -0
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +28 -0
- package/dist/bitcoin/wallet/IBitcoinWallet.js +20 -0
- package/dist/bitcoin/wallet/MinimalBitcoinWalletInterface.d.ts +21 -0
- package/dist/bitcoin/wallet/MinimalBitcoinWalletInterface.js +2 -0
- package/dist/bitcoin/wallet/MinimalLightningNetworkWalletInterface.d.ts +7 -0
- package/dist/bitcoin/wallet/MinimalLightningNetworkWalletInterface.js +2 -0
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +40 -0
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +86 -0
- package/dist/enums/FeeType.d.ts +8 -0
- package/dist/enums/FeeType.js +12 -0
- package/dist/enums/SwapAmountType.d.ts +8 -0
- package/dist/enums/SwapAmountType.js +12 -0
- package/dist/enums/SwapDirection.d.ts +8 -0
- package/dist/enums/SwapDirection.js +12 -0
- package/dist/enums/SwapType.d.ts +14 -0
- package/dist/enums/SwapType.js +18 -0
- package/dist/errors/IntermediaryError.d.ts +9 -0
- package/dist/errors/IntermediaryError.js +26 -0
- package/dist/errors/PaymentAuthError.d.ts +11 -0
- package/dist/errors/PaymentAuthError.js +23 -0
- package/dist/errors/RequestError.d.ts +18 -0
- package/dist/errors/RequestError.js +46 -0
- package/dist/errors/UserError.d.ts +7 -0
- package/dist/errors/UserError.js +15 -0
- package/dist/events/UnifiedSwapEventListener.d.ts +23 -0
- package/dist/events/UnifiedSwapEventListener.js +130 -0
- package/dist/http/HttpUtils.d.ts +27 -0
- package/dist/http/HttpUtils.js +91 -0
- package/dist/http/paramcoders/IParamReader.d.ts +8 -0
- package/dist/http/paramcoders/IParamReader.js +2 -0
- package/dist/http/paramcoders/ParamDecoder.d.ts +44 -0
- package/dist/http/paramcoders/ParamDecoder.js +132 -0
- package/dist/http/paramcoders/ParamEncoder.d.ts +20 -0
- package/dist/http/paramcoders/ParamEncoder.js +31 -0
- package/dist/http/paramcoders/SchemaVerifier.d.ts +26 -0
- package/dist/http/paramcoders/SchemaVerifier.js +145 -0
- package/dist/http/paramcoders/client/ResponseParamDecoder.d.ts +11 -0
- package/dist/http/paramcoders/client/ResponseParamDecoder.js +57 -0
- package/dist/http/paramcoders/client/StreamParamEncoder.d.ts +13 -0
- package/dist/http/paramcoders/client/StreamParamEncoder.js +26 -0
- package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +16 -0
- package/dist/http/paramcoders/client/StreamingFetchPromise.js +174 -0
- package/dist/index.d.ts +82 -4
- package/dist/index.js +128 -4
- package/dist/intermediaries/Intermediary.d.ts +111 -0
- package/dist/intermediaries/Intermediary.js +115 -0
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +166 -0
- package/dist/intermediaries/IntermediaryDiscovery.js +390 -0
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +436 -0
- package/dist/intermediaries/apis/IntermediaryAPI.js +600 -0
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.d.ts +154 -0
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.js +136 -0
- package/dist/lnurl/LNURL.d.ts +102 -0
- package/dist/lnurl/LNURL.js +321 -0
- package/dist/prices/RedundantSwapPrice.d.ts +89 -0
- package/dist/prices/RedundantSwapPrice.js +202 -0
- package/dist/prices/SingleSwapPrice.d.ts +31 -0
- package/dist/prices/SingleSwapPrice.js +41 -0
- package/dist/prices/SwapPriceWithChain.d.ts +70 -0
- package/dist/prices/SwapPriceWithChain.js +91 -0
- package/dist/prices/abstract/ICachedSwapPrice.d.ts +28 -0
- package/dist/prices/abstract/ICachedSwapPrice.js +62 -0
- package/dist/prices/abstract/IPriceProvider.d.ts +81 -0
- package/dist/prices/abstract/IPriceProvider.js +74 -0
- package/dist/prices/abstract/ISwapPrice.d.ts +117 -0
- package/dist/prices/abstract/ISwapPrice.js +219 -0
- package/dist/prices/providers/BinancePriceProvider.d.ts +16 -0
- package/dist/prices/providers/BinancePriceProvider.js +23 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +17 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.js +23 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +19 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +23 -0
- package/dist/prices/providers/CustomPriceProvider.d.ts +13 -0
- package/dist/prices/providers/CustomPriceProvider.js +24 -0
- package/dist/prices/providers/KrakenPriceProvider.d.ts +29 -0
- package/dist/prices/providers/KrakenPriceProvider.js +36 -0
- package/dist/prices/providers/OKXPriceProvider.d.ts +28 -0
- package/dist/prices/providers/OKXPriceProvider.js +23 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +14 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +18 -0
- package/dist/prices/providers/abstract/HttpPriceProvider.d.ts +7 -0
- package/dist/prices/providers/abstract/HttpPriceProvider.js +12 -0
- package/dist/storage/IUnifiedStorage.d.ts +73 -0
- package/dist/storage/IUnifiedStorage.js +2 -0
- package/dist/storage/UnifiedSwapStorage.d.ts +82 -0
- package/dist/storage/UnifiedSwapStorage.js +83 -0
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +39 -0
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +275 -0
- package/dist/{storage → storage-browser}/LocalStorageManager.d.ts +1 -0
- package/dist/{storage → storage-browser}/LocalStorageManager.js +2 -1
- package/dist/swapper/Swapper.d.ts +533 -0
- package/dist/swapper/Swapper.js +1566 -0
- package/dist/swapper/SwapperFactory.d.ts +87 -0
- package/dist/{SwapperFactory.js → swapper/SwapperFactory.js} +37 -19
- package/dist/swapper/SwapperUtils.d.ts +153 -0
- package/dist/swapper/SwapperUtils.js +420 -0
- package/dist/swapper/SwapperWithChain.d.ts +214 -0
- package/dist/swapper/SwapperWithChain.js +315 -0
- package/dist/swapper/SwapperWithSigner.d.ts +178 -0
- package/dist/swapper/SwapperWithSigner.js +172 -0
- package/dist/swaps/IAddressSwap.d.ts +13 -0
- package/dist/swaps/IAddressSwap.js +13 -0
- package/dist/swaps/IBTCWalletSwap.d.ts +55 -0
- package/dist/swaps/IBTCWalletSwap.js +17 -0
- package/dist/swaps/IClaimableSwap.d.ts +17 -0
- package/dist/swaps/IClaimableSwap.js +14 -0
- package/dist/swaps/IClaimableSwapWrapper.d.ts +5 -0
- package/dist/swaps/IClaimableSwapWrapper.js +2 -0
- package/dist/swaps/IRefundableSwap.d.ts +17 -0
- package/dist/swaps/IRefundableSwap.js +13 -0
- package/dist/swaps/ISwap.d.ts +207 -0
- package/dist/swaps/ISwap.js +264 -0
- package/dist/swaps/ISwapWithGasDrop.d.ts +15 -0
- package/dist/swaps/ISwapWithGasDrop.js +11 -0
- package/dist/swaps/ISwapWrapper.d.ts +153 -0
- package/dist/swaps/ISwapWrapper.js +227 -0
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +53 -0
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +116 -0
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +70 -0
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +132 -0
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +85 -0
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +122 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +86 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +115 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +93 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +121 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +45 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +65 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +263 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +933 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +110 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +307 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +236 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +898 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +125 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +393 -0
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +245 -0
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +841 -0
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +120 -0
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +294 -0
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +228 -0
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +721 -0
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +37 -0
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +93 -0
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +86 -0
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +213 -0
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +170 -0
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +520 -0
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +50 -0
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +109 -0
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +93 -0
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +217 -0
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +315 -0
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +1098 -0
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +125 -0
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +631 -0
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +107 -0
- package/dist/swaps/trusted/ln/LnForGasSwap.js +343 -0
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +21 -0
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +62 -0
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +164 -0
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +520 -0
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +48 -0
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +74 -0
- package/dist/types/AmountData.d.ts +9 -0
- package/dist/types/AmountData.js +2 -0
- package/dist/types/CustomPriceFunction.d.ts +5 -0
- package/dist/types/CustomPriceFunction.js +2 -0
- package/dist/types/PriceInfoType.d.ts +28 -0
- package/dist/types/PriceInfoType.js +57 -0
- package/dist/types/SwapExecutionAction.d.ts +7 -0
- package/dist/types/SwapExecutionAction.js +2 -0
- package/dist/types/SwapWithSigner.d.ts +14 -0
- package/dist/types/SwapWithSigner.js +40 -0
- package/dist/types/Token.d.ts +53 -0
- package/dist/types/Token.js +58 -0
- package/dist/types/TokenAmount.d.ts +57 -0
- package/dist/types/TokenAmount.js +47 -0
- package/dist/types/fees/Fee.d.ts +49 -0
- package/dist/types/fees/Fee.js +2 -0
- package/dist/types/fees/FeeBreakdown.d.ts +10 -0
- package/dist/types/fees/FeeBreakdown.js +2 -0
- package/dist/types/fees/PercentagePPM.d.ts +15 -0
- package/dist/types/fees/PercentagePPM.js +17 -0
- package/dist/types/lnurl/LNURLPay.d.ts +54 -0
- package/dist/types/lnurl/LNURLPay.js +28 -0
- package/dist/types/lnurl/LNURLWithdraw.d.ts +42 -0
- package/dist/types/lnurl/LNURLWithdraw.js +24 -0
- package/dist/utils/AutomaticClockDriftCorrection.d.ts +1 -0
- package/dist/utils/AutomaticClockDriftCorrection.js +70 -0
- package/dist/utils/BitcoinUtils.d.ts +13 -0
- package/dist/utils/BitcoinUtils.js +98 -0
- package/dist/utils/BitcoinWalletUtils.d.ts +7 -0
- package/dist/utils/BitcoinWalletUtils.js +14 -0
- package/dist/utils/Logger.d.ts +7 -0
- package/dist/utils/Logger.js +12 -0
- package/dist/utils/RetryUtils.d.ts +21 -0
- package/dist/utils/RetryUtils.js +66 -0
- package/dist/utils/SwapUtils.d.ts +31 -0
- package/dist/utils/SwapUtils.js +18 -0
- package/dist/{Utils.d.ts → utils/TimeoutUtils.d.ts} +9 -3
- package/dist/utils/TimeoutUtils.js +55 -0
- package/dist/utils/TokenUtils.d.ts +11 -0
- package/dist/utils/TokenUtils.js +29 -0
- package/dist/utils/TypeUtils.d.ts +7 -0
- package/dist/utils/TypeUtils.js +2 -0
- package/dist/utils/Utils.d.ts +57 -0
- package/dist/utils/Utils.js +178 -0
- package/package.json +14 -6
- package/src/SmartChainAssets.ts +11 -3
- package/src/bitcoin/BitcoinRpcWithAddressIndex.ts +87 -0
- package/src/bitcoin/LightningNetworkApi.ts +16 -0
- package/src/bitcoin/coinselect2/accumulative.ts +68 -0
- package/src/bitcoin/coinselect2/blackjack.ts +49 -0
- package/src/bitcoin/coinselect2/index.ts +92 -0
- package/src/bitcoin/coinselect2/utils.ts +189 -0
- package/src/bitcoin/mempool/MempoolApi.ts +554 -0
- package/src/bitcoin/mempool/MempoolBitcoinBlock.ts +88 -0
- package/src/bitcoin/mempool/MempoolBitcoinRpc.ts +437 -0
- package/src/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.ts +134 -0
- package/src/bitcoin/wallet/BitcoinWallet.ts +375 -0
- package/src/bitcoin/wallet/IBitcoinWallet.ts +44 -0
- package/src/bitcoin/wallet/MinimalBitcoinWalletInterface.ts +19 -0
- package/src/bitcoin/wallet/MinimalLightningNetworkWalletInterface.ts +7 -0
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +108 -0
- package/src/enums/FeeType.ts +9 -0
- package/src/enums/SwapAmountType.ts +9 -0
- package/src/enums/SwapDirection.ts +9 -0
- package/src/enums/SwapType.ts +15 -0
- package/src/errors/IntermediaryError.ts +24 -0
- package/src/errors/PaymentAuthError.ts +26 -0
- package/src/errors/RequestError.ts +51 -0
- package/src/errors/UserError.ts +14 -0
- package/src/events/UnifiedSwapEventListener.ts +171 -0
- package/src/http/HttpUtils.ts +92 -0
- package/src/http/paramcoders/IParamReader.ts +10 -0
- package/src/http/paramcoders/ParamDecoder.ts +142 -0
- package/src/http/paramcoders/ParamEncoder.ts +37 -0
- package/src/http/paramcoders/SchemaVerifier.ts +153 -0
- package/src/http/paramcoders/client/ResponseParamDecoder.ts +58 -0
- package/src/http/paramcoders/client/StreamParamEncoder.ts +29 -0
- package/src/http/paramcoders/client/StreamingFetchPromise.ts +193 -0
- package/src/index.ts +102 -4
- package/src/intermediaries/Intermediary.ts +204 -0
- package/src/intermediaries/IntermediaryDiscovery.ts +485 -0
- package/src/intermediaries/apis/IntermediaryAPI.ts +940 -0
- package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +257 -0
- package/src/lnurl/LNURL.ts +403 -0
- package/src/prices/RedundantSwapPrice.ts +245 -0
- package/src/prices/SingleSwapPrice.ts +47 -0
- package/src/prices/SwapPriceWithChain.ts +157 -0
- package/src/prices/abstract/ICachedSwapPrice.ts +86 -0
- package/src/prices/abstract/IPriceProvider.ts +128 -0
- package/src/prices/abstract/ISwapPrice.ts +328 -0
- package/src/prices/providers/BinancePriceProvider.ts +41 -0
- package/src/prices/providers/CoinGeckoPriceProvider.ts +40 -0
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +44 -0
- package/src/prices/providers/CustomPriceProvider.ts +29 -0
- package/src/prices/providers/KrakenPriceProvider.ts +74 -0
- package/src/prices/providers/OKXPriceProvider.ts +53 -0
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +29 -0
- package/src/prices/providers/abstract/HttpPriceProvider.ts +15 -0
- package/src/storage/IUnifiedStorage.ts +83 -0
- package/src/storage/UnifiedSwapStorage.ts +104 -0
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +328 -0
- package/src/{storage → storage-browser}/LocalStorageManager.ts +2 -1
- package/src/swapper/Swapper.ts +2107 -0
- package/src/{SwapperFactory.ts → swapper/SwapperFactory.ts} +113 -72
- package/src/swapper/SwapperUtils.ts +510 -0
- package/src/swapper/SwapperWithChain.ts +464 -0
- package/src/swapper/SwapperWithSigner.ts +300 -0
- package/src/swaps/IAddressSwap.ts +20 -0
- package/src/swaps/IBTCWalletSwap.ts +77 -0
- package/src/swaps/IClaimableSwap.ts +30 -0
- package/src/swaps/IClaimableSwapWrapper.ts +9 -0
- package/src/swaps/IRefundableSwap.ts +29 -0
- package/src/swaps/ISwap.ts +490 -0
- package/src/swaps/ISwapWithGasDrop.ts +19 -0
- package/src/swaps/ISwapWrapper.ts +344 -0
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +168 -0
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +197 -0
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +210 -0
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +150 -0
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +219 -0
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +84 -0
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +1082 -0
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +429 -0
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +1078 -0
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +549 -0
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +974 -0
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +443 -0
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +860 -0
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +104 -0
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +256 -0
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +716 -0
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +151 -0
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +299 -0
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +1394 -0
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +796 -0
- package/src/swaps/trusted/ln/LnForGasSwap.ts +402 -0
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +70 -0
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +633 -0
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +110 -0
- package/src/types/AmountData.ts +9 -0
- package/src/types/CustomPriceFunction.ts +5 -0
- package/src/types/PriceInfoType.ts +67 -0
- package/src/types/SwapExecutionAction.ts +8 -0
- package/src/types/SwapWithSigner.ts +57 -0
- package/src/types/Token.ts +90 -0
- package/src/types/TokenAmount.ts +110 -0
- package/src/types/fees/Fee.ts +55 -0
- package/src/types/fees/FeeBreakdown.ts +11 -0
- package/src/types/fees/PercentagePPM.ts +24 -0
- package/src/types/lnurl/LNURLPay.ts +72 -0
- package/src/types/lnurl/LNURLWithdraw.ts +55 -0
- package/src/utils/AutomaticClockDriftCorrection.ts +71 -0
- package/src/utils/BitcoinUtils.ts +86 -0
- package/src/utils/BitcoinWalletUtils.ts +16 -0
- package/src/utils/Logger.ts +15 -0
- package/src/utils/RetryUtils.ts +71 -0
- package/src/utils/SwapUtils.ts +38 -0
- package/src/utils/TimeoutUtils.ts +50 -0
- package/src/utils/TokenUtils.ts +25 -0
- package/src/utils/TypeUtils.ts +9 -0
- package/src/utils/Utils.ts +182 -0
- package/dist/SwapperFactory.d.ts +0 -52
- package/dist/Utils.js +0 -37
- package/dist/fs-storage/FileSystemStorageManager.d.ts +0 -15
- package/dist/fs-storage/FileSystemStorageManager.js +0 -60
- package/dist/fs-storage/index.d.ts +0 -1
- package/dist/fs-storage/index.js +0 -17
- package/src/SmartChainAssets.js +0 -75
- package/src/SwapperFactory.js +0 -120
- package/src/Utils.js +0 -37
- package/src/Utils.ts +0 -31
- package/src/fs-storage/FileSystemStorageManager.ts +0 -71
- package/src/fs-storage/index.ts +0 -1
- package/src/index.js +0 -21
- package/src/storage/LocalStorageManager.js +0 -72
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import {coinSelect, maxSendable, CoinselectAddressTypes, CoinselectTxInput} from "../coinselect2";
|
|
2
|
+
import {BTC_NETWORK} from "@scure/btc-signer/utils"
|
|
3
|
+
import {p2wpkh, OutScript, Transaction, p2tr, Address} from "@scure/btc-signer";
|
|
4
|
+
import {IBitcoinWallet} from "./IBitcoinWallet";
|
|
5
|
+
import {Buffer} from "buffer";
|
|
6
|
+
import {randomBytes} from "../../utils/Utils";
|
|
7
|
+
import {toCoinselectAddressType, toOutputScript} from "../../utils/BitcoinUtils";
|
|
8
|
+
import {BitcoinRpcWithAddressIndex} from "../BitcoinRpcWithAddressIndex";
|
|
9
|
+
import {TransactionInputUpdate} from "@scure/btc-signer/psbt";
|
|
10
|
+
import {getLogger} from "../../utils/Logger";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* UTXO data structure for Bitcoin wallets
|
|
14
|
+
* @category Bitcoin
|
|
15
|
+
*/
|
|
16
|
+
export type BitcoinWalletUtxo = {
|
|
17
|
+
vout: number,
|
|
18
|
+
txId: string,
|
|
19
|
+
value: number,
|
|
20
|
+
type: CoinselectAddressTypes,
|
|
21
|
+
outputScript: Buffer,
|
|
22
|
+
address: string,
|
|
23
|
+
cpfp?: {
|
|
24
|
+
txVsize: number,
|
|
25
|
+
txEffectiveFeeRate: number
|
|
26
|
+
},
|
|
27
|
+
confirmed: boolean
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Identifies the address type of a Bitcoin address
|
|
32
|
+
* @category Bitcoin
|
|
33
|
+
*/
|
|
34
|
+
export function identifyAddressType(address: string, network: BTC_NETWORK): CoinselectAddressTypes {
|
|
35
|
+
switch(Address(network).decode(address).type) {
|
|
36
|
+
case "pkh":
|
|
37
|
+
return "p2pkh";
|
|
38
|
+
case "wpkh":
|
|
39
|
+
return "p2wpkh";
|
|
40
|
+
case "tr":
|
|
41
|
+
return "p2tr";
|
|
42
|
+
case "sh":
|
|
43
|
+
return "p2sh-p2wpkh";
|
|
44
|
+
case "wsh":
|
|
45
|
+
return "p2wsh";
|
|
46
|
+
default:
|
|
47
|
+
throw new Error("Unknown address type of "+address);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const logger = getLogger("BitcoinWallet: ");
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Abstract base class for Bitcoin wallet implementations
|
|
55
|
+
* @category Bitcoin
|
|
56
|
+
*/
|
|
57
|
+
export abstract class BitcoinWallet implements IBitcoinWallet {
|
|
58
|
+
|
|
59
|
+
rpc: BitcoinRpcWithAddressIndex<any>;
|
|
60
|
+
network: BTC_NETWORK;
|
|
61
|
+
feeMultiplier: number;
|
|
62
|
+
feeOverride?: number;
|
|
63
|
+
|
|
64
|
+
constructor(mempoolApi: BitcoinRpcWithAddressIndex<any>, network: BTC_NETWORK, feeMultiplier: number = 1.25, feeOverride?: number) {
|
|
65
|
+
this.rpc = mempoolApi;
|
|
66
|
+
this.network = network;
|
|
67
|
+
this.feeMultiplier = feeMultiplier;
|
|
68
|
+
this.feeOverride = feeOverride;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async getFeeRate(): Promise<number> {
|
|
72
|
+
if(this.feeOverride!=null) {
|
|
73
|
+
return this.feeOverride;
|
|
74
|
+
}
|
|
75
|
+
return Math.floor((await this.rpc.getFeeRate())*this.feeMultiplier);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
protected _sendTransaction(rawHex: string): Promise<string> {
|
|
79
|
+
return this.rpc.sendRawTransaction(rawHex);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected _getBalance(address: string): Promise<{ confirmedBalance: bigint; unconfirmedBalance: bigint }> {
|
|
83
|
+
return this.rpc.getAddressBalances(address);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected async _getUtxoPool(
|
|
87
|
+
sendingAddress: string,
|
|
88
|
+
sendingAddressType: CoinselectAddressTypes
|
|
89
|
+
): Promise<BitcoinWalletUtxo[]> {
|
|
90
|
+
const utxos = await this.rpc.getAddressUTXOs(sendingAddress);
|
|
91
|
+
|
|
92
|
+
let totalSpendable = 0;
|
|
93
|
+
|
|
94
|
+
const outputScript = toOutputScript(this.network, sendingAddress);
|
|
95
|
+
|
|
96
|
+
const utxoPool: BitcoinWalletUtxo[] = [];
|
|
97
|
+
|
|
98
|
+
for(let utxo of utxos) {
|
|
99
|
+
const value = Number(utxo.value);
|
|
100
|
+
totalSpendable += value;
|
|
101
|
+
utxoPool.push({
|
|
102
|
+
vout: utxo.vout,
|
|
103
|
+
txId: utxo.txid,
|
|
104
|
+
value: value,
|
|
105
|
+
type: sendingAddressType,
|
|
106
|
+
outputScript: outputScript,
|
|
107
|
+
address: sendingAddress,
|
|
108
|
+
cpfp: !utxo.confirmed ? await this.rpc.getCPFPData(utxo.txid).then((result) => {
|
|
109
|
+
if(result.effectiveFeePerVsize==null) return;
|
|
110
|
+
return {
|
|
111
|
+
txVsize: result.adjustedVsize,
|
|
112
|
+
txEffectiveFeeRate: result.effectiveFeePerVsize
|
|
113
|
+
}
|
|
114
|
+
}) : undefined,
|
|
115
|
+
confirmed: utxo.confirmed
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
logger.debug("_getUtxoPool(): Total spendable value: "+totalSpendable+" num utxos: "+utxoPool.length);
|
|
120
|
+
|
|
121
|
+
return utxoPool;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
protected async _getPsbt(
|
|
125
|
+
sendingAccounts: {
|
|
126
|
+
pubkey: string,
|
|
127
|
+
address: string,
|
|
128
|
+
addressType: CoinselectAddressTypes,
|
|
129
|
+
}[],
|
|
130
|
+
recipient: string,
|
|
131
|
+
amount: number,
|
|
132
|
+
feeRate?: number
|
|
133
|
+
): Promise<{
|
|
134
|
+
fee: number,
|
|
135
|
+
psbt?: Transaction,
|
|
136
|
+
inputAddressIndexes?: {[address: string]: number[]}
|
|
137
|
+
}> {
|
|
138
|
+
const psbt = new Transaction({PSBTVersion: 0});
|
|
139
|
+
psbt.addOutput({
|
|
140
|
+
amount: BigInt(amount),
|
|
141
|
+
script: toOutputScript(this.network, recipient)
|
|
142
|
+
});
|
|
143
|
+
return this._fundPsbt(sendingAccounts, psbt, feeRate);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
protected async _fundPsbt(
|
|
147
|
+
sendingAccounts: {
|
|
148
|
+
pubkey: string,
|
|
149
|
+
address: string,
|
|
150
|
+
addressType: CoinselectAddressTypes,
|
|
151
|
+
}[],
|
|
152
|
+
psbt: Transaction,
|
|
153
|
+
feeRate?: number
|
|
154
|
+
): Promise<{
|
|
155
|
+
fee: number,
|
|
156
|
+
psbt?: Transaction,
|
|
157
|
+
inputAddressIndexes?: {[address: string]: number[]}
|
|
158
|
+
}> {
|
|
159
|
+
if(feeRate==null) feeRate = await this.getFeeRate();
|
|
160
|
+
|
|
161
|
+
const utxoPool: BitcoinWalletUtxo[] = (await Promise.all(sendingAccounts.map(acc => this._getUtxoPool(acc.address, acc.addressType)))).flat();
|
|
162
|
+
|
|
163
|
+
logger.debug("_fundPsbt(): fee rate: "+feeRate+" utxo pool: ", utxoPool);
|
|
164
|
+
|
|
165
|
+
const accountPubkeys: Record<string, string> = {};
|
|
166
|
+
sendingAccounts.forEach(acc => accountPubkeys[acc.address] = acc.pubkey);
|
|
167
|
+
|
|
168
|
+
const requiredInputs: CoinselectTxInput[] = [];
|
|
169
|
+
for(let i=0;i<psbt.inputsLength;i++) {
|
|
170
|
+
const input = psbt.getInput(i);
|
|
171
|
+
if(input.index==null || input.txid==null) throw new Error("Inputs need txid & index!");
|
|
172
|
+
let amount: bigint;
|
|
173
|
+
let script: Uint8Array;
|
|
174
|
+
if(input.witnessUtxo!=null) {
|
|
175
|
+
amount = input.witnessUtxo.amount as bigint;
|
|
176
|
+
script = input.witnessUtxo.script as Uint8Array;
|
|
177
|
+
} else if(input.nonWitnessUtxo!=null) {
|
|
178
|
+
amount = input.nonWitnessUtxo.outputs[input.index].amount;
|
|
179
|
+
script = input.nonWitnessUtxo.outputs[input.index].script;
|
|
180
|
+
} else throw new Error("Either witnessUtxo or nonWitnessUtxo has to be defined!");
|
|
181
|
+
requiredInputs.push({
|
|
182
|
+
txId: Buffer.from(input.txid).toString('hex'),
|
|
183
|
+
vout: input.index,
|
|
184
|
+
value: Number(amount),
|
|
185
|
+
type: toCoinselectAddressType(script)
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const targets: {value: number, script: Buffer}[] = [];
|
|
190
|
+
for(let i=0;i<psbt.outputsLength;i++) {
|
|
191
|
+
const output = psbt.getOutput(i);
|
|
192
|
+
if(output.amount==null || output.script==null) throw new Error("Outputs need amount & script defined!");
|
|
193
|
+
targets.push({
|
|
194
|
+
value: Number(output.amount),
|
|
195
|
+
script: Buffer.from(output.script)
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
logger.debug("_fundPsbt(): Coinselect targets: ", targets);
|
|
199
|
+
|
|
200
|
+
let coinselectResult = coinSelect(utxoPool, targets, feeRate, sendingAccounts[0].addressType, requiredInputs);
|
|
201
|
+
logger.debug("_fundPsbt(): Coinselect result: ", coinselectResult);
|
|
202
|
+
|
|
203
|
+
if(coinselectResult.inputs==null || coinselectResult.outputs==null) {
|
|
204
|
+
return {
|
|
205
|
+
fee: coinselectResult.fee
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Remove in/outs that are already in the PSBT
|
|
210
|
+
coinselectResult.inputs.splice(0, psbt.inputsLength);
|
|
211
|
+
coinselectResult.outputs.splice(0, psbt.outputsLength);
|
|
212
|
+
|
|
213
|
+
const inputAddressIndexes: {[address: string]: number[]} = {};
|
|
214
|
+
coinselectResult.inputs.forEach((input, index) => {
|
|
215
|
+
inputAddressIndexes[input.address!] ??= [];
|
|
216
|
+
inputAddressIndexes[input.address!].push(index);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const formattedInputs: TransactionInputUpdate[] = await Promise.all<TransactionInputUpdate>(coinselectResult.inputs.map(async (input) => {
|
|
220
|
+
switch(input.type) {
|
|
221
|
+
case "p2tr":
|
|
222
|
+
const parsed = p2tr(Buffer.from(accountPubkeys[input.address!], "hex"));
|
|
223
|
+
return {
|
|
224
|
+
txid: input.txId,
|
|
225
|
+
index: input.vout,
|
|
226
|
+
witnessUtxo: {
|
|
227
|
+
script: input.outputScript!,
|
|
228
|
+
amount: BigInt(input.value)
|
|
229
|
+
},
|
|
230
|
+
tapInternalKey: parsed.tapInternalKey,
|
|
231
|
+
tapMerkleRoot: parsed.tapMerkleRoot,
|
|
232
|
+
tapLeafScript: parsed.tapLeafScript
|
|
233
|
+
};
|
|
234
|
+
case "p2wpkh":
|
|
235
|
+
return {
|
|
236
|
+
txid: input.txId,
|
|
237
|
+
index: input.vout,
|
|
238
|
+
witnessUtxo: {
|
|
239
|
+
script: input.outputScript!,
|
|
240
|
+
amount: BigInt(input.value)
|
|
241
|
+
},
|
|
242
|
+
sighashType: 0x01
|
|
243
|
+
};
|
|
244
|
+
case "p2sh-p2wpkh":
|
|
245
|
+
return {
|
|
246
|
+
txid: input.txId,
|
|
247
|
+
index: input.vout,
|
|
248
|
+
witnessUtxo: {
|
|
249
|
+
script: input.outputScript!,
|
|
250
|
+
amount: BigInt(input.value)
|
|
251
|
+
},
|
|
252
|
+
redeemScript: p2wpkh(Buffer.from(accountPubkeys[input.address!], "hex"), this.network).script,
|
|
253
|
+
sighashType: 0x01
|
|
254
|
+
};
|
|
255
|
+
case "p2pkh":
|
|
256
|
+
const tx = await this.rpc.getTransaction(input.txId);
|
|
257
|
+
if(tx==null) throw new Error("Cannot fetch existing tx "+input.txId);
|
|
258
|
+
return {
|
|
259
|
+
txid: input.txId,
|
|
260
|
+
index: input.vout,
|
|
261
|
+
nonWitnessUtxo: tx.raw,
|
|
262
|
+
sighashType: 0x01
|
|
263
|
+
};
|
|
264
|
+
default:
|
|
265
|
+
throw new Error("Invalid input type: "+input.type);
|
|
266
|
+
}
|
|
267
|
+
}));
|
|
268
|
+
|
|
269
|
+
formattedInputs.forEach(input => psbt.addInput(input));
|
|
270
|
+
|
|
271
|
+
coinselectResult.outputs.forEach(output => {
|
|
272
|
+
if(output.script==null && output.address==null) {
|
|
273
|
+
//Change output
|
|
274
|
+
psbt.addOutput({
|
|
275
|
+
script: toOutputScript(this.network, sendingAccounts[0].address),
|
|
276
|
+
amount: BigInt(Math.floor(output.value))
|
|
277
|
+
});
|
|
278
|
+
} else {
|
|
279
|
+
psbt.addOutput({
|
|
280
|
+
script: output.script ?? toOutputScript(this.network, output.address!),
|
|
281
|
+
amount: BigInt(output.value)
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
psbt,
|
|
288
|
+
fee: coinselectResult.fee,
|
|
289
|
+
inputAddressIndexes
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
protected async _getSpendableBalance(
|
|
294
|
+
sendingAccounts: {
|
|
295
|
+
address: string,
|
|
296
|
+
addressType: CoinselectAddressTypes,
|
|
297
|
+
}[],
|
|
298
|
+
psbt?: Transaction,
|
|
299
|
+
feeRate?: number
|
|
300
|
+
): Promise<{
|
|
301
|
+
balance: bigint,
|
|
302
|
+
feeRate: number,
|
|
303
|
+
totalFee: number
|
|
304
|
+
}> {
|
|
305
|
+
feeRate ??= await this.getFeeRate();
|
|
306
|
+
|
|
307
|
+
const utxoPool: BitcoinWalletUtxo[] = (await Promise.all(sendingAccounts.map(acc => this._getUtxoPool(acc.address, acc.addressType)))).flat();
|
|
308
|
+
|
|
309
|
+
const requiredInputs: CoinselectTxInput[] = [];
|
|
310
|
+
if(psbt!=null) for(let i=0;i<psbt.inputsLength;i++) {
|
|
311
|
+
const input = psbt.getInput(i);
|
|
312
|
+
if(input.index==null || input.txid==null) throw new Error("Inputs need txid & index!");
|
|
313
|
+
let amount: bigint;
|
|
314
|
+
let script: Uint8Array;
|
|
315
|
+
if(input.witnessUtxo!=null) {
|
|
316
|
+
amount = input.witnessUtxo.amount as bigint;
|
|
317
|
+
script = input.witnessUtxo.script as Uint8Array;
|
|
318
|
+
} else if(input.nonWitnessUtxo!=null) {
|
|
319
|
+
amount = input.nonWitnessUtxo.outputs[input.index].amount;
|
|
320
|
+
script = input.nonWitnessUtxo.outputs[input.index].script;
|
|
321
|
+
} else throw new Error("Either witnessUtxo or nonWitnessUtxo has to be defined!");
|
|
322
|
+
requiredInputs.push({
|
|
323
|
+
txId: Buffer.from(input.txid).toString('hex'),
|
|
324
|
+
vout: input.index,
|
|
325
|
+
value: Number(amount),
|
|
326
|
+
type: toCoinselectAddressType(script)
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const additionalOutputs: {value: number, script: Buffer}[] = [];
|
|
331
|
+
if(psbt!=null) for(let i=0;i<psbt.outputsLength;i++) {
|
|
332
|
+
const output = psbt.getOutput(i);
|
|
333
|
+
if(output.amount==null || output.script==null) throw new Error("Outputs need amount & script!");
|
|
334
|
+
additionalOutputs.push({
|
|
335
|
+
value: Number(output.amount),
|
|
336
|
+
script: Buffer.from(output.script)
|
|
337
|
+
})
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const target = OutScript.encode({
|
|
341
|
+
type: "wsh",
|
|
342
|
+
hash: randomBytes(32)
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
let coinselectResult = maxSendable(utxoPool, {script: Buffer.from(target), type: "p2wsh"}, feeRate, requiredInputs, additionalOutputs);
|
|
346
|
+
|
|
347
|
+
logger.debug("_getSpendableBalance(): Max spendable result: ", coinselectResult);
|
|
348
|
+
|
|
349
|
+
return {
|
|
350
|
+
feeRate: feeRate,
|
|
351
|
+
balance: BigInt(Math.floor(coinselectResult.value)),
|
|
352
|
+
totalFee: coinselectResult.fee
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
abstract sendTransaction(address: string, amount: bigint, feeRate?: number): Promise<string>;
|
|
357
|
+
abstract fundPsbt(psbt: Transaction, feeRate?: number): Promise<Transaction>;
|
|
358
|
+
abstract signPsbt(psbt: Transaction, signInputs: number[]): Promise<Transaction>;
|
|
359
|
+
|
|
360
|
+
abstract getTransactionFee(address: string, amount: bigint, feeRate?: number): Promise<number>;
|
|
361
|
+
abstract getFundedPsbtFee(psbt: Transaction, feeRate?: number): Promise<number>;
|
|
362
|
+
|
|
363
|
+
abstract getReceiveAddress(): string;
|
|
364
|
+
abstract getBalance(): Promise<{
|
|
365
|
+
confirmedBalance: bigint,
|
|
366
|
+
unconfirmedBalance: bigint
|
|
367
|
+
}>;
|
|
368
|
+
abstract getSpendableBalance(psbt?: Transaction, feeRate?: number): Promise<{
|
|
369
|
+
balance: bigint,
|
|
370
|
+
feeRate: number,
|
|
371
|
+
totalFee: number
|
|
372
|
+
}>;
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {Transaction} from "@scure/btc-signer";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type guard to check if an object implements IBitcoinWallet
|
|
5
|
+
* @category Bitcoin
|
|
6
|
+
*/
|
|
7
|
+
export function isIBitcoinWallet(val: any): val is IBitcoinWallet {
|
|
8
|
+
return val!==null &&
|
|
9
|
+
typeof(val.sendTransaction)==="function" &&
|
|
10
|
+
typeof(val.fundPsbt)==="function" &&
|
|
11
|
+
typeof(val.signPsbt)==="function" &&
|
|
12
|
+
typeof(val.getFeeRate)==="function" &&
|
|
13
|
+
typeof(val.getTransactionFee)==="function" &&
|
|
14
|
+
typeof(val.getFundedPsbtFee)==="function" &&
|
|
15
|
+
typeof(val.getReceiveAddress)==="function" &&
|
|
16
|
+
typeof(val.getBalance)==="function" &&
|
|
17
|
+
typeof(val.getSpendableBalance)==="function";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Interface for Bitcoin wallet operations
|
|
22
|
+
* @category Bitcoin
|
|
23
|
+
*/
|
|
24
|
+
export interface IBitcoinWallet {
|
|
25
|
+
sendTransaction(address: string, amount: bigint, feeRate?: number): Promise<string>;
|
|
26
|
+
fundPsbt(psbt: Transaction, feeRate?: number): Promise<Transaction>;
|
|
27
|
+
signPsbt(psbt: Transaction, signInputs: number[]): Promise<Transaction>;
|
|
28
|
+
|
|
29
|
+
getFeeRate(): Promise<number>;
|
|
30
|
+
|
|
31
|
+
getTransactionFee(address: string, amount: bigint, feeRate?: number): Promise<number>;
|
|
32
|
+
getFundedPsbtFee(psbt: Transaction, feeRate?: number): Promise<number>;
|
|
33
|
+
|
|
34
|
+
getReceiveAddress(): string;
|
|
35
|
+
getBalance(): Promise<{
|
|
36
|
+
confirmedBalance: bigint,
|
|
37
|
+
unconfirmedBalance: bigint
|
|
38
|
+
}>;
|
|
39
|
+
getSpendableBalance(psbt?: Transaction, feeRate?: number): Promise<{
|
|
40
|
+
balance: bigint,
|
|
41
|
+
feeRate: number,
|
|
42
|
+
totalFee: number
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {Transaction} from "@scure/btc-signer";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A type with minimum possible required data about a bitcoin wallet to be able to estimate fees and produce unsigned
|
|
5
|
+
* PSBTs
|
|
6
|
+
* @category Bitcoin
|
|
7
|
+
*/
|
|
8
|
+
export type MinimalBitcoinWalletInterface = {
|
|
9
|
+
address: string,
|
|
10
|
+
publicKey: string
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A type with minimum possible required data about a bitcoin wallet to be able to estimate fees and sign PSBTs
|
|
15
|
+
* @category Bitcoin
|
|
16
|
+
*/
|
|
17
|
+
export type MinimalBitcoinWalletInterfaceWithSigner = MinimalBitcoinWalletInterface & {
|
|
18
|
+
signPsbt: (psbtToSign: {psbt: Transaction, psbtHex: string, psbtBase64: string}, signInputs: number[]) => Promise<Transaction | string>
|
|
19
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import {CoinselectAddressTypes} from "../coinselect2";
|
|
2
|
+
import {BTC_NETWORK, pubECDSA, randomPrivateKeyBytes} from "@scure/btc-signer/utils"
|
|
3
|
+
import {getAddress, Transaction, WIF} from "@scure/btc-signer";
|
|
4
|
+
import {Buffer} from "buffer";
|
|
5
|
+
import {identifyAddressType, BitcoinWallet} from "./BitcoinWallet";
|
|
6
|
+
import {BitcoinRpcWithAddressIndex} from "../BitcoinRpcWithAddressIndex";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Bitcoin wallet implementation for single-address scenarios
|
|
10
|
+
* @category Bitcoin
|
|
11
|
+
*/
|
|
12
|
+
export class SingleAddressBitcoinWallet extends BitcoinWallet {
|
|
13
|
+
|
|
14
|
+
readonly privKey?: Uint8Array;
|
|
15
|
+
readonly pubkey: Uint8Array;
|
|
16
|
+
readonly address: string;
|
|
17
|
+
readonly addressType: CoinselectAddressTypes;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
mempoolApi: BitcoinRpcWithAddressIndex<any>,
|
|
21
|
+
network: BTC_NETWORK,
|
|
22
|
+
addressDataOrWIF: string | {address: string, publicKey: string},
|
|
23
|
+
feeMultiplier: number = 1.25,
|
|
24
|
+
feeOverride?: number
|
|
25
|
+
) {
|
|
26
|
+
super(mempoolApi, network, feeMultiplier, feeOverride);
|
|
27
|
+
if(typeof(addressDataOrWIF)==="string") {
|
|
28
|
+
try {
|
|
29
|
+
this.privKey = WIF(network).decode(addressDataOrWIF);
|
|
30
|
+
} catch(e) {
|
|
31
|
+
this.privKey = WIF().decode(addressDataOrWIF);
|
|
32
|
+
}
|
|
33
|
+
this.pubkey = pubECDSA(this.privKey);
|
|
34
|
+
const address = getAddress("wpkh", this.privKey, network);
|
|
35
|
+
if(address==null) throw new Error("Failed to generate p2wpkh address from the provided private key!");
|
|
36
|
+
this.address = address;
|
|
37
|
+
} else {
|
|
38
|
+
this.address = addressDataOrWIF.address;
|
|
39
|
+
this.pubkey = Buffer.from(addressDataOrWIF.publicKey, "hex");
|
|
40
|
+
}
|
|
41
|
+
this.addressType = identifyAddressType(this.address, network);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected toBitcoinWalletAccounts(): {pubkey: string, address: string, addressType: CoinselectAddressTypes}[] {
|
|
45
|
+
return [{
|
|
46
|
+
pubkey: Buffer.from(this.pubkey).toString("hex"), address: this.address, addressType: this.addressType
|
|
47
|
+
}];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async sendTransaction(address: string, amount: bigint, feeRate?: number): Promise<string> {
|
|
51
|
+
if(!this.privKey) throw new Error("Not supported.");
|
|
52
|
+
const {psbt, fee} = await super._getPsbt(this.toBitcoinWalletAccounts(), address, Number(amount), feeRate);
|
|
53
|
+
if(psbt==null) throw new Error(`Not enough funds, required for fee: ${fee} sats!`);
|
|
54
|
+
psbt.sign(this.privKey);
|
|
55
|
+
psbt.finalize();
|
|
56
|
+
const txHex = Buffer.from(psbt.extract()).toString("hex");
|
|
57
|
+
return await super._sendTransaction(txHex);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async fundPsbt(inputPsbt: Transaction, feeRate?: number): Promise<Transaction> {
|
|
61
|
+
const {psbt} = await super._fundPsbt(this.toBitcoinWalletAccounts(), inputPsbt, feeRate);
|
|
62
|
+
if(psbt==null) {
|
|
63
|
+
throw new Error("Not enough balance!");
|
|
64
|
+
}
|
|
65
|
+
return psbt;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async signPsbt(psbt: Transaction, signInputs: number[]): Promise<Transaction> {
|
|
69
|
+
if(!this.privKey) throw new Error("Not supported.");
|
|
70
|
+
for(let signInput of signInputs) {
|
|
71
|
+
psbt.signIdx(this.privKey, signInput);
|
|
72
|
+
}
|
|
73
|
+
return psbt;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async getTransactionFee(address: string, amount: bigint, feeRate?: number): Promise<number> {
|
|
77
|
+
const {fee} = await super._getPsbt(this.toBitcoinWalletAccounts(), address, Number(amount), feeRate);
|
|
78
|
+
return fee;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async getFundedPsbtFee(basePsbt: Transaction, feeRate?: number): Promise<number> {
|
|
82
|
+
const {fee} = await super._fundPsbt(this.toBitcoinWalletAccounts(), basePsbt, feeRate);
|
|
83
|
+
return fee;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
getReceiveAddress(): string {
|
|
87
|
+
return this.address;
|
|
88
|
+
}
|
|
89
|
+
getBalance(): Promise<{
|
|
90
|
+
confirmedBalance: bigint,
|
|
91
|
+
unconfirmedBalance: bigint
|
|
92
|
+
}> {
|
|
93
|
+
return this._getBalance(this.address);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getSpendableBalance(psbt?: Transaction, feeRate?: number): Promise<{
|
|
97
|
+
balance: bigint,
|
|
98
|
+
feeRate: number,
|
|
99
|
+
totalFee: number
|
|
100
|
+
}> {
|
|
101
|
+
return this._getSpendableBalance([{address: this.address, addressType: this.addressType}], psbt, feeRate);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static generateRandomPrivateKey(network?: BTC_NETWORK): string {
|
|
105
|
+
return WIF(network).encode(randomPrivateKeyBytes());
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Enum representing different types of swaps
|
|
4
|
+
* @category Core
|
|
5
|
+
*/
|
|
6
|
+
export enum SwapType {
|
|
7
|
+
FROM_BTC=0,
|
|
8
|
+
FROM_BTCLN=1,
|
|
9
|
+
TO_BTC=2,
|
|
10
|
+
TO_BTCLN=3,
|
|
11
|
+
TRUSTED_FROM_BTC=4,
|
|
12
|
+
TRUSTED_FROM_BTCLN=5,
|
|
13
|
+
SPV_VAULT_FROM_BTC=6,
|
|
14
|
+
FROM_BTCLN_AUTO=7
|
|
15
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error or inconsistency in the intermediary's returned data, this will blacklist the intermediary
|
|
3
|
+
* @category Errors
|
|
4
|
+
*/
|
|
5
|
+
export class IntermediaryError extends Error {
|
|
6
|
+
|
|
7
|
+
recoverable: boolean;
|
|
8
|
+
originalStack?: string;
|
|
9
|
+
|
|
10
|
+
constructor(msg: string, originalError?: any, recoverable: boolean = false) {
|
|
11
|
+
if(originalError!=null) {
|
|
12
|
+
if(originalError.name!=null) msg += ": "+originalError.name;
|
|
13
|
+
if(originalError.message!=null) msg += ": "+originalError.message;
|
|
14
|
+
if(typeof(originalError)==="string") msg += ": "+originalError;
|
|
15
|
+
}
|
|
16
|
+
super(msg);
|
|
17
|
+
|
|
18
|
+
if(originalError?.stack!=null) this.originalStack = originalError.stack;
|
|
19
|
+
this.recoverable = recoverable;
|
|
20
|
+
// Set the prototype explicitly.
|
|
21
|
+
Object.setPrototypeOf(this, IntermediaryError.prototype);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error when the payment authorization returned by the intermediary is invalid
|
|
3
|
+
* @category Errors
|
|
4
|
+
*/
|
|
5
|
+
export class PaymentAuthError extends Error {
|
|
6
|
+
|
|
7
|
+
code?: number;
|
|
8
|
+
data: any;
|
|
9
|
+
|
|
10
|
+
constructor(msg: string, code?: number, data?: any) {
|
|
11
|
+
super(msg);
|
|
12
|
+
this.data = data;
|
|
13
|
+
this.code = code;
|
|
14
|
+
// Set the prototype explicitly.
|
|
15
|
+
Object.setPrototypeOf(this, PaymentAuthError.prototype);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getCode(): number | undefined {
|
|
19
|
+
return this.code;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getData(): any {
|
|
23
|
+
return this.data;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
}
|