wowok 1.2.11 → 1.3.3

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,9 +1,8 @@
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
- import { BCS, BcsReader } from '@mysten/bcs';
7
6
  import { ERROR, Errors } from './exception';
8
7
  import { Guard } from './guard';
9
8
 
@@ -59,13 +58,11 @@ export interface PassportQuery {
59
58
  }
60
59
  export class GuardParser {
61
60
  protected guard_list: GuardInfo[] = [];
62
- protected protocol: Protocol;
63
61
  protected guards: string[];
64
62
  private index:number = 0;
65
63
  private get_index() { return this.index++ }
66
64
 
67
- private constructor(protocol: Protocol, guards: string[]) {
68
- this.protocol = protocol ;
65
+ private constructor(guards: string[]) {
69
66
  this.guards = guards;
70
67
  }
71
68
  guardlist = () => { return this.guard_list }
@@ -453,7 +450,7 @@ export class GuardParser {
453
450
  */
454
451
  private static Parse_Guard_Helper(guards: string[], res:SuiObjectResponse[]) {
455
452
  const protocol = Protocol.Instance();
456
- const me = new GuardParser(protocol, guards);
453
+ const me = new GuardParser(guards);
457
454
  res.forEach((r) => {
458
455
  let c = r.data?.content as any;
459
456
  if (!c) return;
@@ -512,7 +509,7 @@ export class GuardParser {
512
509
 
513
510
  parse_constant = (info:GuardInfo, constants:any) => {
514
511
  constants.forEach((v:any) => {
515
- if (v.type == (this.protocol.Package() + '::guard::Constant')) {
512
+ if (v.type == (Protocol.Instance().Package() + '::guard::Constant')) {
516
513
  // ValueType.TYPE_ADDRESS: Query_Cmd maybe used the address, so save it for querying
517
514
  if (v.fields.type == ContextType.TYPE_WITNESS_ID || v.fields.type == ValueType.TYPE_ADDRESS) {
518
515
  info.constant.push({identifier:v.fields.identifier, index:this.get_index(), type:v.fields.type,
@@ -643,7 +640,7 @@ export class GuardParser {
643
640
  })
644
641
 
645
642
  if (onPassportQueryReady) {
646
- this.protocol.Query_Raw(array_unique(objects), {showType:true}).then((res) => {
643
+ Protocol.Instance().Query_Raw(array_unique(objects), {showType:true}).then((res) => {
647
644
  onPassportQueryReady(this.done_helper(res));
648
645
  }).catch(e => {
649
646
  console.log(e);
@@ -651,7 +648,7 @@ export class GuardParser {
651
648
  })
652
649
  return undefined;
653
650
  } else {
654
- const res = await this.protocol.Query_Raw(array_unique(objects), {showType:true});
651
+ const res = await Protocol.Instance().Query_Raw(array_unique(objects), {showType:true});
655
652
  return this.done_helper(res);
656
653
  }
657
654
  }
@@ -686,9 +683,9 @@ export class GuardParser {
686
683
 
687
684
  // create onchain query for objects : object, movecall-types, id
688
685
  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,
686
+ for (let k = 0; k < Protocol.Instance().WOWOK_OBJECTS_TYPE().length; k++) {
687
+ if (data.type.includes(Protocol.Instance().WOWOK_OBJECTS_TYPE()[k]) ) { // type: pack::m::Object<...>
688
+ return { target:Protocol.Instance().WOWOK_OBJECTS_PREFIX_TYPE()[k] + method as FnCallType,
692
689
  object:Inputs.SharedObjectRef({
693
690
  objectId: data.objectId,
694
691
  mutable: false,
@@ -705,73 +702,116 @@ export class GuardParser {
705
702
  export class Passport {
706
703
  static MAX_GUARD_COUNT = 8;
707
704
  protected passport;
708
- protected protocol;
705
+ protected txb;
709
706
 
710
707
  get_object () { return this.passport }
711
708
  // return passport object used
712
709
  // bObject(true) in cmd env; (false) in service env
713
- constructor (protocol:Protocol, query:PassportQuery, bObject:boolean=false) {
710
+ constructor (txb:TransactionBlock, query:PassportQuery, bObject:boolean=false) {
714
711
  if (!query.guard || query.guard.length > Passport.MAX_GUARD_COUNT) {
715
712
  ERROR(Errors.InvalidParam, 'guards' )
716
713
  }
717
714
 
718
- this.protocol = protocol;
719
- let txb = protocol.CurrentSession();
720
- this.passport = txb.moveCall({
721
- target: protocol.PassportFn('new') as FnCallType,
715
+ this.txb = txb;
716
+ this.passport = this.txb.moveCall({
717
+ target:Protocol.Instance().PassportFn('new') as FnCallType,
722
718
  arguments: []
723
719
  });
724
720
 
725
721
  // add others guards, if any
726
722
  query.guard.forEach((g) => {
727
- txb.moveCall({
728
- target:protocol.PassportFn('guard_add') as FnCallType,
729
- arguments:[this.passport, txb.object(g)]
723
+ this.txb.moveCall({
724
+ target:Protocol.Instance().PassportFn('guard_add') as FnCallType,
725
+ arguments:[this.passport, this.txb.object(g)]
730
726
  });
731
727
  })
732
728
 
733
729
  // witness
734
730
  query?.witness.forEach((w) => {
735
- txb.moveCall({
731
+ this.txb.moveCall({
736
732
  target: w.target as FnCallType,
737
- arguments: [this.passport, txb.object(w.object)],
733
+ arguments: [this.passport, this.txb.object(w.object)],
738
734
  typeArguments: w.types,
739
735
  })
740
736
  })
741
737
 
738
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
742
739
  // rules: 'verify' & 'query' in turns; 'verify' at final end.
743
740
  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)]
741
+ let address = this.txb.moveCall({
742
+ target: Protocol.Instance().PassportFn('passport_verify') as FnCallType,
743
+ arguments: [ this.passport, this.txb.object(clock)]
747
744
  });
748
- txb.moveCall({
745
+ this.txb.moveCall({
749
746
  target: q.target as FnCallType,
750
- arguments: [ bObject ? txb.object(q.object) : txb.object(q.id), this.passport, address ],
747
+ arguments: [ bObject ? this.txb.object(q.object) : this.txb.object(q.id), this.passport, address ],
751
748
  typeArguments: q.types,
752
749
  })
753
750
  })
754
- txb.moveCall({
755
- target: protocol.PassportFn('passport_verify') as FnCallType,
756
- arguments: [ this.passport, txb.object(Protocol.CLOCK_OBJECT) ]
751
+ this.txb.moveCall({
752
+ target: Protocol.Instance().PassportFn('passport_verify') as FnCallType,
753
+ arguments: [ this.passport, this.txb.object(clock) ]
757
754
  });
758
755
  }
759
756
 
760
757
  destroy() {
761
- let txb = this.protocol.CurrentSession();
762
- txb.moveCall({
763
- target: this.protocol.PassportFn('destroy') as FnCallType,
758
+ this.txb.moveCall({
759
+ target: Protocol.Instance().PassportFn('destroy') as FnCallType,
764
760
  arguments: [ this.passport ]
765
761
  });
766
762
  }
767
763
 
768
764
  freeze() {
769
- let txb = this.protocol.CurrentSession();
770
- txb.moveCall({
771
- target: this.protocol.PassportFn('freezen') as FnCallType,
765
+ this.txb.moveCall({
766
+ target: Protocol.Instance().PassportFn('freezen') as FnCallType,
767
+ arguments: [ this.passport ]
768
+ });
769
+ }
770
+
771
+ query_result(sender:string, handleResult:OnQueryPassportResult) {
772
+ this.txb.moveCall({
773
+ target: Protocol.Instance().PassportFn('query_result') as FnCallType,
772
774
  arguments: [ this.passport ]
773
775
  });
776
+
777
+ Protocol.Client().devInspectTransactionBlock({sender:sender, transactionBlock:this.txb}).then((res) => {
778
+ const r = Passport.ResolveQueryRes(this.txb, res);
779
+ if (r) handleResult(r);
780
+ }).catch(e=>{
781
+ console.log(e);
782
+ })
774
783
  }
784
+
785
+ query_result_async = async (sender:string) : Promise<QueryPassportResult | undefined> => {
786
+ this.txb.moveCall({
787
+ target: Protocol.Instance().PassportFn('query_result') as FnCallType,
788
+ arguments: [ this.passport ]
789
+ });
790
+
791
+ const res = await Protocol.Client().devInspectTransactionBlock({sender:sender, transactionBlock:this.txb});
792
+ return Passport.ResolveQueryRes(this.txb, res);
793
+ }
794
+
795
+ private static ResolveQueryRes(txb:TransactionBlock, res:any) : QueryPassportResult | undefined {
796
+ for (let i = 0; i < res.results?.length; ++ i) {
797
+ const v = res.results[i];
798
+ if (v?.returnValues && v.returnValues.length === 2 &&
799
+ v.returnValues[0][1] === 'bool' && v.returnValues[1][1] === 'vector<address>') { // (bool, vector<address>)
800
+ const result = Bcs.getInstance().de('bool', Uint8Array.from(v.returnValues[0][0]));
801
+ const guards = Bcs.getInstance().de('vector<address>', Uint8Array.from(v.returnValues[1][0])).map((v:string)=>'0x'+v);
802
+ return {txb:txb, result:result, guards:guards}
803
+ }
804
+ }
805
+ return undefined
806
+ }
807
+ }
808
+
809
+ export interface QueryPassportResult {
810
+ txb: TransactionBlock;
811
+ result: boolean;
812
+ guards: string[];
775
813
  }
776
814
 
815
+ export type OnQueryPassportResult = (result:QueryPassportResult) => void;
816
+
777
817