wowok 1.7.13 → 1.7.16
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/dist/arbitration.d.ts.map +1 -1
- package/dist/arbitration.js +21 -32
- package/dist/arbitration.js.map +1 -1
- package/dist/entity.d.ts +1 -0
- package/dist/entity.d.ts.map +1 -1
- package/dist/entity.js +12 -6
- package/dist/entity.js.map +1 -1
- package/dist/exception.d.ts +2 -1
- package/dist/exception.d.ts.map +1 -1
- package/dist/exception.js +1 -0
- package/dist/exception.js.map +1 -1
- package/dist/guard.d.ts.map +1 -1
- package/dist/guard.js +8 -4
- package/dist/guard.js.map +1 -1
- package/dist/payment.d.ts +2 -2
- package/dist/payment.d.ts.map +1 -1
- package/dist/payment.js +2 -2
- package/dist/payment.js.map +1 -1
- package/dist/protocol.d.ts +21 -19
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +211 -58
- package/dist/protocol.js.map +1 -1
- package/dist/service.d.ts +2 -2
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +5 -6
- package/dist/service.js.map +1 -1
- package/package.json +5 -2
- package/src/arbitration.ts +0 -551
- package/src/demand.ts +0 -300
- package/src/entity.ts +0 -171
- package/src/exception.ts +0 -37
- package/src/guard.ts +0 -810
- package/src/index.ts +0 -40
- package/src/machine.ts +0 -542
- package/src/passport.ts +0 -777
- package/src/payment.ts +0 -94
- package/src/permission.ts +0 -550
- package/src/progress.ts +0 -367
- package/src/protocol.ts +0 -549
- package/src/repository.ts +0 -680
- package/src/resource.ts +0 -155
- package/src/service.ts +0 -1349
- package/src/treasury.ts +0 -425
- package/src/utils.ts +0 -660
- package/src/wowok.ts +0 -70
- package/tsconfig.json +0 -30
- package/tsconfig.tsbuildinfo +0 -1
- package/webpack.config.cjs +0 -23
package/src/payment.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { CoinObject, FnCallType, PaymentAddress, Protocol, TxbObject} from './protocol.js';
|
|
2
|
-
import { IsValidDesription, IsValidAddress, IsValidArray, } from './utils.js';
|
|
3
|
-
import { ERROR, Errors } from './exception.js';
|
|
4
|
-
import { Transaction as TransactionBlock} from '@mysten/sui/transactions';
|
|
5
|
-
|
|
6
|
-
export interface Payment_Receiver {
|
|
7
|
-
address: string;
|
|
8
|
-
coin: CoinObject;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface PaymentParam {
|
|
12
|
-
receiver: Payment_Receiver[];
|
|
13
|
-
business_index: bigint;
|
|
14
|
-
business_remark: string;
|
|
15
|
-
for_object?: string;
|
|
16
|
-
for_guard?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const PAYMENT_MAX_RECEIVER_COUNT = 200;
|
|
20
|
-
export function create_payment(txb:TransactionBlock, pay_token_type:string, param:PaymentParam) : PaymentAddress {
|
|
21
|
-
if (!pay_token_type) ERROR(Errors.InvalidParam, 'Payment.New.pay_token_type');
|
|
22
|
-
|
|
23
|
-
if (param.receiver.length > PAYMENT_MAX_RECEIVER_COUNT) {
|
|
24
|
-
ERROR(Errors.InvalidParam, 'Payment.New.param.receiver');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (!IsValidArray(param.receiver, (item:Payment_Receiver) => { return IsValidAddress(item.address) && Protocol.IsValidObjects([item.coin])})) {
|
|
28
|
-
ERROR(Errors.IsValidArray, 'Payment.New.param.receiver');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (param?.for_object && !IsValidAddress(param.for_object)) {
|
|
32
|
-
ERROR(Errors.IsValidAddress, 'Payment.New.param.for_object')
|
|
33
|
-
}
|
|
34
|
-
if (param?.for_guard && !IsValidAddress(param.for_guard)) {
|
|
35
|
-
ERROR(Errors.IsValidAddress, 'Payment.New.param.for_guard')
|
|
36
|
-
}
|
|
37
|
-
if (param?.business_remark && !IsValidDesription(param?.business_remark)) {
|
|
38
|
-
ERROR(Errors.IsValidDesription, 'Payment.New.param.business_remark')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let obj = txb.pure.option('address', param.for_object ? param.for_object : undefined);
|
|
42
|
-
const clock = txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
|
|
43
|
-
|
|
44
|
-
if (param.for_guard) {
|
|
45
|
-
return txb.moveCall({
|
|
46
|
-
target:Protocol.Instance().paymentFn('create_withGuard') as FnCallType,
|
|
47
|
-
arguments:[txb.pure.vector('address', param.receiver.map((i)=>i.address)), txb.makeMoveVec({elements:param.receiver.map((i)=>{ return (i.coin as any)})}),
|
|
48
|
-
obj, txb.object(param.for_guard), txb.pure.u64(param.business_index), txb.pure.string(param.business_remark), txb.object(clock)],
|
|
49
|
-
typeArguments:[pay_token_type],
|
|
50
|
-
})
|
|
51
|
-
} else {
|
|
52
|
-
return txb.moveCall({
|
|
53
|
-
target:Protocol.Instance().paymentFn('create') as FnCallType,
|
|
54
|
-
arguments:[txb.pure.vector('address', param.receiver.map((i)=>i.address)), txb.makeMoveVec({elements:param.receiver.map((i)=>{ return (i.coin as any)})}),
|
|
55
|
-
obj, txb.pure.u64(param.business_index), txb.pure.string(param.business_remark), txb.object(clock)],
|
|
56
|
-
typeArguments:[pay_token_type],
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export interface ReceivedBalanceObject {
|
|
62
|
-
id: string; // object received.
|
|
63
|
-
type: string; // coin object type.
|
|
64
|
-
balance: string; // balance.
|
|
65
|
-
payment: string; // payment object.
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface ReceivedBalance {
|
|
69
|
-
balance?: string; // total balance if toekn_type specified.
|
|
70
|
-
token_type?: string; // token type for querying.
|
|
71
|
-
received: ReceivedBalanceObject[];
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// receive coins for Order, Treasury, etc...
|
|
75
|
-
export const GetRecievedBalanceObject = async (object_address:string, token_type?:string | undefined) : Promise<ReceivedBalance|undefined> => {
|
|
76
|
-
const type = token_type ? Protocol.Instance().package('wowok')+'::payment::CoinWrapper<'+token_type+'>' : undefined;
|
|
77
|
-
const r = await Protocol.Client().getOwnedObjects({owner:object_address,
|
|
78
|
-
filter:type ? {StructType: type} : undefined, options:{showContent:true, showType:true}});
|
|
79
|
-
try {
|
|
80
|
-
let receive = BigInt(0);
|
|
81
|
-
const res: ReceivedBalanceObject[] = r.data.map((v:any) => {
|
|
82
|
-
const i = v?.data?.content?.fields;
|
|
83
|
-
receive += BigInt(i?.coin?.fields?.balance);
|
|
84
|
-
return {payment:i?.payment, balance:i?.coin?.fields?.balance, id:v?.data?.objectId, type: i?.coin?.type}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
return {balance: token_type ? receive.toString() : undefined, received:res, token_type:token_type};
|
|
88
|
-
} catch (e) {
|
|
89
|
-
//console.log(e)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
package/src/permission.ts
DELETED
|
@@ -1,550 +0,0 @@
|
|
|
1
|
-
import { FnCallType, TxbObject, PermissionObject, PermissionAddress, GuardObject, Protocol, MODULES} from './protocol.js';
|
|
2
|
-
import { array_unique, IsValidAddress, IsValidArray, IsValidDesription, Bcs, IsValidName, IsValidU64} from './utils.js';
|
|
3
|
-
import { ERROR, Errors } from './exception.js';
|
|
4
|
-
import { bcs } from '@mysten/sui/bcs';
|
|
5
|
-
import { Transaction as TransactionBlock } from '@mysten/sui/transactions';
|
|
6
|
-
|
|
7
|
-
export enum PermissionIndex {
|
|
8
|
-
repository = 100,
|
|
9
|
-
repository_description = 101,
|
|
10
|
-
repository_mode = 102,
|
|
11
|
-
repository_policies = 103,
|
|
12
|
-
repository_reference = 104,
|
|
13
|
-
repository_guard = 105,
|
|
14
|
-
|
|
15
|
-
service = 200,
|
|
16
|
-
service_description = 201,
|
|
17
|
-
service_sales = 202,
|
|
18
|
-
service_payee = 203,
|
|
19
|
-
service_repository = 204,
|
|
20
|
-
service_withdraw_guards = 205,
|
|
21
|
-
service_refund_guards = 206,
|
|
22
|
-
service_discount_transfer = 207,
|
|
23
|
-
service_withdraw = 208,
|
|
24
|
-
service_buyer_guard = 209,
|
|
25
|
-
service_machine = 210,
|
|
26
|
-
service_endpoint = 211,
|
|
27
|
-
service_publish = 212,
|
|
28
|
-
service_clone = 213,
|
|
29
|
-
service_customer_required = 214,
|
|
30
|
-
service_pause = 215,
|
|
31
|
-
service_treasury = 216,
|
|
32
|
-
service_arbitration = 217,
|
|
33
|
-
service_location = 218,
|
|
34
|
-
service_refund = 219,
|
|
35
|
-
|
|
36
|
-
demand = 260,
|
|
37
|
-
demand_refund = 261,
|
|
38
|
-
demand_expand_time = 262,
|
|
39
|
-
demand_guard = 263,
|
|
40
|
-
demand_description = 264,
|
|
41
|
-
demand_yes = 265,
|
|
42
|
-
demand_location = 266,
|
|
43
|
-
|
|
44
|
-
machine = 600,
|
|
45
|
-
machine_description = 601,
|
|
46
|
-
machine_repository = 602,
|
|
47
|
-
machine_clone = 604,
|
|
48
|
-
machine_node = 606,
|
|
49
|
-
machine_endpoint = 608,
|
|
50
|
-
machine_pause = 609,
|
|
51
|
-
machine_publish = 610,
|
|
52
|
-
|
|
53
|
-
progress = 650,
|
|
54
|
-
progress_namedOperator = 651,
|
|
55
|
-
progress_bind_task = 652,
|
|
56
|
-
progress_context_repository = 653,
|
|
57
|
-
progress_unhold = 654,
|
|
58
|
-
progress_parent = 655,
|
|
59
|
-
|
|
60
|
-
treasury = 700,
|
|
61
|
-
treasury_receive = 701,
|
|
62
|
-
treasury_deposit = 702,
|
|
63
|
-
treasury_withdraw = 703,
|
|
64
|
-
treasury_descritption = 704,
|
|
65
|
-
treasury_deposit_guard = 705,
|
|
66
|
-
treasury_withdraw_mode = 706,
|
|
67
|
-
treasury_withdraw_guard = 707,
|
|
68
|
-
|
|
69
|
-
arbitration = 800,
|
|
70
|
-
arbitration_description = 801,
|
|
71
|
-
arbitration_fee = 802,
|
|
72
|
-
arbitration_voting_guard = 803,
|
|
73
|
-
arbitration_endpoint = 804,
|
|
74
|
-
arbitration_guard = 805,
|
|
75
|
-
arbitration_pause = 806,
|
|
76
|
-
arbitration_vote = 807,
|
|
77
|
-
arbitration_arbitration = 808,
|
|
78
|
-
arbitration_withdraw = 809,
|
|
79
|
-
arbitration_treasury = 810,
|
|
80
|
-
arbitration_location = 811,
|
|
81
|
-
|
|
82
|
-
user_defined_start = 1000,
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export interface PermissionInfoType {
|
|
86
|
-
index: number;
|
|
87
|
-
name:string;
|
|
88
|
-
description:string;
|
|
89
|
-
module: string;
|
|
90
|
-
guard?: string;
|
|
91
|
-
}
|
|
92
|
-
export const PermissionInfo : PermissionInfoType[] = [
|
|
93
|
-
{index:PermissionIndex.repository, name:'Repository', description:'Launch new Repository', module: MODULES.repository},
|
|
94
|
-
{index:PermissionIndex.repository_description, name:'Description', description:'Set Repository description', module: MODULES.repository},
|
|
95
|
-
{index:PermissionIndex.repository_mode, name:'Policy mode', description:'Set Repository mode', module: MODULES.repository},
|
|
96
|
-
{index:PermissionIndex.repository_policies, name:'Policy', description:'Set Repository policies', module: MODULES.repository},
|
|
97
|
-
{index:PermissionIndex.repository_reference, name:'Reference', description:'Set Repository reference', module: MODULES.repository},
|
|
98
|
-
{index:PermissionIndex.repository_guard, name:'Guard', description:'Set Guard for data query', module: MODULES.repository},
|
|
99
|
-
|
|
100
|
-
{index:PermissionIndex.service, name:'Service', description:'Launch new Service', module: MODULES.service},
|
|
101
|
-
{index:PermissionIndex.service_description, name:'Description', description:'Set Service description', module: MODULES.service},
|
|
102
|
-
{index:PermissionIndex.service_sales, name:'Sales', description:'Set Service sales items', module: MODULES.service},
|
|
103
|
-
{index:PermissionIndex.service_payee, name:'Payee', description:'Set Service payee', module: MODULES.service},
|
|
104
|
-
{index:PermissionIndex.service_repository, name:'Repository', description:'Set Service repositories', module: MODULES.service},
|
|
105
|
-
{index:PermissionIndex.service_withdraw_guards, name:'Withdraw Guard', description:'Set Service withdraw guards', module: MODULES.service},
|
|
106
|
-
{index:PermissionIndex.service_refund_guards, name:'Refund Guard', description:'Set Service refund guards', module: MODULES.service},
|
|
107
|
-
{index:PermissionIndex.service_discount_transfer, name:'Discount', description:'Launch discounts for Service', module: MODULES.service},
|
|
108
|
-
{index:PermissionIndex.service_buyer_guard, name:'Buyer Guard', description:'Set Guard of buying for Service', module: MODULES.service},
|
|
109
|
-
{index:PermissionIndex.service_machine, name:'Machine', description:'Set Machine for Service', module: MODULES.service},
|
|
110
|
-
{index:PermissionIndex.service_endpoint, name:'Endpoint', description:'Set Service endpoint', module: MODULES.service},
|
|
111
|
-
{index:PermissionIndex.service_publish, name:'Publish', description:'Allowing the creation of Order', module: MODULES.service},
|
|
112
|
-
{index:PermissionIndex.service_clone, name:'Clone', description:'Clone Service', module: MODULES.service},
|
|
113
|
-
{index:PermissionIndex.service_customer_required, name:'Buyer info', description:'Set Service buyer info required', module: MODULES.service},
|
|
114
|
-
{index:PermissionIndex.service_pause, name:'Pause', description:'Pause/Unpause Service', module: MODULES.service},
|
|
115
|
-
{index:PermissionIndex.service_treasury, name:'Treasury', description:'Externally withdrawable treasury for compensation or rewards', module: MODULES.service},
|
|
116
|
-
{index:PermissionIndex.service_arbitration, name:'Arbitration', description:'Add/Remove arbitration that allows refunds from orders at any time based on arbitration results', module: MODULES.service},
|
|
117
|
-
{index:PermissionIndex.service_location, name:'Location', description:'Set Service location', module: MODULES.service},
|
|
118
|
-
{index:PermissionIndex.service_refund, name:'Refund', description:'Initiate order refund', module: MODULES.service},
|
|
119
|
-
|
|
120
|
-
{index:PermissionIndex.demand, name:'Demand', description:'Launch new Demand', module: MODULES.demand},
|
|
121
|
-
{index:PermissionIndex.demand_refund, name:'Refund', description:'Refund from Demand', module: MODULES.demand},
|
|
122
|
-
{index:PermissionIndex.demand_expand_time, name:'Expand deadline', description:'Expand Demand deadline', module: MODULES.demand},
|
|
123
|
-
{index:PermissionIndex.demand_guard, name:'Guard', description:'Set Demand guard', module: MODULES.demand},
|
|
124
|
-
{index:PermissionIndex.demand_description, name:'Description', description:'Set Demand description', module: MODULES.demand},
|
|
125
|
-
{index:PermissionIndex.demand_yes, name:'Yes', description:'Pick the Deamand serice', module: MODULES.demand},
|
|
126
|
-
{index:PermissionIndex.demand_location, name:'Location', description:'Set Demand location', module: MODULES.demand},
|
|
127
|
-
|
|
128
|
-
{index:PermissionIndex.machine, name: 'Machine', description:'Launch new Machine', module: MODULES.machine},
|
|
129
|
-
{index:PermissionIndex.machine_description, name: 'Description', description:'Set Machine description', module: MODULES.machine},
|
|
130
|
-
{index:PermissionIndex.machine_repository, name: 'Repository', description:'Set Machine repository', module: MODULES.machine},
|
|
131
|
-
{index:PermissionIndex.machine_clone, name: 'Clone', description:'Clone Machine', module: MODULES.machine},
|
|
132
|
-
{index:PermissionIndex.machine_node, name: 'Node', description:'Set Machine nodes', module: MODULES.machine},
|
|
133
|
-
{index:PermissionIndex.machine_endpoint, name: 'Endpoint', description:'Set Machine endpoint', module: MODULES.machine},
|
|
134
|
-
{index:PermissionIndex.machine_pause, name: 'Pause', description:'Pause/Unpause Machine', module: MODULES.machine},
|
|
135
|
-
{index:PermissionIndex.machine_publish, name: 'Publish', description:'Allowing the creation of Progress', module: MODULES.machine},
|
|
136
|
-
|
|
137
|
-
{index:PermissionIndex.progress, name: 'Progress', description:'Launch new Progress', module: MODULES.progress},
|
|
138
|
-
{index:PermissionIndex.progress_namedOperator, name: 'Operator', description:'Set Progress operators', module: MODULES.progress},
|
|
139
|
-
{index:PermissionIndex.progress_bind_task, name: 'Bind', description:'Set Progress task', module: MODULES.progress},
|
|
140
|
-
{index:PermissionIndex.progress_context_repository, name: 'Repository', description:'Set Progress repository', module: MODULES.progress},
|
|
141
|
-
{index:PermissionIndex.progress_unhold, name: 'Unhold', description:'Release Progress holdings', module: MODULES.progress},
|
|
142
|
-
{index:PermissionIndex.progress_parent, name: 'Parent', description:'Set Progress parent', module: MODULES.progress},
|
|
143
|
-
|
|
144
|
-
{index:PermissionIndex.treasury, name: 'Treasury', description:'Launch new Treasury', module: MODULES.treasury},
|
|
145
|
-
{index:PermissionIndex.treasury_deposit, name: 'Deposit', description:'Deposit coins', module: MODULES.treasury},
|
|
146
|
-
{index:PermissionIndex.treasury_receive, name: 'Receive', description:'Receive coins from some address sent', module: MODULES.treasury},
|
|
147
|
-
{index:PermissionIndex.treasury_withdraw, name: 'Withdraw', description:'Withdraw coins', module: MODULES.treasury},
|
|
148
|
-
{index:PermissionIndex.treasury_withdraw_guard, name: 'Withdraw Guard', description:'Add/Remove Treasury withdraw guard', module: MODULES.treasury},
|
|
149
|
-
{index:PermissionIndex.treasury_withdraw_mode, name: 'Withdraw mode', description:'Set Treasury withdraw mode', module: MODULES.treasury},
|
|
150
|
-
{index:PermissionIndex.treasury_deposit_guard, name: 'Deposit Guard', description:'Set Treasury deposit guard', module: MODULES.treasury},
|
|
151
|
-
{index:PermissionIndex.treasury_descritption, name: 'Description', description:'Set Treasury description', module: MODULES.treasury},
|
|
152
|
-
|
|
153
|
-
{index:PermissionIndex.arbitration, name: 'Arbitration', description:'Launch new Arbitration', module: MODULES.arbitration},
|
|
154
|
-
{index:PermissionIndex.arbitration_description, name: 'Description', description:'Set Arbitration description', module: MODULES.arbitration},
|
|
155
|
-
{index:PermissionIndex.arbitration_endpoint, name: 'Endpoint', description:'Set Arbitration endpoint', module: MODULES.arbitration},
|
|
156
|
-
{index:PermissionIndex.arbitration_fee, name: 'Fee', description:'Set Arbitration fee', module: MODULES.arbitration},
|
|
157
|
-
{index:PermissionIndex.arbitration_guard, name: 'Guard', description:'Set Guard to apply for arbitration', module: MODULES.arbitration},
|
|
158
|
-
{index:PermissionIndex.arbitration_arbitration, name: 'Arbitrate', description:'Determine the outcome of arbitration', module: MODULES.arbitration},
|
|
159
|
-
{index:PermissionIndex.arbitration_pause, name: 'Pause', description:'Allowing/forbidding the creation of Arb', module: MODULES.arbitration},
|
|
160
|
-
{index:PermissionIndex.arbitration_voting_guard, name: 'Voting Guard', description:'Add/Remove voting Guard', module: MODULES.arbitration},
|
|
161
|
-
{index:PermissionIndex.arbitration_vote, name: 'Vote', description:'Vote on the application for arbitration', module: MODULES.arbitration},
|
|
162
|
-
{index:PermissionIndex.arbitration_withdraw, name: 'Withdraw', description:'Withdraw the arbitration fee', module: MODULES.arbitration},
|
|
163
|
-
{index:PermissionIndex.arbitration_treasury, name: 'Withdraw', description:'Set Treasury that fees was collected at the time of withdrawal', module: MODULES.arbitration},
|
|
164
|
-
{index:PermissionIndex.arbitration_location, name: 'Location', description:'Set Arbitration location', module: MODULES.arbitration},
|
|
165
|
-
]
|
|
166
|
-
|
|
167
|
-
export interface PermissionAnswer {
|
|
168
|
-
who: string;
|
|
169
|
-
owner?: boolean;
|
|
170
|
-
admin?: boolean;
|
|
171
|
-
items?: PermissionAnswerItem[]; // items === undefined, while errors
|
|
172
|
-
object: string; // permission object
|
|
173
|
-
}
|
|
174
|
-
export interface PermissionAnswerItem {
|
|
175
|
-
query: PermissionIndexType;
|
|
176
|
-
permission: boolean;
|
|
177
|
-
guard?: string;
|
|
178
|
-
}
|
|
179
|
-
export type OnPermissionAnswer = (answer: PermissionAnswer) => void;
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
export type PermissionIndexType = PermissionIndex | number;
|
|
184
|
-
|
|
185
|
-
export interface Permission_Entity_Permission {
|
|
186
|
-
index: PermissionIndexType;
|
|
187
|
-
guard?: TxbObject;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export interface Permission_Entity {
|
|
191
|
-
address:string;
|
|
192
|
-
permissions:Permission_Entity_Permission[];
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export interface Permission_Index_Entity {
|
|
196
|
-
address: string;
|
|
197
|
-
guard?: TxbObject;
|
|
198
|
-
}
|
|
199
|
-
export interface Permission_Index {
|
|
200
|
-
index: PermissionIndexType;
|
|
201
|
-
entities: Permission_Index_Entity[];
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export interface BizPermission {
|
|
205
|
-
index: PermissionIndexType;
|
|
206
|
-
name: string;
|
|
207
|
-
}
|
|
208
|
-
export class Permission {
|
|
209
|
-
protected txb;
|
|
210
|
-
protected object : TxbObject;
|
|
211
|
-
|
|
212
|
-
get_object() { return this.object }
|
|
213
|
-
private constructor(txb:TransactionBlock) {
|
|
214
|
-
this.txb = txb;
|
|
215
|
-
this.object = '';
|
|
216
|
-
}
|
|
217
|
-
static From(txb:TransactionBlock, object:TxbObject) : Permission {
|
|
218
|
-
let p = new Permission(txb);
|
|
219
|
-
p.object = Protocol.TXB_OBJECT(txb, object);
|
|
220
|
-
return p
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
static New(txb:TransactionBlock, description:string) : Permission {
|
|
224
|
-
if (!IsValidDesription(description)) {
|
|
225
|
-
ERROR(Errors.IsValidDesription)
|
|
226
|
-
}
|
|
227
|
-
let p = new Permission(txb);
|
|
228
|
-
p.object = txb.moveCall({
|
|
229
|
-
target: Protocol.Instance().permissionFn('new') as FnCallType,
|
|
230
|
-
arguments: [txb.pure.string(description)]
|
|
231
|
-
});
|
|
232
|
-
return p
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
launch() : PermissionAddress {
|
|
236
|
-
return this.txb.moveCall({ // address returned
|
|
237
|
-
target:Protocol.Instance().permissionFn('create') as FnCallType,
|
|
238
|
-
arguments:[ Protocol.TXB_OBJECT(this.txb, this.object) ]
|
|
239
|
-
})
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
add_bizPermission(index: number, name:string) {
|
|
243
|
-
if (!Permission.IsValidBizPermissionIndex(index)) {
|
|
244
|
-
ERROR(Errors.IsValidBizPermissionIndex, 'add_bizPermission');
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (!IsValidName(name)) {
|
|
248
|
-
ERROR(Errors.IsValidName, 'add_bizPermission');
|
|
249
|
-
}
|
|
250
|
-
this.txb.moveCall({
|
|
251
|
-
target:Protocol.Instance().permissionFn('user_define_add') as FnCallType,
|
|
252
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u64(index), this.txb.pure.string(name)]
|
|
253
|
-
})
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
remove_bizPermission(index: number) {
|
|
257
|
-
if (!Permission.IsValidBizPermissionIndex(index)) {
|
|
258
|
-
ERROR(Errors.IsValidBizPermissionIndex, 'remove_bizPermission');
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
this.txb.moveCall({
|
|
262
|
-
target:Protocol.Instance().permissionFn('user_define_remove') as FnCallType,
|
|
263
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u64(index)]
|
|
264
|
-
})
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
transfer_permission(old_entity: string, new_entity: string) {
|
|
268
|
-
if (!IsValidAddress(old_entity) || !IsValidAddress(new_entity)) {
|
|
269
|
-
ERROR(Errors.IsValidAddress, 'transfer_permission')
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
this.txb.moveCall({
|
|
273
|
-
target:Protocol.Instance().permissionFn('change_entity') as FnCallType,
|
|
274
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(old_entity),
|
|
275
|
-
this.txb.pure.address(new_entity) ]
|
|
276
|
-
})
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
add_entity2(entities: string[], index?:PermissionIndexType) {
|
|
280
|
-
if (entities.length === 0) return;
|
|
281
|
-
|
|
282
|
-
if (!IsValidArray(entities, IsValidAddress)) {
|
|
283
|
-
ERROR(Errors.IsValidArray, 'add_entity2');
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
if (index !== undefined) {
|
|
287
|
-
this.txb.moveCall({
|
|
288
|
-
target:Protocol.Instance().permissionFn('add_with_index') as FnCallType,
|
|
289
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u64(index),
|
|
290
|
-
this.txb.pure.vector('address', array_unique(entities))]
|
|
291
|
-
})
|
|
292
|
-
} else {
|
|
293
|
-
this.txb.moveCall({
|
|
294
|
-
target:Protocol.Instance().permissionFn('add') as FnCallType,
|
|
295
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', array_unique(entities))]
|
|
296
|
-
})
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
add_entity3(entities: Permission_Index[]) {
|
|
300
|
-
if (entities.length === 0) return;
|
|
301
|
-
const e : Permission_Entity[] = [];
|
|
302
|
-
|
|
303
|
-
entities.forEach((v) => {
|
|
304
|
-
v.entities.forEach((p) => {
|
|
305
|
-
const f = e.find((i) => i.address === p.address);
|
|
306
|
-
if (f) {
|
|
307
|
-
const t = f.permissions.find((k)=>k.index === v.index);
|
|
308
|
-
if (t) {
|
|
309
|
-
t.guard = p.guard;
|
|
310
|
-
} else {
|
|
311
|
-
f.permissions.push({guard:p.guard, index:v.index});
|
|
312
|
-
}
|
|
313
|
-
} else {
|
|
314
|
-
e.push({address:p.address, permissions:[{guard:p.guard, index:v.index}]})
|
|
315
|
-
}
|
|
316
|
-
})
|
|
317
|
-
});
|
|
318
|
-
this.add_entity(e);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
add_entity(entities:Permission_Entity[]) {
|
|
322
|
-
if (entities.length === 0) return
|
|
323
|
-
entities.forEach((v) => {
|
|
324
|
-
if (!IsValidAddress(v.address)) ERROR(Errors.IsValidAddress, 'add_entity.entities.address')
|
|
325
|
-
v.permissions.forEach((p) => {
|
|
326
|
-
if (!Permission.IsValidPermissionIndex(p.index)) ERROR(Errors.IsValidPermissionIndex, 'add_entity.entities.permissions.index')
|
|
327
|
-
if (p?.guard && !Protocol.IsValidObjects([p.guard])) ERROR(Errors.IsValidAddress, 'add_entity.entities.permissions.guard')
|
|
328
|
-
})
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
let guards:any[] = [];
|
|
332
|
-
for (let i = 0; i < entities.length; i++) {
|
|
333
|
-
let entity = entities[i];
|
|
334
|
-
let indexes :number[] = [];
|
|
335
|
-
|
|
336
|
-
for (let j = 0; j < entity.permissions.length; j++) {
|
|
337
|
-
let index = entity.permissions[j];
|
|
338
|
-
if (!Permission.IsValidPermissionIndex(index.index)) {
|
|
339
|
-
continue;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
if (!indexes.includes(index.index)) {
|
|
343
|
-
indexes.push(index.index);
|
|
344
|
-
if (index?.guard) {
|
|
345
|
-
guards.push({address:entity.address, index:index.index, guard:index.guard});
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
if (indexes.length > 0) {
|
|
351
|
-
this.txb.moveCall({
|
|
352
|
-
target:Protocol.Instance().permissionFn('add_batch') as FnCallType,
|
|
353
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(entity.address),
|
|
354
|
-
this.txb.pure.vector('u64', indexes)]
|
|
355
|
-
})
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
// set guards
|
|
359
|
-
guards.forEach(({address, index, guard}) => {
|
|
360
|
-
this.txb.moveCall({
|
|
361
|
-
target:Protocol.Instance().permissionFn('guard_set') as FnCallType,
|
|
362
|
-
arguments:[ Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(address),
|
|
363
|
-
this.txb.pure.u64(index), Protocol.TXB_OBJECT(this.txb, guard)]
|
|
364
|
-
})
|
|
365
|
-
})
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// guard: undefine to set none
|
|
369
|
-
set_guard(address:string, index:PermissionIndexType, guard?:GuardObject) {
|
|
370
|
-
if (!IsValidAddress(address)) {
|
|
371
|
-
ERROR(Errors.IsValidAddress, 'address')
|
|
372
|
-
}
|
|
373
|
-
if(!Permission.IsValidPermissionIndex(index) && !Permission.IsValidBizPermissionIndex(index)) {
|
|
374
|
-
ERROR(Errors.IsValidPermissionIndex, 'index')
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if (guard) {
|
|
378
|
-
this.txb.moveCall({
|
|
379
|
-
target:Protocol.Instance().permissionFn('guard_set') as FnCallType,
|
|
380
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(address),
|
|
381
|
-
this.txb.pure.u64(index), Protocol.TXB_OBJECT(this.txb, guard)]
|
|
382
|
-
})
|
|
383
|
-
} else {
|
|
384
|
-
this.txb.moveCall({
|
|
385
|
-
target:Protocol.Instance().permissionFn('guard_none') as FnCallType,
|
|
386
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(address),
|
|
387
|
-
this.txb.pure.u64(index)]
|
|
388
|
-
})
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
remove_index(address:string, index:PermissionIndexType[]) {
|
|
393
|
-
if (!IsValidAddress(address)) {
|
|
394
|
-
ERROR(Errors.IsValidAddress)
|
|
395
|
-
}
|
|
396
|
-
if (index.length === 0) return ;
|
|
397
|
-
if (!(IsValidArray(index, Permission.IsValidPermissionIndex))) {
|
|
398
|
-
ERROR(Errors.InvalidParam, 'index')
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
this.txb.moveCall({
|
|
402
|
-
target:Protocol.Instance().permissionFn('remove_index') as FnCallType,
|
|
403
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(address),
|
|
404
|
-
this.txb.pure.vector('u64', array_unique(index))]
|
|
405
|
-
})
|
|
406
|
-
}
|
|
407
|
-
remove_entity(address:string[]) {
|
|
408
|
-
if (address.length === 0) return ;
|
|
409
|
-
if (!IsValidArray(address, IsValidAddress)) {
|
|
410
|
-
ERROR(Errors.IsValidArray)
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
this.txb.moveCall({
|
|
414
|
-
target:Protocol.Instance().permissionFn('remove') as FnCallType,
|
|
415
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', array_unique(address))]
|
|
416
|
-
})
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
set_description(description:string) {
|
|
420
|
-
if (!IsValidDesription(description)) {
|
|
421
|
-
ERROR(Errors.IsValidDesription)
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
this.txb.moveCall({
|
|
425
|
-
target:Protocol.Instance().permissionFn('description_set') as FnCallType,
|
|
426
|
-
arguments: [Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.string(description)]
|
|
427
|
-
})
|
|
428
|
-
;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
add_admin(admin:string[]) {
|
|
432
|
-
if (admin.length === 0) return ;
|
|
433
|
-
|
|
434
|
-
if (!IsValidArray(admin, IsValidAddress)) {
|
|
435
|
-
ERROR(Errors.IsValidArray)
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
this.txb.moveCall({
|
|
439
|
-
target:Protocol.Instance().permissionFn('admin_add_batch') as FnCallType,
|
|
440
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', array_unique(admin))]
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
remove_admin(admin:string[], removeall?:boolean) {
|
|
445
|
-
if (!removeall && admin.length === 0) return;
|
|
446
|
-
if (!IsValidArray(admin, IsValidAddress)) {
|
|
447
|
-
ERROR(Errors.IsValidArray, 'admin')
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
if (removeall) {
|
|
451
|
-
this.txb.moveCall({
|
|
452
|
-
target:Protocol.Instance().permissionFn('admins_clear') as FnCallType,
|
|
453
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object)]
|
|
454
|
-
});
|
|
455
|
-
} else if (admin) {
|
|
456
|
-
this.txb.moveCall({
|
|
457
|
-
target:Protocol.Instance().permissionFn('admin_remove_batch') as FnCallType,
|
|
458
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', array_unique(admin))]
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
change_owner(new_owner:string) {
|
|
465
|
-
if (!IsValidAddress(new_owner)) {
|
|
466
|
-
ERROR(Errors.IsValidAddress)
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
this.txb.moveCall({
|
|
470
|
-
target:Protocol.Instance().permissionFn('builder_set') as FnCallType,
|
|
471
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(new_owner)]
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
// query all permissions for address
|
|
476
|
-
query_permissions_all(address_queried:string) {
|
|
477
|
-
if (!IsValidAddress(address_queried)) {
|
|
478
|
-
ERROR(Errors.InvalidParam, 'query_permissions');
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
this.txb.moveCall({
|
|
482
|
-
target:Protocol.Instance().permissionFn('query_permissions_all') as FnCallType,
|
|
483
|
-
arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(address_queried)]
|
|
484
|
-
})
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
QueryPermissions(permission:string, address_queried:string, onPermissionAnswer:OnPermissionAnswer, sender?:string) {
|
|
488
|
-
//@ be the same txb
|
|
489
|
-
this.query_permissions_all(address_queried);
|
|
490
|
-
//console.log(address_queried)
|
|
491
|
-
Protocol.Client().devInspectTransactionBlock({sender:sender ?? address_queried, transactionBlock:this.txb}).then((res) => {
|
|
492
|
-
if (res.results && res.results[0].returnValues && res.results[0].returnValues.length !== 2) {
|
|
493
|
-
onPermissionAnswer({who:address_queried, object:permission});
|
|
494
|
-
return
|
|
495
|
-
}
|
|
496
|
-
const perm = bcs.u8().parse(Uint8Array.from((res.results as any)[0].returnValues[0][0]));
|
|
497
|
-
if (perm === Permission.PERMISSION_ADMIN || perm === Permission.PERMISSION_OWNER_AND_ADMIN) {
|
|
498
|
-
onPermissionAnswer({who:address_queried, admin:true, owner:perm%2===1, items:[], object:permission})
|
|
499
|
-
} else {
|
|
500
|
-
const perms = Bcs.getInstance().de_perms(Uint8Array.from((res.results as any)[0].returnValues[1][0]));
|
|
501
|
-
onPermissionAnswer({who:address_queried, admin:false, owner:perm%2===1, items:perms.map((v:any)=>{
|
|
502
|
-
return {query:v?.index, permission:true, guard:v?.guard}
|
|
503
|
-
}), object:permission});
|
|
504
|
-
}
|
|
505
|
-
}).catch((e) => {
|
|
506
|
-
console.log(e);
|
|
507
|
-
onPermissionAnswer({who:address_queried, object:permission});
|
|
508
|
-
})
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
static HasPermission(answer:PermissionAnswer|undefined, index:PermissionIndexType, bStrict:boolean=false) : {has:boolean, guard?:string, owner?:boolean} | undefined {
|
|
512
|
-
if (answer) {
|
|
513
|
-
if (answer.admin) return {has:true, owner:answer.owner}; // admin
|
|
514
|
-
let i = answer.items?.find((v)=>v.query == index); // index maybe string, so ==
|
|
515
|
-
if (i) {
|
|
516
|
-
return {has:i.permission, guard:i.guard, owner:answer.owner};
|
|
517
|
-
} else {
|
|
518
|
-
return {has:false, guard:undefined, owner:answer?.owner}
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
if (bStrict) {
|
|
522
|
-
return {has:false, guard:undefined, owner:false}
|
|
523
|
-
}
|
|
524
|
-
return undefined // basic: !== false ; otherwise: !
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
static MAX_ADMIN_COUNT = 64;
|
|
528
|
-
static MAX_ENTITY_COUNT = 2000;
|
|
529
|
-
static MAX_PERMISSION_INDEX_COUNT = 200;
|
|
530
|
-
static MAX_PERSONAL_PERMISSION_COUNT = 200;
|
|
531
|
-
|
|
532
|
-
static PERMISSION_NORMAL = 0;
|
|
533
|
-
static PERMISSION_OWNER = 1;
|
|
534
|
-
static PERMISSION_ADMIN = 2;
|
|
535
|
-
static PERMISSION_OWNER_AND_ADMIN = 3;
|
|
536
|
-
static BUSINESS_PERMISSIONS_START = PermissionIndex.user_defined_start;
|
|
537
|
-
|
|
538
|
-
static IsValidBizPermissionIndex = (index:number) => {
|
|
539
|
-
return index >= Permission.BUSINESS_PERMISSIONS_START && IsValidU64(index)
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
static IsValidPermissionIndex = (index:PermissionIndexType) : boolean => {
|
|
543
|
-
//console.log(index)
|
|
544
|
-
if (Object.values(PermissionIndex).includes(index)) {
|
|
545
|
-
return true
|
|
546
|
-
}
|
|
547
|
-
//console.log(Object.keys(PermissionIndex))
|
|
548
|
-
return Permission.IsValidBizPermissionIndex(index);
|
|
549
|
-
}
|
|
550
|
-
}
|