@wishknish/knishio-client-js 0.4.39 → 0.4.42

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/src/Molecule.js CHANGED
@@ -56,10 +56,10 @@ import CheckMolecule from './libraries/check';
56
56
  import { generateBundleHash } from './libraries/crypto';
57
57
  import AtomsMissingException from './exception/AtomsMissingException';
58
58
  import BalanceInsufficientException from './exception/BalanceInsufficientException';
59
- import MetaMissingException from './exception/MetaMissingException';
60
59
  import NegativeAmountException from './exception/NegativeAmountException';
61
60
  import { deepCloning } from './libraries/array';
62
61
  import Meta from './Meta';
62
+ import Rule from './instance/Rules/Rule';
63
63
 
64
64
  const USE_META_CONTEXT = false;
65
65
  const DEFAULT_META_CONTEXT = 'https://www.schema.org';
@@ -158,13 +158,26 @@ export default class Molecule {
158
158
  meta = meta || {};
159
159
  wallet = wallet || this.sourceWallet;
160
160
 
161
+ meta[ 'pubkey' ] = wallet.pubkey;
162
+ meta[ 'characters' ] = wallet.characters;
163
+
164
+ return meta;
165
+ }
166
+
167
+
168
+ /**
169
+ *
170
+ * @param wallet
171
+ * @param meta
172
+ * @returns {*[]}
173
+ */
174
+ tokenUnitMetas ( wallet, meta = [] ) {
175
+
176
+ // Add token units meta key
161
177
  if ( wallet.hasTokenUnits() ) {
162
178
  meta[ 'tokenUnits' ] = wallet.tokenUnitsJson();
163
179
  }
164
180
 
165
- meta[ 'pubkey' ] = wallet.pubkey;
166
- meta[ 'characters' ] = wallet.characters;
167
-
168
181
  return meta;
169
182
  }
170
183
 
@@ -187,49 +200,6 @@ export default class Molecule {
187
200
  }
188
201
 
189
202
 
