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 +3 -3
- package/package.json +1 -1
- package/src/account.ts +26 -4
- package/src/call/arbitration.ts +87 -72
- package/src/call/{call.ts → base.ts} +37 -36
- package/src/call/demand.ts +73 -59
- package/src/call/guard.ts +264 -0
- package/src/call/machine.ts +85 -77
- package/src/call/permission.ts +42 -34
- package/src/call/personal.ts +38 -30
- package/src/call/repository.ts +52 -43
- package/src/call/service.ts +140 -126
- package/src/call/treasury.ts +76 -60
- package/src/call.ts +70 -0
- package/src/index.ts +3 -1
- package/src/objects.ts +338 -338
- package/src/permission.ts +33 -34
package/src/call/demand.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { TransactionBlock, CallResponse} from 'wowok';
|
|
1
|
+
import { TransactionBlock, CallResponse, IsValidArgType} from 'wowok';
|
|
2
2
|
import { PassportObject, IsValidAddress, Errors, ERROR, Permission, Permission_Entity, Permission_Index, PermissionIndex, UserDefinedIndex,
|
|
3
3
|
PermissionIndexType, Demand, WitnessFill
|
|
4
4
|
} from 'wowok';
|
|
5
|
-
import {
|
|
6
|
-
import { CallBase } from "./
|
|
5
|
+
import { query_objects, ObjectDemand } from '../objects';
|
|
6
|
+
import { CallBase, CallResult } from "./base";
|
|
7
7
|
|
|
8
|
-
export
|
|
8
|
+
export interface CallDemand_Data {
|
|
9
|
+
object?: string; // undefined for creating a new object
|
|
10
|
+
permission?: string;
|
|
9
11
|
permission_new?: string;
|
|
10
|
-
type_parameter
|
|
12
|
+
type_parameter?: string;
|
|
11
13
|
guard?: {address:string; service_id_in_guard?:number};
|
|
12
14
|
description?: string;
|
|
13
15
|
time_expire?: {op: 'duration'; minutes:number} | {op:'set'; time:number};
|
|
@@ -15,117 +17,129 @@ export class CallDemand extends CallBase {
|
|
|
15
17
|
present?: {service: string | number; recommend_words:string; service_pay_type:string, guard?:string | 'fetch'}; // guard is the present guard of Demand
|
|
16
18
|
reward?: string; // rerward the service
|
|
17
19
|
refund?: boolean;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
}
|
|
21
|
+
export class CallDemand extends CallBase {
|
|
22
|
+
data: CallDemand_Data;
|
|
23
|
+
constructor(data: CallDemand_Data) {
|
|
24
|
+
super();
|
|
25
|
+
this.data = data;
|
|
21
26
|
}
|
|
22
|
-
async call(account?:string) : Promise<
|
|
23
|
-
if (!this.type_parameter
|
|
27
|
+
async call(account?:string) : Promise<CallResult> {
|
|
28
|
+
if (!this.data?.type_parameter || !IsValidArgType(this.data.type_parameter)) {
|
|
29
|
+
ERROR(Errors.IsValidArgType, 'demand.type_parameter')
|
|
30
|
+
}
|
|
31
|
+
|
|
24
32
|
var checkOwner = false; const guards : string[] = [];
|
|
25
33
|
const perms : PermissionIndexType[] = [];
|
|
26
34
|
|
|
27
|
-
if (this?.permission && IsValidAddress(this.permission)) {
|
|
28
|
-
if (this?.object
|
|
35
|
+
if (this.data?.permission && IsValidAddress(this.data.permission)) {
|
|
36
|
+
if (!this.data?.object) {
|
|
29
37
|
perms.push(PermissionIndex.demand)
|
|
30
38
|
}
|
|
31
|
-
if (this?.permission_new !== undefined) {
|
|
39
|
+
if (this.data?.permission_new !== undefined) {
|
|
32
40
|
checkOwner = true;
|
|
33
41
|
}
|
|
34
|
-
if (this?.description !== undefined && this.object
|
|
42
|
+
if (this.data?.description !== undefined && this.data.object) {
|
|
35
43
|
perms.push(PermissionIndex.demand_description)
|
|
36
44
|
}
|
|
37
|
-
if (this?.time_expire !== undefined && this.object
|
|
45
|
+
if (this.data?.time_expire !== undefined && this.data.object) {
|
|
38
46
|
perms.push(PermissionIndex.demand_expand_time)
|
|
39
47
|
}
|
|
40
|
-
if (this?.guard !== undefined) {
|
|
48
|
+
if (this.data?.guard !== undefined) {
|
|
41
49
|
perms.push(PermissionIndex.demand_guard)
|
|
42
50
|
}
|
|
43
|
-
if (this?.reward !== undefined) {
|
|
51
|
+
if (this.data?.reward !== undefined) {
|
|
44
52
|
perms.push(PermissionIndex.demand_yes)
|
|
45
53
|
}
|
|
46
|
-
if (this?.refund) {
|
|
54
|
+
if (this.data?.refund) {
|
|
47
55
|
perms.push(PermissionIndex.demand_refund)
|
|
48
56
|
}
|
|
49
|
-
if (this?.present?.guard !== undefined) {
|
|
50
|
-
if (IsValidAddress(this.present.guard)) {
|
|
51
|
-
guards.push(this.present.guard)
|
|
52
|
-
} else
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
if (this.data?.present?.guard !== undefined) {
|
|
58
|
+
if (IsValidAddress(this.data.present.guard)) {
|
|
59
|
+
guards.push(this.data.present.guard)
|
|
60
|
+
} else {
|
|
61
|
+
if (!this.data.object) { // new
|
|
62
|
+
if (this.data?.guard?.address && IsValidAddress(this.data?.guard.address)) {
|
|
63
|
+
guards.push(this.data.guard.address)
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
const r = await query_objects({objects:[this.data.object], showContent:true});
|
|
67
|
+
if (r?.objects && r?.objects[0]?.type === 'Demand') {
|
|
68
|
+
const obj = (r?.objects[0] as ObjectDemand);
|
|
69
|
+
if (obj?.guard) {
|
|
70
|
+
guards.push(obj?.guard.object);
|
|
71
|
+
}
|
|
58
72
|
}
|
|
59
73
|
}
|
|
60
74
|
}
|
|
61
75
|
}
|
|
62
|
-
return await this.check_permission_and_call(this.permission, perms, guards, checkOwner, undefined, account)
|
|
76
|
+
return await this.check_permission_and_call(this.data.permission, perms, guards, checkOwner, undefined, account)
|
|
63
77
|
}
|
|
64
78
|
return this.exec(account);
|
|
65
79
|
}
|
|
66
80
|
protected async operate(txb:TransactionBlock, passport?:PassportObject) {
|
|
67
81
|
let obj : Demand | undefined ; let permission: any;
|
|
68
82
|
|
|
69
|
-
if (this.object
|
|
70
|
-
if (!this?.permission || !IsValidAddress(this?.permission)) {
|
|
83
|
+
if (!this.data.object) {
|
|
84
|
+
if (!this.data?.permission || !IsValidAddress(this.data?.permission)) {
|
|
71
85
|
permission = Permission.New(txb, '');
|
|
72
86
|
}
|
|
73
87
|
|
|
74
|
-
if (this.time_expire !== undefined) {
|
|
75
|
-
obj = Demand.New(txb, this.type_parameter
|
|
76
|
-
this.time_expire?.op === 'duration' ? this.time_expire.minutes : this.time_expire?.time,
|
|
77
|
-
permission ? permission.get_object(): this?.permission, this?.description??'', permission?undefined:passport)
|
|
88
|
+
if (this.data.time_expire !== undefined) {
|
|
89
|
+
obj = Demand.New(txb, this.data.type_parameter!, this.data.time_expire?.op === 'duration' ? true : false,
|
|
90
|
+
this.data.time_expire?.op === 'duration' ? this.data.time_expire.minutes : this.data.time_expire?.time,
|
|
91
|
+
permission ? permission.get_object(): this.data?.permission, this.data?.description??'', permission?undefined:passport)
|
|
78
92
|
} else {
|
|
79
|
-
obj = Demand.New(txb, this.type_parameter
|
|
80
|
-
permission ? permission.get_object(): this?.permission, this?.description??'', permission?undefined:passport)
|
|
93
|
+
obj = Demand.New(txb, this.data.type_parameter!, true, 30*24*60, // 30days default
|
|
94
|
+
permission ? permission.get_object(): this.data?.permission, this.data?.description??'', permission?undefined:passport)
|
|
81
95
|
}
|
|
82
96
|
} else {
|
|
83
|
-
if (IsValidAddress(this.object) && this.type_parameter && this.permission && IsValidAddress(this?.permission)) {
|
|
84
|
-
obj = Demand.From(txb, this.type_parameter, this.permission, this.object)
|
|
97
|
+
if (IsValidAddress(this.data.object) && this.data.type_parameter && this.data.permission && IsValidAddress(this.data?.permission)) {
|
|
98
|
+
obj = Demand.From(txb, this.data.type_parameter, this.data.permission, this.data.object)
|
|
85
99
|
}
|
|
86
100
|
}
|
|
87
101
|
|
|
88
102
|
if (obj) {
|
|
89
|
-
if (this?.description !== undefined && this.object
|
|
90
|
-
obj?.set_description(this.description, passport);
|
|
103
|
+
if (this.data?.description !== undefined && this.data.object) {
|
|
104
|
+
obj?.set_description(this.data.description, passport);
|
|
91
105
|
}
|
|
92
|
-
if (this?.time_expire !== undefined && this.object
|
|
93
|
-
obj?.expand_time(this.time_expire.op === 'duration' ? true : false,
|
|
94
|
-
this.time_expire.op === 'duration' ? this.time_expire.minutes : this.time_expire.time, passport)
|
|
106
|
+
if (this.data?.time_expire !== undefined && this.data.object) {
|
|
107
|
+
obj?.expand_time(this.data.time_expire.op === 'duration' ? true : false,
|
|
108
|
+
this.data.time_expire.op === 'duration' ? this.data.time_expire.minutes : this.data.time_expire.time, passport)
|
|
95
109
|
}
|
|
96
|
-
if (this?.bounty !== undefined) {
|
|
97
|
-
if (this.bounty.op === 'add') {
|
|
98
|
-
let deposit : any | undefined; let b = BigInt(this.bounty.balance);
|
|
110
|
+
if (this.data?.bounty !== undefined) {
|
|
111
|
+
if (this.data.bounty.op === 'add') {
|
|
112
|
+
let deposit : any | undefined; let b = BigInt(this.data.bounty.balance);
|
|
99
113
|
if (b > BigInt(0)) {
|
|
100
|
-
if (this.type_parameter === '0x2::sui::SUI' || this.type_parameter === '0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI') {
|
|
114
|
+
if (this.data.type_parameter === '0x2::sui::SUI' || this.data.type_parameter === '0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI') {
|
|
101
115
|
deposit = txb.splitCoins(txb.gas, [b])[0];
|
|
102
|
-
} else if (this?.bounty?.object) {
|
|
103
|
-
deposit = txb.splitCoins(this.bounty.object, [b])[0];
|
|
116
|
+
} else if (this.data?.bounty?.object) {
|
|
117
|
+
deposit = txb.splitCoins(this.data.bounty.object, [b])[0];
|
|
104
118
|
}
|
|
105
119
|
if (deposit) {
|
|
106
120
|
obj?.deposit(deposit);
|
|
107
121
|
}
|
|
108
122
|
}
|
|
109
|
-
} else if (this.bounty.op === 'refund') {
|
|
123
|
+
} else if (this.data.bounty.op === 'refund') {
|
|
110
124
|
obj?.refund(passport);
|
|
111
|
-
} else if (this.bounty.op === 'reward') {
|
|
112
|
-
obj?.yes(this.bounty.service, passport);
|
|
125
|
+
} else if (this.data.bounty.op === 'reward') {
|
|
126
|
+
obj?.yes(this.data.bounty.service, passport);
|
|
113
127
|
}
|
|
114
128
|
}
|
|
115
|
-
if (this?.present !== undefined) {
|
|
129
|
+
if (this.data?.present !== undefined) {
|
|
116
130
|
//@ demand guard and its passport, if set
|
|
117
|
-
obj?.present(this.present.service, this.present.service_pay_type, this.present.recommend_words, passport);
|
|
131
|
+
obj?.present(this.data.present.service, this.data.present.service_pay_type, this.data.present.recommend_words, passport);
|
|
118
132
|
}
|
|
119
|
-
if (this?.guard !== undefined) {
|
|
120
|
-
obj?.set_guard(this.guard.address, this.guard?.service_id_in_guard ?? undefined, passport)
|
|
133
|
+
if (this.data?.guard !== undefined) {
|
|
134
|
+
obj?.set_guard(this.data.guard.address, this.data.guard?.service_id_in_guard ?? undefined, passport)
|
|
121
135
|
}
|
|
122
|
-
if (this?.permission_new !== undefined ) {
|
|
123
|
-
obj?.change_permission(this.permission_new);
|
|
136
|
+
if (this.data?.permission_new !== undefined ) {
|
|
137
|
+
obj?.change_permission(this.data.permission_new);
|
|
124
138
|
}
|
|
125
139
|
if (permission) {
|
|
126
140
|
permission.launch();
|
|
127
141
|
}
|
|
128
|
-
if (this.object
|
|
142
|
+
if (!this.data.object) {
|
|
129
143
|
obj?.launch();
|
|
130
144
|
}
|
|
131
145
|
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* generate and launch a guard
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Bcs, ContextType, ERROR, Errors, IsValidU8, OperatorType, ValueType, GUARD_QUERIES, IsValidAddress,
|
|
7
|
+
concatenate, TransactionBlock, Protocol, FnCallType, hasDuplicates, insertAtHead, CallResponse,
|
|
8
|
+
IsValidDesription} from "wowok";
|
|
9
|
+
import { Account } from "../account";
|
|
10
|
+
import { CallBase, CallResult } from "./base";
|
|
11
|
+
|
|
12
|
+
export interface GuardConst {
|
|
13
|
+
identifier: number; // 1-255, the same identifier to represent the same data in different nodes
|
|
14
|
+
bWitness: boolean; // witness(verifiee provides while verifying in the future) or normal(verifier provides now)
|
|
15
|
+
value_type: ValueType; // data value type
|
|
16
|
+
value?: any; // if bWitness true, value ignores; otherwise, data.
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// parameters: Child nodes arranged in parameter order, with each child node providing the type and data for that parameter
|
|
20
|
+
export type GuardNode = { identifier: number; } // Data from GuardConst
|
|
21
|
+
| {query: number | string, object: string | number; parameters: GuardNode[];} // object: address string or identifier in GuardConst that value_type = ValueType.address
|
|
22
|
+
| {logic: OperatorType.TYPE_LOGIC_AS_U256_GREATER | OperatorType.TYPE_LOGIC_AS_U256_GREATER_EQUAL
|
|
23
|
+
| OperatorType.TYPE_LOGIC_AS_U256_LESSER | OperatorType.TYPE_LOGIC_AS_U256_LESSER_EQUAL
|
|
24
|
+
| OperatorType.TYPE_LOGIC_AS_U256_EQUAL | OperatorType.TYPE_LOGIC_EQUAL | OperatorType.TYPE_LOGIC_HAS_SUBSTRING
|
|
25
|
+
| OperatorType.TYPE_LOGIC_ALWAYS_TRUE | OperatorType.TYPE_LOGIC_NOT
|
|
26
|
+
| OperatorType.TYPE_LOGIC_AND | OperatorType.TYPE_LOGIC_OR; parameters: GuardNode[];}
|
|
27
|
+
| {calc: OperatorType.TYPE_NUMBER_ADD | OperatorType.TYPE_NUMBER_DEVIDE | OperatorType.TYPE_NUMBER_MOD
|
|
28
|
+
| OperatorType.TYPE_NUMBER_MULTIPLY | OperatorType.TYPE_NUMBER_SUBTRACT; parameters: GuardNode[];}
|
|
29
|
+
| {value_type: ValueType; value:any; } // Data
|
|
30
|
+
| {context: ContextType.TYPE_CLOCK | ContextType.TYPE_GUARD | ContextType.TYPE_SIGNER }; // Data from run-time environment
|
|
31
|
+
|
|
32
|
+
export interface CallGuard_Data {
|
|
33
|
+
description: string;
|
|
34
|
+
table: GuardConst[]; // data used by multiple logical guard nodes
|
|
35
|
+
root: GuardNode; // root must return ValueType.TYPE_BOOL
|
|
36
|
+
}
|
|
37
|
+
export class CallGuard extends CallBase {
|
|
38
|
+
data: CallGuard_Data;
|
|
39
|
+
constructor(data: CallGuard_Data) {
|
|
40
|
+
super();
|
|
41
|
+
this.data = data;
|
|
42
|
+
}
|
|
43
|
+
async call(account?:string) : Promise<CallResult> {
|
|
44
|
+
if (!this.data?.root) {
|
|
45
|
+
ERROR(Errors.InvalidParam, 'guard root node invalid')
|
|
46
|
+
}
|
|
47
|
+
if (!IsValidDesription(this.data?.description)) {
|
|
48
|
+
ERROR(Errors.IsValidDesription, 'build_guard - '+this.data.description)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// check const
|
|
52
|
+
this.data.table.forEach(v => {
|
|
53
|
+
if (!IsValidU8(v.identifier) || v.identifier < 1) ERROR(Errors.InvalidParam, 'table.identifer invalid');
|
|
54
|
+
if (!v.bWitness && v.value === undefined) ERROR(Errors.InvalidParam, 'table.value');
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
if (hasDuplicates(this.data.table.map(v => v.identifier))) {
|
|
58
|
+
ERROR(Errors.InvalidParam, 'table.identifer duplicates')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// check root
|
|
62
|
+
var output : Uint8Array[]= [];
|
|
63
|
+
buildNode(this.data.root!, ValueType.TYPE_BOOL, this.data.table, output);
|
|
64
|
+
const bytes = (concatenate(Uint8Array, ...output) as Uint8Array);
|
|
65
|
+
const txb = new TransactionBlock();
|
|
66
|
+
|
|
67
|
+
console.log(bytes)
|
|
68
|
+
const obj = txb.moveCall({
|
|
69
|
+
target: Protocol.Instance().guardFn('new') as FnCallType,
|
|
70
|
+
arguments: [txb.pure.string(this.data.description), txb.pure.vector('u8', [].slice.call(bytes.reverse()))],
|
|
71
|
+
});
|
|
72
|
+
this.data.table.forEach((v) => {
|
|
73
|
+
if (v.bWitness) {
|
|
74
|
+
const n = new Uint8Array(1); n.set([v.value_type], 0);
|
|
75
|
+
txb.moveCall({
|
|
76
|
+
target:Protocol.Instance().guardFn("constant_add") as FnCallType,
|
|
77
|
+
arguments:[txb.object(obj), txb.pure.u8(v.identifier), txb.pure.bool(true), txb.pure.vector('u8', [].slice.call(n)), txb.pure.bool(false)]
|
|
78
|
+
})
|
|
79
|
+
} else {
|
|
80
|
+
const tmp = Uint8Array.from(Bcs.getInstance().ser(v.value_type, v.value));
|
|
81
|
+
const n = insertAtHead(tmp, v.value_type);
|
|
82
|
+
txb.moveCall({
|
|
83
|
+
target:Protocol.Instance().guardFn("constant_add") as FnCallType,
|
|
84
|
+
arguments:[txb.object(obj), txb.pure.u8(v.identifier), txb.pure.bool(false), txb.pure.vector('u8', [].slice.call(n)), txb.pure.bool(false)]
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
txb.moveCall({
|
|
89
|
+
target:Protocol.Instance().guardFn("create") as FnCallType,
|
|
90
|
+
arguments:[txb.object(obj)]
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const pair = Account.Instance().get_pair(account, true);
|
|
94
|
+
if (!pair) ERROR(Errors.Fail, 'account invalid')
|
|
95
|
+
|
|
96
|
+
return await Protocol.Client().signAndExecuteTransaction({
|
|
97
|
+
transaction: txb,
|
|
98
|
+
signer: pair!,
|
|
99
|
+
options:{showObjectChanges:true},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
//export const MAX_CHILD_NODE_COUNT = 6;
|
|
105
|
+
|
|
106
|
+
const buildNode = (guard_node:GuardNode, type_required:ValueType | 'number' | 'variable', table:GuardConst[], output:Uint8Array[]) => {
|
|
107
|
+
const node: any = guard_node as any;
|
|
108
|
+
if (node?.identifier !== undefined) {
|
|
109
|
+
console.log(node)
|
|
110
|
+
const f = table.find(v=>v.identifier === node.identifier);
|
|
111
|
+
if (f) {
|
|
112
|
+
checkType(f.value_type, type_required, node);
|
|
113
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, ContextType.TYPE_CONSTANT));
|
|
114
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.identifier))
|
|
115
|
+
console.log(2)
|
|
116
|
+
} else {
|
|
117
|
+
ERROR(Errors.InvalidParam, 'node identifier - ' + node.toString());
|
|
118
|
+
}
|
|
119
|
+
} else if (node?.query !== undefined) {
|
|
120
|
+
var q: any[] | undefined;
|
|
121
|
+
if (typeof(node.query === 'string')) {
|
|
122
|
+
q = GUARD_QUERIES.find(v=>v[1] === node.query);
|
|
123
|
+
} else if (typeof(node.query === 'number')) {
|
|
124
|
+
q = GUARD_QUERIES.find(v=>v[2] === node.query);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (q) {
|
|
128
|
+
checkType(q[4], type_required, node); // Return type checking
|
|
129
|
+
if ((q[3]).length === node.parameters.length) {
|
|
130
|
+
for (let i = node.parameters.length - 1; i >= 0; --i) { // stack: first in, last out
|
|
131
|
+
buildNode(node.parameters[i], q[3][i], table, output); // Recursive check
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
ERROR(Errors.InvalidParam, 'node query parameters length not match - ' + node.toString())
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
ERROR(Errors.InvalidParam, 'node query not found - ' + node.toString());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (typeof(node.object) === 'string') {
|
|
141
|
+
if (!IsValidAddress(node.object)) {
|
|
142
|
+
ERROR(Errors.InvalidParam, 'node object from address string - ' + node.toString())
|
|
143
|
+
}
|
|
144
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, ValueType.TYPE_ADDRESS));
|
|
145
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_ADDRESS, node.object)); // object address
|
|
146
|
+
} else {
|
|
147
|
+
const f = table.find(v=>v.identifier === node.object);
|
|
148
|
+
if (f) {
|
|
149
|
+
checkType(f.value_type, ValueType.TYPE_ADDRESS, node);
|
|
150
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, ContextType.TYPE_CONSTANT));
|
|
151
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.object)); // object id
|
|
152
|
+
} else {
|
|
153
|
+
ERROR(Errors.InvalidParam, 'node object from identifier - ' + node.toString());
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} else if (node?.logic !== undefined) {
|
|
157
|
+
checkType(ValueType.TYPE_BOOL, type_required, node); // bool
|
|
158
|
+
switch (node?.logic) {
|
|
159
|
+
case OperatorType.TYPE_LOGIC_ALWAYS_TRUE:
|
|
160
|
+
if (node.parameters.length !== 0) ERROR(Errors.InvalidParam, 'node logic parameters length must be 0'+ node.toString());
|
|
161
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
|
|
162
|
+
break;
|
|
163
|
+
case OperatorType.TYPE_LOGIC_AND:
|
|
164
|
+
case OperatorType.TYPE_LOGIC_OR:
|
|
165
|
+
if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node logic parameters length must >= 2'+ node.toString());
|
|
166
|
+
(node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, ValueType.TYPE_BOOL, table, output)); // reserve
|
|
167
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
|
|
168
|
+
output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
|
|
169
|
+
break;
|
|
170
|
+
case OperatorType.TYPE_LOGIC_NOT:
|
|
171
|
+
if (node.parameters.length !== 1) ERROR(Errors.InvalidParam, 'node logic parameters length must be 1'+ node.toString());
|
|
172
|
+
(node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, ValueType.TYPE_BOOL, table, output)); // reserve
|
|
173
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
|
|
174
|
+
break;
|
|
175
|
+
case OperatorType.TYPE_LOGIC_AS_U256_GREATER:
|
|
176
|
+
case OperatorType.TYPE_LOGIC_AS_U256_GREATER_EQUAL:
|
|
177
|
+
case OperatorType.TYPE_LOGIC_AS_U256_LESSER:
|
|
178
|
+
case OperatorType.TYPE_LOGIC_AS_U256_LESSER_EQUAL:
|
|
179
|
+
case OperatorType.TYPE_LOGIC_AS_U256_EQUAL:
|
|
180
|
+
if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node logic parameters length must >= 2'+ node.toString());
|
|
181
|
+
(node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, ValueType.TYPE_BOOL, table, output));
|
|
182
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
|
|
183
|
+
output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
|
|
184
|
+
break;
|
|
185
|
+
case OperatorType.TYPE_LOGIC_EQUAL:
|
|
186
|
+
if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node logic parameters length must >= 2'+ node.toString());
|
|
187
|
+
var any_type: any = 'variable';
|
|
188
|
+
(node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, any_type, table, output));
|
|
189
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
|
|
190
|
+
output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
|
|
191
|
+
break;
|
|
192
|
+
case OperatorType.TYPE_LOGIC_HAS_SUBSTRING:
|
|
193
|
+
if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node logic parameters length must >= 2'+ node.toString());
|
|
194
|
+
(node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, ValueType.TYPE_BOOL, table, output));
|
|
195
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.logic)); // TYPE
|
|
196
|
+
output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
} else if (node?.calc !== undefined) {
|
|
200
|
+
checkType(ValueType.TYPE_U256, type_required, node);
|
|
201
|
+
if (node.parameters.length < 2) ERROR(Errors.InvalidParam, 'node calc parameters length must >= 2'+ node.toString());
|
|
202
|
+
(node.parameters as GuardNode[]).reverse().forEach(v => buildNode(v, ValueType.TYPE_BOOL, table, output));
|
|
203
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.calc)); // TYPE
|
|
204
|
+
output.push((Bcs.getInstance().ser(ValueType.TYPE_U8, node.parameters.length)));
|
|
205
|
+
} else if (node?.value_type !== undefined) {
|
|
206
|
+
checkType(node?.value_type, type_required, node);
|
|
207
|
+
if (node?.value === undefined) ERROR(Errors.InvalidParam, 'node value undefined - ' + node.toString());
|
|
208
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.value_type)); // TYPE
|
|
209
|
+
|
|
210
|
+
if (node.value_type == ValueType.TYPE_STRING || node.value_type === ValueType.TYPE_VEC_U8) {
|
|
211
|
+
if (typeof(node.value) == 'string') {
|
|
212
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_STRING, node.value));
|
|
213
|
+
} else {
|
|
214
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_VEC_U8, node.value));
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
output.push(Bcs.getInstance().ser(node?.value_type, node.value));
|
|
218
|
+
}
|
|
219
|
+
} else if (node?.context !== undefined) {
|
|
220
|
+
output.push(Bcs.getInstance().ser(ValueType.TYPE_U8, node.context));
|
|
221
|
+
switch (node.context) {
|
|
222
|
+
case ContextType.TYPE_CLOCK:
|
|
223
|
+
checkType(ValueType.TYPE_U64, type_required, node);
|
|
224
|
+
break;
|
|
225
|
+
case ContextType.TYPE_GUARD:
|
|
226
|
+
case ContextType.TYPE_SIGNER:
|
|
227
|
+
checkType(ValueType.TYPE_ADDRESS, type_required, node);
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
} else {
|
|
231
|
+
ERROR(Errors.InvalidParam, 'node - ' + node.toString())
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
const checkType = (type: ValueType | ContextType.TYPE_CLOCK | ContextType.TYPE_GUARD | ContextType.TYPE_SIGNER,
|
|
237
|
+
type_required:ValueType | 'number' | 'variable', node?: GuardNode) => {
|
|
238
|
+
if (type_required === 'variable') {
|
|
239
|
+
type_required = type as number;
|
|
240
|
+
} else if (type_required === 'number') {
|
|
241
|
+
if (type === ValueType.TYPE_U128 || type === ValueType.TYPE_U256 || type === ValueType.TYPE_U8 ||
|
|
242
|
+
type === ValueType.TYPE_U64 || type === ContextType.TYPE_CLOCK) {
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
} else if (type_required === ValueType.TYPE_ADDRESS) {
|
|
246
|
+
if (type === ContextType.TYPE_SIGNER || type === ContextType.TYPE_GUARD) {
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
} /*else if (type_required === ValueType.TYPE_STRING) {
|
|
250
|
+
if (type === ValueType.TYPE_VEC_U8) {
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
} else if (type_required === ValueType.TYPE_VEC_U8) {
|
|
254
|
+
if (type === ValueType.TYPE_STRING) {
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
} */
|
|
258
|
+
|
|
259
|
+
if (type !== type_required) {
|
|
260
|
+
var str = '';
|
|
261
|
+
if (node) str = ' - ' + node.toString();
|
|
262
|
+
ERROR(Errors.InvalidParam, 'checkType: ' + type + ' require type: ' + type_required + str);
|
|
263
|
+
}
|
|
264
|
+
}
|