@atomiqlabs/sdk 8.1.8 → 8.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/dist/bitcoin/coinselect2/utils.d.ts +6 -0
  2. package/dist/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
  3. package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
  4. package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
  5. package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
  6. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
  7. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
  8. package/dist/enums/FeeType.d.ts +8 -1
  9. package/dist/enums/FeeType.js +8 -1
  10. package/dist/enums/SwapAmountType.d.ts +7 -0
  11. package/dist/enums/SwapAmountType.js +7 -0
  12. package/dist/enums/SwapDirection.d.ts +7 -0
  13. package/dist/enums/SwapDirection.js +7 -0
  14. package/dist/enums/SwapType.d.ts +62 -1
  15. package/dist/enums/SwapType.js +62 -1
  16. package/dist/errors/IntermediaryError.d.ts +4 -0
  17. package/dist/errors/IntermediaryError.js +1 -0
  18. package/dist/errors/RequestError.d.ts +15 -1
  19. package/dist/errors/RequestError.js +8 -0
  20. package/dist/errors/UserError.d.ts +1 -0
  21. package/dist/errors/UserError.js +1 -0
  22. package/dist/index.d.ts +5 -5
  23. package/dist/index.js +7 -6
  24. package/dist/intermediaries/Intermediary.d.ts +61 -14
  25. package/dist/intermediaries/Intermediary.js +38 -11
  26. package/dist/intermediaries/IntermediaryDiscovery.d.ts +62 -29
  27. package/dist/intermediaries/IntermediaryDiscovery.js +39 -24
  28. package/dist/prices/RedundantSwapPrice.d.ts +26 -5
  29. package/dist/prices/RedundantSwapPrice.js +22 -2
  30. package/dist/prices/SingleSwapPrice.d.ts +10 -7
  31. package/dist/prices/SingleSwapPrice.js +11 -8
  32. package/dist/prices/SwapPriceWithChain.d.ts +56 -19
  33. package/dist/prices/SwapPriceWithChain.js +62 -25
  34. package/dist/prices/abstract/IPriceProvider.d.ts +4 -4
  35. package/dist/prices/abstract/IPriceProvider.js +1 -1
  36. package/dist/prices/abstract/ISwapPrice.d.ts +95 -46
  37. package/dist/prices/abstract/ISwapPrice.js +104 -56
  38. package/dist/prices/providers/BinancePriceProvider.d.ts +8 -1
  39. package/dist/prices/providers/BinancePriceProvider.js +8 -1
  40. package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +7 -1
  41. package/dist/prices/providers/CoinGeckoPriceProvider.js +7 -1
  42. package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +7 -1
  43. package/dist/prices/providers/CoinPaprikaPriceProvider.js +7 -1
  44. package/dist/prices/providers/CustomPriceProvider.d.ts +12 -1
  45. package/dist/prices/providers/CustomPriceProvider.js +12 -1
  46. package/dist/prices/providers/KrakenPriceProvider.d.ts +10 -1
  47. package/dist/prices/providers/KrakenPriceProvider.js +10 -1
  48. package/dist/prices/providers/OKXPriceProvider.d.ts +7 -1
  49. package/dist/prices/providers/OKXPriceProvider.js +7 -1
  50. package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
  51. package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
  52. package/dist/storage/IUnifiedStorage.d.ts +19 -7
  53. package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
  54. package/dist/storage/UnifiedSwapStorage.js +29 -1
  55. package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
  56. package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
  57. package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
  58. package/dist/storage-browser/LocalStorageManager.js +25 -1
  59. package/dist/swapper/Swapper.d.ts +380 -226
  60. package/dist/swapper/Swapper.js +383 -349
  61. package/dist/swapper/SwapperFactory.d.ts +66 -18
  62. package/dist/swapper/SwapperFactory.js +24 -3
  63. package/dist/swapper/SwapperUtils.d.ts +75 -28
  64. package/dist/swapper/SwapperUtils.js +107 -60
  65. package/dist/swapper/SwapperWithChain.d.ts +286 -91
  66. package/dist/swapper/SwapperWithChain.js +218 -64
  67. package/dist/swapper/SwapperWithSigner.d.ts +229 -80
  68. package/dist/swapper/SwapperWithSigner.js +190 -44
  69. package/dist/swaps/IAddressSwap.d.ts +12 -3
  70. package/dist/swaps/IAddressSwap.js +3 -2
  71. package/dist/swaps/IBTCWalletSwap.d.ts +26 -8
  72. package/dist/swaps/IBTCWalletSwap.js +3 -2
  73. package/dist/swaps/IClaimableSwap.d.ts +38 -6
  74. package/dist/swaps/IClaimableSwap.js +3 -2
  75. package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
  76. package/dist/swaps/IRefundableSwap.d.ts +31 -5
  77. package/dist/swaps/IRefundableSwap.js +3 -2
  78. package/dist/swaps/ISwap.d.ts +162 -24
  79. package/dist/swaps/ISwap.js +92 -35
  80. package/dist/swaps/ISwapWithGasDrop.d.ts +8 -2
  81. package/dist/swaps/ISwapWithGasDrop.js +2 -1
  82. package/dist/swaps/ISwapWrapper.d.ts +161 -52
  83. package/dist/swaps/ISwapWrapper.js +131 -73
  84. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +51 -6
  85. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
  86. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
  87. package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
  88. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
  89. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
  90. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
  91. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
  92. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +97 -28
  93. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +91 -27
  94. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
  95. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
  96. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +278 -60
  97. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +519 -241
  98. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +77 -26
  99. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +132 -50
  100. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +313 -52
  101. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +544 -194
  102. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
  103. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
  104. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +222 -55
  105. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +462 -244
  106. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
  107. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
  108. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +195 -58
  109. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +324 -191
  110. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
  111. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
  112. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +61 -20
  113. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +75 -32
  114. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
  115. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
  116. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +37 -14
  117. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +66 -20
  118. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
  119. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
  120. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +350 -88
  121. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +482 -215
  122. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
  123. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +247 -124
  124. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +148 -20
  125. package/dist/swaps/trusted/ln/LnForGasSwap.js +175 -45
  126. package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
  127. package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
  128. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +202 -49
  129. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +232 -80
  130. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
  131. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
  132. package/dist/types/AmountData.d.ts +2 -1
  133. package/dist/types/CustomPriceFunction.d.ts +8 -2
  134. package/dist/types/PriceInfoType.d.ts +4 -4
  135. package/dist/types/PriceInfoType.js +3 -3
  136. package/dist/types/SwapExecutionAction.d.ts +85 -4
  137. package/dist/types/SwapWithSigner.d.ts +5 -2
  138. package/dist/types/SwapWithSigner.js +5 -2
  139. package/dist/types/Token.d.ts +11 -5
  140. package/dist/types/Token.js +6 -3
  141. package/dist/types/TokenAmount.d.ts +3 -0
  142. package/dist/types/TokenAmount.js +2 -0
  143. package/dist/types/fees/Fee.d.ts +3 -2
  144. package/dist/types/fees/FeeBreakdown.d.ts +3 -2
  145. package/dist/types/fees/PercentagePPM.d.ts +4 -2
  146. package/dist/types/fees/PercentagePPM.js +2 -1
  147. package/dist/types/lnurl/LNURLPay.d.ts +20 -12
  148. package/dist/types/lnurl/LNURLPay.js +8 -4
  149. package/dist/types/lnurl/LNURLWithdraw.d.ts +17 -10
  150. package/dist/types/lnurl/LNURLWithdraw.js +8 -4
  151. package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -0
  152. package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
  153. package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
  154. package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +4 -2
  155. package/dist/utils/BitcoinUtils.d.ts +1 -0
  156. package/dist/utils/BitcoinUtils.js +5 -1
  157. package/dist/utils/SwapUtils.d.ts +58 -1
  158. package/dist/utils/SwapUtils.js +55 -1
  159. package/dist/utils/TokenUtils.d.ts +10 -2
  160. package/dist/utils/TokenUtils.js +12 -4
  161. package/package.json +3 -3
  162. package/src/bitcoin/coinselect2/utils.ts +6 -0
  163. package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
  164. package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
  165. package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
  166. package/src/enums/FeeType.ts +8 -1
  167. package/src/enums/SwapAmountType.ts +7 -0
  168. package/src/enums/SwapDirection.ts +7 -0
  169. package/src/enums/SwapType.ts +62 -2
  170. package/src/errors/IntermediaryError.ts +4 -0
  171. package/src/errors/RequestError.ts +15 -1
  172. package/src/errors/UserError.ts +1 -0
  173. package/src/index.ts +12 -5
  174. package/src/intermediaries/Intermediary.ts +61 -14
  175. package/src/intermediaries/IntermediaryDiscovery.ts +69 -34
  176. package/src/prices/RedundantSwapPrice.ts +26 -6
  177. package/src/prices/SingleSwapPrice.ts +11 -8
  178. package/src/prices/SwapPriceWithChain.ts +63 -26
  179. package/src/prices/abstract/IPriceProvider.ts +4 -4
  180. package/src/prices/abstract/ISwapPrice.ts +115 -66
  181. package/src/prices/providers/BinancePriceProvider.ts +8 -1
  182. package/src/prices/providers/CoinGeckoPriceProvider.ts +7 -1
  183. package/src/prices/providers/CoinPaprikaPriceProvider.ts +7 -1
  184. package/src/prices/providers/CustomPriceProvider.ts +12 -1
  185. package/src/prices/providers/KrakenPriceProvider.ts +10 -1
  186. package/src/prices/providers/OKXPriceProvider.ts +7 -1
  187. package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
  188. package/src/storage/IUnifiedStorage.ts +19 -7
  189. package/src/storage/UnifiedSwapStorage.ts +33 -3
  190. package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
  191. package/src/storage-browser/LocalStorageManager.ts +25 -1
  192. package/src/swapper/Swapper.ts +599 -390
  193. package/src/swapper/SwapperFactory.ts +73 -24
  194. package/src/swapper/SwapperUtils.ts +107 -60
  195. package/src/swapper/SwapperWithChain.ts +320 -81
  196. package/src/swapper/SwapperWithSigner.ts +263 -56
  197. package/src/swaps/IAddressSwap.ts +13 -3
  198. package/src/swaps/IBTCWalletSwap.ts +26 -10
  199. package/src/swaps/IClaimableSwap.ts +41 -6
  200. package/src/swaps/IClaimableSwapWrapper.ts +11 -2
  201. package/src/swaps/IRefundableSwap.ts +34 -5
  202. package/src/swaps/ISwap.ts +224 -85
  203. package/src/swaps/ISwapWithGasDrop.ts +8 -2
  204. package/src/swaps/ISwapWrapper.ts +216 -98
  205. package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +64 -18
  206. package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
  207. package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
  208. package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
  209. package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +123 -50
  210. package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
  211. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +562 -258
  212. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +156 -62
  213. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +592 -227
  214. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
  215. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +483 -245
  216. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
  217. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +350 -195
  218. package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
  219. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +87 -40
  220. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
  221. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +89 -34
  222. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
  223. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +556 -259
  224. package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +292 -148
  225. package/src/swaps/trusted/ln/LnForGasSwap.ts +186 -47
  226. package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
  227. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +262 -88
  228. package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
  229. package/src/types/AmountData.ts +2 -1
  230. package/src/types/CustomPriceFunction.ts +8 -2
  231. package/src/types/PriceInfoType.ts +4 -4
  232. package/src/types/SwapExecutionAction.ts +97 -5
  233. package/src/types/SwapWithSigner.ts +8 -4
  234. package/src/types/Token.ts +12 -5
  235. package/src/types/TokenAmount.ts +3 -0
  236. package/src/types/fees/Fee.ts +3 -2
  237. package/src/types/fees/FeeBreakdown.ts +3 -2
  238. package/src/types/fees/PercentagePPM.ts +4 -2
  239. package/src/types/lnurl/LNURLPay.ts +20 -12
  240. package/src/types/lnurl/LNURLWithdraw.ts +17 -10
  241. package/src/types/wallets/LightningInvoiceCreateService.ts +30 -0
  242. package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
  243. package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +4 -2
  244. package/src/utils/BitcoinUtils.ts +5 -0
  245. package/src/utils/SwapUtils.ts +63 -1
  246. package/src/utils/TokenUtils.ts +12 -4
  247. package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
  248. package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
  249. package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
  250. package/dist/bitcoin/LightningNetworkApi.js +0 -2
  251. package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
  252. package/dist/bitcoin/mempool/MempoolApi.js +0 -311
  253. package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
  254. package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
  255. package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
  256. package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
  257. package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
  258. package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
  259. package/dist/errors/PaymentAuthError.d.ts +0 -11
  260. package/dist/errors/PaymentAuthError.js +0 -23
  261. package/src/errors/PaymentAuthError.ts +0 -26
