@atomiqlabs/lp-lib 16.0.4 → 16.0.6

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.
@@ -74,6 +74,8 @@ class EscrowHandler extends SwapHandler_1.SwapHandler {
74
74
  }
75
75
  }
76
76
  async removeSwapData(swap, ultimateState) {
77
+ this.inflightSwaps.delete(swap.getIdentifier());
78
+ this.logger.debug("removeSwapData(): Removing in-flight swap, current in-flight swaps: " + this.inflightSwaps.size);
77
79
  if (ultimateState != null)
78
80
  await swap.setState(ultimateState);
79
81
  if (swap != null)
@@ -586,13 +586,13 @@ class ToBtcAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
586
586
  return resp;
587
587
  }, abortController.signal);
588
588
  metadata.times.priceCalculated = Date.now();
589
- const paymentHash = this.getHash(chainIdentifier, parsedBody.address, parsedBody.confirmations, parsedBody.nonce, amountBD).toString("hex");
589
+ const claimHash = this.getHash(chainIdentifier, parsedBody.address, parsedBody.confirmations, parsedBody.nonce, amountBD).toString("hex");
590
590
  //Add grace period another time, so the user has 1 hour to commit
591
591
  const expirySeconds = this.getExpiryFromCLTV(parsedBody.confirmationTarget, parsedBody.confirmations) + this.config.gracePeriod;
592
592
  const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
593
593
  const minRequiredExpiry = currentTimestamp + expirySeconds;
594
594
  const sequence = base_1.BigIntBufferUtils.fromBuffer((0, crypto_1.randomBytes)(8));
595
- const payObject = await swapContract.createSwapData(base_1.ChainSwapType.CHAIN_NONCED, parsedBody.offerer, signer.getAddress(), useToken, totalInToken, paymentHash, sequence, minRequiredExpiry, true, false, 0n, 0n);
595
+ const payObject = await swapContract.createSwapData(base_1.ChainSwapType.CHAIN_NONCED, parsedBody.offerer, signer.getAddress(), useToken, totalInToken, claimHash, sequence, minRequiredExpiry, true, false, 0n, 0n);
596
596
  abortController.signal.throwIfAborted();
597
597
  metadata.times.swapCreated = Date.now();
598
598
  const sigData = await this.getToBtcSignatureData(chainIdentifier, payObject, req, abortController.signal, signDataPrefetchPromise);
@@ -15,6 +15,7 @@ export type ToBtcLnConfig = ToBtcBaseConfig & {
15
15
  minLnRoutingFeePPM?: bigint;
16
16
  minLnBaseFee?: bigint;
17
17
  exactInExpiry?: number;
18
+ lnSendBitcoinBlockTimeSafetyFactorPPM?: bigint;
18
19
  };
19
20
  type ExactInAuthorization = {
20
21
  chainIdentifier: string;
@@ -31,6 +31,10 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
31
31
  this.config.minLnRoutingFeePPM = this.config.minLnRoutingFeePPM || 1000n;
32
32
  this.config.minLnBaseFee = this.config.minLnBaseFee || 5n;
33
33
  this.config.exactInExpiry = this.config.exactInExpiry || 10 * 1000;
34
+ this.config.lnSendBitcoinBlockTimeSafetyFactorPPM = this.config.lnSendBitcoinBlockTimeSafetyFactorPPM ?? (this.config.safetyFactor * 1000000n);
35
+ if (this.config.lnSendBitcoinBlockTimeSafetyFactorPPM <= 1100000n) {
36
+ throw new Error("Lightning network send block safety factor set below 1.1, this is insecure!");
37
+ }
34
38
  }
