@wishknish/knishio-client-ts 0.7.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.
Files changed (141) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +425 -0
  3. package/dist/index.cjs +9961 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.iife.js +32241 -0
  6. package/dist/index.iife.js.map +1 -0
  7. package/dist/index.js +9853 -0
  8. package/dist/index.js.map +1 -0
  9. package/package.json +113 -0
  10. package/src/AuthToken.ts +214 -0
  11. package/src/KnishIOClient.ts +2020 -0
  12. package/src/constants.ts +398 -0
  13. package/src/core/Atom.ts +646 -0
  14. package/src/core/AtomMeta.ts +278 -0
  15. package/src/core/Meta.ts +428 -0
  16. package/src/core/Molecule.ts +825 -0
  17. package/src/core/PolicyMeta.ts +130 -0
  18. package/src/core/TokenUnit.ts +148 -0
  19. package/src/core/Wallet.ts +467 -0
  20. package/src/exception/AtomIndexException.ts +97 -0
  21. package/src/exception/AtomsMissingException.ts +109 -0
  22. package/src/exception/AuthorizationRejectedException.ts +63 -0
  23. package/src/exception/BalanceInsufficientException.ts +58 -0
  24. package/src/exception/BaseException.ts +275 -0
  25. package/src/exception/BatchIdException.ts +58 -0
  26. package/src/exception/CodeException.ts +119 -0
  27. package/src/exception/DecryptionKeyException.ts +65 -0
  28. package/src/exception/InvalidResponseException.ts +112 -0
  29. package/src/exception/MetaMissingException.ts +58 -0
  30. package/src/exception/MolecularHashMismatchException.ts +115 -0
  31. package/src/exception/MolecularHashMissingException.ts +58 -0
  32. package/src/exception/NegativeAmountException.ts +58 -0
  33. package/src/exception/PolicyInvalidException.ts +58 -0
  34. package/src/exception/SignatureMalformedException.ts +58 -0
  35. package/src/exception/SignatureMismatchException.ts +98 -0
  36. package/src/exception/StackableUnitAmountException.ts +65 -0
  37. package/src/exception/StackableUnitDecimalsException.ts +65 -0
  38. package/src/exception/TransferBalanceException.ts +98 -0
  39. package/src/exception/TransferMalformedException.ts +58 -0
  40. package/src/exception/TransferMismatchedException.ts +58 -0
  41. package/src/exception/TransferRemainderException.ts +58 -0
  42. package/src/exception/TransferToSelfException.ts +58 -0
  43. package/src/exception/TransferUnbalancedException.ts +58 -0
  44. package/src/exception/UnauthenticatedException.ts +147 -0
  45. package/src/exception/WalletCredentialException.ts +108 -0
  46. package/src/exception/WalletShadowException.ts +65 -0
  47. package/src/exception/WrongTokenTypeException.ts +58 -0
  48. package/src/exception/index.ts +272 -0
  49. package/src/index.ts +512 -0
  50. package/src/instance/rules/Callback.ts +257 -0
  51. package/src/instance/rules/Condition.ts +96 -0
  52. package/src/instance/rules/Meta.ts +163 -0
  53. package/src/instance/rules/Rule.ts +29 -0
  54. package/src/instance/rules/exception/RuleArgumentException.ts +65 -0
  55. package/src/libraries/CheckMolecule.ts +581 -0
  56. package/src/libraries/Decimal.ts +94 -0
  57. package/src/libraries/Dot.ts +202 -0
  58. package/src/libraries/GraphQLClient.ts +276 -0
  59. package/src/libraries/Hex.ts +155 -0
  60. package/src/libraries/UrqlClientWrapper.ts +336 -0
  61. package/src/libraries/array.ts +91 -0
  62. package/src/libraries/crypto.ts +816 -0
  63. package/src/libraries/strings.ts +458 -0
  64. package/src/mutation/Mutation.ts +103 -0
  65. package/src/mutation/MutationActiveSession.ts +108 -0
  66. package/src/mutation/MutationClaimShadowWallet.ts +91 -0
  67. package/src/mutation/MutationCreateIdentifier.ts +86 -0
  68. package/src/mutation/MutationCreateMeta.ts +92 -0
  69. package/src/mutation/MutationCreateRule.ts +90 -0
  70. package/src/mutation/MutationCreateToken.ts +92 -0
  71. package/src/mutation/MutationCreateWallet.ts +78 -0
  72. package/src/mutation/MutationDepositBufferToken.ts +72 -0
  73. package/src/mutation/MutationLinkIdentifier.ts +87 -0
  74. package/src/mutation/MutationProposeMolecule.ts +147 -0
  75. package/src/mutation/MutationRequestAuthorization.ts +77 -0
  76. package/src/mutation/MutationRequestAuthorizationGuest.ts +85 -0
  77. package/src/mutation/MutationRequestTokens.ts +98 -0
  78. package/src/mutation/MutationTransferTokens.ts +87 -0
  79. package/src/mutation/MutationWithdrawBufferToken.ts +73 -0
  80. package/src/query/Query.ts +187 -0
  81. package/src/query/QueryActiveSession.ts +89 -0
  82. package/src/query/QueryAtom.ts +275 -0
  83. package/src/query/QueryBalance.ts +102 -0
  84. package/src/query/QueryBatch.ts +145 -0
  85. package/src/query/QueryBatchHistory.ts +86 -0
  86. package/src/query/QueryContinuId.ts +91 -0
  87. package/src/query/QueryMetaType.ts +177 -0
  88. package/src/query/QueryMetaTypeViaAtom.ts +199 -0
  89. package/src/query/QueryPolicy.ts +91 -0
  90. package/src/query/QueryToken.ts +90 -0
  91. package/src/query/QueryUserActivity.ts +153 -0
  92. package/src/query/QueryWalletBundle.ts +92 -0
  93. package/src/query/QueryWalletList.ts +106 -0
  94. package/src/response/EnhancedResponse.ts +345 -0
  95. package/src/response/Response.ts +254 -0
  96. package/src/response/ResponseActiveSession.ts +72 -0
  97. package/src/response/ResponseAtom.ts +132 -0
  98. package/src/response/ResponseAuthorizationGuest.ts +119 -0
  99. package/src/response/ResponseBalance.ts +153 -0
  100. package/src/response/ResponseClaimShadowWallet.ts +56 -0
  101. package/src/response/ResponseContinuId.ts +101 -0
  102. package/src/response/ResponseCreateIdentifier.ts +56 -0
  103. package/src/response/ResponseCreateMeta.ts +58 -0
  104. package/src/response/ResponseCreateRule.ts +56 -0
  105. package/src/response/ResponseCreateToken.ts +58 -0
  106. package/src/response/ResponseCreateWallet.ts +58 -0
  107. package/src/response/ResponseLinkIdentifier.ts +87 -0
  108. package/src/response/ResponseMetaBatch.ts +72 -0
  109. package/src/response/ResponseMetaType.ts +108 -0
  110. package/src/response/ResponseMetaTypeViaAtom.ts +108 -0
  111. package/src/response/ResponsePolicy.ts +90 -0
  112. package/src/response/ResponseProposeMolecule.ts +155 -0
  113. package/src/response/ResponseQueryActiveSession.ts +105 -0
  114. package/src/response/ResponseQueryUserActivity.ts +89 -0
  115. package/src/response/ResponseRequestAuthorization.ts +100 -0
  116. package/src/response/ResponseRequestAuthorizationGuest.ts +133 -0
  117. package/src/response/ResponseRequestTokens.ts +58 -0
  118. package/src/response/ResponseTransferTokens.ts +72 -0
  119. package/src/response/ResponseWalletBundle.ts +95 -0
  120. package/src/response/ResponseWalletList.ts +165 -0
  121. package/src/schemas/index.ts +457 -0
  122. package/src/subscribe/ActiveSessionSubscribe.ts +72 -0
  123. package/src/subscribe/ActiveWalletSubscribe.ts +99 -0
  124. package/src/subscribe/CreateMoleculeSubscribe.ts +106 -0
  125. package/src/subscribe/Subscribe.ts +182 -0
  126. package/src/subscribe/WalletStatusSubscribe.ts +70 -0
  127. package/src/subscribe/index.ts +59 -0
  128. package/src/types/assertions.ts +722 -0
  129. package/src/types/client.ts +567 -0
  130. package/src/types/crypto.ts +541 -0
  131. package/src/types/graphql.ts +630 -0
  132. package/src/types/guards.ts +659 -0
  133. package/src/types/index.ts +614 -0
  134. package/src/types/response.ts +133 -0
  135. package/src/types/template-literals.ts +382 -0
  136. package/src/validation/UNIVERSAL_CONFIGURATION_INTERFACES.ts +580 -0
  137. package/src/validation/ValidationService.ts +607 -0
  138. package/src/validation/schemas.ts +447 -0
  139. package/src/versions/HashAtom.ts +170 -0
  140. package/src/versions/Version4.ts +120 -0
  141. package/src/versions/index.ts +80 -0