@@ -22,20 +22,33 @@ export function isIEscrowSwapInit<T extends SwapData>(obj: any): obj is IEscrowS
22
22
  isISwapInit(obj);
23
23
  }
24
24
 
25
+ /**
26
+ * Base class for escrow-based swaps (i.e. swaps utilizing PrTLC and HTLC primitives)
27
+ *
28
+ * @category Swaps/Abstract
29
+ */
25
30
  export abstract class IEscrowSwap<
26
31
  T extends ChainType = ChainType,
27
32
  D extends IEscrowSwapDefinition<T, IEscrowSwapWrapper<T, D>, IEscrowSwap<T, D, S>> = IEscrowSwapDefinition<T, IEscrowSwapWrapper<T, any>, IEscrowSwap<T, any, any>>,
28
33
  S extends number = number
29
34
  > extends ISwap<T, D, S> {
30
-
31
- data?: T["Data"];
32
-
35
+ /**
36
+ * @internal
37
+ */
38
+ _data?: T["Data"];
33
39
  /**
34
40
  * Transaction IDs for the swap on the smart chain side
41
+ * @internal
35
42
  */
36
- commitTxId?: string;
37
- refundTxId?: string;
38
- claimTxId?: string;
43
+ _commitTxId?: string;
44
+ /**
45
+ * @internal
46
+ */
47
+ _refundTxId?: string;
48
+ /**
49
+ * @internal
50
+ */
51
+ _claimTxId?: string;
39
52
 
40
53
  protected constructor(wrapper: D["Wrapper"], obj: any);
41
54
  protected constructor(wrapper: D["Wrapper"], swapInit: IEscrowSwapInit<T["Data"]>);
@@ -46,16 +59,21 @@ export abstract class IEscrowSwap<
46
59
  super(wrapper, swapInitOrObj);
47
60
 
48
61
  if(isIEscrowSwapInit(swapInitOrObj)) {
49
- this.data = swapInitOrObj.data;
62
+ this._data = swapInitOrObj.data;
50
63
  } else {
51
- if(swapInitOrObj.data!=null) this.data = new wrapper.swapDataDeserializer(swapInitOrObj.data);
64
+ if(swapInitOrObj.data!=null) this._data = new wrapper._swapDataDeserializer(swapInitOrObj.data);
52
65
 
53
- this.commitTxId = swapInitOrObj.commitTxId;
54
- this.claimTxId = swapInitOrObj.claimTxId;
55
- this.refundTxId = swapInitOrObj.refundTxId;
66
+ this._commitTxId = swapInitOrObj.commitTxId;
67
+ this._claimTxId = swapInitOrObj.claimTxId;
68
+ this._refundTxId = swapInitOrObj.refundTxId;
56
69
  }
57
70
  }
