@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/src/libraries/Decimal.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
// Value determines by min sql decimal precision
|
|
2
|
-
const multiplier = 10 ** 18
|
|
2
|
+
const multiplier = 10 ** 18
|
|
3
3
|
|
|
4
4
|
export default class Decimal {
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @param {number} value
|
|
8
7
|
* @return {number}
|
|
9
8
|
*/
|
|
10
|
-
static val (
|
|
11
|
-
if (
|
|
12
|
-
return 0.0
|
|
9
|
+
static val (value) {
|
|
10
|
+
if (Math.abs(value * multiplier) < 1) {
|
|
11
|
+
return 0.0
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
return value
|
|
14
|
+
return value
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
/**
|
|
@@ -21,19 +20,17 @@ export default class Decimal {
|
|
|
21
20
|
* @param {boolean} debug
|
|
22
21
|
* @return {number}
|
|
23
22
|
*/
|
|
24
|
-
static cmp (
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
val2 = Decimal.val( value2 ) * multiplier;
|
|
23
|
+
static cmp (value1, value2, debug = false) {
|
|
24
|
+
const val1 = Decimal.val(value1) * multiplier
|
|
25
|
+
const val2 = Decimal.val(value2) * multiplier
|
|
28
26
|
|
|
29
27
|
// Equal
|
|
30
|
-
if (
|
|
31
|
-
return 0
|
|
28
|
+
if (Math.abs(val1 - val2) < 1) {
|
|
29
|
+
return 0
|
|
32
30
|
}
|
|
33
31
|
|
|
34
32
|
// Greater or smaller
|
|
35
|
-
return (
|
|
36
|
-
|
|
33
|
+
return (val1 > val2) ? 1 : -1
|
|
37
34
|
}
|
|
38
35
|
|
|
39
36
|
/**
|
|
@@ -41,8 +38,7 @@ export default class Decimal {
|
|
|
41
38
|
* @param {number} value2
|
|
42
39
|
* @return {boolean}
|
|
43
40
|
*/
|
|
44
|
-
static equal (
|
|
45
|
-
return (
|
|
41
|
+
static equal (value1, value2) {
|
|
42
|
+
return (Decimal.cmp(value1, value2) === 0)
|
|
46
43
|
}
|
|
47
|
-
|
|
48
44
|
}
|
package/src/libraries/Dot.js
CHANGED
|
@@ -1,81 +1,107 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Utility class for accessing nested object properties using dot notation
|
|
3
3
|
*/
|
|
4
4
|
export default class Dot {
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @param {
|
|
6
|
+
* Initialize the Dot utility with the given object and key path
|
|
7
|
+
* @param {object|array} obj - The object or array to traverse
|
|
8
|
+
* @param {string} keys - The dot-notated string of keys
|
|
9
9
|
* @private
|
|
10
10
|
*/
|
|
11
|
-
static __init (
|
|
12
|
-
|
|
13
|
-
this.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const numberKey = Number( this.key );
|
|
20
|
-
|
|
21
|
-
if ( Number.isInteger( numberKey ) ) {
|
|
22
|
-
this.key = numberKey;
|
|
11
|
+
static __init (obj, keys) {
|
|
12
|
+
this.arr = String(keys).split('.')
|
|
13
|
+
this.key = this.arr.shift()
|
|
14
|
+
|
|
15
|
+
// Convert to number if the key is a valid integer
|
|
16
|
+
const numberKey = Number(this.key)
|
|
17
|
+
if (Number.isInteger(numberKey)) {
|
|
18
|
+
this.key = numberKey
|
|
23
19
|
}
|
|
24
20
|
|
|
25
|
-
this.__nextKey = this.arr.length
|
|
26
|
-
this.__next = this.__tic(
|
|
21
|
+
this.__nextKey = this.arr.length
|
|
22
|
+
this.__next = this.__tic(obj)
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @
|
|
26
|
+
* Check if the current key exists in the object
|
|
27
|
+
* @param {object|array} obj - The object or array to check
|
|
28
|
+
* @return {boolean} - Whether the key exists
|
|
32
29
|
* @private
|
|
33
30
|
*/
|
|
34
|
-
static __tic (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return false;
|
|
31
|
+
static __tic (obj) {
|
|
32
|
+
if (!Array.isArray(obj) && !(obj instanceof Object)) {
|
|
33
|
+
return false
|
|
38
34
|
}
|
|
39
35
|
|
|
40
|
-
return typeof obj[
|
|
36
|
+
return typeof obj[this.key] !== 'undefined'
|
|
41
37
|
}
|
|
42
38
|
|
|
43
39
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @param {
|
|
46
|
-
* @
|
|
40
|
+
* Check if a nested property exists in an object using dot notation
|
|
41
|
+
* @param {object|array} obj - The object or array to search
|
|
42
|
+
* @param {string} keys - The path to the property, using dot notation
|
|
43
|
+
* @return {boolean} - True if the property exists, false otherwise
|
|
47
44
|
*/
|
|
48
|
-
static has (
|
|
45
|
+
static has (obj, keys) {
|
|
46
|
+
this.__init(obj, keys)
|
|
49
47
|
|
|
50
|
-
this.
|
|
51
|
-
|
|
52
|
-
if ( !this.__next ) {
|
|
53
|
-
return false;
|
|
48
|
+
if (!this.__next) {
|
|
49
|
+
return false
|
|
54
50
|
}
|
|
55
|
-
if (
|
|
56
|
-
return true
|
|
51
|
+
if (this.__nextKey === 0) {
|
|
52
|
+
return true
|
|
57
53
|
}
|
|
58
54
|
|
|
59
|
-
return this.has(
|
|
55
|
+
return this.has(obj[this.key], this.arr.join('.'))
|
|
60
56
|
}
|
|
61
57
|
|
|
62
58
|
/**
|
|
63
|
-
*
|
|
64
|
-
* @param {
|
|
65
|
-
* @param {
|
|
66
|
-
* @
|
|
59
|
+
* Get a nested property from an object using dot notation
|
|
60
|
+
* @param {object|array} obj - The object or array to search
|
|
61
|
+
* @param {string} keys - The path to the property, using dot notation
|
|
62
|
+
* @param {*} [def=null] - The default value to return if the property is not found
|
|
63
|
+
* @return {*} - The value of the property, or the default value if not found
|
|
67
64
|
*/
|
|
68
|
-
static get (
|
|
69
|
-
|
|
70
|
-
this.__init( obj, keys );
|
|
65
|
+
static get (obj, keys, def = null) {
|
|
66
|
+
this.__init(obj, keys)
|
|
71
67
|
|
|
72
|
-
if (
|
|
73
|
-
return def
|
|
68
|
+
if (!this.__next) {
|
|
69
|
+
return def
|
|
74
70
|
}
|
|
75
|
-
if (
|
|
76
|
-
return obj[
|
|
71
|
+
if (this.__nextKey === 0) {
|
|
72
|
+
return obj[this.key]
|
|
77
73
|
}
|
|
78
74
|
|
|
79
|
-
return this.get(
|
|
75
|
+
return this.get(obj[this.key], this.arr.join('.'), def)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Set a nested property in an object using dot notation
|
|
80
|
+
* @param {object|array} obj - The object or array to modify
|
|
81
|
+
* @param {string} keys - The path to the property, using dot notation
|
|
82
|
+
* @param {*} value - The value to set
|
|
83
|
+
* @return {object|array} - The modified object or array
|
|
84
|
+
*/
|
|
85
|
+
static set (obj, keys, value) {
|
|
86
|
+
const parts = keys.split('.')
|
|
87
|
+
let current = obj
|
|
88
|
+
const lastIndex = parts.length - 1
|
|
89
|
+
|
|
90
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
91
|
+
const key = parts[i]
|
|
92
|
+
const numberKey = Number(key)
|
|
93
|
+
const useNumberKey = Number.isInteger(numberKey)
|
|
94
|
+
|
|
95
|
+
if (!(useNumberKey ? numberKey : key in current)) {
|
|
96
|
+
current[useNumberKey ? numberKey : key] = parts[i + 1].match(/^\d+$/) ? [] : {}
|
|
97
|
+
}
|
|
98
|
+
current = current[useNumberKey ? numberKey : key]
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const lastKey = parts[lastIndex]
|
|
102
|
+
const lastNumberKey = Number(lastKey)
|
|
103
|
+
current[Number.isInteger(lastNumberKey) ? lastNumberKey : lastKey] = value
|
|
104
|
+
|
|
105
|
+
return obj
|
|
80
106
|
}
|
|
81
107
|
}
|
package/src/libraries/Hex.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Create Uint8Array buffers from hexadecimal strings, and vice versa.
|
|
3
3
|
*/
|
|
4
|
-
export default class Hex {
|
|
5
4
|
|
|
5
|
+
if (typeof self === 'undefined') {
|
|
6
|
+
global.self = global
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default class Hex {
|
|
6
10
|
/**
|
|
7
11
|
* Converts the given buffer to a string containing its hexadecimal representation.
|
|
8
12
|
*
|
|
@@ -19,58 +23,52 @@ export default class Hex {
|
|
|
19
23
|
* @param {object} options
|
|
20
24
|
* @return {string}
|
|
21
25
|
*/
|
|
22
|
-
static toHex (
|
|
23
|
-
|
|
26
|
+
static toHex (arr, options) {
|
|
24
27
|
/**
|
|
25
28
|
* @param {number} val
|
|
26
29
|
* @param {boolean} uppercase
|
|
27
30
|
* @return {*}
|
|
28
31
|
*/
|
|
29
|
-
const numberToHex = (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
[ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' ];
|
|
34
|
-
|
|
35
|
-
return set[ Math.floor( val / 16 ) ] + set[ val % 16 ];
|
|
32
|
+
const numberToHex = (val, uppercase) => {
|
|
33
|
+
const set = uppercase
|
|
34
|
+
? ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
|
|
35
|
+
: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
|
|
36
36
|
|
|
37
|
+
return set[Math.floor(val / 16)] + set[val % 16]
|
|
38
|
+
}
|
|
39
|
+
const opts = Object.assign(
|
|
40
|
+
{
|
|
41
|
+
grouping: 0,
|
|
42
|
+
rowlength: 0,
|
|
43
|
+
uppercase: false
|
|
37
44
|
},
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
grouping: 0,
|
|
41
|
-
rowlength: 0,
|
|
42
|
-
uppercase: false
|
|
43
|
-
},
|
|
44
|
-
options || {}
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
let str = '',
|
|
48
|
-
group = 0,
|
|
49
|
-
column = 0;
|
|
45
|
+
options || {}
|
|
46
|
+
)
|
|
50
47
|
|
|
51
|
-
|
|
48
|
+
let str = ''
|
|
49
|
+
let group = 0
|
|
50
|
+
let column = 0
|
|
52
51
|
|
|
53
|
-
|
|
52
|
+
for (let i = 0; i < arr.length; ++i) {
|
|
53
|
+
str += numberToHex(arr[i], opts.uppercase)
|
|
54
54
|
|
|
55
|
-
if (
|
|
56
|
-
break
|
|
55
|
+
if (i === arr.length - 1) {
|
|
56
|
+
break
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
group = 0;
|
|
62
|
-
|
|
63
|
-
if ( opts.rowlength > 0 && ++column === opts.rowlength ) {
|
|
59
|
+
if (opts.grouping > 0 && ++group === opts.grouping) {
|
|
60
|
+
group = 0
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
if (opts.rowlength > 0 && ++column === opts.rowlength) {
|
|
63
|
+
column = 0
|
|
64
|
+
str += '\n'
|
|
67
65
|
} else {
|
|
68
|
-
str += ' '
|
|
66
|
+
str += ' '
|
|
69
67
|
}
|
|
70
68
|
}
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
return str
|
|
71
|
+
return str
|
|
74
72
|
}
|
|
75
73
|
|
|
76
74
|
/**
|
|
@@ -84,35 +82,32 @@ export default class Hex {
|
|
|
84
82
|
* @param {string} str
|
|
85
83
|
* @return {Uint8Array}
|
|
86
84
|
*/
|
|
87
|
-
static toUint8Array (
|
|
85
|
+
static toUint8Array (str) {
|
|
86
|
+
let target = str.toLowerCase().replace(/\s/g, '')
|
|
88
87
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if ( target.length % 2 === 1 ) {
|
|
92
|
-
target = `0${ target }`;
|
|
88
|
+
if (target.length % 2 === 1) {
|
|
89
|
+
target = `0${ target }`
|
|
93
90
|
}
|
|
94
91
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
for ( let i = 0; i < target.length; ++i ) {
|
|
92
|
+
const buffer = new Uint8Array(Math.floor(target.length / 2))
|
|
93
|
+
let curr = -1
|
|
99
94
|
|
|
100
|
-
|
|
101
|
-
|
|
95
|
+
for (let i = 0; i < target.length; ++i) {
|
|
96
|
+
const c = target[i]
|
|
97
|
+
const val = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'].indexOf(c)
|
|
102
98
|
|
|
103
|
-
if (
|
|
104
|
-
throw Error(
|
|
99
|
+
if (val === -1) {
|
|
100
|
+
throw Error('unexpected character')
|
|
105
101
|
}
|
|
106
102
|
|
|
107
|
-
if (
|
|
108
|
-
curr = 16 * val
|
|
103
|
+
if (curr === -1) {
|
|
104
|
+
curr = 16 * val
|
|
109
105
|
} else {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
curr = -1;
|
|
106
|
+
buffer[Math.floor(i / 2)] = curr + val
|
|
107
|
+
curr = -1
|
|
113
108
|
}
|
|
114
109
|
}
|
|
115
110
|
|
|
116
|
-
return buffer
|
|
111
|
+
return buffer
|
|
117
112
|
}
|
|
118
113
|
}
|
package/src/libraries/array.js
CHANGED
|
@@ -5,69 +5,78 @@
|
|
|
5
5
|
* @param {number} size
|
|
6
6
|
* @return {array}
|
|
7
7
|
*/
|
|
8
|
-
export function chunkArray (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return [];
|
|
8
|
+
export function chunkArray (arr, size) {
|
|
9
|
+
if (!arr.length) {
|
|
10
|
+
return []
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
return [
|
|
13
|
+
return [arr.slice(0, size)].concat(chunkArray(arr.slice(size), size))
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
export function deepCloning (
|
|
18
17
|
o // The only argument passed by user: object to clone
|
|
19
18
|
, h // Cache (organized as array: [key,value,key,value,...])
|
|
20
19
|
) {
|
|
21
|
-
let i
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
h = h || []
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
20
|
+
let i
|
|
21
|
+
let r
|
|
22
|
+
let x // Property indexer, result, temporary variable
|
|
23
|
+
|
|
24
|
+
const t = [Array, Date, Number, String, Boolean] // Types to treat in a special way
|
|
25
|
+
|
|
26
|
+
const s = Object.prototype.toString // Shortcut to Object.prototype.toString
|
|
27
|
+
h = h || [] // If cache is not created yet, create it!
|
|
28
|
+
// Search cache for our object
|
|
29
|
+
for (i = 0; i < h.length; i += 2) {
|
|
30
|
+
if (o === h[i]) {
|
|
31
|
+
r = h[i + 1]
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// Clone o if it is uncached object and not null
|
|
35
|
+
if (!r && o && typeof o === 'object') {
|
|
36
|
+
r = {} // Default result template: plain hash
|
|
37
|
+
// To handle multiframe environment, search for type by
|
|
38
|
+
for (i = 0; i < t.length; i++) {
|
|
39
|
+
// comparing Object.prototype.toString's of our object
|
|
40
|
+
// and new object x created with the constructor t[i]
|
|
41
|
+
// Notice that it will create new Date(o), new String(o)
|
|
42
|
+
// which is good and new Array(o) which is bad
|
|
43
|
+
if (s.call(o) === s.call(x = new t[i](o))) {
|
|
44
|
+
r = i ? x : []
|
|
45
|
+
}
|
|
46
|
+
} // If i==0, t==Array. We need to recreate it. Else use x
|
|
47
|
+
h.push(o, r) // Add object to cache before (!) making recursive call
|
|
48
|
+
// Just copy properties recursively
|
|
49
|
+
for (i in o) {
|
|
50
|
+
// As o might have key 'hasOwnProperty', use something
|
|
51
|
+
if (h.hasOwnProperty.call(o, i)) {
|
|
52
|
+
r[i] = deepCloning(o[i], h)
|
|
53
|
+
}
|
|
54
|
+
} // we defined right instead
|
|
44
55
|
}
|
|
45
|
-
return r || o
|
|
56
|
+
return r || o // Return r if it was found in cache or built in if(){}
|
|
46
57
|
}
|
|
47
58
|
|
|
48
59
|
/**
|
|
49
60
|
* @param arrays
|
|
50
61
|
* @returns {*[]}
|
|
51
62
|
*/
|
|
52
|
-
export function diff (
|
|
63
|
+
export function diff (...arrays) {
|
|
64
|
+
return [].concat(...arrays.map((arr, i) => {
|
|
65
|
+
const others = arrays.slice(0)
|
|
53
66
|
|
|
54
|
-
|
|
67
|
+
others.splice(i, 1)
|
|
55
68
|
|
|
56
|
-
const
|
|
69
|
+
const unique = [...new Set([].concat(...others))]
|
|
57
70
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const unique = [ ...new Set( [].concat( ...others ) ) ];
|
|
61
|
-
|
|
62
|
-
return arr.filter( item => !unique.includes( item ) );
|
|
63
|
-
} ) );
|
|
71
|
+
return arr.filter(item => !unique.includes(item))
|
|
72
|
+
}))
|
|
64
73
|
}
|
|
65
74
|
|
|
66
|
-
|
|
75
|
+
/**
|
|
67
76
|
*
|
|
68
77
|
* @param arrays
|
|
69
78
|
* @return {*[]}
|
|
70
79
|
*/
|
|
71
|
-
export function intersect (
|
|
72
|
-
return arrays.reduce(
|
|
80
|
+
export function intersect (...arrays) {
|
|
81
|
+
return arrays.reduce((first, second) => first.filter(item => second.includes(item)))
|
|
73
82
|
}
|
package/src/libraries/crypto.js
CHANGED
|
@@ -45,8 +45,8 @@ 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
|
|
48
|
+
import { randomString } from './strings'
|
|
49
|
+
import JsSHA from 'jssha'
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* Generates a secret based on an optional seed
|
|
@@ -55,17 +55,14 @@ import { randomString } from './strings';
|
|
|
55
55
|
* @param length
|
|
56
56
|
* @return {string|*}
|
|
57
57
|
*/
|
|
58
|
-
export function generateSecret (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
return randomString( length );
|
|
58
|
+
export function generateSecret (seed = null, length = 2048) {
|
|
59
|
+
// console.info(`Crypto::generateSecret() - Computing new secret${ seed ? ' from existing seed' : '' }...`)
|
|
60
|
+
if (seed) {
|
|
61
|
+
const sponge = new JsSHA('SHAKE256', 'TEXT')
|
|
62
|
+
sponge.update(seed)
|
|
63
|
+
return sponge.getHash('HEX', { outputLen: length * 2 })
|
|
64
|
+
} else {
|
|
65
|
+
return randomString(length)
|
|
69
66
|
}
|
|
70
67
|
}
|
|
71
68
|
|
|
@@ -73,16 +70,13 @@ export function generateSecret ( seed = null, length = 2048 ) {
|
|
|
73
70
|
* Hashes the user secret to produce a bundle hash
|
|
74
71
|
*
|
|
75
72
|
* @param {string} secret
|
|
73
|
+
* @param {string|null} source
|
|
76
74
|
* @return {string}
|
|
77
75
|
*/
|
|
78
|
-
export function generateBundleHash (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const sponge = shake256.create( 256 );
|
|
83
|
-
sponge.update( secret );
|
|
84
|
-
return sponge.hex();
|
|
85
|
-
|
|
76
|
+
export function generateBundleHash (secret, source = null) {
|
|
77
|
+
const sponge = new JsSHA('SHAKE256', 'TEXT')
|
|
78
|
+
sponge.update(secret)
|
|
79
|
+
return sponge.getHash('HEX', { outputLen: 256 })
|
|
86
80
|
}
|
|
87
81
|
|
|
88
82
|
/**
|
|
@@ -93,14 +87,13 @@ export function generateBundleHash ( secret ) {
|
|
|
93
87
|
*
|
|
94
88
|
* @return {string}
|
|
95
89
|
*/
|
|
96
|
-
export function generateBatchId (
|
|
90
|
+
export function generateBatchId ({
|
|
97
91
|
molecularHash = null,
|
|
98
92
|
index = null
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return generateBundleHash( String( molecularHash ) + String( index ) );
|
|
93
|
+
}) {
|
|
94
|
+
if (molecularHash !== null && index !== null) {
|
|
95
|
+
return generateBundleHash(String(molecularHash) + String(index), 'generateBatchId')
|
|
103
96
|
}
|
|
104
97
|
|
|
105
|
-
return randomString(
|
|
98
|
+
return randomString(64)
|
|
106
99
|
}
|