wowok 1.1.2 → 1.1.4

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/passport.ts CHANGED
@@ -1,11 +1,10 @@
1
- import { SuiObjectResponse, SuiObjectDataOptions } from '@mysten/sui.js/client';
2
1
  import { type TransactionObjectInput, Inputs } from '@mysten/sui.js/transactions';
3
- import { FnCallType, Query_Param, GuardObject, Protocol, ContextType, OperatorType, Data_Type,
4
- ValueType, MODULES } from './protocol';
5
- import { parse_object_type, array_unique, Bcs, ulebDecode, IsValidAddress, IsValidArray } from './utils';
6
- import { VariableType, Guard, Guard_Vriable, GuardVariableMaker } from './guard';
7
- import { BCS } from '@mysten/bcs';
2
+ import { FnCallType, GuardObject, Protocol, ContextType, OperatorType, Data_Type,
3
+ ValueType, SER_VALUE, IsValidOperatorType } from './protocol';
4
+ import { parse_object_type, array_unique, Bcs, ulebDecode, IsValidAddress, IsValidArray, OPTION_NONE, readOption } from './utils';
5
+ import { BCS, BcsReader } from '@mysten/bcs';
8
6
  import { ERROR, Errors } from './exception';
7
+ import { Guard } from './guard';
9
8
 