58
71
 
72
+ /**
73
+ * Returns the swap escrow data for this swap
74
+ *
75
+ * @internal
76
+ */
59
77
  protected abstract getSwapData(): T["Data"];
60
78
 
61
79
  //////////////////////////////
@@ -64,24 +82,32 @@ export abstract class IEscrowSwap<
64
82
  /**
65
83
  * Returns the identification hash of the swap, usually claim data hash, but can be overriden, e.g. for
66
84
  * lightning swaps the identifier hash is used instead of claim data hash
85
+ *
86
+ * @internal
67
87
  */
68
88
  protected getIdentifierHash(): Buffer {
69
89
  const claimHashBuffer = Buffer.from(this.getClaimHash(), "hex");
70
- if(this.randomNonce==null) return claimHashBuffer;
71
- return Buffer.concat([claimHashBuffer, Buffer.from(this.randomNonce, "hex")]);
90
+ if(this._randomNonce==null) return claimHashBuffer;
91
+ return Buffer.concat([claimHashBuffer, Buffer.from(this._randomNonce, "hex")]);
72
92
  }
73
93
 
74
94
  /**
75
95
  * Returns the identification hash of the swap, usually claim data hash, but can be overriden, e.g. for
76
96
  * lightning swaps the identifier hash is used instead of claim data hash
97
+ *
98
+ * @internal
77
99
  */
