@atomiqlabs/sdk 8.3.6 → 8.4.4

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 (58) hide show
  1. package/dist/bitcoin/wallet/BitcoinWallet.d.ts +3 -2
  2. package/dist/bitcoin/wallet/BitcoinWallet.js +15 -1
  3. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +25 -3
  4. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +51 -2
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/intermediaries/apis/IntermediaryAPI.d.ts +2 -0
  8. package/dist/intermediaries/apis/IntermediaryAPI.js +2 -0
  9. package/dist/swapper/Swapper.d.ts +2 -1
  10. package/dist/swapper/Swapper.js +4 -3
  11. package/dist/swaps/ISwap.d.ts +34 -0
  12. package/dist/swaps/ISwap.js +10 -0
  13. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +12 -0
  14. package/dist/swaps/escrow_swaps/IEscrowSwap.js +18 -0
  15. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +54 -4
  16. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +72 -8
  17. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +4 -1
  18. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +23 -4
  19. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +89 -3
  20. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +118 -5
  21. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +2 -0
  22. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +30 -4
  23. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +48 -3
  24. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +69 -4
  25. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +40 -2
  26. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +72 -2
  27. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +51 -1
  28. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +71 -3
  29. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +28 -2
  30. package/dist/swaps/trusted/ln/LnForGasSwap.js +39 -1
  31. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +28 -3
  32. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +44 -1
  33. package/dist/types/SwapStateInfo.d.ts +5 -0
  34. package/dist/types/SwapStateInfo.js +2 -0
  35. package/dist/types/lnurl/LNURLPay.d.ts +0 -1
  36. package/dist/types/lnurl/LNURLPay.js +0 -1
  37. package/dist/types/lnurl/LNURLWithdraw.d.ts +0 -1
  38. package/dist/types/lnurl/LNURLWithdraw.js +0 -1
  39. package/package.json +3 -1
  40. package/src/bitcoin/wallet/BitcoinWallet.ts +21 -4
  41. package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +60 -5
  42. package/src/index.ts +1 -0
  43. package/src/intermediaries/apis/IntermediaryAPI.ts +5 -1
  44. package/src/swapper/Swapper.ts +5 -2
  45. package/src/swaps/ISwap.ts +45 -0
  46. package/src/swaps/escrow_swaps/IEscrowSwap.ts +20 -0
  47. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +86 -11
  48. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +28 -3
  49. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +137 -9
  50. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +35 -4
  51. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +83 -6
  52. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +77 -4
  53. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +90 -4
  54. package/src/swaps/trusted/ln/LnForGasSwap.ts +51 -3
  55. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +58 -3
  56. package/src/types/SwapStateInfo.ts +6 -0
  57. package/src/types/lnurl/LNURLPay.ts +0 -1
  58. package/src/types/lnurl/LNURLWithdraw.ts +0 -1
@@ -66,6 +66,16 @@ var FromBTCLNSwapState;
66
66
  */
67
67
  FromBTCLNSwapState[FromBTCLNSwapState["CLAIM_CLAIMED"] = 3] = "CLAIM_CLAIMED";
68
68
  })(FromBTCLNSwapState = exports.FromBTCLNSwapState || (exports.FromBTCLNSwapState = {}));
69
+ const FromBTCLNSwapStateDescription = {
70
+ [FromBTCLNSwapState.FAILED]: "Swap has failed as the user didn't settle the HTLC on the destination before expiration",
71
+ [FromBTCLNSwapState.QUOTE_EXPIRED]: "Swap has expired for good and there is no way how it can be executed anymore",
72
+ [FromBTCLNSwapState.QUOTE_SOFT_EXPIRED]: "Swap is expired, though there is still a chance that it will be processed",
73
+ [FromBTCLNSwapState.EXPIRED]: "Swap HTLC on the destination chain has expired, it is not safe anymore to settle (claim) the swap on the destination smart chain.",
74
+ [FromBTCLNSwapState.PR_CREATED]: "Swap quote was created, pay the bolt11 lightning network invoice to initiate the swap, then use the wait till the lightning network payment is received by the intermediary (LP)",
75
+ [FromBTCLNSwapState.PR_PAID]: "Lightning network payment has been received by the intermediary (LP), the user can now settle the swap on the destination smart chain side.",
76
+ [FromBTCLNSwapState.CLAIM_COMMITED]: "Swap escrow HTLC has been created on the destination chain. Continue by claiming it.",
77
+ [FromBTCLNSwapState.CLAIM_CLAIMED]: "Swap successfully settled and funds received on the destination chain"
78
+ };
69
79
  function isFromBTCLNSwapInit(obj) {
70
80
  return (obj.pr == null || typeof obj.pr === "string") &&
71
81
  (obj.secret == null || typeof obj.secret === "string") &&
@@ -97,6 +107,14 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
97
107
  initOrObject.url += "/frombtcln";
98
108
  super(wrapper, initOrObject);
99
109
  this.TYPE = SwapType_1.SwapType.FROM_BTCLN;
110
+ /**
111
+ * @internal
112
+ */
113
+ this.swapStateName = (state) => FromBTCLNSwapState[state];
114
+ /**
115
+ * @internal
116
+ */
117
+ this.swapStateDescription = FromBTCLNSwapStateDescription;
100
118
  /**
101
119
  * @internal
102
120
  */
@@ -303,6 +321,15 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
303
321
  isFailed() {
304
322
  return this._state === FromBTCLNSwapState.FAILED || this._state === FromBTCLNSwapState.EXPIRED;
305
323
  }
324
+ /**
325
+ * @inheritDoc
326
+ */
327
+ isInProgress() {
328
+ return (this._state === FromBTCLNSwapState.PR_CREATED && this.initiated) ||
329
+ (this._state === FromBTCLNSwapState.QUOTE_SOFT_EXPIRED && this.initiated) ||
330
+ this._state === FromBTCLNSwapState.PR_PAID ||
331
+ this._state === FromBTCLNSwapState.CLAIM_COMMITED;
332
+ }
306
333
  /**
307
334
  * @inheritDoc
308
335
  */
@@ -384,6 +411,24 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
384
411
  const claimHash = this.wrapper._contract.getHashForHtlc(paymentHash).toString("hex");
385
412
  return this.getSwapData().getClaimHash() === claimHash;
386
413
  }
