@wishknish/knishio-client-js 0.5.2 → 0.6.0

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 (138) hide show
  1. package/README.md +207 -274
  2. package/dist/client.iife.js +577 -0
  3. package/package.json +36 -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 +37 -46
  8. package/src/KnishIOClient.js +932 -985
  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/apollo/ApolloClientWrapper.js +143 -0
  54. package/src/libraries/apollo/CipherLink.js +116 -0
  55. package/src/libraries/apollo/Client.js +154 -0
  56. package/src/libraries/apollo/ErrorHandler.js +32 -0
  57. package/src/libraries/apollo/SubscriptionManager.js +60 -0
  58. package/src/libraries/apollo/operationUtils.js +26 -0
  59. package/src/libraries/array.js +50 -41
  60. package/src/libraries/crypto.js +20 -27
  61. package/src/libraries/strings.js +58 -91
  62. package/src/mutation/Mutation.js +44 -25
  63. package/src/mutation/MutationActiveSession.js +12 -12
  64. package/src/mutation/MutationClaimShadowWallet.js +15 -17
  65. package/src/mutation/MutationCreateIdentifier.js +11 -12
  66. package/src/mutation/MutationCreateMeta.js +11 -12
  67. package/src/mutation/MutationCreateRule.js +11 -12
  68. package/src/mutation/MutationCreateToken.js +18 -13
  69. package/src/mutation/MutationCreateWallet.js +9 -11
  70. package/src/mutation/MutationDepositBufferToken.js +7 -9
  71. package/src/mutation/MutationLinkIdentifier.js +12 -14
  72. package/src/mutation/MutationProposeMolecule.js +24 -25
  73. package/src/mutation/MutationRequestAuthorization.js +9 -10
  74. package/src/mutation/MutationRequestAuthorizationGuest.js +12 -15
  75. package/src/mutation/MutationRequestTokens.js +11 -14
  76. package/src/mutation/MutationTransferTokens.js +11 -14
  77. package/src/mutation/MutationWithdrawBufferToken.js +7 -10
  78. package/src/query/Query.js +62 -36
  79. package/src/query/QueryActiveSession.js +11 -13
  80. package/src/query/QueryAtom.js +75 -76
  81. package/src/query/QueryBalance.js +11 -12
  82. package/src/query/QueryBatch.js +17 -14
  83. package/src/query/QueryBatchHistory.js +16 -13
  84. package/src/query/QueryContinuId.js +13 -10
  85. package/src/query/QueryMetaType.js +45 -57
  86. package/src/query/QueryMetaTypeViaAtom.js +49 -57
  87. package/src/query/QueryPolicy.js +11 -12
  88. package/src/query/QueryToken.js +11 -13
  89. package/src/query/QueryUserActivity.js +11 -13
  90. package/src/query/QueryWalletBundle.js +15 -47
  91. package/src/query/QueryWalletList.js +15 -16
  92. package/src/response/Response.js +29 -34
  93. package/src/response/ResponseActiveSession.js +6 -6
  94. package/src/response/ResponseAtom.js +29 -30
  95. package/src/response/ResponseAuthorizationGuest.js +17 -18
  96. package/src/response/ResponseBalance.js +12 -13
  97. package/src/response/ResponseClaimShadowWallet.js +1 -1
  98. package/src/response/ResponseContinuId.js +21 -22
  99. package/src/response/ResponseCreateIdentifier.js +1 -1
  100. package/src/response/ResponseCreateMeta.js +1 -1
  101. package/src/response/ResponseCreateRule.js +1 -1
  102. package/src/response/ResponseCreateToken.js +1 -1
  103. package/src/response/ResponseCreateWallet.js +1 -1
  104. package/src/response/ResponseLinkIdentifier.js +9 -10
  105. package/src/response/ResponseMetaBatch.js +6 -8
  106. package/src/response/ResponseMetaType.js +19 -20
  107. package/src/response/ResponseMetaTypeViaAtom.js +19 -19
  108. package/src/response/ResponsePolicy.js +14 -15
  109. package/src/response/ResponseProposeMolecule.js +27 -30
  110. package/src/response/ResponseQueryActiveSession.js +20 -23
  111. package/src/response/ResponseQueryUserActivity.js +11 -12
  112. package/src/response/ResponseRequestAuthorization.js +11 -16
  113. package/src/response/ResponseRequestAuthorizationGuest.js +18 -21
  114. package/src/response/ResponseRequestTokens.js +1 -1
  115. package/src/response/ResponseTransferTokens.js +8 -9
  116. package/src/response/ResponseWalletBundle.js +16 -17
  117. package/src/response/ResponseWalletList.js +44 -47
  118. package/src/subscribe/ActiveSessionSubscribe.js +5 -6
  119. package/src/subscribe/ActiveWalletSubscribe.js +5 -6
  120. package/src/subscribe/CreateMoleculeSubscribe.js +5 -5
  121. package/src/subscribe/Subscribe.js +26 -26
  122. package/src/subscribe/WalletStatusSubscribe.js +5 -6
  123. package/src/versions/HashAtom.js +78 -0
  124. package/src/versions/Version4.js +34 -0
  125. package/src/versions/index.js +5 -0
  126. package/dist/client.umd.js +0 -453
  127. package/src/httpClient/ApolloClient.js +0 -245
  128. package/src/libraries/ApolloLink/CipherLink.js +0 -117
  129. package/src/libraries/ApolloLink/Client.js +0 -231
  130. package/src/libraries/ApolloLink/PusherLink.js +0 -234
  131. package/src/libraries/ApolloLink/handler.js +0 -106
  132. package/src/libraries/Base58.js +0 -71
  133. package/src/libraries/Base64.js +0 -40
  134. package/src/libraries/BaseX.js +0 -91
  135. package/src/libraries/Soda.js +0 -93
  136. package/src/query/QueryMetaInstance.js +0 -99
  137. package/src/test/Test.js +0 -670
  138. package/src/test/TestTokenUnit.js +0 -340