35
39
  /**
36
40
  * Cleans up exactIn authorization that are already past their expiry
@@ -226,7 +230,7 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
226
230
  //Compute max cltv delta
227
231
  const maxFee = swap.quotedNetworkFee;
228
232
  const maxUsableCLTVdelta = (expiryTimestamp - currentTimestamp - this.config.gracePeriod)
229
- / (this.config.bitcoinBlocktime * this.config.safetyFactor);
233
+ / (this.config.bitcoinBlocktime * this.config.lnSendBitcoinBlockTimeSafetyFactorPPM / 1000000n);
230
234
  //Initiate payment
231
235
  this.swapLogger.info(swap, "sendLightningPayment(): paying lightning network invoice," +
232
236
  " cltvDelta: " + maxUsableCLTVdelta.toString(10) +
@@ -442,10 +446,12 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
442
446
  * @throws {DefinedRuntimeError} will throw an error if the destination is unreachable
443
447
  */
444
448
  async checkAndGetNetworkFee(amountBD, maxFee, expiryTimestamp, currentTimestamp, pr, metadata, abortSignal) {
445
- const maxUsableCLTV = (expiryTimestamp - currentTimestamp - this.config.gracePeriod) / (this.config.bitcoinBlocktime * this.config.safetyFactor);
449
+ const maxUsableCLTV = (expiryTimestamp - currentTimestamp - this.config.gracePeriod)
450
+ / (this.config.bitcoinBlocktime * this.config.lnSendBitcoinBlockTimeSafetyFactorPPM / 1000000n);
446
451
  const blockHeight = await this.lightning.getBlockheight();
447
452
  abortSignal.throwIfAborted();
448
453
  metadata.times.blockheightFetched = Date.now();
454
+ metadata.probeAndRouteTimeoutDelta = maxUsableCLTV;
449
455
  const maxTimeoutBlockheight = BigInt(blockHeight) + maxUsableCLTV;
450
456
  const req = {
451
457
  request: pr,
@@ -453,6 +459,8 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
453
459
  maxFeeMtokens: maxFee * 1000n,
454
460
  maxTimeoutHeight: Number(maxTimeoutBlockheight)
455
461
  };
462
+ this.logger.debug(`checkAndGetNetworkFee(): Attempting to probe/route the payment amount: ${amountBD.toString(10)}` +
463
+ `, maxFee: ${maxFee.toString(10)}, expiryDelta: ${maxUsableCLTV.toString(10)}, expiryHeight: ${maxTimeoutBlockheight.toString(10)}, pr: ${pr}`);
456
464
  let probeOrRouteResp = await this.lightning.probe(req);
457
465
  metadata.times.probeResult = Date.now();
458
466
  metadata.probeResponse = { ...probeOrRouteResp };
@@ -64,6 +64,5 @@ export declare class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVa
64
64
  startRestServer(restServer: Express): void;
65
65
  getInfoData(): any;
66
66
  protected saveSwapData(swap: SpvVaultSwap): Promise<void>;
67
- protected removeSwapData(hash: string, sequence: bigint): Promise<void>;
68
67
  protected removeSwapData(swap: SpvVaultSwap, ultimateState?: SpvVaultSwapState): Promise<void>;
69
68
  }
@@ -518,22 +518,10 @@ class SpvVaultSwapHandler extends SwapHandler_1.SwapHandler {
518
518
  this.btcTxIdIndex.set(swap.btcTxId, swap);
519
519
  return super.saveSwapData(swap);
520
520
  }
521
- async removeSwapData(hashOrSwap, sequenceOrUltimateState) {
522
- let swap;
523
- let state;
524
- if (typeof (hashOrSwap) === "string") {
525
- if (typeof (sequenceOrUltimateState) !== "bigint")
526
- throw new Error("Sequence must be a BN instance!");
527
- swap = await this.storageManager.getData(hashOrSwap, sequenceOrUltimateState);
528
- }
529
- else {
530
- swap = hashOrSwap;
531
- if (sequenceOrUltimateState != null && typeof (sequenceOrUltimateState) !== "bigint")
532
- state = sequenceOrUltimateState;
533
- }
521
+ async removeSwapData(swap, ultimateState) {
534
522
  if (swap.btcTxId != null)
535
523
  this.btcTxIdIndex.delete(swap.btcTxId);
536
- return super.removeSwapData(swap, state);
524
+ return super.removeSwapData(swap, ultimateState);
537
525
  }
538
526
  }
