@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.
- package/README.md +207 -274
- package/dist/client.iife.js +533 -0
- package/package.json +37 -79
- package/src/.babelrc +0 -22
- package/src/Atom.js +171 -132
- package/src/AtomMeta.js +76 -50
- package/src/AuthToken.js +38 -47
- package/src/KnishIOClient.js +934 -987
- package/src/Meta.js +15 -17
- package/src/Molecule.js +423 -494
- package/src/PolicyMeta.js +32 -41
- package/src/TokenUnit.js +30 -32
- package/src/Wallet.js +275 -265
- package/src/exception/AtomIndexException.js +4 -8
- package/src/exception/AtomsMissingException.js +4 -6
- package/src/exception/AuthorizationRejectedException.js +4 -5
- package/src/exception/BalanceInsufficientException.js +4 -8
- package/src/exception/BaseException.js +6 -8
- package/src/exception/BatchIdException.js +5 -7
- package/src/exception/CodeException.js +4 -8
- package/src/{libraries/ApolloLink/HttpLink.js → exception/DecryptionKeyException.js} +12 -15
- package/src/exception/InvalidResponseException.js +4 -5
- package/src/exception/MetaMissingException.js +4 -6
- package/src/exception/MolecularHashMismatchException.js +4 -6
- package/src/exception/MolecularHashMissingException.js +4 -5
- package/src/exception/NegativeAmountException.js +4 -5
- package/src/exception/PolicyInvalidException.js +4 -4
- package/src/exception/SignatureMalformedException.js +4 -5
- package/src/exception/SignatureMismatchException.js +4 -5
- package/src/exception/StackableUnitAmountException.js +4 -5
- package/src/exception/StackableUnitDecimalsException.js +4 -5
- package/src/exception/TransferBalanceException.js +4 -5
- package/src/exception/TransferMalformedException.js +4 -5
- package/src/exception/TransferMismatchedException.js +4 -5
- package/src/exception/TransferRemainderException.js +4 -5
- package/src/exception/TransferToSelfException.js +4 -5
- package/src/exception/TransferUnbalancedException.js +4 -5
- package/src/exception/UnauthenticatedException.js +4 -5
- package/src/{libraries/ApolloLink/AuthLink.js → exception/WalletCredentialException.js} +12 -40
- package/src/exception/WalletShadowException.js +4 -5
- package/src/exception/WrongTokenTypeException.js +4 -5
- package/src/exception/index.js +26 -26
- package/src/index.js +8 -10
- package/src/instance/Rules/Callback.js +91 -93
- package/src/instance/Rules/Condition.js +21 -23
- package/src/instance/Rules/Meta.js +13 -14
- package/src/instance/Rules/Rule.js +39 -43
- package/src/instance/Rules/exception/RuleArgumentException.js +4 -4
- package/src/libraries/CheckMolecule.js +253 -232
- package/src/libraries/Decimal.js +13 -17
- package/src/libraries/Dot.js +74 -48
- package/src/libraries/Hex.js +49 -54
- package/src/libraries/array.js +50 -41
- package/src/libraries/crypto.js +20 -27
- package/src/libraries/strings.js +58 -91
- package/src/libraries/urql/UrqlClientWrapper.js +166 -0
- package/src/mutation/Mutation.js +44 -25
- package/src/mutation/MutationActiveSession.js +12 -12
- package/src/mutation/MutationClaimShadowWallet.js +15 -17
- package/src/mutation/MutationCreateIdentifier.js +11 -12
- package/src/mutation/MutationCreateMeta.js +11 -12
- package/src/mutation/MutationCreateRule.js +11 -12
- package/src/mutation/MutationCreateToken.js +18 -13
- package/src/mutation/MutationCreateWallet.js +9 -11
- package/src/mutation/MutationDepositBufferToken.js +7 -9
- package/src/mutation/MutationLinkIdentifier.js +12 -14
- package/src/mutation/MutationProposeMolecule.js +24 -25
- package/src/mutation/MutationRequestAuthorization.js +9 -10
- package/src/mutation/MutationRequestAuthorizationGuest.js +12 -15
- package/src/mutation/MutationRequestTokens.js +11 -14
- package/src/mutation/MutationTransferTokens.js +11 -14
- package/src/mutation/MutationWithdrawBufferToken.js +7 -10
- package/src/query/Query.js +62 -36
- package/src/query/QueryActiveSession.js +11 -13
- package/src/query/QueryAtom.js +75 -76
- package/src/query/QueryBalance.js +11 -12
- package/src/query/QueryBatch.js +17 -14
- package/src/query/QueryBatchHistory.js +16 -13
- package/src/query/QueryContinuId.js +13 -10
- package/src/query/QueryMetaType.js +45 -57
- package/src/query/QueryMetaTypeViaAtom.js +49 -57
- package/src/query/QueryPolicy.js +11 -12
- package/src/query/QueryToken.js +11 -13
- package/src/query/QueryUserActivity.js +11 -13
- package/src/query/QueryWalletBundle.js +15 -47
- package/src/query/QueryWalletList.js +15 -16
- package/src/response/Response.js +29 -34
- package/src/response/ResponseActiveSession.js +6 -6
- package/src/response/ResponseAtom.js +29 -30
- package/src/response/ResponseAuthorizationGuest.js +17 -18
- package/src/response/ResponseBalance.js +12 -13
- package/src/response/ResponseClaimShadowWallet.js +1 -1
- package/src/response/ResponseContinuId.js +21 -22
- package/src/response/ResponseCreateIdentifier.js +1 -1
- package/src/response/ResponseCreateMeta.js +1 -1
- package/src/response/ResponseCreateRule.js +1 -1
- package/src/response/ResponseCreateToken.js +1 -1
- package/src/response/ResponseCreateWallet.js +1 -1
- package/src/response/ResponseLinkIdentifier.js +9 -10
- package/src/response/ResponseMetaBatch.js +6 -8
- package/src/response/ResponseMetaType.js +19 -20
- package/src/response/ResponseMetaTypeViaAtom.js +19 -19
- package/src/response/ResponsePolicy.js +14 -15
- package/src/response/ResponseProposeMolecule.js +27 -30
- package/src/response/ResponseQueryActiveSession.js +20 -23
- package/src/response/ResponseQueryUserActivity.js +11 -12
- package/src/response/ResponseRequestAuthorization.js +11 -16
- package/src/response/ResponseRequestAuthorizationGuest.js +18 -21
- package/src/response/ResponseRequestTokens.js +1 -1
- package/src/response/ResponseTransferTokens.js +8 -9
- package/src/response/ResponseWalletBundle.js +16 -17
- package/src/response/ResponseWalletList.js +44 -47
- package/src/subscribe/ActiveSessionSubscribe.js +5 -6
- package/src/subscribe/ActiveWalletSubscribe.js +5 -6
- package/src/subscribe/CreateMoleculeSubscribe.js +5 -5
- package/src/subscribe/Subscribe.js +26 -26
- package/src/subscribe/WalletStatusSubscribe.js +5 -6
- package/src/versions/HashAtom.js +78 -0
- package/src/versions/Version4.js +34 -0
- package/src/versions/index.js +5 -0
- package/dist/client.umd.js +0 -453
- package/src/httpClient/ApolloClient.js +0 -245
- package/src/libraries/ApolloLink/CipherLink.js +0 -117
- package/src/libraries/ApolloLink/Client.js +0 -231
- package/src/libraries/ApolloLink/PusherLink.js +0 -234
- package/src/libraries/ApolloLink/handler.js +0 -106
- package/src/libraries/Base58.js +0 -71
- package/src/libraries/Base64.js +0 -40
- package/src/libraries/BaseX.js +0 -91
- package/src/libraries/Soda.js +0 -93
- package/src/query/QueryMetaInstance.js +0 -99
- package/src/test/Test.js +0 -670
- 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.
|
|
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
|
|
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": "
|
|
26
|
+
"email": "yuriy@wishknish.com",
|
|
27
|
+
"homepage": "https://wishknish.com",
|
|
26
28
|
"role": "developer"
|
|
27
29
|
}
|
|
28
30
|
],
|
|
29
|
-
"main": "dist/client.
|
|
30
|
-
"module": "dist/client.
|
|
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
|
-
"@
|
|
42
|
-
"@
|
|
43
|
-
"@
|
|
44
|
-
"
|
|
45
|
-
"
|
|
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
|
-
"
|
|
52
|
-
"
|
|
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
|
-
"@
|
|
62
|
-
"@
|
|
63
|
-
"@
|
|
64
|
-
"@
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
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": "
|
|
114
|
-
"
|
|
115
|
-
"
|
|
116
|
-
"
|
|
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
|
-
"
|
|
124
|
-
"
|
|
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
|
|
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
|
-
|
|
106
|
-
this.
|
|
107
|
-
this.
|
|
108
|
-
this.
|
|
109
|
-
this.
|
|
110
|
-
this.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
this.
|
|
118
|
-
this.
|
|
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 (
|
|
144
|
-
meta = new AtomMeta
|
|
158
|
+
if (!meta) {
|
|
159
|
+
meta = new AtomMeta()
|
|
145
160
|
}
|
|
146
161
|
|
|
147
|
-
// If
|
|
148
|
-
if (
|
|
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.
|
|
170
|
+
meta.setAtomWallet(wallet)
|
|
152
171
|
|
|
153
172
|
// If batch ID does not passed: set it from the wallet
|
|
154
|
-
if (
|
|
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 (
|
|
187
|
-
|
|
188
|
-
const
|
|
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 (
|
|
192
|
-
if (
|
|
193
|
-
delete target[
|
|
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
|
|
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
|
-
|
|
249
|
-
|
|
250
|
-
|
|
226
|
+
if (atomList.length === 0) {
|
|
227
|
+
throw new AtomsMissingException()
|
|
228
|
+
}
|
|
251
229
|
|
|
252
|
-
|
|
253
|
-
|
|
230
|
+
atomList.map(atom => {
|
|
231
|
+
if (!(atom instanceof Atom)) {
|
|
232
|
+
throw new AtomsMissingException()
|
|
233
|
+
}
|
|
234
|
+
return atom
|
|
235
|
+
})
|
|
254
236
|
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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 (
|
|
259
|
+
switch (output) {
|
|
266
260
|
case 'hex': {
|
|
267
|
-
return molecularSponge.
|
|
261
|
+
return molecularSponge.getHash('HEX', { outputLen: 256 })
|
|
268
262
|
}
|
|
269
263
|
case 'array': {
|
|
270
|
-
return molecularSponge.
|
|
264
|
+
return molecularSponge.getHash('ARRAYBUFFER', { outputLen: 256 })
|
|
271
265
|
}
|
|
272
266
|
default: {
|
|
273
|
-
return charsetBaseConvert(
|
|
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 (
|
|
285
|
-
const atomList = [
|
|
286
|
+
static sortAtoms (atoms) {
|
|
287
|
+
const atomList = [...atoms]
|
|
286
288
|
|
|
287
289
|
// Sort based on atomic index
|
|
288
|
-
atomList.sort(
|
|
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
|
-
|
|
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
|
}
|