@wishknish/knishio-client-js 0.7.4 → 0.7.5

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.
@@ -63,6 +63,7 @@ import WrongTokenTypeException from './../exception/WrongTokenTypeException.js'
63
63
  import BatchIdException from './../exception/BatchIdException.js'
64
64
  import Atom from './../Atom.js'
65
65
  import Meta from './../Meta.js'
66
+ import Molecule from './../Molecule.js'
66
67
  import Wallet from './../Wallet.js'
67
68
  import Rule from '../instance/Rules/Rule.js'
68
69
  import {
@@ -119,6 +120,8 @@ export default class CheckMolecule {
119
120
  this.isotopeR() &&
120
121
  this.isotopeP() &&
121
122
  this.isotopeA() &&
123
+ this.isotopeB() &&
124
+ this.isotopeF() &&
122
125
  this.isotopeV(senderWallet)
123
126
  }
124
127
 
@@ -384,6 +387,104 @@ export default class CheckMolecule {
384
387
  return true
385
388
  }
386
389
 
390
+ /**
391
+ * Validates B-isotope (Buffer/Exchange) atoms
392
+ *
393
+ * @returns {boolean}
394
+ */
395
+ isotopeB () {
396
+ const isotopeB = this.molecule.getIsotopes('B')
397
+
398
+ if (isotopeB.length === 0) {
399
+ return true
400
+ }
401
+
402
+ for (const atom of isotopeB) {
403
+ // B atoms must reference a wallet bundle
404
+ if (!atom.metaType || atom.metaType !== 'walletBundle') {
405
+ throw new MetaMissingException('Check::isotopeB() - B-isotope atoms must have metaType "walletBundle"!')
406
+ }
407
+
408
+ if (!atom.metaId) {
409
+ throw new MetaMissingException('Check::isotopeB() - B-isotope atoms must have a metaId!')
410
+ }
411
+
412
+ // Value must be parseable as a number
413
+ const value = Number(atom.value)
414
+ if (Number.isNaN(value)) {
415
+ throw new TransferMalformedException('Check::isotopeB() - B-isotope atom value is not a valid number!')
416
+ }
417
+ }
418
+
419
+ // V+B balance conservation: sum of all V and B atom values must equal zero
420
+ const vAtoms = this.molecule.getIsotopes('V')
421
+ if (vAtoms.length > 0) {
422
+ let sum = 0
423
+ for (const atom of [...vAtoms, ...isotopeB]) {
424
+ const value = Number(atom.value)
425
+ if (!Number.isNaN(value)) {
426
+ sum += value
427
+ }
428
+ }
429
+ if (sum !== 0) {
430
+ throw new TransferUnbalancedException('Check::isotopeB() - V+B atom values do not balance to zero!')
431
+ }
432
+ }
433
+
434
+ return true
435
+ }
436
+
437
+ /**
438
+ * Validates F-isotope (Fusion/NFT) atoms
439
+ *
440
+ * @returns {boolean}
441
+ */
442
+ isotopeF () {
443
+ const isotopeF = this.molecule.getIsotopes('F')
444
+
445
+ if (isotopeF.length === 0) {
446
+ return true
447
+ }
448
+
449
+ for (const atom of isotopeF) {
450
+ // F atoms must reference a wallet bundle
451
+ if (!atom.metaType || atom.metaType !== 'walletBundle') {
452
+ throw new MetaMissingException('Check::isotopeF() - F-isotope atoms must have metaType "walletBundle"!')
453
+ }
454
+
455
+ if (!atom.metaId) {
456
+ throw new MetaMissingException('Check::isotopeF() - F-isotope atoms must have a metaId!')
457
+ }
458
+
459
+ // Value must be parseable
460
+ const value = Number(atom.value)
461
+ if (Number.isNaN(value)) {
462
+ throw new TransferMalformedException('Check::isotopeF() - F-isotope atom value is not a valid number!')
463
+ }
464
+
465
+ if (value < 0) {
466
+ throw new TransferMalformedException('Check::isotopeF() - F-isotope atom value must not be negative!')
467
+ }
468
+ }
469
+
470
+ // V+F balance conservation: sum of all V and F atom values must equal zero
471
+ const vAtoms = this.molecule.getIsotopes('V')
472
+ if (vAtoms.length > 0) {
473
+ let sum = 0
474
+ for (const atom of [...vAtoms, ...isotopeF]) {
475
+ const value = Number(atom.value)
476
+ if (!Number.isNaN(value)) {
477
+ sum += value
478
+ }
479
+ }
480
+ if (sum !== 0) {
481
+ throw new TransferUnbalancedException('Check::isotopeF() - V+F atom values do not balance to zero!')
482
+ }
483
+ }
484
+
485
+ return true
486
+ }
487
+
387
488
  /**
388
489
  *
389
490
  * @param senderWallet
@@ -396,9 +497,14 @@ export default class CheckMolecule {
396
497
  return true
397
498
  }
398
499
 
500
+ // When B or F atoms are present, cross-isotope conservation is validated
501
+ // by isotopeB()/isotopeF() — skip V-only conservation check
502
+ const hasCrossIsotope = this.molecule.getIsotopes('B').length > 0 ||
503
+ this.molecule.getIsotopes('F').length > 0
504
+
399
505
  const firstAtom = this.molecule.atoms[0]
400
506
 
401
- if (firstAtom.isotope === 'V' && isotopeV.length === 2) {
507
+ if (!hasCrossIsotope && firstAtom.isotope === 'V' && isotopeV.length === 2) {
402
508
  const endAtom = isotopeV[isotopeV.length - 1]
403
509
 
404
510
  if (firstAtom.token !== endAtom.token) {
@@ -409,6 +515,11 @@ export default class CheckMolecule {
409
515
  throw new TransferMalformedException()
410
516
  }
411
517
 
518
+ // Conservation check for 2-atom transfers
519
+ if ((Number(firstAtom.value) + Number(endAtom.value)) !== 0) {
520
+ throw new TransferUnbalancedException()
521
+ }
522
+
412
523
  return true
413
524
  }
414
525
 
@@ -454,8 +565,8 @@ export default class CheckMolecule {
454
565
  }
455
566
  }
456
567
 
457
- // All atoms must sum to zero for a balanced transaction
458
- if (sum !== 0) {
568
+ // V-only conservation: all V atoms must sum to zero (skip for B/F cross-isotope)
569
+ if (!hasCrossIsotope && sum !== 0) {
459
570
  throw new TransferUnbalancedException()
460
571
  }
461
572
 
@@ -467,7 +578,7 @@ export default class CheckMolecule {
467
578
  throw new TypeError('Invalid isotope "V" values')
468
579
  }
469
580
 
470
- const remainder = senderWallet.balance + value
581
+ const remainder = Number(senderWallet.balance) + value
471
582
 
472
583
  // Is there enough balance to send?
473
584
  if (remainder < 0) {
@@ -475,7 +586,8 @@ export default class CheckMolecule {
475
586
  }
476
587
 
477
588
  // Does the remainder match what should be there in the source wallet, if provided?
478
- if (remainder !== sum) {
589
+ // Skip for cross-isotope (B/F) conservation is validated by isotopeB()/isotopeF()
590
+ if (!hasCrossIsotope && remainder !== sum) {
479
591
  throw new TransferRemainderException()
480
592
  }
481
593
  } else if (value !== 0) {
@@ -578,4 +690,98 @@ export default class CheckMolecule {
578
690
  // Looks like we passed all the tests!
579
691
  return true
580
692
  }
693
+
694
+ /**
695
+ * Converts server-side molecule data (from GraphQL meta query responses)
696
+ * into a Molecule instance suitable for verification via CheckMolecule.
697
+ *
698
+ * Handles field mapping differences between server and client:
699
+ * - tokenSlug → token
700
+ * - metasJson (JSON string) → meta (array of {key, value})
701
+ * - bundleHash → bundle
702
+ *
703
+ * @param {object} serverData - Molecule data from GraphQL response
704
+ * @param {string} serverData.molecularHash
705
+ * @param {string} serverData.bundleHash
706
+ * @param {string|null} serverData.cellSlug
707
+ * @param {string|null} serverData.status
708
+ * @param {string|null} serverData.createdAt
709
+ * @param {array} serverData.atoms - Array of server-format atom objects
710
+ * @return {Molecule}
711
+ */
712
+ static fromServerData ({
713
+ molecularHash,
714
+ bundleHash,
715
+ cellSlug = null,
716
+ status = null,
717
+ createdAt = null,
718
+ atoms = []
719
+ }) {
720
+ const mappedAtoms = atoms.map(serverAtom => {
721
+ let meta = []
722
+ if (serverAtom.metasJson) {
723
+ try {
724
+ const parsed = JSON.parse(serverAtom.metasJson)
725
+ if (Array.isArray(parsed)) {
726
+ // Already in [{key, value}] format
727
+ meta = parsed
728
+ } else if (parsed && typeof parsed === 'object') {
729
+ // Object format {key1: val1, key2: val2} — convert to [{key, value}] pairs
730
+ meta = Object.entries(parsed).map(([key, value]) => ({ key, value }))
731
+ }
732
+ } catch (e) {
733
+ meta = []
734
+ }
735
+ }
736
+
737
+ return {
738
+ position: serverAtom.position || null,
739
+ walletAddress: serverAtom.walletAddress || null,
740
+ isotope: serverAtom.isotope || null,
741
+ token: serverAtom.tokenSlug || serverAtom.token || null,
742
+ value: serverAtom.value != null ? String(serverAtom.value) : null,
743
+ batchId: serverAtom.batchId || null,
744
+ metaType: serverAtom.metaType || null,
745
+ metaId: serverAtom.metaId || null,
746
+ meta,
747
+ index: serverAtom.index != null ? serverAtom.index : null,
748
+ otsFragment: serverAtom.otsFragment || null,
749
+ createdAt: serverAtom.createdAt || null
750
+ }
751
+ })
752
+
753
+ return Molecule.fromJSON({
754
+ molecularHash,
755
+ bundle: bundleHash,
756
+ cellSlug,
757
+ status,
758
+ createdAt,
759
+ atoms: mappedAtoms
760
+ })
761
+ }
762
+
763
+ /**
764
+ * Verifies a molecule reconstructed from server-side GraphQL data.
765
+ * Returns an object with verification result and any error details.
766
+ *
767
+ * @param {object} moleculeData - Server molecule data (same format as fromServerData)
768
+ * @return {{ molecularHash: string, verified: boolean, error: string|null }}
769
+ */
770
+ static verifyFromServerData (moleculeData) {
771
+ try {
772
+ const molecule = CheckMolecule.fromServerData(moleculeData)
773
+ new CheckMolecule(molecule).verify()
774
+ return {
775
+ molecularHash: moleculeData.molecularHash,
776
+ verified: true,
777
+ error: null
778
+ }
779
+ } catch (error) {
780
+ return {
781
+ molecularHash: moleculeData.molecularHash || null,
782
+ verified: false,
783
+ error: error.message || String(error)
784
+ }
785
+ }
786
+ }
581
787
  }
