@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.cjs CHANGED
@@ -2934,24 +2934,6 @@ var AtomMeta = class _AtomMeta {
2934
2934
  this.merge({ shadowWalletClaim: Number(shadowWalletClaim) });
2935
2935
  return this;
2936
2936
  }
2937
- /**
2938
- * Set signing wallet metadata
2939
- * @param signingWallet - Wallet used for signing
2940
- * @return This instance for chaining
2941
- */
2942
- setSigningWallet(signingWallet) {
2943
- this.merge({
2944
- signingWallet: JSON.stringify({
2945
- tokenSlug: signingWallet.token,
2946
- bundleHash: signingWallet.bundle,
2947
- address: signingWallet.address,
2948
- position: signingWallet.position,
2949
- pubkey: signingWallet.pubkey,
2950
- characters: signingWallet.characters
2951
- })
2952
- });
2953
- return this;
2954
- }
2955
2937
  /**
2956
2938
  * Add policy metadata
2957
2939
  * @param policy - Policy object to add
@@ -4791,7 +4773,6 @@ var Rule = class _Rule {
4791
4773
 
4792
4774
  // src/libraries/CheckMolecule.ts
4793
4775
  init_exception();
4794
- init_Dot();
4795
4776
  var CheckMolecule = class _CheckMolecule {
4796
4777
  molecule;
4797
4778
  /**
@@ -4879,12 +4860,13 @@ var CheckMolecule = class _CheckMolecule {
4879
4860
  /**
4880
4861
  * Validate isotope U atoms
4881
4862
  * @return True if valid
4882
- * @throws WrongTokenTypeException if token is not AUTH
4863
+ * @throws WrongTokenTypeException if token is neither AUTH (fresh login) nor USER (re-login
4864
+ * signed from the ContinuID pointer)
4883
4865
  * @throws AtomIndexException if index is not 0
4884
4866
  */