190
- /*
191
- * Replenishes non-finite token supplies
192
- *
193
- * @param {number} amount
194
- * @param {string} token
195
- * @param {array|object} metas
196
- * @return {Molecule}
197
- */
198
- replenishTokens ( {
199
- amount,
200
- token,
201
- metas
202
- } ) {
203
- metas.action = 'add';
204
-
205
- for ( let key of [ 'address', 'position', 'batchId' ] ) {
206
- if ( typeof metas[ key ] === 'undefined' ) {
207
- throw new MetaMissingException( `Molecule::replenishTokens() - Missing ${ key } in meta!` );
208
- }
209
- }
210
-
211
- this.molecularHash = null;
212
-
213
- this.atoms.push(
214
- Atom.create.C( {
215
- position: this.sourceWallet.position,
216
- walletAddress: this.sourceWallet.address,
217
- token: this.sourceWallet.token,
218
- amount,
219
- batchId: this.sourceWallet.batchId,
220
- metaType: 'token',
221
- metaId: token,
222
- meta: this.finalMetas( metas ),
223
- index: this.generateIndex()
224
- } )
225
- );
226
-
227
- this.addUserRemainderAtom( this.remainderWallet );
228
- this.atoms = Atom.sortAtoms( this.atoms );
229
-
230
- return this;
231
- }
232
-
233
203
  /**
234
204
  * Add user remainder atom for ContinuID
235
205
  *
@@ -291,6 +261,7 @@ export default class Molecule {
291
261
  return this;
292
262
  }
293
263
 
264
+
294
265
  /**
295
266
  * Burns some amount of tokens from a wallet
296
267
  *
@@ -321,7 +292,7 @@ export default class Molecule {
321
292
  token: this.sourceWallet.token,
322
293
  value: -amount,
323
294
  batchId: this.sourceWallet.batchId,
324
- meta: this.finalMetas( {} ),
295
+ meta: this.finalMetas( this.tokenUnitMetas( this.sourceWallet ) ),
325
296
  index: this.generateIndex()
326
297
  } )
327
298
  );
@@ -335,7 +306,7 @@ export default class Molecule {
335
306
  batchId: this.remainderWallet.batchId,
336
307
  metaType: walletBundle ? 'walletBundle' : null,
337
308
  metaId: walletBundle,
338
- meta: this.finalMetas( {}, this.remainderWallet ),
309
+ meta: this.finalMetas( this.tokenUnitMetas( this.remainderWallet ), this.remainderWallet ),
339
310
  index: this.generateIndex()
340
311
  } )
341
312
  );
@@ -346,6 +317,83 @@ export default class Molecule {
346
317
 
347
318
  }
348
319
 
320
+
321
+ /*
322
+ * Replenishes non-finite token supplies
323
+ *
324
+ * @param {number} amount
325
+ * @param {string} token
326
+ * @param {array|object} metas
327
+ * @return {Molecule}
328
+ */
329
+ replenishToken ( {
330
+ amount,
331
+ units = []
332
+ } ) {
333
+
334
+ if ( amount < 0 ) {
335
+ throw new NegativeAmountException( 'Molecule::replenishToken() - Amount to replenish must be positive!' );
336
+ }
337
+
338
+ // Special code for the token unit logic
339
+ if ( units.length ) {
340
+
341
+ // Prepare token units to formatted style
342
+ units = Wallet.getTokenUnits( units );
343
+
344
+ // Merge token units with source wallet & new items
345
+ this.remainderWallet.tokenUnits = this.sourceWallet.tokenUnits;
346
+ for ( const unit of units ) {
347
+ this.remainderWallet.tokenUnits.push( unit );
348
+ }
349
+ this.remainderWallet.balance = this.remainderWallet.tokenUnits.length;
350
+
351
+ // Override first atom'a token units to replenish values
352
+ this.sourceWallet.tokenUnits = units;
353
+ this.sourceWallet.balance = this.sourceWallet.tokenUnits.length;
354
+ }
355
+
356
+ // Update wallet's balances
357
+ else {
358
+ this.remainderWallet.balance = this.sourceWallet.balance + amount;
359
+ this.sourceWallet.balance = amount;
360
+ }
361
+
362
+ this.molecularHash = null;
363
+
364
+ // Initializing a new Atom to remove tokens from source
365
+ this.atoms.push(
366
+ Atom.create.V( {
367
+ position: this.sourceWallet.position,
368
+ walletAddress: this.sourceWallet.address,
369
+ token: this.sourceWallet.token,
370
+ value: this.sourceWallet.balance,
371
+ batchId: this.sourceWallet.batchId,
372
+ meta: this.finalMetas( this.tokenUnitMetas( this.sourceWallet ) ),
373
+ index: this.generateIndex()
374
+ } )
375
+ );
376
+
377
+ const walletBundle = this.remainderWallet.bundle;
378
+ this.atoms.push(
379
+ Atom.create.V( {
380
+ position: this.remainderWallet.position,
381
+ walletAddress: this.remainderWallet.address,
382
+ token: this.sourceWallet.token,
383
+ value: this.remainderWallet.balance,
384
+ batchId: this.remainderWallet.batchId,
385
+ metaType: walletBundle ? 'walletBundle' : null,
386
+ metaId: walletBundle,
387
+ meta: this.finalMetas( this.tokenUnitMetas( this.remainderWallet ), this.remainderWallet ),
388
+ index: this.generateIndex()
389
+ } )
390
+ );
391
+
392
+ this.atoms = Atom.sortAtoms( this.atoms );
393
+
394
+ return this;
395
+ }
396
+
349
397
  /**
350
398
  * Initialize a V-type molecule to transfer value from one wallet to another, with a third,
351
399
  * regenerated wallet receiving the remainder
@@ -373,7 +421,7 @@ export default class Molecule {
373
421
  token: this.sourceWallet.token,
374
422
  value: -amount,
375
423
  batchId: this.sourceWallet.batchId,
376
- meta: this.finalMetas( {} ),
424
+ meta: this.finalMetas( this.tokenUnitMetas( this.sourceWallet ) ),
377
425
  index: this.generateIndex()
378
426
  } )
379
427
  );
@@ -388,7 +436,7 @@ export default class Molecule {
388
436
  batchId: recipientWallet.batchId,
389
437
  metaType: 'walletBundle',
390
438
  metaId: recipientWallet.bundle,
391
- meta: this.finalMetas( {}, recipientWallet ),
439
+ meta: this.finalMetas( this.tokenUnitMetas( recipientWallet ), recipientWallet ),
392
440
  index: this.generateIndex()
393
441
  } )
394
442
  );
@@ -402,7 +450,7 @@ export default class Molecule {
402
450
  batchId: this.remainderWallet.batchId,
403
451
  metaType: 'walletBundle',
404
452
  metaId: this.sourceWallet.bundle,
405
- meta: this.finalMetas( {}, this.remainderWallet ),
453
+ meta: this.finalMetas( this.tokenUnitMetas( this.remainderWallet ), this.remainderWallet ),
406
454
  index: this.generateIndex()
407
455
  } )
408
456
  );
@@ -471,7 +519,7 @@ export default class Molecule {
471
519
  for ( const walletKey of [ 'walletAddress', 'walletPosition', 'walletPubkey', 'walletCharacters' ] ) {
472
520
  // Importing wallet fields into meta object
473
521
  if ( !meta[ walletKey ] ) {
474
- meta[ walletKey ] = recipientWallet[ walletKey.toLowerCase().substr( 6 ) ];
522
+ meta[ walletKey ] = recipientWallet[ walletKey.toLowerCase().substring( 6 ) ];
475
523
  }
476
524
  }
477
525
 
@@ -501,27 +549,28 @@ export default class Molecule {
501
549
  *
502
550
  * @param {string} metaType
503
551
  * @param {string} metaId
504
- * @param {object|array} meta
552
+ * @param {object[]} rule,
553
+ * @param {object} policy
505
554
  * @return {Molecule}
506
555
  */
