wowok_agent 1.5.13 → 1.5.14

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/call/guard.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  import { Bcs, ContextType, ERROR, Errors, IsValidU8, OperatorType, ValueType, GUARD_QUERIES, IsValidAddress,
7
7
  concatenate, TransactionBlock, Protocol, FnCallType, hasDuplicates, insertAtHead,
8
8
  IsValidDesription, PassportObject, IsValidGuardIdentifier, GuardQuery, BCS,
9
+ MODULES,
9
10
  } from "wowok";
10
11
  import { CallBase, CallResult, Namedbject } from "./base.js";
11
12
  import { LocalMark } from "../local/local.js";
@@ -17,9 +18,14 @@ export interface GuardConst {
17
18
  value?: any; // if bWitness true, value ignores; otherwise, data.
18
19
  }
19
20
 
21
+ interface FunctiionQuery {
22
+ module:MODULES,
23
+ function:string,
24
+ }
25
+
20
26
  // parameters: Child nodes arranged in parameter order, with each child node providing the type and data for that parameter
21
27
  export type GuardNode = { identifier: number; } // Data from GuardConst
22
- | {query: number, object: string | number; parameters: GuardNode[];} // object: address string or identifier in GuardConst that value_type = ValueType.address
28
+ | {query: number | FunctiionQuery, object: string | number; parameters: GuardNode[];} // object: address string or identifier in GuardConst that value_type = ValueType.address
23
29
  | {logic: OperatorType.TYPE_LOGIC_AS_U256_GREATER | OperatorType.TYPE_LOGIC_AS_U256_GREATER_EQUAL
24
30
  | OperatorType.TYPE_LOGIC_AS_U256_LESSER | OperatorType.TYPE_LOGIC_AS_U256_LESSER_EQUAL
25
31
  | OperatorType.TYPE_LOGIC_AS_U256_EQUAL | OperatorType.TYPE_LOGIC_EQUAL | OperatorType.TYPE_LOGIC_HAS_SUBSTRING
@@ -65,7 +71,7 @@ export class CallGuard extends CallBase {
65
71
 
66
72
  // check root
67
73
  var output : Uint8Array[]= [];
68
- buildNode(this.data.root!, ValueType.TYPE_BOOL, this.data?.table ?? [], output);
74
+ await buildNode(this.data.root!, ValueType.TYPE_BOOL, this.data?.table ?? [], output);
69
75
  const bytes = (concatenate(Uint8Array, ...output) as Uint8Array);
70
76
 
71
77
  const obj = txb.moveCall({
@@ -83,7 +89,7 @@ export class CallGuard extends CallBase {
83
89
  })
84
90
  } else {
85
91
  if (v.value_type === ValueType.TYPE_ADDRESS) {
86
- v.value = await LocalMark.Instance().get_address(v.value);
92
+ v.value = await LocalMark.Instance().get_address(v.value); // address or name
87
93
  if (!v.value) { ERROR(Errors.InvalidParam, `CallGuard_Data.data.table address`)}
88
94
  };
89
95
  const tmp = Uint8Array.from(Bcs.getInstance().ser(v.value_type, v.value));
@@ -106,7 +112,7 @@ export class CallGuard extends CallBase {
106
112
 
107
113
  //export const MAX_CHILD_NODE_COUNT = 6;
108
114
 
109
- const buildNode = (guard_node:GuardNode, type_required:ValueType | 'number' | 'variable', table:GuardConst[], output:Uint8Array[]) => {
115
+ const buildNode = async (guard_node:GuardNode, type_required:ValueType | 'number' | 'variable', table:GuardConst[], output:Uint8Array[]) => {
110
116
  const node: any = guard_node as any;
111
117
  if (node?.identifier !== undefined) {
112
118
  const f = table.find(v=>v.identifier === node.identifier);
@@ -119,17 +125,18 @@ const buildNode = (guard_node:GuardNode, type_required:ValueType | 'number' | 'v
119
125
  }
120
126
  } else if (node?.query !== undefined) {
121
127
  var q: GuardQuery | undefined;
122
- if (typeof(node.query) === 'string') {
123
- q = GUARD_QUERIES.find(v=>v.query_name === node.query);
124
- } else if (typeof(node.query) === 'number') {
128
+ if (typeof(node.query) === 'number') {
125
129
  q = GUARD_QUERIES.find(v=>v.query_id === node.query);
130
+ } else {
131
+ q = GUARD_QUERIES.find(v=> v.module === node.query.module && v.query_name === node.query.function);
126
132
  }
127
- if (!q) ERROR(Errors.InvalidParam, 'query invalid - ' + node?.query);
133
+
134
+ if (!q) ERROR(Errors.InvalidParam, `${node.query} invalid. Its a query number or a struct that module and function name specified`);
128
135
 
129
136
  checkType(q!.return, type_required, node); // Return type checking
130
137
  if (q!.parameters.length === node.parameters.length) {
131
138
  for (let i = node.parameters.length - 1; i >= 0; --i) { // stack: first in, last out
132
- buildNode(node.parameters[i], q!.parameters[i], table, output); // Recursive check
139
+ await buildNode(node.parameters[i], q!.parameters[i], table, output); // Recursive check
133
140
  }
134
141
  } else {
135
142
  ERROR(Errors.InvalidParam, 'node query parameters length not match - ' + JSON.stringify(node))
@@ -137,11 +144,12 @@ const buildNode = (guard_node:GuardNode, type_required:ValueType | 'number' | 'v
137
144
 
138
145
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, OperatorType.TYPE_QUERY)); // QUERY TYPE + addr + cmd
139
146
  if (typeof(node.object) === 'string') {
140
- if (!IsValidAddress(node.object)) {
141
- ERROR(Errors.InvalidParam, 'node object from address string - ' + JSON.stringify(node))
147
+ const object = await LocalMark.Instance().get_address(node.object); // object name or address
148
+ if (!IsValidAddress(object)) {
149
+ ERROR(Errors.InvalidParam, 'node object from string - ' + JSON.stringify(node))
142
150
  }
143
151
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, ValueType.TYPE_ADDRESS));
144
- output.push(Bcs.getInstance().ser(ValueType.TYPE_ADDRESS, node.object)); // object address
152
+ output.push(Bcs.getInstance().ser(ValueType.TYPE_ADDRESS, object)); // object address
145
153
  } else {
146
154
  const f = table.find(v=>v.identifier === node.object);
147
155
  if (f) {
@@ -157,51 +165,77 @@ const buildNode = (guard_node:GuardNode, type_required:ValueType | 'number' | 'v
157
165
  checkType(ValueType.TYPE_BOOL, type_required, node); // bool
158
166
  switch (node?.logic) {
159
167
  case OperatorType.TYPE_LOGIC_AND:
160
- case OperatorType.TYPE_LOGIC_OR:
168
+ case OperatorType.TYPE_LOGIC_OR: {
161
169
  if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node logic parameters length must >= 2'+ JSON.stringify(node));
162
- (node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, ValueType.TYPE_BOOL, table, output)); // reserve
170
+ const p = (node.parameters as GuardNode[]).reverse(); // reserve
171
+ for (let i = 0; i < p.length; ++i) {
172
+ await buildNode(p[i], ValueType.TYPE_BOOL, table, output);
173
+ }
163
174
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
164
175
  output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
165
176
  break;
166
- case OperatorType.TYPE_LOGIC_NOT:
177
+ }
178
+ case OperatorType.TYPE_LOGIC_NOT: {
167
179
  if (node.parameters.length !== 1) ERROR(Errors.InvalidParam, 'node logic parameters length must be 1'+ JSON.stringify(node));
168
- (node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, ValueType.TYPE_BOOL, table, output)); // reserve
180
+ const p = (node.parameters as GuardNode[]).reverse();
181
+ for (let i = 0; i < p.length; ++i) {
182
+ await buildNode(p[i], ValueType.TYPE_BOOL, table, output);
183
+ }
169
184
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
170
185
  break;
186
+ }
171
187
  case OperatorType.TYPE_LOGIC_AS_U256_GREATER:
172
188
  case OperatorType.TYPE_LOGIC_AS_U256_GREATER_EQUAL:
173
189
  case OperatorType.TYPE_LOGIC_AS_U256_LESSER:
174
190
  case OperatorType.TYPE_LOGIC_AS_U256_LESSER_EQUAL:
175
- case OperatorType.TYPE_LOGIC_AS_U256_EQUAL:
191
+ case OperatorType.TYPE_LOGIC_AS_U256_EQUAL: {
176
192
  if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node logic parameters length must >= 2'+ JSON.stringify(node));
177
- (node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, 'number', table, output));
193
+ const p = (node.parameters as GuardNode[]).reverse();
194
+ for (let i = 0; i < p.length; ++i) {
195
+ await buildNode(p[i], 'number', table, output);
196
+ }
178
197
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
179
198
  output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
180
199
  break;
181
- case OperatorType.TYPE_LOGIC_EQUAL:
200
+ }
201
+ case OperatorType.TYPE_LOGIC_EQUAL: {
182
202
  if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node logic parameters length must >= 2'+ JSON.stringify(node));
183
203
  var any_type: any = 'variable';
184
- (node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, any_type, table, output));
204
+ const p = (node.parameters as GuardNode[]).reverse();
205
+ for (let i = 0; i < p.length; ++i) {
206
+ await buildNode(p[i], any_type, table, output);
207
+ }
185
208
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
186
209
  output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
187
- break;
188
- case OperatorType.TYPE_LOGIC_HAS_SUBSTRING:
210
+ break;
211
+ }
212
+ case OperatorType.TYPE_LOGIC_HAS_SUBSTRING: {
189
213
  if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node logic parameters length must >= 2'+ JSON.stringify(node));
190
- (node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, ValueType.TYPE_STRING, table, output));
214
+ const p = (node.parameters as GuardNode[]).reverse();
215
+ for (let i = 0; i < p.length; ++i) {
216
+ await buildNode(p[i], ValueType.TYPE_STRING, table, output);
217
+ }
191
218
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
192
219
  output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
193
220
  break;
221
+ }
194
222
  }
195
223
  } else if (node?.calc !== undefined) {
196
224
  if (node?.calc === OperatorType.TYPE_NUMBER_ADDRESS) {
197
225
  checkType(ValueType.TYPE_ADDRESS, type_required, node);
198
226
  if (node.parameters.length !== 1) ERROR(Errors.InvalidParam, 'node TYPE_NUMBER_ADDRESS parameters length must == 1'+ JSON.stringify(node));
199
- (node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, 'number', table, output));
227
+ const p = (node.parameters as GuardNode[]).reverse();
228
+ for (let i = 0; i < p.length; ++i) {
229
+ await buildNode(p[i], 'number', table, output);
230
+ }
200
231
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.calc)); // TYPE
201
232
  } else {
202
233
  checkType(ValueType.TYPE_U256, type_required, node);
203
234
  if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node calc parameters length must >= 2'+ JSON.stringify(node));
204
- (node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, 'number', table, output));
235
+ const p = (node.parameters as GuardNode[]).reverse();
236
+ for (let i = 0; i < p.length; ++i) {
237
+ await buildNode(p[i], 'number', table, output);
238
+ }
205
239
  output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.calc)); // TYPE