4885
4867
  isotopeU() {
4886
4868
  for (const atom of this.molecule.getIsotopes("U")) {
4887
- if (atom.token !== "AUTH") {
4869
+ if (atom.token !== "AUTH" && atom.token !== "USER") {
4888
4870
  throw new WrongTokenTypeException(`Check::isotopeU() - "${atom.token}" is not a valid Token slug for "${atom.isotope}" isotope Atoms!`);
4889
4871
  }
4890
4872
  if (atom.index !== 0) {
@@ -5273,16 +5255,7 @@ var CheckMolecule = class _CheckMolecule {
5273
5255
  if (!signingAtom) {
5274
5256
  throw new exports.SignatureMismatchException();
5275
5257
  }
5276
- let signingAddress = signingAtom.walletAddress;
5277
- const signingWallet = exports.Dot.get(signingAtom.aggregatedMeta(), "signingWallet");
5278
- if (signingWallet) {
5279
- const parsedWallet = JSON.parse(signingWallet);
5280
- const walletAddress = exports.Dot.get(parsedWallet, "address");
5281
- if (walletAddress) {
5282
- signingAddress = walletAddress;
5283
- }
5284
- }
5285
- if (address !== signingAddress) {
5258
+ if (address !== signingAtom.walletAddress) {
5286
5259
  throw new exports.SignatureMismatchException();
5287
5260
  }
5288
5261
  return true;
@@ -5617,18 +5590,7 @@ var Molecule = class _Molecule {
5617
5590
  if (!signingAtom) {
5618
5591
  throw new SignatureMalformedException("No atoms available for signing!");
5619
5592
  }
5620
- let signingPosition = signingAtom.position;
5621
- const signingWalletMeta = signingAtom.aggregatedMeta ? signingAtom.aggregatedMeta() : null;
5622
- const signingWalletJson = signingWalletMeta?.signingWallet;
5623
- if (signingWalletJson) {
5624
- try {
5625
- const parsedSigningWallet = JSON.parse(signingWalletJson);
5626
- if (parsedSigningWallet.position) {
5627
- signingPosition = parsedSigningWallet.position;
5628
- }
5629
- } catch {
5630
- }
5631
- }
5593
+ const signingPosition = signingAtom.position;
5632
5594
  if (!signingPosition) {
5633
5595
  throw new SignatureMalformedException("Signing wallet must have a position!");
5634
5596
  }
@@ -6031,9 +5993,7 @@ var Molecule = class _Molecule {
6031
5993
  const atomMeta = new AtomMeta({
6032
5994
  rule: JSON.stringify(rules)
6033
5995
  });
6034
- if (policy && Object.keys(policy).length > 0) {
6035
- atomMeta.addPolicy(policy);
6036
- }
5996
+ atomMeta.addPolicy(policy ?? {});
6037
5997
  this.addAtom(Atom.create({
6038
5998
  isotope: "R",
6039
5999
  wallet: this.sourceWallet,
@@ -6286,12 +6246,10 @@ var Molecule = class _Molecule {
6286
6246
  * Matches JavaScript SDK Molecule.initWithdrawBuffer implementation exactly
6287
6247
  *
6288
6248
  * @param recipients - Map of recipientBundle → amount
6289
- * @param signingWallet - Optional signing wallet for reconciliation
6290
6249
  * @return This molecule instance for chaining
6291
6250
  */
6292
6251
  initWithdrawBuffer({
6293
- recipients,
6294
- signingWallet = null
6252
+ recipients
6295
6253
  }) {
6296
6254
  if (!this.sourceWallet) {
6297
6255
  throw new Error("Source wallet required for buffer withdrawal");
@@ -6303,15 +6261,10 @@ var Molecule = class _Molecule {
6303
6261
  if (Number(this.sourceWallet.balance) - amount < 0) {
6304
6262
  throw new BalanceInsufficientException();
6305
6263
  }
6306
- const firstAtomMeta = new AtomMeta();
6307
- if (signingWallet) {
6308
- firstAtomMeta.setSigningWallet(signingWallet);
6309
- }
6310
6264
  this.addAtom(Atom.create({
6311
6265
  isotope: "B",
6312
6266
  wallet: this.sourceWallet,
6313
6267
  value: -Number(this.sourceWallet.balance),
6314
- meta: firstAtomMeta,
6315
6268
  metaType: "walletBundle",
6316
6269
  metaId: this.sourceWallet.bundle
6317
6270
  }));
@@ -6746,7 +6699,7 @@ var AuthToken = class _AuthToken {
6746
6699
  static restore(snapshot, secret) {
6747
6700
  const wallet = new Wallet({
6748
6701
  secret,
6749
- token: "AUTH",
6702
+ token: snapshot.wallet?.token ?? "AUTH",
6750
6703
  position: snapshot.wallet?.position ?? null,
6751
6704
  characters: snapshot.wallet?.characters ?? null,
6752
6705
  mlKemParameterSet: _AuthToken.resolveMlKemParameterSet(snapshot)
@@ -6829,6 +6782,7 @@ var AuthToken = class _AuthToken {
6829
6782
  encrypt: this.$__encrypt,
6830
6783
  ...this.$__wallet ? {
6831
6784
  wallet: {
6785
+ token: this.$__wallet.token,
6832
6786
  position: this.$__wallet.position,
6833
6787
  characters: this.$__wallet.characters,
6834
6788
  mlKemParameterSet: this.$__wallet.mlKemParameterSet
@@ -8317,8 +8271,8 @@ var QueryContinuId = class extends Query {
8317
8271
  */
8318
8272
  constructor(graphQLClient, knishIOClient) {
8319
8273
  super(graphQLClient, knishIOClient);
8320
- this.$__query = core.gql`query ($bundle: String!) {
8321
- ContinuId(bundle: $bundle) {
8274
+ this.$__query = core.gql`query ($bundle: String!, $token: String) {
8275
+ ContinuId(bundle: $bundle, token: $token) {
8322
8276
  address,
8323
8277
  bundleHash,
8324
8278
  tokenSlug,
@@ -9622,6 +9576,18 @@ var ResponseProposeMolecule = class extends exports.Response {
9622
9576
  code: "VERIFICATION_FAILED"
9623
9577
  });
9624
9578
  }
9579
+ if (/signer address mismatch/.test(lc)) {
9580
+ return new exports.SignatureMismatchException(reason, {
9581
+ details: { reason },
9582
+ code: "SIGNER_ADDRESS_MISMATCH"
9583
+ });
9584
+ }
9585
+ if (/no accepted origin available/.test(lc)) {
9586
+ return new exports.AtomIndexException(reason, {
9587
+ details: { reason },
9588
+ code: "ORIGIN_PENDING"
9589
+ });
9590
+ }
9625
9591
  return null;
9626
9592
  }
9627
9593
  /**
@@ -10119,12 +10085,10 @@ var MutationWithdrawBufferToken = class extends MutationProposeMolecule {
10119
10085
  * Fills the molecule with buffer withdrawal data
10120
10086
  */
10121
10087
  fillMolecule({
10122
- recipients,
10123
- signingWallet
10088
+ recipients
10124
10089
  }) {
10125
10090
  this.$__molecule.initWithdrawBuffer({
10126
- recipients,
10127
- signingWallet
10091
+ recipients
10128
10092
  });
10129
10093
  this.$__molecule.sign({});
10130
10094
  this.$__molecule.check(this.$__molecule.sourceWallet);
@@ -11610,24 +11574,27 @@ var KnishIOClient = class {
11610
11574
  this.setCellSlug(cellSlug);
11611
11575
  }
11612
11576
  this.$__authInProcess = true;
11613
- let response;
11614
- if (secret) {
11615
- response = await this.requestProfileAuthToken({
11616
- secret,
11617
- encrypt
11618
- });
11619
- } else {
11620
- response = await this.requestGuestAuthToken({
11621
- cellSlug: cellSlug || this.getCellSlug(),
11622
- encrypt
11623
- });
11624
- }
11625
- if (this.$__authToken) {
11626
- this.log("info", `KnishIOClient::requestAuthToken() - Successfully retrieved auth token...`);
11577
+ try {
11578
+ let response;
11579
+ if (secret) {
11580
+ response = await this.requestProfileAuthToken({
11581
+ secret,
11582
+ encrypt
11583
+ });
11584
+ } else {
11585
+ response = await this.requestGuestAuthToken({
11586
+ cellSlug: cellSlug || this.getCellSlug(),
11587
+ encrypt
11588
+ });
11589
+ }
11590
+ if (this.$__authToken) {
11591
+ this.log("info", `KnishIOClient::requestAuthToken() - Successfully retrieved auth token...`);
11592
+ }
11593
+ this.switchEncryption(encrypt);
11594
+ return response;
11595
+ } finally {
11596
+ this.$__authInProcess = false;
11627
11597
  }
11628
- this.switchEncryption(encrypt);
11629
- this.$__authInProcess = false;
11630
- return response;
11631
11598
  }
11632
11599
  /**
11633
11600
  * Logging utility
@@ -11771,14 +11738,16 @@ var KnishIOClient = class {
11771
11738
  }
11772
11739
  /**
11773
11740
  * Query next ContinuId wallet
11741
+ *
11742
+ * @param token - Restricts the lookup to wallets of this token. Without it the validator falls
11743
+ * back to the bundle's newest wallet of ANY token when the bundle has no ContinuID pointer.
11774
11744
  */
11775
11745
  async queryContinuId({
11776
- bundle
11746
+ bundle,
11747
+ token = null
11777
11748
  }) {
11778
11749
  const query = this.createQuery(QueryContinuId);
11779
- return this.executeQuery(query, {
11780
- bundle
11781
- });
11750
+ return this.executeQuery(query, token ? { bundle, token } : { bundle });
11782
11751
  }
11783
11752
  /**
11784
11753
  * Query metadata
@@ -12595,8 +12564,7 @@ var KnishIOClient = class {
12595
12564
  async withdrawBufferToken({
12596
12565
  tokenSlug,
12597
12566
  amount,
12598
- sourceWallet = null,
12599
- signingWallet = null
12567
+ sourceWallet = null
12600
12568
  }) {
12601
12569
  this.log("info", `KnishIOClient::withdrawBufferToken() - Withdrawing ${amount} of ${tokenSlug} from buffer...`);
12602
12570
  if (!sourceWallet) {
@@ -12605,11 +12573,7 @@ var KnishIOClient = class {
12605
12573
  const mutation = await this.createMoleculeMutation({ mutationClass: MutationWithdrawBufferToken });
12606
12574
  const recipients = {};
12607
12575
  recipients[this.getBundle()] = amount;
12608
- const fillMoleculeParams = { recipients };
12609
- if (signingWallet) {
12610
- fillMoleculeParams.signingWallet = signingWallet;
12611
- }
12612
- await mutation.fillMolecule(fillMoleculeParams);
12576
+ await mutation.fillMolecule({ recipients });
12613
12577
  const response = await this.executeQuery(mutation);
12614
12578
  if (!response) {
12615
12579
  throw new CodeException("Buffer token withdrawal failed");
@@ -12627,21 +12591,33 @@ var KnishIOClient = class {
12627
12591
  encrypt = false
12628
12592
  } = {}) {
12629
12593
  this.log("info", "KnishIOClient::requestGuestAuthToken() - Requesting guest auth token...");
12630
- const mutation = this.createQuery(MutationRequestAuthorizationGuest);
12631
- const response = await this.executeQuery(mutation, {
12632
- cellSlug: cellSlug || this.getCellSlug()
12633
- });
12634
- if (!response) {
12635
- throw new CodeException("Guest auth token request failed");
12636
- }
12637
- if (encrypt) {
12638
- this.switchEncryption(true);
12594
+ const authInProcess = this.$__authInProcess;
12595
+ this.$__authInProcess = true;
12596
+ try {
12597
+ const mutation = this.createQuery(MutationRequestAuthorizationGuest);
12598
+ const response = await this.executeQuery(mutation, {
12599
+ cellSlug: cellSlug || this.getCellSlug()
12600
+ });
12601
+ if (!response) {
12602
+ throw new CodeException("Guest auth token request failed");
12603
+ }
12604
+ if (encrypt) {
12605
+ this.switchEncryption(true);
12606
+ }
12607
+ return response;
12608
+ } finally {
12609
+ this.$__authInProcess = authInProcess;
12639
12610
  }
12640
- return response;
12641
12611
  }
12642
12612
  /**
12643
12613
  * Request profile authentication token
12644
- * Matches JavaScript SDK requestProfileAuthToken implementation
12614
+ *
12615
+ * A returning user (the bundle has a USER ContinuID pointer) signs from the USER wallet
12616
+ * registered at that pointer, so validator 0.5.0+ marks the token proven and the identity keeps
12617
+ * read/subscription access to permissioned and private cells. A first login — or a pointer this
12618
+ * secret does not own — signs from a fresh AUTH wallet as before. A rejected pointer-signed
12619
+ * login falls back to the AUTH login exactly once, so one call sends at most two
12620
+ * authorization molecules (testnet allows 3 auths/min/IP).
12645
12621
  */
12646
12622
  async requestProfileAuthToken({
12647
12623
  secret,
@@ -12649,13 +12625,58 @@ var KnishIOClient = class {
12649
12625
  }) {
12650
12626
  this.log("info", "KnishIOClient::requestProfileAuthToken() - Requesting profile auth token...");
12651
12627
  this.setSecret(secret);
12652
- const wallet = new Wallet({
12653
- secret,
12654
- token: "AUTH",
12655
- mlKemParameterSet: this.getMlKemParameterSet()
12656
- });
12628
+ const authInProcess = this.$__authInProcess;
12629
+ this.$__authInProcess = true;
12630
+ try {
12631
+ const pointer = (await this.queryContinuId({
12632
+ bundle: this.getBundle(),
12633
+ token: "USER"
12634
+ }))?.payload();
12635
+ let pointerWallet = null;
12636
+ if (pointer && pointer.token === "USER" && pointer.position) {
12637
+ const wallet2 = new Wallet({
12638
+ secret,
12639
+ token: "USER",
12640
+ position: pointer.position,
12641
+ mlKemParameterSet: this.getMlKemParameterSet()
12642
+ });
12643
+ if (!pointer.address || wallet2.address === pointer.address) {
12644
+ pointerWallet = wallet2;
12645
+ } else {
12646
+ this.log("warn", "KnishIOClient::requestProfileAuthToken() - ContinuID wallet address does not derive from this secret; signing from a fresh AUTH wallet");
12647
+ }
12648
+ }
12649
+ if (pointerWallet) {
12650
+ const response2 = await this.proposeProfileAuthorization(pointerWallet, encrypt);
12651
+ if (response2.success()) {
12652
+ this.log("info", "KnishIOClient::requestProfileAuthToken() - Auth token signed from the ContinuID pointer (proven)");
12653
+ return this.acceptProfileAuthorization(response2, pointerWallet, encrypt);
12654
+ }
12655
+ this.log("warn", `KnishIOClient::requestProfileAuthToken() - Pointer-signed authorization rejected (${response2.reason()}); falling back once to an AUTH-wallet login`);
12656
+ }
12657
+ const wallet = new Wallet({
12658
+ secret,
12659
+ token: "AUTH",
12660
+ mlKemParameterSet: this.getMlKemParameterSet()
12661
+ });
12662
+ const response = await this.proposeProfileAuthorization(wallet, encrypt);
12663
+ if (!response.success()) {
12664
+ throw new AuthorizationRejectedException_default(
12665
+ `KnishIOClient::requestProfileAuthToken() - Authorization attempt rejected by ledger. Reason: ${response.reason()}`
12666
+ );
12667
+ }
12668
+ 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"})`);
12669
+ return this.acceptProfileAuthorization(response, wallet, encrypt);
12670
+ } finally {
12671
+ this.$__authInProcess = authInProcess;
12672
+ }
12673
+ }
12674
+ /**
12675
+ * Signs and proposes a profile authorization molecule from the given source wallet.
12676
+ */
12677
+ async proposeProfileAuthorization(wallet, encrypt) {
12657
12678
  const molecule = await this.createMolecule({
12658
- secret,
12679
+ secret: this.getSecret(),
12659
12680
  sourceWallet: wallet
12660
12681
  });
12661
12682
  const mutation = await this.createMoleculeMutation({
@@ -12673,20 +12694,20 @@ var KnishIOClient = class {
12673
12694
  if (!response) {
12674
12695
  throw new CodeException("Profile auth token request failed");
12675
12696
  }
12676
- if (response.success()) {
12677
- const authToken = AuthToken.create({
12678
- token: response.token(),
12679
- expiresAt: response.expiresAt(),
12680
- encrypt: String(response.encrypt()) === "true",
12681
- pubkey: response.pubKey()
12682
- }, wallet);
12683
- this.setAuthToken(authToken);
12684
- this.lastMoleculeQuery = null;
12685
- } else {
12686
- throw new AuthorizationRejectedException_default(
12687
- `KnishIOClient::requestProfileAuthToken() - Authorization attempt rejected by ledger. Reason: ${response.reason()}`
12688
- );
12689
- }
12697
+ return response;
12698
+ }
12699
+ /**
12700
+ * Binds the accepted authorization's token to the wallet that signed it.
12701
+ */
12702
+ acceptProfileAuthorization(response, wallet, encrypt) {
12703
+ const authToken = AuthToken.create({
12704
+ token: response.token(),
12705
+ expiresAt: response.expiresAt(),
12706
+ encrypt: String(response.encrypt()) === "true",
12707
+ pubkey: response.pubKey()
12708
+ }, wallet);
12709
+ this.setAuthToken(authToken);
12710
+ this.lastMoleculeQuery = null;
12690
12711
  if (encrypt) {
12691
12712
  this.switchEncryption(true);
12692
12713
  }
@@ -14044,7 +14065,7 @@ function createDefaultSecretStorage(options = {}) {
14044
14065
  }
14045
14066
 
14046
14067
  // src/index.ts
14047
- var SDK_VERSION = "1.2.1";
14068
+ var SDK_VERSION = "1.3.0";
14048
14069
  var SDK_NAME = "KnishIO-Client-TS";
14049
14070
  var COMPATIBLE_SERVER_VERSIONS = [4, 5];
14050
14071
  var SDK_INFO = {