@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
package/README.md CHANGED
@@ -5,8 +5,7 @@
5
5
 
6
6
  # Knish.IO Javascript Client SDK
7
7
 
8
- This is an experimental Javascript / NodeJS implementation of the Knish.IO client SDK. Its purpose is to expose class
9
- libraries for building and signing Knish.IO Molecules, composing Atoms, generating Wallets, and so much more.
8
+ This is the official Javascript / NodeJS implementation of the Knish.IO client SDK. Its purpose is to expose class libraries for building and signing Knish.IO Molecules, composing Atoms, generating Wallets, and much more.
10
9
 
11
10
  ## Installation
12
11
 
@@ -16,6 +15,8 @@ The SDK can be installed via either of the following:
16
15
 
17
16
  2. `npm install @wishknish/knishio-client-js --save`
18
17
 
18
+ (**Note:** For installations in a Vite-based environment, you will need to install the https://www.npmjs.com/package/vite-plugin-node-polyfills plugin to ensure compatibility with Node.js libraries.)
19
+
19
20
  ## Basic Usage
20
21
 
21
22
  The purpose of the Knish.IO SDK is to expose various ledger functions to new or existing applications.
@@ -37,62 +38,56 @@ This document will explain both ways.
37
38
 
38
39
  2. Instantiate the class with your node URI:
39
40
  ```javascript
40
- const client = new KnishIOClient( {
41
- uri: myNodeURI
42
- } );
41
+ const client = new KnishIOClient({
42
+ uri: myNodeURI,
43
+ cellSlug: myCellSlug,
44
+ serverSdkVersion: 3, // Optional, defaults to 3
45
+ logging: false // Optional, enables logging
46
+ });
43
47
  ```
44
48
 
45
- 3. Set the Cell to match your app:
46
- ```javascript
47
- client.setCellSlug( myCellSlug );
48
- ```
49
- (**Note:** the `knishio_cells` table on the node must contain an entry for this Cell)
50
-
51
-
52
- 4. Request authorization token from the node:
49
+ 3. Request authorization token from the node:
53
50
  ```javascript
54
- client.requestAuthToken ( {
55
- seed: 'myTopSecretCode'
56
- } )
51
+ await client.requestAuthToken({
52
+ seed: 'myTopSecretCode',
53
+ encrypt: true // Optional, enables encryption
54
+ });
57
55
  ```
58
56
 
59
- (**Note:** the `seed` parameter can be a salted combination of username + password, a biometric hash, an existing
60
- user identifier from an external authentication process, for example)
57
+ (**Note:** The `seed` parameter can be a salted combination of username + password, a biometric hash, an existing user identifier from an external authentication process, for example)
61
58
 
62
-
63
- 5. Begin using `client` to trigger commands described below...
59
+ 4. Begin using `client` to trigger commands described below...
64
60
 
65
61
  ### KnishIOClient Methods
66
62
 
67
- - Query metadata for a **Wallet Bundle**. Omit the `bundleHash` parameter to query your own Wallet Bundle:
63
+ - Query metadata for a **Wallet Bundle**. Omit the `bundle` parameter to query your own Wallet Bundle:
68
64
  ```javascript
69
- const result = await client.queryBundle ( {
70
- bundleHash: 'c47e20f99df190e418f0cc5ddfa2791e9ccc4eb297cfa21bd317dc0f98313b1d',
71
- } );
65
+ const result = await client.queryBundle({
66
+ bundle: 'c47e20f99df190e418f0cc5ddfa2791e9ccc4eb297cfa21bd317dc0f98313b1d',
67
+ });
72
68
 
73
- // Raw Metadata
74
- console.log( result );
69
+ console.log(result); // Raw Metadata
75
70
  ```
76
71
 
77
- - Query metadata for a **Meta Asset**. Omit any parameters to widen the search:
72
+ - Query metadata for a **Meta Asset**:
78
73
 
79
74
  ```javascript
80
- const result = await client.queryMeta ( {
75
+ const result = await client.queryMeta({
81
76
  metaType: 'Vehicle',
82
77
  metaId: null, // Meta ID
83
78
  key: 'LicensePlate',
84
79
  value: '1H17P',
85
- latest: true // Limit meta values to latest per key
86
- } );
80
+ latest: true, // Limit meta values to latest per key
81
+ throughAtom: true // Optional, query through Atom (default: true)
82
+ });
87
83
 
88
- // Raw Metadata
89
- console.log( result );
84
+ console.log(result); // Raw Metadata
90
85
  ```
91
86
 
92
- - Writing new metadata for a **Meta Asset**.
87
+ - Writing new metadata for a **Meta Asset**:
93
88
 
94
89
  ```javascript
95
- const result = await client.createMeta ( {
90
+ const result = await client.createMeta({
96
91
  metaType: 'Pokemon',
97
92
  metaId: 'Charizard',
98
93
  meta: {
@@ -107,46 +102,50 @@ This document will explain both ways.
107
102
  ],
108
103
  hp: 78,
109
104
  attack: 84,
110
- }
111
- } );
105
+ },
106
+ policy: {} // Optional policy object
107
+ });
112
108
 
113
- if( result.success() ) {
109
+ if (result.success()) {
114
110
  // Do things!
115
111
  }
116
112
 
117
- console.log( result.data() ); // Raw response
113
+ console.log(result.data()); // Raw response
118
114
  ```
119
115
 
120
116
  - Query Wallets associated with a Wallet Bundle:
121
117
 
122
118
  ```javascript
123
- const result = await client.queryWallets ( {
124
- bundleHash: 'c47e20f99df190e418f0cc5ddfa2791e9ccc4eb297cfa21bd317dc0f98313b1d',
125
- unspent: true // limit results to unspent wallets?
126
- } );
119
+ const result = await client.queryWallets({
120
+ bundle: 'c47e20f99df190e418f0cc5ddfa2791e9ccc4eb297cfa21bd317dc0f98313b1d',
121
+ token: 'FOO', // Optional, filter by token
122
+ unspent: true // Optional, limit results to unspent wallets
123
+ });
127
124
 
128
- console.log( results ); // Raw response
125
+ console.log(result); // Raw response
129
126
  ```
130
127
 
131
- - Declaring new **Wallets**. If Tokens are sent to undeclared Wallets, **Shadow Wallets** will be used (placeholder
132
- Wallets that can receive, but cannot send) to store tokens until they are claimed.
128
+ - Declaring new **Wallets**:
129
+
130
+ (**Note:** If Tokens are sent to undeclared Wallets, **Shadow Wallets** will be used (placeholder
131
+ Wallets that can receive, but cannot send) to store tokens until they are claimed.)
133
132
 
134
133
  ```javascript
135
- const result = await client.createWallet ( {
134
+ const result = await client.createWallet({
136
135
  token: 'FOO' // Token Slug for the wallet we are declaring
137
- } );
136
+ });
138
137
 
139
- if( result.success() ) {
138
+ if (result.success()) {
140
139
  // Do things!
141
140
  }
142
141
 
143
- console.log( result.data() ); // Raw response
142
+ console.log(result.data()); // Raw response
144
143
  ```
145
144
 
146
145
  - Issuing new **Tokens**:
147
146
 
148
147
  ```javascript
149
- const result = await client.createToken ( {
148
+ const result = await client.createToken({
150
149
  token: 'CRZY', // Token slug (ticker symbol)
151
150
  amount: '100000000', // Initial amount to issue
152
151
  meta: {
@@ -154,234 +153,170 @@ This document will explain both ways.
154
153
  fungibility: 'fungible', // Fungibility style (fungible / nonfungible / stackable)
155
154
  supply: 'limited', // Supply style (limited / replenishable)
156
155
  decimals: '2' // Decimal places
157
- }
158
- } );
156
+ },
157
+ units: [], // Optional, for stackable tokens
158
+ batchId: null // Optional, for stackable tokens
159
+ });
159
160
 
160
- if( result.success() ) {
161
+ if (result.success()) {
161
162
  // Do things!
162
163
  }
163
164
 
164
- console.log( result.data() ); // Raw response
165
+ console.log(result.data()); // Raw response
165
166
  ```
166
167
 
167
168
  - Transferring **Tokens** to other users:
168
169
 
169
170
  ```javascript
170
- const result = await client.transferToken ( {
171
- recipient: '7bf38257401eb3b0f20cabf5e6cf3f14c76760386473b220d95fa1c38642b61d', // Recipient's bundle hash,
171
+ const result = await client.transferToken({
172
+ bundleHash: '7bf38257401eb3b0f20cabf5e6cf3f14c76760386473b220d95fa1c38642b61d', // Recipient's bundle hash
172
173
  token: 'CRZY', // Token slug
173
- amount: '100'
174
- } );
174
+ amount: '100',
175
+ units: [], // Optional, for stackable tokens
176
+ batchId: null // Optional, for stackable tokens
177
+ });
175
178
 
176
- if( result.success() ) {
179
+ if (result.success()) {
177
180
  // Do things!
178
181
  }
179
182
 
180
- console.log( result.data() ); // Raw response
183
+ console.log(result.data()); // Raw response
181
184
  ```
182
185
 
183
- ## The Hard Way: working directly with Molecules
186
+ - Creating a new **Rule**:
184
187
 
185
- - Return a Molecule instance (via Promise) that you can manually add atoms to:
186
- ```javascript
187
- client.createMolecule ()
188
- ```
188
+ ```javascript
189
+ const result = await client.createRule({
190
+ metaType: 'MyMetaType',
191
+ metaId: 'MyMetaId',
192
+ rule: [
193
+ // Rule definition
194
+ ],
195
+ policy: {} // Optional policy object
196
+ });
197
+
198
+ if (result.success()) {
199
+ // Do things!
200
+ }
189
201
 
190
- - Return a customized Query instance (via Promise) that can be used to generate arbitrary transactions to the ledger for
191
- the supplied Query class:
192
- ```javascript
193
- client.createMoleculeMutation ( {
194
- mutationClass: myQueryClass // More info on these below
195
- } )
196
- ```
202
+ console.log(result.data()); // Raw response
203
+ ```
197
204
 
198
- - Retrieves the active balance (in the form of a Wallet object:
199
- ```javascript
200
- client.queryBalance ( {
201
- token: myTokenSlug,
202
- bundle: myBundleHash // Omit to get your own balance
203
- } )
204
- ```
205
+ - Querying **Atoms**:
205
206
 
206
- - Create a new Token on the ledger and places initial balance into a new wallet created for you; `tokenMetadata` object
207
- must contain properties for `name` and `fungibility` (which can presently be `'fungible'`, `'nonfungible'`,
208
- or `'stackable'`):
209
- ```javascript
210
- client.createToken ( {
211
- token: tokenSlug,
212
- amount: initialAmount,
213
- meta: tokenMetadata
214
- } )
215
- ```
207
+ ```javascript
208
+ const result = await client.queryAtom({
209
+ molecularHash: 'hash',
210
+ bundleHash: 'bundle',
211
+ isotope: 'V',
212
+ tokenSlug: 'CRZY',
213
+ latest: true,
214
+ queryArgs: {
215
+ limit: 15,
216
+ offset: 1
217
+ }
218
+ });
216
219
 
217
- - Retrieve a list of Shadow Wallets (wallets that have a balance in a particular token, but no keys - as can happen when
218
- you are sent tokens for which you lack a prior wallet):
219
- ```javascript
220
- client.queryShadowWallets ( {
221
- token: tokenSlug,
222
- bundle: myBundleHash // Omit to get your own balance
223
- } )
224
- ```
220
+ console.log(result.data()); // Raw response
221
+ ```
225
222
 
226
- - Attempt to claim a Shadow Wallet by generating keys for it, which turns it into a usable Wallet:
227
- ```javascript
228
- client.claimShadowWallet ( {
229
- token: tokenSlug
230
- } )
231
- ```
223
+ - Working with **Buffer Tokens**:
232
224
 
233
- - Transfer tokens to a recipient Wallet or Bundle:
234
- ```javascript
235
- client.transferToken ( {
236
- recipient: walletObjectOrBundleHash,
237
- token: tokenSlug,
238
- amount: transferAmount
239
- } )
240
- ```
225
+ ```javascript
226
+ // Deposit to buffer
227
+ const depositResult = await client.depositBufferToken({
228
+ tokenSlug: 'CRZY',
229
+ amount: 100,
230
+ tradeRates: {
231
+ 'OTHER_TOKEN': 0.5
232
+ }
233
+ });
241
234
 
242
- ### Knish.IO Query Classes
243
-
244
- The `KnishIOClient` can utilize a wide variety of built-in query classes
245
- via `client.createMoleculeQuery ( myQueryClass )`, in case you need something more flexible than the built-in methods.
246
-
247
- After calling `client.createMoleculeQuery ( myQueryClass )`, you will receive a `Query` class instance, which will let
248
- you add any necessary metadata to fulfill the GraphQL query or mutation. The metadata required will be different based
249
- on the type of `Query` class you choose, via an overloaded `fill()` method.
250
-
251
- Here are the most commonly used ones:
252
-
253
- #### `QueryMetaType` (for retrieving Meta Asset information)
254
-
255
- ```javascript
256
- // Build the query
257
- const query = await client.createQuery( QueryMetaType );
258
-
259
- // Define variable parameters
260
- // (eg: which MetaType we are querying)
261
- const variables = {
262
- metaType: 'SomeMetaType'
263
- }
264
-
265
- // Define which fields we want to get back
266
- const fields = {
267
- createdAt: null,
268
- metas: {
269
- key: null,
270
- value: null,
271
- },
272
- }
273
-
274
- // Execute the query
275
- const result = await query.execute( {
276
- variables,
277
- fields
278
- } );
279
-
280
- console.log( result.data() );
281
- ```
282
-
283
- #### `QueryWalletBundle` (for retrieving information about Wallet Bundles)
284
-
285
- ```javascript
286
- // Build the query
287
- const query = await client.createQuery( QueryWalletBundle );
288
-
289
- // Define variable parameters
290
- // (eg: how we want to filter Wallet Bundles)
291
- const variables = {
292
- key: 'publicName',
293
- value: 'Eugene'
294
- }
295
-
296
- // Define which fields we want to get back
297
- const fields = {
298
- bundleHash: null,
299
- metas: {
300
- key: null,
301
- value: null,
302
- },
303
- }
304
-
305
- // Execute the query
306
- const result = await query.execute( {
307
- variables,
308
- fields
309
- } );
310
-
311
- console.log( result.data() );
312
- ```
313
-
314
- #### `QueryWalletList` (for getting a list of Wallets)
315
-
316
- ```javascript
317
- // Build the query
318
- const query = await client.createQuery( QueryWalletList );
319
-
320
- // Define variable parameters
321
- // (eg: how we want to filter Wallet Bundles)
322
- const variables = {
323
- token: 'DYD',
324
- };
325
-
326
- // Define which fields we want to get back
327
- const fields = {
328
- address: null,
329
- amount: null,
330
- };
331
-
332
- // Execute the query
333
- const result = await query.execute( {
334
- variables,
335
- fields
336
- } );
337
-
338
- console.log( result.data() );
339
- ```
340
-
341
- ## The Extreme Way: DIY Everything
342
-
343
- This method involves individually building Atoms and Molecules, triggering the signature and validation processes, and
344
- communicating the resulting signed Molecule mutation or Query to a Knish.IO node via your favorite GraphQL client.
235
+ // Withdraw from buffer
236
+ const withdrawResult = await client.withdrawBufferToken({
237
+ tokenSlug: 'CRZY',
238
+ amount: 50
239
+ });
240
+
241
+ console.log(depositResult.data(), withdrawResult.data()); // Raw responses
242
+ ```
243
+
244
+ - Getting client fingerprint:
245
+
246
+ ```javascript
247
+ const fingerprint = await client.getFingerprint();
248
+ console.log(fingerprint);
249
+
250
+ const fingerprintData = await client.getFingerprintData();
251
+ console.log(fingerprintData);
252
+ ```
253
+
254
+ ## Advanced Usage: Working with Molecules
255
+
256
+ For more granular control, you can work directly with Molecules:
257
+
258
+ - Create a new Molecule:
259
+ ```javascript
260
+ const molecule = await client.createMolecule();
261
+ ```
262
+
263
+ - Create a custom Mutation:
264
+ ```javascript
265
+ const mutation = await client.createMoleculeMutation({
266
+ mutationClass: MyCustomMutationClass
267
+ });
268
+ ```
269
+
270
+ - Sign and check a Molecule:
271
+ ```javascript
272
+ molecule.sign();
273
+ if (!molecule.check()) {
274
+ // Handle error
275
+ }
276
+ ```
277
+
278
+ - Execute a custom Query or Mutation:
279
+ ```javascript
280
+ const result = await client.executeQuery(myQueryOrMutation, variables);
281
+ ```
282
+
283
+ ## The Hard Way: DIY Everything
284
+
285
+ This method involves individually building Atoms and Molecules, triggering the signature and validation processes, and communicating the resulting signed Molecule mutation or Query to a Knish.IO node via your favorite GraphQL client.
345
286
 
346
287
  1. Include the relevant classes in your application code:
347
288
  ```javascript
348
- import { Molecule, Wallet } from '@wishknish/knishio-client-js'
289
+ import { Molecule, Wallet, Atom } from '@wishknish/knishio-client-js'
349
290
  ```
350
291
 
351
- 2. Generate a 2048-symbol hexadecimal secret, either randomly, or via hashing login + password + salt, OAuth secret ID,
352
- biometric ID, or any other static value
353
-
292
+ 2. Generate a 2048-symbol hexadecimal secret, either randomly, or via hashing login + password + salt, OAuth secret ID, biometric ID, or any other static value.
354
293
 
355
294
  3. (optional) Initialize a signing wallet with:
356
295
  ```javascript
357
- const wallet = new Wallet( {
296
+ const wallet = new Wallet({
358
297
  secret: mySecret,
359
298
  token: tokenSlug,
360
- position: myCustomPosition // (optional) instantiate specific wallet instance vs. random
361
-
362
- // (optional) helps you override the character set used by the wallet, for inter-ledger compatibility. Currently supported options are: `GMP`, `BITCOIN`, `FLICKR`, `RIPPLE`, and `IPFS`.
363
- // characters: myCharacterSet
364
- } )
299
+ position: myCustomPosition, // (optional) instantiate specific wallet instance vs. random
300
+ characters: myCharacterSet // (optional) override the character set used by the wallet
301
+ })
365
302
  ```
366
303
 
367
- **WARNING 1:** If ContinuID is enabled on the node, you will need to use a specific wallet, and therefore will first
368
- need to query the node to retrieve the `position` for that wallet.
304
+ **WARNING 1:** If ContinuID is enabled on the node, you will need to use a specific wallet, and therefore will first need to query the node to retrieve the `position` for that wallet.
369
305
 
370
306
  **WARNING 2:** The Knish.IO protocol mandates that all C and M transactions be signed with a `USER` token wallet.
371
307
 
372
-
373
308
  4. Build your molecule with:
374
309
  ```javascript
375
- const molecule = new Molecule( {
310
+ const molecule = new Molecule({
376
311
  secret: mySecret,
377
312
  sourceWallet: mySourceWallet, // (optional) wallet for signing
378
- remainderWaller: myRemainderWallet, // (optional) wallet to receive remainder tokens
379
- cellSlug: myCellSlug // (optional) used to point a transaction to a specific branch of the ledger
380
- } );
313
+ remainderWallet: myRemainderWallet, // (optional) wallet to receive remainder tokens
314
+ cellSlug: myCellSlug, // (optional) used to point a transaction to a specific branch of the ledger
315
+ version: 4 // (optional) specify the molecule version
316
+ });
381
317
  ```
382
318
 
383
- 5. Either use one of the shortcut methods provided by the `Molecule` class (which will build `Atom` instances for you),
384
- or create `Atom` instances yourself.
319
+ 5. Either use one of the shortcut methods provided by the `Molecule` class (which will build `Atom` instances for you), or create `Atom` instances yourself.
385
320
 
386
321
  DIY example:
387
322
  ```javascript
@@ -393,11 +328,11 @@ communicating the resulting signed Molecule mutation or Query to a Knish.IO node
393
328
  token: newWallet.token,
394
329
  bundle: newWallet.bundle,
395
330
  position: newWallet.position,
396
- batch_id: newWallet.batchId,
331
+ batchId: newWallet.batchId,
397
332
  }
398
-
333
+
399
334
  // Build the C isotope atom
400
- const walletCreationAtom = new Atom( {
335
+ const walletCreationAtom = new Atom({
401
336
  position: sourceWallet.position,
402
337
  walletAddress: sourceWallet.address,
403
338
  isotope: 'C',
@@ -406,13 +341,13 @@ communicating the resulting signed Molecule mutation or Query to a Knish.IO node
406
341
  metaId: newWallet.address,
407
342
  meta: newWalletMeta,
408
343
  index: molecule.generateIndex()
409
- } )
410
-
344
+ })
345
+
411
346
  // Add the atom to our molecule
412
- molecule.addAtom( walletCreationAtom )
413
-
347
+ molecule.addAtom(walletCreationAtom)
348
+
414
349
  // Adding a ContinuID / remainder atom
415
- molecule.addUserRemainderAtom( new Wallet( secret ) );
350
+ molecule.addContinuIdAtom();
416
351
  ```
417
352
 
418
353
  Molecule shortcut method example:
@@ -424,12 +359,13 @@ communicating the resulting signed Molecule mutation or Query to a Knish.IO node
424
359
  foo: 'Foo',
425
360
  bar: 'Bar'
426
361
  }
427
-
428
- molecule.initMeta( {
362
+
363
+ molecule.initMeta({
429
364
  meta: metadata,
430
365
  metaType: 'MyMetaType',
431
- metaId: 'MetaId123'
432
- } );
366
+ metaId: 'MetaId123',
367
+ policy: {} // Optional policy object
368
+ });
433
369
  ```
434
370
 
435
371
  6. Sign the molecule with the stored user secret:
@@ -439,52 +375,49 @@ communicating the resulting signed Molecule mutation or Query to a Knish.IO node
439
375
 
440
376
  7. Make sure everything checks out by verifying the molecule:
441
377
  ```javascript
442
- if ( !molecule.check() ) {
443
- // Throw some exception?
444
- }
445
-
446
- // If we're validating a V isotope transaction,
447
- // add the source wallet as a parameter
448
- if ( !molecule.check( sourceWallet ) ) {
449
- // Insufficient tokens?
378
+ try {
379
+ molecule.check();
380
+ // If we're validating a V isotope transaction,
381
+ // add the source wallet as a parameter
382
+ molecule.check(sourceWallet);
383
+ } catch (error) {
384
+ console.error('Molecule check failed:', error);
385
+ // Handle the error
450
386
  }
451
387
  ```
452
388
 
453
389
  8. Broadcast the molecule to a Knish.IO node:
454
390
  ```javascript
455
391
  // Build our query object using the KnishIOClient wrapper
456
- const query = new MutationProposeMolecule( client, molecule );
457
-
392
+ const mutation = await client.createMoleculeMutation({
393
+ mutationClass: MutationProposeMolecule,
394
+ molecule: molecule
395
+ });
396
+
458
397
  // Send the query to the node and get a response
459
- const response = await query.execute();
398
+ const response = await client.executeQuery(mutation);
460
399
  ```
461
400
 
462
401
  9. Inspect the response...
463
402
  ```javascript
464
403
  // For basic queries, we look at the data property:
465
- console.log( response.data() )
466
- ```
467
- If you are sending a mutation, you can also check if the molecule was accepted by the ledger:
468
- ```javascript
469
- // For mutations only
470
- console.log( response.success() )
471
-
404
+ console.log(response.data())
405
+
406
+ // For mutations, check if the molecule was accepted by the ledger:
407
+ console.log(response.success())
408
+
472
409
  // We can also check the reason for rejection
473
- console.log( response.reason() )
474
- ```
475
- Some queries may also produce a payload, with additional data:
476
- ```javascript
477
- console.log( response.payload() )
410
+ console.log(response.reason())
411
+
412
+ // Some queries may also produce a payload, with additional data:
413
+ console.log(response.payload())
478
414
  ```
415
+
479
416
  Payloads are provided by responses to the following queries:
480
417
  1. `QueryBalance` and `QueryContinuId` -> returns a `Wallet` instance
481
418
  2. `QueryWalletList` -> returns a list of `Wallet` instances
482
- 3. `MutationProposeMolecule`, `MutationRequestAuthorization`, `MutationCreateIdentifier`, `MutationLinkIdentifier`
483
- , `MutationClaimShadowWallet`, `MutationCreateToken`, `MutationRequestTokens`, and `MutationTransferTokens` ->
484
- returns molecule metadata
419
+ 3. `MutationProposeMolecule`, `MutationRequestAuthorization`, `MutationCreateIdentifier`, `MutationLinkIdentifier`, `MutationClaimShadowWallet`, `MutationCreateToken`, `MutationRequestTokens`, and `MutationTransferTokens` -> returns molecule metadata
485
420
 
486
421
  ## Getting Help
487
422
 
488
- Knish.IO is active development, and our team is ready to assist with integration questions. The best way to seek help is
489
- to stop by our [Telegram Support Channel](https://t.me/wishknish). You can
490
- also [send us a contact request](https://knish.io/contact) via our website.
423
+ Knish.IO is under active development, and our team is ready to assist with integration questions. The best way to seek help is to stop by our [Telegram Support Channel](https://t.me/wishknish). You can also [send us a contact request](https://knish.io/contact) via our website.