414
+ /**
415
+ * Sets the secret preimage for the swap, in case it is not known already
416
+ *
417
+ * @param secret Secret preimage that matches the expected payment hash
418
+ *
419
+ * @throws {Error} If an invalid secret preimage is provided
420
+ */
421
+ setSecretPreimage(secret) {
422
+ if (!this.isValidSecretPreimage(secret))
423
+ throw new Error("Invalid secret preimage provided, hash doesn't match!");
424
+ this.secret = secret;
425
+ }
426
+ /**
427
+ * Returns whether the secret preimage for this swap is known
428
+ */
429
+ hasSecretPreimage() {
430
+ return this.secret != null;
431
+ }
387
432
  //////////////////////////////
388
433
  //// Execution
389
434
  /**
@@ -396,10 +441,10 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
396
441
  * link, wallet is not required and the LN invoice can be paid externally as well (just pass null or undefined here)
397
442
  * @param callbacks Callbacks to track the progress of the swap
398
443
  * @param options Optional options for the swap like feeRate, AbortSignal, and timeouts/intervals
399
- * @param secret A swap secret to use for the claim transaction, generally only needed if the swap
444
+ * @param options.secret A swap secret to use for the claim transaction, generally only needed if the swap
400
445
  * was recovered from on-chain data, or the pre-image was generated outside the SDK
401
446
  */
402
- async execute(dstSigner, walletOrLnurlWithdraw, callbacks, options, secret) {
447
+ async execute(dstSigner, walletOrLnurlWithdraw, callbacks, options) {
403
448
  if (this._state === FromBTCLNSwapState.FAILED)
404
449
  throw new Error("Swap failed!");
405
450
  if (this._state === FromBTCLNSwapState.EXPIRED)
@@ -431,7 +476,7 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
431
476
  }
432
477
  if (this._state === FromBTCLNSwapState.PR_PAID || this._state === FromBTCLNSwapState.CLAIM_COMMITED) {
433
478
  if (this.canCommitAndClaimInOneShot()) {
434
- await this.commitAndClaim(dstSigner, options?.abortSignal, undefined, callbacks?.onDestinationCommitSent, callbacks?.onDestinationClaimSent, secret);
479
+ await this.commitAndClaim(dstSigner, options?.abortSignal, undefined, callbacks?.onDestinationCommitSent, callbacks?.onDestinationClaimSent, options?.secret);
435
480
  }
436
481
  else {
437
482
  if (this._state === FromBTCLNSwapState.PR_PAID) {
@@ -440,7 +485,7 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
440
485
  await (0, TimeoutUtils_1.timeoutPromise)(options.delayBetweenCommitAndClaimSeconds * 1000, options?.abortSignal);
441
486
  }
442
487
  if (this._state === FromBTCLNSwapState.CLAIM_COMMITED) {
443
- await this.claim(dstSigner, options?.abortSignal, callbacks?.onDestinationClaimSent, secret);
488
+ await this.claim(dstSigner, options?.abortSignal, callbacks?.onDestinationClaimSent, options?.secret);
444
489
  }
445
490
  }
446
491
  }
@@ -449,6 +494,7 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
449
494
  if (callbacks?.onSwapSettled != null)
450
495
  callbacks.onSwapSettled(this.getOutputTxId());
451
496
  }
497
+ return true;
452
498
  }
453
499
  /**
454
500
  * @inheritDoc
@@ -457,10 +503,10 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
457
503
  * @param options.skipChecks Skip checks like making sure init signature is still valid and swap
458
504
  * wasn't commited yet (this is handled on swap creation, if you commit right after quoting, you
459
505
  * can use `skipChecks=true`)
460
- * @param secret A swap secret to use for the claim transaction, generally only needed if the swap
506
+ * @param options.secret A swap secret to use for the claim transaction, generally only needed if the swap
461
507
  * was recovered from on-chain data, or the pre-image was generated outside the SDK
462
508
  */
