@wishknish/knishio-client-ts 1.2.1 → 1.3.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.
package/dist/index.js CHANGED
@@ -2928,24 +2928,6 @@ var AtomMeta = class _AtomMeta {
2928
2928
  this.merge({ shadowWalletClaim: Number(shadowWalletClaim) });
2929
2929
  return this;
2930
2930
  }
2931
- /**
2932
- * Set signing wallet metadata
2933
- * @param signingWallet - Wallet used for signing
2934
- * @return This instance for chaining
2935
- */
2936
- setSigningWallet(signingWallet) {
2937
- this.merge({
2938
- signingWallet: JSON.stringify({
2939
- tokenSlug: signingWallet.token,
2940
- bundleHash: signingWallet.bundle,
2941
- address: signingWallet.address,
2942
- position: signingWallet.position,
2943
- pubkey: signingWallet.pubkey,
2944
- characters: signingWallet.characters
2945
- })
2946
- });
2947
- return this;
2948
- }
2949
2931
  /**
2950
2932
  * Add policy metadata
2951
2933
  * @param policy - Policy object to add
@@ -4785,7 +4767,6 @@ var Rule = class _Rule {
4785
4767
 
4786
4768
  // src/libraries/CheckMolecule.ts
4787
4769
  init_exception();
4788
- init_Dot();
4789
4770
  var CheckMolecule = class _CheckMolecule {
4790
4771
  molecule;
4791
4772
  /**
@@ -4873,12 +4854,13 @@ var CheckMolecule = class _CheckMolecule {
4873
4854
  /**
4874
4855
  * Validate isotope U atoms
4875
4856
  * @return True if valid
4876
- * @throws WrongTokenTypeException if token is not AUTH
4857
+ * @throws WrongTokenTypeException if token is neither AUTH (fresh login) nor USER (re-login
4858
+ * signed from the ContinuID pointer)
4877
4859
  * @throws AtomIndexException if index is not 0
4878
4860
  */
