@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
@@ -1,245 +0,0 @@
1
- /*
2
- (
3
- (/(
4
- (//(
5
- (///(
6
- (/////(
7
- (//////( )
8
- (////////( (/)
9
- (////////( (///)
10
- (//////////( (////)
11
- (//////////( (//////)
12
- (////////////( (///////)
13
- (/////////////( (/////////)
14
- (//////////////( (///////////)
15
- (///////////////( (/////////////)
16
- (////////////////( (//////////////)
17
- ((((((((((((((((((( (((((((((((((((
18
- ((((((((((((((((((( ((((((((((((((
19
- ((((((((((((((((((( ((((((((((((((
20
- (((((((((((((((((((( (((((((((((((
21
- (((((((((((((((((((( ((((((((((((
22
- ((((((((((((((((((( ((((((((((((
23
- ((((((((((((((((((( ((((((((((
24
- ((((((((((((((((((/ (((((((((
25
- (((((((((((((((((( ((((((((
26
- ((((((((((((((((( (((((((
27
- (((((((((((((((((( (((((
28
- ################# ##
29
- ################ #
30
- ################# ##
31
- %################ ###
32
- ###############( ####
33
- ############### ####
34
- ############### ######
35
- %#############( (#######
36
- %############# #########
37
- ############( ##########
38
- ########### #############
39
- ######### ##############
40
- %######
41
-
42
- Powered by Knish.IO: Connecting a Decentralized World
43
-
44
- Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
-
46
- License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
- */
48
- import { operationName } from '../libraries/ApolloLink/handler';
49
- import Client from '../libraries/ApolloLink/Client';
50
-
51
-
52
- export default class ApolloClient {
53
-
54
- /**
55
- * @param {string} serverUri
56
- * @param {object|null} socket
57
- * @param {boolean} encrypt
58
- */
59
- constructor ( {
60
- serverUri,
61
- socket = null,
62
- encrypt = false
63
- } ) {
64
-
65
- this.$__subscribers = {};
66
- this.$__uri = serverUri;
67
- this.$__socket = {
68
- ...{
69
- socketUri: null,
70
- appKey: 'knishio'
71
- }, ...socket || {}
72
- };
73
- this.$__client = null;
74
-
75
- this.restartTransport( encrypt );
76
- }
77
-
78
- /**
79
- * If you have subscriptions, you unsubscribe
80
- *
81
- * @param {boolean} encrypt
82
- */
83
- restartTransport ( encrypt = false ) {
84
- const client = new Client( {
85
- serverUri: this.$__uri,
86
- soketi: this.$__socket,
87
- encrypt
88
- } );
89
-
90
- if ( this.$__client ) {
91
- client.setAuthData( {
92
- token: this.$__client.getAuthToken(),
93
- pubkey: this.$__client.getPubKey(),
94
- wallet: this.$__client.getWallet()
95
- } );
96
-
97
- this.socketDisconnect();
98
- }
99
-
100
- this.$__client = client;
101
- }
102
-
103
- /**
104
- * Sets the encryption mode for this session
105
- *
106
- * @param {boolean} encrypt
107
- */
108
- setEncryption( encrypt = false ) {
109
- this.restartTransport( encrypt );
110
- }
111
-
112
- /**
113
- * @param {string} operationName
114
- */
115
- unsubscribe ( operationName ) {
116
- if ( this.$__subscribers.hasOwnProperty( operationName ) ) {
117
- this.$__subscribers[ operationName ].unsubscribe();
118
- this.$__client.unsubscribeFromChannel( operationName );
119
- delete this.$__subscribers[ operationName ];
120
- }
121
- }
122
-
123
- /**
124
- *
125
- */
126
- unsubscribeAll () {
127
- for ( let subscribe in this.$__subscribers ) {
128
- this.unsubscribe( subscribe );
129
- }
130
- }
131
-
132
- /**
133
- *
134
- */
135
- socketDisconnect () {
136
- this.$__client.socketDisconnect();
137
- this.$__subscribers = {};
138
- }
139
-
140
- /**
141
- * @param {Operation} request
142
- * @param {function} closure
143
- *
144
- * @return {string}
145
- */
146
- subscribe ( request, closure ) {
147
- const operation = operationName( request );
148
-
149
- this.unsubscribe( operation );
150
-
151
- this.$__subscribers[ operation ] = this.$__client
152
- .subscribe( request )
153
- .subscribe( data => closure( data ) );
154
-
155
- return operation;
156
- }
157
-
158
- /**
159
- *
160
- * @param request
161
- * @returns {Promise<*>}
162
- */
163
- async query ( request ) {
164
- return await this.$__client.query( request );
165
- }
166
-
167
- /**
168
- *
169
- * @param request
170
- * @returns {Promise<*>}
171
- */
172
- async mutate ( request ) {
173
- return await this.$__client.mutate( request );
174
- }
175
-
176
- /**
177
- * Sets the authorization token for this session
178
- *
179
- * @param {string} token
180
- * @param {string} pubkey
181
- * @param {Wallet|null} wallet
182
- */
183
- setAuthData ( {
184
- token,
185
- pubkey,
186
- wallet
187
- } ) {
188
- this.$__client.setAuthData( {
189
- token,
190
- pubkey,
191
- wallet
192
- } );
193
- }
194
-
195
- /**
196
- * Gets the current auth token
197
- *
198
- * @return {string|null}
199
- */
200
- getAuthToken () {
201
- let authTokenObject = this.$__client.getAuthToken();
202
- if ( !authTokenObject ) {
203
- return null;
204
- }
205
- return authTokenObject.getToken();
206
- }
207
-
208
- /**
209
- * Gets the endpoint URI
210
- *
211
- * @return {string}
212
- */
213
- getUri () {
214
- return this.$__uri;
215
- }
216
-
217
- /**
218
- * Sets the endpoint URI
219
- *
220
- * @param {string} uri
221
- */
222
- setUri ( uri ) {
223
- this.$__uri = uri;
224
- }
225
-
226
- /**
227
- * @return {string|null}
228
- */
229
- getSocketUri () {
230
- return this.$__socket ? this.$__socket.socketUri : null;
231
- }
232
-
233
- /**
234
- *
235
- * @param {string} socketUri
236
- * @param {string} appKey
237
- */
238
- setSocketUri ( {
239
- socketUri,
240
- appKey
241
- } ) {
242
- this.$__socket = arguments.length ? arguments[ 0 ] : this.$__socket;
243
- }
244
-
245
- }
@@ -1,117 +0,0 @@
1
- import {
2
- ApolloLink,
3
- gql
4
- } from '@apollo/client/core';
5
- import { print } from 'graphql';
6
- import {
7
- operationName,
8
- operationType
9
- } from './handler';
10
- import CodeException from '../../exception/CodeException';
11
- import Dot from '../Dot';
12
-
13
-
14
- class CipherLink extends ApolloLink {
15
-
16
- constructor () {
17
- super();
18
-
19
- this.__wallet = null;
20
- this.__pubkey = null;
21
- }
22
-
23
- /**
24
- * @param {null|Wallet} wallet
25
- */
26
- setWallet ( wallet ) {
27
- this.__wallet = wallet;
28
- }
29
-
30
- /**
31
- * @return {null|Wallet}
32
- */
33
- getWallet () {
34
- return this.__wallet;
35
- }
36
-
37
- /**
38
- * @param {null|string} pubkey
39
- */
40
- setPubKey ( pubkey ) {
41
- this.__pubkey = pubkey;
42
- }
43
-
44
- /**
45
- * @return {null|string}
46
- */
47
- getPubKey () {
48
- return this.__pubkey;
49
- }
50
-
51
- /**
52
- *
53
- * @param {Operation} operation
54
- * @param {NextLink | undefined} forward
55
- * @returns {Observable<FetchResult> | null}
56
- */
57
- request ( operation, forward ) {
58
-
59
- const requestName = operationName( operation ),
60
- requestType = operationType( operation ),
61
- isMoleculeMutation = ( requestType === 'mutation' && requestName === 'ProposeMolecule' ),
62
- condition = [
63
- ( requestType === 'query' && [ '__schema', 'ContinuId' ].includes( requestName ) ),
64
- ( requestType === 'mutation' && requestName === 'AccessToken' ),
65
- ( isMoleculeMutation && Dot.get( operation, 'variables.molecule.atoms.0.isotope' ) === 'U' )
66
- ],
67
- cipher = {
68
- query: print( operation.query ),
69
- variables: JSON.stringify( operation.variables )
70
- },
71
- wallet = this.getWallet(),
72
- pubKey = this.getPubKey();
73
-
74
- for ( const key in condition ) {
75
- if ( condition[ key ] ) {
76
- return forward( operation );
77
- }
78
- }
79
-
80
- if ( !pubKey ) {
81
- throw new CodeException( 'CipherLink::request() - Node public key missing!' );
82
- }
83
-
84
- if ( !wallet ) {
85
- throw new CodeException( 'CipherLink::request() - Authorized wallet missing!' );
86
- }
87
-
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
- return forward( operation ).map( data => {
93
-
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
-
117
- export default CipherLink;
@@ -1,231 +0,0 @@
1
- /*
2
- (
3
- (/(
4
- (//(
5
- (///(
6
- (/////(
7
- (//////( )
8
- (////////( (/)
9
- (////////( (///)
10
- (//////////( (////)
11
- (//////////( (//////)
12
- (////////////( (///////)
13
- (/////////////( (/////////)
14
- (//////////////( (///////////)
15
- (///////////////( (/////////////)
16
- (////////////////( (//////////////)
17
- ((((((((((((((((((( (((((((((((((((
18
- ((((((((((((((((((( ((((((((((((((
19
- ((((((((((((((((((( ((((((((((((((
20
- (((((((((((((((((((( (((((((((((((
21
- (((((((((((((((((((( ((((((((((((
22
- ((((((((((((((((((( ((((((((((((
23
- ((((((((((((((((((( ((((((((((
24
- ((((((((((((((((((/ (((((((((
25
- (((((((((((((((((( ((((((((
26
- ((((((((((((((((( (((((((
27
- (((((((((((((((((( (((((
28
- ################# ##
29
- ################ #
30
- ################# ##
31
- %################ ###
32
- ###############( ####
33
- ############### ####
34
- ############### ######
35
- %#############( (#######
36
- %############# #########
37
- ############( ##########
38
- ########### #############
39
- ######### ##############
40
- %######
41
-
42
- Powered by Knish.IO: Connecting a Decentralized World
43
-
44
- Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
-
46
- License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
- */
48
- import {
49
- ApolloClient,
50
- concat,
51
- from
52
- } from '@apollo/client/core';
53
- import fetch from 'isomorphic-fetch';
54
- import { InMemoryCache } from '@apollo/client/cache';
55
- import { onError } from '@apollo/client/link/error';
56
-
57
- import HttpLink from './HttpLink';
58
- import PusherLink from './PusherLink';
59
- import { parse } from 'uri-js';
60
- import AuthLink from './AuthLink';
61
- import { errorHandler } from './handler';
62
- import CipherLink from './CipherLink';
63
-
64
- class Client extends ApolloClient {
65
-
66
- /**
67
- * @param {string} serverUri
68
- * @param {object|null} soketi
69
- * @param {boolean} encrypt
70
- */
71
- constructor ( {
72
- serverUri,
73
- soketi = null,
74
- encrypt = false
75
- } ) {
76
- const _socket = {
77
- ...{
78
- socketUri: null,
79
- appKey: 'knishio'
80
- },
81
- ...soketi || {}
82
- };
83
- const links = [];
84
- const http = new HttpLink( {
85
- uri: serverUri,
86
- fetch: fetch,
87
- transportBatching: true
88
- } );
89
- const auth = new AuthLink();
90
-
91
- let cipher = null;
92
- let socket = null;
93
-
94
- links.push( auth );
95
-
96
- if ( encrypt ) {
97
- cipher = new CipherLink();
98
- links.push( cipher );
99
- }
100
-
101
- if ( _socket && _socket.socketUri ) {
102
- const path = parse( serverUri );
103
- socket = new PusherLink( {
104
- socketUri: _socket.socketUri,
105
- authEndpoint: `${ path.scheme }://${ path.host }/graphql/subscriptions/auth`,
106
- appKey: _socket.appKey
107
- } );
108
- links.push( socket );
109
- }
110
-
111
- links.push( concat( onError( errorHandler ), http ) );
112
-
113
- super( {
114
- link: from( links ),
115
- cache: new InMemoryCache(),
116
- connectToDevTools: true,
117
- defaultOptions: {
118
- watchQuery: {
119
- fetchPolicy: 'no-cache',
120
- errorPolicy: 'ignore'
121
- },
122
- query: {
123
- fetchPolicy: 'no-cache',
124
- errorPolicy: 'all'
125
- },
126
- mutate: {
127
- fetchPolicy: 'no-cache',
128
- errorPolicy: 'all'
129
- },
130
- subscribe: {
131
- fetchPolicy: 'no-cache',
132
- errorPolicy: 'all'
133
- }
134
- }
135
- } );
136
-
137
- this.__serverUri = serverUri;
138
- this.__soketi = _socket;
139
- this.__authLink = auth;
140
- /**
141
- *
142
- * @type {PusherLink}
143
- * @private
144
- */
145
- this.__socket = socket;
146
- this.__cipherLink = cipher;
147
-
148
- this.__pubkey = null;
149
- this.__wallet = null;
150
- }
151
-
152
- /**
153
- * @return {string}
154
- */
155
- getAuthToken () {
156
- return this.__authLink.getAuthToken();
157
- }
158
-
159
- /**
160
- *
161
- * @return {string|null}
162
- */
163
- getPubKey () {
164
- return this.__pubkey;
165
- }
166
-
167
- /**
168
- * @return {Wallet|null}
169
- */
170
- getWallet () {
171
- return this.__wallet;
172
- }
173
-
174
- /**
175
- * @param {string} token
176
- * @param {string|null} pubkey
177
- * @param {Wallet|null} wallet
178
- */
179
- setAuthData ( {
180
- token,
181
- pubkey = null,
182
- wallet = null
183
- } ) {
184
-
185
- this.__wallet = wallet;
186
- this.__pubkey = pubkey;
187
- this.__authLink.setAuthToken( token );
188
-
189
- if ( this.__socket ) {
190
- this.__socket.setAuthToken( token );
191
- }
192
-
193
- if ( this.__cipherLink ) {
194
- this.__cipherLink.setWallet( this.__wallet );
195
- this.__cipherLink.setPubKey( this.__pubkey );
196
- }
197
- }
198
-
199
- socketDisconnect () {
200
- if ( this.__socket ) {
201
- this.__socket.disconnect();
202
- }
203
- }
204
-
205
- /**
206
- *
207
- * @param {string} channel
208
- */
209
- unsubscribeFromChannel ( channel ) {
210
- if ( this.__socket ) {
211
- this.__socket.unsubscribeFromChannel( channel );
212
- }
213
- }
214
-
215
- /**
216
- * @return {string}
217
- */
218
- getServerUri () {
219
- return this.__serverUri;
220
- }
221
-
222
- /**
223
- * @return {string|null}
224
- */
225
- getSocketUri () {
226
- return this.__soketi.socketUri;
227
- }
228
-
229
- }
230
-
231
- export default Client;