@atomiqlabs/lp-lib 17.0.3 → 17.1.0

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.
@@ -17,6 +17,7 @@ export type FromBtcLnRequestType = {
17
17
  paymentHash: string;
18
18
  amount: bigint;
19
19
  token: string;
20
+ description?: string;
20
21
  descriptionHash?: string;
21
22
  exactOut?: boolean;
22
23
  };
@@ -50,6 +51,13 @@ export declare class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAb
50
51
  * @param invoice
51
52
  */
52
53
  private htlcReceived;
54
+ /**
55
+ * Checks invoice description
56
+ *
57
+ * @param description
58
+ * @throws {DefinedRuntimeError} will throw an error if the description is invalid
59
+ */
60
+ private checkDescription;
53
61
  /**
54
62
  * Checks invoice description hash
55
63
  *
@@ -314,6 +314,20 @@ class FromBtcLnAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
314
314
  return;
315
315
  }
316
316
  }
317
+ /**
318
+ * Checks invoice description
319
+ *
320
+ * @param description
321
+ * @throws {DefinedRuntimeError} will throw an error if the description is invalid
322
+ */
323
+ checkDescription(description) {
324
+ if (description != null && Buffer.byteLength(description, "utf8") > 500) {
325
+ throw {
326
+ code: 20100,
327
+ msg: "Invalid request body (description)"
328
+ };
329
+ }
330
+ }
317
331
  /**
318
332
  * Checks invoice description hash
319
333
  *
@@ -430,22 +444,6 @@ class FromBtcLnAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
430
444
  code: 10001,
431
445
  msg: "Invoice expired/canceled"
432
446
  };
433
- const arr = invoice.description.split("-");
434
- if (arr.length < 2)
435
- throw {
436
- _httpStatus: 200,
437
- code: 10001,
438
- msg: "Invoice expired/canceled"
439
- };
440
- const chainIdentifier = arr[0];
441
- const address = arr[1];
442
- const { chainInterface } = this.getChain(chainIdentifier);
443
- if (!chainInterface.isValidAddress(address, true))
444
- throw {
445
- _httpStatus: 200,
446
- code: 10001,
447
- msg: "Invoice expired/canceled"
448
- };
449
447
  switch (invoice.status) {
450
448
  case "canceled":
451
449
  throw {
@@ -484,6 +482,7 @@ class FromBtcLnAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
484
482
  * amount: string amount (in sats) of the invoice
485
483
  * token: string Desired token to swap
486
484
  * exactOut: boolean Whether the swap should be an exact out instead of exact in swap
485
+ * description: string Description of the invoice (max 500 bytes)
487
486
  * descriptionHash: string Description hash of the invoice
488
487
  *
489
488
  *Sent later:
@@ -501,6 +500,7 @@ class FromBtcLnAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
501
500
  token: (val) => val != null &&
502
501
  typeof (val) === "string" &&
503
502
  this.isTokenSupported(chainIdentifier, val) ? val : null,
503
+ description: SchemaVerifier_1.FieldTypeEnum.StringOptional,
504
504
  descriptionHash: SchemaVerifier_1.FieldTypeEnum.StringOptional,
505
505
  exactOut: SchemaVerifier_1.FieldTypeEnum.BooleanOptional
506
506
  });
@@ -520,6 +520,7 @@ class FromBtcLnAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
520
520
  const useToken = parsedBody.token;
521
521
  //Check request params
522
522
  this.checkTooManyInflightSwaps();
523
+ this.checkDescription(parsedBody.description);
523
524
  this.checkDescriptionHash(parsedBody.descriptionHash);
524
525
  const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount);
525
526
  metadata.times.requestChecked = Date.now();
@@ -550,7 +551,7 @@ class FromBtcLnAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
550
551
  metadata.times.balanceChecked = Date.now();
551
552
  //Create swap
552
553
  const hodlInvoiceObj = {
553
- description: chainIdentifier + "-" + parsedBody.address,
554
+ description: parsedBody.description ?? (chainIdentifier + "-" + parsedBody.address),
554
555
  cltvDelta: Number(this.config.minCltv) + 5,
555
556
  expiresAt: Date.now() + (this.config.invoiceTimeoutSeconds * 1000),
556
557
  id: parsedBody.paymentHash,
@@ -23,6 +23,7 @@ export type FromBtcLnAutoRequestType = {
23
23
  gasToken: string;
24
24
  gasAmount: bigint;
25
25
  claimerBounty: bigint;
26
+ description?: string;
26
27
  descriptionHash?: string;
27
28
  exactOut?: boolean;
28
29
  };
@@ -63,6 +64,13 @@ export declare class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoS
63
64
  */