@@ -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 ( value ) {
11
- if ( Math.abs( value * multiplier ) < 1 ) {
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 ( value1, value2, debug = false ) {
25
-
26
- const val1 = Decimal.val( value1 ) * multiplier,
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 ( Math.abs( val1 - val2 ) < 1 ) {
31
- return 0;
28
+ if (Math.abs(val1 - val2) < 1) {
29
+ return 0
32
30
  }
33
31
 
34
32
  // Greater or smaller
35
- return ( val1 > val2 ) ? 1 : -1;
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 ( value1, value2 ) {
45
- return ( Decimal.cmp( value1, value2 ) === 0 );
41
+ static equal (value1, value2) {
42
+ return (Decimal.cmp(value1, value2) === 0)
46
43
  }
47
-
48
44
  }
@@ -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
- * @param {object|array} obj
8
- * @param {string} keys
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 ( obj, keys ) {
12
-
13
- this.arr = [];
14
- this.key = null;
15
-
16
- this.arr = String( keys ).split( '.' );
17
- this.key = this.arr.shift();
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( obj );
21
+ this.__nextKey = this.arr.length
22
+ this.__next = this.__tic(obj)
27
23
  }
28
24
 
29
25
  /**
30
- * @param {object|array} obj
31
- * @return {boolean}
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 ( obj ) {
35
-
36
- if ( !Array.isArray( obj ) && !( obj instanceof Object ) ) {
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[ this.key ] !== 'undefined';
36
+ return typeof obj[this.key] !== 'undefined'
41
37
  }
42
38
 
43
39
  /**
44
- * @param {object|array} obj
45
- * @param {string} keys
46
- * @return {boolean}
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 ( obj, keys ) {
45
+ static has (obj, keys) {
46
+ this.__init(obj, keys)
49
47
 
50
- this.__init( obj, keys );
51
-
52
- if ( !this.__next ) {
53
- return false;
48
+ if (!this.__next) {
49
+ return false
54
50
  }
55
- if ( this.__nextKey === 0 ) {
56
- return true;
51
+ if (this.__nextKey === 0) {
52
+ return true
57
53
  }
58
54
 
59
- return this.has( obj[ this.key ], this.arr.join( '.' ) );
55
+ return this.has(obj[this.key], this.arr.join('.'))
60
56
  }
61
57
 
62
58
  /**
63
- * @param {object|array} obj
64
- * @param {string} keys
65
- * @param {*} def
66
- * @return {*}
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 ( obj, keys, def = null ) {
69
-
70
- this.__init( obj, keys );
65
+ static get (obj, keys, def = null) {
66
+ this.__init(obj, keys)
71
67
 
72
- if ( !this.__next ) {
73
- return def;
68
+ if (!this.__next) {
69
+ return def
74
70
  }
75
- if ( this.__nextKey === 0 ) {
76
- return obj[ this.key ];
71
+ if (this.__nextKey === 0) {
72
+ return obj[this.key]
77
73
  }
78
74
 
79
- return this.get( obj[ this.key ], this.arr.join( '.' ), def );
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
  }
@@ -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 ( arr, options ) {
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 = ( val, uppercase ) => {
30
-
31
- const set = uppercase ?
32
- [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ] :
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
- opts = Object.assign(
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
- for ( let i = 0; i < arr.length; ++i ) {
48
+ let str = ''
49
+ let group = 0
50
+ let column = 0
52
51
 
53
- str += numberToHex( arr[ i ], opts.uppercase );
52
+ for (let i = 0; i < arr.length; ++i) {
53
+ str += numberToHex(arr[i], opts.uppercase)
54
54
 
55
- if ( i === arr.length - 1 ) {
56
- break;
55
+ if (i === arr.length - 1) {
56
+ break
57
57
  }
58
58
 
59
- if ( opts.grouping > 0 && ++group === opts.grouping ) {
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
- column = 0;
66
- str += '\n';
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 ( str ) {
85
+ static toUint8Array (str) {
86
+ let target = str.toLowerCase().replace(/\s/g, '')
88
87
 
89
- let target = str.toLowerCase().replace( /\s/g, '' );
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
- let buffer = new Uint8Array( Math.floor( target.length / 2 ) ),
96
- curr = -1;
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
- let c = target[ i ],
101
- val = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' ].indexOf( c );
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 ( val === -1 ) {
104
- throw Error( 'unexpected character' );
99
+ if (val === -1) {
100
+ throw Error('unexpected character')
105
101
  }
106
102
 
107
- if ( curr === -1 ) {
108
- curr = 16 * val;
103
+ if (curr === -1) {
104
+ curr = 16 * val
109
105
  } else {
110
-
111
- buffer[ Math.floor( i / 2 ) ] = curr + val;
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
  }
@@ -0,0 +1,143 @@
1
+ import Client from './Client'
2
+ import SubscriptionManager from './SubscriptionManager'
3
+
4
+ /**
5
+ * ApolloClient wrapper class
6
+ * Provides a high-level interface for interacting with the GraphQL server
7
+ */
8
+ export default class ApolloClientWrapper {
9
+ /**
10
+ * @param {Object} config - Configuration object
11
+ * @param {string} config.serverUri - URI of the GraphQL server
12
+ * @param {Object|null} config.socket - WebSocket configuration (optional)
13
+ * @param {boolean} config.encrypt - Whether to use encryption (default: false)
14
+ */
15
+ constructor ({ serverUri, socket = null, encrypt = false }) {
16
+ // Initialize the underlying Apollo Client
17
+ this.$__client = new Client({ serverUri, soketi: socket, encrypt })
18
+
19
+ // Initialize the subscription manager
20
+ this.$__subscriptionManager = new SubscriptionManager(this.$__client)
21
+ }
22
+
23
+ /**
24
+ * Set encryption for the client
25
+ * @param {boolean} encrypt - Whether to use encryption
26
+ */
27
+ setEncryption (encrypt = false) {
28
+ const clientConfig = {
29
+ serverUri: this.$__client.getServerUri(),
30
+ soketi: {
31
+ socketUri: this.$__client.getSocketUri(),
32
+ appKey: 'knishio'
33
+ },
34
+ encrypt
35
+ }
36
+ this.$__client = new Client(clientConfig)
37
+ this.$__subscriptionManager.setClient(this.$__client)
38
+ }
39
+
40
+ /**
41
+ * Unsubscribe from a specific subscription
42
+ * @param {string} operationName - Name of the operation to unsubscribe from
43
+ */
44
+ unsubscribe (operationName) {
45
+ this.$__subscriptionManager.unsubscribe(operationName)
46
+ }
47
+
48
+ /**
49
+ * Unsubscribe from all subscriptions
50
+ */
51
+ unsubscribeAll () {
52
+ this.$__subscriptionManager.unsubscribeAll()
53
+ }
54
+
55
+ /**
56
+ * Disconnect the WebSocket
57
+ */
58
+ socketDisconnect () {
59
+ this.$__client.socketDisconnect()
60
+ this.$__subscriptionManager.clear()
61
+ }
62
+
63
+ /**
64
+ * Create a new subscription
65
+ * @param {Object} request - Subscription request
66
+ * @param {Function} closure - Callback function for subscription updates
67
+ * @returns {Object} Subscription object
68
+ */
69
+ subscribe (request, closure) {
70
+ return this.$__subscriptionManager.subscribe(request, closure)
71
+ }
72
+
73
+ /**
74
+ * Execute a query
75
+ * @param {Object} request - Query request
76
+ * @returns {Promise} Promise resolving to the query result
77
+ */
78
+ async query (request) {
79
+ return this.$__client.query(request)
80
+ }
81
+
82
+ /**
83
+ * Execute a mutation
84
+ * @param {Object} request - Mutation request
85
+ * @returns {Promise} Promise resolving to the mutation result
86
+ */
87
+ async mutate (request) {
88
+ return this.$__client.mutate(request)
89
+ }
90
+
91
+ /**
92
+ * Set authentication data for the client
93
+ * @param {Object} authData - Authentication data
94
+ * @param {string} authData.token - Authentication token
95
+ * @param {string} authData.pubkey - Public key
96
+ * @param {Object} authData.wallet - Wallet object
97
+ */
98
+ setAuthData ({ token, pubkey, wallet }) {
99
+ this.$__client.setAuthData({ token, pubkey, wallet })
100
+ }
101
+
102
+ // Getter methods
103
+ getAuthToken () { return this.$__client.getAuthToken() }
104
+
105
+ getUri () {
106
+ return this.$__client ? this.$__client.getServerUri() : null
107
+ }
108
+
109
+ getSocketUri () { return this.$__client.getSocketUri() }
110
+
111
+ /**
112
+ * Set the server URI
113
+ * @param {string} uri - New server URI
114
+ */
115
+ setUri (uri) {
116
+ const clientConfig = {
117
+ serverUri: uri,
118
+ soketi: {
119
+ socketUri: this.$__client.getSocketUri(),
120
+ appKey: 'knishio'
121
+ },
122
+ encrypt: !!this.$__client.cipherLink
123
+ }
124
+ this.$__client = new Client(clientConfig)
125
+ this.$__subscriptionManager.setClient(this.$__client)
126
+ }
127
+
128
+ /**
129
+ * Set the WebSocket URI
130
+ * @param {Object} config - WebSocket configuration
131
+ * @param {string} config.socketUri - New WebSocket URI
132
+ * @param {string} config.appKey - Application key for the WebSocket
133
+ */
134
+ setSocketUri ({ socketUri, appKey }) {
135
+ const clientConfig = {
136
+ serverUri: this.$__client.getServerUri(),
137
+ soketi: { socketUri, appKey },
138
+ encrypt: !!this.$__client.cipherLink
139
+ }
140
+ this.$__client = new Client(clientConfig)
141
+ this.$__subscriptionManager.setClient(this.$__client)
142
+ }
143
+ }
@@ -0,0 +1,116 @@
1
+ import { ApolloLink, gql } from '@apollo/client/core'
2
+ import { print } from 'graphql'
3
+ import { operationName, operationType } from './operationUtils'
4
+ import CodeException from '../../exception/CodeException'
5
+ import Dot from '../Dot'
6
+
7
+ /**
8
+ * Custom Apollo Link for handling encryption and decryption of GraphQL operations
9
+ */
10
+ class CipherLink extends ApolloLink {
11
+ constructor () {
12
+ super()
13
+ this.__wallet = null
14
+ this.__pubkey = null
15
+ }
16
+
17
+ /**
18
+ * Set the wallet for encryption
19
+ * @param {Object|null} wallet - Wallet object
20
+ */
21
+ setWallet (wallet) {
22
+ this.__wallet = wallet
23
+ }
24
+
25
+ /**
26
+ * Get the current wallet
27
+ * @returns {Object|null} Wallet object
28
+ */
29
+ getWallet () {
30
+ return this.__wallet
31
+ }
32
+
33
+ /**
34
+ * Set the public key for encryption
35
+ * @param {string|null} pubkey - Public key
36
+ */
37
+ setPubKey (pubkey) {
38
+ this.__pubkey = pubkey
39
+ }
40
+
41
+ /**
42
+ * Get the current public key
43
+ * @returns {string|null} Public key
44
+ */
45
+ getPubKey () {
46
+ return this.__pubkey
47
+ }
48
+
49
+ /**
50
+ * Handle the request and apply encryption if necessary
51
+ * @param {Object} operation - GraphQL operation
52
+ * @param {function} forward - Function to forward the operation
53
+ * @returns {Observable} Observable of the operation result
54
+ */
55
+ request (operation, forward) {
56
+ const requestName = operationName(operation)
57
+ const requestType = operationType(operation)
58
+ const isMoleculeMutation = (requestType === 'mutation' && requestName === 'ProposeMolecule')
59
+
60
+ // Check if encryption should be skipped
61
+ const skipEncryption = [
62
+ (requestType === 'query' && ['__schema', 'ContinuId'].includes(requestName)),
63
+ (requestType === 'mutation' && requestName === 'AccessToken'),
64
+ (isMoleculeMutation && Dot.get(operation, 'variables.molecule.atoms.0.isotope') === 'U')
65
+ ].some(condition => condition)
66
+
67
+ if (skipEncryption) {
68
+ return forward(operation)
69
+ }
70
+
71
+ const wallet = this.getWallet()
72
+ const pubKey = this.getPubKey()
73
+
74
+ if (!pubKey) {
75
+ throw new CodeException('CipherLink::request() - Node public key missing!')
76
+ }
77
+
78
+ if (!wallet) {
79
+ throw new CodeException('CipherLink::request() - Authorized wallet missing!')
80
+ }
81
+
82
+ const cipher = {
83
+ query: print(operation.query),
84
+ variables: JSON.stringify(operation.variables)
85
+ }
86
+
87
+ // Encrypt the operation
88
+ operation.operationName = null
89
+ operation.query = gql`query ($Hash: String!) { CipherHash(Hash: $Hash) { hash } }`
90
+ operation.variables = { Hash: JSON.stringify(wallet.encryptMessage(cipher, pubKey)) }
91
+
92
+ // Forward the encrypted operation and handle the response
93
+ return forward(operation).map(data => {
94
+ let response = data.data
95
+
96
+ if (response.data) {
97
+ response = response.data
98
+ }
99
+
100
+ if (response.CipherHash && response.CipherHash.hash) {
101
+ const encrypted = JSON.parse(response.CipherHash.hash)
102
+ const decryption = wallet.decryptMessage(encrypted)
103
+
104
+ if (decryption === null) {
105
+ throw new CodeException('CipherLink::request() - Unable to decrypt response!')
106
+ }
107
+
108
+ return decryption
109
+ }
110
+
111
+ return data
112
+ })
113
+ }
114
+ }
115
+
116
+ export default CipherLink