@wishknish/knishio-client-js 0.6.4 → 0.7.4

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 (119) hide show
  1. package/dist/client.cjs.js +513 -0
  2. package/dist/client.cjs.js.map +1 -0
  3. package/dist/client.es.mjs +8176 -0
  4. package/dist/client.es.mjs.map +1 -0
  5. package/dist/client.iife.js +30 -45
  6. package/dist/client.iife.js.map +1 -0
  7. package/package.json +48 -28
  8. package/src/Atom.js +132 -5
  9. package/src/AtomMeta.js +7 -8
  10. package/src/AuthToken.js +1 -1
  11. package/src/KnishIOClient.js +133 -76
  12. package/src/Meta.js +6 -2
  13. package/src/Molecule.js +289 -38
  14. package/src/PolicyMeta.js +1 -1
  15. package/src/Wallet.js +19 -11
  16. package/src/exception/AtomIndexException.js +1 -1
  17. package/src/exception/AtomsMissingException.js +1 -1
  18. package/src/exception/AuthorizationRejectedException.js +1 -1
  19. package/src/exception/BalanceInsufficientException.js +1 -1
  20. package/src/exception/BatchIdException.js +1 -1
  21. package/src/exception/CodeException.js +1 -1
  22. package/src/exception/DecryptionKeyException.js +1 -1
  23. package/src/exception/InvalidResponseException.js +1 -1
  24. package/src/exception/MetaMissingException.js +1 -1
  25. package/src/exception/MolecularHashMismatchException.js +1 -1
  26. package/src/exception/MolecularHashMissingException.js +1 -1
  27. package/src/exception/NegativeAmountException.js +1 -1
  28. package/src/exception/PolicyInvalidException.js +1 -1
  29. package/src/exception/SignatureMalformedException.js +1 -1
  30. package/src/exception/SignatureMismatchException.js +1 -1
  31. package/src/exception/StackableUnitAmountException.js +1 -1
  32. package/src/exception/StackableUnitDecimalsException.js +1 -1
  33. package/src/exception/TransferBalanceException.js +1 -1
  34. package/src/exception/TransferMalformedException.js +1 -1
  35. package/src/exception/TransferMismatchedException.js +1 -1
  36. package/src/exception/TransferRemainderException.js +1 -1
  37. package/src/exception/TransferToSelfException.js +1 -1
  38. package/src/exception/TransferUnbalancedException.js +1 -1
  39. package/src/exception/UnauthenticatedException.js +1 -1
  40. package/src/exception/WalletCredentialException.js +1 -1
  41. package/src/exception/WalletShadowException.js +1 -1
  42. package/src/exception/WrongTokenTypeException.js +1 -1
  43. package/src/exception/index.js +25 -25
  44. package/src/index.js +23 -9
  45. package/src/instance/Rules/Callback.js +5 -5
  46. package/src/instance/Rules/Condition.js +1 -1
  47. package/src/instance/Rules/Rule.js +4 -4
  48. package/src/instance/Rules/exception/RuleArgumentException.js +1 -1
  49. package/src/libraries/CheckMolecule.js +76 -24
  50. package/src/libraries/crypto.js +16 -2
  51. package/src/libraries/strings.js +1 -1
  52. package/src/libraries/urql/UrqlClientWrapper.js +3 -1
  53. package/src/mutation/Mutation.js +2 -2
  54. package/src/mutation/MutationActiveSession.js +2 -2
  55. package/src/mutation/MutationAppendRequest.js +91 -0
  56. package/src/mutation/MutationClaimShadowWallet.js +3 -3
  57. package/src/mutation/MutationCreateIdentifier.js +2 -2
  58. package/src/mutation/MutationCreateMeta.js +2 -2
  59. package/src/mutation/MutationCreateRule.js +2 -2
  60. package/src/mutation/MutationCreateToken.js +2 -2
  61. package/src/mutation/MutationCreateWallet.js +2 -2
  62. package/src/mutation/MutationDepositBufferToken.js +1 -1
  63. package/src/mutation/MutationLinkIdentifier.js +2 -2
  64. package/src/mutation/MutationPeering.js +82 -0
  65. package/src/mutation/MutationProposeMolecule.js +2 -2
  66. package/src/mutation/MutationRequestAuthorization.js +2 -2
  67. package/src/mutation/MutationRequestAuthorizationGuest.js +2 -2
  68. package/src/mutation/MutationRequestTokens.js +2 -2
  69. package/src/mutation/MutationTransferTokens.js +2 -2
  70. package/src/mutation/MutationWithdrawBufferToken.js +1 -1
  71. package/src/query/Query.js +2 -2
  72. package/src/query/QueryActiveSession.js +2 -2
  73. package/src/query/QueryAtom.js +2 -2
  74. package/src/query/QueryBalance.js +2 -2
  75. package/src/query/QueryBatch.js +2 -2
  76. package/src/query/QueryBatchHistory.js +3 -3
  77. package/src/query/QueryContinuId.js +2 -2
  78. package/src/query/QueryMetaType.js +11 -5
  79. package/src/query/QueryMetaTypeViaAtom.js +11 -4
  80. package/src/query/QueryPolicy.js +2 -2
  81. package/src/query/QueryToken.js +2 -2
  82. package/src/query/QueryUserActivity.js +2 -2
  83. package/src/query/QueryWalletBundle.js +2 -2
  84. package/src/query/QueryWalletList.js +2 -2
  85. package/src/response/Response.js +168 -4
  86. package/src/response/ResponseActiveSession.js +2 -2
  87. package/src/response/{ResponseMetaBatch.js → ResponseAppendRequest.js} +4 -21
  88. package/src/response/ResponseAtom.js +2 -2
  89. package/src/response/ResponseAuthorizationGuest.js +4 -4
  90. package/src/response/ResponseBalance.js +9 -4
  91. package/src/response/ResponseClaimShadowWallet.js +1 -1
  92. package/src/response/ResponseContinuId.js +3 -3
  93. package/src/response/ResponseCreateIdentifier.js +1 -1
  94. package/src/response/ResponseCreateMeta.js +1 -1
  95. package/src/response/ResponseCreateRule.js +1 -1
  96. package/src/response/ResponseCreateToken.js +1 -1
  97. package/src/response/ResponseCreateWallet.js +1 -1
  98. package/src/response/ResponseLinkIdentifier.js +3 -3
  99. package/src/response/ResponseMetaType.js +2 -2
  100. package/src/response/ResponseMetaTypeViaAtom.js +2 -2
  101. package/src/response/ResponsePeering.js +55 -0
  102. package/src/response/ResponsePolicy.js +2 -2
  103. package/src/response/ResponseProposeMolecule.js +3 -3
  104. package/src/response/ResponseQueryActiveSession.js +2 -2
  105. package/src/response/ResponseQueryUserActivity.js +2 -2
  106. package/src/response/ResponseRequestAuthorization.js +3 -3
  107. package/src/response/ResponseRequestAuthorizationGuest.js +3 -3
  108. package/src/response/ResponseRequestTokens.js +1 -1
  109. package/src/response/ResponseTransferTokens.js +1 -1
  110. package/src/response/ResponseWalletBundle.js +3 -3
  111. package/src/response/ResponseWalletList.js +4 -4
  112. package/src/subscribe/ActiveSessionSubscribe.js +1 -1
  113. package/src/subscribe/ActiveWalletSubscribe.js +1 -1
  114. package/src/subscribe/CreateMoleculeSubscribe.js +1 -1
  115. package/src/subscribe/Subscribe.js +1 -1
  116. package/src/subscribe/WalletStatusSubscribe.js +1 -1
  117. package/src/types/index.js +2 -2
  118. package/src/versions/Version4.js +1 -1
  119. package/src/versions/index.js +1 -1