@@ -0,0 +1,223 @@
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-JS for information.
45
+
46
+ License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
+ */
48
+ import Query from './Query.js'
49
+ import ResponseMetaTypeViaMolecule from '../response/ResponseMetaTypeViaMolecule.js'
50
+ import { gql } from '@urql/core'
51
+
52
+ /**
53
+ * Query for retrieving Meta Asset information via Molecule data.
54
+ *
55
+ * Unlike QueryMetaTypeViaAtom, this query does NOT request the redundant
56
+ * instance-level `metas` field. Instead, metadata is extracted client-side
57
+ * from molecule atoms' `metasJson`, eliminating duplicate data transfer.
58
+ */
59
+ export default class QueryMetaTypeViaMolecule extends Query {
60
+ /**
61
+ * @param {UrqlClientWrapper} graphQLClient
62
+ * @param {KnishIOClient} knishIOClient
63
+ */
64
+ constructor (graphQLClient, knishIOClient) {
65
+ super(graphQLClient, knishIOClient)
66
+
67
+ this.$__query = gql`query ($metaTypes: [String!], $metaIds: [String!], $values: [String!], $keys: [String!], $latest: Boolean, $filter: [MetaFilter!], $queryArgs: QueryArgs, $countBy: String, $atomValues: [String!], $cellSlugs: [String!] ) {
68
+ MetaTypeViaAtom(
69
+ metaTypes: $metaTypes
70
+ metaIds: $metaIds
71
+ atomValues: $atomValues
72
+ cellSlugs: $cellSlugs
73
+ filter: $filter,
74
+ latest: $latest,
75
+ queryArgs: $queryArgs
76
+ countBy: $countBy
77
+ ) {
78
+ metaType,
79
+ instanceCount {
80
+ key,
81
+ value
82
+ },
83
+ instances {
84
+ metaType,
85
+ metaId,
86
+ createdAt,
87
+ metas( values: $values, keys: $keys ) {
88
+ molecularHash,
89
+ position,
90
+ key,
91
+ value,
92
+ createdAt
93
+ },
94
+ molecule {
95
+ molecularHash,
96
+ bundleHash,
97
+ cellSlug,
98
+ status,
99
+ createdAt,
100
+ atoms {
101
+ position,
102
+ walletAddress,
103
+ isotope,
104
+ tokenSlug,
105
+ value,
106
+ batchId,
107
+ metaType,
108
+ metaId,
109
+ index,
110
+ createdAt,
111
+ otsFragment,
112
+ metasJson
113
+ }
114
+ }
115
+ },
116
+ paginatorInfo {
117
+ currentPage,
118
+ total
119
+ }
120
+ }
121
+ }`
122
+ }
123
+
124
+ /**
125
+ * Builds a GraphQL-friendly variables object based on input fields
126
+ *
127
+ * @param {string|array|null} metaType
128
+ * @param {string|array|null} metaId
129
+ * @param {string|null} key
130
+ * @param {string|null} value
131
+ * @param {array|null} values
132
+ * @param {array|null} keys
133
+ * @param {array|null} atomValues
134
+ * @param {boolean|null} latest
135
+ * @param {array|null} filter
136
+ * @param {object|null} queryArgs
137
+ * @param {string|null} countBy
138
+ * @param {string|null} cellSlug
139
+ * @return {{}}
140
+ */
141
+ static createVariables ({
142
+ metaType = null,
143
+ metaId = null,
144
+ key = null,
145
+ value = null,
146
+ keys = null,
147
+ values = null,
148
+ atomValues = null,
149
+ latest = null,
150
+ filter = null,
151
+ queryArgs = null,
152
+ countBy = null,
153
+ cellSlug = null
154
+ }) {
155
+ const variables = {}
156
+
157
+ if (atomValues) {
158
+ variables.atomValues = atomValues
159
+ }
160
+
161
+ if (keys) {
162
+ variables.keys = keys
163
+ }
164
+
165
+ if (values) {
166
+ variables.values = values
167
+ }
168
+
169
+ if (metaType) {
170
+ variables.metaTypes = typeof metaType === 'string' ? [metaType] : metaType
171
+ }
172
+
173
+ if (metaId) {
174
+ variables.metaIds = typeof metaId === 'string' ? [metaId] : metaId
175
+ }
176
+
177
+ if (cellSlug) {
178
+ variables.cellSlugs = typeof cellSlug === 'string' ? [cellSlug] : cellSlug
179
+ }
180
+
181
+ if (countBy) {
182
+ variables.countBy = countBy
183
+ }
184
+
185
+ if (filter) {
186
+ variables.filter = filter
187
+ }
188
+
189
+ if (key && value) {
190
+ variables.filter = variables.filter || []
191
+ variables.filter.push({
192
+ key,
193
+ value,
194
+ comparison: '='
195
+ })
196
+ }
197
+
198
+ variables.latest = latest === true
199
+
200
+ if (queryArgs) {
201
+ if (typeof queryArgs.limit === 'undefined' || queryArgs.limit === 0) {
202
+ queryArgs.limit = '*'
203
+ }
204
+
205
+ variables.queryArgs = queryArgs
206
+ }
207
+
208
+ return variables
209
+ }
210
+
211
+ /**
212
+ * Returns a Response object
213
+ *
214
+ * @param {object} json
215
+ * @return {ResponseMetaTypeViaMolecule}
216
+ */
217
+ createResponse (json) {
218
+ return new ResponseMetaTypeViaMolecule({
219
+ query: this,
220
+ json
221
+ })
222
+ }
223
+ }
@@ -91,7 +91,7 @@ export default class ResponseContinuId extends Response {
91
91
  wallet.batchId = continuId.batchId
92
92
  wallet.characters = continuId.characters
93
93
  wallet.pubkey = continuId.pubkey
94
- wallet.balance = continuId.amount * 1.0
94
+ wallet.balance = String(continuId.amount != null ? continuId.amount : 0)
95
95
  }
96
96
 
97
97
  return wallet