@@ -0,0 +1,447 @@
1
+ /*
2
+ (
3
+ (/(
4
+ (//(
5
+ (///(
6
+ (/////(
7
+ (//////( )
8
+ (////////( (/)
9
+ (////////( (///)
10
+ (//////////( (////)
11
+ (//////////( (//////)
12
+ (////////////( (///////)
13
+ (/////////////( (/////////)
14
+ (//////////////( (///////////)
15
+ (///////////////( (/////////////)
16
+ (////////////////( (//////////////)
17
+ ((((((((((((((((((( (((((((((((((((
18
+ ((((((((((((((((((( ((((((((((((((
19
+ ((((((((((((((((((( ((((((((((((((
20
+ (((((((((((((((((((( (((((((((((((
21
+ (((((((((((((((((((( ((((((((((((
22
+ ((((((((((((((((((( ((((((((((((
23
+ ((((((((((((((((((( ((((((((((
24
+ ((((((((((((((((((/ (((((((((
25
+ (((((((((((((((((( ((((((((
26
+ ((((((((((((((((( (((((((
27
+ (((((((((((((((((( (((((
28
+ ################# ##
29
+ ################ #
30
+ ################# ##
31
+ %################ ###
32
+ ###############( ####
33
+ ############### ####
34
+ ############### ######
35
+ %#############( (#######
36
+ %############# #########
37
+ ############( ##########
38
+ ########### #############
39
+ ######### ##############
40
+ %######
41
+
42
+ Powered by Knish.IO: Connecting a Decentralized World
43
+
44
+ Please visit https://github.com/WishKnish/KnishIO-Client-TS for information.
45
+
46
+ License: https://github.com/WishKnish/KnishIO-Client-TS/blob/master/LICENSE
47
+ */
48
+
49
+ /**
50
+ * Zod Validation Schemas for KnishIO SDK
51
+ *
52
+ * Provides runtime validation with compile-time type safety
53
+ * Implements 2025 TypeScript best practices with Zod integration
54
+ */
55
+
56
+ import { z } from 'zod'
57
+
58
+ // =============================================================================
59
+ // CORE TYPE SCHEMAS (2025 TYPESCRIPT PATTERN)
60
+ // =============================================================================
61
+
62
+ /**
63
+ * Branded type helper for Zod schemas
64
+ * Creates runtime-validated branded types
65
+ */
66
+ function createBrandedSchema<T extends string>(
67
+ name: T,
68
+ baseSchema: z.ZodString = z.string()
69
+ ) {
70
+ return baseSchema.brand(name)
71
+ }
72
+
73
+ // Crypto and identifier schemas with validation
74
+ export const WalletAddressSchema = createBrandedSchema('WalletAddress',
75
+ z.string().regex(/^[0-9a-fA-F]{64}$/, 'Invalid wallet address format')
76
+ )
77
+
78
+ export const BundleHashSchema = createBrandedSchema('BundleHash',
79
+ z.string().regex(/^[0-9a-fA-F]{64}$/, 'Invalid bundle hash format')
80
+ )
81
+
82
+ export const PositionSchema = createBrandedSchema('Position',
83
+ z.string().regex(/^[0-9a-fA-F]{64}$/, 'Invalid position format')
84
+ )
85
+
86
+ export const MolecularHashSchema = createBrandedSchema('MolecularHash',
87
+ z.string().regex(/^[0-9a-g]+$/, 'Invalid molecular hash format (base17)')
88
+ )
89
+
90
+ export const TokenSlugSchema = createBrandedSchema('TokenSlug',
91
+ z.string().min(1).max(32).regex(/^[A-Z0-9_]+$/, 'Token slug must be uppercase alphanumeric')
92
+ )
93
+
94
+ export const BatchIdSchema = createBrandedSchema('BatchId',
95
+ z.string().uuid('Invalid batch ID format')
96
+ )
97
+
98
+ export const CellSlugSchema = createBrandedSchema('CellSlug',
99
+ z.string().min(1).max(64).regex(/^[a-zA-Z0-9\-_.]+$/, 'Invalid cell slug format')
100
+ )
101
+
102
+ // =============================================================================
103
+ // ATOM ISOTOPE SCHEMAS (DISCRIMINATED UNIONS)
104
+ // =============================================================================
105
+
106
+ const ISOTOPES = ['C', 'V', 'U', 'T', 'M', 'I', 'R', 'B', 'F'] as const
107
+ export const AtomIsotopeSchema = z.enum(ISOTOPES)
108
+
109
+ // Isotope-specific validation
110
+ export const ValueAtomSchema = z.object({
111
+ isotope: z.literal('V'),
112
+ value: z.union([z.string(), z.number()]).refine(
113
+ val => !isNaN(Number(val)),
114
+ 'Value must be numeric'
115
+ )
116
+ }).strict()
117
+
118
+ export const MetaAtomSchema = z.object({
119
+ isotope: z.literal('M'),
120
+ metaType: z.string().min(1),
121
+ metaId: z.string().min(1),
122
+ meta: z.array(z.object({
123
+ key: z.string(),
124
+ value: z.union([z.string(), z.number(), z.boolean(), z.null()])
125
+ }))
126
+ }).strict()
127
+
128
+ export const ContinueAtomSchema = z.object({
129
+ isotope: z.literal('C')
130
+ }).strict()
131
+
132
+ // =============================================================================
133
+ // METADATA SCHEMAS (ENHANCED VALIDATION)
134
+ // =============================================================================
135
+
136
+ export const MetaDataSchema: z.ZodType<any> = z.lazy(() =>
137
+ z.union([
138
+ z.string(),
139
+ z.number(),
140
+ z.boolean(),
141
+ z.null(),
142
+ z.record(z.string(), MetaDataSchema),
143
+ z.array(MetaDataSchema)
144
+ ])
145
+ )
146
+
147
+ export const AtomMetaDataSchema = z.object({
148
+ key: z.string().min(1),
149
+ value: z.union([z.string(), z.number(), z.boolean(), z.null()])
150
+ }).strict()
151
+
152
+ export const NormalizedMetaSchema = z.object({
153
+ key: z.string(),
154
+ value: z.union([z.string(), z.number(), z.boolean(), z.null()])
155
+ }).passthrough() // Allow additional properties
156
+
157
+ // =============================================================================
158
+ // CORE ENTITY SCHEMAS
159
+ // =============================================================================
160
+
161
+ export const AtomParamsSchema = z.object({
162
+ position: z.string().optional(),
163
+ walletAddress: z.union([WalletAddressSchema, z.string()]).optional(),
164
+ isotope: AtomIsotopeSchema.default('C'),
165
+ token: z.union([TokenSlugSchema, z.string()]).default('USER'),
166
+ value: z.union([z.string(), z.number(), z.null()]).optional(),
167
+ batchId: z.union([BatchIdSchema, z.string(), z.null()]).optional(),
168
+ metaType: z.string().optional(),
169
+ metaId: z.string().optional(),
170
+ meta: z.array(AtomMetaDataSchema).optional(),
171
+ otsFragment: z.string().optional(),
172
+ index: z.number().int().min(0).optional(),
173
+ createdAt: z.string().optional(),
174
+ version: z.number().int().min(1).optional()
175
+ }).strict()
176
+
177
+ export const WalletParamsSchema = z.object({
178
+ secret: z.string().optional(),
179
+ bundle: z.union([BundleHashSchema, z.string()]).optional(),
180
+ token: z.union([TokenSlugSchema, z.string()]).default('USER'),
181
+ address: z.union([WalletAddressSchema, z.string()]).optional(),
182
+ position: z.union([PositionSchema, z.string()]).optional(),
183
+ batchId: z.union([BatchIdSchema, z.string()]).optional(),
184
+ characters: z.string().optional()
185
+ }).strict()
186
+
187
+ export const MoleculeParamsSchema = z.object({
188
+ secret: z.string().optional(),
189
+ bundle: z.union([BundleHashSchema, z.string()]).optional(),
190
+ sourceWallet: WalletParamsSchema.optional(),
191
+ remainderWallet: WalletParamsSchema.optional(),
192
+ cellSlug: z.union([CellSlugSchema, z.string()]).optional(),
193
+ version: z.number().int().min(1).optional()
194
+ }).strict()
195
+
196
+ // =============================================================================
197
+ // CLIENT CONFIGURATION SCHEMAS (ENV VALIDATION)
198
+ // =============================================================================
199
+
200
+ export const KnishIOClientConfigSchema = z.object({
201
+ uri: z.union([
202
+ z.string().url('Invalid URI format'),
203
+ z.array(z.string().url('Invalid URI format')).min(1)
204
+ ]).optional(),
205
+ cellSlug: z.union([CellSlugSchema, z.string()]).optional(),
206
+ client: z.unknown().optional(),
207
+ socket: z.unknown().optional(),
208
+ serverSdkVersion: z.number().int().min(1).max(10).default(4),
209
+ logging: z.boolean().default(false)
210
+ }).strict()
211
+
212
+ // Environment configuration with validation
213
+ export const EnvironmentConfigSchema = z.object({
214
+ NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
215
+ KNISHIO_NODE_URI: z.string().url().optional(),
216
+ KNISHIO_CELL_SLUG: z.string().optional(),
217
+ KNISHIO_LOGGING: z.string().transform(val => val === 'true').default('false'),
218
+ KNISHIO_SERVER_SDK_VERSION: z.string().transform(val => parseInt(val, 10)).default('4')
219
+ }).partial()
220
+
221
+ // =============================================================================
222
+ // OPERATION PARAMETER SCHEMAS
223
+ // =============================================================================
224
+
225
+ export const TransferParamsSchema = z.object({
226
+ recipient: z.union([WalletAddressSchema, z.string()]),
227
+ amount: z.union([z.number(), z.string()]).refine(
228
+ val => Number(val) > 0,
229
+ 'Amount must be positive'
230
+ ),
231
+ token: z.union([TokenSlugSchema, z.string()]).optional(),
232
+ callbackUrl: z.string().url().optional(),
233
+ metaType: z.string().optional(),
234
+ metaId: z.string().optional(),
235
+ meta: MetaDataSchema.optional()
236
+ }).strict()
237
+
238
+ export const CreateTokenParamsSchema = z.object({
239
+ token: z.union([TokenSlugSchema, z.string()]),
240
+ amount: z.union([z.number(), z.string()]).refine(
241
+ val => Number(val) > 0,
242
+ 'Amount must be positive'
243
+ ),
244
+ metaType: z.string().optional(),
245
+ metaId: z.string().optional(),
246
+ meta: MetaDataSchema.optional(),
247
+ fungible: z.boolean().default(true),
248
+ splittable: z.number().int().min(0).optional(),
249
+ supplyToken: z.union([TokenSlugSchema, z.string()]).optional()
250
+ }).strict()
251
+
252
+ export const RequestTokensParamsSchema = z.object({
253
+ amount: z.union([z.number(), z.string()]).refine(
254
+ val => Number(val) > 0,
255
+ 'Amount must be positive'
256
+ ),
257
+ token: z.union([TokenSlugSchema, z.string()]).optional(),
258
+ metaType: z.string().optional(),
259
+ metaId: z.string().optional(),
260
+ meta: MetaDataSchema.optional()
261
+ }).strict()
262
+
263
+ // =============================================================================
264
+ // QUERY PARAMETER SCHEMAS
265
+ // =============================================================================
266
+
267
+ export const BalanceQueryParamsSchema = z.object({
268
+ address: z.union([WalletAddressSchema, z.string()]).optional(),
269
+ bundleHash: z.union([BundleHashSchema, z.string()]).optional(),
270
+ token: z.union([TokenSlugSchema, z.string()]).optional(),
271
+ type: z.enum(['token', 'user']).optional()
272
+ }).strict()
273
+
274
+ export const MetaQueryParamsSchema = z.object({
275
+ metaType: z.string().min(1),
276
+ metaId: z.string().optional(),
277
+ key: z.string().optional(),
278
+ value: z.string().optional(),
279
+ latest: z.boolean().optional(),
280
+ filter: z.string().optional(),
281
+ queryArgs: z.record(z.unknown()).optional(),
282
+ count: z.number().int().min(0).optional(),
283
+ countBy: z.string().optional(),
284
+ cellSlug: z.union([CellSlugSchema, z.string()]).optional()
285
+ }).strict()
286
+
287
+ export const WalletQueryParamsSchema = z.object({
288
+ bundleHash: z.union([BundleHashSchema, z.string()]).optional(),
289
+ tokenSlug: z.union([TokenSlugSchema, z.string()]).optional(),
290
+ unspent: z.boolean().optional()
291
+ }).strict()
292
+
293
+ // =============================================================================
294
+ // AUTHENTICATION SCHEMAS
295
+ // =============================================================================
296
+
297
+ export const AuthTokenParamsSchema = z.object({
298
+ token: z.string().min(1),
299
+ expiresAt: z.number().int().min(0),
300
+ encrypt: z.boolean(),
301
+ pubkey: z.string().min(1)
302
+ }).strict()
303
+
304
+ export const AuthParamsSchema = z.object({
305
+ cellSlug: z.union([CellSlugSchema, z.string()]).optional(),
306
+ encrypt: z.boolean().optional(),
307
+ callback: z.function().optional()
308
+ }).strict()
309
+
310
+ export const GuestAuthParamsSchema = z.object({
311
+ cellSlug: z.union([CellSlugSchema, z.string()]).optional()
312
+ }).strict()
313
+
314
+ // =============================================================================
315
+ // GRAPHQL SCHEMAS
316
+ // =============================================================================
317
+
318
+ export const GraphQLRequestSchema = z.object({
319
+ query: z.string().min(1),
320
+ variables: z.record(z.unknown()).optional(),
321
+ operationName: z.string().optional()
322
+ }).strict()
323
+
324
+ export const GraphQLErrorSchema = z.object({
325
+ message: z.string(),
326
+ locations: z.array(z.object({
327
+ line: z.number().int(),
328
+ column: z.number().int()
329
+ })).optional(),
330
+ path: z.array(z.union([z.string(), z.number()])).optional(),
331
+ extensions: z.record(z.unknown()).optional()
332
+ }).strict()
333
+
334
+ export const GraphQLResponseSchema = z.object({
335
+ data: z.unknown().optional(),
336
+ errors: z.array(GraphQLErrorSchema).optional(),
337
+ extensions: z.record(z.unknown()).optional()
338
+ }).strict()
339
+
340
+ // =============================================================================
341
+ // VALIDATION UTILITIES (2025 PATTERNS)
342
+ // =============================================================================
343
+
344
+ /**
345
+ * Safe parsing with enhanced error information
346
+ */
347
+ export function safeParse<T>(schema: z.ZodType<T>, data: unknown) {
348
+ const result = schema.safeParse(data)
349
+
350
+ if (!result.success) {
351
+ return {
352
+ success: false as const,
353
+ error: {
354
+ issues: result.error.issues,
355
+ message: result.error.message,
356
+ formatted: result.error.format()
357
+ }
358
+ }
359
+ }
360
+
361
+ return {
362
+ success: true as const,
363
+ data: result.data
364
+ }
365
+ }
366
+
367
+ /**
368
+ * Strict validation that throws on failure
369
+ */
370
+ export function validateStrict<T>(schema: z.ZodType<T>, data: unknown, context?: string): T {
371
+ try {
372
+ return schema.parse(data)
373
+ } catch (error) {
374
+ if (error instanceof z.ZodError) {
375
+ const contextMsg = context ? ` in ${context}` : ''
376
+ throw new Error(`Validation failed${contextMsg}: ${error.message}`)
377
+ }
378
+ throw error
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Transform and validate with default fallbacks
384
+ */
385
+ export function validateWithDefaults<T>(schema: z.ZodType<T>, data: unknown): T {
386
+ return schema.parse(data)
387
+ }
388
+
389
+ /**
390
+ * Runtime type guard generator
391
+ */
392
+ export function createTypeGuard<T>(schema: z.ZodType<T>) {
393
+ return (value: unknown): value is T => {
394
+ return schema.safeParse(value).success
395
+ }
396
+ }
397
+
398
+ // =============================================================================
399
+ // SCHEMA EXPORTS FOR EXTERNAL USE
400
+ // =============================================================================
401
+
402
+ export const Schemas = {
403
+ // Core types
404
+ WalletAddress: WalletAddressSchema,
405
+ BundleHash: BundleHashSchema,
406
+ Position: PositionSchema,
407
+ MolecularHash: MolecularHashSchema,
408
+ TokenSlug: TokenSlugSchema,
409
+ BatchId: BatchIdSchema,
410
+ CellSlug: CellSlugSchema,
411
+
412
+ // Entities
413
+ AtomParams: AtomParamsSchema,
414
+ WalletParams: WalletParamsSchema,
415
+ MoleculeParams: MoleculeParamsSchema,
416
+
417
+ // Configuration
418
+ ClientConfig: KnishIOClientConfigSchema,
419
+ EnvironmentConfig: EnvironmentConfigSchema,
420
+
421
+ // Operations
422
+ TransferParams: TransferParamsSchema,
423
+ CreateTokenParams: CreateTokenParamsSchema,
424
+ RequestTokensParams: RequestTokensParamsSchema,
425
+
426
+ // Queries
427
+ BalanceQueryParams: BalanceQueryParamsSchema,
428
+ MetaQueryParams: MetaQueryParamsSchema,
429
+ WalletQueryParams: WalletQueryParamsSchema,
430
+
431
+ // Auth
432
+ AuthTokenParams: AuthTokenParamsSchema,
433
+ AuthParams: AuthParamsSchema,
434
+ GuestAuthParams: GuestAuthParamsSchema,
435
+
436
+ // GraphQL
437
+ GraphQLRequest: GraphQLRequestSchema,
438
+ GraphQLResponse: GraphQLResponseSchema
439
+ } as const
440
+
441
+ // Type exports for compile-time usage
442
+ export type ValidatedWalletAddress = z.infer<typeof WalletAddressSchema>
443
+ export type ValidatedBundleHash = z.infer<typeof BundleHashSchema>
444
+ export type ValidatedTokenSlug = z.infer<typeof TokenSlugSchema>
445
+ export type ValidatedClientConfig = z.infer<typeof KnishIOClientConfigSchema>
446
+ export type ValidatedTransferParams = z.infer<typeof TransferParamsSchema>
447
+ export type ValidatedGraphQLResponse = z.infer<typeof GraphQLResponseSchema>
@@ -0,0 +1,170 @@
1
+ /*
2
+ (
3
+ (/(
4
+ (//(
5
+ (///(
6
+ (/////(
7
+ (//////( )
8
+ (////////( (/)
9
+ (////////( (///)
10
+ (//////////( (////)
11
+ (//////////( (//////)
12
+ (////////////( (///////)
13
+ (/////////////( (/////////)
14
+ (//////////////( (///////////)
15
+ (///////////////( (/////////////)
16
+ (////////////////( (//////////////)
17
+ ((((((((((((((((((( (((((((((((((((
18
+ ((((((((((((((((((( ((((((((((((((
19
+ ((((((((((((((((((( ((((((((((((((
20
+ (((((((((((((((((((( (((((((((((((
21
+ (((((((((((((((((((( ((((((((((((
22
+ ((((((((((((((((((( ((((((((((((
23
+ ((((((((((((((((((( ((((((((((
24
+ ((((((((((((((((((/ (((((((((
25
+ (((((((((((((((((( ((((((((
26
+ ((((((((((((((((( (((((((
27
+ (((((((((((((((((( (((((
28
+ ################# ##
29
+ ################ #
30
+ ################# ##
31
+ %################ ###
32
+ ###############( ####
33
+ ############### ####
34
+ ############### ######
35
+ %#############( (#######
36
+ %############# #########
37
+ ############( ##########
38
+ ########### #############
39
+ ######### ##############
40
+ %######
41
+
42
+ Powered by Knish.IO: Connecting a Decentralized World
43
+
44
+ Please visit https://github.com/WishKnish/KnishIO-Client-TS for information.
45
+
46
+ License: https://github.com/WishKnish/KnishIO-Client-TS/blob/master/LICENSE
47
+ */
48
+
49
+ import type Atom from '@/core/Atom'
50
+
51
+ // Define structured value types for better type safety
52
+ type StructuredValue =
53
+ | string
54
+ | number
55
+ | boolean
56
+ | null
57
+ | undefined
58
+ | StructuredObject
59
+ | StructuredArray
60
+
61
+ type StructuredObject = Record<string, StructuredValue>
62
+ type StructuredArray = StructuredValue[]
63
+
64
+ // Define types for hashable values
65
+ type HashableValue =
66
+ | StructuredValue
67
+ | Record<string, unknown>
68
+ | unknown[]
69
+
70
+ // Type guard functions
71
+ function isObject(value: unknown): value is Record<string, unknown> {
72
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
73
+ }
74
+
75
+ function isArray(value: unknown): value is unknown[] {
76
+ return Array.isArray(value)
77
+ }
78
+
79
+ function isPrimitive(value: unknown): value is string | number | boolean | null | undefined {
80
+ return value === null || value === undefined || ['string', 'number', 'boolean'].includes(typeof value)
81
+ }
82
+
83
+ /**
84
+ * HashAtom - Base class for versioned atomic hashing with enhanced type safety
85
+ *
86
+ * Implements TypeScript 2025 patterns for structured data processing
87
+ */
88
+ export default class HashAtom {
89
+ /**
90
+ * Creates a HashAtom instance from an Atom with proper typing
91
+ */
92
+ static create<T extends HashAtom>(this: new(params: Record<string, unknown>) => T, atom: Atom): T {
93
+ const parameters: Record<string, unknown> = {}
94
+
95
+ // Extract atom properties with type safety
96
+ for (const key of Object.keys(atom)) {
97
+ if (Object.prototype.hasOwnProperty.call(atom, key)) {
98
+ parameters[key] = (atom as Record<string, unknown>)[key]
99
+ }
100
+ }
101
+
102
+ return new this(parameters)
103
+ }
104
+
105
+ /**
106
+ * Recursively structures an object or array for hashing consistency
107
+ * Eliminates any types with proper generic constraints
108
+ */
109
+ static structure<T extends HashableValue>(object: T): StructuredValue {
110
+ if (isPrimitive(object)) {
111
+ return object
112
+ }
113
+
114
+ if (isArray(object)) {
115
+ const result: StructuredArray = []
116
+ for (const value of object) {
117
+ result.push(HashAtom.isStructure(value) ? HashAtom.structure(value) : value as StructuredValue)
118
+ }
119
+ return result
120
+ }
121
+
122
+ if (isObject(object)) {
123
+ const result: Array<Record<string, StructuredValue>> = []
124
+ const keys = Object.keys(object).sort((first, second) => {
125
+ if (first === second) {
126
+ return 0
127
+ }
128
+ return (first < second) ? -1 : 1
129
+ })
130
+
131
+ for (const key of keys) {
132
+ if (Object.prototype.hasOwnProperty.call(object, key)) {
133
+ const item: Record<string, StructuredValue> = {}
134
+ const value = object[key]
135
+ item[key] = HashAtom.isStructure(value) ? HashAtom.structure(value) : value as StructuredValue
136
+ result.push(item)
137
+ }
138
+ }
139
+
140
+ if (result.length > 0) {
141
+ return result
142
+ }
143
+ }
144
+
145
+ // Fallback for unknown types
146
+ return object as StructuredValue
147
+ }
148
+
149
+ /**
150
+ * Type guard to determine if a value needs recursive structure processing
151
+ */
152
+ static isStructure(structure: unknown): structure is Record<string, unknown> | unknown[] {
153
+ return isObject(structure) || isArray(structure)
154
+ }
155
+
156
+ /**
157
+ * Returns the structured view of this HashAtom instance
158
+ */
159
+ view(): StructuredValue {
160
+ return HashAtom.structure(this as Record<string, unknown>)
161
+ }
162
+
163
+ /**
164
+ * Protected constructor to support subclass instantiation
165
+ */
166
+ protected constructor(params: Record<string, unknown> = {}) {
167
+ // Assign parameters to instance
168
+ Object.assign(this, params)
169
+ }
170
+ }