wowok_agent 0.0.1 → 0.1.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/README.md CHANGED
@@ -16,8 +16,8 @@ $ npm i wowok_agent@latest
16
16
  The protocol address may change due to protocol upgrade. Please update the SDK to the latest version.
17
17
 
18
18
  ### Fetures
19
- ##### Operation account generation and management.
20
- ##### Integrate queries and operations.
21
- ##### Simplifies and supports JSON-driven operations.
19
+ - *Operation account generation and management.*
20
+ - *Integrate queries and operations.*
21
+ - *Simplifies and supports JSON-driven operations.*
22
22
 
23
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wowok_agent",
3
- "version": "0.0.1",
3
+ "version": "0.1.3",
4
4
  "description": "Create, collaborate, and transact on your own terms with the AI-driven web3 collaboration protocol.",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
package/src/account.ts CHANGED
@@ -2,13 +2,19 @@ import * as fs from 'fs';
2
2
  import * as path from 'path';
3
3
  import * as os from 'os';
4
4
  import { Ed25519Keypair, fromHEX, toHEX, decodeSuiPrivateKey } from 'wowok';
5
-
5
+ import { getFaucetHost, requestSuiFromFaucetV0, requestSuiFromFaucetV1 } from 'wowok';
6
6
  export interface AccountData {
7
7
  name: string;
8
8
  default?: boolean;
9
9
  key: string;
10
10
  }
11
11
 
12
+ export interface AccountData_Show {
13
+ name: string;
14
+ default?: boolean;
15
+ address: string;
16
+ }
17
+
12
18
  const Account_FileName = 'wowok.dat';
13
19
  const Account_Key = 'wowok-data-v1';
14
20
 
@@ -184,15 +190,31 @@ export class Account {
184
190
  }
185
191
  }
186
192
 
187
- list() : AccountData[] {
193
+ list() : AccountData_Show[] {
188
194
  try {
189
195
  if (this.storage === 'File') {
190
196
  const filePath = path.join(os.homedir(), Account_FileName);
191
- return JSON.parse(fs.readFileSync(filePath, 'utf-8')) as AccountData[]
197
+ const a = JSON.parse(fs.readFileSync(filePath, 'utf-8')) as AccountData[];
198
+ return a.map(v => {
199
+ return {name:v.name, default:v?.default, address:Ed25519Keypair.fromSecretKey(fromHEX(v.key)).getPublicKey().toSuiAddress()}
200
+ })
192
201
  } else if (this.storage === 'Explorer') {
193
- return JSON.parse(localStorage.getItem(Account_Key) ?? '') as AccountData[];
202
+ const a = JSON.parse(localStorage.getItem(Account_Key) ?? '') as AccountData[];
203
+ return a.map(v => {
204
+ return {name:v.name, default:v?.default, address:Ed25519Keypair.fromSecretKey(fromHEX(v.key)).getPublicKey().toSuiAddress()}
205
+ })
194
206
  }
195
207
  } catch (e) { console.log(e) }
196
208
  return []
197
209
  }
210
+
211
+ faucet(name?:string) {
212
+ const address = this.get_address(name, true);
213
+ if (address) {
214
+ requestSuiFromFaucetV0({host:getFaucetHost('testnet'), recipient:address}).catch(e => {
215
+ console.log(e)
216
+ requestSuiFromFaucetV1({host:getFaucetHost('testnet'), recipient:address}).catch(e => console.log(e));
217
+ })
218
+ }
219
+ }
198
220
  }
@@ -1,12 +1,14 @@
1
- import { TransactionBlock, CallResponse} from 'wowok';
1
+ import { TransactionBlock, CallResponse, IsValidArgType} from 'wowok';
2
2
  import { PassportObject, IsValidAddress, Errors, ERROR, Permission, PermissionIndex, PermissionIndexType, Treasury,
3
3
  Arbitration, Dispute, Feedback, Vote, VotingGuard, WithdrawFee, WitnessFill
4
4
  } from 'wowok';
