@wishknish/knishio-client-js 0.5.2 → 0.6.1

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 (133) hide show
  1. package/README.md +207 -274
  2. package/dist/client.iife.js +533 -0
  3. package/package.json +37 -79
  4. package/src/.babelrc +0 -22
  5. package/src/Atom.js +171 -132
  6. package/src/AtomMeta.js +76 -50
  7. package/src/AuthToken.js +38 -47
  8. package/src/KnishIOClient.js +934 -987
  9. package/src/Meta.js +15 -17
  10. package/src/Molecule.js +423 -494
  11. package/src/PolicyMeta.js +32 -41
  12. package/src/TokenUnit.js +30 -32
  13. package/src/Wallet.js +275 -265
  14. package/src/exception/AtomIndexException.js +4 -8
  15. package/src/exception/AtomsMissingException.js +4 -6
  16. package/src/exception/AuthorizationRejectedException.js +4 -5
  17. package/src/exception/BalanceInsufficientException.js +4 -8
  18. package/src/exception/BaseException.js +6 -8
  19. package/src/exception/BatchIdException.js +5 -7
  20. package/src/exception/CodeException.js +4 -8
  21. package/src/{libraries/ApolloLink/HttpLink.js → exception/DecryptionKeyException.js} +12 -15
  22. package/src/exception/InvalidResponseException.js +4 -5
  23. package/src/exception/MetaMissingException.js +4 -6
  24. package/src/exception/MolecularHashMismatchException.js +4 -6
  25. package/src/exception/MolecularHashMissingException.js +4 -5
  26. package/src/exception/NegativeAmountException.js +4 -5
  27. package/src/exception/PolicyInvalidException.js +4 -4
  28. package/src/exception/SignatureMalformedException.js +4 -5
  29. package/src/exception/SignatureMismatchException.js +4 -5
  30. package/src/exception/StackableUnitAmountException.js +4 -5
  31. package/src/exception/StackableUnitDecimalsException.js +4 -5
  32. package/src/exception/TransferBalanceException.js +4 -5
  33. package/src/exception/TransferMalformedException.js +4 -5
  34. package/src/exception/TransferMismatchedException.js +4 -5
  35. package/src/exception/TransferRemainderException.js +4 -5
  36. package/src/exception/TransferToSelfException.js +4 -5
  37. package/src/exception/TransferUnbalancedException.js +4 -5
  38. package/src/exception/UnauthenticatedException.js +4 -5
  39. package/src/{libraries/ApolloLink/AuthLink.js → exception/WalletCredentialException.js} +12 -40
  40. package/src/exception/WalletShadowException.js +4 -5
  41. package/src/exception/WrongTokenTypeException.js +4 -5
  42. package/src/exception/index.js +26 -26
  43. package/src/index.js +8 -10
  44. package/src/instance/Rules/Callback.js +91 -93
  45. package/src/instance/Rules/Condition.js +21 -23
  46. package/src/instance/Rules/Meta.js +13 -14
  47. package/src/instance/Rules/Rule.js +39 -43
  48. package/src/instance/Rules/exception/RuleArgumentException.js +4 -4
  49. package/src/libraries/CheckMolecule.js +253 -232
  50. package/src/libraries/Decimal.js +13 -17
  51. package/src/libraries/Dot.js +74 -48
  52. package/src/libraries/Hex.js +49 -54
  53. package/src/libraries/array.js +50 -41
  54. package/src/libraries/crypto.js +20 -27
  55. package/src/libraries/strings.js +58 -91
  56. package/src/libraries/urql/UrqlClientWrapper.js +166 -0
  57. package/src/mutation/Mutation.js +44 -25
  58. package/src/mutation/MutationActiveSession.js +12 -12
  59. package/src/mutation/MutationClaimShadowWallet.js +15 -17
  60. package/src/mutation/MutationCreateIdentifier.js +11 -12
  61. package/src/mutation/MutationCreateMeta.js +11 -12
  62. package/src/mutation/MutationCreateRule.js +11 -12
  63. package/src/mutation/MutationCreateToken.js +18 -13
  64. package/src/mutation/MutationCreateWallet.js +9 -11
  65. package/src/mutation/MutationDepositBufferToken.js +7 -9
  66. package/src/mutation/MutationLinkIdentifier.js +12 -14
  67. package/src/mutation/MutationProposeMolecule.js +24 -25
  68. package/src/mutation/MutationRequestAuthorization.js +9 -10
  69. package/src/mutation/MutationRequestAuthorizationGuest.js +12 -15
  70. package/src/mutation/MutationRequestTokens.js +11 -14
  71. package/src/mutation/MutationTransferTokens.js +11 -14
  72. package/src/mutation/MutationWithdrawBufferToken.js +7 -10
  73. package/src/query/Query.js +62 -36
  74. package/src/query/QueryActiveSession.js +11 -13
  75. package/src/query/QueryAtom.js +75 -76
  76. package/src/query/QueryBalance.js +11 -12
  77. package/src/query/QueryBatch.js +17 -14
  78. package/src/query/QueryBatchHistory.js +16 -13
  79. package/src/query/QueryContinuId.js +13 -10
  80. package/src/query/QueryMetaType.js +45 -57
  81. package/src/query/QueryMetaTypeViaAtom.js +49 -57
  82. package/src/query/QueryPolicy.js +11 -12
  83. package/src/query/QueryToken.js +11 -13
  84. package/src/query/QueryUserActivity.js +11 -13
  85. package/src/query/QueryWalletBundle.js +15 -47
  86. package/src/query/QueryWalletList.js +15 -16
  87. package/src/response/Response.js +29 -34
  88. package/src/response/ResponseActiveSession.js +6 -6
  89. package/src/response/ResponseAtom.js +29 -30
  90. package/src/response/ResponseAuthorizationGuest.js +17 -18
  91. package/src/response/ResponseBalance.js +12 -13
  92. package/src/response/ResponseClaimShadowWallet.js +1 -1
  93. package/src/response/ResponseContinuId.js +21 -22
  94. package/src/response/ResponseCreateIdentifier.js +1 -1
  95. package/src/response/ResponseCreateMeta.js +1 -1
  96. package/src/response/ResponseCreateRule.js +1 -1
  97. package/src/response/ResponseCreateToken.js +1 -1
  98. package/src/response/ResponseCreateWallet.js +1 -1
  99. package/src/response/ResponseLinkIdentifier.js +9 -10
  100. package/src/response/ResponseMetaBatch.js +6 -8
  101. package/src/response/ResponseMetaType.js +19 -20
  102. package/src/response/ResponseMetaTypeViaAtom.js +19 -19
  103. package/src/response/ResponsePolicy.js +14 -15
  104. package/src/response/ResponseProposeMolecule.js +27 -30
  105. package/src/response/ResponseQueryActiveSession.js +20 -23
  106. package/src/response/ResponseQueryUserActivity.js +11 -12
  107. package/src/response/ResponseRequestAuthorization.js +11 -16
  108. package/src/response/ResponseRequestAuthorizationGuest.js +18 -21
  109. package/src/response/ResponseRequestTokens.js +1 -1
  110. package/src/response/ResponseTransferTokens.js +8 -9
  111. package/src/response/ResponseWalletBundle.js +16 -17
  112. package/src/response/ResponseWalletList.js +44 -47
  113. package/src/subscribe/ActiveSessionSubscribe.js +5 -6
  114. package/src/subscribe/ActiveWalletSubscribe.js +5 -6
  115. package/src/subscribe/CreateMoleculeSubscribe.js +5 -5
  116. package/src/subscribe/Subscribe.js +26 -26
  117. package/src/subscribe/WalletStatusSubscribe.js +5 -6
  118. package/src/versions/HashAtom.js +78 -0
  119. package/src/versions/Version4.js +34 -0
  120. package/src/versions/index.js +5 -0
  121. package/dist/client.umd.js +0 -453
  122. package/src/httpClient/ApolloClient.js +0 -245
  123. package/src/libraries/ApolloLink/CipherLink.js +0 -117
  124. package/src/libraries/ApolloLink/Client.js +0 -231
  125. package/src/libraries/ApolloLink/PusherLink.js +0 -234
  126. package/src/libraries/ApolloLink/handler.js +0 -106
  127. package/src/libraries/Base58.js +0 -71
  128. package/src/libraries/Base64.js +0 -40
  129. package/src/libraries/BaseX.js +0 -91
  130. package/src/libraries/Soda.js +0 -93
  131. package/src/query/QueryMetaInstance.js +0 -99
  132. package/src/test/Test.js +0 -670
  133. package/src/test/TestTokenUnit.js +0 -340
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wishknish/knishio-client-js",
3
- "version": "0.5.2",
3
+ "version": "0.6.1",
4
4
  "productName": "Knish.IO Javascript SDK Client",