507
556
  createRule ( {
508
557
  metaType,
509
558
  metaId,
510
- meta
559
+ rule,
560
+ policy = {}
511
561
  } ) {
512
- for ( let key of [ 'conditions', 'callback', 'rule' ] ) {
513
-
514
- if ( typeof meta[ key ] === 'undefined' ) {
515
- throw new MetaMissingException( `Molecule::createRule() - Value for required meta key ${ key } in missing!` );
516
- }
562
+ const $rules = [];
517
563
 
518
- for ( let item of [ '[object Object]', '[object Array]' ] ) {
519
- if ( Object.prototype.toString.call( meta[ key ] ) === item ) {
520
- meta[ key ] = JSON.stringify( meta[ key ] );
521
- }
522
- }
564
+ for ( const $rule of rule ) {
565
+ $rules.push( $rule instanceof Rule ? $rule : Rule.toObject( $rule ) );
523
566
  }
524
567
 
568
+ const rules = {
569
+ rule: JSON.stringify( $rules )
570
+ };
571
+
572
+ const policies = Meta.policy( rules, policy );
573
+
525
574
  this.addAtom(
526
575
  Atom.create.R( {
527
576
  position: this.sourceWallet.position,
@@ -529,7 +578,7 @@ export default class Molecule {
529
578
  token: this.sourceWallet.token,
530
579
  metaType,
531
580
  metaId,
532
- meta: this.finalMetas( meta, this.sourceWallet ),
581
+ meta: { ...rules, ...policies },
533
582
  index: this.generateIndex()
534
583
  } )
535
584
  );
@@ -45,7 +45,6 @@ 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 { Operation } from '@apollo/client/core';
49
48
  import { operationName } from '../libraries/ApolloLink/handler';
50
49
  import Client from '../libraries/ApolloLink/Client';
51
50
 
@@ -0,0 +1,124 @@
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
+
49
+ import Meta from './Meta';
50
+ import RuleArgumentException from './exception/RuleArgumentException';
51
+
52
+
53
+ export default class Callback {
54
+ // server to execute signed molecule with given atom meta; other options: 'reject' (reject matches);
55
+ // 'accept' (reject non-matches), 'burn' (require appropriate burning of tokens in same molecule)
56
+ #action
57
+ // supporting metadata for executing callback goes here
58
+ #meta = null
59
+
60
+ /**
61
+ *
62
+ * @param {string} action
63
+ * @param {Meta|null} meta
64
+ */
65
+ constructor ( {
66
+ action,
67
+ meta
68
+ } ) {
69
+ if ( meta ) {
70
+ this.meta = meta;
71
+ }
72
+
73
+ if ( !action ) {
74
+ throw new RuleArgumentException( 'Callback structure violated, missing mandatory "action" parameter.' );
75
+ }
76
+
77
+ this.#action = action;
78
+ }
79
+
80
+ set meta ( meta ) {
81
+ if ( ! ( meta instanceof Meta ) ) {
82
+ throw new RuleArgumentException( 'Incorrect meta argument. The meta argument can only be an instance of the Meta class.' );
83
+ }
84
+
85
+ this.#meta = meta;
86
+ }
87
+
88
+ toJSON () {
89
+ const meta = {
90
+ action: this.#action
91
+ };
92
+
93
+ if ( this.#meta ) {
94
+ meta.meta = this.#meta;
95
+ }
96
+
97
+ return meta;
98
+ }
99
+
100
+ /**
101
+ *
102
+ * @param {object} object
103
+ *
104
+ * @return Callback
105
+ */
106
+ static toObject ( object ) {
107
+ const callback = new Callback( {
108
+ action: object.action,
109
+ meta: null
110
+ } );
111
+
112
+ if ( object.meta ) {
113
+ callback.meta = new Meta( {
114
+ metaType: object.meta.metaType,
115
+ metaId: object.meta.metaId,
116
+ isotope: object.meta.isotope,
117
+ token: object.meta.token,
118
+ amount: object.meta.amount
119
+ } );
120
+ }
121
+
122
+ return callback;
123
+ }
124
+ }
@@ -0,0 +1,90 @@
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
+
49
+ export default class Meta {
50
+
51
+ #metaType
52
+ #metaId
53
+ #isotope
54
+ #token
55
+ #amount
56
+
57
+ /**
58
+ *
59
+ * @param {string} metaType
60
+ * @param {string} metaId
61
+ * @param {string} isotope
62
+ * @param {string} token
63
+ * @param {number} amount
64
+ */
65
+ constructor ( {
66
+ metaType,
67
+ // keyword that will be automatically replaced on server with sender's wallet bundle,
68
+ metaId,
69
+ isotope,
70
+ token,
71
+ amount
72
+ } ) {
73
+ this.#metaType = metaType;
74
+ this.#metaId = metaId;
75
+ this.#isotope = isotope;
76
+ this.#token = token;
77
+ this.#amount = amount;
78
+ }
79
+
80
+
81
+ toJSON () {
82
+ return {
83
+ metaType: this.#metaType,
84
+ metaId: this.#metaId,
85
+ isotope: this.#isotope,
86
+ token: this.#token,
87
+ amount: this.#amount
88
+ };
89
+ }
90
+ }
@@ -0,0 +1,142 @@
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
+
49
+ import Callback from './Callback';
50
+ import RuleArgumentException from './exception/RuleArgumentException';
51
+
52
+
53
+ export default class Rule {
54
+ // which metadata field are we targeting? (if we're dealing with a ledger object like a Wallet,
55
+ // this could be internal fields like 'amount', etc.)
56
+ #key
57
+ // what is the target value that will trigger the rule? (meta value OR token amount)
58
+ #value
59
+ // same list of possible options as when querying
60
+ #comparison
61
+ #callback
62
+
63
+ /**
64
+ *
65
+ * @param {string} key
66
+ * @param {string} value
67
+ * @param {string} comparison
68
+ * @param {Callback[]} callback
69
+ */
70
+ constructor ( {
71
+ key,
72
+ value,
73
+ comparison = '===',
74
+ callback = []
75
+ } ) {
76
+ if ( !key ) {
77
+ throw new RuleArgumentException( 'Rule structure violated, missing mandatory "key" parameter!' );
78
+ }
79
+
80
+ if ( !value ) {
81
+ throw new RuleArgumentException( 'Rule structure violated, missing mandatory "value" parameter' );
82
+ }
83
+
84
+ for ( const element of callback ) {
85
+ if ( ! ( element instanceof Callback ) ) {
86
+ throw new RuleArgumentException();
87
+ }
88
+ }
89
+
90
+ this.#key = key;
91
+ this.#value = value;
92
+ this.#comparison = comparison;
93
+ this.#callback = callback;
94
+ }
95
+
96
+ /**
97
+ *
98
+ * @param {string} comparison
99
+ */
100
+ set comparison ( comparison ) {
101
+ this.#comparison = comparison;
102
+ }
103
+
104
+ set callback ( callback ) {
105
+ this.#callback.push( callback );
106
+ }
107
+
108
+ toJSON () {
109
+ return {
110
+ key: this.#key,
111
+ value: this.#value,
112
+ comparison: this.#comparison,
113
+ callback: this.#callback
114
+ };
115
+ }
116
+
117
+ /**
118
+ *
119
+ * @param {object} object
120
+ *
121
+ * @return {Rule}
122
+ */
123
+ static toObject ( object ) {
124
+
125
+ const rule = new Rule( {
126
+ key: object.key,
127
+ value: object.value
128
+ } );
129
+
130
+ if ( object.comparison ) {
131
+ rule.comparison = object.comparison;
132
+ }
133
+
134
+ if ( object.callback ) {
135
+ for ( const callback of object.callback ) {
136
+ rule.callback = callback instanceof Callback ? callback : Callback.toObject( callback );
137
+ }
138
+ }
139
+
140
+ return rule;
141
+ }
142
+ }
@@ -0,0 +1,15 @@
1
+ import BaseException from '../../../exception/BaseException';
2
+
3
+ export default class RuleArgumentException extends BaseException {
4
+ /**
5
+ * Class constructor
6
+ *
7
+ * @param {string} message
8
+ * @param {string|null} fileName
9
+ * @param {number|null} lineNumber
10
+ */
11
+ constructor ( message = 'An incorrect argument!', fileName = null, lineNumber = null ) {
12
+ super( message, fileName, lineNumber );
13
+ this.name = 'RuleArgumentException';
14
+ }
15
+ }
@@ -46,9 +46,7 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
48
  import {
49
- ApolloLink,
50
- Operation,
51
- NextLink
49
+ ApolloLink
52
50
  } from '@apollo/client/core';
53
51
 
54
52
 
@@ -1,7 +1,5 @@
1
1
  import {
2
- ApolloLink,
3
- Operation,
4
- NextLink
2
+ ApolloLink
5
3
  } from '@apollo/client/core';
6
4
  import { print } from 'graphql';
7
5
  import { gql } from '@apollo/client/core';