206
240
  output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
207
241
  }
@@ -216,6 +250,12 @@ const buildNode = (guard_node:GuardNode, type_required:ValueType | 'number' | 'v
216
250
  } else {
217
251
  output.push(Bcs.getInstance().ser(ValueType.TYPE_VEC_U8, node.value));
218
252
  }
253
+ } else if (node.value_type === ValueType.TYPE_ADDRESS) {
254
+ const addr = await LocalMark.Instance().get_address(node.value); // address or name
255
+ if (!IsValidAddress(addr)) {
256
+ ERROR(Errors.IsValidAddress, 'node value from string - ' + JSON.stringify(node))
257
+ }
258
+ output.push(Bcs.getInstance().ser(ValueType.TYPE_ADDRESS, addr));
219
259
  } else {
220
260
  output.push(Bcs.getInstance().ser(node?.value_type, node.value));
221
261
  }
@@ -104,8 +104,11 @@ export class CallPermission extends CallBase {
104
104
  for (let i = 0; i < this.data.permission.entities.length; ++i) {
105
105
  const v = this.data.permission.entities[i];
106
106
  const addr = await GetAccountOrMark_Address(v.address);
107
+
107
108
  if (addr) {
108
109
  add_entity.push({address:addr, permissions:v.permissions});
110
+ } else {
111
+ ERROR(Errors.InvalidParam, `CallPermission_Data.data.permission.entities.address ${i}: ${v.address.name_or_address} NOT found `);
109
112
  }
110
113
  }
111
114
  obj?.add_entity(add_entity);
@@ -56,31 +56,25 @@ export class CallPersonal extends CallBase {
56
56
  if (obj && obj?.get_object()) {
57
57
  switch(this.data.mark.op) {
58
58
  case 'add':
59
- const add = [];
60
59
  for (let i = 0; i < this.data.mark.data.length; ++i) {
61
60
  const v = this.data.mark.data[i];
62
61
  const addr = await GetAccountOrMark_Address(v.address);
63
62
  if (addr) {
64
- add.push({address:addr, tags:v.tags, name:v.name})
63
+ obj?.add(addr, v.tags ?? [], v.name);
64
+ //@ always add to local, for easy using
65
+ await LocalMark.Instance().put(v.name, {address:addr, tags:v.tags});
65
66
  }
66
67
  }
67
-
68
- add.forEach(v => {
69
- obj?.add(v.address, v.tags ?? [], v.name)
70
- })
71
68
  break;
72
69
  case 'remove':
73
- const remove = [];
74
70
  for (let i = 0; i < this.data.mark.data.length; ++i) {
75
71
  const v = this.data.mark.data[i];
76
72
  const addr = await GetAccountOrMark_Address(v.address);
77
73
  if (addr) {
78
- remove.push({address:addr, tags:v.tags})
74
+ obj?.remove(addr, v.tags ?? []);
75
+ //@ dont del from local
79
76
  }
80
- }
81
- remove.forEach(v => {
82
- obj?.remove(v.address, v.tags ?? [])
83
- })
77
+ }
84
78
  break;
85
79
  case 'removeall':
86
80
  for (let i = 0; i < this.data.mark.addresses.length; ++i) {
@@ -42,11 +42,11 @@ export class Account {
42
42
  }; return Account._instance
43
43
  }
44
44
 
45
- private accountData(data:AccountData | undefined) : AccountData | undefined {
45
+ private accountData(data:AccountData | undefined, showSecret:boolean=false) : AccountData | undefined {
46
46
  if (!data) return ;
47
47
  const r = Ed25519Keypair.fromSecretKey(fromHex(data.secret!));
48
48
  data.pubkey = r.getPublicKey().toSuiPublicKey();
49
- data.secret = undefined; //r.getSecretKey();
49
+ data.secret = showSecret ? r.getSecretKey() : undefined;
50
50
  return data;
51
51
  }
52
52
 
@@ -192,15 +192,15 @@ export class Account {
192
192
  })
193
193
  }
194
194
 
195
- async list(showSuspended?:boolean) : Promise<AccountData[]> {
195
+ async list(showSuspended?:boolean, showSecret:boolean=false) : Promise<AccountData[]> {
196
196
  return await retry_db(this.location, async(storage:Level) => {
197
197
  const r = await storage.get(AccountKey);
198
198
  if (r) {
199
199
  const s = JSON.parse(r) as AccountData[];
200
200
  if (showSuspended) {
201
- return s.map(v => this.accountData(v)!);
201
+ return s.map(v => this.accountData(v, showSecret)!);
202
202
  } else {
203
- return s.filter(v => !v.suspended).map(v => this.accountData(v)!);
203
+ return s.filter(v => !v.suspended).map(v => this.accountData(v, showSecret)!);
204
204
  }
205
205
  }
206
206
  return [];
@@ -3,7 +3,7 @@ import { z } from "zod";
3
3
  import * as WOWOK from 'wowok';
4
4
  import * as D from './const.js';
5
5
  import { zodToJsonSchema } from "zod-to-json-schema";
6
- import { ObjectUrl } from "./util.js";
6
+ import { GuardQueryModules, ObjectUrl } from "./util.js";
7
7
 
8
8
  export const GetMarkNameSchema = (object:string='') : z.ZodString=> {
9
9
  return z.string().nonempty().describe(D.MarkName_Address_Description(object));
@@ -83,12 +83,17 @@ const ObjectsOperationSchema = (object:string='') => {
83
83
  ])
84
84
  };
85
85
 
86
+ export const ModuleSchema = z.enum(GuardQueryModules() as [string, ...string[]]).describe("Modules of the Guard queries");
87
+
86
88
  const GuardNodeSchema: z.ZodType = z.lazy(() => z.union([
87
89
  z.object({
88
90
  identifier: GuardIndentifierSchema
89
91
  }).describe(D.Identifier_Description),
90
92
  z.object({
91
- query: z.number().int().describe(D.QueryId_Description),
93
+ query: z.union([z.number().int().describe(D.QueryId_Description), z.object({
94
+ module: ModuleSchema,
95
+ function: z.string().nonempty(),
96
+ }).describe(D.QueryName_Description)]),
92
97
  object: z.union([GetMarkNameSchema(), GuardIndentifierSchema.describe(D.ObjectIdentifier_Description)
93
98
  ]).describe(D.ObjectQuery_Description),
94
99
  parameters: z.array(GuardNodeSchema).describe(D.GuardNodeParams_Description)
@@ -27,7 +27,7 @@ export const useAddressIfNameExist_Description = `In case of naming conflict: pr
27
27
  export const onChain_Description = `If true, the name and tags of the object will be made visible on-chain`;
28
28
  export const OnchainDescription_Description = `Description content for newly created on-chain wowok object`
29
29
  export const Type_Description = `Generic type for the on-chain object (e.g., specify payment token type '0x2::sui::SUI')`;
30
- export const ValueType_Description = `Data types of the Wowok protocol:
30
+ export const ValueType_Description = `Data types of the Wowok protocol.
31
31
  100: Bool, 101: Address, 102: U8, 103: U64, 104: Vec<U8>, 105: U128,
32
32
  106:Vec<Address>, 107: Vec<Bool>, 108: Vec<Vec<U8>>,
33
33
  109:Vec<U64>, 110: Vec<U128>, 111: Option<Address>, 112: Option<Bool>,
@@ -42,7 +42,8 @@ export const Permission_Description = `Operation permissions for the new object
42
42
  export const Identifier_Description = `Data from the Guard table corresponding to the identifier`
43
43
  export const ObjectIdentifier_Description = 'Object address corresponding to the identifier in the Guard table';
44
44
  export const ObjectQuery_Description = 'Object to query';
45
- export const QueryId_Description = `query ID. SSE: wowok://guard_queries/`;
45
+ export const QueryId_Description = `Guard query by the ID specified`;
46
+ export const QueryName_Description = 'Guard query by the Module and function Name specified';
46
47
  export const GuardNodeParams_Description = `Query parameters are sourced from their respective child nodes, with child node return types matching the parameter data types`;
47
48
  export const GuardQuery_Description = 'Data from querying the on-chain object'
48
49
  export const GuardLogic_Description = `Logical operators for parameters`;
@@ -3,28 +3,10 @@ import { z } from "zod";
3
3
  import { GetMarkNameSchema, AccountOrMarkNameSchema } from "./call.js";
4
4
  import { zodToJsonSchema } from "zod-to-json-schema";
5
5
  import { GUARD_QUERIES, PermissionInfo } from "wowok";
6
+ import { GuardQueryModules, PermissionModules } from "./util.js";
6
7
 
7
8
  export const QueryWowokProtocolSchemaDescription = `Retrieves the Wowok protocol data`;
8
9
 
9
- const PermissionModules = () : string[] => {
10
- const ret : string[] = [];
11
- PermissionInfo.forEach(v => {
12
- if (!ret.find(i => i === v.module)) {
13
- ret.push(v.module);
14
- }
15
- });
16
- return ret;
17
- }
18
- const GuardQueryModules = () : string[] => {
19
- const ret : string[] = [];
20
- GUARD_QUERIES.forEach(v => {
21
- if (!ret.find(i => i === v.module)) {
22
- ret.push(v.module);
23
- }
24
- });
25
- return ret;
26
- }
27
-
28
10
  export const BuiltInPermissionSchema = z.object({
29
11
  module: z.union([z.array(z.enum(PermissionModules() as [string, ...string[]])).describe("Modules of the built-in permissions"), z.literal('all').describe('All modules')]),
30
12
  }).describe("Built-in permissions within the modules of the Wowok protocol");
@@ -96,4 +96,23 @@ export const ObjectOperationResult = (r: CallResult) : string => {
96
96
  }
97
97
  })
98
98
  return JSON.stringify({objects:output, raw_data:r})
99
+ }
100
+
101
+ export const PermissionModules = () : string[] => {
102
+ const ret : string[] = [];
103
+ WOWOK.PermissionInfo.forEach(v => {
104
+ if (!ret.find(i => i === v.module)) {
105
+ ret.push(v.module);
106
+ }
107
+ });
108
+ return ret;
109
+ }
110
+ export const GuardQueryModules = () : string[] => {
111
+ const ret : string[] = [];
112
+ WOWOK.GUARD_QUERIES.forEach(v => {
113
+ if (!ret.find(i => i === v.module)) {
114
+ ret.push(v.module);
115
+ }
116
+ });
117
+ return ret;
99
118
  }