5
5
  "description": "JavaScript implementation of the Knish.IO SDK to consume Knish.IO GraphQL APIs.",
6
6
  "license": "GPL-3.0-or-later",
@@ -17,17 +17,19 @@
17
17
  },
18
18
  {
19
19
  "name": "Vladimir Makarov",
20
- "email": "vladimir.php@gmail.com",
20
+ "email": "vladimir@wishknish.com",
21
+ "homepage": "https://wishknish.com",
21
22
  "role": "developer"
22
23
  },
23
24
  {
24
25
  "name": "Yuri Kizilov",
25
- "email": "y.kizilov.sev@yandex.ru",
26
+ "email": "yuriy@wishknish.com",
27
+ "homepage": "https://wishknish.com",
26
28
  "role": "developer"
27
29
  }
28
30
  ],
29
- "main": "dist/client.umd.js",
30
- "module": "dist/client.esm.js",
31
+ "main": "dist/client.iife.js",
32
+ "module": "dist/client.es.js",
31
33
  "commonjs": "dist/client.cjs.js",
32
34
  "keywords": [
33
35
  "wishknish",
@@ -38,71 +40,32 @@
38
40
  "graphql"
39
41
  ],
40
42
  "dependencies": {
41
- "@apollo/client": "^3.5.10",
42
- "@stablelib/base64": "^1.0.1",
43
- "@stablelib/utf8": "^1.0.1",
44
- "base-x": "^4.0.0",
45
- "big-integer": "^1.6.51",
46
- "buffer": "^6.0.3",
47
- "core-js": "3.21.1",
48
- "get-random-values": "^1.2.2",
49
- "graphql": "^16.3.0",
43
+ "@noble/post-quantum": "^0.4.0",
44
+ "@thumbmarkjs/thumbmarkjs": "^0.19.1",
45
+ "@urql/core": "^5.1.0",
46
+ "graphql": "^16.10.0",
47
+ "graphql-ws": "^5.16.2",
50
48
  "isomorphic-fetch": "^3.0.0",
51
- "js-sha3": "^0.8.0",
52
- "pusher-js": "^7.0.6",
53
- "tweetnacl": "^1.0.3",
54
- "tweetnacl-sealedbox-js": "^1.2.0",
55
- "uri-js": "^4.4.1"
49
+ "jssha": "^3.3.1",
50
+ "wonka": "^6.3.4"
56
51
  },