package/src/Molecule.js CHANGED
@@ -45,27 +45,26 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import Atom from './Atom'
49
- import AtomMeta from './AtomMeta'
50
- import Wallet from './Wallet'
48
+ import Atom from './Atom.js'
49
+ import AtomMeta from './AtomMeta.js'
50
+ import Wallet from './Wallet.js'
51
51
  import JsSHA from 'jssha'
52
52
  import {
53
53
  chunkSubstr,
54
54
  hexToBase64
55
- } from './libraries/strings'
56
- import CheckMolecule from './libraries/CheckMolecule'
55
+ } from './libraries/strings.js'
56
+ import CheckMolecule from './libraries/CheckMolecule.js'
57
57
  import {
58
58
  generateBatchId,
59
59
  generateBundleHash
60
- } from './libraries/crypto'
61
- import { deepCloning } from './libraries/array'
62
- import Dot from './libraries/Dot'
63
- import Rule from './instance/Rules/Rule'
64
- import AtomsMissingException from './exception/AtomsMissingException'
65
- import BalanceInsufficientException from './exception/BalanceInsufficientException'
66
- import NegativeAmountException from './exception/NegativeAmountException'
67
- import SignatureMalformedException from './exception/SignatureMalformedException'
68
- import versions from './versions/index'
60
+ } from './libraries/crypto.js'
61
+ import Dot from './libraries/Dot.js'
62
+ import Rule from './instance/Rules/Rule.js'
63
+ import AtomsMissingException from './exception/AtomsMissingException.js'
64
+ import BalanceInsufficientException from './exception/BalanceInsufficientException.js'
65
+ import NegativeAmountException from './exception/NegativeAmountException.js'
66
+ import SignatureMalformedException from './exception/SignatureMalformedException.js'
67
+ import versions from './versions/index.js'
69
68
 