539
527
  exports.SpvVaultSwapHandler = SpvVaultSwapHandler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/lp-lib",
3
- "version": "16.0.4",
3
+ "version": "16.0.6",
4
4
  "description": "Main functionality implementation for atomiq LP node",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -89,8 +89,9 @@ export abstract class EscrowHandler<V extends EscrowHandlerSwap<SwapData, S>, S>
89
89
  }
90
90
  }
91
91
 
92
- protected removeSwapData(swap: V, ultimateState?: S): Promise<void>;
93
92
  protected async removeSwapData(swap: V, ultimateState?: S) {
93
+ this.inflightSwaps.delete(swap.getIdentifier());
94
+ this.logger.debug("removeSwapData(): Removing in-flight swap, current in-flight swaps: "+this.inflightSwaps.size);
94
95
  if(ultimateState!=null) await swap.setState(ultimateState);
95
96
  if(swap!=null) await PluginManager.swapRemove(swap);
96
97
  this.swapLogger.debug(swap, "removeSwapData(): removing swap final state: "+swap.state);
@@ -708,7 +708,7 @@ export class ToBtcAbs extends ToBtcBaseSwapHandler<ToBtcSwapAbs, ToBtcSwapState>
708
708
  }, abortController.signal);
709
709
  metadata.times.priceCalculated = Date.now();
710
710
 
711
- const paymentHash = this.getHash(chainIdentifier, parsedBody.address, parsedBody.confirmations, parsedBody.nonce, amountBD).toString("hex");
711
+ const claimHash = this.getHash(chainIdentifier, parsedBody.address, parsedBody.confirmations, parsedBody.nonce, amountBD).toString("hex");
712
712
 
713
713
  //Add grace period another time, so the user has 1 hour to commit
714
714
  const expirySeconds = this.getExpiryFromCLTV(parsedBody.confirmationTarget, parsedBody.confirmations) + this.config.gracePeriod;
@@ -722,7 +722,7 @@ export class ToBtcAbs extends ToBtcBaseSwapHandler<ToBtcSwapAbs, ToBtcSwapState>
722
722
  signer.getAddress(),
723
723
  useToken,
724
724
  totalInToken,
725
- paymentHash,
725
+ claimHash,
726
726
  sequence,
727
727
  minRequiredExpiry,
728
728
  true,
@@ -40,7 +40,9 @@ export type ToBtcLnConfig = ToBtcBaseConfig & {
40
40
  minLnRoutingFeePPM?: bigint,
41
41
  minLnBaseFee?: bigint,
42
42
 
43
- exactInExpiry?: number
43
+ exactInExpiry?: number,
44
+
45
+ lnSendBitcoinBlockTimeSafetyFactorPPM?: bigint
44
46
  };
45
47
 
46
48
  type ExactInAuthorization = {
@@ -118,6 +120,10 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
118
120
  this.config.minLnRoutingFeePPM = this.config.minLnRoutingFeePPM || 1000n;
119
121
  this.config.minLnBaseFee = this.config.minLnBaseFee || 5n;
120
122
  this.config.exactInExpiry = this.config.exactInExpiry || 10*1000;
123
+ this.config.lnSendBitcoinBlockTimeSafetyFactorPPM = this.config.lnSendBitcoinBlockTimeSafetyFactorPPM ?? (this.config.safetyFactor * 1_000_000n);
124
+ if(this.config.lnSendBitcoinBlockTimeSafetyFactorPPM <= 1_100_000n) {
125
+ throw new Error("Lightning network send block safety factor set below 1.1, this is insecure!");
126
+ }
121
127
  }
122
128
 