57
- "files": [
58
- "/src"
59
- ],
60
52
  "devDependencies": {
61
- "@babel/core": "^7.17.8",
62
- "@babel/eslint-parser": "^7.17.0",
63
- "@babel/eslint-plugin": "^7.17.7",
64
- "@babel/plugin-external-helpers": "^7.16.7",
65
- "@babel/plugin-proposal-class-properties": "^7.16.7",
66
- "@babel/plugin-proposal-decorators": "^7.17.8",
67
- "@babel/plugin-proposal-do-expressions": "^7.16.7",
68
- "@babel/plugin-proposal-export-default-from": "^7.16.7",
69
- "@babel/plugin-proposal-export-namespace-from": "^7.16.7",
70
- "@babel/plugin-proposal-function-bind": "^7.16.7",
71
- "@babel/plugin-proposal-function-sent": "^7.16.7",
72
- "@babel/plugin-proposal-json-strings": "^7.16.7",
73
- "@babel/plugin-proposal-logical-assignment-operators": "^7.16.7",
74
- "@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.7",
75
- "@babel/plugin-proposal-numeric-separator": "^7.16.7",
76
- "@babel/plugin-proposal-optional-chaining": "^7.16.7",
77
- "@babel/plugin-proposal-pipeline-operator": "^7.17.6",
78
- "@babel/plugin-proposal-throw-expressions": "^7.16.7",
79
- "@babel/plugin-syntax-dynamic-import": "^7.8.3",
80
- "@babel/plugin-syntax-import-meta": "^7.10.4",
81
- "@babel/plugin-transform-arrow-functions": "^7.16.7",
82
- "@babel/plugin-transform-async-to-generator": "^7.16.8",
83
- "@babel/plugin-transform-classes": "^7.16.7",
84
- "@babel/plugin-transform-for-of": "^7.16.7",
85
- "@babel/plugin-transform-property-mutators": "^7.16.7",
86
- "@babel/plugin-transform-runtime": "^7.12.15",
87
- "@babel/plugin-transform-shorthand-properties": "^7.12.13",
88
- "@babel/preset-env": "^7.12.13",
89
- "@babel/runtime": "^7.12.13",
90
- "@rollup/plugin-babel": "^5.2.3",
91
- "@rollup/plugin-commonjs": "^21.0.3",
92
- "@rollup/plugin-inject": "^4.0.4",
93
- "@rollup/plugin-node-resolve": "^13.0.0",
94
- "@rollup/plugin-replace": "^4.0.0",
95
- "babel-loader": "^8.2.4",
96
- "eslint": "^8.12.0",
97
- "eslint-plugin-vue": "^8.6.0",
98
- "nodemon": "^2.0.15",
99
- "rollup": "^2.70.1",
100
- "rollup-plugin-peer-deps-external": "^2.2.4",
101
- "rollup-plugin-terser": "^7.0.2",
102
- "uglify-js": "^3.15.3",
103
- "webpack": "^5.71.0",
104
- "webpack-cli": "^4.9.2",
105
- "webpack-merge": "^5.8.0"
53
+ "@jest/globals": "^29.7.0",
54
+ "@rollup/plugin-babel": "^6.0.4",
55
+ "@rollup/plugin-commonjs": "^25.0.8",
56
+ "@rollup/plugin-node-resolve": "^15.3.1",
57
+ "esbuild-jest": "^0.5.0",
58
+ "eslint": "^8.57.1",
59
+ "eslint-config-standard": "^17.1.0",
60
+ "eslint-plugin-import": "^2.31.0",
61
+ "eslint-plugin-n": "^16.6.2",
62
+ "eslint-plugin-promise": "^6.6.0",
63
+ "eslint-plugin-vue": "^9.32.0",
64
+ "esmock": "^2.7.0",
65
+ "jest": "^29.7.0",
66
+ "rollup": "^4.34.6",
67
+ "vite": "^5.4.14",
68
+ "vite-plugin-node-polyfills": "^0.23.0"
106
69
  },