5
- import { OBJECT_QUERY, ObjectArbitration, } from '../objects';
6
- import { CallBase } from "./call";
5
+ import { query_objects, ObjectArbitration, } from '../objects';
6
+ import { CallBase, CallResult } from "./base";
7
7
 
8
- export class CallArbitration extends CallBase {
9
- type_parameter: string;
8
+ export interface CallArbitration_Data {
9
+ object?: string; // undefined for creating a new object
10
+ permission?: string;
11
+ type_parameter?: string;
10
12
  permission_new?: string;
11
13
  description?: string;
12
14
  bPaused?: boolean;
@@ -19,120 +21,133 @@ export class CallArbitration extends CallBase {
19
21
  arb_withdraw_fee?: {arb:string; data:WithdrawFee};
20
22
  arb_vote?: Vote;
21
23
  arb_arbitration?: Feedback;
22
- constructor(type_parameter:string, object: string | 'new' = 'new') {
23
- super(object)
24
- this.type_parameter = type_parameter;
24
+ }
25
+ export class CallArbitration extends CallBase {
26
+ data: CallArbitration_Data;
27
+ constructor (data: CallArbitration_Data) {
28
+ super();
29
+ this.data = data;
25
30
  }
26
- async call(account?:string) : Promise<WitnessFill[] | CallResponse | undefined> {
27
- if (!this.type_parameter) ERROR(Errors.InvalidParam, 'type_parameter')
31
+
32
+ async call(account?:string) : Promise<CallResult> {
33
+ if (!this.data?.type_parameter || !IsValidArgType(this.data.type_parameter)) {
34
+ ERROR(Errors.IsValidArgType, 'arbitration.type_parameter')
35
+ }
28
36
 
29
37
  var checkOwner = false; const guards : string[] = [];
30
38
  const perms : PermissionIndexType[] = []; var obj: ObjectArbitration | undefined ;
31
39
 
32
- if (this?.permission && IsValidAddress(this.permission)) {
33
- if (this?.object === 'new') {
40
+ if (this.data?.permission && IsValidAddress(this.data.permission)) {
41
+ if (!this.data?.object) {
34
42
  perms.push(PermissionIndex.arbitration)
35
43
  }
36
- if (this?.permission_new !== undefined) {
44
+ if (this.data?.permission_new !== undefined) {
37
45
  checkOwner = true;
38
46
  }
39
- if (this?.description !== undefined && this.object !== 'new') {
47
+ if (this.data?.description !== undefined && this.data.object) {
40
48
  perms.push(PermissionIndex.arbitration_description)
41
49
  }
42
- if (this?.bPaused !== undefined) {
50
+ if (this.data?.bPaused !== undefined) {
43
51
  perms.push(PermissionIndex.arbitration_pause)
44
52
  }
45
- if (this?.endpoint == undefined) { // publish is an irreversible one-time operation
53
+ if (this.data?.endpoint == undefined) { // publish is an irreversible one-time operation
46
54
  perms.push(PermissionIndex.arbitration_endpoint)
47
55
  }
48
- if (this?.fee !== undefined && this.object !== 'new') {
56
+ if (this.data?.fee !== undefined && this.data.object) {
49
57
  perms.push(PermissionIndex.arbitration_fee)
50
58
  }
51
- if (this?.fee_treasury !== undefined && this.object !== 'new') {
59
+ if (this.data?.fee_treasury !== undefined && this.data.object) {
52
60
  perms.push(PermissionIndex.arbitration_treasury)
53
61
  }
54
- if (this?.usage_guard !== undefined) {
62
+ if (this.data?.usage_guard !== undefined) {
55
63
  perms.push(PermissionIndex.arbitration_guard)
56
64
  }
57
- if (this?.voting_guard !== undefined) {
65
+ if (this.data?.voting_guard !== undefined) {
58
66
  perms.push(PermissionIndex.arbitration_voting_guard)
59
67
  }
60
- if (this?.arb_arbitration !== undefined) {
68
+ if (this.data?.arb_arbitration !== undefined) {
61
69
  perms.push(PermissionIndex.arbitration_arbitration)
62
70
  }
63
- if (this?.arb_new?.guard !== undefined) {
64
- if (IsValidAddress(this.arb_new.guard)) {
65
- guards.push(this.arb_new.guard)
71
+ if (this.data?.arb_new?.guard !== undefined) {
72
+ if (IsValidAddress(this.data.arb_new.guard)) {
73
+ guards.push(this.data.arb_new.guard)
66
74
  } else {
67
- if (!obj) {
68
- const r = await OBJECT_QUERY.objects({objects:[this.object], showContent:true});
69
- if (r?.objects && r.objects[0].type === 'Treasury') {
70
- obj = r.objects[0] as ObjectArbitration;
75
+ if (!this.data.object) { // new
76
+ if (this.data?.usage_guard && IsValidAddress(this.data?.usage_guard)) {
77
+ guards.push(this.data.usage_guard)
71
78
  }
72
- }
73
- if (obj?.usage_guard) {
74
- guards.push(obj?.usage_guard)
79
+ } else {
80
+ if (!obj) {
81
+ const r = await query_objects({objects:[this.data.object!], showContent:true});
82
+ if (r?.objects && r.objects[0].type === 'Arbitration') {
83
+ obj = r.objects[0] as ObjectArbitration;
84
+ }
85
+ }
86
+
87
+ if (obj?.usage_guard) {
88
+ guards.push(obj.usage_guard)
89
+ }
75
90
  }
76
91
  }
77
92
  }
78
- if (this?.arb_vote !== undefined) {
93
+ if (this.data?.arb_vote !== undefined) {
79
94
  perms.push(PermissionIndex.treasury_receive)
80
95
  }
81
- if (typeof(this?.arb_vote?.voting_guard) === 'string' && IsValidAddress(this?.arb_vote?.voting_guard)) {
82
- guards.push(this?.arb_vote?.voting_guard)
96
+ if (typeof(this.data?.arb_vote?.voting_guard) === 'string' && IsValidAddress(this.data?.arb_vote?.voting_guard)) {
97
+ guards.push(this.data?.arb_vote?.voting_guard)
83
98
  }
84
99
 
85
- return await this.check_permission_and_call(this.permission, perms, guards, checkOwner, undefined, account)
100
+ return await this.check_permission_and_call(this.data.permission, perms, guards, checkOwner, undefined, account)
86
101
  }
87
102
  return this.exec(account);
88
103
  }
89
104
  protected async operate(txb:TransactionBlock, passport?:PassportObject) {
90
105
  let obj : Arbitration | undefined ; let permission: any; let withdraw_treasury:any;
91
- if (this.object === 'new' && this?.type_parameter) {
92
- if (!this?.permission || !IsValidAddress(this?.permission)) {
106
+ if (!this.data.object) {
107
+ if (!this.data?.permission || !IsValidAddress(this.data?.permission)) {
93
108
  permission = Permission.New(txb, '');
94
109
  }
95
- if (!this?.fee_treasury || !IsValidAddress(this?.fee_treasury)) {
96
- withdraw_treasury = Treasury.New(txb, this?.type_parameter, permission ?? this?.permission, '', permission?undefined:passport);
110
+ if (!this.data?.fee_treasury || !IsValidAddress(this.data?.fee_treasury)) {
111
+ withdraw_treasury = Treasury.New(txb, this.data?.type_parameter!, permission ?? this.data?.permission, '', permission?undefined:passport);
97
112
  }
98
- obj = Arbitration.New(txb, this.type_parameter, permission ?? this?.permission, this?.description??'',
99
- BigInt(this?.fee ?? 0), withdraw_treasury??this.fee_treasury, permission?undefined:passport);
100
- } else {
101
- if (IsValidAddress(this.object) && this.type_parameter && this.permission && IsValidAddress(this?.permission)) {
102
- obj = Arbitration.From(txb, this.type_parameter, this.permission, this.object)
113
+ obj = Arbitration.New(txb, this.data.type_parameter!, permission ?? this.data?.permission, this.data?.description??'',
114
+ BigInt(this.data?.fee ?? 0), withdraw_treasury??this.data.fee_treasury, permission?undefined:passport);
115
+ } else if (this.data.object) {
116
+ if (IsValidAddress(this.data.object) && this.data.type_parameter && this.data.permission && IsValidAddress(this.data?.permission)) {
117
+ obj = Arbitration.From(txb, this.data.type_parameter, this.data.permission, this.data.object)
103
118
  }
104
119
  }
105
120
 
106
121
  if (obj) {
107
- if (this?.description !== undefined && this.object !== 'new') {
108
- obj?.set_description(this.description, passport);
122
+ if (this.data?.description !== undefined && this.data.object) {
123
+ obj?.set_description(this.data.description, passport);
109
124
  }
110
- if (this?.bPaused !== undefined) {
111
- obj?.pause(this.bPaused, passport);
125
+ if (this.data?.bPaused !== undefined) {
126
+ obj?.pause(this.data.bPaused, passport);
112
127
  }
113
- if (this?.endpoint !== undefined) {
114
- obj?.set_endpoint(this.endpoint, passport)
128
+ if (this.data?.endpoint !== undefined) {
129
+ obj?.set_endpoint(this.data.endpoint, passport)
115
130
  }
116
- if (this?.fee !== undefined && this.object !== 'new') {
117
- obj?.set_fee(BigInt(this.fee), passport)
131
+ if (this.data?.fee !== undefined && this.data.object) {
132
+ obj?.set_fee(BigInt(this.data.fee), passport)
118
133
  }
119
- if (this.fee_treasury !== undefined && this.object !== 'new') {
120
- obj?.set_withdrawTreasury(this.fee_treasury, passport)
134
+ if (this.data.fee_treasury !== undefined && this.data.object) {
135
+ obj?.set_withdrawTreasury(this.data.fee_treasury, passport)
121
136
  }
122
- if (this.usage_guard !== undefined) {
123
- obj?.set_guard(this.usage_guard, passport)
137
+ if (this.data.usage_guard !== undefined) {
138
+ obj?.set_guard(this.data.usage_guard, passport)
124
139
  }
125
- if (this?.voting_guard !== undefined) {
126
- switch (this.voting_guard.op) {
140
+ if (this.data?.voting_guard !== undefined) {
141
+ switch (this.data.voting_guard.op) {
127
142
  case 'add':
128
- obj?.add_voting_guard(this.voting_guard.data, passport)
143
+ obj?.add_voting_guard(this.data.voting_guard.data, passport)
129
144
  break;
130
145
  case 'remove':
131
- obj?.remove_voting_guard(this.voting_guard.guards, false, passport)
146
+ obj?.remove_voting_guard(this.data.voting_guard.guards, false, passport)
132
147
  break;
133
148
  case 'set':
134
149
  obj?.remove_voting_guard([], true, passport)
135
- obj?.add_voting_guard(this.voting_guard.data, passport)
150
+ obj?.add_voting_guard(this.data.voting_guard.data, passport)
136
151
  break;
137
152
  case 'removeall':
138
153
  obj?.remove_voting_guard([], true, passport)
@@ -140,20 +155,20 @@ export class CallArbitration extends CallBase {
140
155
  }
141
156
  }
142
157
 
143
- if (this?.arb_new !== undefined) {
144
- obj?.dispute(this.arb_new.data, passport)
158
+ if (this.data?.arb_new !== undefined) {
159
+ obj?.dispute(this.data.arb_new.data, passport)
145
160
  }
146
- if (this?.arb_arbitration !== undefined) {
147
- obj?.arbitration(this.arb_arbitration, passport)
161
+ if (this.data?.arb_arbitration !== undefined) {
162
+ obj?.arbitration(this.data.arb_arbitration, passport)
148
163
  }
149
- if (this?.arb_vote !== undefined) {
150
- obj?.vote(this.arb_vote, passport)
164
+ if (this.data?.arb_vote !== undefined) {
165
+ obj?.vote(this.data.arb_vote, passport)
151
166
  }
152
- if (this?.arb_withdraw_fee !== undefined) {
153
- obj?.withdraw_fee(this.arb_withdraw_fee.arb, this.arb_withdraw_fee.data, passport)
167
+ if (this.data?.arb_withdraw_fee !== undefined) {
168
+ obj?.withdraw_fee(this.data.arb_withdraw_fee.arb, this.data.arb_withdraw_fee.data, passport)
154
169
  }
155
- if (this?.permission_new !== undefined) {
156
- obj?.change_permission(this.permission_new);
170
+ if (this.data?.permission_new !== undefined) {
171
+ obj?.change_permission(this.data.permission_new);
157
172
  }
158
173
  if (withdraw_treasury) {
159
174
  withdraw_treasury.launch();
@@ -161,7 +176,7 @@ export class CallArbitration extends CallBase {
161
176
  if (permission) {
162
177
  permission.launch();
163
178
  }
164
- if (this.object === 'new') {
179
+ if (!this.data.object) {
165
180
  obj?.launch();
166
181
  }
167
182
  }
@@ -1,14 +1,10 @@
1
- /**
2
- * Provide a this interface for AI
3
- * Operation sequence Priority: common operation > Guard change > permission change
4
- * Recommended: Changes to guard and permission are committed on-chain separately to avoid permission dependencies for other operations.
5
- */
6
1
 
7
- import { Protocol, TransactionBlock, CallResponse} from 'wowok';
2
+
3
+ import { Protocol, TransactionBlock, CallResponse, Guard} from 'wowok';
8
4
  import { PassportObject, Errors, ERROR, Permission,
9
5
  PermissionIndexType, GuardParser, Passport, WitnessFill
10
6
  } from 'wowok';
11
- import { PERMISSION_QUERY } from '../permission';
7
+ import { query_permission } from '../permission';
12
8
  import { Account } from '../account';
13
9
  import { ObjectBase } from '../objects';
14
10
 
@@ -17,27 +13,46 @@ export interface ResponseData extends ObjectBase {
17
13
  change:'created' | 'mutated' | string;
18
14
  }
19
15
 
16
+ export interface GuardInfo_forCall {
17
+ guard: string[];
18
+ witness: WitnessFill[];
19
+ }
20
+
21
+ export interface CallWithWitnessParam {
22
+ info: GuardInfo_forCall;
23
+ account?:string;
24
+ }
25
+ export type CallResult = GuardInfo_forCall | CallResponse | undefined;
26
+
27
+ export function ResponseData(response: CallResponse | undefined ) : ResponseData[] {
28
+ const res : ResponseData[] = [];
29
+ response?.objectChanges?.forEach(v => {
30
+ const type_raw: string | undefined = (v as any)?.objectType;
31
+ const type:string | undefined = type_raw ? Protocol.Instance().object_name_from_type_repr(type_raw) : undefined;
32
+ if (type) {
33
+ res.push({type:type, type_raw:type_raw, object:(v as any)?.objectId, version:(v as any)?.version,
34
+ owner:(v as any)?.owner, change:v.type
35
+ })
36
+ }
37
+ })
38
+ return res;
39
+ }
20
40
  export class CallBase {
21
- object: string | 'new' ;
22
- permission?: string;
23
- constructor(object: string | 'new' = 'new') {
24
- this.object = object;
25
- }
26
41
  // operation implementation for a call
27
42
  protected async operate(txb:TransactionBlock, passport?:PassportObject) {};
28
-
43
+ constructor () {}
29
44
  // return WitnessFill to resolve filling witness, and than 'call_with_witness' to complete the call;
30
45
  // return ResponseData when the call has completed;
31
46
  // throw an exception when errors.
32
- async call(account?:string) : Promise<WitnessFill[] | CallResponse | undefined> { return undefined };
33
- async call_with_witness (guards: string[], fill?: WitnessFill[], account?:string) : Promise<CallResponse | undefined> {
34
- if (guards.length > 0) { // prepare passport
35
- const p: GuardParser | undefined = await GuardParser.Create([...guards]);
36
- const pair = Account.Instance().get_pair(account, true);
47
+ async call(account?:string) : Promise<CallResult> { return undefined };
48
+ async call_with_witness (param: CallWithWitnessParam) : Promise<CallResponse | undefined> {
49
+ if (param.info.guard.length > 0) { // prepare passport
50
+ const p: GuardParser | undefined = await GuardParser.Create([...param.info.guard]);
51
+ const pair = Account.Instance().get_pair(param.account, true);
37
52
  if (!pair) ERROR(Errors.Fail, 'account invalid')
38
53
 
39
54
  if (p) {
40
- const query = await p.done(fill);
55
+ const query = await p.done(param.info.witness);
41
56
  if (query) {
42
57
  const txb = new TransactionBlock();
43
58
  const passport = new Passport(txb, query!);
@@ -57,13 +72,13 @@ export class CallBase {
57
72
  }
58
73
 
59
74
  protected async check_permission_and_call (permission:string, permIndex: PermissionIndexType[], guards_needed: string[],
60
- checkOwner?:boolean, checkAdmin?:boolean, account?:string) : Promise<WitnessFill[] | CallResponse> {
75
+ checkOwner?:boolean, checkAdmin?:boolean, account?:string) : Promise<CallResult> {
61
76
  var guards : string[] = [];
62
77
  const pair = Account.Instance().get_pair(account, true);
63
78
  if (!pair) ERROR(Errors.Fail, 'account invalid')
64
79
 
65
80
  if (permIndex.length > 0 || checkOwner) {
66
- const p = await PERMISSION_QUERY.permission({permission_object:permission, address:pair!.toSuiAddress()});
81
+ const p = await query_permission({permission_object:permission, address:pair!.toSuiAddress()});
67
82
  if (checkOwner && !p.owner) ERROR(Errors.noPermission, 'owner');
68
83
  if (checkAdmin && !p.admin) ERROR(Errors.noPermission, 'admin');
69
84
 
@@ -99,7 +114,7 @@ export class CallBase {
99
114
  }
100
115
  }
101
116
 
102
- return p!.future_fills();
117
+ return {guard:[...guards], witness:p!.future_fills()};
103
118
  } else { // no passport needed
104
119
  return this.exec()
105
120
  }
@@ -116,18 +131,4 @@ export class CallBase {
116
131
  options:{showObjectChanges:true},
117
132
  });
118
133
  }
119
-
120
- static ResponseData(response: CallResponse | undefined ) : ResponseData[] {
121
- const res : ResponseData[] = [];
122
- response?.objectChanges?.forEach(v => {
123
- const type_raw: string | undefined = (v as any)?.objectType;
124
- const type:string | undefined = type_raw ? Protocol.Instance().object_name_from_type_repr(type_raw) : undefined;
125
- if (type) {
126
- res.push({type:type, type_raw:type_raw, object:(v as any)?.objectId, version:(v as any)?.version,
127
- owner:(v as any)?.owner, change:v.type
128
- })
129
- }
130
- })
131
- return res;
132
- }
133
134
  }