463
- async txsExecute(options, secret) {
509
+ async txsExecute(options) {
464
510
  if (this._state === FromBTCLNSwapState.PR_CREATED) {
465
511
  if (!await this._verifyQuoteValid())
466
512
  throw new Error("Quote already expired or close to expiry!");
@@ -483,7 +529,7 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
483
529
  if (!await this._verifyQuoteValid())
484
530
  throw new Error("Quote already expired or close to expiry!");
485
531
  const txsCommit = await this.txsCommit(options?.skipChecks);
486
- const txsClaim = await this._txsClaim(undefined, secret);
532
+ const txsClaim = await this._txsClaim(undefined, options?.secret);
487
533
  return [
488
534
  {
489
535
  name: "Commit",
@@ -500,7 +546,7 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
500
546
  ];
501
547
  }
502
548
  if (this._state === FromBTCLNSwapState.CLAIM_COMMITED) {
503
- const txsClaim = await this.txsClaim(undefined, secret);
549
+ const txsClaim = await this.txsClaim(undefined, options?.secret);
504
550
  return [
505
551
  {
506
552
  name: "Claim",
@@ -512,6 +558,24 @@ class FromBTCLNSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
512
558
  }
513
559
  throw new Error("Invalid swap state to obtain execution txns, required PR_CREATED, PR_PAID or CLAIM_COMMITED");
514
560
  }
561
+ /**
562
+ * @inheritDoc
563
+ *
564
+ * @param options
565
+ * @param options.skipChecks Skip checks like making sure init signature is still valid and swap
566
+ * wasn't commited yet (this is handled on swap creation, if you commit right after quoting, you
567
+ * can use `skipChecks=true`)
568
+ * @param options.secret A swap secret to use for the claim transaction, generally only needed if the swap
569
+ * was recovered from on-chain data, or the pre-image was generated outside the SDK
570
+ */
571
+ async getCurrentActions(options) {
572
+ try {
573
+ return await this.txsExecute(options);
574
+ }
575
+ catch (e) {
576
+ return [];
577
+ }
578
+ }
515
579
  //////////////////////////////
516
580
  //// Payment
517
581
  /**
@@ -18,6 +18,8 @@ import { AmountData } from "../../../../types/AmountData";
18
18
  import { LNURLWithdrawParamsWithUrl } from "../../../../types/lnurl/LNURLWithdraw";
19
19
  import { AllOptional } from "../../../../utils/TypeUtils";
20
20
  export type FromBTCLNOptions = {
21
+ paymentHash?: Buffer;
22
+ description?: string;
21
23
  descriptionHash?: Buffer;
22
24
  unsafeSkipLnNodeCheck?: boolean;
23
25
  };
@@ -130,10 +132,11 @@ export declare class FromBTCLNWrapper<T extends ChainType> extends IFromBTCLNWra
130
132
  * @param lnurl LNURL-withdraw link to pull the funds from
131
133
  * @param amountData Amount, token and exact input/output data for to swap
132
134
  * @param lps An array of intermediaries (LPs) to get the quotes from
135
+ * @param options Optional additional quote options
133
136
  * @param additionalParams Optional additional parameters sent to the LP when creating the swap
134
137
  * @param abortSignal Abort signal
135
138
  */
136
- createViaLNURL(recipient: string, lnurl: string | LNURLWithdrawParamsWithUrl, amountData: AmountData, lps: Intermediary[], additionalParams?: Record<string, any>, abortSignal?: AbortSignal): Promise<{
139
+ createViaLNURL(recipient: string, lnurl: string | LNURLWithdrawParamsWithUrl, amountData: AmountData, lps: Intermediary[], options?: FromBTCLNOptions, additionalParams?: Record<string, any>, abortSignal?: AbortSignal): Promise<{
137
140
  quote: Promise<FromBTCLNSwap<T>>;
138
141
  intermediary: Intermediary;
139
142
  }[]>;
@@ -121,6 +121,8 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
121
121
  throw new IntermediaryError_1.IntermediaryError("Invalid intermediary address/pubkey");
122
122
  if (options.descriptionHash != null && decodedPr.tagsObject.purpose_commit_hash !== options.descriptionHash.toString("hex"))
123
123
  throw new IntermediaryError_1.IntermediaryError("Invalid pr returned - description hash");
124
+ if (options.description != null && decodedPr.tagsObject.description !== options.description)
125
+ throw new IntermediaryError_1.IntermediaryError("Invalid pr returned - description");
124
126
  if (decodedPr.tagsObject.payment_hash == null ||
125
127
  !buffer_1.Buffer.from(decodedPr.tagsObject.payment_hash, "hex").equals(paymentHash))
126
128
  throw new IntermediaryError_1.IntermediaryError("Invalid pr returned - payment hash");
@@ -151,12 +153,25 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
151
153
  * @param preFetches Optional pre-fetches for speeding up the quoting process (mainly used internally)
152
154
  */
153
155
  create(recipient, amountData, lps, options, additionalParams, abortSignal, preFetches) {
156
+ if (!this.isInitialized)
157
+ throw new Error("Not initialized, call init() first!");
154
158
  if (options == null)
155
159
  options = {};
156
160
  options.unsafeSkipLnNodeCheck ??= this._options.unsafeSkipLnNodeCheck;
161
+ if (options.paymentHash != null && options.paymentHash.length !== 32)
162
+ throw new UserError_1.UserError("Invalid payment hash length, must be exactly 32 bytes!");
157
163
  if (options.descriptionHash != null && options.descriptionHash.length !== 32)
158
164
  throw new UserError_1.UserError("Invalid description hash length");
159
- const { secret, paymentHash } = this.getSecretAndHash();
165
+ if (options.description != null && buffer_1.Buffer.byteLength(options.description, "utf8") > 500)
166
+ throw new UserError_1.UserError("Invalid description length");
167
+ let secret;
168
+ let paymentHash;
169
+ if (options?.paymentHash != null) {
170
+ paymentHash = options.paymentHash;
171
+ }
172
+ else {
173
+ ({ secret, paymentHash } = this.getSecretAndHash());
174
+ }
160
175
  const claimHash = this._contract.getHashForHtlc(paymentHash);
161
176
  const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
162
177
  const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
@@ -179,6 +194,7 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
179
194
  amount: amountData.amount,
180
195
  claimer: recipient,
181
196
  token: amountData.token.toString(),
197
+ description: options?.description,
182
198
  descriptionHash: options?.descriptionHash,
183
199
  exactOut: !amountData.exactIn,
184
200
  feeRate: (0, Utils_1.throwIfUndefined)(_preFetches.feeRatePromise),
@@ -211,7 +227,7 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
211
227
  feeRate: (await _preFetches.feeRatePromise),
212
228
  initialSwapData: await this._contract.createSwapData(base_1.ChainSwapType.HTLC, lp.getAddress(this.chainIdentifier), recipient, amountData.token, resp.total, claimHash.toString("hex"), this.getRandomSequence(), BigInt(Math.floor(Date.now() / 1000)), false, true, resp.securityDeposit, 0n, nativeTokenAddress),
213
229
  pr: resp.pr,
214
- secret: secret.toString("hex"),
230
+ secret: secret?.toString("hex"),
215
231
  exactIn: amountData.exactIn ?? true
216
232
  });
217
233
  await quote._save();
@@ -236,12 +252,15 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
236
252
  * @param lnurl LNURL-withdraw link to pull the funds from
237
253
  * @param amountData Amount, token and exact input/output data for to swap
238
254
  * @param lps An array of intermediaries (LPs) to get the quotes from
255
+ * @param options Optional additional quote options
239
256
  * @param additionalParams Optional additional parameters sent to the LP when creating the swap
240
257
  * @param abortSignal Abort signal
241
258
  */
242
- async createViaLNURL(recipient, lnurl, amountData, lps, additionalParams, abortSignal) {
259
+ async createViaLNURL(recipient, lnurl, amountData, lps, options, additionalParams, abortSignal) {
243
260
  if (!this.isInitialized)
244
261
  throw new Error("Not initialized, call init() first!");
262
+ if (options?.paymentHash != null && options.paymentHash.length !== 32)
263
+ throw new UserError_1.UserError("Invalid payment hash length, must be exactly 32 bytes!");
245
264
  const abortController = (0, Utils_1.extendAbortController)(abortSignal);
246
265
  const preFetches = {
247
266
  pricePrefetchPromise: this.preFetchPrice(amountData, abortController.signal),
@@ -270,7 +289,7 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
270
289
  if ((amount * 105n / 100n) > max)
271
290
  throw new UserError_1.UserError("Amount more than LNURL-withdraw maximum");
272
291
  }
273
- return this.create(recipient, amountData, lps, undefined, additionalParams, abortSignal, preFetches).map(data => {
292
+ return this.create(recipient, amountData, lps, options, additionalParams, abortSignal, preFetches).map(data => {
274
293
  return {
275
294
  quote: data.quote.then(quote => {
276
295
  quote._setLNURLData(withdrawRequest.url, withdrawRequest.k1, withdrawRequest.callback);
@@ -16,18 +16,52 @@ import { BtcToken, SCToken } from "../../../../types/Token";
16
16
  import { LoggerType } from "../../../../utils/Logger";
17
17
  import { LNURLWithdraw } from "../../../../types/lnurl/LNURLWithdraw";
18
18
  import { PriceInfoType } from "../../../../types/PriceInfoType";
19
+ import { SwapExecutionAction } from "../../../../types/SwapExecutionAction";
19
20
  /**
20
21
  * State enum for FromBTCLNAuto swaps
21
22
  * @category Swaps/Lightning → Smart chain
22
23
  */
23
24
  export declare enum FromBTCLNAutoSwapState {
25
+ /**
26
+ * Swap has failed as the user didn't settle the HTLC on the destination before expiration
27
+ */
24
28
  FAILED = -4,
29
+ /**
30
+ * Swap has expired for good and there is no way how it can be executed anymore
31
+ */
25
32
  QUOTE_EXPIRED = -3,
33
+ /**
34
+ * A swap is almost expired, and it should be presented to the user as expired, though
35
+ * there is still a chance that it will be processed
36
+ */
26
37
  QUOTE_SOFT_EXPIRED = -2,
38
+ /**
39
+ * Swap HTLC on the destination chain has expired, it is not safe anymore to settle (claim) the
40
+ * swap on the destination smart chain.
41
+ */
27
42
  EXPIRED = -1,
43
+ /**
44
+ * Swap quote was created, use {@link FromBTCLNAutoSwap.getAddress} or {@link FromBTCLNAutoSwap.getHyperlink}
45
+ * to get the bolt11 lightning network invoice to pay to initiate the swap, then use the
46
+ * {@link FromBTCLNAutoSwap.waitForPayment} to wait till the lightning network payment is received
47
+ * by the intermediary (LP) and the destination HTLC escrow is created
48
+ */
28
49
  PR_CREATED = 0,
50
+ /**
51
+ * Lightning network payment has been received by the intermediary (LP), but the destination chain
52
+ * HTLC escrow hasn't been created yet. Use {@link FromBTCLNAutoSwap.waitForPayment} to continue waiting
53
+ * till the destination HTLC escrow is created.
54
+ */
29
55
  PR_PAID = 1,
56
+ /**
57
+ * Swap escrow HTLC has been created on the destination chain, wait for automatic settlement by the watchtowers
58
+ * using the {@link FromBTCLNAutoSwap.waitTillClaimed} function or settle manually using the
59
+ * {@link FromBTCLNAutoSwap.claim} or {@link FromBTCLNAutoSwap.txsClaim} function.
60
+ */
30
61
  CLAIM_COMMITED = 2,
62
+ /**
63
+ * Swap successfully settled and funds received on the destination chain
64
+ */
31
65
  CLAIM_CLAIMED = 3
32
66
  }
33
67
  export type FromBTCLNAutoSwapInit<T extends SwapData> = IEscrowSwapInit<T> & {
@@ -54,6 +88,23 @@ export declare function isFromBTCLNAutoSwapInit<T extends SwapData>(obj: any): o
54
88
  */
55
89
  export declare class FromBTCLNAutoSwap<T extends ChainType = ChainType> extends IEscrowSwap<T, FromBTCLNAutoDefinition<T>> implements IAddressSwap, ISwapWithGasDrop<T>, IClaimableSwap<T, FromBTCLNAutoDefinition<T>, FromBTCLNAutoSwapState> {
56
90
  protected readonly TYPE: SwapType.FROM_BTCLN_AUTO;
91
+ /**
92
+ * @internal
93
+ */
94
+ protected readonly swapStateName: (state: number) => string;
95
+ /**
96
+ * @internal
97
+ */
98
+ protected readonly swapStateDescription: {
99
+ [-4]: string;
100
+ [-3]: string;
101
+ [-2]: string;
102
+ [-1]: string;
103
+ 0: string;
104
+ 1: string;
105
+ 2: string;
106
+ 3: string;
107
+ };
57
108
  /**
58
109
  * @internal
59
110
  */
@@ -62,6 +113,11 @@ export declare class FromBTCLNAutoSwap<T extends ChainType = ChainType> extends
62
113
  * @internal
63
114
  */
64
115
  protected readonly inputToken: BtcToken<true>;
116
+ /**
117
+ * Timestamp at which the HTLC was commited on the smart chain side
118
+ * @internal
119
+ */
120
+ _commitedAt?: number;
65
121
  private readonly lnurlFailSignal;
66
122
  private readonly usesClaimHashAsId;
67
123
  private readonly initialSwapData;
@@ -189,6 +245,10 @@ export declare class FromBTCLNAutoSwap<T extends ChainType = ChainType> extends
189
245
  * @inheritDoc
190
246
  */
191
247
  isFailed(): boolean;
248
+ /**
249
+ * @inheritDoc
250
+ */
251
+ isInProgress(): boolean;
192
252
  /**
193
253
  * @inheritDoc
194
254
  */
@@ -301,14 +361,26 @@ export declare class FromBTCLNAutoSwap<T extends ChainType = ChainType> extends
301
361
  }
302
362
  ];
303
363
  private isValidSecretPreimage;
364
+ /**
365
+ * Sets the secret preimage for the swap, in case it is not known already
366
+ *
367
+ * @param secret Secret preimage that matches the expected payment hash
368
+ *
369
+ * @throws {Error} If an invalid secret preimage is provided
370
+ */
371
+ setSecretPreimage(secret: string): void;
372
+ /**
373
+ * Returns whether the secret preimage for this swap is known
374
+ */
375
+ hasSecretPreimage(): boolean;
304
376
  /**
305
377
  * Executes the swap with the provided bitcoin lightning network wallet or LNURL
306
378
  *
307
379
  * @param walletOrLnurlWithdraw Bitcoin lightning wallet to use to pay the lightning network invoice, or an LNURL-withdraw
308
380
  * link, wallet is not required and the LN invoice can be paid externally as well (just pass null or undefined here)
309
381
  * @param callbacks Callbacks to track the progress of the swap
310
- * @param options Optional options for the swap like feeRate, AbortSignal, and timeouts/intervals
311
- * @param secret A swap secret to broadcast to watchtowers, generally only needed if the swap
382
+ * @param options Optional options for the swap like AbortSignal, and timeouts/intervals
383
+ * @param options.secret A swap secret to broadcast to watchtowers, generally only needed if the swap
312
384
  * was recovered from on-chain data, or the pre-image was generated outside the SDK
313
385
  *
314
386
  * @returns {boolean} Whether a swap was settled automatically by swap watchtowers or requires manual claim by the
@@ -321,7 +393,8 @@ export declare class FromBTCLNAutoSwap<T extends ChainType = ChainType> extends
321
393
  abortSignal?: AbortSignal;
322
394
  lightningTxCheckIntervalSeconds?: number;
323
395
  maxWaitTillAutomaticSettlementSeconds?: number;
324
- }, secret?: string): Promise<boolean>;
396
+ secret?: string;
397
+ }): Promise<boolean>;
325
398
  /**
326
399
  * @inheritDoc
327
400
  */
@@ -335,6 +408,19 @@ export declare class FromBTCLNAutoSwap<T extends ChainType = ChainType> extends
335
408
  hyperlink: string;
336
409
  }[];
337
410
  }[]>;
411
+ /**
412
+ *
413
+ * @param options.manualSettlementSmartChainSigner Optional smart chain signer to create a manual claim (settlement) transaction
414
+ * @param options.maxWaitTillAutomaticSettlementSeconds Maximum time to wait for an automatic settlement after
415
+ * the bitcoin transaction is confirmed (defaults to 60 seconds)
416
+ * @param options.secret A swap secret to broadcast to watchtowers, generally only needed if the swap
417
+ * was recovered from on-chain data, or the pre-image was generated outside the SDK
418
+ */
419
+ getCurrentActions(options?: {
420
+ manualSettlementSmartChainSigner?: string | T["Signer"] | T["NativeSigner"];
421
+ maxWaitTillAutomaticSettlementSeconds?: number;
422
+ secret?: string;
423
+ }): Promise<SwapExecutionAction<T>[]>;
338
424
  /**
339
425
  * Checks whether the LP received the LN payment
340
426
  *
@@ -26,15 +26,58 @@ const sha2_1 = require("@noble/hashes/sha2");
26
26
  */
27
27
  var FromBTCLNAutoSwapState;
28
28
  (function (FromBTCLNAutoSwapState) {
29
+ /**
30
+ * Swap has failed as the user didn't settle the HTLC on the destination before expiration
31
+ */
29
32
  FromBTCLNAutoSwapState[FromBTCLNAutoSwapState["FAILED"] = -4] = "FAILED";
33
+ /**
34
+ * Swap has expired for good and there is no way how it can be executed anymore
35
+ */
30
36
  FromBTCLNAutoSwapState[FromBTCLNAutoSwapState["QUOTE_EXPIRED"] = -3] = "QUOTE_EXPIRED";
37
+ /**
38
+ * A swap is almost expired, and it should be presented to the user as expired, though
39
+ * there is still a chance that it will be processed
40
+ */
31
41
  FromBTCLNAutoSwapState[FromBTCLNAutoSwapState["QUOTE_SOFT_EXPIRED"] = -2] = "QUOTE_SOFT_EXPIRED";
42
+ /**
43
+ * Swap HTLC on the destination chain has expired, it is not safe anymore to settle (claim) the
44
+ * swap on the destination smart chain.
45
+ */
32
46
  FromBTCLNAutoSwapState[FromBTCLNAutoSwapState["EXPIRED"] = -1] = "EXPIRED";
47
+ /**
48
+ * Swap quote was created, use {@link FromBTCLNAutoSwap.getAddress} or {@link FromBTCLNAutoSwap.getHyperlink}
49
+ * to get the bolt11 lightning network invoice to pay to initiate the swap, then use the
50
+ * {@link FromBTCLNAutoSwap.waitForPayment} to wait till the lightning network payment is received
51
+ * by the intermediary (LP) and the destination HTLC escrow is created
52
+ */
33
53
  FromBTCLNAutoSwapState[FromBTCLNAutoSwapState["PR_CREATED"] = 0] = "PR_CREATED";
54
+ /**
55
+ * Lightning network payment has been received by the intermediary (LP), but the destination chain
56
+ * HTLC escrow hasn't been created yet. Use {@link FromBTCLNAutoSwap.waitForPayment} to continue waiting
57
+ * till the destination HTLC escrow is created.
58
+ */
34
59
  FromBTCLNAutoSwapState[FromBTCLNAutoSwapState["PR_PAID"] = 1] = "PR_PAID";
60
+ /**
61
+ * Swap escrow HTLC has been created on the destination chain, wait for automatic settlement by the watchtowers
62
+ * using the {@link FromBTCLNAutoSwap.waitTillClaimed} function or settle manually using the
63
+ * {@link FromBTCLNAutoSwap.claim} or {@link FromBTCLNAutoSwap.txsClaim} function.
64
+ */
35
65
  FromBTCLNAutoSwapState[FromBTCLNAutoSwapState["CLAIM_COMMITED"] = 2] = "CLAIM_COMMITED";
66
+ /**
67
+ * Swap successfully settled and funds received on the destination chain
68
+ */
36
69
  FromBTCLNAutoSwapState[FromBTCLNAutoSwapState["CLAIM_CLAIMED"] = 3] = "CLAIM_CLAIMED";
37
70
  })(FromBTCLNAutoSwapState = exports.FromBTCLNAutoSwapState || (exports.FromBTCLNAutoSwapState = {}));
71
+ const FromBTCLNAutoSwapStateDescription = {
72
+ [FromBTCLNAutoSwapState.FAILED]: "Swap has failed as the user didn't settle the HTLC on the destination before expiration",
73
+ [FromBTCLNAutoSwapState.QUOTE_EXPIRED]: "Swap has expired for good and there is no way how it can be executed anymore",
74
+ [FromBTCLNAutoSwapState.QUOTE_SOFT_EXPIRED]: "A swap is expired, though there is still a chance that it will be processed",
75
+ [FromBTCLNAutoSwapState.EXPIRED]: "Swap HTLC on the destination chain has expired, it is not safe anymore to settle (claim) the swap on the destination smart chain.",
76
+ [FromBTCLNAutoSwapState.PR_CREATED]: "Swap quote was created, pay the bolt11 lightning network invoice to initiate the swap, then wait till the lightning network payment is received by the intermediary (LP) and the destination HTLC escrow is created",
77
+ [FromBTCLNAutoSwapState.PR_PAID]: "Lightning network payment has been received by the intermediary (LP), but the destination chain HTLC escrow hasn't been created yet. Continue waiting till the destination HTLC escrow is created.",
78
+ [FromBTCLNAutoSwapState.CLAIM_COMMITED]: "Swap escrow HTLC has been created on the destination chain, wait for automatic settlement by the watchtowers or settle manually.",
79
+ [FromBTCLNAutoSwapState.CLAIM_CLAIMED]: "Swap successfully settled and funds received on the destination chain"
80
+ };
38
81
  function isFromBTCLNAutoSwapInit(obj) {
39
82
  return (obj.pr == null || typeof obj.pr === "string") &&
40
83
  (obj.secret == null || typeof obj.secret === "string") &&
@@ -73,6 +116,14 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
73
116
  initOrObject.url += "/frombtcln_auto";
74
117
  super(wrapper, initOrObject);
75
118
  this.TYPE = SwapType_1.SwapType.FROM_BTCLN_AUTO;
119
+ /**
120
+ * @internal
121
+ */
122
+ this.swapStateName = (state) => FromBTCLNAutoSwapState[state];
123
+ /**
124
+ * @internal
125
+ */
126
+ this.swapStateDescription = FromBTCLNAutoSwapStateDescription;
76
127
  /**
77
128
  * @internal
78
129
  */
@@ -111,6 +162,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
111
162
  this.gasPricingInfo = (0, PriceInfoType_1.deserializePriceInfoType)(initOrObject.gasPricingInfo);
112
163
  this._commitTxId = initOrObject.commitTxId;
113
164
  this._claimTxId = initOrObject.claimTxId;
165
+ this._commitedAt = initOrObject.commitedAt;
114
166
  this.lnurl = initOrObject.lnurl;
115
167
  this.lnurlK1 = initOrObject.lnurlK1;
116
168
  this.lnurlCallback = initOrObject.lnurlCallback;
@@ -300,6 +352,15 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
300
352
  isFailed() {
301
353
  return this._state === FromBTCLNAutoSwapState.FAILED || this._state === FromBTCLNAutoSwapState.EXPIRED;
302
354
  }
355
+ /**
356
+ * @inheritDoc
357
+ */
358
+ isInProgress() {
359
+ return (this._state === FromBTCLNAutoSwapState.PR_CREATED && this.initiated) ||
360
+ (this._state === FromBTCLNAutoSwapState.QUOTE_SOFT_EXPIRED && this.initiated) ||
361
+ this._state === FromBTCLNAutoSwapState.PR_PAID ||
362
+ this._state === FromBTCLNAutoSwapState.CLAIM_COMMITED;
363
+ }
303
364
  /**
304
365
  * @inheritDoc
305
366
  */
@@ -518,6 +579,24 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
518
579
  const claimHash = this.wrapper._contract.getHashForHtlc(paymentHash).toString("hex");
519
580
  return this.getSwapData().getClaimHash() === claimHash;
520
581
  }
582
+ /**
583
+ * Sets the secret preimage for the swap, in case it is not known already
584
+ *
585
+ * @param secret Secret preimage that matches the expected payment hash
586
+ *
587
+ * @throws {Error} If an invalid secret preimage is provided
588
+ */
589
+ setSecretPreimage(secret) {
590
+ if (!this.isValidSecretPreimage(secret))
591
+ throw new Error("Invalid secret preimage provided, hash doesn't match!");
592
+ this.secret = secret;
593
+ }
594
+ /**
595
+ * Returns whether the secret preimage for this swap is known
596
+ */
597
+ hasSecretPreimage() {
598
+ return this.secret != null;
599
+ }
521
600
  //////////////////////////////
522
601
  //// Execution
523
602
  /**
@@ -526,14 +605,14 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
526
605
  * @param walletOrLnurlWithdraw Bitcoin lightning wallet to use to pay the lightning network invoice, or an LNURL-withdraw
527
606
  * link, wallet is not required and the LN invoice can be paid externally as well (just pass null or undefined here)
528
607
  * @param callbacks Callbacks to track the progress of the swap
529
- * @param options Optional options for the swap like feeRate, AbortSignal, and timeouts/intervals
530
- * @param secret A swap secret to broadcast to watchtowers, generally only needed if the swap
608
+ * @param options Optional options for the swap like AbortSignal, and timeouts/intervals
609
+ * @param options.secret A swap secret to broadcast to watchtowers, generally only needed if the swap
531
610
  * was recovered from on-chain data, or the pre-image was generated outside the SDK
532
611
  *
533
612
  * @returns {boolean} Whether a swap was settled automatically by swap watchtowers or requires manual claim by the
534
613
  * user, in case `false` is returned the user should call `swap.claim()` to settle the swap on the destination manually
535
614
  */
536
- async execute(walletOrLnurlWithdraw, callbacks, options, secret) {
615
+ async execute(walletOrLnurlWithdraw, callbacks, options) {
537
616
  if (this._state === FromBTCLNAutoSwapState.FAILED)
538
617
  throw new Error("Swap failed!");
539
618
  if (this._state === FromBTCLNAutoSwapState.EXPIRED)
@@ -568,9 +647,9 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
568
647
  if (this._state === FromBTCLNAutoSwapState.CLAIM_CLAIMED)
569
648
  return true;
570
649
  if (this._state === FromBTCLNAutoSwapState.CLAIM_COMMITED) {
571
- if (this.secret == null && secret == null)
650
+ if (this.secret == null && options?.secret == null)
572
651
  throw new Error("Tried to wait till settlement, but no secret pre-image is known, please pass the secret pre-image as an argument!");
573
- const success = await this.waitTillClaimed(options?.maxWaitTillAutomaticSettlementSeconds ?? 60, options?.abortSignal, secret);
652
+ const success = await this.waitTillClaimed(options?.maxWaitTillAutomaticSettlementSeconds ?? 60, options?.abortSignal, options?.secret);
574
653
  if (success && callbacks?.onSwapSettled != null)
575
654
  callbacks.onSwapSettled(this.getOutputTxId());
576
655
  return success;
@@ -601,6 +680,37 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
601
680
  }
602
681
  throw new Error("Invalid swap state to obtain execution txns, required PR_CREATED");
603
682
  }
683
+ /**
684
+ *
685
+ * @param options.manualSettlementSmartChainSigner Optional smart chain signer to create a manual claim (settlement) transaction
686
+ * @param options.maxWaitTillAutomaticSettlementSeconds Maximum time to wait for an automatic settlement after
687
+ * the bitcoin transaction is confirmed (defaults to 60 seconds)
688
+ * @param options.secret A swap secret to broadcast to watchtowers, generally only needed if the swap
689
+ * was recovered from on-chain data, or the pre-image was generated outside the SDK
690
+ */
691
+ async getCurrentActions(options) {
692
+ if (options?.secret != null)
693
+ this.setSecretPreimage(options.secret);
694
+ if (this._state === FromBTCLNAutoSwapState.PR_CREATED) {
695
+ try {
696
+ return await this.txsExecute();
697
+ }
698
+ catch (e) { }
699
+ }
700
+ if (this.isClaimable()) {
701
+ if (this._commitedAt == null ||
702
+ options?.maxWaitTillAutomaticSettlementSeconds === 0 ||
703
+ (Date.now() - this._commitedAt) > (options?.maxWaitTillAutomaticSettlementSeconds ?? 60) * 1000) {
704
+ return [{
705
+ name: "Claim",
706
+ description: "Manually settle (claim) the swap on the destination smart chain",
707
+ chain: this.chainIdentifier,
708
+ txs: await this.txsClaim(options?.manualSettlementSmartChainSigner)
709
+ }];
710
+ }
711
+ }
712
+ return [];
713
+ }
604
714
  //////////////////////////////
605
715
  //// Payment
606
716
  /**
@@ -828,6 +938,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
828
938
  return;
829
939
  }
830
940
  if (this._state === FromBTCLNAutoSwapState.PR_PAID) {
941
+ this._commitedAt ??= Date.now();
831
942
  await this._saveAndEmit(FromBTCLNAutoSwapState.CLAIM_COMMITED);
832
943
  }
833
944
  if (result === 0)
@@ -1021,6 +1132,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
1021
1132
  data: this._data == null ? null : this._data.serialize(),
1022
1133
  commitTxId: this._commitTxId,
1023
1134
  claimTxId: this._claimTxId,
1135
+ commitedAt: this._commitedAt,
1024
1136
  btcAmountSwap: this.btcAmountSwap == null ? null : this.btcAmountSwap.toString(10),
1025
1137
  btcAmountGas: this.btcAmountGas == null ? null : this.btcAmountGas.toString(10),
1026
1138
  gasSwapFeeBtc: this.gasSwapFeeBtc == null ? null : this.gasSwapFeeBtc.toString(10),
@@ -1159,6 +1271,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
1159
1271
  return true;
1160
1272
  case base_1.SwapCommitStateType.COMMITED:
1161
1273
  if (this._state !== FromBTCLNAutoSwapState.CLAIM_COMMITED && this._state !== FromBTCLNAutoSwapState.EXPIRED) {
1274
+ this._commitedAt ??= Date.now();
1162
1275
  this._state = FromBTCLNAutoSwapState.CLAIM_COMMITED;
1163
1276
  return true;
1164
1277
  }
@@ -18,6 +18,8 @@ import { AmountData } from "../../../../types/AmountData";
18
18
  import { LNURLWithdrawParamsWithUrl } from "../../../../types/lnurl/LNURLWithdraw";
19
19
  import { AllOptional } from "../../../../utils/TypeUtils";
20
20
  export type FromBTCLNAutoOptions = {
21
+ paymentHash?: Buffer;
22
+ description?: string;
21
23
  descriptionHash?: Buffer;
22
24
  unsafeSkipLnNodeCheck?: boolean;
23
25
  gasAmount?: bigint;