@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,659 @@
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
+ * Exhaustive Type Guards for KnishIO SDK
51
+ *
52
+ * Implements 2025 TypeScript patterns:
53
+ * - Exhaustive type checking with assertNever
54
+ * - Advanced type guards with user-defined type predicates
55
+ * - Discriminated union type narrowing
56
+ * - Compile-time completeness guarantees
57
+ */
58
+
59
+ import type {
60
+ AtomIsotope,
61
+ KnishIOErrorType,
62
+ QueryType,
63
+ MutationType,
64
+ SubscriptionType,
65
+ BalanceQueryType,
66
+ NodeEnvironment
67
+ } from '../constants'
68
+
69
+ import type {
70
+ WalletAddress,
71
+ BundleHash,
72
+ TokenSlug,
73
+ Position,
74
+ MolecularHash,
75
+ CellSlug,
76
+ BatchId,
77
+ AtomParams,
78
+ WalletParams,
79
+ MoleculeParams,
80
+ ValueAtom,
81
+ MetaAtom,
82
+ ContinueAtom,
83
+ AnyAtom
84
+ } from './index'
85
+
86
+ // =============================================================================
87
+ // EXHAUSTIVE CHECKING UTILITIES (2025 TYPESCRIPT)
88
+ // =============================================================================
89
+
90
+ /**
91
+ * Exhaustive checking function to ensure all union cases are handled
92
+ * Provides compile-time guarantees for completeness
93
+ */
94
+ export function assertNever(value: never): never {
95
+ throw new Error(`Exhaustive check failed. Received unexpected value: ${JSON.stringify(value)}`)
96
+ }
97
+
98
+ /**
99
+ * Assert that all cases in a union are handled
100
+ * Usage: assertExhaustive(isotope, handleIsotope)
101
+ */
102
+ export function assertExhaustive<T>(
103
+ value: T,
104
+ handler: (value: T) => unknown
105
+ ): asserts value is never {
106
+ handler(value)
107
+ throw new Error(`Exhaustive check failed for value: ${String(value)}`)
108
+ }
109
+
110
+ // =============================================================================
111
+ // ISOTOPE TYPE GUARDS (EXHAUSTIVE)
112
+ // =============================================================================
113
+
114
+ /**
115
+ * Exhaustive isotope type guard with compile-time checking
116
+ */
117
+ export function isAtomIsotope(value: unknown): value is AtomIsotope {
118
+ return typeof value === 'string' &&
119
+ (['C', 'V', 'U', 'T', 'M', 'I', 'R', 'B', 'F'] as const).includes(value as AtomIsotope)
120
+ }
121
+
122
+ /**
123
+ * Specific isotope type guards for precise type narrowing
124
+ */
125
+ export function isContinueIsotope(value: unknown): value is 'C' {
126
+ return value === 'C'
127
+ }
128
+
129
+ export function isValueIsotope(value: unknown): value is 'V' {
130
+ return value === 'V'
131
+ }
132
+
133
+ export function isMetaIsotope(value: unknown): value is 'M' {
134
+ return value === 'M'
135
+ }
136
+
137
+ export function isUserIsotope(value: unknown): value is 'U' {
138
+ return value === 'U'
139
+ }
140
+
141
+ export function isTokenIsotope(value: unknown): value is 'T' {
142
+ return value === 'T'
143
+ }
144
+
145
+ export function isIdentityIsotope(value: unknown): value is 'I' {
146
+ return value === 'I'
147
+ }
148
+
149
+ export function isRuleIsotope(value: unknown): value is 'R' {
150
+ return value === 'R'
151
+ }
152
+
153
+ export function isBufferIsotope(value: unknown): value is 'B' {
154
+ return value === 'B'
155
+ }
156
+
157
+ export function isFuseIsotope(value: unknown): value is 'F' {
158
+ return value === 'F'
159
+ }
160
+
161
+ /**
162
+ * Exhaustive isotope handler with compile-time completeness
163
+ */
164
+ export function handleIsotope<T>(
165
+ isotope: AtomIsotope,
166
+ handlers: {
167
+ readonly C: () => T
168
+ readonly V: () => T
169
+ readonly U: () => T
170
+ readonly T: () => T
171
+ readonly M: () => T
172
+ readonly I: () => T
173
+ readonly R: () => T
174
+ readonly B: () => T
175
+ readonly F: () => T
176
+ }
177
+ ): T {
178
+ switch (isotope) {
179
+ case 'C': return handlers.C()
180
+ case 'V': return handlers.V()
181
+ case 'U': return handlers.U()
182
+ case 'T': return handlers.T()
183
+ case 'M': return handlers.M()
184
+ case 'I': return handlers.I()
185
+ case 'R': return handlers.R()
186
+ case 'B': return handlers.B()
187
+ case 'F': return handlers.F()
188
+ default: return assertNever(isotope)
189
+ }
190
+ }
191
+
192
+ // =============================================================================
193
+ // ERROR TYPE GUARDS (EXHAUSTIVE)
194
+ // =============================================================================
195
+
196
+ /**
197
+ * Exhaustive error type guard
198
+ */
199
+ export function isKnishIOErrorType(value: unknown): value is KnishIOErrorType {
200
+ const errorTypes = [
201
+ 'ATOM_INDEX_ERROR',
202
+ 'ATOMS_MISSING_ERROR',
203
+ 'AUTHORIZATION_REJECTED_ERROR',
204
+ 'BALANCE_INSUFFICIENT_ERROR',
205
+ 'BATCH_ID_ERROR',
206
+ 'CODE_ERROR',
207
+ 'DECRYPTION_KEY_ERROR',
208
+ 'INVALID_RESPONSE_ERROR',
209
+ 'META_MISSING_ERROR',
210
+ 'MOLECULAR_HASH_MISMATCH_ERROR',
211
+ 'MOLECULAR_HASH_MISSING_ERROR',
212
+ 'NEGATIVE_AMOUNT_ERROR',
213
+ 'POLICY_INVALID_ERROR',
214
+ 'SIGNATURE_MALFORMED_ERROR',
215
+ 'SIGNATURE_MISMATCH_ERROR',
216
+ 'STACKABLE_UNIT_AMOUNT_ERROR',
217
+ 'STACKABLE_UNIT_DECIMALS_ERROR',
218
+ 'TRANSFER_BALANCE_ERROR',
219
+ 'TRANSFER_MALFORMED_ERROR',
220
+ 'TRANSFER_MISMATCHED_ERROR',
221
+ 'TRANSFER_REMAINDER_ERROR',
222
+ 'TRANSFER_TO_SELF_ERROR',
223
+ 'TRANSFER_UNBALANCED_ERROR',
224
+ 'UNAUTHENTICATED_ERROR',
225
+ 'WALLET_CREDENTIAL_ERROR',
226
+ 'WALLET_SHADOW_ERROR',
227
+ 'WRONG_TOKEN_TYPE_ERROR'
228
+ ] as const
229
+
230
+ return typeof value === 'string' && errorTypes.includes(value as KnishIOErrorType)
231
+ }
232
+
233
+ /**
234
+ * Error severity type guard
235
+ */
236
+ export function isErrorSeverity(value: unknown): value is 'low' | 'medium' | 'high' {
237
+ return typeof value === 'string' && ['low', 'medium', 'high'].includes(value)
238
+ }
239
+
240
+ /**
241
+ * Exhaustive error handler with compile-time completeness
242
+ */
243
+ export function handleError<T>(
244
+ errorType: KnishIOErrorType,
245
+ handlers: Record<KnishIOErrorType, () => T>
246
+ ): T {
247
+ const handler = handlers[errorType]
248
+ if (!handler) {
249
+ return assertNever(errorType as never)
250
+ }
251
+ return handler()
252
+ }
253
+
254
+ // =============================================================================
255
+ // OPERATION TYPE GUARDS (EXHAUSTIVE)
256
+ // =============================================================================
257
+
258
+ /**
259
+ * Query type guards
260
+ */
261
+ export function isQueryType(value: unknown): value is QueryType {
262
+ const queryTypes = [
263
+ 'Balance',
264
+ 'WalletBundle',
265
+ 'WalletList',
266
+ 'Atom',
267
+ 'MetaType',
268
+ 'MetaTypeViaAtom',
269
+ 'ContinuId',
270
+ 'Batch',
271
+ 'ActiveSession',
272
+ 'Policy',
273
+ 'Token',
274
+ 'UserActivity'
275
+ ] as const
276
+
277
+ return typeof value === 'string' && queryTypes.includes(value as QueryType)
278
+ }
279
+
280
+ /**
281
+ * Mutation type guards
282
+ */
283
+ export function isMutationType(value: unknown): value is MutationType {
284
+ const mutationTypes = [
285
+ 'TransferTokens',
286
+ 'CreateWallet',
287
+ 'RequestAuthorization',
288
+ 'CreateMeta',
289
+ 'CreateToken',
290
+ 'RequestTokens',
291
+ 'ActiveSession',
292
+ 'ClaimShadowWallet',
293
+ 'CreateIdentifier',
294
+ 'CreateRule',
295
+ 'RequestAuthorizationGuest',
296
+ 'DepositBufferToken',
297
+ 'LinkIdentifier',
298
+ 'WithdrawBufferToken',
299
+ 'ProposeMolecule'
300
+ ] as const
301
+
302
+ return typeof value === 'string' && mutationTypes.includes(value as MutationType)
303
+ }
304
+
305
+ /**
306
+ * Subscription type guards
307
+ */
308
+ export function isSubscriptionType(value: unknown): value is SubscriptionType {
309
+ const subscriptionTypes = [
310
+ 'CreateMolecule',
311
+ 'ActiveSession',
312
+ 'ActiveWallet',
313
+ 'WalletStatus'
314
+ ] as const
315
+
316
+ return typeof value === 'string' && subscriptionTypes.includes(value as SubscriptionType)
317
+ }
318
+
319
+ // =============================================================================
320
+ // BRANDED TYPE GUARDS (PATTERN VALIDATION)
321
+ // =============================================================================
322
+
323
+ /**
324
+ * Enhanced wallet address validation with pattern checking
325
+ */
326
+ export function isWalletAddress(value: unknown): value is WalletAddress {
327
+ return typeof value === 'string' &&
328
+ value.length === 64 &&
329
+ /^[0-9a-fA-F]{64}$/.test(value)
330
+ }
331
+
332
+ /**
333
+ * Enhanced bundle hash validation
334
+ */
335
+ export function isBundleHash(value: unknown): value is BundleHash {
336
+ return typeof value === 'string' &&
337
+ value.length === 64 &&
338
+ /^[0-9a-fA-F]{64}$/.test(value)
339
+ }
340
+
341
+ /**
342
+ * Enhanced position validation
343
+ */
344
+ export function isPosition(value: unknown): value is Position {
345
+ return typeof value === 'string' &&
346
+ value.length === 64 &&
347
+ /^[0-9a-fA-F]{64}$/.test(value)
348
+ }
349
+
350
+ /**
351
+ * Token slug validation with pattern checking
352
+ */
353
+ export function isTokenSlug(value: unknown): value is TokenSlug {
354
+ return typeof value === 'string' &&
355
+ value.length >= 1 &&
356
+ value.length <= 32 &&
357
+ /^[A-Z0-9_]+$/.test(value)
358
+ }
359
+
360
+ /**
361
+ * Molecular hash validation (base17 format)
362
+ */
363
+ export function isMolecularHash(value: unknown): value is MolecularHash {
364
+ return typeof value === 'string' &&
365
+ value.length >= 32 &&
366
+ value.length <= 128 &&
367
+ /^[0-9a-g]+$/i.test(value)
368
+ }
369
+
370
+ /**
371
+ * Cell slug validation
372
+ */
373
+ export function isCellSlug(value: unknown): value is CellSlug {
374
+ return typeof value === 'string' &&
375
+ value.length >= 1 &&
376
+ value.length <= 64 &&
377
+ /^[a-zA-Z0-9\-_.]+$/.test(value)
378
+ }
379
+
380
+ /**
381
+ * Batch ID validation (UUID format)
382
+ */
383
+ export function isBatchId(value: unknown): value is BatchId {
384
+ return typeof value === 'string' &&
385
+ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
386
+ }
387
+
388
+ // =============================================================================
389
+ // OBJECT TYPE GUARDS (DISCRIMINATED UNIONS)
390
+ // =============================================================================
391
+
392
+ /**
393
+ * Atom parameter validation
394
+ */
395
+ export function isAtomParams(value: unknown): value is AtomParams {
396
+ if (!value || typeof value !== 'object') return false
397
+
398
+ const obj = value as Record<string, unknown>
399
+ return (
400
+ (!obj.isotope || isAtomIsotope(obj.isotope)) &&
401
+ (!obj.walletAddress || typeof obj.walletAddress === 'string') &&
402
+ (!obj.token || typeof obj.token === 'string') &&
403
+ (!obj.position || typeof obj.position === 'string')
404
+ )
405
+ }
406
+
407
+ /**
408
+ * Wallet parameter validation
409
+ */
410
+ export function isWalletParams(value: unknown): value is WalletParams {
411
+ if (!value || typeof value !== 'object') return false
412
+
413
+ const obj = value as Record<string, unknown>
414
+ return (
415
+ (!obj.secret || typeof obj.secret === 'string') &&
416
+ (!obj.bundle || typeof obj.bundle === 'string') &&
417
+ (!obj.token || typeof obj.token === 'string') &&
418
+ (!obj.address || typeof obj.address === 'string')
419
+ )
420
+ }
421
+
422
+ /**
423
+ * Molecule parameter validation
424
+ */
425
+ export function isMoleculeParams(value: unknown): value is MoleculeParams {
426
+ if (!value || typeof value !== 'object') return false
427
+
428
+ const obj = value as Record<string, unknown>
429
+ return (
430
+ (!obj.secret || typeof obj.secret === 'string') &&
431
+ (!obj.bundle || typeof obj.bundle === 'string') &&
432
+ (!obj.cellSlug || typeof obj.cellSlug === 'string') &&
433
+ (!obj.version || typeof obj.version === 'number')
434
+ )
435
+ }
436
+
437
+ // =============================================================================
438
+ // ATOM TYPE DISCRIMINATION (ADVANCED)
439
+ // =============================================================================
440
+
441
+ /**
442
+ * Advanced atom type discrimination based on isotope
443
+ */
444
+ export function isValueAtom(atom: AnyAtom): atom is ValueAtom {
445
+ return atom.isotope === 'V' && 'value' in atom && atom.value !== null
446
+ }
447
+
448
+ export function isMetaAtom(atom: AnyAtom): atom is MetaAtom {
449
+ return atom.isotope === 'M' && 'metaType' in atom && 'metaId' in atom && 'meta' in atom
450
+ }
451
+
452
+ export function isContinueAtom(atom: AnyAtom): atom is ContinueAtom {
453
+ return atom.isotope === 'C'
454
+ }
455
+
456
+ /**
457
+ * Exhaustive atom type handler
458
+ */
459
+ export function handleAtomType<T>(
460
+ atom: AnyAtom,
461
+ handlers: {
462
+ readonly value: (atom: ValueAtom) => T
463
+ readonly meta: (atom: MetaAtom) => T
464
+ readonly continue: (atom: ContinueAtom) => T
465
+ readonly default: (atom: AnyAtom) => T
466
+ }
467
+ ): T {
468
+ if (isValueAtom(atom)) {
469
+ return handlers.value(atom)
470
+ }
471
+ if (isMetaAtom(atom)) {
472
+ return handlers.meta(atom)
473
+ }
474
+ if (isContinueAtom(atom)) {
475
+ return handlers.continue(atom)
476
+ }
477
+ return handlers.default(atom)
478
+ }
479
+
480
+ // =============================================================================
481
+ // ENVIRONMENT AND CONFIGURATION GUARDS
482
+ // =============================================================================
483
+
484
+ /**
485
+ * Node environment validation
486
+ */
487
+ export function isNodeEnvironment(value: unknown): value is NodeEnvironment {
488
+ return typeof value === 'string' &&
489
+ (['development', 'production', 'test'] as const).includes(value as NodeEnvironment)
490
+ }
491
+
492
+ /**
493
+ * Balance query type validation
494
+ */
495
+ export function isBalanceQueryType(value: unknown): value is BalanceQueryType {
496
+ return typeof value === 'string' &&
497
+ (['token', 'user'] as const).includes(value as BalanceQueryType)
498
+ }
499
+
500
+ // =============================================================================
501
+ // UTILITY TYPE GUARD COMBINATORS
502
+ // =============================================================================
503
+
504
+ /**
505
+ * Create a union type guard from multiple guards
506
+ */
507
+ export function createUnionGuard<T extends readonly unknown[]>(
508
+ ...guards: { [K in keyof T]: (value: unknown) => value is T[K] }
509
+ ): (value: unknown) => value is T[number] {
510
+ return (value: unknown): value is T[number] => {
511
+ return guards.some(guard => guard(value))
512
+ }
513
+ }
514
+
515
+ /**
516
+ * Create an array type guard from an element guard
517
+ */
518
+ export function createArrayGuard<T>(
519
+ elementGuard: (value: unknown) => value is T
520
+ ): (value: unknown) => value is T[] {
521
+ return (value: unknown): value is T[] => {
522
+ return Array.isArray(value) && value.every(elementGuard)
523
+ }
524
+ }
525
+
526
+ /**
527
+ * Create an optional type guard
528
+ */
529
+ export function createOptionalGuard<T>(
530
+ guard: (value: unknown) => value is T
531
+ ): (value: unknown) => value is T | undefined {
532
+ return (value: unknown): value is T | undefined => {
533
+ return value === undefined || guard(value)
534
+ }
535
+ }
536
+
537
+ // =============================================================================
538
+ // COMPLEX VALIDATION PATTERNS
539
+ // =============================================================================
540
+
541
+ /**
542
+ * Validate object with required and optional properties
543
+ */
544
+ export function validateObjectShape<
545
+ Required extends Record<string, (value: unknown) => boolean>,
546
+ Optional extends Record<string, (value: unknown) => boolean>
547
+ >(
548
+ value: unknown,
549
+ requiredProps: Required,
550
+ optionalProps: Optional = {} as Optional
551
+ ): value is {
552
+ [K in keyof Required]: Required[K] extends (value: unknown) => value is infer T ? T : never
553
+ } & {
554
+ [K in keyof Optional]?: Optional[K] extends (value: unknown) => value is infer T ? T : never
555
+ } {
556
+ if (!value || typeof value !== 'object') return false
557
+
558
+ const obj = value as Record<string, unknown>
559
+
560
+ // Check required properties
561
+ for (const [key, guard] of Object.entries(requiredProps)) {
562
+ if (!(key in obj) || !guard(obj[key])) {
563
+ return false
564
+ }
565
+ }
566
+
567
+ // Check optional properties
568
+ for (const [key, guard] of Object.entries(optionalProps)) {
569
+ if (key in obj && !guard(obj[key])) {
570
+ return false
571
+ }
572
+ }
573
+
574
+ return true
575
+ }
576
+
577
+ // =============================================================================
578
+ // EXPORT GROUPED GUARDS
579
+ // =============================================================================
580
+
581
+ /**
582
+ * Grouped type guards for convenient access
583
+ */
584
+ export const Guards = {
585
+ // Isotope guards
586
+ isotope: {
587
+ is: isAtomIsotope,
588
+ continue: isContinueIsotope,
589
+ value: isValueIsotope,
590
+ meta: isMetaIsotope,
591
+ user: isUserIsotope,
592
+ token: isTokenIsotope,
593
+ identity: isIdentityIsotope,
594
+ rule: isRuleIsotope,
595
+ buffer: isBufferIsotope,
596
+ fuse: isFuseIsotope,
597
+ handle: handleIsotope
598
+ },
599
+
600
+ // Error guards
601
+ error: {
602
+ type: isKnishIOErrorType,
603
+ severity: isErrorSeverity,
604
+ handle: handleError
605
+ },
606
+
607
+ // Operation guards
608
+ operation: {
609
+ query: isQueryType,
610
+ mutation: isMutationType,
611
+ subscription: isSubscriptionType
612
+ },
613
+
614
+ // Branded type guards
615
+ branded: {
616
+ walletAddress: isWalletAddress,
617
+ bundleHash: isBundleHash,
618
+ position: isPosition,
619
+ tokenSlug: isTokenSlug,
620
+ molecularHash: isMolecularHash,
621
+ cellSlug: isCellSlug,
622
+ batchId: isBatchId
623
+ },
624
+
625
+ // Object guards
626
+ object: {
627
+ atomParams: isAtomParams,
628
+ walletParams: isWalletParams,
629
+ moleculeParams: isMoleculeParams
630
+ },
631
+
632
+ // Atom discrimination
633
+ atom: {
634
+ value: isValueAtom,
635
+ meta: isMetaAtom,
636
+ continue: isContinueAtom,
637
+ handle: handleAtomType
638
+ },
639
+
640
+ // Environment guards
641
+ env: {
642
+ nodeEnv: isNodeEnvironment,
643
+ balanceQueryType: isBalanceQueryType
644
+ },
645
+
646
+ // Utility guards
647
+ util: {
648
+ union: createUnionGuard,
649
+ array: createArrayGuard,
650
+ optional: createOptionalGuard,
651
+ shape: validateObjectShape
652
+ },
653
+
654
+ // Exhaustive checking
655
+ exhaustive: {
656
+ never: assertNever,
657
+ check: assertExhaustive
658
+ }
659
+ } as const