70
69
  /**
71
70
  * Molecule class used for committing changes to the ledger
@@ -320,11 +319,38 @@ export default class Molecule {
320
319
  * @return {Molecule}
321
320
  */
322
321
  addContinuIdAtom () {
322
+ // If remainder wallet is not USER token, create a new USER remainder wallet
323
+ if (!this.remainderWallet || this.remainderWallet.token !== 'USER') {
324
+ this.remainderWallet = Wallet.create({
325
+ secret: this.secret,
326
+ bundle: this.bundle
327
+ })
328
+ }
329
+
330
+ // Build ContinuID chain metadata (matches Rust SDK GAP-07-002)
331
+ const continuIdMeta = {}
332
+
333
+ // previousPosition: source wallet position being consumed
334
+ if (this.sourceWallet && this.sourceWallet.position) {
335
+ continuIdMeta.previousPosition = this.sourceWallet.position
336
+ }
337
+
338
+ // pubkey: remainder wallet's public key for chain verification
339
+ if (this.remainderWallet.pubkey) {
340
+ continuIdMeta.pubkey = this.remainderWallet.pubkey
341
+ }
342
+
343
+ // characters: encoding format
344
+ if (this.remainderWallet.characters) {
345
+ continuIdMeta.characters = this.remainderWallet.characters
346
+ }
347
+
323
348
  this.addAtom(Atom.create({
324
349
  isotope: 'I',
325
350
  wallet: this.remainderWallet,
326
351
  metaType: 'walletBundle',
327
- metaId: this.remainderWallet.bundle
352
+ metaId: this.remainderWallet.bundle,
353
+ meta: new AtomMeta(continuIdMeta)
328
354
  }))
329
355
  return this
330
356
  }
@@ -512,11 +538,11 @@ export default class Molecule {
512
538
  throw new BalanceInsufficientException()
513
539
  }
514
540
 
515
- // Initializing a new Atom to remove tokens from source
541
+ // Initializing a new Atom to remove tokens from source (debit full balance)
516
542
  this.addAtom(Atom.create({
517
543
  isotope: 'V',
518
544
  wallet: this.sourceWallet,
519
- value: -amount
545
+ value: -this.sourceWallet.balance
520
546
  }))
521
547
  // Initializing a new Atom to add tokens to recipient