107
70
  "browserslist": [
108
71
  "> 1%",
@@ -110,17 +73,12 @@
110
73
  "not ie <= 8"
111
74
  ],
112
75
  "scripts": {
113
- "build": "yarn build:es && yarn build:umd && yarn build:cjs",
114
- "build:es": "rollup --config build/rollup.config.es.js",
115
- "build:umd": "rollup --config build/rollup.config.umd.js",
116
- "build:cjs": "rollup --config build/rollup.config.cjs.js",
117
- "dev": "nodemon --exec \"yarn build:es && yarn build:umd && yarn build:cjs\" --watch",
118
- "build:webpack:dev": "webpack --mode development --watch --stats-colors --profile --stats-error-details",
119
- "build:webpack:dev:sub": "webpack --mode development --watch --stats-colors --profile --config ./webpack.config.sub.js",
120
- "build:webpack": "webpack --mode production --progress --stats-colors --profile",
121
- "lint": "eslint --ext .js src"
76
+ "build": "vite build --config build/vite.config.mjs",
77
+ "lint": "eslint --ext .js src",
78
+ "test": "jest",
79
+ "test:coverage": "jest --coverage"
122
80
  },
123
- "nodemonConfig": {
124
- "delay": 5500
125
- }
81
+ "files": [
82
+ "/src"
83
+ ]
126
84
  }
