wowok 1.1.0 → 1.1.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.
Files changed (54) hide show
  1. package/README.md +1 -1
  2. package/dist/demand.d.ts +2 -3
  3. package/dist/demand.d.ts.map +1 -1
  4. package/dist/demand.js +19 -11
  5. package/dist/exception.d.ts +2 -1
  6. package/dist/exception.d.ts.map +1 -1
  7. package/dist/exception.js +1 -0
  8. package/dist/graphql.d.ts +2 -0
  9. package/dist/graphql.d.ts.map +1 -1
  10. package/dist/graphql.js +19 -0
  11. package/dist/guard.d.ts +18 -23
  12. package/dist/guard.d.ts.map +1 -1
  13. package/dist/guard.js +220 -199
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +1 -0
  17. package/dist/machine.d.ts +6 -6
  18. package/dist/machine.d.ts.map +1 -1
  19. package/dist/machine.js +7 -7
  20. package/dist/passport.d.ts +34 -22
  21. package/dist/passport.d.ts.map +1 -1
  22. package/dist/passport.js +124 -214
  23. package/dist/permission.d.ts +3 -0
  24. package/dist/permission.d.ts.map +1 -1
  25. package/dist/permission.js +5 -2
  26. package/dist/progress.js +3 -3
  27. package/dist/protocol.d.ts +11 -5
  28. package/dist/protocol.d.ts.map +1 -1
  29. package/dist/protocol.js +30 -6
  30. package/dist/repository.d.ts +2 -0
  31. package/dist/repository.d.ts.map +1 -1
  32. package/dist/repository.js +73 -5
  33. package/dist/service.d.ts +3 -3
  34. package/dist/service.d.ts.map +1 -1
  35. package/dist/service.js +115 -116
  36. package/dist/utils.d.ts +10 -2
  37. package/dist/utils.d.ts.map +1 -1
  38. package/dist/utils.js +39 -1
  39. package/dist/vote.js +8 -8
  40. package/package.json +1 -1
  41. package/src/exception.ts +1 -0
  42. package/src/graphql.ts +20 -0
  43. package/src/guard.ts +208 -192
  44. package/src/index.ts +1 -0
  45. package/src/machine.ts +10 -10
  46. package/src/passport.ts +139 -251
  47. package/src/permission.ts +5 -2
  48. package/src/progress.ts +3 -3
  49. package/src/protocol.ts +26 -5
  50. package/src/repository.ts +73 -5
  51. package/src/service.ts +25 -25
  52. package/src/utils.ts +44 -4
  53. package/src/vote.ts +8 -8
  54. package/tsconfig.json +3 -2
package/src/guard.ts CHANGED
@@ -2,142 +2,37 @@
2
2
  import { BCS } from '@mysten/bcs';
3
3
  import { Protocol, GuardAddress, FnCallType, Data_Type, MODULES, ContextType, ValueType, OperatorType} from './protocol';
4
4
  import { concatenate, array_equal } from './utils';
5
- import { IsValidDesription, BCS_CONVERT, IsValidInt, IsValidAddress } from './utils';
5
+ import { IsValidDesription, Bcs, IsValidInt, IsValidAddress } from './utils';
6
6
  import { ERROR, Errors } from './exception';
7
7
 
8
- export enum Guard_Sense_Binder {
9
- AND = 0, // first sense of guard always AND; for combining other guards
10
- OR = 1,
11
- }
12
-
13
- export type Guard_Sense = {
14
- input: Uint8Array;
15
- notAfterSense: boolean;
16
- binder:Guard_Sense_Binder ;
17
- };
18
8
  export type VariableType = Map<number, Guard_Vriable>;
19
9
 
