@wishknish/knishio-client-ts 0.9.4 → 0.9.6

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
@@ -2178,11 +2178,9 @@ function verifyOTSSignature(otsFragments, molecularHash, signingAddress) {
2178
2178
  const base17Hash = convertToBase17(molecularHash);
2179
2179
  const enumerated = enumerateMolecularHash(base17Hash);
2180
2180
  const normalized = normalizeMolecularHash(enumerated);
2181
- let ots = otsFragments;
2181
+ const ots = otsFragments;
2182
2182
  if (ots.length !== 2048) {
2183
- if (ots.length !== 2048) {
2184
- return false;
2185
- }
2183
+ return false;
2186
2184
  }
2187
2185
  const otsChunks = [];
2188
2186
  for (let i = 0; i < ots.length; i += CRYPTO_CONSTANTS.KEY_FRAGMENT_SIZE) {
@@ -2685,6 +2683,123 @@ var Meta = class {
2685
2683
  });
2686
2684
  }
2687
2685
  };
2686
+
2687
+ // src/libraries/array.ts
2688
+ function deepCloning(o, h) {
2689
+ let i;
2690
+ let r;
2691
+ let x;
2692
+ const t = [Array, Date, Number, String, Boolean];
2693
+ const s = Object.prototype.toString;
2694
+ h = h || [];
2695
+ for (i = 0; i < h.length; i += 2) {
2696
+ if (o === h[i]) {
2697
+ return h[i + 1];
2698
+ }
2699
+ }
2700
+ if (!r && o && typeof o === "object") {
2701
+ r = {};
2702
+ for (i = 0; i < t.length; i++) {
2703
+ if (s.call(o) === s.call(x = new t[i](o))) {
2704
+ r = i ? x : [];
2705
+ }
2706
+ }
2707
+ h.push(o, r);
2708
+ for (i in o) {
2709
+ if (Object.prototype.hasOwnProperty.call(o, i)) {
2710
+ r[i] = deepCloning(o[i], h);
2711
+ }
2712
+ }
2713
+ }
2714
+ return r || o;
2715
+ }
2716
+ function chunkArray(arr, size) {
2717
+ const chunks = [];
2718
+ for (let i = 0; i < arr.length; i += size) {
2719
+ chunks.push(arr.slice(i, i + size));
2720
+ }
2721
+ return chunks;
2722
+ }
2723
+ function diff(...arrays) {
2724
+ return [].concat(...arrays.map((arr, i) => {
2725
+ const others = arrays.slice(0);
2726
+ others.splice(i, 1);
2727
+ const unique = [...new Set([].concat(...others))];
2728
+ return arr.filter((item) => !unique.includes(item));
2729
+ }));
2730
+ }
2731
+ function intersect(...arrays) {
2732
+ if (arrays.length === 0) return [];
2733
+ if (arrays.length === 1) return arrays[0];
2734
+ return arrays.reduce(
2735
+ (first, second) => first.filter((item) => second.includes(item))
2736
+ );
2737
+ }
2738
+
2739
+ // src/core/PolicyMeta.ts
2740
+ var PolicyMeta = class _PolicyMeta {
2741
+ policy;
2742
+ /**
2743
+ * Create new PolicyMeta instance
2744
+ * Matches JavaScript SDK constructor signature exactly
2745
+ */
2746
+ constructor(policy = {}, metaKeys = []) {
2747
+ this.policy = _PolicyMeta.normalizePolicy(policy);
2748
+ this.fillDefault(metaKeys);
2749
+ }
2750
+ /**
2751
+ * Normalize policy object structure
2752
+ * Matches JavaScript SDK normalizePolicy method exactly
2753
+ */
2754
+ static normalizePolicy(policy = {}) {
2755
+ const policyMeta = {};
2756
+ for (const [policyKey, value] of Object.entries(policy)) {
2757
+ if (value !== null && ["read", "write"].includes(policyKey)) {
2758
+ policyMeta[policyKey] = {};
2759
+ for (const [key, content] of Object.entries(value)) {
2760
+ policyMeta[policyKey][key] = content;
2761
+ }
2762
+ }
2763
+ }
2764
+ return policyMeta;
2765
+ }
2766
+ /**
2767
+ * Fill default policy values for metadata keys
2768
+ * Matches JavaScript SDK fillDefault method exactly
2769
+ */
2770
+ fillDefault(metaKeys = []) {
2771
+ const readPolicy = Array.from(this.policy).filter((item) => item.action === "read");
2772
+ const writePolicy = Array.from(this.policy).filter((item) => item.action === "write");
2773
+ for (const [type, value] of Object.entries({
2774
+ read: readPolicy,
2775
+ write: writePolicy
2776
+ })) {
2777
+ const policyKey = value.map((item) => item.key);
2778
+ if (!this.policy[type]) {
2779
+ this.policy[type] = {};
2780
+ }
2781
+ for (const key of diff(metaKeys, policyKey)) {
2782
+ if (!this.policy[type][key]) {
2783
+ this.policy[type][key] = type === "write" && !["characters", "pubkey"].includes(key) ? ["self"] : ["all"];
2784
+ }
2785
+ }
2786
+ }
2787
+ }
2788
+ /**
2789
+ * Get the policy object
2790
+ * Matches JavaScript SDK get method exactly
2791
+ */
2792
+ get() {
2793
+ return this.policy;
2794
+ }
2795
+ /**
2796
+ * Convert policy to JSON string
2797
+ * Matches JavaScript SDK toJson method exactly
2798
+ */
2799
+ toJson() {
2800
+ return JSON.stringify(this.get());
2801
+ }
2802
+ };
2688
2803
  var AtomMeta = class _AtomMeta {
2689
2804
  meta;
2690
2805
  /**
@@ -2780,8 +2895,9 @@ var AtomMeta = class _AtomMeta {
2780
2895
  * @return This instance for chaining
2781
2896
  */
2782
2897
  addPolicy(policy) {
2898
+ const policyMeta = new PolicyMeta(policy, Object.keys(this.meta));
2783
2899
  this.merge({
2784
- policy: JSON.stringify(policy)
2900
+ policy: policyMeta.toJson()
2785
2901
  });
2786
2902
  return this;
2787
2903
  }
@@ -3414,6 +3530,87 @@ function createMolecularHash(value) {
3414
3530
  }
3415
3531
  return value;
3416
3532
  }