package/src/.babelrc CHANGED
@@ -10,25 +10,12 @@
10
10
  "plugins" : [
11
11
  "@babel/plugin-proposal-function-bind",
12
12
  "@babel/plugin-proposal-export-default-from",
13
- "@babel/plugin-proposal-logical-assignment-operators",
14
- [
15
- "@babel/plugin-proposal-optional-chaining",
16
- {
17
- "loose" : false
18
- }
19
- ],
20
13
  [
21
14
  "@babel/plugin-proposal-pipeline-operator",
22
15
  {
23
16
  "proposal" : "minimal"
24
17
  }
25
18
  ],
26
- [
27
- "@babel/plugin-proposal-nullish-coalescing-operator",
28
- {
29
- "loose" : false
30
- }
31
- ],
32
19
  "@babel/plugin-proposal-do-expressions",
33
20
  [
34
21
  "@babel/plugin-proposal-decorators",
@@ -37,18 +24,9 @@
37
24
  }
38
25
  ],
39
26
  "@babel/plugin-proposal-function-sent",
40
- "@babel/plugin-proposal-export-namespace-from",
41
- "@babel/plugin-proposal-numeric-separator",
42
27
  "@babel/plugin-proposal-throw-expressions",
43
28
  "@babel/plugin-syntax-dynamic-import",
44
29
  "@babel/plugin-syntax-import-meta",
45
- [
46
- "@babel/plugin-proposal-class-properties",
47
- {
48
- "loose" : false
49
- }
50
- ],
51
- "@babel/plugin-proposal-json-strings",
52
30
  "@babel/plugin-transform-property-mutators",
53
31
  "@babel/plugin-transform-shorthand-properties",
54
32
  "@babel/plugin-transform-for-of",
package/src/Atom.js CHANGED
@@ -45,35 +45,17 @@ 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 { shake256 } from 'js-sha3';
49
- import { charsetBaseConvert } from './libraries/strings';
50
- import Meta from './Meta';
51
- import AtomMeta from './AtomMeta';
48
+ import JsSHA from 'jssha'
49
+ import { charsetBaseConvert } from './libraries/strings'
50
+ import Meta from './Meta'
51
+ import AtomMeta from './AtomMeta'
52
+ import AtomsMissingException from './exception/AtomsMissingException'
53
+ import versions from './versions/index'
52
54
 
53
55
  /**
54
56
  * Atom class used to form microtransactions within a Molecule
55
57
  */
