@wishknish/knishio-client-ts 0.9.3 → 0.9.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.
package/dist/index.js CHANGED
@@ -4487,7 +4487,7 @@ var CheckMolecule = class _CheckMolecule {
4487
4487
  * @return True if all validations pass
4488
4488
  */
4489
4489
  verify(senderWallet) {
4490
- return this.molecularHash() && this.ots() && this.batchId() && this.continuId() && this.isotopeM() && this.isotopeT() && this.isotopeC() && this.isotopeU() && this.isotopeI() && this.isotopeR() && this.isotopeV(senderWallet || null);
4490
+ return this.molecularHash() && this.ots() && this.batchId() && this.continuId() && this.isotopeM() && this.isotopeT() && this.isotopeC() && this.isotopeU() && this.isotopeI() && this.isotopeR() && this.isotopeP() && this.isotopeA() && this.isotopeB() && this.isotopeF() && this.isotopeV(senderWallet || null);
4491
4491
  }
4492
4492
  /**
4493
4493
  * Validate ContinuID requirement
@@ -4675,6 +4675,141 @@ var CheckMolecule = class _CheckMolecule {
4675
4675
  }
4676
4676
  return true;
4677
4677
  }
4678
+ /**
4679
+ * Validate isotope P atoms (peering)
4680
+ * @return True if valid
4681
+ * @throws WrongTokenTypeException if the token slug is not USER
4682
+ * @throws MetaMissingException if the required peerHost meta field is absent
4683
+ */
4684
+ isotopeP() {
4685
+ for (const atom of this.molecule.getIsotopes("P")) {
4686
+ if (atom.token !== "USER") {
4687
+ throw new WrongTokenTypeException(`Check::isotopeP() - "${atom.token}" is not a valid Token slug for "${atom.isotope}" isotope Atoms!`);
4688
+ }
4689
+ const metas = atom.aggregatedMeta();
4690
+ if (!Object.prototype.hasOwnProperty.call(metas, "peerHost") || !metas.peerHost) {
4691
+ throw new MetaMissingException('Check::isotopeP() - Required meta field "peerHost" is missing!');
4692
+ }
4693
+ }
4694
+ return true;
4695
+ }
4696
+ /**
4697
+ * Validate isotope A atoms (append request)
4698
+ * @return True if valid
4699
+ * @throws WrongTokenTypeException if the token slug is not USER
4700
+ * @throws MetaMissingException if metaType, metaId or the action meta field is absent
4701
+ */
4702
+ isotopeA() {
4703
+ for (const atom of this.molecule.getIsotopes("A")) {
4704
+ if (atom.token !== "USER") {
4705
+ throw new WrongTokenTypeException(`Check::isotopeA() - "${atom.token}" is not a valid Token slug for "${atom.isotope}" isotope Atoms!`);
4706
+ }
4707
+ if (!atom.metaType) {
4708
+ throw new MetaMissingException('Check::isotopeA() - Required field "metaType" is missing!');
4709
+ }
4710
+ if (!atom.metaId) {
4711
+ throw new MetaMissingException('Check::isotopeA() - Required field "metaId" is missing!');
4712
+ }
4713
+ const metas = atom.aggregatedMeta();
4714
+ if (!Object.prototype.hasOwnProperty.call(metas, "action") || !metas.action) {
4715
+ throw new MetaMissingException('Check::isotopeA() - Required meta field "action" is missing!');
4716
+ }
4717
+ }
4718
+ return true;
4719
+ }
4720
+ /**
4721
+ * Validate isotope B atoms (buffer/exchange)
4722
+ *
4723
+ * Buffer molecules are cross-isotope: their V atoms do not balance on their own
4724
+ * because a B atom absorbs the difference. isotopeV() therefore skips its V-only
4725
+ * conservation checks whenever B (or F) atoms are present, and conservation is
4726
+ * enforced here instead over the combined V+B set.
4727
+ *
4728
+ * @return True if valid
4729
+ * @throws MetaMissingException if metaType is not walletBundle, or metaId is absent
4730
+ * @throws TransferMalformedException if a B atom value is not a number
4731
+ * @throws TransferUnbalancedException if V+B atom values do not sum to zero
4732
+ */
4733
+ isotopeB() {
4734
+ const isotopeB = this.molecule.getIsotopes("B");
4735
+ if (isotopeB.length === 0) {
4736
+ return true;
4737
+ }
4738
+ for (const atom of isotopeB) {
4739
+ if (!atom.metaType || atom.metaType !== "walletBundle") {
4740
+ throw new MetaMissingException('Check::isotopeB() - B-isotope atoms must have metaType "walletBundle"!');
4741
+ }
4742
+ if (!atom.metaId) {
4743
+ throw new MetaMissingException("Check::isotopeB() - B-isotope atoms must have a metaId!");
4744
+ }
4745
+ const value = atom.value !== null ? Number(atom.value) : Number.NaN;
4746
+ if (Number.isNaN(value)) {
4747
+ throw new TransferMalformedException("Check::isotopeB() - B-isotope atom value is not a valid number!");
4748
+ }
4749
+ }
4750
+ const vAtoms = this.molecule.getIsotopes("V");
4751
+ if (vAtoms.length > 0) {
4752
+ let sum = 0;
4753
+ for (const atom of [...vAtoms, ...isotopeB]) {
4754
+ const value = atom.value !== null ? Number(atom.value) : Number.NaN;
4755
+ if (!Number.isNaN(value)) {
4756
+ sum += value;
4757
+ }
4758
+ }
4759
+ if (sum !== 0) {
4760
+ throw new TransferUnbalancedException("Check::isotopeB() - V+B atom values do not balance to zero!");
4761
+ }
4762
+ }
4763
+ return true;
4764
+ }
4765
+ /**
4766
+ * Validate isotope F atoms (fusion/NFT)
4767
+ *
4768
+ * Mirrors isotopeB() and additionally forbids negative values. Must stay paired
4769
+ * with the hasCrossIsotope gate in isotopeV(), which is keyed on B *or* F: without
4770
+ * this check an F-isotope molecule would skip V-only conservation with nothing
4771
+ * validating V+F conservation in its place.
4772
+ *
4773
+ * @return True if valid
4774
+ * @throws MetaMissingException if metaType is not walletBundle, or metaId is absent
4775
+ * @throws TransferMalformedException if an F atom value is not a number, or is negative
4776
+ * @throws TransferUnbalancedException if V+F atom values do not sum to zero
4777
+ */
4778
+ isotopeF() {
4779
+ const isotopeF = this.molecule.getIsotopes("F");
4780
+ if (isotopeF.length === 0) {
4781
+ return true;
4782
+ }
4783
+ for (const atom of isotopeF) {
4784
+ if (!atom.metaType || atom.metaType !== "walletBundle") {
4785
+ throw new MetaMissingException('Check::isotopeF() - F-isotope atoms must have metaType "walletBundle"!');
4786
+ }
4787
+ if (!atom.metaId) {
4788
+ throw new MetaMissingException("Check::isotopeF() - F-isotope atoms must have a metaId!");
4789
+ }
4790
+ const value = atom.value !== null ? Number(atom.value) : Number.NaN;
4791
+ if (Number.isNaN(value)) {
4792
+ throw new TransferMalformedException("Check::isotopeF() - F-isotope atom value is not a valid number!");
4793
+ }
4794
+ if (value < 0) {
4795
+ throw new TransferMalformedException("Check::isotopeF() - F-isotope atom value must not be negative!");
4796
+ }
4797
+ }
4798
+ const vAtoms = this.molecule.getIsotopes("V");
4799
+ if (vAtoms.length > 0) {
4800
+ let sum = 0;
4801
+ for (const atom of [...vAtoms, ...isotopeF]) {
4802
+ const value = atom.value !== null ? Number(atom.value) : Number.NaN;
4803
+ if (!Number.isNaN(value)) {
4804
+ sum += value;
4805
+ }
4806
+ }
4807
+ if (sum !== 0) {
4808
+ throw new TransferUnbalancedException("Check::isotopeF() - V+F atom values do not balance to zero!");
4809
+ }
4810
+ }
4811
+ return true;
4812
+ }
4678
4813
  /**
4679
4814
  * Validate isotope V atoms (value transfers)
4680
4815
  * @param senderWallet - Optional wallet for balance validation
@@ -4686,11 +4821,12 @@ var CheckMolecule = class _CheckMolecule {
4686
4821
  if (isotopeV.length === 0) {
4687
4822
  return true;
4688
4823
  }
4824
+ const hasCrossIsotope = this.molecule.getIsotopes("B").length > 0 || this.molecule.getIsotopes("F").length > 0;
4689
4825
  const firstAtom = this.molecule.atoms[0];
4690
4826
  if (!firstAtom) {
4691
4827
  throw new AtomsMissingException("Check::isotopeV() - Missing first atom");
4692
4828
  }
4693
- if (firstAtom.isotope === "V" && isotopeV.length === 2) {
4829
+ if (!hasCrossIsotope && firstAtom.isotope === "V" && isotopeV.length === 2) {
4694
4830
  const endAtom = isotopeV[isotopeV.length - 1];
4695
4831
  if (!endAtom) {
4696
4832
  throw new AtomsMissingException("Check::isotopeV() - Missing end atom");
@@ -4702,6 +4838,10 @@ var CheckMolecule = class _CheckMolecule {
4702
4838
  if (endValue < 0) {
4703
4839
  throw new TransferMalformedException();
4704
4840
  }
4841
+ const firstValueForPair = firstAtom.value !== null ? Number(firstAtom.value) : 0;
4842
+ if (firstValueForPair + endValue !== 0) {
4843
+ throw new TransferUnbalancedException();
4844
+ }
4705
4845
  return true;
4706
4846
  }
4707
4847
  let sum = 0;
@@ -4730,7 +4870,7 @@ var CheckMolecule = class _CheckMolecule {
4730
4870
  sum += value;
4731
4871
  }
4732
4872
  }
4733
- if (sum !== 0) {
4873
+ if (!hasCrossIsotope && sum !== 0) {
4734
4874
  throw new TransferUnbalancedException();
4735
4875
  }
4736
4876
  if (senderWallet) {
@@ -4742,7 +4882,7 @@ var CheckMolecule = class _CheckMolecule {
4742
4882
  if (remainder < 0) {
4743
4883
  throw new TransferBalanceException();
4744
4884
  }
4745
- if (remainder !== sum) {
4885
+ if (!hasCrossIsotope && remainder !== sum) {
4746
4886
  throw new TransferRemainderException();
4747
4887
  }
4748
4888
  } else if (sum !== 0) {