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