56
58
  export default class Atom {
57
-
58
- /**
59
- *
60
- * @returns {string[]}
61
- */
62
- static getHashableProps () {
63
- return [
64
- 'position',
65
- 'walletAddress',
66
- 'isotope',
67
- 'token',
68
- 'value',
69
- 'batchId',
70
- 'metaType',
71
- 'metaId',
72
- 'meta',
73
- 'createdAt'
74
- ];
75
- }
76
-
77
59
  /**
78
60
  * Class constructor
79
61
  *
@@ -88,8 +70,9 @@ export default class Atom {
88
70
  * @param {array|object|null} meta
89
71
  * @param {string|null} otsFragment
90
72
  * @param {number|null} index
73
+ * @param {string|null} version
91
74
  */
92
- constructor ( {
75
+ constructor ({
93
76
  position = null,
94
77
  walletAddress = null,
95
78
  isotope = null,
@@ -100,37 +83,69 @@ export default class Atom {
100
83
  metaId = null,
101
84
  meta = null,
102
85
  otsFragment = null,
103
- index = null
104
- } ) {
105
- this.position = position;
106
- this.walletAddress = walletAddress;
107
- this.isotope = isotope;
108
- this.token = token;
109
- this.value = null !== value ? String( value ) : null;
110
- this.batchId = batchId;
111
-
112
- this.metaType = metaType;
113
- this.metaId = metaId;
114
- this.meta = meta ? Meta.normalizeMeta( meta ) : [];
115
-
116
- this.index = index;
117
- this.otsFragment = otsFragment;
118
- this.createdAt = String( +new Date );
86
+ index = null,
87
+ version = null
88
+ }) {
89
+ this.position = position
90
+ this.walletAddress = walletAddress
91
+ this.isotope = isotope
92
+ this.token = token
93
+ this.value = value !== null ? String(value) : null
94
+ this.batchId = batchId
95
+
96
+ this.metaType = metaType
97
+ this.metaId = metaId
98
+ this.meta = meta ? Meta.normalizeMeta(meta) : []
99
+
100
+ this.index = index
101
+ this.otsFragment = otsFragment
102
+ this.createdAt = String(+new Date())
103
+ if (version !== null && Object.prototype.hasOwnProperty.call(versions, version)) {
104
+ this.version = String(version)
105
+ }
119
106
  }
120
107
 
108
+ /**
109
+ *
110
+ * @returns {string[]}
111
+ */
112
+ static getHashableProps () {
113
+ return [
114
+ 'position',
115
+ 'walletAddress',
116
+ 'isotope',
117
+ 'token',
118
+ 'value',
119
+ 'batchId',
120
+ 'metaType',
121
+ 'metaId',
122
+ 'meta',
123
+ 'createdAt'
124
+ ]
125
+ }
126
+
127
+ /**
128
+ *
129
+ * @returns {string[]}
130
+ */
131
+ static getUnclaimedProps () {
132
+ return [
133
+ 'otsFragment'
134
+ ]
135
+ }
121
136
 
122
137
  /**
123
138
  *
124
- * @param isotope
125
- * @param wallet
126
- * @param value
127
- * @param metaType
128
- * @param metaId
129
- * @param meta
130
- * @param batchId
139
+ * @param {string} isotope
140
+ * @param {Wallet|null} wallet
141
+ * @param {int|null} value
142
+ * @param {string|null} metaType
143
+ * @param {string|null} metaId
144
+ * @param {AtomMeta|array|object|null} meta
145
+ * @param {string|null} batchId
131
146
  * @returns {Atom}
132
147
  */
133
- static create( {
148
+ static create ({
134
149
  isotope,
135
150
  wallet = null,
136
151
  value = null,
@@ -138,26 +153,30 @@ export default class Atom {
138
153
  metaId = null,
139
154
  meta = null,
140
155
  batchId = null
141
- } ) {
156
+ }) {
142
157
  // If meta object is not passed - create it
143
- if ( !meta ) {
144
- meta = new AtomMeta;
158
+ if (!meta) {
159
+ meta = new AtomMeta()
145
160
  }
146
161
 
147
- // If wallet has been passed => add related metas
148
- if ( wallet ) {
162
+ // If meta object is not an instance of AtomMeta - create it from meta
163
+ if (!(meta instanceof AtomMeta)) {
164
+ meta = new AtomMeta(meta)
165
+ }
149
166
 
167
+ // If wallet has been passed => add related metas
168
+ if (wallet) {
150
169
  // Add wallet's meta
151
- meta.addWallet( wallet );
170
+ meta.setAtomWallet(wallet)
152
171
 
153
172
  // If batch ID does not passed: set it from the wallet
154
- if ( !batchId ) {
155
- batchId = wallet.batchId;
173
+ if (!batchId) {
174
+ batchId = wallet.batchId
156
175
  }
157
176
  }
158
177
 
159
178
  // Create the final atom's object
160
- return new Atom( {
179
+ return new Atom({
161
180
  position: wallet ? wallet.position : null,
162
181
  walletAddress: wallet ? wallet.address : null,
163
182
  isotope,
@@ -167,14 +186,7 @@ export default class Atom {
167
186
  metaType,
168
187
  metaId,
169
188
  meta: meta.get()
170
- } );
171
- }
172
-
173
- /**
174
- * Get aggregated meta from stored normalized ones
175
- */
176
- aggregatedMeta () {
177
- return Meta.aggregateMeta( this.meta );
189
+ })
178
190
  }
179
191
 
180
192
  /**
@@ -183,49 +195,17 @@ export default class Atom {
183
195
  * @param {string} json
184
196
  * @return {object}
185
197
  */
186
- static jsonToObject ( json ) {
187
-
188
- const target = Object.assign( new Atom( {} ), JSON.parse( json ) ),
189
- properties = Object.keys( new Atom( {} ) );
198
+ static jsonToObject (json) {
199
+ const target = Object.assign(new Atom({}), JSON.parse(json))
200
+ const properties = Object.keys(new Atom({}))
190
201
 
191
- for ( const property in target ) {
192
- if ( target.hasOwnProperty( property ) && !properties.includes( property ) ) {
193
- delete target[ property ];
202
+ for (const property in target) {
203
+ if (Object.prototype.hasOwnProperty.call(target, property) && !properties.includes(property)) {
204
+ delete target[property]
194
205
  }
195
206
  }
196
207
 
197
- return target;
198
- }
199
-
200
- /**
201
- *
202
- * @returns {*[]}
203
- */
204
- getHashableValues() {
205
- const hashableValues = [];
206
- for ( let property of Atom.getHashableProps() ) {
207
- const value = this[ property ];
208
-
209
- // All nullable values are not hashed (only custom keys)
210
- if ( value === null && ![ 'position', 'walletAddress' ].includes( property ) ) {
211
- continue;
212
- }
213
-
214
- // Hashing individual meta keys and values
215
- if ( property === 'meta' ) {
216
- for ( const meta of value ) {
217
- if ( typeof meta.value !== 'undefined' && meta.value !== null ) {
218
- hashableValues.push( String( meta.key ) );
219
- hashableValues.push( String( meta.value ) );
220
- }
221
- }
222
- }
223
- // Default value
224
- else {
225
- hashableValues.push( value === null ? '' : String( value ) );
226
- }
227
- }
228
- return hashableValues;
208
+ return target
229
209
  }
230
210
 
231
211
  /**
@@ -236,59 +216,118 @@ export default class Atom {
236
216
  * @param {string} output
237
217
  * @return {number[]|*}
238
218
  */
239
- static hashAtoms ( {
219
+ static hashAtoms ({
240
220
  atoms,
241
221
  output = 'base17'
242
- } ) {
243
-
244
- const molecularSponge = shake256.create( 256 ),
245
- numberOfAtoms = atoms.length,
246
- atomList = Atom.sortAtoms( atoms );
222
+ }) {
223
+ const molecularSponge = new JsSHA('SHAKE256', 'TEXT')
224
+ const atomList = Atom.sortAtoms(atoms)
247
225
 
248
- // Hashing each atom in the molecule to produce a molecular hash
249
- let hashableValues = [];
250
- for ( const atom of atomList ) {
226
+ if (atomList.length === 0) {
227
+ throw new AtomsMissingException()
228
+ }
251
229
 
252
- // Add number of atoms (???)
253
- hashableValues.push( String( numberOfAtoms ) );
230
+ atomList.map(atom => {
231
+ if (!(atom instanceof Atom)) {
232
+ throw new AtomsMissingException()
233
+ }
234
+ return atom
235
+ })
254
236
 
255
- // Add atom's properties
256
- hashableValues = hashableValues.concat( atom.getHashableValues() );
257
- }
237
+ // Hashing each atom in the molecule to produce a molecular hash
238
+ if (atomList.every(atom => atom.version && Object.prototype.hasOwnProperty.call(versions, atom.version))) {
239
+ molecularSponge.update(JSON.stringify(atomList.map(atom => versions[atom.version].create(atom).view())))
240
+ } else {
241
+ const numberOfAtoms = String(atoms.length)
242
+ let hashableValues = []
243
+
244
+ for (const atom of atomList) {
245
+ // Add number of atoms (???)
246
+ hashableValues.push(numberOfAtoms)
247
+
248
+ // Add atom's properties
249
+ hashableValues = hashableValues.concat(atom.getHashableValues())
250
+ }
258
251
 
259
- // Add hash values to the sponge
260
- for ( const hashableValue of hashableValues ) {
261
- molecularSponge.update( hashableValue );
252
+ // Add hash values to the sponge
253
+ for (const hashableValue of hashableValues) {
254
+ molecularSponge.update(hashableValue)
255
+ }
262
256
  }
263
257
 
264
258
  // Return the hash in the requested format
265
- switch ( output ) {
259
+ switch (output) {
266
260
  case 'hex': {
267
- return molecularSponge.hex();
261
+ return molecularSponge.getHash('HEX', { outputLen: 256 })
268
262
  }
269
263
  case 'array': {
270
- return molecularSponge.array();
264
+ return molecularSponge.getHash('ARRAYBUFFER', { outputLen: 256 })
271
265
  }
272
266
  default: {
273
- return charsetBaseConvert( molecularSponge.hex(), 16, 17, '0123456789abcdef', '0123456789abcdefg' ).padStart( 64, '0' );
267
+ return charsetBaseConvert(molecularSponge.getHash('HEX', { outputLen: 256 }), 16, 17, '0123456789abcdef', '0123456789abcdefg').padStart(64, '0')
274
268
  }
275
269
  }
276
270
  }
277
271
 
272
+ static jsonSerialization (key, value) {
273
+ if (!Atom.getUnclaimedProps().includes(key)) {
274
+ return value
275
+ }
276
+
277
+ return undefined
278
+ }
279
+
278
280
  /**
279
281
  * Sort the atoms in a Molecule
280
282
  *
281
283
  * @param {array} atoms
282
284
  * @return {array}
283
285
  */
284
- static sortAtoms ( atoms ) {
285
- const atomList = [ ...atoms ];
286
+ static sortAtoms (atoms) {
287
+ const atomList = [...atoms]
286
288
 
287
289
  // Sort based on atomic index
288
- atomList.sort( ( first, second ) => {
289
- return first.index < second.index ? -1 : 1;
290
- } );
290
+ atomList.sort((first, second) => {
291
+ return first.index < second.index ? -1 : 1
292
+ })
293
+
294
+ return atomList
295
+ }
296
+
297
+ /**
298
+ * Get aggregated meta from stored normalized ones
299
+ */
300
+ aggregatedMeta () {
301
+ return Meta.aggregateMeta(this.meta)
302
+ }
291
303
 
292
- return atomList;
304
+ /**
305
+ *
306
+ * @returns {*[]}
307
+ */
308
+ getHashableValues () {
309
+ const hashableValues = []
310
+ for (const property of Atom.getHashableProps()) {
311
+ const value = this[property]
312
+
313
+ // All nullable values are not hashed (only custom keys)
314
+ if (value === null && !['position', 'walletAddress'].includes(property)) {
315
+ continue
316
+ }
317
+
318
+ // Hashing individual meta keys and values
319
+ if (property === 'meta') {
320
+ for (const meta of value) {
321
+ if (typeof meta.value !== 'undefined' && meta.value !== null) {
322
+ hashableValues.push(String(meta.key))
323
+ hashableValues.push(String(meta.value))
324
+ }
325
+ }
326
+ } else {
327
+ // Default value
328
+ hashableValues.push(value === null ? '' : String(value))
329
+ }
330
+ }
331
+ return hashableValues
293
332
  }
294
333
  }