wowok 1.2.10 → 1.2.12

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,12 @@
1
- import { type TransactionObjectInput, Inputs, TransactionObjectArgument} from '@mysten/sui.js/transactions';
2
- import { SuiObjectResponse } from '@mysten/sui.js/client';
1
+ import { type TransactionObjectInput, Inputs, Transaction as TransactionBlock} from '@mysten/sui/transactions';
2
+ import { SuiObjectResponse } from '@mysten/sui/client';
3
3
  import { FnCallType, GuardObject, Protocol, ContextType, OperatorType, Data_Type,
4
4
  ValueType, SER_VALUE, IsValidOperatorType } from './protocol';
5
5
  import { parse_object_type, array_unique, Bcs, ulebDecode, IsValidAddress, IsValidArray, OPTION_NONE, readOption, readOptionString } from './utils';
6
6
  import { BCS, BcsReader } from '@mysten/bcs';
7
7
  import { ERROR, Errors } from './exception';
8
8
  import { Guard } from './guard';
9
+ import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils';
9
10
 
10
11
  export type Guard_Query_Object = {
11
12
  target: FnCallType, // object fnCall
@@ -59,13 +60,11 @@ export interface PassportQuery {
59
60
  }
60
61
  export class GuardParser {
61
62
  protected guard_list: GuardInfo[] = [];
62
- protected protocol: Protocol;
63
63
  protected guards: string[];
64
64
  private index:number = 0;
65
65
  private get_index() { return this.index++ }
66
66
 
67
- private constructor(protocol: Protocol, guards: string[]) {
68
- this.protocol = protocol ;
67
+ private constructor(guards: string[]) {
69
68
  this.guards = guards;
70
69
  }
71
70
  guardlist = () => { return this.guard_list }
@@ -453,7 +452,7 @@ export class GuardParser {
453
452
  */
454
453
  private static Parse_Guard_Helper(guards: string[], res:SuiObjectResponse[]) {
455
454
  const protocol = Protocol.Instance();
456
- const me = new GuardParser(protocol, guards);
455
+ const me = new GuardParser(guards);
457
456
  res.forEach((r) => {
458
457
  let c = r.data?.content as any;
459
458
  if (!c) return;
@@ -512,7 +511,7 @@ export class GuardParser {
512
511
 
513
512
  parse_constant = (info:GuardInfo, constants:any) => {
514
513
  constants.forEach((v:any) => {
515
- if (v.type == (this.protocol.Package() + '::guard::Constant')) {
514
+ if (v.type == (Protocol.Instance().Package() + '::guard::Constant')) {
516
515
  // ValueType.TYPE_ADDRESS: Query_Cmd maybe used the address, so save it for querying
517
516
  if (v.fields.type == ContextType.TYPE_WITNESS_ID || v.fields.type == ValueType.TYPE_ADDRESS) {
518
517
  info.constant.push({identifier:v.fields.identifier, index:this.get_index(), type:v.fields.type,
@@ -643,7 +642,7 @@ export class GuardParser {
643
642
  })
644
643
 
645
644
  if (onPassportQueryReady) {
646
- this.protocol.Query_Raw(array_unique(objects), {showType:true}).then((res) => {
645
+ Protocol.Instance().Query_Raw(array_unique(objects), {showType:true}).then((res) => {
647
646
  onPassportQueryReady(this.done_helper(res));
648
647
  }).catch(e => {
649
648
  console.log(e);
@@ -651,7 +650,7 @@ export class GuardParser {
651
650
  })
652
651
  return undefined;
653
652
  } else {
654
- const res = await this.protocol.Query_Raw(array_unique(objects), {showType:true});
653
+ const res = await Protocol.Instance().Query_Raw(array_unique(objects), {showType:true});
655
654
  return this.done_helper(res);
656
655
  }
657
656
  }
@@ -686,9 +685,9 @@ export class GuardParser {
686
685
 
687
686
  // create onchain query for objects : object, movecall-types, id
688
687
  private object_query = (data: any, method:'guard_query'|'witness'='guard_query') : Guard_Query_Object | undefined=> {
689
- for (let k = 0; k < this.protocol.WOWOK_OBJECTS_TYPE().length; k++) {
690
- if (data.type.includes(this.protocol.WOWOK_OBJECTS_TYPE()[k]) ) { // type: pack::m::Object<...>
691
- return { target:this.protocol.WOWOK_OBJECTS_PREFIX_TYPE()[k] + method as FnCallType,
688
+ for (let k = 0; k < Protocol.Instance().WOWOK_OBJECTS_TYPE().length; k++) {
689
+ if (data.type.includes(Protocol.Instance().WOWOK_OBJECTS_TYPE()[k]) ) { // type: pack::m::Object<...>
690
+ return { target:Protocol.Instance().WOWOK_OBJECTS_PREFIX_TYPE()[k] + method as FnCallType,
692
691
  object:Inputs.SharedObjectRef({
693
692
  objectId: data.objectId,
694
693
  mutable: false,
@@ -705,71 +704,68 @@ export class GuardParser {
705
704
  export class Passport {
706
705
  static MAX_GUARD_COUNT = 8;
707
706
  protected passport;
708
- protected protocol;
707
+ protected txb;
709
708
 
710
709
  get_object () { return this.passport }
711
710
  // return passport object used
712
711
  // bObject(true) in cmd env; (false) in service env
713
- constructor (protocol:Protocol, query:PassportQuery, bObject:boolean=false) {
712
+ constructor (txb:TransactionBlock, query:PassportQuery, bObject:boolean=false) {
714
713
  if (!query.guard || query.guard.length > Passport.MAX_GUARD_COUNT) {
715
714
  ERROR(Errors.InvalidParam, 'guards' )
716
715
  }
717
716
 
718
- this.protocol = protocol;
719
- let txb = protocol.CurrentSession();
720
- this.passport = txb.moveCall({
721
- target: protocol.PassportFn('new') as FnCallType,
717
+ this.txb = txb;
718
+ this.passport = this.txb.moveCall({
719
+ target:Protocol.Instance().PassportFn('new') as FnCallType,
722
720
  arguments: []
723
721
  });
724
722
 
725
723
  // add others guards, if any
726
724
  query.guard.forEach((g) => {
727
- txb.moveCall({
728
- target:protocol.PassportFn('guard_add') as FnCallType,
729
- arguments:[this.passport, txb.object(g)]
725
+ this.txb.moveCall({
726
+ target:Protocol.Instance().PassportFn('guard_add') as FnCallType,
727
+ arguments:[this.passport, this.txb.object(g)]
730
728
  });
731
729
  })
732
730
 
733
731
  // witness
734
732
  query?.witness.forEach((w) => {
735
- txb.moveCall({
733
+ this.txb.moveCall({
736
734
  target: w.target as FnCallType,
737
- arguments: [this.passport, txb.object(w.object)],
735
+ arguments: [this.passport, this.txb.object(w.object)],
738
736
  typeArguments: w.types,
739
737
  })
740
738
  })
741
739
 
740
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
742
741
  // rules: 'verify' & 'query' in turns; 'verify' at final end.
743
742
  query?.query.forEach((q) => {
744
- let address = txb.moveCall({
745
- target: protocol.PassportFn('passport_verify') as FnCallType,
746
- arguments: [ this.passport, txb.object(Protocol.CLOCK_OBJECT)]
743
+ let address = this.txb.moveCall({
744
+ target: Protocol.Instance().PassportFn('passport_verify') as FnCallType,
745
+ arguments: [ this.passport, this.txb.object(clock)]
747
746
  });
748
- txb.moveCall({
747
+ this.txb.moveCall({
749
748
  target: q.target as FnCallType,
750
- arguments: [ bObject ? txb.object(q.object) : txb.object(q.id), this.passport, address ],
749
+ arguments: [ bObject ? this.txb.object(q.object) : this.txb.object(q.id), this.passport, address ],
751
750
  typeArguments: q.types,
752
751
  })
753
752
  })
754
-
755
- txb.moveCall({
756
- target: protocol.PassportFn('passport_verify') as FnCallType,
757
- arguments: [ this.passport, txb.object(Protocol.CLOCK_OBJECT) ]
753
+ this.txb.moveCall({
754
+ target: Protocol.Instance().PassportFn('passport_verify') as FnCallType,
755
+ arguments: [ this.passport, this.txb.object(clock) ]
758
756
  });
759
757
  }
760
758
 
761
759
  destroy() {
762
- let txb = this.protocol.CurrentSession();
763
- txb.moveCall({
764
- target: this.protocol.PassportFn('destroy') as FnCallType,
760
+ this.txb.moveCall({
761
+ target: Protocol.Instance().PassportFn('destroy') as FnCallType,
765
762
  arguments: [ this.passport ]
766
763
  });
767
764
  }
768
765
 
769
766
  freeze() {
770
- let txb = this.protocol.CurrentSession();
771
- txb.moveCall({
772
- target: this.protocol.PassportFn('freezen') as FnCallType,
767
+ this.txb.moveCall({
768
+ target: Protocol.Instance().PassportFn('freezen') as FnCallType,
773
769
  arguments: [ this.passport ]
774
770
  });
775
771
  }
package/src/permission.ts CHANGED
@@ -3,6 +3,8 @@ import { FnCallType, TxbObject, PermissionObject, PermissionAddress, GuardObject
3
3
  import { array_unique, IsValidAddress, IsValidArray, IsValidDesription, IsValidUintLarge, Bcs, IsValidName} from './utils';
4
4
  import { ERROR, Errors } from './exception';
5
5
  import { ValueType } from './protocol';
6
+ import { Passport } from './passport';
7
+ import { Transaction as TransactionBlock } from '@mysten/sui/transactions';
6
8
 
7
9
  export enum PermissionIndex {
8
10
  repository = 100,
@@ -85,6 +87,19 @@ export interface PermissionInfoType {
85
87
  guard?: string;
86
88
  }
87
89
 
90
+ export interface PermissionAnswer {
91
+ who: string;
92
+ owner?: boolean;
93
+ admin?: boolean;
94
+ items?: PermissionAnswerItem[]; // items === undefined, while errors
95
+ }
96
+ export interface PermissionAnswerItem {
97
+ query: PermissionIndexType;
98
+ permission: boolean;
99
+ guard?: string;
100
+ }
101
+ export type OnPermissionAnswer = (answer: PermissionAnswer) => void;
102
+
88
103
  export const PermissionInfo : PermissionInfoType[] = [
89
104
  {index:PermissionIndex.repository, name:'Repository', description:'Launch new Repository', module: 'repository'},
90
105
  {index:PermissionIndex.repository_set_description_set, name:'Description', description:'Set Repository description', module: 'repository'},
@@ -169,46 +184,43 @@ export type Permission_Entity = {
169
184
  }
170
185
 
171
186
  export class Permission {
172
- protected protocol;
187
+ protected txb;
173
188
  protected object : TxbObject;
174
189
 
175
190
  get_object() { return this.object }
176
- private constructor(protocol:Protocol) {
177
- this.protocol = protocol;
191
+ private constructor(txb:TransactionBlock) {
192
+ this.txb = txb;
178
193
  this.object = '';
179
194
  }
180
- static From(protocol:Protocol, object:TxbObject) : Permission {
181
- let p = new Permission(protocol);
182
- p.object = Protocol.TXB_OBJECT(protocol.CurrentSession(), object);
195
+ static From(txb:TransactionBlock, object:TxbObject) : Permission {
196
+ let p = new Permission(txb);
197
+ p.object = Protocol.TXB_OBJECT(txb, object);
183
198
  return p
184
199
  }
185
200
 
186
- static New(protocol:Protocol, description:string) : Permission {
201
+ static New(txb:TransactionBlock, description:string) : Permission {
187
202
  if (!IsValidDesription(description)) {
188
203
  ERROR(Errors.IsValidDesription)
189
204
  }
190
- let p = new Permission(protocol);
191
- let txb = protocol.CurrentSession();
205
+ let p = new Permission(txb);
192
206
  p.object = txb.moveCall({
193
- target: protocol.PermissionFn('new') as FnCallType,
194
- arguments: [txb.pure(description)]
207
+ target: Protocol.Instance().PermissionFn('new') as FnCallType,
208
+ arguments: [txb.pure.string(description)]
195
209
  });
196
210
  return p
197
211
  }
198
212
 
199
213
  launch() : PermissionAddress {
200
- let txb = this.protocol.CurrentSession();
201
- return txb.moveCall({ // address returned
202
- target:this.protocol.PermissionFn('create') as FnCallType,
203
- arguments:[ Protocol.TXB_OBJECT(txb, this.object) ]
214
+ return this.txb.moveCall({ // address returned
215
+ target:Protocol.Instance().PermissionFn('create') as FnCallType,
216
+ arguments:[ Protocol.TXB_OBJECT(this.txb, this.object) ]
204
217
  })
205
218
  }
206
219
 
207
220
  destroy() {
208
- let txb = this.protocol.CurrentSession();
209
- txb.moveCall({
210
- target:this.protocol.PermissionFn('destroy') as FnCallType,
211
- arguments: [Protocol.TXB_OBJECT(txb, this.object)],
221
+ this.txb.moveCall({
222
+ target:Protocol.Instance().PermissionFn('destroy') as FnCallType,
223
+ arguments: [Protocol.TXB_OBJECT(this.txb, this.object)],
212
224
  })
213
225
  }
214
226
  add_userdefine(index: number, name:string) {
@@ -219,10 +231,9 @@ export class Permission {
219
231
  if (!IsValidName(name)) {
220
232
  ERROR(Errors.IsValidName, 'add_userdefine');
221
233
  }
222
- let txb = this.protocol.CurrentSession();
223
- txb.moveCall({
224
- target:this.protocol.PermissionFn('user_define_add') as FnCallType,
225
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(index, BCS.U64), txb.pure(name)]
234
+ this.txb.moveCall({
235
+ target:Protocol.Instance().PermissionFn('user_define_add') as FnCallType,
236
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u64(index), this.txb.pure.string(name)]
226
237
  })
227
238
  }
228
239
 
@@ -231,10 +242,9 @@ export class Permission {
231
242
  ERROR(Errors.IsValidUserDefinedIndex, 'add_userdefine');
232
243
  }
233
244
 
234
- let txb = this.protocol.CurrentSession();
235
- txb.moveCall({
236
- target:this.protocol.PermissionFn('user_define_remove') as FnCallType,
237
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(index, BCS.U64)]
245
+ this.txb.moveCall({
246
+ target:Protocol.Instance().PermissionFn('user_define_remove') as FnCallType,
247
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u64(index)]
238
248
  })
239
249
  }
240
250
 
@@ -242,11 +252,11 @@ export class Permission {
242
252
  if (!IsValidAddress(old_entity) || !IsValidAddress(new_entity)) {
243
253
  ERROR(Errors.IsValidAddress, 'change_entity')
244
254
  }
245
- let txb = this.protocol.CurrentSession();
246
- txb.moveCall({
247
- target:this.protocol.PermissionFn('change_entity') as FnCallType,
248
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(old_entity, BCS.ADDRESS),
249
- txb.pure(new_entity, BCS.ADDRESS) ]
255
+
256
+ this.txb.moveCall({
257
+ target:Protocol.Instance().PermissionFn('change_entity') as FnCallType,
258
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(old_entity),
259
+ this.txb.pure.address(new_entity) ]
250
260
  })
251
261
  }
252
262
 
@@ -257,17 +267,16 @@ export class Permission {
257
267
  ERROR(Errors.IsValidArray, 'add_entity2');
258
268
  }
259
269
 
260
- let txb = this.protocol.CurrentSession();
261
270
  if (index) {
262
- txb.moveCall({
263
- target:this.protocol.PermissionFn('add_with_index') as FnCallType,
264
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(index, BCS.U64),
265
- txb.pure(array_unique(entities), 'vector<address>')]
271
+ this.txb.moveCall({
272
+ target:Protocol.Instance().PermissionFn('add_with_index') as FnCallType,
273
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u64(index),
274
+ this.txb.pure.vector('address', array_unique(entities))]
266
275
  })
267
276
  } else {
268
- txb.moveCall({
269
- target:this.protocol.PermissionFn('add') as FnCallType,
270
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(array_unique(entities), 'vector<address>')]
277
+ this.txb.moveCall({
278
+ target:Protocol.Instance().PermissionFn('add') as FnCallType,
279
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', array_unique(entities))]
271
280
  })
272
281
  }
273
282
  }
@@ -289,9 +298,7 @@ export class Permission {
289
298
  ERROR(Errors.InvalidParam, 'entities');
290
299
  }
291
300
 
292
- let txb = this.protocol.CurrentSession();
293
301
  let guards:any[] = [];
294
-
295
302
  for (let i = 0; i < entities.length; i++) {
296
303
  let entity = entities[i];
297
304
  let indexes :number[] = [];
@@ -310,19 +317,19 @@ export class Permission {
310
317
  }
311
318
  }
312
319
  //if (indexes.length > 0) {
313
- txb.moveCall({
314
- target:this.protocol.PermissionFn('add_batch') as FnCallType,
315
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(entity.entity_address, BCS.ADDRESS),
316
- txb.pure(indexes, 'vector<u64>')]
320
+ this.txb.moveCall({
321
+ target:Protocol.Instance().PermissionFn('add_batch') as FnCallType,
322
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(entity.entity_address),
323
+ this.txb.pure.vector('u64', indexes)]
317
324
  })
318
325
  //}
319
326
  }
320
327
  // set guards
321
328
  guards.forEach(({entity_address, index, guard}) => {
322
- txb.moveCall({
323
- target:this.protocol.PermissionFn('guard_set') as FnCallType,
324
- arguments:[ Protocol.TXB_OBJECT(txb, this.object), txb.pure(entity_address, BCS.ADDRESS),
325
- txb.pure(index, BCS.U64), Protocol.TXB_OBJECT(txb, guard)]
329
+ this.txb.moveCall({
330
+ target:Protocol.Instance().PermissionFn('guard_set') as FnCallType,
331
+ arguments:[ Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure(entity_address, BCS.ADDRESS),
332
+ this.txb.pure.u64(index), Protocol.TXB_OBJECT(this.txb, guard)]
326
333
  })
327
334
  })
328
335
  }
@@ -336,18 +343,17 @@ export class Permission {
336
343
  ERROR(Errors.IsValidPermissionIndex, 'index')
337
344
  }
338
345
 
339
- let txb = this.protocol.CurrentSession();
340
346
  if (guard) {
341
- txb.moveCall({
342
- target:this.protocol.PermissionFn('guard_set') as FnCallType,
343
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(entity_address, BCS.ADDRESS),
344
- txb.pure(index, BCS.U64), Protocol.TXB_OBJECT(txb, guard)]
347
+ this.txb.moveCall({
348
+ target:Protocol.Instance().PermissionFn('guard_set') as FnCallType,
349
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(entity_address),
350
+ this.txb.pure.u64(index), Protocol.TXB_OBJECT(this.txb, guard)]
345
351
  })
346
352
  } else {
347
- txb.moveCall({
348
- target:this.protocol.PermissionFn('guard_none') as FnCallType,
349
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(entity_address, BCS.ADDRESS),
350
- txb.pure(index, BCS.U64)]
353
+ this.txb.moveCall({
354
+ target:Protocol.Instance().PermissionFn('guard_none') as FnCallType,
355
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(entity_address),
356
+ this.txb.pure.u64(index)]
351
357
  })
352
358
  };
353
359
  }
@@ -360,11 +366,10 @@ export class Permission {
360
366
  ERROR(Errors.InvalidParam, 'index')
361
367
  }
362
368
 
363
- let txb = this.protocol.CurrentSession();
364
- txb.moveCall({
365
- target:this.protocol.PermissionFn('remove_index') as FnCallType,
366
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(entity_address, BCS.ADDRESS),
367
- txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, array_unique(index)))]
369
+ this.txb.moveCall({
370
+ target:Protocol.Instance().PermissionFn('remove_index') as FnCallType,
371
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(entity_address),
372
+ this.txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, array_unique(index)))]
368
373
  })
369
374
  }
370
375
  remove_entity(entity_address:string[]) {
@@ -372,21 +377,20 @@ export class Permission {
372
377
  ERROR(Errors.IsValidArray)
373
378
  }
374
379
 
375
- let txb = this.protocol.CurrentSession();
376
- txb.moveCall({
377
- target:this.protocol.PermissionFn('remove') as FnCallType,
378
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(array_unique(entity_address), 'vector<address>')]
380
+ this.txb.moveCall({
381
+ target:Protocol.Instance().PermissionFn('remove') as FnCallType,
382
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', array_unique(entity_address))]
379
383
  })
380
- ;
381
384
  }
385
+
382
386
  set_description(description:string) {
383
387
  if (!IsValidDesription(description)) {
384
388
  ERROR(Errors.IsValidDesription)
385
389
  }
386
- let txb = this.protocol.CurrentSession();
387
- txb.moveCall({
388
- target:this.protocol.PermissionFn('description_set') as FnCallType,
389
- arguments: [Protocol.TXB_OBJECT(txb, this.object), txb.pure(description)]
390
+
391
+ this.txb.moveCall({
392
+ target:Protocol.Instance().PermissionFn('description_set') as FnCallType,
393
+ arguments: [Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.string(description)]
390
394
  })
391
395
  ;
392
396
  }
@@ -396,12 +400,10 @@ export class Permission {
396
400
  ERROR(Errors.IsValidArray)
397
401
  }
398
402
 
399
- let txb = this.protocol.CurrentSession();
400
- txb.moveCall({
401
- target:this.protocol.PermissionFn('admin_add_batch') as FnCallType,
402
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(array_unique(admin), 'vector<address>')]
403
+ this.txb.moveCall({
404
+ target:Protocol.Instance().PermissionFn('admin_add_batch') as FnCallType,
405
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', array_unique(admin))]
403
406
  });
404
- ;
405
407
  }
406
408
 
407
409
  remove_admin(admin:string[], removeall?:boolean) {
@@ -410,16 +412,15 @@ export class Permission {
410
412
  ERROR(Errors.IsValidArray, 'admin')
411
413
  }
412
414
 
413
- let txb = this.protocol.CurrentSession();
414
415
  if (removeall) {
415
- txb.moveCall({
416
- target:this.protocol.PermissionFn('admins_clear') as FnCallType,
417
- arguments:[Protocol.TXB_OBJECT(txb, this.object)]
416
+ this.txb.moveCall({
417
+ target:Protocol.Instance().PermissionFn('admins_clear') as FnCallType,
418
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object)]
418
419
  });
419
420
  } else if (admin) {
420
- txb.moveCall({
421
- target:this.protocol.PermissionFn('admin_remove_batch') as FnCallType,
422
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(array_unique(admin), 'vector<address>')]
421
+ this.txb.moveCall({
422
+ target:Protocol.Instance().PermissionFn('admin_remove_batch') as FnCallType,
423
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', array_unique(admin))]
423
424
  });
424
425
  }
425
426
 
@@ -430,17 +431,84 @@ export class Permission {
430
431
  ERROR(Errors.IsValidAddress)
431
432
  }
432
433
 
433
- let txb = this.protocol.CurrentSession();
434
- txb.moveCall({
435
- target:this.protocol.PermissionFn('builder_set') as FnCallType,
436
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(new_owner, BCS.ADDRESS)]
434
+ this.txb.moveCall({
435
+ target:Protocol.Instance().PermissionFn('builder_set') as FnCallType,
436
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(new_owner)]
437
437
  });
438
438
  }
439
+ query_permissions(address_queried:string, permissions:PermissionIndexType[]) {
440
+ if (!IsValidAddress(address_queried)) {
441
+ ERROR(Errors.InvalidParam, 'query_permissions');
442
+ }
443
+
444
+ if (permissions.length === 0 || permissions.length > Permission.MAX_QUERY_COUNT) {
445
+ ERROR(Errors.InvalidParam, 'permissions count');
446
+ }
447
+
448
+ this.txb.moveCall({
449
+ target:Protocol.Instance().PermissionFn('query_permissions') as FnCallType,
450
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(address_queried), this.txb.pure.vector('u64', permissions)]
451
+ })
452
+ }
453
+
454
+ QueryPermissions(txb:TransactionBlock, address_queried:string, permissions:PermissionIndexType[], onPermissionAnswer:OnPermissionAnswer, sender?:string) {
455
+ this.query_permissions(address_queried, permissions);
456
+ Protocol.Client().devInspectTransactionBlock({sender:sender ?? address_queried, transactionBlock:txb}).then((res) => {
457
+ if (res.results && res.results[0].returnValues && res.results[0].returnValues.length !== 3 ) {
458
+ onPermissionAnswer({who:address_queried});
459
+ return
460
+ }
461
+
462
+ const perm = Bcs.getInstance().de(BCS.U8, Uint8Array.from((res.results as any)[0].returnValues[0][0]));
463
+
464
+ if (perm === Permission.PERMISSION_ADMIN || perm === Permission.PERMISSION_OWNER_AND_ADMIN) {
465
+ onPermissionAnswer({who:address_queried, admin:true, owner:perm%2===1, items:[]})
466
+ } else {
467
+ const perms = Bcs.getInstance().de('vector<u8>', Uint8Array.from((res.results as any)[0].returnValues[1][0]));
468
+ const guards = Bcs.getInstance().de('vector<address>', Uint8Array.from((res.results as any)[0].returnValues[2][0]));
469
+ if (perms.length !== permissions.length) {
470
+ onPermissionAnswer({who:address_queried});
471
+ return
472
+ }
473
+
474
+ const items: PermissionAnswerItem[] = permissions.map((v, index) => {
475
+ const p = perms[index] === Permission.PERMISSION_QUERY_NONE ? false : true;
476
+ let g : any = undefined;
477
+ if (p && perms[index] < guards.length) {
478
+ g = '0x' + guards[perms[index] as number];
479
+ }
480
+ return {query:v, permission:p, guard:g}
481
+ })
482
+ onPermissionAnswer({who:address_queried, admin:false, owner:perm%2===1, items:items});
483
+ }
484
+ }).catch((e) => {
485
+ console.log(e);
486
+ onPermissionAnswer({who:address_queried});
487
+ })
488
+ }
489
+ static HasPermission(answer:PermissionAnswer|undefined, index:PermissionIndexType) : {has:boolean, guard?:string, owner?:boolean} {
490
+ if (answer) {
491
+ if (answer.admin) return {has:true, owner:answer.owner}; // admin
492
+ let i = answer.items?.find((v)=>v.query === index);
493
+ if (i) {
494
+ return {has:i.permission, guard:i.guard, owner:answer.owner};
495
+ }
496
+ }
497
+ return {has:false}
498
+ }
439
499
 
440
500
  static MAX_ADMIN_COUNT = 64;
441
501
  static MAX_ENTITY_COUNT = 2000;
442
502
  static MAX_PERMISSION_INDEX_COUNT = 200;
443
503
  static MAX_PERSONAL_PERMISSION_COUNT = 200;
504
+ static MAX_QUERY_COUNT = 250; //
505
+ static PERMISSION_QUERY_NONE = 255;
506
+ static PERMISSION_QUERY_HAS = 254;
507
+ static PERMISSION_NORMAL = 0;
508
+ static PERMISSION_OWNER = 1;
509
+ static PERMISSION_ADMIN = 2;
510
+ static PERMISSION_OWNER_AND_ADMIN = 3;
511
+
444
512
  static IsValidUserDefinedIndex = (index:number) => {
445
513
  return index >= PermissionIndex.user_defined_start && IsValidUintLarge(index)
446
514
  }