78
100
  protected getIdentifierHashString(): string {
79
101
  const identifierHash = this.getIdentifierHash();
80
102
  return identifierHash.toString("hex");
81
103
  }
82
104
 
105
+ /**
106
+ * @inheritDoc
107
+ * @internal
108
+ */
83
109
  _getEscrowHash(): string | null {
84
- return this.data?.getEscrowHash() ?? null;
110
+ return this._data?.getEscrowHash() ?? null;
85
111
  }
86
112
 
87
113
  /**
@@ -92,12 +118,15 @@ export abstract class IEscrowSwap<
92
118
  }
93
119
 
94
120
  /**
95
- * Returns the claim data hash - i.e. hash passed to the claim handler
121
+ * Returns the claim data hash specifying the claim path of the escrow - i.e. hash passed to the claim handler
96
122
  */
97
123
  getClaimHash(): string {
98
124
  return this.getSwapData().getClaimHash();
99
125
  }
100
126
 
127
+ /**
128
+ * @inheritDoc
129
+ */
101
130
  getId(): string {
102
131
  return this.getIdentifierHashString();
103
132
  }
@@ -111,17 +140,17 @@ export abstract class IEscrowSwap<
111
140
  *
112
141
  * @param intervalSeconds How often to check (in seconds), default to 5s
113
142
  * @param abortSignal
114
- * @protected
143
+ * @internal
115
144
  */