522
548
  this.addAtom(Atom.create({
@@ -526,7 +552,7 @@ export default class Molecule {
526
552
  metaType: 'walletBundle',
527
553
  metaId: recipientWallet.bundle
528
554
  }))
529
- // Ininitlizing a remainder atom
555
+ // Initializing a remainder atom
530
556
  this.addAtom(Atom.create({
531
557
  isotope: 'V',
532
558
  wallet: this.remainderWallet,
@@ -560,11 +586,11 @@ export default class Molecule {
560
586
  })
561
587
  bufferWallet.tradeRates = tradeRates
562
588
 
563
- // Initializing a new Atom to remove tokens from source
589
+ // Initializing a new Atom to remove tokens from source (debit full balance)
564
590
  this.addAtom(Atom.create({
565
591
  isotope: 'V',
566
592
  wallet: this.sourceWallet,
567
- value: -amount
593
+ value: -this.sourceWallet.balance
568
594
  }))
569
595
 
570
596
  // Initializing a new Atom to add tokens to recipient
@@ -612,11 +638,11 @@ export default class Molecule {
612
638
  firstAtomMeta.setSigningWallet(signingWallet)
613
639
  }
614
640
 
615
- // Initializing a new Atom to remove tokens from source
641
+ // Initializing a new Atom to remove tokens from source (debit full balance)
616
642
  this.addAtom(Atom.create({
617
643
  isotope: 'B',
618
644
  wallet: this.sourceWallet,
619
- value: -amount,
645
+ value: -this.sourceWallet.balance,
620
646
  meta: firstAtomMeta,
621
647
  metaType: 'walletBundle',
622
648
  metaId: this.sourceWallet.bundle
@@ -812,14 +838,67 @@ export default class Molecule {
812
838
  meta: new AtomMeta(meta)
813
839
  }))
814
840
 
815
- this.addPolicyAtom({
841
+ // Only add policy atom if policy is provided and not empty
842
+ if (policy && Object.keys(policy).length > 0) {
843
+ this.addPolicyAtom({
844
+ metaType,
845
+ metaId,
846
+ meta,
847
+ policy
848
+ })
849
+ }
850
+
851
+ // User remainder atom
852
+ this.addContinuIdAtom()
853
+
854
+ return this
855
+ }
856
+
857
+ /**
858
+ * Initialize a P-type molecule for peer registration
859
+ *
860
+ * @param {string} host - The peer host URL to register
861
+ * @return {Molecule}
862
+ */
863
+ initPeering ({
864
+ host
865
+ }) {
866
+ this.addAtom(Atom.create({
867
+ isotope: 'P',
868
+ wallet: this.sourceWallet,
869
+ metaType: 'walletBundle',
870
+ metaId: this.bundle,
871
+ meta: new AtomMeta({ peerHost: host })
872
+ }))
873
+
874
+ this.addContinuIdAtom()
875
+
876
+ return this
877
+ }
878
+
879
+ /**
880
+ * Initialize an A-type molecule for an append request
881
+ *
882
+ * @param {string} metaType - The target MetaType to append to
883
+ * @param {string} metaId - The target MetaId to append to
884
+ * @param {string} action - The action to perform
885
+ * @param {object} meta - Additional metadata
886
+ * @return {Molecule}
887
+ */
888
+ initAppendRequest ({
889
+ metaType,
890
+ metaId,
891
+ action,
892
+ meta = {}
893
+ }) {
894
+ this.addAtom(Atom.create({
895
+ isotope: 'A',
896
+ wallet: this.sourceWallet,
816
897
  metaType,
817
898
  metaId,
818
- meta,
819
- policy
820
- })
899
+ meta: new AtomMeta({ action, ...meta })
900
+ }))
821
901
 
822
- // User remainder atom
823
902
  this.addContinuIdAtom()
824
903
 
825
904
  return this
@@ -846,8 +925,7 @@ export default class Molecule {
846
925
  batchId = null
847
926
  }) {
848
927
  meta.token = token
849
-
850
- this.local = 1
928
+ meta.amount = String(amount)
851
929
 
852
930
  this.addAtom(Atom.create({
853
931
  isotope: 'T',
@@ -985,18 +1063,191 @@ export default class Molecule {
985
1063
  }
986
1064
 
987
1065
  /**
988
- * Returns JSON-ready clone minus protected properties
1066
+ * Returns JSON-ready object for cross-SDK compatibility (2025 JS best practices)
989
1067
  *
990
- * @return {object}
1068
+ * Includes all necessary fields for cross-SDK validation while excluding sensitive data.
1069
+ * Follows 2025 JavaScript best practices with proper error handling and type safety.
1070
+ *
1071
+ * @param {Object} options - Serialization options
1072
+ * @param {boolean} options.includeValidationContext - Include sourceWallet/remainderWallet for validation (default: false)
1073
+ * @param {boolean} options.includeOtsFragments - Include OTS signature fragments (default: true)
1074
+ * @param {boolean} options.secureMode - Extra security checks (default: false)
1075
+ * @return {Object} JSON-serializable object
1076
+ * @throws {Error} If molecule is in invalid state for serialization
1077
+ */
1078
+ toJSON (options = {}) {
1079
+ const {
1080
+ includeValidationContext = false,
1081
+ includeOtsFragments = true
1082
+ } = options;
1083
+
1084
+ try {
1085
+ // Core molecule properties (server-compatible fields only)
1086
+ const serialized = {
1087
+ status: this.status,
1088
+ molecularHash: this.molecularHash,
1089
+ createdAt: this.createdAt,
1090
+ cellSlug: this.cellSlug,
1091
+ bundle: this.bundle,
1092
+
1093
+ // Serialized atoms array with optional OTS fragments
1094
+ atoms: this.atoms.map(atom => atom.toJSON({
1095
+ includeOtsFragments
1096
+ }))
1097
+ };
1098
+
1099
+ // Extended context for Rust validator and local validation
1100
+ if (includeValidationContext) {
1101
+ serialized.cellSlugOrigin = this.cellSlugOrigin
1102
+ serialized.version = this.version
1103
+
1104
+ if (this.sourceWallet) {
1105
+ serialized.sourceWallet = {
1106
+ address: this.sourceWallet.address,
1107
+ position: this.sourceWallet.position,
1108
+ token: this.sourceWallet.token,
1109
+ balance: this.sourceWallet.balance || 0,
1110
+ bundle: this.sourceWallet.bundle,
1111
+ batchId: this.sourceWallet.batchId || null,
1112
+ characters: this.sourceWallet.characters || 'BASE64',
1113
+ pubkey: this.sourceWallet.pubkey || null,
1114
+ tokenUnits: this.sourceWallet.tokenUnits || [],
1115
+ tradeRates: this.sourceWallet.tradeRates || {},
1116
+ molecules: this.sourceWallet.molecules || {}
1117
+ };
1118
+ }
1119
+
1120
+ if (this.remainderWallet) {
1121
+ serialized.remainderWallet = {
1122
+ address: this.remainderWallet.address,
1123
+ position: this.remainderWallet.position,
1124
+ token: this.remainderWallet.token,
1125
+ balance: this.remainderWallet.balance || 0,
1126
+ bundle: this.remainderWallet.bundle,
1127
+ batchId: this.remainderWallet.batchId || null,
1128
+ characters: this.remainderWallet.characters || 'BASE64',
1129
+ pubkey: this.remainderWallet.pubkey || null,
1130
+ tokenUnits: this.remainderWallet.tokenUnits || [],
1131
+ tradeRates: this.remainderWallet.tradeRates || {},
1132
+ molecules: this.remainderWallet.molecules || {}
1133
+ };
1134
+ }
1135
+ }
1136
+
1137
+ return serialized;
1138
+
1139
+ } catch (error) {
1140
+ throw new Error(`Molecule serialization failed: ${error.message}`);
1141
+ }
1142
+ }
1143
+
1144
+ /**
1145
+ * Creates a Molecule instance from JSON data (2025 JS best practices)
1146
+ *
1147
+ * Handles cross-SDK deserialization with robust error handling and validation.
1148
+ * Essential for cross-platform molecule validation and compatibility testing.
1149
+ *
1150
+ * @param {string|Object} json - JSON string or object to deserialize
1151
+ * @param {Object} options - Deserialization options
1152
+ * @param {boolean} options.includeValidationContext - Reconstruct sourceWallet/remainderWallet (default: false)
1153
+ * @param {boolean} options.validateStructure - Validate required fields (default: true)
1154
+ * @return {Molecule} Reconstructed molecule instance
1155
+ * @throws {Error} If JSON is invalid or required fields are missing
991
1156
  */
992
- toJSON () {
993
- const clone = deepCloning(this)
994
- for (const key of ['remainderWallet', 'secret', 'sourceWallet', 'cellSlugOrigin', 'version']) {
995
- if (Object.prototype.hasOwnProperty.call(clone, key)) {
996
- delete clone[key]
1157
+ static fromJSON (json, options = {}) {
1158
+ const {
1159
+ includeValidationContext = false,
1160
+ validateStructure = true
1161
+ } = options;
1162
+
1163
+ try {
1164
+ // Parse JSON safely
1165
+ const data = typeof json === 'string' ? JSON.parse(json) : json;
1166
+
1167
+ // Validate required fields in strict mode
1168
+ if (validateStructure) {
1169
+ if (!data.molecularHash || !Array.isArray(data.atoms)) {
1170
+ throw new Error('Invalid molecule data: missing molecularHash or atoms array');
1171
+ }
1172
+ }
1173
+
1174
+ // Create minimal molecule instance (never include secret from JSON)
1175
+ const molecule = new Molecule({
1176
+ secret: null,
1177
+ bundle: data.bundle || null,
1178
+ cellSlug: data.cellSlug || null,
1179
+ version: data.version || null
1180
+ });
1181
+
1182
+ // Populate core properties
1183
+ molecule.status = data.status;
1184
+ molecule.molecularHash = data.molecularHash;
1185
+ molecule.createdAt = data.createdAt || String(+new Date());
1186
+ molecule.cellSlugOrigin = data.cellSlugOrigin;
1187
+
1188
+ // Reconstruct atoms array with proper Atom instances
1189
+ if (Array.isArray(data.atoms)) {
1190
+ molecule.atoms = data.atoms.map((atomData, index) => {
1191
+ try {
1192
+ return Atom.fromJSON(atomData);
1193
+ } catch (error) {
1194
+ throw new Error(`Failed to reconstruct atom ${index}: ${error.message}`);
1195
+ }
1196
+ });
1197
+ }
1198
+
1199
+ // Reconstruct validation context if available and requested
1200
+ if (includeValidationContext) {
1201
+ if (data.sourceWallet) {
1202
+ // Create source wallet for validation (without secret for security)
1203
+ molecule.sourceWallet = new Wallet({
1204
+ secret: null,
1205
+ token: data.sourceWallet.token,
1206
+ position: data.sourceWallet.position,
1207
+ bundle: data.sourceWallet.bundle,
1208
+ batchId: data.sourceWallet.batchId,
1209
+ characters: data.sourceWallet.characters
1210
+ });
1211
+
1212
+ // Set additional properties for validation context
1213
+ molecule.sourceWallet.balance = data.sourceWallet.balance || 0;
1214
+ molecule.sourceWallet.address = data.sourceWallet.address;
1215
+ if (data.sourceWallet.pubkey) {
1216
+ molecule.sourceWallet.pubkey = data.sourceWallet.pubkey;
1217
+ }
1218
+ molecule.sourceWallet.tokenUnits = data.sourceWallet.tokenUnits || [];
1219
+ molecule.sourceWallet.tradeRates = data.sourceWallet.tradeRates || {};
1220
+ molecule.sourceWallet.molecules = data.sourceWallet.molecules || {};
1221
+ }
1222
+
1223
+ if (data.remainderWallet) {
1224
+ // Create remainder wallet for validation (without secret for security)
1225
+ molecule.remainderWallet = new Wallet({
1226
+ secret: null,
1227
+ token: data.remainderWallet.token,
1228
+ position: data.remainderWallet.position,
1229
+ bundle: data.remainderWallet.bundle,
1230
+ batchId: data.remainderWallet.batchId,
1231
+ characters: data.remainderWallet.characters
1232
+ });
1233
+
1234
+ // Set additional properties for validation context
1235
+ molecule.remainderWallet.balance = data.remainderWallet.balance || 0;
1236
+ molecule.remainderWallet.address = data.remainderWallet.address;
1237
+ if (data.remainderWallet.pubkey) {
1238
+ molecule.remainderWallet.pubkey = data.remainderWallet.pubkey;
1239
+ }
1240
+ molecule.remainderWallet.tokenUnits = data.remainderWallet.tokenUnits || [];
1241
+ molecule.remainderWallet.tradeRates = data.remainderWallet.tradeRates || {};
1242
+ molecule.remainderWallet.molecules = data.remainderWallet.molecules || {};
1243
+ }
997
1244
  }
1245
+
1246
+ return molecule;
1247
+
1248
+ } catch (error) {
1249
+ throw new Error(`Molecule deserialization failed: ${error.message}`);
998
1250
  }
999
- return clone
1000
1251
  }
1001
1252
 
1002
1253
  /**
@@ -1005,7 +1256,7 @@ export default class Molecule {
1005
1256
  * @param senderWallet
1006
1257
  */
1007
1258
  check (senderWallet = null) {
1008
- (new CheckMolecule(this)).verify(senderWallet)
1259
+ return (new CheckMolecule(this)).verify(senderWallet)
1009
1260
  }
1010
1261
 
1011
1262
  /**
package/src/PolicyMeta.js CHANGED
@@ -46,7 +46,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
48
 
49
- import { diff } from './libraries/array'
49
+ import { diff } from './libraries/array.js'
50
50
 
51
51
  /**
52
52
  *
package/src/Wallet.js CHANGED
@@ -50,15 +50,16 @@ import {
50
50
  chunkSubstr,
51
51
  isHex,
52
52
  randomString
53
- } from './libraries/strings'
53
+ } from './libraries/strings.js'
54
54
  import {
55
55
  generateBatchId,
56
56
  generateBundleHash,
57
- generateSecret
58
- } from './libraries/crypto'
59
- import TokenUnit from './TokenUnit'
60
- import WalletCredentialException from './exception/WalletCredentialException'
61
- import { ml_kem768 as MlKEM768 } from '@noble/post-quantum/ml-kem'
57
+ generateSecret,
58
+ shake256
59
+ } from './libraries/crypto.js'
60
+ import TokenUnit from './TokenUnit.js'
61
+ import WalletCredentialException from './exception/WalletCredentialException.js'
62
+ import { ml_kem768 as MlKEM768 } from '@noble/post-quantum/ml-kem.js'
62
63
 
63
64
  /**
64
65
  * Wallet class represents the set of public and private
@@ -207,11 +208,16 @@ export default class Wallet {
207
208
  token,
208
209
  position
209
210
  }) {
211
+ // Normalize non-hex secret via SHAKE256 (matching Rust/Kotlin behavior)
212
+ const secretHex = isHex(secret) ? secret : shake256(secret, 1024)
213
+ // Normalize non-hex position via SHAKE256
214
+ const positionHex = isHex(position) ? position : shake256(position, 256)
215
+
210
216
  // Converting secret to bigInt
211
- const bigIntSecret = BigInt(`0x${ secret }`)
217
+ const bigIntSecret = BigInt(`0x${ secretHex }`)
212
218
 
213
219
  // Adding new position to the user secret to produce the indexed key
214
- const indexedKey = bigIntSecret + BigInt(`0x${ position }`)
220
+ const indexedKey = bigIntSecret + BigInt(`0x${ positionHex }`)
215
221
 
216
222
  // Hashing the indexed key to produce the intermediate key
217
223
  const intermediateKeySponge = new JsSHA('SHAKE256', 'TEXT')
@@ -270,16 +276,18 @@ export default class Wallet {
270
276
  * Initializes the ML-KEM key pair
271
277
  */
272
278
  initializeMLKEM () {
273
- // Generate a 64-byte (512-bit) seed from the Knish.IO private key
274
- const seedHex = generateSecret(this.key, 64)
279
+ // Generate a 64-byte (512-bit) seed from the Knish.IO private key
280
+ // Use deterministic approach: generateSecret(key, 128) → 128 hex chars = 64 bytes
281
+ const seedHex = generateSecret(this.key, 128) // 128 hex chars = 64 bytes
275
282
 
276
- // Convert the hex string to a Uint8Array
283
+ // Convert the hex string to a Uint8Array
277
284
  const seed = new Uint8Array(64)
278
285
  for (let i = 0; i < 64; i++) {
279
286
  seed[i] = parseInt(seedHex.substr(i * 2, 2), 16)
280
287
  }
281
288
 
282
289
  const { publicKey, secretKey } = MlKEM768.keygen(seed)
290
+
283
291
  this.pubkey = this.serializeKey(publicKey)
284
292
  this.privkey = secretKey // Note: We're keeping privkey as UInt8Array for security
285
293
  }
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Atoms must have an index so that they can be sorted in a Molecule
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Empty molecules are not allowed
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Thrown when attempt to authorize is rejected by the node
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Thrown when the node does not have enough tokens to fulfill a T request
@@ -1,4 +1,4 @@
1
- import BaseException from './BaseException'
1
+ import BaseException from './BaseException.js'
2
2
 
3
3
  /**
4
4
  * Generic code exception
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Generic code exception
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Decryption key exception
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Exception for bad GraphQL responses
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * M isotope atoms must always contain metadata
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Molecular hash does not match - contents may have been tampered with
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * A molecular hash is always required.
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Thrown when a negative amount is provided
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Exception for bad GraphQL responses
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Thrown when a molecular signature is malformed
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Thrown when signature does not match the public key
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Thrown when a stackable token is being transferred with both unit IDs AND an amount specified (ambiguous)
@@ -45,7 +45,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import BaseException from './BaseException'
48
+ import BaseException from './BaseException.js'
49
49
 
50
50
  /**
51
51
  * Thrown when a stackable token is being created with units and non-zero decimals