@wishknish/knishio-client-js 0.5.1 → 0.5.2
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/package.json +1 -1
- package/src/KnishIOClient.js +1 -1
- package/src/test/TestTokenUnit.js +340 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wishknish/knishio-client-js",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"productName": "Knish.IO Javascript SDK Client",
|
|
5
5
|
"description": "JavaScript implementation of the Knish.IO SDK to consume Knish.IO GraphQL APIs.",
|
|
6
6
|
"license": "GPL-3.0-or-later",
|
package/src/KnishIOClient.js
CHANGED
|
@@ -1127,7 +1127,7 @@ export default class KnishIOClient {
|
|
|
1127
1127
|
// What is the fungibility mode for this token?
|
|
1128
1128
|
const fungibility = Dot.get( meta || {}, 'fungibility' );
|
|
1129
1129
|
|
|
1130
|
-
//
|
|
1130
|
+
// For stackable token - create a batch ID
|
|
1131
1131
|
if ( fungibility === 'stackable' ) {
|
|
1132
1132
|
meta.batchId = batchId || generateBatchId( {} );
|
|
1133
1133
|
}
|
|
@@ -0,0 +1,340 @@
|
|
|
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 KnishIOClient from '../KnishIOClient';
|
|
49
|
+
import Dot from '../libraries/Dot';
|
|
50
|
+
import {
|
|
51
|
+
generateBundleHash,
|
|
52
|
+
generateSecret
|
|
53
|
+
} from '../libraries/crypto';
|
|
54
|
+
import ResponseMolecule from '../response/ResponseProposeMolecule';
|
|
55
|
+
import TokenUnit from '../TokenUnit';
|
|
56
|
+
|
|
57
|
+
/*
|
|
58
|
+
|
|
59
|
+
import Test from '@wishknish/knishio-client-js/src/test/Test';
|
|
60
|
+
import { KNISHIO_SETTINGS, } from 'src/libraries/constants/knishio';
|
|
61
|
+
|
|
62
|
+
// Run all test
|
|
63
|
+
await TestTokenUnit.run( KNISHIO_SETTINGS.serverUriConfig );
|
|
64
|
+
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
export default class TestTokenUnit {
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Run all
|
|
71
|
+
*/
|
|
72
|
+
static async run ( uris ) {
|
|
73
|
+
for ( let uriIndex in uris ) {
|
|
74
|
+
let test = new TestTokenUnit( uris[ uriIndex ] );
|
|
75
|
+
await test.testAll();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
*
|
|
81
|
+
* @param graphqlUrl
|
|
82
|
+
* @param encrypt
|
|
83
|
+
*/
|
|
84
|
+
constructor ( graphqlUrl, encrypt = false ) {
|
|
85
|
+
this.encrypt = encrypt;
|
|
86
|
+
this.secrets = [ generateSecret(), generateSecret() ];
|
|
87
|
+
this.tokenSlugs = [ 'UTNFUNGUNIT' ];
|
|
88
|
+
this.graphqlUrl = graphqlUrl;
|
|
89
|
+
console.log( `---------- GraphQL URI: ${ this.graphqlUrl }` );
|
|
90
|
+
|
|
91
|
+
this.clients = {};
|
|
92
|
+
this.tokenUnits = [
|
|
93
|
+
[ 'unit_id_1', 'unit_name_1', {url: "test1.com"} ],
|
|
94
|
+
[ 'unit_id_2', 'unit_name_2', {url: "test2.com"} ],
|
|
95
|
+
[ 'unit_id_3', 'unit_name_3', {url: "test3.com"} ],
|
|
96
|
+
[ 'unit_id_4', 'unit_name_4', {url: "test4.com"} ],
|
|
97
|
+
[ 'unit_id_5', 'unit_name_5', {url: "test5.com"} ],
|
|
98
|
+
[ 'unit_id_6', 'unit_name_6', {url: "test6.com"} ],
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Test all KnishIOClient functions
|
|
105
|
+
*/
|
|
106
|
+
async testAll () {
|
|
107
|
+
console.info( `Executing test for: ${ this.graphqlUrl }...` );
|
|
108
|
+
|
|
109
|
+
await this.client( this.secrets[ 0 ] );
|
|
110
|
+
await this.client( this.secrets[ 1 ] );
|
|
111
|
+
|
|
112
|
+
await this.testCreateToken();
|
|
113
|
+
await this.queryWalletsOutput();
|
|
114
|
+
|
|
115
|
+
await this.testTransferToken1();
|
|
116
|
+
await this.queryWalletsOutput();
|
|
117
|
+
|
|
118
|
+
await this.testTransferToken2();
|
|
119
|
+
await this.queryWalletsOutput();
|
|
120
|
+
|
|
121
|
+
await this.testClaimShadowWallet();
|
|
122
|
+
await this.queryWalletsOutput();
|
|
123
|
+
|
|
124
|
+
await this.testTransferTokenBack1();
|
|
125
|
+
await this.queryWalletsOutput();
|
|
126
|
+
|
|
127
|
+
await this.testTransferTokenBack2();
|
|
128
|
+
await this.queryWalletsOutput();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @throws \Exception
|
|
133
|
+
*/
|
|
134
|
+
async testCreateToken () {
|
|
135
|
+
|
|
136
|
+
let responses = {};
|
|
137
|
+
|
|
138
|
+
// Regular stackable token
|
|
139
|
+
let client = await this.client( this.secrets[ 0 ] );
|
|
140
|
+
|
|
141
|
+
// --------- UNITABLE TOKENS ----------
|
|
142
|
+
|
|
143
|
+
// Create stackable unit token
|
|
144
|
+
responses[ 0 ] = await client.createToken( {
|
|
145
|
+
token: this.tokenSlugs[ 0 ],
|
|
146
|
+
units: this.tokenUnits,
|
|
147
|
+
meta: {
|
|
148
|
+
name: this.tokenSlugs[ 0 ],
|
|
149
|
+
supply: 'replenishable',
|
|
150
|
+
fungibility: 'nonfungible'
|
|
151
|
+
}
|
|
152
|
+
} );
|
|
153
|
+
this.checkResponse( responses[ 0 ], 'testCreateToken.0' );
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
*
|
|
159
|
+
* @return {Promise<void>}
|
|
160
|
+
*/
|
|
161
|
+
async testTransferToken1 () {
|
|
162
|
+
|
|
163
|
+
let bundleHash = generateBundleHash( this.secrets[ 1 ] );
|
|
164
|
+
|
|
165
|
+
let client = await this.client( this.secrets[ 0 ] );
|
|
166
|
+
let response = await client.transferToken( {
|
|
167
|
+
bundleHash,
|
|
168
|
+
token: this.tokenSlugs[ 0 ],
|
|
169
|
+
units: [ 'unit_id_1', 'unit_id_2' ],
|
|
170
|
+
} );
|
|
171
|
+
this.checkResponse( response, 'testTransferUnitToken.1' );
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
*
|
|
176
|
+
* @return {Promise<void>}
|
|
177
|
+
*/
|
|
178
|
+
async testTransferToken2 () {
|
|
179
|
+
|
|
180
|
+
let bundleHash = generateBundleHash( this.secrets[ 1 ] );
|
|
181
|
+
|
|
182
|
+
let client = await this.client( this.secrets[ 0 ] );
|
|
183
|
+
let response = await client.transferToken( {
|
|
184
|
+
bundleHash,
|
|
185
|
+
token: this.tokenSlugs[ 0 ],
|
|
186
|
+
units: [ 'unit_id_3', 'unit_id_4' ],
|
|
187
|
+
} );
|
|
188
|
+
this.checkResponse( response, 'testTransferUnitToken.2' );
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
*
|
|
193
|
+
*/
|
|
194
|
+
async testClaimShadowWallet () {
|
|
195
|
+
let client = await this.client( this.secrets[ 1 ] );
|
|
196
|
+
|
|
197
|
+
let response = await client.claimShadowWallet( {
|
|
198
|
+
token: this.tokenSlugs[ 0 ]
|
|
199
|
+
} );
|
|
200
|
+
this.checkResponse( response, 'testClaimShadowWallet' );
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
*
|
|
206
|
+
* @return {Promise<void>}
|
|
207
|
+
*/
|
|
208
|
+
async testTransferTokenBack1 () {
|
|
209
|
+
|
|
210
|
+
let bundleHash = generateBundleHash( this.secrets[ 0 ] );
|
|
211
|
+
|
|
212
|
+
let client = await this.client( this.secrets[ 1 ] );
|
|
213
|
+
let response = await client.transferToken( {
|
|
214
|
+
bundleHash,
|
|
215
|
+
token: this.tokenSlugs[ 0 ],
|
|
216
|
+
units: [ 'unit_id_1', ],
|
|
217
|
+
} );
|
|
218
|
+
this.checkResponse( response, 'testTransferBackToken.1' );
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
*
|
|
223
|
+
* @return {Promise<void>}
|
|
224
|
+
*/
|
|
225
|
+
async testTransferTokenBack2 () {
|
|
226
|
+
|
|
227
|
+
let bundleHash = generateBundleHash( this.secrets[ 0 ] );
|
|
228
|
+
|
|
229
|
+
let client = await this.client( this.secrets[ 1 ] );
|
|
230
|
+
let response = await client.transferToken( {
|
|
231
|
+
bundleHash,
|
|
232
|
+
token: this.tokenSlugs[ 0 ],
|
|
233
|
+
units: [ 'unit_id_3' ],
|
|
234
|
+
} );
|
|
235
|
+
this.checkResponse( response, 'testTransferBackToken.2' );
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
*
|
|
241
|
+
*/
|
|
242
|
+
async queryWalletsOutput() {
|
|
243
|
+
await this.queryWallet( this.secrets[ 0 ], 'Token creation wallet' );
|
|
244
|
+
await this.queryWallet( this.secrets[ 1 ], 'Recipient wallet' );
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
*
|
|
250
|
+
*/
|
|
251
|
+
async queryWallet ( secret, title ) {
|
|
252
|
+
let client = await this.client( secret );
|
|
253
|
+
let response = await client.queryWallets( {
|
|
254
|
+
token: this.tokenSlugs[ 0 ]
|
|
255
|
+
} );
|
|
256
|
+
|
|
257
|
+
let outputData = 'Wallet not found.';
|
|
258
|
+
if ( response[ 0 ] ) {
|
|
259
|
+
outputData = response[ 0 ].tokenUnits;
|
|
260
|
+
}
|
|
261
|
+
console.warn( `--- Query wallet: ${ title }: `, outputData );
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Get a client for each secret
|
|
267
|
+
*
|
|
268
|
+
* @param secret
|
|
269
|
+
* @param cellSlug
|
|
270
|
+
* @returns {Promise<*>}
|
|
271
|
+
*/
|
|
272
|
+
async client ( secret, cellSlug = 'unit_test' ) {
|
|
273
|
+
|
|
274
|
+
// Create new client
|
|
275
|
+
if ( !this.clients[ secret ] ) {
|
|
276
|
+
|
|
277
|
+
// Create a client
|
|
278
|
+
this.clients[ secret ] = new KnishIOClient( {
|
|
279
|
+
uri: this.graphqlUrl,
|
|
280
|
+
logging: true
|
|
281
|
+
} );
|
|
282
|
+
|
|
283
|
+
// Auth the client
|
|
284
|
+
await this.clients[ secret ]
|
|
285
|
+
.requestAuthToken( {
|
|
286
|
+
secret,
|
|
287
|
+
encrypt: this.encrypt,
|
|
288
|
+
cellSlug
|
|
289
|
+
} );
|
|
290
|
+
if ( !this.clients[ secret ].getAuthToken() ) {
|
|
291
|
+
console.log( 'Error with authorize - get an empty response.' );
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Return the client by secret
|
|
296
|
+
return this.clients[ secret ];
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Check a response
|
|
302
|
+
* @param response
|
|
303
|
+
* @param key
|
|
304
|
+
*/
|
|
305
|
+
checkResponse ( response, key ) {
|
|
306
|
+
|
|
307
|
+
console.log( ` ############### ${ key } ###############` );
|
|
308
|
+
console.log( response );
|
|
309
|
+
|
|
310
|
+
// Check molecule response
|
|
311
|
+
if ( response instanceof ResponseMolecule ) {
|
|
312
|
+
if ( !response.success() ) {
|
|
313
|
+
this.debug( response );
|
|
314
|
+
}
|
|
315
|
+
console.assert( response.success(), response );
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Default response
|
|
319
|
+
else {
|
|
320
|
+
this.debug( response );
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Debug output
|
|
327
|
+
* @param response
|
|
328
|
+
*/
|
|
329
|
+
debug ( response ) {
|
|
330
|
+
|
|
331
|
+
// Reason data on the top of the output
|
|
332
|
+
if ( response.data && Dot.get( response.data() || {}, 'reason' ) ) {
|
|
333
|
+
console.log( response.data().reason );
|
|
334
|
+
} else {
|
|
335
|
+
console.log( response );
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
}
|