116
145
  protected async watchdogWaitTillCommited(intervalSeconds?: number, abortSignal?: AbortSignal): Promise<boolean> {
117
- if(this.data==null) throw new Error("Tried to await commitment but data is null, invalid state?");
146
+ if(this._data==null) throw new Error("Tried to await commitment but data is null, invalid state?");
118
147
 
119
148
  intervalSeconds ??= 5;
120
149
  let status: SwapCommitState = {type: SwapCommitStateType.NOT_COMMITED};
121
150
  while(status?.type===SwapCommitStateType.NOT_COMMITED) {
122
151
  await timeoutPromise(intervalSeconds*1000, abortSignal);
123
152
  try {
124
- status = await this.wrapper.contract.getCommitStatus(this._getInitiator(), this.data);
153
+ status = await this.wrapper._contract.getCommitStatus(this._getInitiator(), this._data);
125
154
  if(
126
155
  status?.type===SwapCommitStateType.NOT_COMMITED &&
127
156
  await this._verifyQuoteDefinitelyExpired()
@@ -139,19 +168,19 @@ export abstract class IEscrowSwap<
139
168
  *
140
169
  * @param intervalSeconds How often to check (in seconds), default to 5s
141
170
  * @param abortSignal
142
- * @protected
171
+ * @internal
143
172
  */
144
173
  protected async watchdogWaitTillResult(intervalSeconds?: number, abortSignal?: AbortSignal): Promise<
145
174
  SwapPaidState | SwapExpiredState | SwapNotCommitedState
146
175
  > {
147
- if(this.data==null) throw new Error("Tried to await result but data is null, invalid state?");
176
+ if(this._data==null) throw new Error("Tried to await result but data is null, invalid state?");
148
177
 
149
178
  intervalSeconds ??= 5;
150
179
  let status: SwapCommitState = {type: SwapCommitStateType.COMMITED};
151
180
  while(status?.type===SwapCommitStateType.COMMITED || status?.type===SwapCommitStateType.REFUNDABLE) {
152
181
  await timeoutPromise(intervalSeconds*1000, abortSignal);
153
182
  try {
154
- status = await this.wrapper.contract.getCommitStatus(this._getInitiator(), this.data);
183
+ status = await this.wrapper._contract.getCommitStatus(this._getInitiator(), this._data);
155
184
  } catch (e) {
156
185
  this.logger.error("watchdogWaitTillResult(): Error when fetching commit status: ", e);
157
186
  }
@@ -162,35 +191,52 @@ export abstract class IEscrowSwap<
162
191
 
163
192
 
164
193
  //////////////////////////////
165
- //// Quote verification
194
+ //// Helpers for batched swap checks
166
195
 
167
196
  /**
168
- * Checks if the swap's quote is expired for good (i.e. the swap strictly cannot be committed on-chain anymore)
197
+ * Whether on-chain state should be fetched for this swap
198
+ * @internal
169
199
  */
170
- abstract _verifyQuoteDefinitelyExpired(): Promise<boolean>;
200
+ abstract _shouldFetchOnchainState(): boolean;
171
201
 
172
202
  /**
173
- * Checks if the swap's quote is still valid
203
+ * Whether expiration status of the swap quote should be checked for this swap
204
+ * @internal
174
205
  */
175
- abstract verifyQuoteValid(): Promise<boolean>;
176
-
177
-
178
- //////////////////////////////
179
- //// Helpers for batched swap checks
180
-
181
- abstract _shouldFetchCommitStatus(): boolean;
182
-
183
206
  abstract _shouldFetchExpiryStatus(): boolean;
184
207
 
208
+ /**
209
+ * @inheritDoc
210
+ *
211
+ * @param save Whether to save the new swap state or not
212
+ * @param quoteDefinitelyExpired Optionally pass whether the quote is definitely expired from a batch pre-fetch,
213
+ * fetched on-demand if not provided
214
+ * @param commitStatus Optionally pass the quote on-chain state from a batch pre-fetch, fetched on-demand if
215
+ * not provided
216
+ *
217
+ * @internal
218
+ */
185
219
  abstract _sync(save?: boolean, quoteDefinitelyExpired?: boolean, commitStatus?: SwapCommitState): Promise<boolean>;
186
220
 
221
+ /**
222
+ * Forcibly overrides current swap state from fetched on-chain swap state
223
+ *
224
+ * @param commitStatus Swap state fetched from the smart chain
225
+ *
226
+ * @internal
227
+ */
228
+ abstract _forciblySetOnchainState(commitStatus: SwapCommitState): Promise<boolean>;
229
+
230
+ /**
231
+ * @inheritDoc
232
+ */
187
233
  serialize(): any {
188
234
  return {
189
235
  ...super.serialize(),
190
- data: this.data!=null ? this.data.serialize() : null,
191
- commitTxId: this.commitTxId,
192
- claimTxId: this.claimTxId,
193
- refundTxId: this.refundTxId
236
+ data: this._data!=null ? this._data.serialize() : null,
237
+ commitTxId: this._commitTxId,
238
+ claimTxId: this._claimTxId,
239
+ refundTxId: this._refundTxId
194
240
  }
195
241
  };
196
242
 
@@ -5,7 +5,6 @@ import {
5
5
  InitializeEvent,
6
6
  RefundEvent,
7
7
  SignatureData,
8
- SignatureVerificationError,
9
8
  SwapCommitState,
10
9
  SwapEvent
11
10
  } from "@atomiqlabs/base";
@@ -20,6 +19,11 @@ import {Intermediary} from "../../intermediaries/Intermediary";
20
19
 
21
20
  export type IEscrowSwapDefinition<T extends ChainType, W extends IEscrowSwapWrapper<T, any>, S extends IEscrowSwap<T>> = SwapTypeDefinition<T, W, S>;
22
21
 
22
+ /**
23
+ * Base class for wrappers of escrow-based swaps (i.e. swaps utilizing PrTLC and HTLC primitives)
24
+ *
25
+ * @category Swaps/Abstract
26
+ */
23
27
  export abstract class IEscrowSwapWrapper<
24
28
  T extends ChainType,
25
29
  D extends IEscrowSwapDefinition<T, IEscrowSwapWrapper<T, D>, IEscrowSwap<T, D>>,
@@ -27,8 +31,14 @@ export abstract class IEscrowSwapWrapper<
27
31
  > extends ISwapWrapper<T, D, O> {
28
32
  readonly abstract TYPE: SwapType;
29
33
 
30
- readonly contract: T["Contract"];
31
- readonly swapDataDeserializer: new (data: any) => T["Data"];
34
+ /**
35
+ * @internal
36
+ */
37
+ readonly _contract: T["Contract"];
38
+ /**
39
+ * @internal
40
+ */
41
+ readonly _swapDataDeserializer: new (data: any) => T["Data"];
32
42
 
33
43
  constructor(
34
44
  chainIdentifier: string,
@@ -43,22 +53,23 @@ export abstract class IEscrowSwapWrapper<
43
53
  events?: EventEmitter<{swapState: [ISwap]}>
44
54
  ) {
45
55
  super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, options, events);
46
- this.swapDataDeserializer = swapDataDeserializer;
47
- this.contract = contract;
56
+ this._swapDataDeserializer = swapDataDeserializer;
57
+ this._contract = contract;
48
58
  }
49
59
 
50
60
  /**
51
61
  * Pre-fetches signature verification data from the server's pre-sent promise, doesn't throw, instead returns null
52
62
  *
53
63
  * @param signDataPrefetch Promise that resolves when we receive "signDataPrefetch" from the LP in streaming mode
54
- * @protected
55
64
  * @returns Pre-fetched signature verification data or null if failed
65
+ *
66
+ * @internal
56
67
  */
57
68
  protected preFetchSignData(signDataPrefetch: Promise<any | null>): Promise<T["PreFetchVerification"] | undefined> {
58
- if(this.contract.preFetchForInitSignatureVerification==null) return Promise.resolve(undefined);
69
+ if(this._contract.preFetchForInitSignatureVerification==null) return Promise.resolve(undefined);
59
70
  return signDataPrefetch.then(obj => {
60
71
  if(obj==null) return undefined;
61
- return this.contract.preFetchForInitSignatureVerification!(obj);
72
+ return this._contract.preFetchForInitSignatureVerification!(obj);
62
73
  }).catch(e => {
63
74
  this.logger.error("preFetchSignData(): Error: ", e);
64
75
  });
@@ -73,9 +84,10 @@ export abstract class IEscrowSwapWrapper<
73
84
  * @param feeRatePromise Pre-fetched fee rate promise
74
85
  * @param preFetchSignatureVerificationData Pre-fetched signature verification data
75
86
  * @param abortSignal
76
- * @protected
77
87
  * @returns Swap initialization signature expiry
78
88
  * @throws {SignatureVerificationError} when swap init signature is invalid
89
+ *
90
+ * @internal
79
91
  */
80
92
  protected async verifyReturnedSignature(
81
93
  initiator: string,
@@ -86,16 +98,17 @@ export abstract class IEscrowSwapWrapper<
86
98
  abortSignal?: AbortSignal
87
99
  ): Promise<number> {
88
100
  const [feeRate, preFetchedSignatureData] = await Promise.all([feeRatePromise, preFetchSignatureVerificationData]);
89
- await this.contract.isValidInitAuthorization(initiator, data, signature, feeRate, preFetchedSignatureData);
90
- return await this.contract.getInitAuthorizationExpiry(data, signature, preFetchedSignatureData);
101
+ await this._contract.isValidInitAuthorization(initiator, data, signature, feeRate, preFetchedSignatureData);
102
+ return await this._contract.getInitAuthorizationExpiry(data, signature, preFetchedSignatureData);
91
103
  }
92
104
 
93
105
  /**
94
106
  * Processes InitializeEvent for a given swap
95
107
  * @param swap
96
108
  * @param event
97
- * @protected
98
109
  * @returns Whether the swap was updated/changed
110
+ *
111
+ * @internal
99
112
  */
100
113
  protected abstract processEventInitialize(swap: D["Swap"], event: InitializeEvent<T["Data"]>): Promise<boolean>;
101
114
 
@@ -103,8 +116,9 @@ export abstract class IEscrowSwapWrapper<
103
116
  * Processes ClaimEvent for a given swap
104
117
  * @param swap
105
118
  * @param event
106
- * @protected
107
119
  * @returns Whether the swap was updated/changed
120
+ *
121
+ * @internal
108
122
  */
109
123
  protected abstract processEventClaim(swap: D["Swap"], event: ClaimEvent<T["Data"]>): Promise<boolean>;
110
124
 
@@ -112,16 +126,18 @@ export abstract class IEscrowSwapWrapper<
112
126
  * Processes RefundEvent for a given swap
113
127
  * @param swap
114
128
  * @param event
115
- * @protected
116
129
  * @returns Whether the swap was updated/changed
130
+ *
131
+ * @internal
117
132
  */
118
133
  protected abstract processEventRefund(swap: D["Swap"], event: RefundEvent<T["Data"]>): Promise<boolean>;
119
134
 
120
135
  /**
121
136
  * Processes a single SC on-chain event
122
- * @private
123
137
  * @param event
124
138
  * @param swap
139
+ *
140
+ * @internal
125
141
  */
126
142
  protected async processEvent(event: SwapEvent<T["Data"]>, swap: D["Swap"]): Promise<void> {
127
143
  if(swap==null) return;
@@ -129,22 +145,22 @@ export abstract class IEscrowSwapWrapper<
129
145
  let swapChanged: boolean = false;
130
146
  if(event instanceof InitializeEvent) {
131
147
  swapChanged = await this.processEventInitialize(swap, event);
132
- if(event.meta?.txId!=null && swap.commitTxId!==event.meta.txId) {
133
- swap.commitTxId = event.meta.txId;
148
+ if(event.meta?.txId!=null && swap._commitTxId!==event.meta.txId) {
149
+ swap._commitTxId = event.meta.txId;
134
150
  swapChanged ||= true;
135
151
  }
136
152
  }
137
153
  if(event instanceof ClaimEvent) {
138
154
  swapChanged = await this.processEventClaim(swap, event);
139
- if(event.meta?.txId!=null && swap.claimTxId!==event.meta.txId) {
140
- swap.claimTxId = event.meta.txId;
155
+ if(event.meta?.txId!=null && swap._claimTxId!==event.meta.txId) {
156
+ swap._claimTxId = event.meta.txId;
141
157
  swapChanged ||= true;
142
158
  }
143
159
  }
144
160
  if(event instanceof RefundEvent) {
145
161
  swapChanged = await this.processEventRefund(swap, event);
146
- if(event.meta?.txId!=null && swap.refundTxId!==event.meta.txId) {
147
- swap.refundTxId = event.meta.txId;
162
+ if(event.meta?.txId!=null && swap._refundTxId!==event.meta.txId) {
163
+ swap._refundTxId = event.meta.txId;
148
164
  swapChanged ||= true;
149
165
  }
150
166
  }
@@ -156,26 +172,30 @@ export abstract class IEscrowSwapWrapper<
156
172
  }
157
173
  }
158
174
 
175
+ /**
176
+ * @inheritDoc
177
+ * @internal
178
+ */
159
179
  protected async _checkPastSwaps(pastSwaps: D["Swap"][]): Promise<{ changedSwaps: D["Swap"][]; removeSwaps: D["Swap"][] }> {
160
180
  const changedSwaps: D["Swap"][] = [];
161
181
  const removeSwaps: D["Swap"][] = [];
162
182
 
163
183
  const swapExpiredStatus: {[id: string]: boolean} = {};
164
184
 
165
- const checkStatusSwaps: (D["Swap"] & {data: T["Data"]})[] = [];
185
+ const checkStatusSwaps: (D["Swap"] & {_data: T["Data"]})[] = [];
166
186
 
167
187
  for(let pastSwap of pastSwaps) {
168
188
  if(pastSwap._shouldFetchExpiryStatus()) {
169
189
  //Check expiry
170
190
  swapExpiredStatus[pastSwap.getId()] = await pastSwap._verifyQuoteDefinitelyExpired();
171
191
  }
172
- if(pastSwap._shouldFetchCommitStatus()) {
192
+ if(pastSwap._shouldFetchOnchainState()) {
173
193
  //Add to swaps for which status should be checked
174
- if(pastSwap.data!=null) checkStatusSwaps.push(pastSwap as (D["Swap"] & {data: T["Data"]}));
194
+ if(pastSwap._data!=null) checkStatusSwaps.push(pastSwap as (D["Swap"] & {_data: T["Data"]}));
175
195
  }
176
196
  }
177
197
 
178
- const swapStatuses = await this.contract.getCommitStatuses(checkStatusSwaps.map(val => ({signer: val._getInitiator(), swapData: val.data})));
198
+ const swapStatuses = await this._contract.getCommitStatuses(checkStatusSwaps.map(val => ({signer: val._getInitiator(), swapData: val._data})));
179
199
 
180
200
  for(let pastSwap of checkStatusSwaps) {
181
201
  const escrowHash = pastSwap.getEscrowHash();
@@ -199,12 +219,23 @@ export abstract class IEscrowSwapWrapper<
199
219
  };
200
220
  }
201
221
 
202
- recoverFromSwapDataAndState(
203
- init: {data: T["Data"], getInitTxId: () => Promise<string>, getTxBlock: () => Promise<{blockTime: number, blockHeight: number}>},
222
+ /**
223
+ * Recovers a swap from smart chain on-chain data, please note that not all values for the recovered
224
+ * swaps might be populated, as some data is purely off-chain and can never be recovered purely
225
+ * from on-chain data.
226
+ *
227
+ * @param init Swap escrow initialization transaction and swap data
228
+ * @param state Current on-chain state of the swap
229
+ * @param lp Intermediary (LP) used as a counterparty for the swap
230
+ */
231
+ public abstract recoverFromSwapDataAndState(
232
+ init: {
233
+ data: T["Data"],
234
+ getInitTxId: () => Promise<string>,
235
+ getTxBlock: () => Promise<{blockTime: number, blockHeight: number}>
236
+ },
204
237
  state: SwapCommitState,
205
238
  lp?: Intermediary
206
- ): Promise<D["Swap"] | null> {
207
- return Promise.resolve(null);
208
- }
239
+ ): Promise<D["Swap"] | null>;
209
240
 
210
241
  }
@@ -18,12 +18,19 @@ import {LNURLWithdrawParamsWithUrl} from "../../../types/lnurl/LNURLWithdraw";
18
18
 
19
19
  export type IFromBTCLNDefinition<T extends ChainType, W extends IFromBTCLNWrapper<T, any>, S extends IEscrowSwap<T>> = IFromBTCDefinition<T, W, S>;
20
20
 
21
+ /**
22
+ * Base class for wrappers of escrow-based Lightning -> Smart chain swaps
23
+ *
24
+ * @category Swaps/Abstract
25
+ */
21
26
  export abstract class IFromBTCLNWrapper<
22
27
  T extends ChainType,
23
28
  D extends IFromBTCLNDefinition<T, IFromBTCLNWrapper<T, D>, IEscrowSwap<T, D>>,
24
29
  O extends ISwapWrapperOptions = ISwapWrapperOptions
25
30
  > extends IFromBTCWrapper<T, D, O> {
26
-
31
+ /**
32
+ * @internal
33
+ */
27
34
  protected readonly lnApi: LightningNetworkApi;
28
35
 
29
36
  /**
@@ -55,20 +62,13 @@ export abstract class IFromBTCLNWrapper<
55
62
  super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, options, events);
56
63
  this.lnApi = lnApi;
57
64
  }
58
- /**
59
- * Returns the swap expiry, leaving enough time for the user to claim the HTLC
60
- *
61
- * @param data Parsed swap data
62
- */
63
- getHtlcTimeout(data: SwapData): bigint {
64
- return data.getExpiry() - 600n;
65
- }
66
65
 
67
66
  /**
68
- * Generates a new 32-byte secret to be used as pre-image for lightning network invoice & HTLC swap\
67
+ * Generates a new 32-byte secret to be used as pre-image for lightning network invoice & HTLC swap
69
68
  *
70
- * @private
71
69
  * @returns Hash pre-image & payment hash
70
+ *
71
+ * @internal
72
72
  */
73
73
  protected getSecretAndHash(): {secret: Buffer, paymentHash: Buffer} {
74
74
  const secret = randomBytes(32);
@@ -79,10 +79,12 @@ export abstract class IFromBTCLNWrapper<
79
79
  /**
80
80
  * Pre-fetches intermediary's LN node capacity, doesn't throw, instead returns null
81
81
  *
82
- * @param pubkeyPromise Promise that resolves when we receive "lnPublicKey" param from the intermediary thorugh
82
+ * @param pubkeyPromise Promise that resolves when we receive "lnPublicKey" param from the intermediary through
83
83
  * streaming
84
- * @private
84
+ *
85
85
  * @returns LN Node liquidity
86
+ *
87
+ * @internal
86
88
  */
87
89
  protected preFetchLnCapacity(pubkeyPromise: Promise<string | null>): Promise<LNNodeLiquidity | null> {
88
90
  return pubkeyPromise.then(pubkey => {
@@ -99,13 +101,14 @@ export abstract class IFromBTCLNWrapper<
99
101
  *
100
102
  * @param lp Intermediary
101
103
  * @param decodedPr Decoded bolt11 lightning network invoice
102
- * @param amountIn Amount to be paid for the swap in sats
103
104
  * @param lnCapacityPrefetchPromise Pre-fetch for LN node capacity, preFetchLnCapacity()
104
- * @param abortSignal
105
- * @private
105
+ * @param abortSignal Abort signal
106
+ *
106
107
  * @throws {IntermediaryError} if the lightning network node doesn't have enough inbound liquidity
107
108
  * @throws {Error} if the lightning network node's inbound liquidity might be enough, but the swap would
108
109
  * deplete more than half of the liquidity
110
+ *
111
+ * @internal
109
112
  */
110
113
  protected async verifyLnNodeCapacity(
111
114
  lp: Intermediary,
@@ -119,6 +122,8 @@ export abstract class IFromBTCLNWrapper<
119
122
  const _result = await lnCapacityPrefetchPromise ?? await this.lnApi.getLNNodeLiquidity(decodedPr.payeeNodeKey);
120
123
  if(_result===null) throw new IntermediaryError("LP's lightning node not found in the lightning network graph!");
121
124
 
125
+ if(abortSignal!=null) abortSignal.throwIfAborted();
126
+
122
127
  lp.lnData = _result;
123
128
 
124
129
  if(decodedPr.payeeNodeKey!==_result.publicKey) throw new IntermediaryError("Invalid pr returned - payee pubkey");
@@ -133,17 +138,30 @@ export abstract class IFromBTCLNWrapper<
133
138
  * Parses and fetches lnurl withdraw params from the specified lnurl
134
139
  *
135
140
  * @param lnurl LNURL to be parsed and fetched
136
- * @param abortSignal
137
- * @private
141
+ * @param abortSignal Abort signal
142
+ *
138
143
  * @throws {UserError} if the LNURL is invalid or if it's not a LNURL-withdraw
144
+ *
145
+ * @internal
139
146
  */
140
147
  protected async getLNURLWithdraw(lnurl: string | LNURLWithdrawParamsWithUrl, abortSignal: AbortSignal): Promise<LNURLWithdrawParamsWithUrl> {
141
148
  if(typeof(lnurl)!=="string") return lnurl;
142
149
 
143
- const res = await LNURL.getLNURL(lnurl, true, this.options.getRequestTimeout, abortSignal);
150
+ const res = await LNURL.getLNURL(lnurl, true, this._options.getRequestTimeout, abortSignal);
144
151
  if(res==null) throw new UserError("Invalid LNURL");
145
152
  if(res.tag!=="withdrawRequest") throw new UserError("Not a LNURL-withdrawal");
146
153
  return res;
147
154
  }
148
155
 
156
+ /**
157
+ * Returns the swap expiry, leaving enough time for the user to claim the HTLC
158
+ *
159
+ * @param data Parsed swap data
160
+ *
161
+ * @internal
162
+ */
163
+ _getHtlcTimeout(data: SwapData): bigint {
164
+ return data.getExpiry() - 600n;
165
+ }
166
+
149
167
  }