20
- export type Guard_Vriable = {
10
+ export interface Guard_Vriable {
21
11
  type: ContextType | OperatorType,
22
12
  value?: Uint8Array,
23
13
  witness?: Uint8Array,
24
14
  }
15
+
25
16
  export type Guard_Creation = {
26
17
  description: string;
27
18
  variables?: VariableType;
28
- senses: Guard_Sense[];
19
+ input: Uint8Array;
29
20
  }
30
21
 
31
22
  export class Guard {
32
- static MAX_SENSE_COUNT = 16;
33
- static IsValidGuardVirableType = (type:OperatorType | ContextType) : boolean => {
34
- if (type == OperatorType.TYPE_FUTURE_QUERY || type == ContextType.TYPE_CONTEXT_FUTURE_ID || type == OperatorType.TYPE_QUERY_FROM_CONTEXT ||
35
- type == ContextType.TYPE_CONTEXT_bool || type ==ContextType.TYPE_CONTEXT_address || type == ContextType.TYPE_CONTEXT_u64 ||
36
- type == ContextType.TYPE_CONTEXT_u8 || type == ContextType.TYPE_CONTEXT_vec_u8) {
37
- return true;
38
- };
39
- return false;
40
- }
41
- static IsValidIndentifier = (identifier:number) : boolean => {
42
- if (!IsValidInt(identifier) || identifier > 255) return false;
43
- return true
44
- }
45
- static get_variable_value(variables:VariableType, identifier:number, type:OperatorType | ContextType) : Uint8Array | boolean {
46
- if (variables.has(identifier)) {
47
- let v = variables.get(identifier);
48
- if (v?.value && v.type == type) {
49
- return v.value;
50
- }
51
- } return false;
52
- }
53
- static get_variable_witness(variables:VariableType, identifier:number, type:OperatorType | ContextType) : Uint8Array | boolean {
54
- if (variables.has(identifier)) {
55
- let v = variables.get(identifier);
56
- if (v?.witness && v.type == type) {
57
- return v.witness;
58
- }
59
- } return false;
60
- }
61
-
62
- static add_future_variable(variables:VariableType, identifier:number, type:OperatorType | ContextType,
63
- witness:any, value?:any, bNeedSerialize=true) : boolean {
64
- if (!Guard.IsValidIndentifier(identifier)) return false;
65
- if (!Guard.IsValidGuardVirableType(type)) return false;
66
- if (!witness && !value) return false;
67
-
68
- switch (type) {
69
- case OperatorType.TYPE_FUTURE_QUERY :
70
- case ContextType.TYPE_CONTEXT_FUTURE_ID :
71
- if (variables.has(identifier)) {
72
- let v = (variables.get(identifier) as Guard_Vriable);
73
- if (bNeedSerialize) {
74
- v.value = value ? BCS_CONVERT.ser_address(value) : undefined;
75
- v.witness = witness ? BCS_CONVERT.ser_address(witness) : undefined;
76
- } else {
77
- v.value = value ? value : undefined;
78
- v.witness = witness ? witness : undefined;
79
- }
80
- } else {
81
- if (bNeedSerialize) {
82
- variables.set(identifier, {type:type, value:value ? BCS_CONVERT.ser_address(value) : undefined, witness:witness ? BCS_CONVERT.ser_address(witness) : undefined})
83
- } else {
84
- variables.set(identifier, {type:type, value:value?value:undefined, witness:witness?witness:undefined});
85
- }
86
- }
87
- return true;
88
- }
89
- return false;
90
- }
91
-
92
- static add_variable(variables:VariableType, identifier:number, type:OperatorType | ContextType,
93
- value:any, bNeedSerialize=true) : boolean {
94
- if (!Guard.IsValidIndentifier(identifier)) return false;
95
- if (!Guard.IsValidGuardVirableType(type)) return false;
96
- if (!value) return false;
97
-
98
- switch (type) {
99
- case ContextType.TYPE_CONTEXT_bool:
100
- bNeedSerialize ? variables.set(identifier, {type:type, value:BCS_CONVERT.ser_bool(value)}) :
101
- variables.set(identifier, {type:type, value:value})
102
- return true;
103
- case ContextType.TYPE_CONTEXT_address:
104
- case OperatorType.TYPE_QUERY_FROM_CONTEXT:
105
- bNeedSerialize ? variables.set(identifier, {type:type, value:BCS_CONVERT.ser_address(value)}):
106
- variables.set(identifier, {type:type, value:value});
107
- return true;
108
- case ContextType.TYPE_CONTEXT_u64:
109
- bNeedSerialize ? variables.set(identifier, {type:type, value:BCS_CONVERT.ser_u64(value)}) :
110
- variables.set(identifier, {type:type, value:value})
111
- return true;
112
- case ContextType.TYPE_CONTEXT_u8:
113
- bNeedSerialize ? variables.set(identifier, {type:type, value:BCS_CONVERT.ser_u8(value)}) :
114
- variables.set(identifier, {type:type, value:value})
115
- return true;
116
- case ContextType.TYPE_CONTEXT_vec_u8:
117
- bNeedSerialize ? variables.set(identifier, {type:type, value:BCS_CONVERT.ser_string(value)}) :
118
- variables.set(identifier, {type:type, value:value})
119
- return true;
120
- }
121
- return false;
122
- }
23
+ static MAX_INPUT_LENGTH = 2048;
123
24
  static launch(protocol:Protocol, creation:Guard_Creation) : GuardAddress {
124
25
  if (!IsValidDesription(creation.description)) {
125
26
  ERROR(Errors.IsValidDesription)
126
27
  }
127
- if (creation.senses.length == 0 || creation.senses.length > Guard.MAX_SENSE_COUNT) {
128
- ERROR(Errors.InvalidParam, 'creation.senses')
28
+ if (creation.input.length == 0 || creation.input.length > Guard.MAX_INPUT_LENGTH) {
29
+ ERROR(Errors.InvalidParam, 'creation.input')
129
30
  }
130
31
 
131
32
  let bValid = true;
132
- creation.senses.forEach((v) => {
133
- if (!v.input || v.input.length == 0) bValid = false;
134
- })
135
- if (!bValid) {
136
- ERROR(Errors.InvalidParam, 'creation.senses')
137
- }
138
33
  creation?.variables?.forEach((v, k) => {
139
- if (!Guard.IsValidIndentifier(k)) bValid = false;
140
- if (!Guard.IsValidGuardVirableType(v.type)) bValid = false;
34
+ if (!GuardVariableMaker.IsValidIndentifier(k)) bValid = false;
35
+ if (!GuardVariableMaker.IsValidGuardVirableType(v.type)) bValid = false;
141
36
  if (!v.value && !v.witness) bValid = false;
142
37
  })
143
38
  if (!bValid) {
@@ -145,19 +40,14 @@ export class Guard {
145
40
  }
146
41
 
147
42
  let txb = protocol.CurrentSession();
43
+ let input = new Uint8Array(creation.input); // copy new uint8array to reserve!
44
+
45
+ // reserve the bytes for guard
148
46
  let guard = txb.moveCall({
149
47
  target: protocol.GuardFn('new') as FnCallType,
150
- arguments: [txb.pure(creation.description , BCS.STRING)],
151
- });
152
- creation.senses.forEach((sense: Guard_Sense) => {
153
- txb.moveCall({
154
- target: protocol.GuardFn('sense_add') as FnCallType,
155
- arguments:[guard, txb.pure([].slice.call(sense.input)),
156
- txb.pure(sense.notAfterSense, BCS.BOOL),
157
- txb.pure(sense.binder, BCS.U8),
158
- ]
159
- })
48
+ arguments: [txb.pure(creation.description , BCS.STRING), txb.pure([].slice.call(input.reverse()))],
160
49
  });
50
+
161
51
  creation?.variables?.forEach((v, k) => {
162
52
  if (v.type == OperatorType.TYPE_FUTURE_QUERY || v.type == ContextType.TYPE_CONTEXT_FUTURE_ID) {
163
53
  if (!v.witness) {
@@ -222,7 +112,9 @@ export class Guard {
222
112
  [MODULES.repository, 'value', 9, [ValueType.TYPE_STATIC_address, ValueType.TYPE_STATIC_vec_u8], ValueType.TYPE_STATIC_vec_u8],
223
113
  [MODULES.repository, 'type', 10, [], ValueType.TYPE_STATIC_u8],
224
114
  [MODULES.repository, 'policy_mode', 11, [], ValueType.TYPE_STATIC_u8],
225
-
115
+ [MODULES.repository, 'reference_count', 12, [], ValueType.TYPE_STATIC_u64],
116
+ [MODULES.repository, 'has_reference', 13, [ValueType.TYPE_STATIC_address], ValueType.TYPE_STATIC_bool],
117
+
226
118
  [MODULES.machine, 'permission', 1, [], ValueType.TYPE_STATIC_address],
227
119
  [MODULES.machine, 'has_paused', 2, [], ValueType.TYPE_STATIC_bool],
228
120
  [MODULES.machine, 'has_published', 3, [], ValueType.TYPE_STATIC_bool],
@@ -350,51 +242,143 @@ export const graphql_query_objects = (protocol: Protocol, nodes:any) : Guard_Que
350
242
  return ret
351
243
  } */
352
244
 
353
- export class SenseMaker {
245
+ export class GuardVariableMaker {
246
+ static IsValidGuardVirableType = (type:OperatorType | ContextType) : boolean => {
247
+ if (type == OperatorType.TYPE_FUTURE_QUERY || type == ContextType.TYPE_CONTEXT_FUTURE_ID || type == OperatorType.TYPE_QUERY_FROM_CONTEXT ||
248
+ type == ContextType.TYPE_CONTEXT_bool || type ==ContextType.TYPE_CONTEXT_address || type == ContextType.TYPE_CONTEXT_u64 ||
249
+ type == ContextType.TYPE_CONTEXT_u8 || type == ContextType.TYPE_CONTEXT_vec_u8) {
250
+ return true;
251
+ };
252
+ return false;
253
+ }
254
+ static IsValidIndentifier = (identifier:number) : boolean => {
255
+ if (!IsValidInt(identifier) || identifier > 255) return false;
256
+ return true
257
+ }
258
+ static get_variable_value(variables:VariableType, identifier:number, type:OperatorType | ContextType) : Uint8Array | boolean {
259
+ if (variables.has(identifier)) {
260
+ let v = variables.get(identifier);
261
+ if (v?.value && v.type == type) {
262
+ return v.value;
263
+ }
264
+ } return false;
265
+ }
266
+ static get_variable_witness(variables:VariableType, identifier:number, type:OperatorType | ContextType) : Uint8Array | boolean {
267
+ if (variables.has(identifier)) {
268
+ let v = variables.get(identifier);
269
+ if (v?.witness && v.type == type) {
270
+ return v.witness;
271
+ }
272
+ } return false;
273
+ }
274
+
275
+ static add_future_variable(variables:VariableType, identifier:number, type:OperatorType | ContextType,
276
+ witness:any, value?:any, bNeedSerialize=true) : boolean {
277
+ if (!GuardVariableMaker.IsValidIndentifier(identifier)) return false;
278
+ if (!GuardVariableMaker.IsValidGuardVirableType(type)) return false;
279
+ if (!witness && !value) return false;
280
+
281
+ switch (type) {
282
+ case OperatorType.TYPE_FUTURE_QUERY :
283
+ case ContextType.TYPE_CONTEXT_FUTURE_ID :
284
+ if (variables.has(identifier)) {
285
+ let v = (variables.get(identifier) as Guard_Vriable);
286
+ if (bNeedSerialize) {
287
+ v.value = value ? Bcs.getInstance().ser_address(value) : undefined;
288
+ v.witness = witness ? Bcs.getInstance().ser_address(witness) : undefined;
289
+ } else {
290
+ v.value = value ? value : undefined;
291
+ v.witness = witness ? witness : undefined;
292
+ }
293
+ } else {
294
+ if (bNeedSerialize) {
295
+ variables.set(identifier, {type:type, value:value ? Bcs.getInstance().ser_address(value) : undefined, witness:witness ? Bcs.getInstance().ser_address(witness) : undefined})
296
+ } else {
297
+ variables.set(identifier, {type:type, value:value?value:undefined, witness:witness?witness:undefined});
298
+ }
299
+ }
300
+ return true;
301
+ }
302
+ return false;
303
+ }
304
+
305
+ static add_variable(variables:VariableType, identifier:number, type:OperatorType | ContextType,
306
+ value:any, bNeedSerialize=true) : boolean {
307
+ if (!GuardVariableMaker.IsValidIndentifier(identifier)) return false;
308
+ if (!GuardVariableMaker.IsValidGuardVirableType(type)) return false;
309
+ if (!value) return false;
310
+
311
+ switch (type) {
312
+ case ContextType.TYPE_CONTEXT_bool:
313
+ bNeedSerialize ? variables.set(identifier, {type:type, value:Bcs.getInstance().ser_bool(value)}) :
314
+ variables.set(identifier, {type:type, value:value})
315
+ return true;
316
+ case ContextType.TYPE_CONTEXT_address:
317
+ case OperatorType.TYPE_QUERY_FROM_CONTEXT:
318
+ bNeedSerialize ? variables.set(identifier, {type:type, value:Bcs.getInstance().ser_address(value)}):
319
+ variables.set(identifier, {type:type, value:value});
320
+ return true;
321
+ case ContextType.TYPE_CONTEXT_u64:
322
+ bNeedSerialize ? variables.set(identifier, {type:type, value:Bcs.getInstance().ser_u64(value)}) :
323
+ variables.set(identifier, {type:type, value:value})
324
+ return true;
325
+ case ContextType.TYPE_CONTEXT_u8:
326
+ bNeedSerialize ? variables.set(identifier, {type:type, value:Bcs.getInstance().ser_u8(value)}) :
327
+ variables.set(identifier, {type:type, value:value})
328
+ return true;
329
+ case ContextType.TYPE_CONTEXT_vec_u8:
330
+ bNeedSerialize ? variables.set(identifier, {type:type, value:Bcs.getInstance().ser_string(value)}) :
331
+ variables.set(identifier, {type:type, value:value})
332
+ return true;
333
+ }
334
+ return false;
335
+ }
336
+ }
337
+ export class GuardInputMaker {
354
338
  protected data : Uint8Array[] = [];
355
339
  protected type_validator : Data_Type[] = [];
356
340
  constructor() { }
357
341
 
358
342
  // serialize const & data
359
- add_param(type:ValueType | ContextType, param?:any, variable?:VariableType) : boolean {
343
+ add_param(type:ValueType | ContextType, param?:any, variable?:VariableType) {
360
344
  switch(type) {
361
345
  case ValueType.TYPE_STATIC_address:
362
- if (!param) return false
363
- this.data.push(BCS_CONVERT.ser_u8(type));
364
- this.data.push(BCS_CONVERT.ser_address(param));
346
+ if (!param) ERROR(Errors.InvalidParam, 'param');
347
+ this.data.push(Bcs.getInstance().ser_u8(type));
348
+ this.data.push(Bcs.getInstance().ser_address(param));
365
349
  this.type_validator.push(type);
366
350
  break;
367
351
  case ValueType.TYPE_STATIC_bool:
368
- if (!param) return false
369
- this.data.push(BCS_CONVERT.ser_u8(type));
370
- this.data.push(BCS_CONVERT.ser_bool(param));
352
+ if (!param) ERROR(Errors.InvalidParam, 'param');
353
+ this.data.push(Bcs.getInstance().ser_u8(type));
354
+ this.data.push(Bcs.getInstance().ser_bool(param));
371
355
  this.type_validator.push(type);
372
356
  break;
373
357
  case ValueType.TYPE_STATIC_u8:
374
- if (!param) return false
375
- this.data.push(BCS_CONVERT.ser_u8(type));
376
- this.data.push(BCS_CONVERT.ser_u8(param));
358
+ if (!param) ERROR(Errors.InvalidParam, 'param');
359
+ this.data.push(Bcs.getInstance().ser_u8(type));
360
+ this.data.push(Bcs.getInstance().ser_u8(param));
377
361
  this.type_validator.push(type);
378
362
  break;
379
363
  case ValueType.TYPE_STATIC_u64:
380
- if (!param) return false
381
- this.data.push(BCS_CONVERT.ser_u8(type));
382
- this.data.push(BCS_CONVERT.ser_u64(param));
364
+ if (!param) ERROR(Errors.InvalidParam, 'param');
365
+ this.data.push(Bcs.getInstance().ser_u8(type));
366
+ this.data.push(Bcs.getInstance().ser_u64(param));
383
367
  this.type_validator.push(type);
384
368
  break;
385
369
  case ValueType.TYPE_STATIC_vec_u8:
386
- if (!param) return false
387
- this.data.push(BCS_CONVERT.ser_u8(type));
388
- this.data.push(BCS_CONVERT.ser_string(param));
370
+ if (!param) ERROR(Errors.InvalidParam, 'param');
371
+ this.data.push(Bcs.getInstance().ser_u8(type));
372
+ this.data.push(Bcs.getInstance().ser_string(param));
389
373
  this.type_validator.push(type);
390
374
  // this.data[this.data.length-1].forEach((item : number) => console.log(item))
391
375
  break;
392
376
  case ContextType.TYPE_CONTEXT_SIGNER:
393
- this.data.push(BCS_CONVERT.ser_u8(type));
377
+ this.data.push(Bcs.getInstance().ser_u8(type));
394
378
  this.type_validator.push(ValueType.TYPE_STATIC_address);
395
379
  break;
396
380
  case ContextType.TYPE_CONTEXT_CLOCK:
397
- this.data.push(BCS_CONVERT.ser_u8(type));
381
+ this.data.push(Bcs.getInstance().ser_u8(type));
398
382
  this.type_validator.push(ValueType.TYPE_STATIC_u64);
399
383
  break;
400
384
  case ContextType.TYPE_CONTEXT_bool:
@@ -403,14 +387,17 @@ export class SenseMaker {
403
387
  case ContextType.TYPE_CONTEXT_vec_u8:
404
388
  case ContextType.TYPE_CONTEXT_address:
405
389
  case ContextType.TYPE_CONTEXT_FUTURE_ID:
406
- if (!variable || !param) return false;
407
- if (typeof(param) != 'number') return false;
408
- if (!IsValidInt(param) || param > 255) return false;
390
+ if (!variable || !param) {
391
+ ERROR(Errors.InvalidParam, 'variable or param');
392
+ }
393
+ if (typeof(param) != 'number' || !IsValidInt(param) || param > 255) {
394
+ ERROR(Errors.InvalidParam, 'param');
395
+ }
409
396
 
410
- var v = variable.get(param);
397
+ var v = variable!.get(param);
411
398
  if (v?.type == type) {
412
- this.data.push(BCS_CONVERT.ser_u8(type));
413
- this.data.push(BCS_CONVERT.ser_u8(param));
399
+ this.data.push(Bcs.getInstance().ser_u8(type));
400
+ this.data.push(Bcs.getInstance().ser_u8(param));
414
401
  if (type == ContextType.TYPE_CONTEXT_bool) {
415
402
  this.type_validator.push(ValueType.TYPE_STATIC_bool);
416
403
  } else if (type == ContextType.TYPE_CONTEXT_u8) {
@@ -426,12 +413,10 @@ export class SenseMaker {
426
413
  }
427
414
  break;
428
415
  };
429
- return false;
416
+ ERROR(Errors.InvalidParam, 'variable');
430
417
  default:
431
- return false;
418
+ ERROR(Errors.InvalidParam, 'type');
432
419
  };
433
-
434
- return true;
435
420
  }
436
421
  static query_index(module:MODULES, query_name:string) : number {
437
422
  for (let i = 0; i < Guard.QUERIES.length; i++) {
@@ -441,103 +426,134 @@ export class SenseMaker {
441
426
  }
442
427
  return -1;
443
428
  }
444
- add_future_query(identifier:number, module:MODULES, query_name:string, variable:VariableType) : boolean {
445
- let query_index = SenseMaker.query_index(module, query_name);
446
- if (!Guard.IsValidIndentifier(identifier) || query_index == -1) return false;
447
- if (module != MODULES.order && module != MODULES.progress) return false;
448
- if (!variable || variable.get(identifier)?.type != OperatorType.TYPE_FUTURE_QUERY) return false;
429
+ add_future_query(identifier:number, module:MODULES, query_name:string, variable:VariableType) {
430
+ let query_index = GuardInputMaker.query_index(module, query_name);
431
+ if (!GuardVariableMaker.IsValidIndentifier(identifier) || query_index == -1) {
432
+ ERROR(Errors.InvalidParam, 'identifier or query_name');
433
+ }
434
+ if (module != MODULES.order && module != MODULES.progress){
435
+ ERROR(Errors.InvalidParam, 'module');
436
+ }
437
+ if (!variable || variable.get(identifier)?.type != OperatorType.TYPE_FUTURE_QUERY) {
438
+ ERROR(Errors.InvalidParam, 'variable');
439
+ }
449
440
 
450
441
  let offset = this.type_validator.length - Guard.QUERIES[query_index][3].length;
451
442
  if (offset < 0) {
452
- return false;
443
+ ERROR(Errors.InvalidParam, 'query_name');
453
444
  }
454
445
 
455
446
  let types = this.type_validator.slice(offset);
456
447
  if (!array_equal(types, Guard.QUERIES[query_index][3])) { // type validate
457
- return false;
448
+ ERROR(Errors.Fail, 'array_equal');
458
449
  }
459
450
 
460
- this.data.push(BCS_CONVERT.ser_u8(OperatorType.TYPE_FUTURE_QUERY)); // TYPE
461
- this.data.push(BCS_CONVERT.ser_u8(identifier)); // variable identifier
462
- this.data.push(BCS_CONVERT.ser_u8(Guard.QUERIES[query_index][2])); // cmd
451
+ this.data.push(Bcs.getInstance().ser_u8(OperatorType.TYPE_FUTURE_QUERY)); // TYPE
452
+ this.data.push(Bcs.getInstance().ser_u8(identifier)); // variable identifier
453
+ this.data.push(Bcs.getInstance().ser_u8(Guard.QUERIES[query_index][2])); // cmd
463
454
 
464
455
  this.type_validator.splice(offset, Guard.QUERIES[query_index][3].length); // delete type stack
465
456
  this.type_validator.push(Guard.QUERIES[query_index][4]); // add the return value type to type stack
466
457
  // console.log(this.type_validator)
467
- return true;
468
458
  }
469
459
 
470
460
  // object_address_from: string for static address; number as identifier for variable
471
- add_query(module:MODULES, query_name:string, object_address_from:string | number) : boolean {
472
- let query_index = SenseMaker.query_index(module, query_name); // query_index: index(from 0) of array Guard.QUERIES
473
- if (query_index == -1) return false;
461
+ add_query(module:MODULES, query_name:string, object_address_from:string | number) {
462
+ let query_index = GuardInputMaker.query_index(module, query_name); // query_index: index(from 0) of array Guard.QUERIES
463
+ if (query_index == -1) {
464
+ ERROR(Errors.InvalidParam, 'query_name');
465
+ }
474
466
  if (typeof(object_address_from) == 'number') {
475
- if (!Guard.IsValidIndentifier(object_address_from)) return false;
467
+ if (!GuardVariableMaker.IsValidIndentifier(object_address_from)) {
468
+ ERROR(Errors.InvalidParam, 'object_address_from');
469
+ }
476
470
  } else {
477
- if (!IsValidAddress(object_address_from)) return false;
471
+ if (!IsValidAddress(object_address_from)) {
472
+ ERROR(Errors.InvalidParam, 'object_address_from');
473
+ }
478
474
  }
479
475
 
480
476
  let offset = this.type_validator.length - Guard.QUERIES[query_index][3].length;
481
477
  if (offset < 0) {
482
- return false;
478
+ ERROR(Errors.InvalidParam, 'query_name');
483
479
  }
484
480
 
485
481
  let types = this.type_validator.slice(offset);
486
482
  if (!array_equal(types, Guard.QUERIES[query_index][3])) { // type validate
487
- return false;
483
+ ERROR(Errors.Fail, 'array_equal');
488
484
  }
489
485
 
490
486
  if (typeof(object_address_from) == 'string') {
491
- this.data.push(BCS_CONVERT.ser_u8(OperatorType.TYPE_QUERY));// TYPE
492
- this.data.push(BCS_CONVERT.ser_address(object_address_from)); // object address
487
+ this.data.push(Bcs.getInstance().ser_u8(OperatorType.TYPE_QUERY));// TYPE
488
+ this.data.push(Bcs.getInstance().ser_address(object_address_from)); // object address
493
489
  } else {
494
- this.data.push(BCS_CONVERT.ser_u8(OperatorType.TYPE_QUERY_FROM_CONTEXT));// TYPE
495
- this.data.push(BCS_CONVERT.ser_u8(object_address_from)); // object identifer in variables
490
+ this.data.push(Bcs.getInstance().ser_u8(OperatorType.TYPE_QUERY_FROM_CONTEXT));// TYPE
491
+ this.data.push(Bcs.getInstance().ser_u8(object_address_from)); // object identifer in variables
496
492
  }
497
493
 
498
- this.data.push(BCS_CONVERT.ser_u8(Guard.QUERIES[query_index][2])); // cmd
494
+ this.data.push(Bcs.getInstance().ser_u8(Guard.QUERIES[query_index][2])); // cmd
499
495
 
500
496
  this.type_validator.splice(offset, Guard.QUERIES[query_index][3].length); // delete type stack
501
497
  this.type_validator.push(Guard.QUERIES[query_index][4]); // add the return value type to type stack
502
498
  // console.log(this.type_validator)
503
- return true;
504
499
  }
505
500
 
506
- add_logic(type:OperatorType) : boolean {
501
+ add_logic(type:OperatorType) {
502
+ let splice_len = 2;
507
503
  switch (type) {
508
504
  case OperatorType.TYPE_LOGIC_OPERATOR_U128_GREATER:
509
505
  case OperatorType.TYPE_LOGIC_OPERATOR_U128_GREATER_EQUAL:
510
506
  case OperatorType.TYPE_LOGIC_OPERATOR_U128_LESSER:
511
507
  case OperatorType.TYPE_LOGIC_OPERATOR_U128_LESSER_EQUAL:
512
508
  case OperatorType.TYPE_LOGIC_OPERATOR_U128_EQUAL:
513
- if (this.type_validator.length < 2) { return false; }
514
- if (!SenseMaker.match_u128(this.type_validator[this.type_validator.length - 1])) { return false; }
515
- if (!SenseMaker.match_u128(this.type_validator[this.type_validator.length - 2])) { return false; }
509
+ if (this.type_validator.length < splice_len) { ERROR(Errors.Fail, 'type_validator.length') }
510
+ if (!GuardInputMaker.match_u128(this.type_validator[this.type_validator.length - 1])) { ERROR(Errors.Fail, 'type_validator check') }
511
+ if (!GuardInputMaker.match_u128(this.type_validator[this.type_validator.length - 2])) { ERROR(Errors.Fail, 'type_validator check') }
516
512
  break;
517
513
  case OperatorType.TYPE_LOGIC_OPERATOR_EQUAL:
514
+ if (this.type_validator.length < splice_len) { ERROR(Errors.Fail, 'type_validator.length') }
515
+ break;
518
516
  case OperatorType.TYPE_LOGIC_OPERATOR_HAS_SUBSTRING:
519
- if (this.type_validator.length < 2) { return false; }
517
+ if (this.type_validator.length < splice_len) { ERROR(Errors.Fail, 'type_validator.length') }
518
+ break;
519
+ case OperatorType.TYPE_LOGIC_NOT:
520
+ splice_len = 1;
521
+ if (this.type_validator.length < splice_len) { ERROR(Errors.Fail, 'type_validator.length') }
522
+ if (this.type_validator[this.type_validator.length -1] != ValueType.TYPE_STATIC_bool) { ERROR(Errors.Fail, 'type_validator check') }
523
+ break;
524
+ case OperatorType.TYPE_LOGIC_AND:
525
+ case OperatorType.TYPE_LOGIC_OR:
526
+ if (this.type_validator.length < splice_len) { ERROR(Errors.Fail, 'type_validator.length') }
527
+ if (this.type_validator[this.type_validator.length -1] != ValueType.TYPE_STATIC_bool) { ERROR(Errors.Fail, 'type_validator check') }
528
+ if (this.type_validator[this.type_validator.length -2] != ValueType.TYPE_STATIC_bool) { ERROR(Errors.Fail, 'type_validator check') }
520
529
  break;
521
530
  default:
522
- return false;
531
+ ERROR(Errors.InvalidParam, 'type')
523
532
  }
524
- this.data.push(BCS_CONVERT.ser_u8(type)); // TYPE
525
- this.type_validator.splice(this.type_validator.length - 2); // delete type stack
533
+ this.data.push(Bcs.getInstance().ser_u8(type)); // TYPE
534
+ this.type_validator.splice(this.type_validator.length - splice_len); // delete type stack
526
535
  this.type_validator.push(ValueType.TYPE_STATIC_bool); // add bool to type stack
527
- return true;
528
536
  }
529
537
 
530
- make(bNotAfterSense:boolean = false, binder:Guard_Sense_Binder = Guard_Sense_Binder.AND) : boolean | Guard_Sense {
538
+ make(bNot = false) : Uint8Array {
531
539
  //console.log(this.type_validator);
532
540
  //this.data.forEach((value:Uint8Array) => console.log(value));
533
541
  if (this.type_validator.length != 1 || this.type_validator[0] != ValueType.TYPE_STATIC_bool) {
534
542
  console.log(this.type_validator)
535
- return false;
543
+ ERROR(Errors.Fail, 'type_validator check')
536
544
  } // ERROR
545
+ if (bNot) {
546
+ this.add_logic(OperatorType.TYPE_LOGIC_NOT);
547
+ }
548
+ return concatenate(Uint8Array, ...this.data) as Uint8Array;
549
+ }
537
550
 
538
- let input = concatenate(Uint8Array, ...this.data) as Uint8Array;
539
- const sense:Guard_Sense = {input:input, notAfterSense:bNotAfterSense, binder:binder};
540
- return sense;
551
+ static combine(input1:Uint8Array, input2:Uint8Array, bAnd:boolean = true) : Uint8Array {
552
+ let op = bAnd ? OperatorType.TYPE_LOGIC_AND : OperatorType.TYPE_LOGIC_OR;
553
+ return concatenate(Uint8Array, input1, input2, Bcs.getInstance().ser_u8(op)) as Uint8Array;
554
+ }
555
+ static not(input:Uint8Array) : Uint8Array {
556
+ return concatenate(Uint8Array, input, Bcs.getInstance().ser_u8(OperatorType.TYPE_LOGIC_NOT)) as Uint8Array;
541
557
  }
542
558
 
543
559
  static match_u128(type:number) : boolean {
package/src/index.ts CHANGED
@@ -10,3 +10,4 @@ export * from './protocol'
10
10
  export * from './passport'
11
11
  export * from './machine'
12
12
  export * from './service'
13
+ export * from './graphql'
package/src/machine.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { TransactionBlock, Inputs, type TransactionResult } from '@mysten/sui.js/transactions';
2
2
  import { BCS } from '@mysten/bcs';
3
3
  import { Protocol, FnCallType, PermissionObject, RepositoryObject, PassportObject, MachineObject, MachineAddress, GuardObject, TxbObject} from './protocol';
4
- import { IsValidInt, IsValidUint, BCS_CONVERT, array_unique, IsValidArray, IsValidAddress, IsValidName, IsValidName_AllowEmpty,
4
+ import { IsValidInt, IsValidUint, Bcs, array_unique, IsValidArray, IsValidAddress, IsValidName, IsValidName_AllowEmpty,
5
5
  IsValidEndpoint, OptionNone, IsValidDesription} from './utils'
6
6
  import { Permission, PermissionIndexType } from './permission';
7
7
  import { Errors, ERROR} from './exception'
@@ -9,19 +9,19 @@ import { Errors, ERROR} from './exception'
9
9
 
10
10
  export type MachineNodeObject = TransactionResult | String;
11
11
 
12
- export type Machine_Forward = {
12
+ export interface Machine_Forward {
13
13
  name: string; // foward name
14
14
  namedOperator?: string; // dynamic operator
15
15
  permission?: PermissionIndexType; // this.permission-index or named-operator MUST one defined.
16
16
  weight?: number;
17
17
  guard?: GuardObject;
18
18
  }
19
- export type Machine_Node_Pair = {
19
+ export interface Machine_Node_Pair {
20
20
  prior_node: string;
21
21
  forwards: Machine_Forward[];
22
22
  threshold?: number;
23
23
  }
24
- export type Machine_Node = {
24
+ export interface Machine_Node {
25
25
  name: string;
26
26
  description: string;
27
27
  pairs: Machine_Node_Pair[];
@@ -58,7 +58,7 @@ export class Machine {
58
58
  ERROR(Errors.IsValidEndpoint)
59
59
  }
60
60
  let txb = protocol.CurrentSession();
61
- let ep = endpoint? txb.pure(BCS_CONVERT.ser_option_string(endpoint)) : OptionNone(txb);
61
+ let ep = endpoint? txb.pure(Bcs.getInstance().ser_option_string(endpoint)) : OptionNone(txb);
62
62
 
63
63
  if (passport) {
64
64
  m.object = txb.moveCall({
@@ -103,11 +103,11 @@ export class Machine {
103
103
  arguments:[txb.pure(node.name), txb.pure(node.description)]
104
104
  });
105
105
  node.pairs.forEach((pair) => {
106
- let threshold = pair?.threshold ? txb.pure(BCS_CONVERT.ser_option_u64(pair.threshold)) : OptionNone(txb);
106
+ let threshold = pair?.threshold ? txb.pure(Bcs.getInstance().ser_option_u64(pair.threshold)) : OptionNone(txb);
107
107
 
108
108
  pair.forwards.forEach((forward) => {
109
109
  let weight = forward?.weight ? forward.weight : 1;
110
- let perm = forward?.permission ? txb.pure(BCS_CONVERT.ser_option_u64(forward.permission)) : OptionNone(txb);
110
+ let perm = forward?.permission ? txb.pure(Bcs.getInstance().ser_option_u64(forward.permission)) : OptionNone(txb);
111
111
  let namedOperator = forward?.namedOperator ? txb.pure(forward.namedOperator) : txb.pure('');
112
112
  let f;
113
113
 
@@ -168,13 +168,13 @@ export class Machine {
168
168
  if (passport) {
169
169
  txb.moveCall({
170
170
  target:this.protocol.MachineFn('node_remove_with_passport') as FnCallType,
171
- arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(BCS_CONVERT.ser_vector_string(nodes_name)),
171
+ arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser_vector_string(nodes_name)),
172
172
  txb.pure(bTransferMyself, BCS.BOOL), Protocol.TXB_OBJECT(txb, this.permission)],
173
173
  });
174
174
  } else {
175
175
  txb.moveCall({
176
176
  target:this.protocol.MachineFn('node_remove') as FnCallType,
177
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(BCS_CONVERT.ser_vector_string(nodes_name)), txb.pure(bTransferMyself, BCS.BOOL), Protocol.TXB_OBJECT(txb, this.permission)],
177
+ arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser_vector_string(nodes_name)), txb.pure(bTransferMyself, BCS.BOOL), Protocol.TXB_OBJECT(txb, this.permission)],
178
178
  });
179
179
  }
180
180
  }
@@ -287,7 +287,7 @@ export class Machine {
287
287
  }
288
288
 
289
289
  let txb = this.protocol.CurrentSession();
290
- let ep = endpoint? txb.pure(BCS_CONVERT.ser_option_string(endpoint)) : OptionNone(txb);
290
+ let ep = endpoint? txb.pure(Bcs.getInstance().ser_option_string(endpoint)) : OptionNone(txb);
291
291
 
292
292
  if (passport) {
293
293
  txb.moveCall({