10
9
  export type Guard_Query_Object = {
11
10
  target: FnCallType, // object fnCall
@@ -14,51 +13,420 @@ export type Guard_Query_Object = {
14
13
  id: string, // object id
15
14
  }
16
15
 
17
- interface FutrueInfo {
18
- identifier: number;
19
- type: number;
20
- witness: string;
21
- futrue?: string;
16
+ interface QueryInfo {
17
+ identifier?: number;
18
+ index: number;
19
+ type: number;
20
+ value_or_witness: string;
21
+ future?: string;
22
22
  }
23
23
  interface GuardInfo {
24
24
  id: string;
25
- query_list: (string | {identifier:number, type:number, witness:string})[]; // object or future object query
26
- futrue_list: FutrueInfo[];
25
+ query_list: (string | QueryInfo)[]; // object or witness object query
26
+ variable: QueryInfo[]; // witness in variable & ValueType.TYPE_ADDRESS(for Query)
27
+ input_witness: QueryInfo[]; // witness in input
27
28
  }
29
+
30
+ interface DeGuardVariable {
31
+ type: number;
32
+ value: any;
33
+ identifier?: number;
34
+ }
35
+ interface DeGuardData {
36
+ type: number;
37
+ value?: any;
38
+ identifier?: number;
39
+ cmd?: number;
40
+ child: DeGuardData[];
41
+ ret_type?: number;
42
+ }
43
+
28
44
  interface FutrueFill {
29
45
  guard: string;
30
- identifier: number;
46
+ index: number;
31
47
  future: string;
32
48
  }
33
-
34
49
  interface PassportQuery {
50
+ guard: GuardObject[];
35
51
  query: Guard_Query_Object[];
36
52
  witness: Guard_Query_Object[];
37
53
  }
38
54
  export class GuardParser {
39
55
  protected guard_list: GuardInfo[] = [];
40
56
  protected protocol: Protocol;
41
- private constructor(protocol: Protocol) { this.protocol = protocol }
57
+ protected guards: GuardObject[];
58
+ private index:number = 0;
59
+ private get_index() { return this.index++ }
60
+
61
+ private constructor(protocol: Protocol, guards: string[]) {
62
+ this.protocol = protocol ;
63
+ this.guards = guards.map(g => protocol.CurrentSession().object(g) as GuardObject);
64
+ }
42
65
  guardlist = () => { return this.guard_list }
43
66
 
67
+ /// convert guard-on-chain to js object
68
+ static DeGuardObject = async (protocol: Protocol, guard: string) : Promise<DeGuardData> => {
69
+ if (!IsValidAddress(guard)) {
70
+ ERROR(Errors.IsValidAddress, 'GuardObject guard')
71
+ }
72
+
73
+ let res = await protocol.Query_Raw([guard]);
74
+ if (res.length == 0 || !res[0].data || res[0].data?.objectId != guard) {
75
+ ERROR(Errors.Fail, 'GuardObject query error');
76
+ }
77
+
78
+ // console.log(res[0].data?.content);
79
+ let content = res[0].data!.content as any;
80
+ if (content?.type != protocol.Package() + '::guard::Guard') {
81
+ ERROR(Errors.Fail, 'GuardObject object invalid')
82
+ }
83
+
84
+ let variables : DeGuardVariable[] = [];
85
+ content.fields.variables.forEach((v:any) => {
86
+ let value : any;
87
+ switch (v.fields.type) {
88
+ case ContextType.TYPE_WITNESS_ID:
89
+ case ValueType.TYPE_ADDRESS:
90
+ value = '0x' + Bcs.getInstance().de(BCS.ADDRESS, Uint8Array.from(v.fields.value)).toString();
91
+ break;
92
+ case ValueType.TYPE_BOOL:
93
+ case ValueType.TYPE_U8:
94
+ case ValueType.TYPE_U64:
95
+ case ValueType.TYPE_U128:
96
+ case ValueType.TYPE_U256:
97
+ case ValueType.TYPE_VEC_U8:
98
+ case ValueType.TYPE_VEC_U64:
99
+ case ValueType.TYPE_VEC_U128:
100
+ case ValueType.TYPE_VEC_ADDRESS:
101
+ case ValueType.TYPE_VEC_BOOL:
102
+ case ValueType.TYPE_VEC_VEC_U8:
103
+ case ValueType.TYPE_OPTION_ADDRESS:
104
+ case ValueType.TYPE_OPTION_BOOL:
105
+ case ValueType.TYPE_OPTION_U128:
106
+ case ValueType.TYPE_OPTION_U8:
107
+ case ValueType.TYPE_OPTION_U64:
108
+ case ValueType.TYPE_OPTION_U256:
109
+ case ValueType.TYPE_VEC_U256:
110
+ let de = SER_VALUE.find(s=>s.type==v.fields.type);
111
+ if (!de) ERROR(Errors.Fail, 'GuardObject de error')
112
+ value = Bcs.getInstance().de(de!.name, Uint8Array.from(v.fields.value));
113
+ break;
114
+
115
+ default:
116
+ ERROR(Errors.Fail, 'GuardObject variable type invalid')
117
+ }
118
+ variables.push({identifier:v.fields.identifier, type:v.fields.type, value:value});
119
+ });
120
+ // console.log(variables)
121
+
122
+ let arr = [].slice.call(content.fields.input.fields.bytes.reverse());
123
+ let data : DeGuardData[] = [];
124
+ while (arr.length > 0) {
125
+ let type : unknown = arr.shift() ;
126
+ let value:any; let cmd:any; let identifier:any;
127
+ switch (type as Data_Type) {
128
+ case ContextType.TYPE_SIGNER:
129
+ case ContextType.TYPE_CLOCK:
130
+ case OperatorType.TYPE_LOGIC_AS_U256_GREATER:
131
+ case OperatorType.TYPE_LOGIC_AS_U256_GREATER_EQUAL:
132
+ case OperatorType.TYPE_LOGIC_AS_U256_LESSER:
133
+ case OperatorType.TYPE_LOGIC_AS_U256_LESSER_EQUAL:
134
+ case OperatorType.TYPE_LOGIC_AS_U256_EQUAL:
135
+ case OperatorType.TYPE_LOGIC_EQUAL:
136
+ case OperatorType.TYPE_LOGIC_HAS_SUBSTRING:
137
+ case OperatorType.TYPE_LOGIC_ALWAYS_TRUE:
138
+ case OperatorType.TYPE_LOGIC_NOT:
139
+ case OperatorType.TYPE_LOGIC_AND:
140
+ case OperatorType.TYPE_LOGIC_OR:
141
+ break;
142
+ case ContextType.TYPE_VARIABLE:
143
+ identifier = arr.shift()! as number; // identifier
144
+ break;
145
+ case ContextType.TYPE_WITNESS_ID: // add to variable
146
+ case ValueType.TYPE_ADDRESS:
147
+ value = '0x' + Bcs.getInstance().de(BCS.ADDRESS, Uint8Array.from(arr)).toString();
148
+ arr.splice(0, 32); // address
149
+ break;
150
+ case ValueType.TYPE_BOOL:
151
+ value = Bcs.getInstance().de(BCS.BOOL, Uint8Array.from(arr)) as boolean;
152
+ arr.shift();
153
+ break;
154
+ case ValueType.TYPE_U8:
155
+ value = Bcs.getInstance().de(BCS.U8, Uint8Array.from(arr)) as number;
156
+ arr.shift();
157
+ break;
158
+ case ValueType.TYPE_U64:
159
+ value = Bcs.getInstance().de(BCS.U64, Uint8Array.from(arr)) as number;
160
+ arr.splice(0, 8);
161
+ break;
162
+ case ValueType.TYPE_U128:
163
+ value = Bcs.getInstance().de(BCS.U128, Uint8Array.from(arr)) as bigint;
164
+ arr.splice(0, 16);
165
+ break;
166
+ case ValueType.TYPE_U256:
167
+ value = Bcs.getInstance().de(BCS.U256, Uint8Array.from(arr)) as bigint;
168
+ arr.splice(0, 32);
169
+ break;
170
+ case ValueType.TYPE_VEC_U8:
171
+ case ValueType.TYPE_VEC_BOOL:
172
+ let r = ulebDecode(Uint8Array.from(arr));
173
+ value = Bcs.getInstance().de('vector<u8>', Uint8Array.from(arr));
174
+ arr.splice(0, r.value+r.length);
175
+ break;
176
+ case ValueType.TYPE_VEC_ADDRESS:
177
+ r = ulebDecode(Uint8Array.from(arr));
178
+ value = Bcs.getInstance().de('vector<address>', Uint8Array.from(arr));
179
+ arr.splice(0, r.value*32+r.length);
180
+ break;
181
+ case ValueType.TYPE_VEC_U128:
182
+ r = ulebDecode(Uint8Array.from(arr));
183
+ value = Bcs.getInstance().de('vector<u128>', Uint8Array.from(arr));
184
+ arr.splice(0, r.value*16+r.length);
185
+ break;
186
+ case ValueType.TYPE_VEC_U256:
187
+ r = ulebDecode(Uint8Array.from(arr));
188
+ value = Bcs.getInstance().de('vector<u256>', Uint8Array.from(arr));
189
+ arr.splice(0, r.value*32+r.length);
190
+ break;
191
+ case ValueType.TYPE_VEC_U64:
192
+ r = ulebDecode(Uint8Array.from(arr));
193
+ value = Bcs.getInstance().de('vector<u64>', Uint8Array.from(arr));
194
+ arr.splice(0, r.value*8+r.length);
195
+ break;
196
+ case ValueType.TYPE_VEC_VEC_U8:
197
+ r = ulebDecode(Uint8Array.from(arr)); arr.splice(0, r.length);
198
+ let res = [];
199
+ for (let i = 0; i < r.value; i++) {
200
+ let r2 = ulebDecode(Uint8Array.from(arr));
201
+ res.push(Bcs.getInstance().de('vector<u8>', Uint8Array.from(arr)));
202
+ arr.splice(0, r2.length+r2.value);
203
+ }
204
+ value = res;
205
+ break;
206
+ case OperatorType.TYPE_QUERY:
207
+ let t = arr.splice(0, 1); // data-type
208
+ if (t[0] == ValueType.TYPE_ADDRESS || t[0] == ContextType.TYPE_WITNESS_ID) {
209
+ let addr = '0x' + Bcs.getInstance().de(BCS.ADDRESS, Uint8Array.from(arr)).toString();
210
+ arr.splice(0, 32); // address
211
+ value = addr;
212
+ cmd = arr.shift()! as number; // cmd
213
+ } else if (t[0] == ContextType.TYPE_VARIABLE) {
214
+ let id = arr.splice(0, 1); // key
215
+ let v = variables.find((v) =>
216
+ (v.identifier == id[0]) &&
217
+ ((v.type == ValueType.TYPE_ADDRESS) || (v.type == ContextType.TYPE_WITNESS_ID)));
218
+ if (!v) { ERROR(Errors.Fail, 'GuardObject: indentifier not in variable')}
219
+ identifier = id[0];
220
+ cmd = arr.shift()! as number; // cmd
221
+ } else {
222
+ ERROR(Errors.Fail, 'GuardObject: variable type invalid');
223
+ }
224
+ break;
225
+ case ValueType.TYPE_OPTION_ADDRESS:
226
+ let read = readOption(arr, BCS.ADDRESS);
227
+ value = read.value;
228
+ if (!read.bNone) arr.splice(0, 32);
229
+ break;
230
+ case ValueType.TYPE_OPTION_BOOL:
231
+ read = readOption(arr, BCS.BOOL);
232
+ value = read.value;
233
+ if (!read.bNone) arr.splice(0, 1);
234
+ break;
235
+ case ValueType.TYPE_OPTION_U8:
236
+ read = readOption(arr, BCS.U8);
237
+ value = read.value;
238
+ if (!read.bNone) arr.splice(0, 1);
239
+ break;
240
+ case ValueType.TYPE_OPTION_U128:
241
+ read = readOption(arr, BCS.U128);
242
+ value = read.value;
243
+ if (!read.bNone) arr.splice(0, 16);
244
+ break;
245
+ case ValueType.TYPE_OPTION_U256:
246
+ read = readOption(arr, BCS.U256);
247
+ value = read.value;
248
+ if (!read.bNone) arr.splice(0, 32);
249
+ break;
250
+ case ValueType.TYPE_OPTION_U64:
251
+ read = readOption(arr, BCS.U64);
252
+ value = read.value;
253
+ if (!read.bNone) arr.splice(0, 8);
254
+ break;
255
+ default:
256
+ ERROR(Errors.Fail, 'GuardObject: parse_bcs types')
257
+ }
258
+ data.push({type:type as number, value:value, cmd:cmd, identifier:identifier, child:[]});
259
+ }
260
+
261
+ // console.log(data);
262
+ if (!data || data.length == 0) ERROR(Errors.Fail, 'GuardObject: data parsed error');
263
+ let stack: DeGuardData[] = [];
264
+ data.forEach((d) => {
265
+ this.ResolveData(variables, stack, d);
266
+ })
267
+
268
+ if (stack.length != 1) {
269
+ ERROR(Errors.Fail, 'GuardObject: parse error');
270
+ }
271
+
272
+ return stack.pop()!;
273
+ }
274
+
275
+ static ResolveData = (variables: DeGuardVariable[], stack:DeGuardData[], current: DeGuardData) => {
276
+ switch (current.type) {
277
+ case OperatorType.TYPE_LOGIC_ALWAYS_TRUE:
278
+ current.ret_type = ValueType.TYPE_BOOL;
279
+ return;
280
+ case OperatorType.TYPE_LOGIC_NOT:
281
+ current.ret_type = ValueType.TYPE_BOOL;
282
+ if (stack.length < 1) ERROR(Errors.Fail, 'ResolveData: TYPE_LOGIC_NOT');
283
+
284
+ let param = stack.pop() as DeGuardData;
285
+ if (!param.ret_type || param.ret_type != ValueType.TYPE_BOOL) {
286
+ ERROR(Errors.Fail, 'ResolveData: TYPE_LOGIC_NOT type invalid');
287
+ }
288
+
289
+ current.child.push(param);
290
+ stack.push(current);
291
+ return;
292
+ case OperatorType.TYPE_LOGIC_AS_U256_GREATER:
293
+ case OperatorType.TYPE_LOGIC_AS_U256_GREATER_EQUAL:
294
+ case OperatorType.TYPE_LOGIC_AS_U256_LESSER:
295
+ case OperatorType.TYPE_LOGIC_AS_U256_LESSER_EQUAL:
296
+ case OperatorType.TYPE_LOGIC_AS_U256_EQUAL:
297
+ current.ret_type = ValueType.TYPE_BOOL;
298
+ if (stack.length < 2) ERROR(Errors.Fail, 'ResolveData: ' + current.type);
299
+ for (let i = 0; i < 2; ++i) {
300
+ let p = stack.pop() as DeGuardData;
301
+ if (!p.ret_type) ERROR(Errors.Fail, 'ResolveData: ' + current.type + ' INVALID param type');
302
+ if (p.ret_type != ValueType.TYPE_U8 && p.ret_type != ValueType.TYPE_U64 &&
303
+ p.ret_type != ValueType.TYPE_U128 && p.ret_type != ValueType.TYPE_U256) {
304
+ ERROR(Errors.Fail, 'ResolveData: ' + current.type + ' INVALID param type');
305
+ };
306
+ current.child.push(p);
307
+ }
308
+ stack.push(current);
309
+ return;
310
+ case OperatorType.TYPE_LOGIC_EQUAL:
311
+ current.ret_type = ValueType.TYPE_BOOL;
312
+ if (stack.length < 2) ERROR(Errors.Fail, 'ResolveData: ' + current.type);
313
+ var p1 = stack.pop() as DeGuardData; var p2 = stack.pop() as DeGuardData;
314
+ if (!p1.ret_type || !p2.ret_type) ERROR(Errors.Fail, 'ResolveData: ' + current.type + ' INVALID param type');
315
+ if (p1.ret_type != p2.ret_type) ERROR(Errors.Fail, 'ResolveData: ' + current.type + ' param type not match');
316
+
317
+ current.child.push(p1);
318
+ current.child.push(p2);
319
+ stack.push(current);
320
+ return;
321
+ case OperatorType.TYPE_LOGIC_HAS_SUBSTRING:
322
+ current.ret_type = ValueType.TYPE_BOOL;
323
+ if (stack.length < 2) ERROR(Errors.Fail, 'ResolveData: ' + current.type);
324
+ var p1 = stack.pop() as DeGuardData; var p2 = stack.pop() as DeGuardData;
325
+ if (!p1.ret_type || !p2.ret_type) ERROR(Errors.Fail, 'ResolveData: ' + current.type + ' INVALID param type');
326
+ if (p1.ret_type != ValueType.TYPE_VEC_U8 || p2.ret_type != ValueType.TYPE_VEC_U8) {
327
+ ERROR(Errors.Fail, 'ResolveData: ' + current.type + ' param type not match');
328
+ }
329
+
330
+ current.child.push(p1);
331
+ current.child.push(p2);
332
+ stack.push(current);
333
+ return
334
+ case OperatorType.TYPE_LOGIC_AND:
335
+ case OperatorType.TYPE_LOGIC_OR:
336
+ current.ret_type = ValueType.TYPE_BOOL;
337
+ if (stack.length < 2) ERROR(Errors.Fail, 'ResolveData: ' + current.type);
338
+ var p1 = stack.pop() as DeGuardData; var p2 = stack.pop() as DeGuardData;
339
+ if (!p1.ret_type || !p2.ret_type) ERROR(Errors.Fail, 'ResolveData: ' + current.type + ' INVALID param type');
340
+ if (p1.ret_type != ValueType.TYPE_BOOL || p2.ret_type != ValueType.TYPE_BOOL) {
341
+ ERROR(Errors.Fail, 'ResolveData: ' + current.type + ' param type not match');
342
+ }
343
+
344
+ current.child.push(p1);
345
+ current.child.push(p2);
346
+ stack.push(current);
347
+ return
348
+ case OperatorType.TYPE_QUERY:
349
+ if (!current?.cmd) ERROR(Errors.Fail, 'OperateParamCount: cmd invalid ' + current.type);
350
+ let r = Guard.GetCmd(current.cmd!);
351
+ if (!r) ERROR(Errors.Fail, 'OperateParamCount: cmd not supported ' + current.type);
352
+ current.ret_type = r[4];
353
+
354
+ if (stack.length < r[3].length) ERROR(Errors.Fail, 'OperateParamCount: cmd param lost ' + current.type);
355
+ r[3].forEach((e:number) => {
356
+ let d = stack.pop() as DeGuardData;
357
+ if (!d?.ret_type || d.ret_type != e) {
358
+ ERROR(Errors.Fail, 'OperateParamCount: cmd param not match ' + current.type);
359
+ }
360
+ current.child.push(d)
361
+ });
362
+
363
+ stack.push(current);
364
+ return
365
+ case ValueType.TYPE_ADDRESS:
366
+ case ValueType.TYPE_BOOL:
367
+ case ValueType.TYPE_U128:
368
+ case ValueType.TYPE_U256:
369
+ case ValueType.TYPE_U64:
370
+ case ValueType.TYPE_U8:
371
+ case ValueType.TYPE_VEC_ADDRESS:
372
+ case ValueType.TYPE_VEC_BOOL:
373
+ case ValueType.TYPE_VEC_U128:
374
+ case ValueType.TYPE_VEC_U256:
375
+ case ValueType.TYPE_VEC_U64:
376
+ case ValueType.TYPE_VEC_U8:
377
+ case ValueType.TYPE_VEC_VEC_U8:
378
+ case ValueType.TYPE_OPTION_ADDRESS:
379
+ case ValueType.TYPE_OPTION_BOOL:
380
+ case ValueType.TYPE_OPTION_U128:
381
+ case ValueType.TYPE_OPTION_U256:
382
+ case ValueType.TYPE_OPTION_U64:
383
+ case ValueType.TYPE_OPTION_U8:
384
+ current.ret_type = current.type;
385
+ stack.push(current);
386
+ return;
387
+ case ContextType.TYPE_CLOCK:
388
+ current.ret_type = ValueType.TYPE_U64;
389
+ stack.push(current);
390
+ return;
391
+ case ContextType.TYPE_SIGNER:
392
+ case ContextType.TYPE_WITNESS_ID: /// notice!! convert witness type to address type
393
+ current.ret_type = ValueType.TYPE_ADDRESS;
394
+ stack.push(current);
395
+ return;
396
+ case ContextType.TYPE_VARIABLE:
397
+ let v = variables.find((e) => e.identifier == current?.identifier);
398
+ if (!v) ERROR(Errors.Fail, 'OperateParamCount: identifier invalid ' + current.type);
399
+
400
+ current.ret_type = v?.type;
401
+ if (v?.type == ContextType.TYPE_WITNESS_ID) {
402
+ current.ret_type = ValueType.TYPE_ADDRESS;
403
+ }
404
+
405
+ stack.push(current);
406
+ return;
407
+ }
408
+ ERROR(Errors.Fail, 'OperateParamCount: type invalid ' + current.type);
409
+ }
410
+
411
+ /// create GuardParser ayncly
44
412
  static CreateAsync = async (protocol: Protocol, guards: string[]) => {
45
413
  if (!IsValidArray(guards, IsValidAddress)) {
46
414
  ERROR(Errors.IsValidArray, 'guards');
47
415
  }
48
416
 
49
417
  let guard_list = array_unique(guards);
50
- const me = new GuardParser(protocol);
418
+ const me = new GuardParser(protocol, guards);
51
419
 
52
- let res = await protocol.Query_Raw(guards);
420
+ let res = await protocol.Query_Raw(guard_list);
53
421
  res.forEach((r) => {
54
422
  let c = r.data?.content as any;
55
423
  if (!c) return;
56
424
 
57
- let index = protocol.WOWOK_OBJECTS_TYPE().findIndex(v => v.includes('guard::Guard') && v == c.type);
425
+ let index = protocol.WOWOK_OBJECTS_TYPE().findIndex(v => {return v.includes('guard::Guard') && v == c.type});
58
426
  if (index == -1) return;
59
427
 
60
- let info:GuardInfo = {id: c.fields.id.id, query_list:[], futrue_list:[]};
61
- me.parse_future(info, c.fields.variables);
428
+ let info:GuardInfo = {id: c.fields.id.id, query_list:[], variable:[], input_witness:[]};
429
+ me.parse_variable(info, c.fields.variables);
62
430
  if (c.fields.input.type == (protocol.Package() + '::bcs::BCS')) {
63
431
  me.parse_bcs(info, Uint8Array.from(c.fields.input.fields.bytes));
64
432
  }
@@ -67,75 +435,91 @@ export class GuardParser {
67
435
  return me
68
436
  }
69
437
 
70
- private parse_future = (info:GuardInfo, variables:any) => {
438
+ parse_variable = (info:GuardInfo, variables:any) => {
71
439
  variables.forEach((v:any) => {
72
440
  if (v.type == (this.protocol.Package() + '::guard::Variable')) {
73
- if (v.fields.type == OperatorType.TYPE_FUTURE_QUERY || v.fields.type == ContextType.TYPE_CONTEXT_FUTURE_ID) {
74
- info.futrue_list.push({identifier:v.fields.identifier, type:v.fields.type,
75
- witness:'0x' + Bcs.getInstance().de(BCS.ADDRESS, Uint8Array.from(v.fields.value))});
441
+ // ValueType.TYPE_ADDRESS: Query_Cmd maybe used the address, so save it for using
442
+ if (v.fields.type == ContextType.TYPE_WITNESS_ID || v.fields.type == ValueType.TYPE_ADDRESS) {
443
+ info.variable.push({identifier:v.fields.identifier, index:this.get_index(), type:v.fields.type,
444
+ value_or_witness:'0x' + Bcs.getInstance().de(BCS.ADDRESS, Uint8Array.from(v.fields.value))});
76
445
  }
77
446
  }
78
447
  });
79
448
  }
80
449
 
81
- private parse_bcs = (info:GuardInfo, chain_bytes: Uint8Array) => {
450
+ parse_bcs = (info:GuardInfo, chain_bytes: Uint8Array) => {
82
451
  var arr = [].slice.call(chain_bytes.reverse());
83
452
  while (arr.length > 0) {
84
453
  var type : unknown = arr.shift() ;
85
454
  // console.log(type);
86
455
  switch (type as Data_Type) {
87
- case ContextType.TYPE_CONTEXT_SIGNER:
88
- case ContextType.TYPE_CONTEXT_CLOCK:
89
- case OperatorType.TYPE_LOGIC_OPERATOR_U128_GREATER:
90
- case OperatorType.TYPE_LOGIC_OPERATOR_U128_GREATER_EQUAL:
91
- case OperatorType.TYPE_LOGIC_OPERATOR_U128_LESSER:
92
- case OperatorType.TYPE_LOGIC_OPERATOR_U128_LESSER_EQUAL:
93
- case OperatorType.TYPE_LOGIC_OPERATOR_U128_EQUAL:
94
- case OperatorType.TYPE_LOGIC_OPERATOR_EQUAL:
95
- case OperatorType.TYPE_LOGIC_OPERATOR_HAS_SUBSTRING:
456
+ case ContextType.TYPE_SIGNER:
457
+ case ContextType.TYPE_CLOCK:
458
+ case OperatorType.TYPE_LOGIC_AS_U256_GREATER:
459
+ case OperatorType.TYPE_LOGIC_AS_U256_GREATER_EQUAL:
460
+ case OperatorType.TYPE_LOGIC_AS_U256_LESSER:
461
+ case OperatorType.TYPE_LOGIC_AS_U256_LESSER_EQUAL:
462
+ case OperatorType.TYPE_LOGIC_AS_U256_EQUAL:
463
+ case OperatorType.TYPE_LOGIC_EQUAL:
464
+ case OperatorType.TYPE_LOGIC_HAS_SUBSTRING:
96
465
  case OperatorType.TYPE_LOGIC_ALWAYS_TRUE:
97
466
  case OperatorType.TYPE_LOGIC_NOT:
98
467
  case OperatorType.TYPE_LOGIC_AND:
99
468
  case OperatorType.TYPE_LOGIC_OR:
100
469
  break;
101
- case ContextType.TYPE_CONTEXT_FUTURE_ID:
102
- case OperatorType.TYPE_FUTURE_QUERY:
103
- var identifer = arr.splice(0, 1);
104
- let i = info.futrue_list.find(f => f.identifier == identifer[0]) ;
105
- if (!i) { ERROR(Errors.Fail, 'futrue_list not found')}
106
- if (type == OperatorType.TYPE_FUTURE_QUERY) {
107
- info.query_list.push({identifier:identifer[0], type:type as number, witness:i!.witness}); // query list item
108
- arr.splice(0, 1); // cmd
109
- }
470
+ case ContextType.TYPE_VARIABLE:
471
+ arr.splice(0, 1); // identifier of variable
110
472
  break;
111
- case ContextType.TYPE_CONTEXT_address:
112
- case ContextType.TYPE_CONTEXT_bool:
113
- case ContextType.TYPE_CONTEXT_u8:
114
- case ContextType.TYPE_CONTEXT_u64:
115
- case ContextType.TYPE_CONTEXT_vec_u8:
116
- case ValueType.TYPE_STATIC_bool:
117
- case ValueType.TYPE_STATIC_u8:
118
- arr.splice(0, 1); // identifier
473
+ case ContextType.TYPE_WITNESS_ID: // add to variable
474
+ let addr = '0x' + Bcs.getInstance().de(BCS.ADDRESS, Uint8Array.from(arr)).toString();
475
+ arr.splice(0, 32); // address
476
+ info.input_witness.push({index:this.get_index(), type:ContextType.TYPE_WITNESS_ID, value_or_witness:addr})
119
477
  break;
120
- case OperatorType.TYPE_QUERY_FROM_CONTEXT:
121
- arr.splice(0, 2); // identifer + cmd
478
+ case ValueType.TYPE_BOOL:
479
+ case ValueType.TYPE_U8:
480
+ arr.splice(0, 1); // identifier
122
481
  break;
123
- case ValueType.TYPE_STATIC_address:
482
+ case ValueType.TYPE_ADDRESS:
124
483
  arr.splice(0, 32);
125
484
  break;
126
- case ValueType.TYPE_STATIC_u64:
485
+ case ValueType.TYPE_U64:
127
486
  arr.splice(0, 8);
128
487
  break;
129
- case ValueType.TYPE_STATIC_u128:
488
+ case ValueType.TYPE_U128:
130
489
  arr.splice(0, 16);
131
490
  break;
132
- case ValueType.TYPE_STATIC_vec_u8:
491
+ case ValueType.TYPE_U256:
492
+ arr.splice(0, 32);
493
+ break;
494
+ case ValueType.TYPE_VEC_U8:
133
495
  let {value, length} = ulebDecode(Uint8Array.from(arr));
134
496
  arr.splice(0, value+length);
135
497
  break;
136
498
  case OperatorType.TYPE_QUERY:
137
- info.query_list.push('0x' + Bcs.getInstance().de(BCS.ADDRESS, Uint8Array.from(arr)).toString());
138
- arr.splice(0, 33); // address + cmd
499
+ let type = arr.splice(0, 1);
500
+ if (type[0] == ValueType.TYPE_ADDRESS || type[0] == ContextType.TYPE_WITNESS_ID) {
501
+ let addr = '0x' + Bcs.getInstance().de(BCS.ADDRESS, Uint8Array.from(arr)).toString();
502
+ arr.splice(0, 33); // address + cmd
503
+ if (type[0] == ValueType.TYPE_ADDRESS) {
504
+ info.query_list.push(addr);
505
+ } else if (type[0] == ContextType.TYPE_WITNESS_ID){
506
+ info.query_list.push({index:this.get_index(), type:type[0], value_or_witness:addr});
507
+ }
508
+ } else if (type[0] == ContextType.TYPE_VARIABLE) {
509
+ let identifer = arr.splice(0, 2); // key + cmd
510
+ let variable = info.variable.find((v) =>
511
+ (v.identifier == identifer[0]) &&
512
+ ((v.type == ValueType.TYPE_ADDRESS) || (v.type == ContextType.TYPE_WITNESS_ID)));
513
+ if (!variable) { ERROR(Errors.Fail, 'indentifier not in variable')}
514
+ if (variable?.type == ValueType.TYPE_ADDRESS) {
515
+ info.query_list.push(variable.value_or_witness);
516
+ } else if (variable?.type == ContextType.TYPE_WITNESS_ID) {
517
+ info.query_list.push({identifier:identifer[0], type:variable.type, value_or_witness:variable.value_or_witness, index:this.get_index()});
518
+ }
519
+ } else {
520
+ ERROR(Errors.Fail, 'variable type invalid');
521
+ }
522
+
139
523
  break;
140
524
  default:
141
525
  ERROR(Errors.Fail, 'parse_bcs types')
@@ -143,27 +527,38 @@ export class GuardParser {
143
527
  }
144
528
  }
145
529
 
530
+ private get_object(guardid:string, info:QueryInfo, fill?:FutrueFill[]) : string {
531
+ let r = fill?.find(i => guardid == i.guard && i.index == info.index);
532
+ if (!r || !r.future) {
533
+ if (!info.future) {
534
+ ERROR(Errors.InvalidParam, 'index invalid for fill')
535
+ }
536
+ } else {
537
+ info.future = r!.future;
538
+ }
539
+ return info.future!
540
+ }
541
+
146
542
  done = async (fill?:FutrueFill[]) : Promise<PassportQuery>=> {
147
543
  let objects: string[] = [];
148
544
  this.guard_list.forEach((g) => {
149
- g.futrue_list.forEach((f) => {
150
- let r = fill?.find(i => i.identifier == f.identifier && g.id == i.guard);
151
- if (!r && !f.futrue) { ERROR(Errors.InvalidParam, 'fill') }
152
-
153
- if (r) f.futrue = r!.future;
154
- objects.push(f.futrue!);
545
+ g.variable.filter(v => v.type == ContextType.TYPE_WITNESS_ID).forEach((q) => {
546
+ objects.push(this.get_object(g.id, q, fill));
155
547
  })
156
- g.query_list = g.query_list.map((q) => {
548
+ let list = g.query_list.map((q) => {
157
549
  if (typeof(q) === "string") {
158
550
  objects.push(q)
159
551
  return q
160
552
  } else {
161
- let r = g.futrue_list.find(f => f.identifier == q.identifier && f.type == q.type && f.witness == q.witness);
162
- if (!r || !r.futrue) { ERROR(Errors.Fail, 'query witness not match')}
163
- objects.push(r!.futrue!);
164
- return r!.futrue!
553
+ let r = this.get_object(g.id, q, fill);
554
+ objects.push(r);
555
+ return r
165
556
  }
166
557
  })
558
+ g.query_list = list;
559
+ g.input_witness.forEach((q) => {
560
+ objects.push(this.get_object(g.id, q, fill));
561
+ })
167
562
  })
168
563
 
169
564
  // objects info
@@ -175,18 +570,22 @@ export class GuardParser {
175
570
  let r = res.find(r => r.data?.objectId == q as string);
176
571
  if (!r) { ERROR(Errors.Fail, 'query object not match')}
177
572
  let object = this.object_query(r!.data);
178
- if (!object) { ERROR(Errors.Fail, 'object fail')}
573
+ if (!object) { ERROR(Errors.Fail, 'query object fail')}
179
574
  query.push(object!);
180
575
  })
181
- g.futrue_list.forEach(f => {
182
- let r = res.find(r => r.data?.objectId == f.futrue!);
183
- if (!r) { ERROR(Errors.Fail, 'query future not match')}
184
- let object = this.object_query(r!.data, 'witness');
185
- witness.push(object!)
576
+ res.forEach(q => {
577
+ let r1 = g.variable.find(v => v.future == q.data?.objectId);
578
+ let r2 = g.input_witness.find(v => v.future == q.data?.objectId)
579
+ // not match r1 || r2 means query-cmd, not witness-cmd
580
+ if (r1 || r2) {
581
+ let object = this.object_query(q.data, 'witness');
582
+ if (!object) { ERROR(Errors.Fail, 'witness object fail') }
583
+ witness.push(object!);
584
+ }
186
585
  })
187
586
  })
188
587
 
189
- return {query:query, witness:witness} as PassportQuery;
588
+ return {guard:this.guards, query:query, witness:witness} as PassportQuery;
190
589
  }
191
590
 
192
591
  private object_query = (data: any, method:string='guard_query') : Guard_Query_Object | undefined=> {
@@ -213,11 +612,11 @@ export class Passport {
213
612
 
214
613
  get_object () { return this.passport }
215
614
  // return passport object used
216
- constructor (protocol:Protocol, guards:GuardObject[], query?:PassportQuery) {
217
- if (!guards || guards.length > Passport.MAX_GUARD_COUNT) {
615
+ constructor (protocol:Protocol, query:PassportQuery) {
616
+ if (!query.guard || query.guard.length > Passport.MAX_GUARD_COUNT) {
218
617
  ERROR(Errors.InvalidParam, 'guards' )
219
618
  }
220
- if (!Protocol.IsValidObjects(guards)) {
619
+ if (!Protocol.IsValidObjects(query.guard)) {
221
620
  ERROR(Errors.IsValidObjects, 'guards')
222
621
  }
223
622
 
@@ -229,10 +628,10 @@ export class Passport {
229
628
  });
230
629
 
231
630
  // add others guards, if any
232
- guards.forEach((guard) => {
631
+ query.guard.forEach((g) => {
233
632
  txb.moveCall({
234
633
  target:protocol.PassportFn('guard_add') as FnCallType,
235
- arguments:[this.passport, Protocol.TXB_OBJECT(txb, guard)]
634
+ arguments:[this.passport, Protocol.TXB_OBJECT(txb, g)]
236
635
  });
237
636
  })
238
637
 
@@ -247,13 +646,13 @@ export class Passport {
247
646
 
248
647
  // rules: 'verify' & 'query' in turns;'verify' at final end.
249
648
  query?.query.forEach((q) => {
250
- let [type, address] = txb.moveCall({
649
+ let address = txb.moveCall({
251
650
  target: protocol.PassportFn('passport_verify') as FnCallType,
252
651
  arguments: [ this.passport, txb.object(Protocol.CLOCK_OBJECT)]
253
652
  });
254
653
  txb.moveCall({
255
654
  target: q.target as FnCallType,
256
- arguments: [ txb.object(q.object), this.passport, type, address ],
655
+ arguments: [ txb.object(q.object), this.passport, address ],
257
656
  typeArguments: q.types,
258
657
  })
259
658
  })