3533
+
3534
+ // src/core/TokenUnit.ts
3535
+ var TokenUnit = class _TokenUnit {
3536
+ id;
3537
+ name;
3538
+ metas;
3539
+ /**
3540
+ * Create new TokenUnit instance
3541
+ * Matches JavaScript SDK constructor signature exactly
3542
+ */
3543
+ constructor(id, name, metas) {
3544
+ this.id = id;
3545
+ this.name = name;
3546
+ this.metas = metas || {};
3547
+ }
3548
+ /**
3549
+ * Create TokenUnit from GraphQL response data
3550
+ * Matches JavaScript SDK createFromGraphQL method exactly
3551
+ */
3552
+ static createFromGraphQL(data) {
3553
+ let metas = data.metas || {};
3554
+ if (Array.isArray(metas) && metas.length) {
3555
+ try {
3556
+ metas = JSON.parse(metas);
3557
+ if (!metas) {
3558
+ metas = {};
3559
+ }
3560
+ } catch (error) {
3561
+ metas = {};
3562
+ }
3563
+ }
3564
+ return new _TokenUnit(
3565
+ data.id,
3566
+ data.name,
3567
+ metas
3568
+ );
3569
+ }
3570
+ /**
3571
+ * Create TokenUnit from database array data
3572
+ * Matches JavaScript SDK createFromDB method exactly
3573
+ */
3574
+ static createFromDB(data) {
3575
+ return new _TokenUnit(
3576
+ data[0],
3577
+ data[1],
3578
+ data.length > 2 ? data[2] : {}
3579
+ );
3580
+ }
3581
+ /**
3582
+ * Get fragment zone from metadata
3583
+ * Matches JavaScript SDK getFragmentZone method exactly
3584
+ */
3585
+ getFragmentZone() {
3586
+ return this.metas.fragmentZone || null;
3587
+ }
3588
+ /**
3589
+ * Get fused token units from metadata
3590
+ * Matches JavaScript SDK getFusedTokenUnits method exactly
3591
+ */
3592
+ getFusedTokenUnits() {
3593
+ return this.metas.fusedTokenUnits || null;
3594
+ }
3595
+ /**
3596
+ * Convert to data array format
3597
+ * Matches JavaScript SDK toData method exactly
3598
+ */
3599
+ toData() {
3600
+ return [this.id, this.name, this.metas];
3601
+ }
3602
+ /**
3603
+ * Convert to GraphQL response format
3604
+ * Matches JavaScript SDK toGraphQLResponse method exactly
3605
+ */
3606
+ toGraphQLResponse() {
3607
+ return {
3608
+ id: this.id,
3609
+ name: this.name,
3610
+ metas: JSON.stringify(this.metas)
3611
+ };
3612
+ }
3613
+ };
3417
3614
  var Wallet = class _Wallet {
3418
3615
  token;
3419
3616
  balance;
@@ -3554,11 +3751,13 @@ var Wallet = class _Wallet {
3554
3751
  return typeof maybeBundleHash === "string" && isBundleHash(maybeBundleHash);
3555
3752
  }
3556
3753
  /**
3557
- * Get formatted token units from raw data
3558
- * Stub implementation for now
3754
+ * Map raw token-unit tuples to TokenUnit instances.
3755
+ * Matches JS SDK Wallet.getTokenUnits (Wallet.js:190-196). The serialised shape reaches hashed
3756
+ * atom meta via AtomMeta.setAtomWallet -> JSON.stringify(getTokenUnitsData()), so returning raw
3757
+ * tuples here would diverge from every other SDK.
3559
3758
  */
3560
3759
  static getTokenUnits(unitsData) {
3561
- return unitsData;
3760
+ return unitsData.map((unitData) => TokenUnit.createFromDB(unitData));
3562
3761
  }
3563
3762
  /**
3564
3763
  * Create a remainder wallet for transactions
@@ -3839,6 +4038,14 @@ var Wallet = class _Wallet {
3839
4038
  };
3840
4039
  var hexStringRegex = /^[0-9a-fA-F]+$/;
3841
4040
  var base17HashRegex = /^[0-9a-g]+$/;
4041
+ var urlString = (message = "Invalid URL") => zod.z.string().refine((value) => {
4042
+ try {
4043
+ new URL(value);
4044
+ return true;
4045
+ } catch {
4046
+ return false;
4047
+ }
4048
+ }, message);
3842
4049
  zod.z.string().regex(hexStringRegex, "Must be a valid hexadecimal string").brand();
3843
4050
  var WalletAddressSchema = zod.z.string().regex(hexStringRegex, "Wallet address must be hexadecimal").length(64, "Wallet address must be exactly 64 characters").brand();
3844
4051
  var BundleHashSchema = zod.z.string().regex(hexStringRegex, "Bundle hash must be hexadecimal").length(64, "Bundle hash must be exactly 64 characters").brand();
@@ -3847,10 +4054,10 @@ zod.z.string().regex(base17HashRegex, "Molecular hash must be base17 format (0-9
3847
4054
  var TokenSlugSchema = zod.z.string().min(1, "Token slug cannot be empty").max(64, "Token slug cannot exceed 64 characters").transform((val) => val.toUpperCase()).brand();
3848
4055
  var MetaTypeSchema = zod.z.string().min(1, "Meta type cannot be empty").max(256, "Meta type cannot exceed 256 characters").brand();
3849
4056
  var MetaIdSchema = zod.z.string().min(1, "Meta ID cannot be empty").brand();
3850
- var BatchIdSchema = zod.z.string().min(1, "Batch ID cannot be empty").brand();
4057
+ var BatchIdSchema = zod.z.string().regex(/^[0-9a-fA-F]{64}$/, "Batch ID must be 64 hexadecimal characters").brand();
3851
4058
  var CellSlugSchema = zod.z.string().min(1, "Cell slug cannot be empty").max(64, "Cell slug cannot exceed 64 characters").brand();
3852
4059
  var AtomIsotopeSchema = zod.z.enum(["C", "V", "U", "T", "M", "I", "R", "B", "F"], {
3853
- errorMap: () => ({ message: "Invalid atom isotope. Must be one of: C, V, U, T, M, I, R, B, F" })
4060
+ error: "Invalid atom isotope. Must be one of: C, V, U, T, M, I, R, B, F"
3854
4061
  });
3855
4062
  var MetaDataValueSchema = zod.z.union([
3856
4063
  zod.z.string(),
@@ -3863,7 +4070,7 @@ var AtomMetaDataSchema = zod.z.object({
3863
4070
  value: MetaDataValueSchema
3864
4071
  }).strict();
3865
4072
  var MetaDataSchema = zod.z.lazy(
3866
- () => zod.z.record(zod.z.union([
4073
+ () => zod.z.record(zod.z.string(), zod.z.union([
3867
4074
  MetaDataValueSchema,
3868
4075
  MetaDataSchema,
3869
4076
  zod.z.array(MetaDataSchema)
@@ -3902,7 +4109,7 @@ zod.z.object({
3902
4109
  version: zod.z.number().int().min(1).optional()
3903
4110
  }).strict();
3904
4111
  zod.z.object({
3905
- uri: zod.z.union([zod.z.string().url(), zod.z.array(zod.z.string().url())]).optional(),
4112
+ uri: zod.z.union([urlString(), zod.z.array(urlString())]).optional(),
3906
4113
  cellSlug: zod.z.union([CellSlugSchema, zod.z.string(), zod.z.null()]).optional(),
3907
4114
  client: zod.z.unknown().optional(),
3908
4115
  socket: zod.z.unknown().optional(),
@@ -3919,7 +4126,7 @@ zod.z.object({
3919
4126
  zod.z.object({
3920
4127
  cellSlug: zod.z.union([CellSlugSchema, zod.z.string(), zod.z.null()]).optional(),
3921
4128
  encrypt: zod.z.boolean().optional(),
3922
- callback: zod.z.function().args(zod.z.unknown()).returns(zod.z.void()).optional()
4129
+ callback: zod.z.custom((value) => typeof value === "function").optional()
3923
4130
  }).strict();
3924
4131
  zod.z.object({
3925
4132
  cellSlug: zod.z.union([CellSlugSchema, zod.z.string(), zod.z.null()]).optional()
@@ -3928,7 +4135,7 @@ zod.z.object({
3928
4135
  recipient: zod.z.union([WalletAddressSchema, zod.z.string()]),
3929
4136
  amount: zod.z.union([zod.z.number().positive(), zod.z.string().min(1)]),
3930
4137
  token: zod.z.union([TokenSlugSchema, zod.z.string()]).optional(),
3931
- callbackUrl: zod.z.union([zod.z.string().url(), zod.z.null()]).optional(),
4138
+ callbackUrl: zod.z.union([urlString(), zod.z.null()]).optional(),
3932
4139
  metaType: zod.z.union([MetaTypeSchema, zod.z.string(), zod.z.null()]).optional(),
3933
4140
  metaId: zod.z.union([MetaIdSchema, zod.z.string(), zod.z.null()]).optional(),
3934
4141
  meta: zod.z.union([MetaDataSchema, zod.z.null()]).optional()
@@ -3969,7 +4176,7 @@ zod.z.object({
3969
4176
  value: zod.z.union([zod.z.string(), zod.z.null()]).optional(),
3970
4177
  latest: zod.z.union([zod.z.boolean(), zod.z.null()]).optional(),
3971
4178
  filter: zod.z.union([zod.z.array(MetaFilterSchema), zod.z.null()]).optional(),
3972
- queryArgs: zod.z.union([zod.z.record(zod.z.unknown()), zod.z.null()]).optional(),
4179
+ queryArgs: zod.z.union([zod.z.record(zod.z.string(), zod.z.unknown()), zod.z.null()]).optional(),
3973
4180
  count: zod.z.union([zod.z.number().int().min(1), zod.z.null()]).optional(),
3974
4181
  countBy: zod.z.union([zod.z.string(), zod.z.null()]).optional(),
3975
4182
  cellSlug: zod.z.union([CellSlugSchema, zod.z.string(), zod.z.null()]).optional()
@@ -3994,7 +4201,7 @@ zod.z.object({
3994
4201
  }).strict();
3995
4202
  zod.z.object({
3996
4203
  query: zod.z.string().min(1, "GraphQL query cannot be empty"),
3997
- variables: zod.z.record(zod.z.unknown()).optional(),
4204
+ variables: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
3998
4205
  operationName: zod.z.union([zod.z.string(), zod.z.null()]).optional()
3999
4206
  }).strict();
4000
4207
  var GraphQLErrorSchema = zod.z.object({
@@ -4004,18 +4211,18 @@ var GraphQLErrorSchema = zod.z.object({
4004
4211
  column: zod.z.number().int().min(1)
4005
4212
  })).optional(),
4006
4213
  path: zod.z.array(zod.z.union([zod.z.string(), zod.z.number()])).optional(),
4007
- extensions: zod.z.record(zod.z.unknown()).optional()
4214
+ extensions: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
4008
4215
  }).strict();
4009
4216
  zod.z.object({
4010
4217
  data: zod.z.unknown().optional(),
4011
4218
  errors: zod.z.array(GraphQLErrorSchema).nullable().optional(),
4012
- extensions: zod.z.record(zod.z.unknown()).optional()
4219
+ extensions: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
4013
4220
  }).strict();
4014
4221
  zod.z.object({
4015
4222
  operationName: zod.z.string().optional(),
4016
4223
  query: zod.z.string().optional(),
4017
- variables: zod.z.record(zod.z.unknown()).optional(),
4018
- callback: zod.z.function().args(zod.z.unknown()).returns(zod.z.void()).optional()
4224
+ variables: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
4225
+ callback: zod.z.custom((value) => typeof value === "function").optional()
4019
4226
  }).strict();
4020
4227
  zod.z.object({
4021
4228
  valid: zod.z.boolean(),
@@ -4025,7 +4232,7 @@ zod.z.object({
4025
4232
  }).strict();
4026
4233
  zod.z.object({
4027
4234
  operation: zod.z.string().optional(),
4028
- parameters: zod.z.record(zod.z.unknown()).optional(),
4235
+ parameters: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
4029
4236
  timestamp: zod.z.number().int().min(0).optional(),
4030
4237
  stack: zod.z.string().optional()
4031
4238
  }).strict();
@@ -4171,65 +4378,13 @@ var RuleArgumentException = class extends exports.BaseException {
4171
4378
  };
4172
4379
  var RuleArgumentException_default = RuleArgumentException;
4173
4380
 
4174
- // src/libraries/array.ts
4175
- function deepCloning(o, h) {
4176
- let i;
4177
- let r;
4178
- let x;
4179
- const t = [Array, Date, Number, String, Boolean];
4180
- const s = Object.prototype.toString;
4181
- h = h || [];
4182
- for (i = 0; i < h.length; i += 2) {
4183
- if (o === h[i]) {
4184
- return h[i + 1];
4185
- }
4186
- }
4187
- if (!r && o && typeof o === "object") {
4188
- r = {};
4189
- for (i = 0; i < t.length; i++) {
4190
- if (s.call(o) === s.call(x = new t[i](o))) {
4191
- r = i ? x : [];
4192
- }
4193
- }
4194
- h.push(o, r);
4195
- for (i in o) {
4196
- if (Object.prototype.hasOwnProperty.call(o, i)) {
4197
- r[i] = deepCloning(o[i], h);
4198
- }
4199
- }
4200
- }
4201
- return r || o;
4202
- }
4203
- function chunkArray(arr, size) {
4204
- const chunks = [];
4205
- for (let i = 0; i < arr.length; i += size) {
4206
- chunks.push(arr.slice(i, i + size));
4207
- }
4208
- return chunks;
4209
- }
4210
- function diff(...arrays) {
4211
- return [].concat(...arrays.map((arr, i) => {
4212
- const others = arrays.slice(0);
4213
- others.splice(i, 1);
4214
- const unique = [...new Set([].concat(...others))];
4215
- return arr.filter((item) => !unique.includes(item));
4216
- }));
4217
- }
4218
- function intersect(...arrays) {
4219
- if (arrays.length === 0) return [];
4220
- if (arrays.length === 1) return arrays[0];
4221
- return arrays.reduce(
4222
- (first, second) => first.filter((item) => second.includes(item))
4223
- );
4224
- }
4225
-
4226
4381
  // src/instance/rules/Callback.ts
4227
4382
  init_exception();
4228
4383
  var CallbackParamsSchema = zod.z.object({
4229
4384
  action: zod.z.string().min(1, "Action cannot be empty"),
4230
4385
  metaType: zod.z.union([zod.z.string(), zod.z.null()]).optional(),
4231
4386
  metaId: zod.z.union([zod.z.string(), zod.z.null()]).optional(),
4232
- meta: zod.z.union([zod.z.instanceof(Meta2), zod.z.record(zod.z.unknown()), zod.z.null()]).optional(),
4387
+ meta: zod.z.union([zod.z.instanceof(Meta2), zod.z.record(zod.z.string(), zod.z.unknown()), zod.z.null()]).optional(),
4233
4388
  address: zod.z.union([zod.z.string(), zod.z.null()]).optional(),
4234
4389
  token: zod.z.union([zod.z.string(), zod.z.null()]).optional(),
4235
4390
  amount: zod.z.union([zod.z.string(), zod.z.null()]).optional(),
@@ -5553,6 +5708,51 @@ var Molecule = class _Molecule {
5553
5708
  }));
5554
5709
  return this;
5555
5710
  }
5711
+ /**
5712
+ * Replenishes non-finite token supplies.
5713
+ * Matches JS SDK Molecule.replenishToken (Molecule.js:521-566) exactly.
5714
+ *
5715
+ * Two orderings here are load-bearing for the molecular hash and must not be reordered:
5716
+ * the remainder balance is computed BEFORE the source balance is overwritten, and the
5717
+ * source V-atom is added BEFORE the remainder V-atom.
5718
+ */
5719
+ replenishToken({
5720
+ amount,
5721
+ units = []
5722
+ }) {
5723
+ if (amount < 0) {
5724
+ throw new NegativeAmountException("Molecule::replenishToken() - Amount to replenish must be positive!");
5725
+ }
5726
+ if (!this.sourceWallet || !this.remainderWallet) {
5727
+ throw new Error("Source and remainder wallets required for token replenishment");
5728
+ }
5729
+ if (units.length) {
5730
+ const formatted = Wallet.getTokenUnits(units);
5731
+ this.remainderWallet.tokenUnits = this.sourceWallet.tokenUnits;
5732
+ for (const unit of formatted) {
5733
+ this.remainderWallet.tokenUnits.push(unit);
5734
+ }
5735
+ this.remainderWallet.balance = String(this.remainderWallet.tokenUnits.length);
5736
+ this.sourceWallet.tokenUnits = formatted;
5737
+ this.sourceWallet.balance = String(this.sourceWallet.tokenUnits.length);
5738
+ } else {
5739
+ this.remainderWallet.balance = String(Number(this.sourceWallet.balance) + amount);
5740
+ this.sourceWallet.balance = String(amount);
5741
+ }
5742
+ this.addAtom(Atom.create({
5743
+ isotope: "V",
5744
+ wallet: this.sourceWallet,
5745
+ value: Number(this.sourceWallet.balance)
5746
+ }));
5747
+ this.addAtom(Atom.create({
5748
+ isotope: "V",
5749
+ wallet: this.remainderWallet,
5750
+ value: Number(this.remainderWallet.balance),
5751
+ metaType: "walletBundle",
5752
+ metaId: this.remainderWallet.bundle
5753
+ }));
5754
+ return this;
5755
+ }
5556
5756
  /**
5557
5757
  * Initialize authorization request
5558
5758
  * Creates U-isotope (authorization) atom for requesting auth token
@@ -6087,16 +6287,27 @@ var GraphQLClient = class {
6087
6287
  return core.createClient({
6088
6288
  url: serverUri,
6089
6289
  exchanges,
6090
- // PQ-transport Phase E: when encryption is on, route fetch through the CipherHash wrapper
6091
- // (encrypt the request body to the validator's ML-KEM pubkey, decrypt the response).
6092
- // Omitted → urql uses the global fetch (plaintext).
6093
- ...this.cipherLink ? { fetch: ((input, init) => this.cipherFetch(input, init)) } : {},
6290
+ // urql 5 had no default and always POSTed; urql 6 defaults to 'within-url-limit', which
6291
+ // URL-encodes short queries and sends NO body. That would silently disable the CipherHash
6292
+ // envelope below — cipherFetch's `typeof init.body === 'string'` guard fails on a GET, so
6293
+ // the query would leave as plaintext URL parameters with no error. Pin POST explicitly.
6294
+ preferGetMethod: false,
6295
+ // Always route through our own fetch. Two reasons: (1) PQ-transport Phase E — when
6296
+ // encryption is on, cipherFetch wraps the request body in the CipherHash envelope and
6297
+ // decrypts the response; (2) the 60s timeout. urql's makeFetchSource unconditionally
6298
+ // overwrites init.signal with its own AbortController, so a signal returned from
6299
+ // fetchOptions() is discarded and never fired. Combining it here is the only place it
6300
+ // survives.
6301
+ fetch: ((input, init) => {
6302
+ const timeoutSignal = AbortSignal.timeout(6e4);
6303
+ const signal = init?.signal && typeof AbortSignal.any === "function" ? AbortSignal.any([init.signal, timeoutSignal]) : init?.signal ?? timeoutSignal;
6304
+ const timedInit = { ...init, signal };
6305
+ return this.cipherLink ? this.cipherFetch(input, timedInit) : fetch(input, timedInit);
6306
+ }),
6094
6307
  fetchOptions: () => ({
6095
6308
  headers: {
6096
6309
  "X-Auth-Token": this.$__authToken
6097
- },
6098
- // Add 60 second timeout
6099
- signal: AbortSignal.timeout(6e4)
6310
+ }
6100
6311
  })
6101
6312
  });
6102
6313
  }
@@ -6576,6 +6787,14 @@ var Mutation = class extends Query {
6576
6787
  }
6577
6788
  }
6578
6789
  };
6790
+ var urlString2 = (message = "Invalid URL") => zod.z.string().refine((value) => {
6791
+ try {
6792
+ new URL(value);
6793
+ return true;
6794
+ } catch {
6795
+ return false;
6796
+ }
6797
+ }, message);
6579
6798
  function createBrandedSchema(name, baseSchema = zod.z.string()) {
6580
6799
  return baseSchema.brand(name);
6581
6800
  }
@@ -6601,7 +6820,12 @@ var TokenSlugSchema2 = createBrandedSchema(
6601
6820
  );
6602
6821
  var BatchIdSchema2 = createBrandedSchema(
6603
6822
  "BatchId",
6604
- zod.z.string().uuid("Invalid batch ID format")
6823
+ // 64 hex characters. generateBatchId (src/libraries/crypto.ts) returns either
6824
+ // shake256(molecularHash + index, 256) — 256 bits, 64 hex chars — or randomString(64) over the
6825
+ // alphabet 'abcdef0123456789'. A UUID shape matches nothing this SDK can produce, so the
6826
+ // previous 8-4-4-4-12 regex rejected every real batch ID. Agrees with isBatchId in
6827
+ // src/types/guards.ts.
6828
+ zod.z.string().regex(/^[0-9a-fA-F]{64}$/, "Invalid batch ID format")
6605
6829
  );
6606
6830
  var CellSlugSchema2 = createBrandedSchema(
6607
6831
  "CellSlug",
@@ -6645,7 +6869,7 @@ var AtomMetaDataSchema2 = zod.z.object({
6645
6869
  zod.z.object({
6646
6870
  key: zod.z.string(),
6647
6871
  value: zod.z.union([zod.z.string(), zod.z.number(), zod.z.boolean(), zod.z.null()])
6648
- }).passthrough();
6872
+ }).loose();
6649
6873
  var AtomParamsSchema2 = zod.z.object({
6650
6874
  position: zod.z.string().optional(),
6651
6875
  walletAddress: zod.z.union([WalletAddressSchema2, zod.z.string()]).optional(),
@@ -6680,8 +6904,8 @@ var MoleculeParamsSchema2 = zod.z.object({
6680
6904
  }).strict();
6681
6905
  var KnishIOClientConfigSchema2 = zod.z.object({
6682
6906
  uri: zod.z.union([
6683
- zod.z.string().url("Invalid URI format"),
6684
- zod.z.array(zod.z.string().url("Invalid URI format")).min(1)
6907
+ urlString2("Invalid URI format"),
6908
+ zod.z.array(urlString2("Invalid URI format")).min(1)
6685
6909
  ]).optional(),
6686
6910
  cellSlug: zod.z.union([CellSlugSchema2, zod.z.string()]).optional(),
6687
6911
  client: zod.z.unknown().optional(),
@@ -6694,11 +6918,11 @@ var KnishIOClientConfigSchema2 = zod.z.object({
6694
6918
  defaultRequestPolicy: zod.z.enum(["cache-first", "cache-only", "network-only", "cache-and-network"]).nullable().optional()
6695
6919
  }).strict();
6696
6920
  var EnvironmentConfigSchema = zod.z.object({
6697
- NODE_ENV: zod.z.enum(["development", "production", "test"]).default("development"),
6698
- KNISHIO_NODE_URI: zod.z.string().url().optional(),
6921
+ NODE_ENV: zod.z.enum(["development", "production", "test"]).optional(),
6922
+ KNISHIO_NODE_URI: urlString2().optional(),
6699
6923
  KNISHIO_CELL_SLUG: zod.z.string().optional(),
6700
- KNISHIO_LOGGING: zod.z.string().transform((val) => val === "true").default("false"),
6701
- KNISHIO_SERVER_SDK_VERSION: zod.z.string().transform((val) => parseInt(val, 10)).default("4")
6924
+ KNISHIO_LOGGING: zod.z.string().transform((val) => val === "true").optional(),
6925
+ KNISHIO_SERVER_SDK_VERSION: zod.z.string().transform((val) => parseInt(val, 10)).optional()
6702
6926
  }).partial();
6703
6927
  var TransferParamsSchema2 = zod.z.object({
6704
6928
  recipient: zod.z.union([WalletAddressSchema2, zod.z.string()]),
@@ -6707,7 +6931,7 @@ var TransferParamsSchema2 = zod.z.object({
6707
6931
  "Amount must be positive"
6708
6932
  ),
6709
6933
  token: zod.z.union([TokenSlugSchema2, zod.z.string()]).optional(),
6710
- callbackUrl: zod.z.string().url().optional(),
6934
+ callbackUrl: urlString2().optional(),
6711
6935
  metaType: zod.z.string().optional(),
6712
6936
  metaId: zod.z.string().optional(),
6713
6937
  meta: MetaDataSchema2.optional()
@@ -6748,7 +6972,7 @@ var MetaQueryParamsSchema2 = zod.z.object({
6748
6972
  value: zod.z.string().optional(),
6749
6973
  latest: zod.z.boolean().optional(),
6750
6974
  filter: zod.z.string().optional(),
6751
- queryArgs: zod.z.record(zod.z.unknown()).optional(),
6975
+ queryArgs: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
6752
6976
  count: zod.z.number().int().min(0).optional(),
6753
6977
  countBy: zod.z.string().optional(),
6754
6978
  cellSlug: zod.z.union([CellSlugSchema2, zod.z.string()]).optional()
@@ -6767,14 +6991,14 @@ var AuthTokenParamsSchema2 = zod.z.object({
6767
6991
  var AuthParamsSchema2 = zod.z.object({
6768
6992
  cellSlug: zod.z.union([CellSlugSchema2, zod.z.string()]).optional(),
6769
6993
  encrypt: zod.z.boolean().optional(),
6770
- callback: zod.z.function().optional()
6994
+ callback: zod.z.custom((value) => typeof value === "function").optional()
6771
6995
  }).strict();
6772
6996
  var GuestAuthParamsSchema2 = zod.z.object({
6773
6997
  cellSlug: zod.z.union([CellSlugSchema2, zod.z.string()]).optional()
6774
6998
  }).strict();
6775
6999
  var GraphQLRequestSchema2 = zod.z.object({
6776
7000
  query: zod.z.string().min(1),
6777
- variables: zod.z.record(zod.z.unknown()).optional(),
7001
+ variables: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
6778
7002
  operationName: zod.z.string().optional()
6779
7003
  }).strict();
6780
7004
  var GraphQLErrorSchema2 = zod.z.object({
@@ -6784,12 +7008,12 @@ var GraphQLErrorSchema2 = zod.z.object({
6784
7008
  column: zod.z.number().int()
6785
7009
  })).optional(),
6786
7010
  path: zod.z.array(zod.z.union([zod.z.string(), zod.z.number()])).optional(),
6787
- extensions: zod.z.record(zod.z.unknown()).optional()
7011
+ extensions: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
6788
7012
  }).strict();
6789
7013
  var GraphQLResponseSchema2 = zod.z.object({
6790
7014
  data: zod.z.unknown().optional(),
6791
7015
  errors: zod.z.array(GraphQLErrorSchema2).optional(),
6792
- extensions: zod.z.record(zod.z.unknown()).optional()
7016
+ extensions: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
6793
7017
  }).strict();
6794
7018
  function safeParse(schema, data) {
6795
7019
  const result = schema.safeParse(data);
@@ -6799,7 +7023,7 @@ function safeParse(schema, data) {
6799
7023
  error: {
6800
7024
  issues: result.error.issues,
6801
7025
  message: result.error.message,
6802
- formatted: result.error.format()
7026
+ formatted: zod.z.treeifyError(result.error)
6803
7027
  }
6804
7028
  };
6805
7029
  }
@@ -7504,89 +7728,6 @@ var ConfigValidator = class _ConfigValidator {
7504
7728
 
7505
7729
  // src/response/ResponseBalance.ts
7506
7730
  init_Response();
7507
-
7508
- // src/core/TokenUnit.ts
7509
- var TokenUnit = class _TokenUnit {
7510
- id;
7511
- name;
7512
- metas;
7513
- /**
7514
- * Create new TokenUnit instance
7515
- * Matches JavaScript SDK constructor signature exactly
7516
- */
7517
- constructor(id, name, metas) {
7518
- this.id = id;
7519
- this.name = name;
7520
- this.metas = metas || {};
7521
- }
7522
- /**
7523
- * Create TokenUnit from GraphQL response data
7524
- * Matches JavaScript SDK createFromGraphQL method exactly
7525
- */
7526
- static createFromGraphQL(data) {
7527
- let metas = data.metas || {};
7528
- if (Array.isArray(metas) && metas.length) {
7529
- try {
7530
- metas = JSON.parse(metas);
7531
- if (!metas) {
7532
- metas = {};
7533
- }
7534
- } catch (error) {
7535
- metas = {};
7536
- }
7537
- }
7538
- return new _TokenUnit(
7539
- data.id,
7540
- data.name,
7541
- metas
7542
- );
7543
- }
7544
- /**
7545
- * Create TokenUnit from database array data
7546
- * Matches JavaScript SDK createFromDB method exactly
7547
- */
7548
- static createFromDB(data) {
7549
- return new _TokenUnit(
7550
- data[0],
7551
- data[1],
7552
- data.length > 2 ? data[2] : {}
7553
- );
7554
- }
7555
- /**
7556
- * Get fragment zone from metadata
7557
- * Matches JavaScript SDK getFragmentZone method exactly
7558
- */
7559
- getFragmentZone() {
7560
- return this.metas.fragmentZone || null;
7561
- }
7562
- /**
7563
- * Get fused token units from metadata
7564
- * Matches JavaScript SDK getFusedTokenUnits method exactly
7565
- */
7566
- getFusedTokenUnits() {
7567
- return this.metas.fusedTokenUnits || null;
7568
- }
7569
- /**
7570
- * Convert to data array format
7571
- * Matches JavaScript SDK toData method exactly
7572
- */
7573
- toData() {
7574
- return [this.id, this.name, this.metas];
7575
- }
7576
- /**
7577
- * Convert to GraphQL response format
7578
- * Matches JavaScript SDK toGraphQLResponse method exactly
7579
- */
7580
- toGraphQLResponse() {
7581
- return {
7582
- id: this.id,
7583
- name: this.name,
7584
- metas: JSON.stringify(this.metas)
7585
- };
7586
- }
7587
- };
7588
-
7589
- // src/response/ResponseBalance.ts
7590
7731
  var ResponseBalance = class _ResponseBalance extends exports.Response {
7591
7732
  /**
7592
7733
  * Class constructor
@@ -10095,6 +10236,12 @@ var MutationAppendRequest = class extends MutationProposeMolecule {
10095
10236
  }
10096
10237
  };
10097
10238
 
10239
+ // src/mutation/MutationReplenishToken.ts
10240
+ var MutationReplenishToken = class extends MutationProposeMolecule {
10241
+ fillMolecule() {
10242
+ }
10243
+ };
10244
+
10098
10245
  // src/subscribe/Subscribe.ts
10099
10246
  init_exception();
10100
10247
  var Subscribe = class {
@@ -11528,20 +11675,42 @@ var KnishIOClient = class {
11528
11675
  return response;
11529
11676
  }
11530
11677
  /**
11531
- * Replenish tokens
11678
+ * Replenish a non-finite token supply.
11679
+ * Matches JS SDK KnishIOClient.replenishToken (KnishIOClient.js:2195-2231).
11532
11680
  */
11533
11681
  async replenishToken({
11534
11682
  token,
11535
11683
  amount = null,
11536
11684
  units = null,
11537
- sourceWallet: _sourceWallet = null
11685
+ sourceWallet = null
11538
11686
  }) {
11539
11687
  this.log("info", `KnishIOClient::replenishToken() - Replenishing ${amount || "units"} of ${token}...`);
11540
- return this.requestTokens({
11541
- token,
11542
- amount,
11543
- units
11688
+ if (!sourceWallet) {
11689
+ sourceWallet = (await this.queryBalance({ token }))?.payload();
11690
+ }
11691
+ if (!sourceWallet) {
11692
+ throw new exports.TransferBalanceException("Source wallet is missing or invalid.");
11693
+ }
11694
+ const remainderWallet = sourceWallet.createRemainder(this.getSecret());
11695
+ const molecule = await this.createMolecule({
11696
+ sourceWallet,
11697
+ remainderWallet
11698
+ });
11699
+ molecule.replenishToken({
11700
+ amount: Number(amount ?? 0),
11701
+ units: units ?? []
11702
+ });
11703
+ molecule.sign({ bundle: this.getBundle() });
11704
+ molecule.check();
11705
+ const mutation = await this.createMoleculeMutation({
11706
+ mutationClass: MutationReplenishToken,
11707
+ molecule
11544
11708
  });
11709
+ const response = await this.executeQuery(mutation);
11710
+ if (!response) {
11711
+ throw new CodeException("Token replenishment failed");
11712
+ }
11713
+ return response;
11545
11714
  }
11546
11715
  /**
11547
11716
  * Fuse token units
@@ -11992,75 +12161,8 @@ var KnishIOClient = class {
11992
12161
 
11993
12162
  // src/index.ts
11994
12163
  init_Response();
11995
-
11996
- // src/core/PolicyMeta.ts
11997
- var PolicyMeta = class _PolicyMeta {
11998
- policy;
11999
- /**
12000
- * Create new PolicyMeta instance
12001
- * Matches JavaScript SDK constructor signature exactly
12002
- */
12003
- constructor(policy = {}, metaKeys = []) {
12004
- this.policy = _PolicyMeta.normalizePolicy(policy);
12005
- this.fillDefault(metaKeys);
12006
- }
12007
- /**
12008
- * Normalize policy object structure
12009
- * Matches JavaScript SDK normalizePolicy method exactly
12010
- */
12011
- static normalizePolicy(policy = {}) {
12012
- const policyMeta = {};
12013
- for (const [policyKey, value] of Object.entries(policy)) {
12014
- if (value !== null && ["read", "write"].includes(policyKey)) {
12015
- policyMeta[policyKey] = {};
12016
- for (const [key, content] of Object.entries(value)) {
12017
- policyMeta[policyKey][key] = content;
12018
- }
12019
- }
12020
- }
12021
- return policyMeta;
12022
- }
12023
- /**
12024
- * Fill default policy values for metadata keys
12025
- * Matches JavaScript SDK fillDefault method exactly
12026
- */
12027
- fillDefault(metaKeys = []) {
12028
- const readPolicy = Array.from(this.policy).filter((item) => item.action === "read");
12029
- const writePolicy = Array.from(this.policy).filter((item) => item.action === "write");
12030
- for (const [type, value] of Object.entries({
12031
- read: readPolicy,
12032
- write: writePolicy
12033
- })) {
12034
- const policyKey = value.map((item) => item.key);
12035
- if (!this.policy[type]) {
12036
- this.policy[type] = {};
12037
- }
12038
- for (const key of diff(metaKeys, policyKey)) {
12039
- if (!this.policy[type][key]) {
12040
- this.policy[type][key] = type === "write" && !["characters", "pubkey"].includes(key) ? ["self"] : ["all"];
12041
- }
12042
- }
12043
- }
12044
- }
12045
- /**
12046
- * Get the policy object
12047
- * Matches JavaScript SDK get method exactly
12048
- */
12049
- get() {
12050
- return this.policy;
12051
- }
12052
- /**
12053
- * Convert policy to JSON string
12054
- * Matches JavaScript SDK toJson method exactly
12055
- */
12056
- toJson() {
12057
- return JSON.stringify(this.get());
12058
- }
12059
- };
12060
-
12061
- // src/index.ts
12062
12164
  init_exception();
12063
- var SDK_VERSION = "1.0.0";
12165
+ var SDK_VERSION = "0.9.6";
12064
12166
  var SDK_NAME = "KnishIO-Client-TS";
12065
12167
  var COMPATIBLE_SERVER_VERSIONS = [4, 5];
12066
12168
  var SDK_INFO = {