64
65
  private htlcReceived;
65
66
  private offerHtlc;
67
+ /**
68
+ * Checks invoice description
69
+ *
70
+ * @param description
71
+ * @throws {DefinedRuntimeError} will throw an error if the description is invalid
72
+ */
73
+ private checkDescription;
66
74
  /**
67
75
  * Checks invoice description hash
68
76
  *
@@ -360,6 +360,20 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
360
360
  }
361
361
  return true;
362
362
  }
363
+ /**
364
+ * Checks invoice description
365
+ *
366
+ * @param description
367
+ * @throws {DefinedRuntimeError} will throw an error if the description is invalid
368
+ */
369
+ checkDescription(description) {
370
+ if (description != null && Buffer.byteLength(description, "utf8") > 500) {
371
+ throw {
372
+ code: 20100,
373
+ msg: "Invalid request body (description)"
374
+ };
375
+ }
376
+ }
363
377
  /**
364
378
  * Checks invoice description hash
365
379
  *
@@ -454,22 +468,6 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
454
468
  code: 10001,
455
469
  msg: "Invoice expired/canceled"
456
470
  };
457
- const arr = invoice.description.split("-");
458
- if (arr.length < 2)
459
- throw {
460
- _httpStatus: 200,
461
- code: 10001,
462
- msg: "Invoice expired/canceled"
463
- };
464
- const chainIdentifier = arr[0];
465
- const address = arr[1];
466
- const { chainInterface } = this.getChain(chainIdentifier);
467
- if (!chainInterface.isValidAddress(address, true))
468
- throw {
469
- _httpStatus: 200,
470
- code: 10001,
471
- msg: "Invoice expired/canceled"
472
- };
473
471
  switch (invoice.status) {
474
472
  case "canceled":
475
473
  throw {
@@ -511,6 +509,7 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
511
509
  * amount: string amount (in sats) of the invoice
512
510
  * token: string Desired token to swap
513
511
  * exactOut: boolean Whether the swap should be an exact out instead of exact in swap
512
+ * description: string Description of the invoice (max 500 bytes)
514
513
  * descriptionHash: string Description hash of the invoice
515
514
  * gasAmount: string Desired amount in gas token to also get
516
515
  * gasToken: string
@@ -528,6 +527,7 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
528
527
  token: (val) => val != null &&
529
528
  typeof (val) === "string" &&
530
529
  this.isTokenSupported(chainIdentifier, val) ? val : null,
530
+ description: SchemaVerifier_1.FieldTypeEnum.StringOptional,
531
531
  descriptionHash: SchemaVerifier_1.FieldTypeEnum.StringOptional,
532
532
  exactOut: SchemaVerifier_1.FieldTypeEnum.BooleanOptional,
533
533
  gasToken: (val) => val != null &&
@@ -572,6 +572,7 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
572
572
  const useToken = parsedBody.token;
573
573
  const gasToken = parsedBody.gasToken;
574
574
  //Check request params
575
+ this.checkDescription(parsedBody.description);
575
576
  this.checkDescriptionHash(parsedBody.descriptionHash);
576
577
  this.checkTooManyInflightSwaps();
577
578
  const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount, gasTokenAmount);
@@ -606,7 +607,7 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
606
607
  metadata.times.balanceChecked = Date.now();
607
608
  //Create swap
608
609
  const hodlInvoiceObj = {
609
- description: chainIdentifier + "-" + parsedBody.address,
610
+ description: parsedBody.description ?? (chainIdentifier + "-" + parsedBody.address),
610
611
  cltvDelta: Number(this.config.minCltv) + 5,
611
612
  expiresAt: Date.now() + (this.config.invoiceTimeoutSeconds * 1000),
612
613
  id: parsedBody.paymentHash,
@@ -511,9 +511,9 @@ class SpvVaults {
511
511
  const btcTxOutput = btcTx.outs[parseInt(voutStr)];
512
512
  const vaultAddress = this.bitcoin.fromOutputScript(Buffer.from(btcTxOutput.scriptPubKey.hex, "hex"));
513
513
  const vault = new SpvVault_1.SpvVault(chainId, vaultData, vaultAddress);
514
- vault.state = SpvVault_1.SpvVaultState.OPENED;
514
+ vault.state = vaultData.isOpened() ? SpvVault_1.SpvVaultState.OPENED : SpvVault_1.SpvVaultState.CLOSED;
515
515
  recoveredVaults.push(vault);
516
- if (await this.bitcoinRpc.isSpent(vaultData.getUtxo())) {
516
+ if (vaultData.isOpened() && await this.bitcoinRpc.isSpent(vaultData.getUtxo())) {
517
517
  if (!this.bitcoin.isReady())
518
518
  throw new Error("Bitcoin wallet is not ready, but is required to check wallet transactions!");
519
519
  //The latest smart chain UTXO is spent, we need to check if we have some further transactions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/lp-lib",
3
- "version": "17.0.3",
3
+ "version": "17.1.0",
4
4
  "description": "Main functionality implementation for atomiq LP node",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -40,6 +40,7 @@ export type FromBtcLnRequestType = {
40
40
  paymentHash: string,
41
41
  amount: bigint,
42
42
  token: string,
43
+ description?: string,
43
44
  descriptionHash?: string,
44
45
  exactOut?: boolean
45
46
  }
@@ -399,6 +400,21 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
399
400
  }
400
401
  }
401
402
 
403
+ /**
404
+ * Checks invoice description
405
+ *
406
+ * @param description
407
+ * @throws {DefinedRuntimeError} will throw an error if the description is invalid
408
+ */
409
+ private checkDescription(description: string) {
410
+ if(description!=null && Buffer.byteLength(description, "utf8") > 500) {
411
+ throw {
412
+ code: 20100,
413
+ msg: "Invalid request body (description)"
414
+ };
415
+ }
416
+ }
417
+
402
418
  /**
403
419
  * Checks invoice description hash
404
420
  *
@@ -535,22 +551,6 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
535
551
  msg: "Invoice expired/canceled"
536
552
  };
537
553
 
538
- const arr = invoice.description.split("-");
539
- if(arr.length<2) throw {
540
- _httpStatus: 200,
541
- code: 10001,
542
- msg: "Invoice expired/canceled"
543
- };
544
- const chainIdentifier = arr[0];
545
- const address = arr[1];
546
-
547
- const {chainInterface} = this.getChain(chainIdentifier);
548
- if(!chainInterface.isValidAddress(address, true)) throw {
549
- _httpStatus: 200,
550
- code: 10001,
551
- msg: "Invoice expired/canceled"
552
- };
553
-
554
554
  switch(invoice.status) {
555
555
  case "canceled":
556
556
  throw {
@@ -600,6 +600,7 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
600
600
  * amount: string amount (in sats) of the invoice
601
601
  * token: string Desired token to swap
602
602
  * exactOut: boolean Whether the swap should be an exact out instead of exact in swap
603
+ * description: string Description of the invoice (max 500 bytes)
603
604
  * descriptionHash: string Description hash of the invoice
604
605
  *
605
606
  *Sent later:
@@ -617,6 +618,7 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
617
618
  token: (val: string) => val!=null &&
618
619
  typeof(val)==="string" &&
619
620
  this.isTokenSupported(chainIdentifier, val) ? val : null,
621
+ description: FieldTypeEnum.StringOptional,
620
622
  descriptionHash: FieldTypeEnum.StringOptional,
621
623
  exactOut: FieldTypeEnum.BooleanOptional
622
624
  });
@@ -637,6 +639,7 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
637
639
 
638
640
  //Check request params
639
641
  this.checkTooManyInflightSwaps();
642
+ this.checkDescription(parsedBody.description);
640
643
  this.checkDescriptionHash(parsedBody.descriptionHash);
641
644
  const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount);
642
645
  metadata.times.requestChecked = Date.now();
@@ -689,7 +692,7 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
689
692
 
690
693
  //Create swap
691
694
  const hodlInvoiceObj: HodlInvoiceInit = {
692
- description: chainIdentifier+"-"+parsedBody.address,
695
+ description: parsedBody.description ?? (chainIdentifier+"-"+parsedBody.address),
693
696
  cltvDelta: Number(this.config.minCltv) + 5,
694
697
  expiresAt: Date.now()+(this.config.invoiceTimeoutSeconds*1000),
695
698
  id: parsedBody.paymentHash,
@@ -35,6 +35,7 @@ export type FromBtcLnAutoRequestType = {
35
35
  gasToken: string,
36
36
  gasAmount: bigint,
37
37
  claimerBounty: bigint,
38
+ description?: string,
38
39
  descriptionHash?: string,
39
40
  exactOut?: boolean
40
41
  }
@@ -439,6 +440,21 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
439
440
  return true;
440
441
  }
441
442
 
443
+ /**
444
+ * Checks invoice description
445
+ *
446
+ * @param description
447
+ * @throws {DefinedRuntimeError} will throw an error if the description is invalid
448
+ */
449
+ private checkDescription(description: string) {
450
+ if(description!=null && Buffer.byteLength(description, "utf8") > 500) {
451
+ throw {
452
+ code: 20100,
453
+ msg: "Invalid request body (description)"
454
+ };
455
+ }
456
+ }
457
+
442
458
  /**
443
459
  * Checks invoice description hash
444
460
  *
@@ -539,22 +555,6 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
539
555
  msg: "Invoice expired/canceled"
540
556
  };
541
557
 
542
- const arr = invoice.description.split("-");
543
- if(arr.length<2) throw {
544
- _httpStatus: 200,
545
- code: 10001,
546
- msg: "Invoice expired/canceled"
547
- };
548
- const chainIdentifier = arr[0];
549
- const address = arr[1];
550
-
551
- const {chainInterface} = this.getChain(chainIdentifier);
552
- if(!chainInterface.isValidAddress(address, true)) throw {
553
- _httpStatus: 200,
554
- code: 10001,
555
- msg: "Invoice expired/canceled"
556
- };
557
-
558
558
  switch(invoice.status) {
559
559
  case "canceled":
560
560
  throw {
@@ -605,6 +605,7 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
605
605
  * amount: string amount (in sats) of the invoice
606
606
  * token: string Desired token to swap
607
607
  * exactOut: boolean Whether the swap should be an exact out instead of exact in swap
608
+ * description: string Description of the invoice (max 500 bytes)
608
609
  * descriptionHash: string Description hash of the invoice
609
610
  * gasAmount: string Desired amount in gas token to also get
610
611
  * gasToken: string
@@ -622,6 +623,7 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
622
623
  token: (val: string) => val!=null &&
623
624
  typeof(val)==="string" &&
624
625
  this.isTokenSupported(chainIdentifier, val) ? val : null,
626
+ description: FieldTypeEnum.StringOptional,
625
627
  descriptionHash: FieldTypeEnum.StringOptional,
626
628
  exactOut: FieldTypeEnum.BooleanOptional,
627
629
  gasToken: (val: string) => val!=null &&
@@ -666,6 +668,7 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
666
668
  const gasToken = parsedBody.gasToken;
667
669
 
668
670
  //Check request params
671
+ this.checkDescription(parsedBody.description);
669
672
  this.checkDescriptionHash(parsedBody.descriptionHash);
670
673
  this.checkTooManyInflightSwaps();
671
674
  const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount, gasTokenAmount);
@@ -724,7 +727,7 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
724
727
 
725
728
  //Create swap
726
729
  const hodlInvoiceObj: HodlInvoiceInit = {
727
- description: chainIdentifier+"-"+parsedBody.address,
730
+ description: parsedBody.description ?? (chainIdentifier+"-"+parsedBody.address),
728
731
  cltvDelta: Number(this.config.minCltv) + 5,
729
732
  expiresAt: Date.now()+(this.config.invoiceTimeoutSeconds*1000),
730
733
  id: parsedBody.paymentHash,
@@ -1,6 +1,8 @@
1
1
  import {SpvVault, SpvVaultState} from "./SpvVault";
2
2
  import {
3
- BitcoinRpc, BtcBlock, BtcTx,
3
+ BitcoinRpc,
4
+ BtcBlock,
5
+ BtcTx,
4
6
  IStorageManager,
5
7
  SpvVaultClaimEvent,
6
8
  SpvVaultCloseEvent,
@@ -591,9 +593,9 @@ export class SpvVaults {
591
593
  const btcTxOutput = btcTx.outs[parseInt(voutStr)];
592
594
  const vaultAddress = this.bitcoin.fromOutputScript(Buffer.from(btcTxOutput.scriptPubKey.hex, "hex"));
593
595
  const vault = new SpvVault(chainId, vaultData, vaultAddress);
594
- vault.state = SpvVaultState.OPENED;
596
+ vault.state = vaultData.isOpened() ? SpvVaultState.OPENED : SpvVaultState.CLOSED;
595
597
  recoveredVaults.push(vault);
596
- if(await this.bitcoinRpc.isSpent(vaultData.getUtxo())) {
598
+ if(vaultData.isOpened() && await this.bitcoinRpc.isSpent(vaultData.getUtxo())) {
597
599
  if(!this.bitcoin.isReady())
598
600
  throw new Error("Bitcoin wallet is not ready, but is required to check wallet transactions!");
599
601