4879
4861
  isotopeU() {
4880
4862
  for (const atom of this.molecule.getIsotopes("U")) {
4881
- if (atom.token !== "AUTH") {
4863
+ if (atom.token !== "AUTH" && atom.token !== "USER") {
4882
4864
  throw new WrongTokenTypeException(`Check::isotopeU() - "${atom.token}" is not a valid Token slug for "${atom.isotope}" isotope Atoms!`);
4883
4865
  }
4884
4866
  if (atom.index !== 0) {
@@ -5267,16 +5249,7 @@ var CheckMolecule = class _CheckMolecule {
5267
5249
  if (!signingAtom) {
5268
5250
  throw new SignatureMismatchException();
5269
5251
  }
5270
- let signingAddress = signingAtom.walletAddress;
5271
- const signingWallet = Dot.get(signingAtom.aggregatedMeta(), "signingWallet");
5272
- if (signingWallet) {
5273
- const parsedWallet = JSON.parse(signingWallet);
5274
- const walletAddress = Dot.get(parsedWallet, "address");
5275
- if (walletAddress) {
5276
- signingAddress = walletAddress;
5277
- }
5278
- }
5279
- if (address !== signingAddress) {
5252
+ if (address !== signingAtom.walletAddress) {
5280
5253
  throw new SignatureMismatchException();
5281
5254
  }
5282
5255
  return true;
@@ -5611,18 +5584,7 @@ var Molecule = class _Molecule {
5611
5584
  if (!signingAtom) {
5612
5585
  throw new SignatureMalformedException("No atoms available for signing!");
5613
5586
  }
5614
- let signingPosition = signingAtom.position;
5615
- const signingWalletMeta = signingAtom.aggregatedMeta ? signingAtom.aggregatedMeta() : null;
5616
- const signingWalletJson = signingWalletMeta?.signingWallet;
5617
- if (signingWalletJson) {
5618
- try {
5619
- const parsedSigningWallet = JSON.parse(signingWalletJson);
5620
- if (parsedSigningWallet.position) {
5621
- signingPosition = parsedSigningWallet.position;
5622
- }
5623
- } catch {
5624
- }
5625
- }
5587
+ const signingPosition = signingAtom.position;
5626
5588
  if (!signingPosition) {
5627
5589
  throw new SignatureMalformedException("Signing wallet must have a position!");
5628
5590
  }
@@ -6025,9 +5987,7 @@ var Molecule = class _Molecule {
6025
5987
  const atomMeta = new AtomMeta({
6026
5988
  rule: JSON.stringify(rules)
6027
5989
  });
6028
- if (policy && Object.keys(policy).length > 0) {
6029
- atomMeta.addPolicy(policy);
6030
- }
5990
+ atomMeta.addPolicy(policy ?? {});
6031
5991
  this.addAtom(Atom.create({
6032
5992
  isotope: "R",
6033
5993
  wallet: this.sourceWallet,
@@ -6280,12 +6240,10 @@ var Molecule = class _Molecule {
6280
6240
  * Matches JavaScript SDK Molecule.initWithdrawBuffer implementation exactly
6281
6241
  *
6282
6242
  * @param recipients - Map of recipientBundle → amount
6283
- * @param signingWallet - Optional signing wallet for reconciliation
6284
6243
  * @return This molecule instance for chaining
6285
6244
  */
6286
6245
  initWithdrawBuffer({
6287
- recipients,
6288
- signingWallet = null
6246
+ recipients
6289
6247
  }) {
6290
6248
  if (!this.sourceWallet) {
6291
6249
  throw new Error("Source wallet required for buffer withdrawal");
@@ -6297,15 +6255,10 @@ var Molecule = class _Molecule {
6297
6255
  if (Number(this.sourceWallet.balance) - amount < 0) {
6298
6256
  throw new BalanceInsufficientException();
6299
6257
  }
6300
- const firstAtomMeta = new AtomMeta();
6301
- if (signingWallet) {
6302
- firstAtomMeta.setSigningWallet(signingWallet);
6303
- }
6304
6258
  this.addAtom(Atom.create({
6305
6259
  isotope: "B",
6306
6260
  wallet: this.sourceWallet,
6307
6261
  value: -Number(this.sourceWallet.balance),
6308
- meta: firstAtomMeta,
6309
6262
  metaType: "walletBundle",
6310
6263
  metaId: this.sourceWallet.bundle
6311
6264
  }));
@@ -6740,7 +6693,7 @@ var AuthToken = class _AuthToken {
6740
6693
  static restore(snapshot, secret) {
6741
6694
  const wallet = new Wallet({
6742
6695
  secret,
6743
- token: "AUTH",
6696
+ token: snapshot.wallet?.token ?? "AUTH",
6744
6697
  position: snapshot.wallet?.position ?? null,
6745
6698
  characters: snapshot.wallet?.characters ?? null,
6746
6699
  mlKemParameterSet: _AuthToken.resolveMlKemParameterSet(snapshot)
@@ -6823,6 +6776,7 @@ var AuthToken = class _AuthToken {
6823
6776
  encrypt: this.$__encrypt,
6824
6777
  ...this.$__wallet ? {
6825
6778
  wallet: {
6779
+ token: this.$__wallet.token,
6826
6780
  position: this.$__wallet.position,
6827
6781
  characters: this.$__wallet.characters,
6828
6782
  mlKemParameterSet: this.$__wallet.mlKemParameterSet
@@ -8311,8 +8265,8 @@ var QueryContinuId = class extends Query {
8311
8265
  */
8312
8266
  constructor(graphQLClient, knishIOClient) {
8313
8267
  super(graphQLClient, knishIOClient);
8314
- this.$__query = gql`query ($bundle: String!) {
8315
- ContinuId(bundle: $bundle) {
8268
+ this.$__query = gql`query ($bundle: String!, $token: String) {
8269
+ ContinuId(bundle: $bundle, token: $token) {
8316
8270
  address,
8317
8271
  bundleHash,
8318
8272
  tokenSlug,
@@ -9616,6 +9570,18 @@ var ResponseProposeMolecule = class extends Response2 {
9616
9570
  code: "VERIFICATION_FAILED"
9617
9571
  });
9618
9572
  }
9573
+ if (/signer address mismatch/.test(lc)) {
9574
+ return new SignatureMismatchException(reason, {
9575
+ details: { reason },
9576
+ code: "SIGNER_ADDRESS_MISMATCH"
9577
+ });
9578
+ }
9579
+ if (/no accepted origin available/.test(lc)) {
9580
+ return new AtomIndexException(reason, {
9581
+ details: { reason },
9582
+ code: "ORIGIN_PENDING"
9583
+ });
9584
+ }
9619
9585
  return null;
9620
9586
  }
9621
9587
  /**
@@ -10113,12 +10079,10 @@ var MutationWithdrawBufferToken = class extends MutationProposeMolecule {
10113
10079
  * Fills the molecule with buffer withdrawal data
10114
10080
  */
10115
10081
  fillMolecule({
10116
- recipients,
10117
- signingWallet
10082
+ recipients
10118
10083
  }) {
10119
10084
  this.$__molecule.initWithdrawBuffer({
10120
- recipients,
10121
- signingWallet
10085
+ recipients
10122
10086
  });
10123
10087
  this.$__molecule.sign({});
10124
10088
  this.$__molecule.check(this.$__molecule.sourceWallet);
@@ -11604,24 +11568,27 @@ var KnishIOClient = class {
11604
11568
  this.setCellSlug(cellSlug);
11605
11569
  }
11606
11570
  this.$__authInProcess = true;
11607
- let response;
11608
- if (secret) {
11609
- response = await this.requestProfileAuthToken({
11610
- secret,
11611
- encrypt
11612
- });
11613
- } else {
11614
- response = await this.requestGuestAuthToken({
11615
- cellSlug: cellSlug || this.getCellSlug(),
11616
- encrypt
11617
- });
11618
- }
11619
- if (this.$__authToken) {
11620
- this.log("info", `KnishIOClient::requestAuthToken() - Successfully retrieved auth token...`);
11571
+ try {
11572
+ let response;
11573
+ if (secret) {
11574
+ response = await this.requestProfileAuthToken({
11575
+ secret,
11576
+ encrypt
11577
+ });
11578
+ } else {
11579
+ response = await this.requestGuestAuthToken({
11580
+ cellSlug: cellSlug || this.getCellSlug(),
11581
+ encrypt
11582
+ });
11583
+ }
11584
+ if (this.$__authToken) {
11585
+ this.log("info", `KnishIOClient::requestAuthToken() - Successfully retrieved auth token...`);
11586
+ }
11587
+ this.switchEncryption(encrypt);
11588
+ return response;
11589
+ } finally {
11590
+ this.$__authInProcess = false;
11621
11591
  }
11622
- this.switchEncryption(encrypt);
11623
- this.$__authInProcess = false;
11624
- return response;
11625
11592
  }
11626
11593
  /**
11627
11594
  * Logging utility
@@ -11765,14 +11732,16 @@ var KnishIOClient = class {
11765
11732
  }
11766
11733
  /**
11767
11734
  * Query next ContinuId wallet
11735
+ *
11736
+ * @param token - Restricts the lookup to wallets of this token. Without it the validator falls
11737
+ * back to the bundle's newest wallet of ANY token when the bundle has no ContinuID pointer.
11768
11738
  */
11769
11739
  async queryContinuId({
11770
- bundle
11740
+ bundle,
11741
+ token = null
11771
11742
  }) {
11772
11743
  const query = this.createQuery(QueryContinuId);
11773
- return this.executeQuery(query, {
11774
- bundle
11775
- });
11744
+ return this.executeQuery(query, token ? { bundle, token } : { bundle });
11776
11745
  }
11777
11746
  /**
11778
11747
  * Query metadata
@@ -12589,8 +12558,7 @@ var KnishIOClient = class {
12589
12558
  async withdrawBufferToken({
12590
12559
  tokenSlug,
12591
12560
  amount,
12592
- sourceWallet = null,
12593
- signingWallet = null
12561
+ sourceWallet = null
12594
12562
  }) {
12595
12563
  this.log("info", `KnishIOClient::withdrawBufferToken() - Withdrawing ${amount} of ${tokenSlug} from buffer...`);
12596
12564
  if (!sourceWallet) {
@@ -12599,11 +12567,7 @@ var KnishIOClient = class {
12599
12567
  const mutation = await this.createMoleculeMutation({ mutationClass: MutationWithdrawBufferToken });
12600
12568
  const recipients = {};
12601
12569
  recipients[this.getBundle()] = amount;
12602
- const fillMoleculeParams = { recipients };
12603
- if (signingWallet) {
12604
- fillMoleculeParams.signingWallet = signingWallet;
12605
- }
12606
- await mutation.fillMolecule(fillMoleculeParams);
12570
+ await mutation.fillMolecule({ recipients });
12607
12571
  const response = await this.executeQuery(mutation);
12608
12572
  if (!response) {
12609
12573
  throw new CodeException("Buffer token withdrawal failed");
@@ -12621,21 +12585,33 @@ var KnishIOClient = class {
12621
12585
  encrypt = false
12622
12586
  } = {}) {
12623
12587
  this.log("info", "KnishIOClient::requestGuestAuthToken() - Requesting guest auth token...");
12624
- const mutation = this.createQuery(MutationRequestAuthorizationGuest);
12625
- const response = await this.executeQuery(mutation, {
12626
- cellSlug: cellSlug || this.getCellSlug()
12627
- });
12628
- if (!response) {
12629
- throw new CodeException("Guest auth token request failed");
12630
- }
12631
- if (encrypt) {
12632
- this.switchEncryption(true);
12588
+ const authInProcess = this.$__authInProcess;
12589
+ this.$__authInProcess = true;
12590
+ try {
12591
+ const mutation = this.createQuery(MutationRequestAuthorizationGuest);
12592
+ const response = await this.executeQuery(mutation, {
12593
+ cellSlug: cellSlug || this.getCellSlug()
12594
+ });
12595
+ if (!response) {
12596
+ throw new CodeException("Guest auth token request failed");
12597
+ }
12598
+ if (encrypt) {
12599
+ this.switchEncryption(true);
12600
+ }
12601
+ return response;
12602
+ } finally {
12603
+ this.$__authInProcess = authInProcess;
12633
12604
  }
12634
- return response;
12635
12605
  }
12636
12606
  /**
12637
12607
  * Request profile authentication token
12638
- * Matches JavaScript SDK requestProfileAuthToken implementation
12608
+ *
12609
+ * A returning user (the bundle has a USER ContinuID pointer) signs from the USER wallet
12610
+ * registered at that pointer, so validator 0.5.0+ marks the token proven and the identity keeps
12611
+ * read/subscription access to permissioned and private cells. A first login — or a pointer this
12612
+ * secret does not own — signs from a fresh AUTH wallet as before. A rejected pointer-signed
12613
+ * login falls back to the AUTH login exactly once, so one call sends at most two
12614
+ * authorization molecules (testnet allows 3 auths/min/IP).
12639
12615
  */
12640
12616
  async requestProfileAuthToken({
12641
12617
  secret,
@@ -12643,13 +12619,58 @@ var KnishIOClient = class {
12643
12619
  }) {
12644
12620
  this.log("info", "KnishIOClient::requestProfileAuthToken() - Requesting profile auth token...");
12645
12621
  this.setSecret(secret);
12646
- const wallet = new Wallet({
12647
- secret,
12648
- token: "AUTH",
12649
- mlKemParameterSet: this.getMlKemParameterSet()
12650
- });
12622
+ const authInProcess = this.$__authInProcess;
12623
+ this.$__authInProcess = true;
12624
+ try {
12625
+ const pointer = (await this.queryContinuId({
12626
+ bundle: this.getBundle(),
12627
+ token: "USER"
12628
+ }))?.payload();
12629
+ let pointerWallet = null;
12630
+ if (pointer && pointer.token === "USER" && pointer.position) {
12631
+ const wallet2 = new Wallet({
12632
+ secret,
12633
+ token: "USER",
12634
+ position: pointer.position,
12635
+ mlKemParameterSet: this.getMlKemParameterSet()
12636
+ });
12637
+ if (!pointer.address || wallet2.address === pointer.address) {
12638
+ pointerWallet = wallet2;
12639
+ } else {
12640
+ this.log("warn", "KnishIOClient::requestProfileAuthToken() - ContinuID wallet address does not derive from this secret; signing from a fresh AUTH wallet");
12641
+ }
12642
+ }
12643
+ if (pointerWallet) {
12644
+ const response2 = await this.proposeProfileAuthorization(pointerWallet, encrypt);
12645
+ if (response2.success()) {
12646
+ this.log("info", "KnishIOClient::requestProfileAuthToken() - Auth token signed from the ContinuID pointer (proven)");
12647
+ return this.acceptProfileAuthorization(response2, pointerWallet, encrypt);
12648
+ }
12649
+ this.log("warn", `KnishIOClient::requestProfileAuthToken() - Pointer-signed authorization rejected (${response2.reason()}); falling back once to an AUTH-wallet login`);
12650
+ }
12651
+ const wallet = new Wallet({
12652
+ secret,
12653
+ token: "AUTH",
12654
+ mlKemParameterSet: this.getMlKemParameterSet()
12655
+ });
12656
+ const response = await this.proposeProfileAuthorization(wallet, encrypt);
12657
+ if (!response.success()) {
12658
+ throw new AuthorizationRejectedException_default(
12659
+ `KnishIOClient::requestProfileAuthToken() - Authorization attempt rejected by ledger. Reason: ${response.reason()}`
12660
+ );
12661
+ }
12662
+ this.log("info", `KnishIOClient::requestProfileAuthToken() - Auth token signed from a fresh AUTH wallet (${pointerWallet ? "fallback after the pointer-signed login was rejected" : "no usable ContinuID pointer"})`);
12663
+ return this.acceptProfileAuthorization(response, wallet, encrypt);
12664
+ } finally {
12665
+ this.$__authInProcess = authInProcess;
12666
+ }
12667
+ }
12668
+ /**
12669
+ * Signs and proposes a profile authorization molecule from the given source wallet.
12670
+ */
12671
+ async proposeProfileAuthorization(wallet, encrypt) {
12651
12672
  const molecule = await this.createMolecule({
12652
- secret,
12673
+ secret: this.getSecret(),
12653
12674
  sourceWallet: wallet
12654
12675
  });
12655
12676
  const mutation = await this.createMoleculeMutation({
@@ -12667,20 +12688,20 @@ var KnishIOClient = class {
12667
12688
  if (!response) {
12668
12689
  throw new CodeException("Profile auth token request failed");
12669
12690
  }
12670
- if (response.success()) {
12671
- const authToken = AuthToken.create({
12672
- token: response.token(),
12673
- expiresAt: response.expiresAt(),
12674
- encrypt: String(response.encrypt()) === "true",
12675
- pubkey: response.pubKey()
12676
- }, wallet);
12677
- this.setAuthToken(authToken);
12678
- this.lastMoleculeQuery = null;
12679
- } else {
12680
- throw new AuthorizationRejectedException_default(
12681
- `KnishIOClient::requestProfileAuthToken() - Authorization attempt rejected by ledger. Reason: ${response.reason()}`
12682
- );
12683
- }
12691
+ return response;
12692
+ }
12693
+ /**
12694
+ * Binds the accepted authorization's token to the wallet that signed it.
12695
+ */
12696
+ acceptProfileAuthorization(response, wallet, encrypt) {
12697
+ const authToken = AuthToken.create({
12698
+ token: response.token(),
12699
+ expiresAt: response.expiresAt(),
12700
+ encrypt: String(response.encrypt()) === "true",
12701
+ pubkey: response.pubKey()
12702
+ }, wallet);
12703
+ this.setAuthToken(authToken);
12704
+ this.lastMoleculeQuery = null;
12684
12705
  if (encrypt) {
12685
12706
  this.switchEncryption(true);
12686
12707
  }
@@ -14038,7 +14059,7 @@ function createDefaultSecretStorage(options = {}) {
14038
14059
  }
14039
14060
 
14040
14061
  // src/index.ts
14041
- var SDK_VERSION = "1.2.1";
14062
+ var SDK_VERSION = "1.3.0";
14042
14063
  var SDK_NAME = "KnishIO-Client-TS";
14043
14064
  var COMPATIBLE_SERVER_VERSIONS = [4, 5];
14044
14065
  var SDK_INFO = {