@atomiqlabs/lp-lib 16.0.5 → 16.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +2 -2
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +1 -0
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +10 -2
- package/dist/swaps/spv_vault_swap/SpvVaults.js +5 -2
- package/package.json +1 -1
- package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +2 -2
- package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +14 -3
- package/src/swaps/spv_vault_swap/SpvVaults.ts +4 -2
|
@@ -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
|
|
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,
|
|
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);
|
|
@@ -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.
|
|
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)
|
|
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 };
|
|
@@ -120,7 +120,7 @@ class SpvVaults {
|
|
|
120
120
|
return Object.keys(this.vaultStorage.data)
|
|
121
121
|
.map(key => this.vaultStorage.data[key])
|
|
122
122
|
.filter(val => chainId == null ? true : val.chainId === chainId)
|
|
123
|
-
.filter(val => val.data.getOwner() === this.chains.chains[val.chainId]?.signer?.getAddress())
|
|
123
|
+
.filter(val => this.chains.chains[val.chainId] != null && val.data.getOwner() === this.chains.chains[val.chainId]?.signer?.getAddress())
|
|
124
124
|
.filter(val => token == null ? true : val.data.getTokenData()[0].token === token);
|
|
125
125
|
}
|
|
126
126
|
async fundVault(vault, tokenAmounts) {
|
|
@@ -296,7 +296,10 @@ class SpvVaults {
|
|
|
296
296
|
const claimWithdrawals = [];
|
|
297
297
|
let promises = [];
|
|
298
298
|
for (let vault of vaults) {
|
|
299
|
-
const
|
|
299
|
+
const chainData = this.chains.chains[vault.chainId];
|
|
300
|
+
if (chainData == null)
|
|
301
|
+
continue;
|
|
302
|
+
const { signer, spvVaultContract, chainInterface } = chainData;
|
|
300
303
|
if (vault.data.getOwner() !== signer.getAddress())
|
|
301
304
|
continue;
|
|
302
305
|
if (vault.state === SpvVault_1.SpvVaultState.BTC_INITIATED) {
|
package/package.json
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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.
|
|
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)
|
|
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};
|
|
@@ -162,7 +162,7 @@ export class SpvVaults {
|
|
|
162
162
|
return Object.keys(this.vaultStorage.data)
|
|
163
163
|
.map(key => this.vaultStorage.data[key])
|
|
164
164
|
.filter(val => chainId==null ? true : val.chainId===chainId)
|
|
165
|
-
.filter(val => val.data.getOwner()===this.chains.chains[val.chainId]?.signer?.getAddress())
|
|
165
|
+
.filter(val => this.chains.chains[val.chainId]!=null && val.data.getOwner()===this.chains.chains[val.chainId]?.signer?.getAddress())
|
|
166
166
|
.filter(val => token==null ? true : val.data.getTokenData()[0].token===token);
|
|
167
167
|
}
|
|
168
168
|
|
|
@@ -351,7 +351,9 @@ export class SpvVaults {
|
|
|
351
351
|
let promises: Promise<void>[] = [];
|
|
352
352
|
|
|
353
353
|
for(let vault of vaults) {
|
|
354
|
-
const
|
|
354
|
+
const chainData = this.chains.chains[vault.chainId];
|
|
355
|
+
if(chainData==null) continue;
|
|
356
|
+
const {signer, spvVaultContract, chainInterface} = chainData;
|
|
355
357
|
if(vault.data.getOwner()!==signer.getAddress()) continue;
|
|
356
358
|
|
|
357
359
|
if(vault.state===SpvVaultState.BTC_INITIATED) {
|