123
129
  /**
@@ -328,7 +334,7 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
328
334
  //Compute max cltv delta
329
335
  const maxFee = swap.quotedNetworkFee;
330
336
  const maxUsableCLTVdelta = (expiryTimestamp - currentTimestamp - this.config.gracePeriod)
331
- / (this.config.bitcoinBlocktime * this.config.safetyFactor);
337
+ / (this.config.bitcoinBlocktime * this.config.lnSendBitcoinBlockTimeSafetyFactorPPM / 1_000_000n);
332
338
 
333
339
  //Initiate payment
334
340
  this.swapLogger.info(swap, "sendLightningPayment(): paying lightning network invoice,"+
@@ -554,11 +560,13 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
554
560
  confidence: number,
555
561
  networkFee: bigint
556
562
  }> {
557
- const maxUsableCLTV: bigint = (expiryTimestamp - currentTimestamp - this.config.gracePeriod) / (this.config.bitcoinBlocktime * this.config.safetyFactor);
563
+ const maxUsableCLTV: bigint = (expiryTimestamp - currentTimestamp - this.config.gracePeriod)
564
+ / (this.config.bitcoinBlocktime * this.config.lnSendBitcoinBlockTimeSafetyFactorPPM / 1_000_000n);
558
565
 
559
566
  const blockHeight = await this.lightning.getBlockheight();
560
567
  abortSignal.throwIfAborted();
561
568
  metadata.times.blockheightFetched = Date.now();
569
+ metadata.probeAndRouteTimeoutDelta = maxUsableCLTV;
562
570
 
563
571
  const maxTimeoutBlockheight = BigInt(blockHeight) + maxUsableCLTV;
564
572
 
@@ -569,6 +577,9 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
569
577
  maxTimeoutHeight: Number(maxTimeoutBlockheight)
570
578
  };
571
579
 
580
+ this.logger.debug(`checkAndGetNetworkFee(): Attempting to probe/route the payment amount: ${amountBD.toString(10)}`+
581
+ `, maxFee: ${maxFee.toString(10)}, expiryDelta: ${maxUsableCLTV.toString(10)}, expiryHeight: ${maxTimeoutBlockheight.toString(10)}, pr: ${pr}`);
582
+
572
583
  let probeOrRouteResp: ProbeAndRouteResponse = await this.lightning.probe(req);
573
584
  metadata.times.probeResult = Date.now();
574
585
  metadata.probeResponse = {...probeOrRouteResp};
@@ -667,20 +667,9 @@ export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapS
667
667
  return super.saveSwapData(swap);
668
668
  }
669
669
 
670
- protected removeSwapData(hash: string, sequence: bigint): Promise<void>;
671
- protected removeSwapData(swap: SpvVaultSwap, ultimateState?: SpvVaultSwapState): Promise<void>;
672
- protected async removeSwapData(hashOrSwap: string | SpvVaultSwap, sequenceOrUltimateState?: bigint | SpvVaultSwapState): Promise<void> {
673
- let swap: SpvVaultSwap;
674
- let state: SpvVaultSwapState;
675
- if(typeof(hashOrSwap)==="string") {
676
- if(typeof(sequenceOrUltimateState)!=="bigint") throw new Error("Sequence must be a BN instance!");
677
- swap = await this.storageManager.getData(hashOrSwap, sequenceOrUltimateState);
678
- } else {
679
- swap = hashOrSwap;
680
- if(sequenceOrUltimateState!=null && typeof(sequenceOrUltimateState)!=="bigint") state = sequenceOrUltimateState;
681
- }
670
+ protected async removeSwapData(swap: SpvVaultSwap, ultimateState?: SpvVaultSwapState): Promise<void> {
682
671
  if(swap.btcTxId!=null) this.btcTxIdIndex.delete(swap.btcTxId);
683
- return super.removeSwapData(swap, state);
672
+ return super.removeSwapData(swap, ultimateState);
684
673
  }
685
674
 
686
675
  }