wowok_agent 1.2.40 → 1.2.43
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/call/arbitration.d.ts.map +1 -1
- package/dist/call/arbitration.js +5 -3
- package/dist/call/arbitration.js.map +1 -1
- package/dist/call/base.d.ts +1 -1
- package/dist/call/base.d.ts.map +1 -1
- package/dist/call/base.js +7 -8
- package/dist/call/base.js.map +1 -1
- package/dist/call/call.d.ts.map +1 -1
- package/dist/call/call.js +12 -15
- package/dist/call/call.js.map +1 -1
- package/dist/call/demand.d.ts.map +1 -1
- package/dist/call/demand.js +6 -4
- package/dist/call/demand.js.map +1 -1
- package/dist/call/machine.d.ts.map +1 -1
- package/dist/call/machine.js +14 -7
- package/dist/call/machine.js.map +1 -1
- package/dist/call/permission.d.ts.map +1 -1
- package/dist/call/permission.js +3 -2
- package/dist/call/permission.js.map +1 -1
- package/dist/call/service.d.ts.map +1 -1
- package/dist/call/service.js +5 -3
- package/dist/call/service.js.map +1 -1
- package/dist/call/treasury.d.ts.map +1 -1
- package/dist/call/treasury.js +5 -3
- package/dist/call/treasury.js.map +1 -1
- package/dist/local/account.d.ts +26 -12
- package/dist/local/account.d.ts.map +1 -1
- package/dist/local/account.js +201 -93
- package/dist/local/account.js.map +1 -1
- package/dist/local/index.d.ts +15 -8
- package/dist/local/index.d.ts.map +1 -1
- package/dist/local/index.js +22 -19
- package/dist/local/index.js.map +1 -1
- package/dist/local/local.d.ts +0 -1
- package/dist/local/local.d.ts.map +1 -1
- package/dist/local/local.js +1 -27
- package/dist/local/local.js.map +1 -1
- package/dist/query/permission.d.ts +2 -2
- package/dist/query/permission.d.ts.map +1 -1
- package/dist/query/permission.js +13 -13
- package/dist/query/permission.js.map +1 -1
- package/package.json +2 -2
- package/src/call/arbitration.ts +5 -3
- package/src/call/base.ts +8 -9
- package/src/call/call.ts +12 -15
- package/src/call/demand.ts +6 -4
- package/src/call/machine.ts +16 -8
- package/src/call/permission.ts +3 -2
- package/src/call/service.ts +5 -3
- package/src/call/treasury.ts +5 -3
- package/src/local/account.ts +214 -92
- package/src/local/index.ts +32 -29
- package/src/local/local.ts +0 -26
- package/src/query/permission.ts +17 -14
- package/tsconfig.tsbuildinfo +1 -1
package/src/call/machine.ts
CHANGED
|
@@ -96,17 +96,19 @@ export class CallMachine extends CallBase { //@ todo self-owned node operate
|
|
|
96
96
|
perms.push(PermissionIndex.machine_pause)
|
|
97
97
|
}
|
|
98
98
|
if (this.data?.progress_next !== undefined) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
if (this.data?.progress_next?.guard) {
|
|
100
|
+
const guard = await LocalMark.Instance().get_address(this.data?.progress_next?.guard);
|
|
101
|
+
if (guard) {
|
|
102
|
+
guards.push(guard);
|
|
103
|
+
}
|
|
102
104
|
} else if (object_address) { // fetch guard
|
|
103
105
|
const p = await LocalMark.Instance().get_address(this.data?.progress_next.progress);
|
|
104
106
|
if (p) {
|
|
105
107
|
const guard = await Progress.QueryForwardGuard(this.data?.progress_next.progress, object_address,
|
|
106
|
-
await Account.Instance().default() ?? '0xe386bb9e01b3528b75f3751ad8a1e418b207ad979fea364087deef5250a73d3f',
|
|
108
|
+
(await Account.Instance().default())?.address ?? '0xe386bb9e01b3528b75f3751ad8a1e418b207ad979fea364087deef5250a73d3f',
|
|
107
109
|
this.data.progress_next.operation.next_node_name, this.data.progress_next.operation.forward);
|
|
108
110
|
if (guard) {
|
|
109
|
-
guards.push(guard)
|
|
111
|
+
guards.push(guard);
|
|
110
112
|
}
|
|
111
113
|
}
|
|
112
114
|
}
|
|
@@ -256,15 +258,21 @@ export class CallMachine extends CallBase { //@ todo self-owned node operate
|
|
|
256
258
|
} else {
|
|
257
259
|
Progress.From(txb, obj?.get_object(), perm, p!).hold(this.data.progress_hold.operation, this.data.progress_hold.bHold)
|
|
258
260
|
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (this.data?.progress_next !== undefined) {
|
|
264
|
+
const p = this.data?.progress_next.progress
|
|
265
|
+
? await LocalMark.Instance().get_address(this.data?.progress_next.progress)
|
|
266
|
+
: new_progress?.get_object();
|
|
267
|
+
if (!p) ERROR(Errors.InvalidParam, 'CallMachine_Data.data.progress_next.progress');
|
|
268
|
+
Progress.From(txb, obj?.get_object(), perm, p!).next(this.data.progress_next.operation, this.data.progress_next.deliverable, pst)
|
|
259
269
|
}
|
|
270
|
+
|
|
260
271
|
const addr = new_progress?.launch();
|
|
261
272
|
if (addr) {
|
|
262
273
|
await this.new_with_mark('Progress', txb, addr, this.data?.progress_new?.namedNew, account);
|
|
263
274
|
}
|
|
264
275
|
|
|
265
|
-
if (this.data?.progress_next !== undefined) {
|
|
266
|
-
Progress.From(txb, obj?.get_object(), perm, this.data?.progress_next.progress).next(this.data.progress_next.operation, this.data.progress_next.deliverable, pst)
|
|
267
|
-
}
|
|
268
276
|
if (this.data?.bPaused !== undefined) {
|
|
269
277
|
obj?.pause(this.data.bPaused, pst)
|
|
270
278
|
}
|
package/src/call/permission.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Account } from "../local/account.js";
|
|
1
2
|
import { LocalMark } from "../local/local.js";
|
|
2
3
|
import { CallBase, CallResult, Namedbject } from "./base.js";
|
|
3
4
|
import { PassportObject, IsValidAddress, Errors, ERROR, Permission, Permission_Entity, Permission_Index, BizPermission,
|
|
@@ -129,8 +130,8 @@ export class CallPermission extends CallBase {
|
|
|
129
130
|
}
|
|
130
131
|
}
|
|
131
132
|
if (this.data?.builder !== undefined ) {
|
|
132
|
-
const b = await
|
|
133
|
-
if (b) obj?.change_owner(b);
|
|
133
|
+
const b = await Account.Instance().get(this.data.builder);
|
|
134
|
+
if (b) obj?.change_owner(b.address);
|
|
134
135
|
}
|
|
135
136
|
if (!this.object_address) {
|
|
136
137
|
await this.new_with_mark('Permission', txb, obj.launch(), (this.data?.object as any)?.namedNew, account);
|
package/src/call/service.ts
CHANGED
|
@@ -131,9 +131,11 @@ export class CallService extends CallBase {
|
|
|
131
131
|
perms.push(PermissionIndex.service_sales)
|
|
132
132
|
}
|
|
133
133
|
if (this.data?.order_new !== undefined) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
134
|
+
if (this.data.order_new.guard) {
|
|
135
|
+
const guard = await LocalMark.Instance().get_address(this.data.order_new.guard);
|
|
136
|
+
if (guard) {
|
|
137
|
+
guards.push(guard)
|
|
138
|
+
}
|
|
137
139
|
} else {
|
|
138
140
|
if (!object_address) {
|
|
139
141
|
const buy_guard = await LocalMark.Instance().get_address(this.data?.buy_guard);
|
package/src/call/treasury.ts
CHANGED
|
@@ -73,9 +73,11 @@ export class CallTreasury extends CallBase {
|
|
|
73
73
|
perms.push(PermissionIndex.treasury_deposit_guard)
|
|
74
74
|
}
|
|
75
75
|
if (this.data?.deposit !== undefined) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
if (this.data.deposit.guard) {
|
|
77
|
+
const guard = await LocalMark.Instance().get_address(this.data?.deposit?.guard);
|
|
78
|
+
if (guard) {
|
|
79
|
+
guards.push(guard)
|
|
80
|
+
}
|
|
79
81
|
} else {
|
|
80
82
|
if (!object_address) {
|
|
81
83
|
const guard = await LocalMark.Instance().get_address(this.data.deposit_guard);
|
package/src/local/account.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* account management and use
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { Ed25519Keypair, fromHEX, toHEX, decodeSuiPrivateKey, Protocol, TransactionBlock,
|
|
5
|
+
import { Ed25519Keypair, fromHEX, toHEX, decodeSuiPrivateKey, Protocol, TransactionBlock,
|
|
6
6
|
getFaucetHost, requestSuiFromFaucetV0, requestSuiFromFaucetV1, CoinBalance, CoinStruct, TransactionArgument, TransactionResult,
|
|
7
|
-
CallResponse, TransactionObjectArgument} from 'wowok';
|
|
7
|
+
CallResponse, TransactionObjectArgument, Errors, ERROR, IsValidName} from 'wowok';
|
|
8
8
|
import { isBrowser } from '../common.js';
|
|
9
9
|
import path from 'path';
|
|
10
10
|
import os from 'os';
|
|
@@ -12,8 +12,16 @@ import { Level } from 'level';
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
const AccountLocation = 'wowok-acc';
|
|
15
|
-
const
|
|
15
|
+
const AccountKey = 'account';
|
|
16
16
|
|
|
17
|
+
export interface AccountData {
|
|
18
|
+
address: string;
|
|
19
|
+
secret?: string;
|
|
20
|
+
pubkey?: string;
|
|
21
|
+
name?: string;
|
|
22
|
+
suspended?: boolean;
|
|
23
|
+
default?: boolean;
|
|
24
|
+
}
|
|
17
25
|
export class Account {
|
|
18
26
|
private storage;
|
|
19
27
|
|
|
@@ -34,112 +42,228 @@ export class Account {
|
|
|
34
42
|
}; return Account._instance
|
|
35
43
|
}
|
|
36
44
|
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
private accountData(data:AccountData | undefined) : AccountData | undefined {
|
|
46
|
+
if (!data) return ;
|
|
47
|
+
|
|
48
|
+
data.pubkey = Ed25519Keypair.fromSecretKey(fromHEX(data.secret!)).getPublicKey().toSuiPublicKey();
|
|
49
|
+
data.secret = undefined;
|
|
50
|
+
return data;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async set_default(address_or_name: string) : Promise<boolean> {
|
|
54
|
+
const r = await this.storage.get(AccountKey);
|
|
55
|
+
var found = false;
|
|
56
|
+
if (r) {
|
|
57
|
+
const s = JSON.parse(r) as AccountData[];
|
|
58
|
+
for (let i = 0; i < s.length; i++) {
|
|
59
|
+
if (s[i].address === address_or_name || s[i].name === address_or_name && !found) {
|
|
60
|
+
s[i].default = true;
|
|
61
|
+
found = true;
|
|
62
|
+
} else {
|
|
63
|
+
s[i].default = false;
|
|
64
|
+
}
|
|
41
65
|
}
|
|
42
|
-
await this.storage.put(
|
|
43
|
-
} else {
|
|
44
|
-
await this.storage.del(SettingDefault);
|
|
66
|
+
await this.storage.put(AccountKey, JSON.stringify(s));
|
|
45
67
|
}
|
|
46
|
-
|
|
68
|
+
|
|
69
|
+
return found;
|
|
47
70
|
}
|
|
48
71
|
|
|
49
|
-
async gen(bDefault?: boolean) : Promise<
|
|
72
|
+
async gen(bDefault?: boolean, name?:string) : Promise<AccountData> {
|
|
73
|
+
if (name && !IsValidName(name)) {
|
|
74
|
+
ERROR(Errors.IsValidName, `Name ${name} is not valid`);
|
|
75
|
+
}
|
|
76
|
+
|
|
50
77
|
var secret = '0x'+toHEX(decodeSuiPrivateKey(Ed25519Keypair.generate().getSecretKey()).secretKey);
|
|
51
78
|
var address = Ed25519Keypair.fromSecretKey(fromHEX(secret)).getPublicKey().toSuiAddress();
|
|
52
|
-
await this.storage.
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
79
|
+
const r = await this.storage.get(AccountKey);
|
|
80
|
+
if (r) {
|
|
81
|
+
const s = JSON.parse(r) as AccountData[];
|
|
82
|
+
if (name) {
|
|
83
|
+
if (s.find(v => v.name === name)) {
|
|
84
|
+
ERROR(Errors.IsValidName, `Name ${name} already exists`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (bDefault) {
|
|
89
|
+
s.forEach(v => {
|
|
90
|
+
if (v.default) {
|
|
91
|
+
v.default = false;
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const ret:AccountData = {address: address, secret:secret, name: name ? name:undefined, default: bDefault};
|
|
97
|
+
s.push(ret);
|
|
98
|
+
|
|
99
|
+
await this.storage.put(AccountKey, JSON.stringify(s));
|
|
100
|
+
return this.accountData(ret)!;
|
|
101
|
+
} else {
|
|
102
|
+
const ret:AccountData = {address: address, secret:secret, name: name ? name:undefined, default: bDefault};
|
|
103
|
+
await this.storage.put(AccountKey, JSON.stringify([ret]));
|
|
104
|
+
return this.accountData(ret)!;
|
|
105
|
+
}
|
|
57
106
|
}
|
|
58
107
|
|
|
59
|
-
async default(
|
|
60
|
-
const r = await this.storage.get(
|
|
108
|
+
async default() : Promise<AccountData | undefined> {
|
|
109
|
+
const r = await this.storage.get(AccountKey);
|
|
61
110
|
if (r) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return await this.gen(true);
|
|
111
|
+
const s = JSON.parse(r) as AccountData[];
|
|
112
|
+
return this.accountData(s.find(v => v.default));
|
|
65
113
|
}
|
|
66
114
|
}
|
|
67
115
|
|
|
68
116
|
// address: if undefined, the default returned.
|
|
69
|
-
async
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
117
|
+
async get(address_or_name?: string) : Promise<AccountData | undefined> {
|
|
118
|
+
return this.accountData(await this.get_imp(address_or_name));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private async get_imp(address_or_name?: string) : Promise<AccountData | undefined> {
|
|
122
|
+
const r = await this.storage.get(AccountKey);
|
|
123
|
+
if (r) {
|
|
124
|
+
const s = JSON.parse(r) as AccountData[];
|
|
125
|
+
if (!address_or_name) {
|
|
126
|
+
return s.find(v => v.default);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return s.find(v => v.address === address_or_name || v.name === address_or_name);
|
|
75
130
|
}
|
|
76
131
|
}
|
|
77
132
|
|
|
78
|
-
async
|
|
79
|
-
return
|
|
133
|
+
async get_many(address_or_names: (string | null | undefined)[]) : Promise<(AccountData | undefined)[]> {
|
|
134
|
+
return await this.get_many_imp(address_or_names).then(v => v.map(i => this.accountData(i)));
|
|
80
135
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
|
|
136
|
+
private async get_many_imp(address_or_names: (string | null | undefined)[]) : Promise<(AccountData | undefined)[]> {
|
|
137
|
+
const r = await this.storage.get(AccountKey);
|
|
138
|
+
if (r) {
|
|
139
|
+
const s = JSON.parse(r) as AccountData[];
|
|
140
|
+
return address_or_names.map(i => {
|
|
141
|
+
if (!i) {
|
|
142
|
+
return s.find(v => v.default);
|
|
143
|
+
} else {
|
|
144
|
+
return s.find(v => v.address === i || v.name === i);
|
|
145
|
+
}
|
|
146
|
+
})
|
|
85
147
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
148
|
+
return address_or_names.map(v => undefined);
|
|
149
|
+
}
|
|
150
|
+
async set_name(name:string, address_or_name?:string) : Promise<boolean> {
|
|
151
|
+
if (!IsValidName(name)) {
|
|
152
|
+
ERROR(Errors.IsValidName, `Name ${name} is not valid`);
|
|
89
153
|
}
|
|
154
|
+
|
|
155
|
+
const r = await this.storage.get(AccountKey);
|
|
156
|
+
if (r) {
|
|
157
|
+
const s = JSON.parse(r) as AccountData[];
|
|
158
|
+
if (s.find(v => v.name === name)) {
|
|
159
|
+
ERROR(Errors.IsValidName, `Name ${name} already exists`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (!address_or_name) {
|
|
163
|
+
const f = s.find(v => v.default);
|
|
164
|
+
if (f) {
|
|
165
|
+
f.name = name;
|
|
166
|
+
await this.storage.put(AccountKey, JSON.stringify(s));
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
const f = s.find(v => v.address === address_or_name || v.name === address_or_name);
|
|
171
|
+
if (f) {
|
|
172
|
+
f.name = name;
|
|
173
|
+
await this.storage.put(AccountKey, JSON.stringify(s));
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
90
179
|
}
|
|
91
180
|
|
|
92
|
-
async
|
|
93
|
-
const
|
|
94
|
-
if (
|
|
95
|
-
|
|
181
|
+
async list(showSuspended?:boolean) : Promise<AccountData[]> {
|
|
182
|
+
const r = await this.storage.get(AccountKey);
|
|
183
|
+
if (r) {
|
|
184
|
+
const s = JSON.parse(r) as AccountData[];
|
|
185
|
+
if (showSuspended) {
|
|
186
|
+
return s.map(v => this.accountData(v)!);
|
|
187
|
+
} else {
|
|
188
|
+
return s.filter(v => !v.suspended).map(v => this.accountData(v)!);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return [];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async suspend(address_or_name?:string, suspend:boolean=true) : Promise<void> {
|
|
195
|
+
const r = await this.storage.get(AccountKey);
|
|
196
|
+
if (r) {
|
|
197
|
+
const s = JSON.parse(r) as AccountData[];
|
|
198
|
+
if (!address_or_name) {
|
|
199
|
+
const f = s.find(v => v.default);
|
|
200
|
+
if (f) {
|
|
201
|
+
f.suspended = suspend;
|
|
202
|
+
f.name = undefined;
|
|
203
|
+
await this.storage.put(AccountKey, JSON.stringify(s));
|
|
204
|
+
}
|
|
205
|
+
} else {
|
|
206
|
+
const f = s.find(v => v.address === address_or_name || v.name === address_or_name);
|
|
207
|
+
if (f) {
|
|
208
|
+
f.suspended = suspend;
|
|
209
|
+
f.name = undefined;
|
|
210
|
+
await this.storage.put(AccountKey, JSON.stringify(s));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
96
213
|
}
|
|
214
|
+
}
|
|
97
215
|
|
|
98
|
-
|
|
216
|
+
async faucet(address_or_name?:string) {
|
|
217
|
+
const a = await this.get(address_or_name);
|
|
99
218
|
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
transaction: txb,
|
|
103
|
-
signer: Ed25519Keypair.fromSecretKey(fromHEX(secret)),
|
|
104
|
-
options:{showObjectChanges:true},
|
|
105
|
-
});
|
|
219
|
+
if (a) {
|
|
220
|
+
await requestSuiFromFaucetV0({host:getFaucetHost('testnet'), recipient:a.address}).catch(e => {})
|
|
106
221
|
}
|
|
107
222
|
}
|
|
108
223
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
|
|
224
|
+
async sign_and_commit(txb: TransactionBlock, address_or_name?:string) : Promise<CallResponse | undefined> {
|
|
225
|
+
const a = await this.get_imp(address_or_name);
|
|
226
|
+
if (a) {
|
|
227
|
+
const pair = Ed25519Keypair.fromSecretKey(fromHEX(a.secret!));
|
|
228
|
+
if (pair) {
|
|
229
|
+
return await Protocol.Client().signAndExecuteTransaction({
|
|
230
|
+
transaction: txb,
|
|
231
|
+
signer: pair,
|
|
232
|
+
options:{showObjectChanges:true},
|
|
233
|
+
});
|
|
234
|
+
}
|
|
113
235
|
}
|
|
236
|
+
}
|
|
114
237
|
|
|
115
|
-
|
|
116
|
-
|
|
238
|
+
// token_type is 0x2::sui::SUI, if not specified.
|
|
239
|
+
balance = async (address_or_name?:string, token_type?:string) : Promise<CoinBalance | undefined> => {
|
|
240
|
+
const a = await this.get(address_or_name);
|
|
241
|
+
const token_type_ = token_type ?? '0x2::sui::SUI';
|
|
242
|
+
if (a) {
|
|
243
|
+
return await Protocol.Client().getBalance({owner: a.address, coinType:token_type_});
|
|
117
244
|
}
|
|
118
245
|
}
|
|
119
246
|
|
|
120
247
|
// token_type is 0x2::sui::SUI, if not specified.
|
|
121
|
-
coin = async (
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return (await Protocol.Client().getCoins({owner: address, coinType:token_type})).data;
|
|
248
|
+
coin = async (token_type?:string, address_or_name?:string) : Promise<CoinStruct[] | undefined> => {
|
|
249
|
+
const a = await this.get(address_or_name);
|
|
250
|
+
const token_type_ = token_type ?? '0x2::sui::SUI';
|
|
251
|
+
if (a) {
|
|
252
|
+
return (await Protocol.Client().getCoins({owner: a.address, coinType:token_type_})).data;
|
|
127
253
|
}
|
|
128
254
|
}
|
|
129
255
|
|
|
130
|
-
get_coin_object = async (txb: TransactionBlock, balance_required:string | bigint | number,
|
|
131
|
-
|
|
132
|
-
address = await this.default();
|
|
133
|
-
}
|
|
256
|
+
get_coin_object = async (txb: TransactionBlock, balance_required:string | bigint | number, address_or_name?:string, token_type?:string) : Promise<TransactionResult | undefined> => {
|
|
257
|
+
const a = await this.get(address_or_name);
|
|
134
258
|
|
|
135
|
-
if (
|
|
259
|
+
if (a) {
|
|
136
260
|
const b = BigInt(balance_required);
|
|
137
261
|
|
|
138
262
|
if (b >= BigInt(0)) {
|
|
139
263
|
if (!token_type || token_type === '0x2::sui::SUI' || token_type === '0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI') {
|
|
140
264
|
return txb.splitCoins(txb.gas, [b]);
|
|
141
265
|
} else {
|
|
142
|
-
const r = await Protocol.Client().getCoins({owner: address, coinType:token_type});
|
|
266
|
+
const r = await Protocol.Client().getCoins({owner: a.address , coinType:token_type});
|
|
143
267
|
const objects : string[] = []; var current = BigInt(0);
|
|
144
268
|
for (let i = 0; i < r.data.length; ++ i) {
|
|
145
269
|
current += BigInt(r.data[i].balance);
|
|
@@ -160,41 +284,39 @@ export class Account {
|
|
|
160
284
|
}
|
|
161
285
|
}
|
|
162
286
|
}
|
|
163
|
-
async transfer(
|
|
164
|
-
const
|
|
165
|
-
if (!
|
|
166
|
-
const
|
|
167
|
-
if (!
|
|
287
|
+
async transfer(amount:number|string, token_type?:string, to_address_or_name?:string, from_address_or_name?:string) : Promise<CallResponse | undefined> {
|
|
288
|
+
const [from, to]= await this.get_many_imp([from_address_or_name, to_address_or_name]);
|
|
289
|
+
if (!from) ERROR(Errors.InvalidParam, `Invalid from address or name ${from_address_or_name}`);
|
|
290
|
+
const to_address = to?.address ?? to_address_or_name;
|
|
291
|
+
if (!to_address) ERROR(Errors.InvalidParam, `Invalid to address or name ${to_address_or_name}`);
|
|
168
292
|
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
293
|
+
const pair = Ed25519Keypair.fromSecretKey(fromHEX(from.secret!))
|
|
294
|
+
if (pair) {
|
|
295
|
+
const txb = new TransactionBlock();
|
|
296
|
+
const coin = await this.get_coin_object(txb, amount, from.address, token_type);
|
|
297
|
+
if (coin) {
|
|
298
|
+
txb.transferObjects([coin as TransactionObjectArgument], to_address)
|
|
299
|
+
const r = await Protocol.Client().signAndExecuteTransaction({
|
|
300
|
+
transaction: txb,
|
|
301
|
+
signer: pair,
|
|
302
|
+
options:{showObjectChanges:true},
|
|
303
|
+
});
|
|
304
|
+
return r;
|
|
305
|
+
}
|
|
179
306
|
}
|
|
180
307
|
}
|
|
181
308
|
|
|
182
|
-
coinObject_with_balance = async(balance_required:string | bigint | number,
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (!address) return undefined;
|
|
188
|
-
const secret = await this.storage.get(address);
|
|
189
|
-
if (!secret) return undefined;
|
|
309
|
+
coinObject_with_balance = async(balance_required:string | bigint | number, address_or_name?:string, token_type?:string) : Promise<string | undefined> => {
|
|
310
|
+
const a = await this.get_imp(address_or_name);
|
|
311
|
+
if (!a) return undefined;
|
|
190
312
|
|
|
191
|
-
const pair = Ed25519Keypair.fromSecretKey(fromHEX(secret))
|
|
313
|
+
const pair = Ed25519Keypair.fromSecretKey(fromHEX(a.secret!))
|
|
192
314
|
if (!pair) return undefined;
|
|
193
315
|
|
|
194
316
|
const txb = new TransactionBlock();
|
|
195
|
-
const res = await this.get_coin_object(txb, balance_required, address, token_type);
|
|
317
|
+
const res = await this.get_coin_object(txb, balance_required, a.address, token_type);
|
|
196
318
|
if (res) {
|
|
197
|
-
txb.transferObjects([res as TransactionObjectArgument], address)
|
|
319
|
+
txb.transferObjects([res as TransactionObjectArgument], a.address)
|
|
198
320
|
const r = await Protocol.Client().signAndExecuteTransaction({
|
|
199
321
|
transaction: txb,
|
|
200
322
|
signer: pair,
|
package/src/local/index.ts
CHANGED
|
@@ -1,27 +1,19 @@
|
|
|
1
|
-
import { CallResponse, CoinBalance, CoinStruct } from "wowok"
|
|
2
|
-
import { Account } from "./account.js"
|
|
1
|
+
import { CallResponse, CoinBalance, CoinStruct, Protocol } from "wowok"
|
|
2
|
+
import { Account, AccountData } from "./account.js"
|
|
3
3
|
import { LocalInfo, LocalInfoNameDefault, LocalMark, LocalMarkFilter, MarkData } from "./local.js"
|
|
4
4
|
|
|
5
5
|
export const query_local_mark_list = async (filter?:LocalMarkFilter) : Promise<string> => {
|
|
6
6
|
return JSON.stringify(await LocalMark.Instance().list(filter))
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export const query_account_list = async () : Promise<
|
|
10
|
-
|
|
11
|
-
res.addresses = await Account.Instance().list();
|
|
12
|
-
res.default = await Account.Instance().default(false);
|
|
13
|
-
return res;
|
|
9
|
+
export const query_account_list = async (showSuspendedAccount?:boolean) : Promise<AccountData[]> => {
|
|
10
|
+
return await Account.Instance().list(showSuspendedAccount);
|
|
14
11
|
}
|
|
15
12
|
|
|
16
13
|
export const query_local_info_list = async () : Promise<string> => {
|
|
17
14
|
return JSON.stringify(await LocalInfo.Instance().list())
|
|
18
15
|
}
|
|
19
16
|
|
|
20
|
-
export interface QueryAccountsResult {
|
|
21
|
-
default?: string;
|
|
22
|
-
addresses?: string[];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
17
|
export const query_local_mark = async (name: string) : Promise<MarkData | undefined> => {
|
|
26
18
|
return await LocalMark.Instance().get(name)
|
|
27
19
|
}
|
|
@@ -46,15 +38,19 @@ export interface QueryAccountResult {
|
|
|
46
38
|
}
|
|
47
39
|
|
|
48
40
|
export const query_account = async (query: QueryAccount) : Promise<QueryAccountResult> => {
|
|
49
|
-
const r = await
|
|
50
|
-
|
|
51
|
-
|
|
41
|
+
const r = await Account.Instance().get(query.name_or_address);
|
|
42
|
+
if (!r) {
|
|
43
|
+
return {name_or_address: query.name_or_address};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const res : QueryAccountResult = {address: r.address, name_or_address: r.name};
|
|
52
47
|
|
|
53
48
|
if (r) {
|
|
49
|
+
const token_type_ = query.token_type ?? '0x2::sui::SUI';
|
|
54
50
|
if (query?.balance_or_coin === BalanceOrCoin.Balance) {
|
|
55
|
-
res.balance = await
|
|
51
|
+
res.balance = await Protocol.Client().getBalance({owner: r.address, coinType:token_type_});
|
|
56
52
|
} else if (query?.balance_or_coin === BalanceOrCoin.Coin) {
|
|
57
|
-
res.coin =
|
|
53
|
+
res.coin = (await Protocol.Client().getCoins({owner: r.address, coinType:token_type_})).data;
|
|
58
54
|
}
|
|
59
55
|
}
|
|
60
56
|
|
|
@@ -66,29 +62,36 @@ export const query_local_info = async (name: string = LocalInfoNameDefault) : Pr
|
|
|
66
62
|
}
|
|
67
63
|
|
|
68
64
|
export interface AccountOperation {
|
|
69
|
-
gen?: {name?:string, default?: boolean
|
|
70
|
-
|
|
65
|
+
gen?: {name?:string, default?: boolean} | null; // generate a new account, if not specified, generate a new default account.
|
|
66
|
+
default?: {name_or_address: string} | null; // set the default account.
|
|
67
|
+
suspend?: {name_or_address?: string, suspend?:boolean} | null; // suspend the account, if not specified, suspend the default account.
|
|
68
|
+
name?: {new_name:string, name_or_address?:string} | null; // name the account, if not specified, name the default account.
|
|
69
|
+
transfer?: {name_or_address_from?: string, name_or_address_to?:string, amount:number|string, token_type?: string} | null; // transfer the token.
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
export interface AccountOperationResult {
|
|
74
|
-
gen?: {address:string, default
|
|
73
|
+
gen?: {address:string, default?:boolean, name?: string};
|
|
75
74
|
transfer?: CallResponse;
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
export const account_operation = async(op: AccountOperation) : Promise<AccountOperationResult> => {
|
|
79
78
|
var res : AccountOperationResult = {};
|
|
80
79
|
if (op.gen) {
|
|
81
|
-
const acc = await Account.Instance().gen(op.gen?.default);
|
|
82
|
-
|
|
83
|
-
res.gen = {address: acc, default: op.gen.default ?? false, name:name};
|
|
80
|
+
const acc = await Account.Instance().gen(op.gen?.default, op.gen?.name);
|
|
81
|
+
res.gen = {address: acc?.address, default: acc?.default, name:acc?.name};
|
|
84
82
|
}
|
|
85
|
-
|
|
83
|
+
if (op.default) {
|
|
84
|
+
await Account.Instance().set_default(op.default.name_or_address);
|
|
85
|
+
}
|
|
86
|
+
if (op.suspend) {
|
|
87
|
+
await Account.Instance().suspend(op.suspend.name_or_address, op.suspend.suspend);
|
|
88
|
+
}
|
|
89
|
+
if (op.name) {
|
|
90
|
+
await Account.Instance().set_name(op.name.new_name, op.name.name_or_address);
|
|
91
|
+
}
|
|
92
|
+
|
|
86
93
|
if(op.transfer) {
|
|
87
|
-
|
|
88
|
-
const to = await LocalMark.Instance().get_account(op.transfer.name_or_address_to, false);
|
|
89
|
-
if (from && to) {
|
|
90
|
-
res.transfer = await Account.Instance().transfer(from, to, op.transfer?.amount, op.transfer?.token_type);
|
|
91
|
-
}
|
|
94
|
+
res.transfer = await Account.Instance().transfer(op.transfer.amount, op.transfer.token_type, op.transfer.name_or_address_to, op.transfer.name_or_address_from);
|
|
92
95
|
}
|
|
93
96
|
return res;
|
|
94
97
|
}
|
package/src/local/local.ts
CHANGED
|
@@ -8,7 +8,6 @@ import os from "os";
|
|
|
8
8
|
import { Level } from "level";
|
|
9
9
|
import { isBrowser } from "../common.js";
|
|
10
10
|
import { ERROR, Errors, IsValidAddress, TagName } from "wowok";
|
|
11
|
-
import { Account } from "./account.js";
|
|
12
11
|
|
|
13
12
|
export interface MarkData {
|
|
14
13
|
object: string;
|
|
@@ -121,31 +120,6 @@ export class LocalMark {
|
|
|
121
120
|
return (await this.get_many_address(name_or_addresses)).filter(v => v!==undefined && v!== null) as string[]
|
|
122
121
|
}
|
|
123
122
|
|
|
124
|
-
// get account address:
|
|
125
|
-
// 1. if name_or_address is undefined, return default account address.
|
|
126
|
-
// 2. if name_or_address is address, return it.
|
|
127
|
-
// 3. if name_or_address is name, return the address of the name.
|
|
128
|
-
// 4. if not found and genNewIfNotFound is true, generate a new address and save it with name_or_address.
|
|
129
|
-
async get_account(name_or_address?: string, genNewIfNotFound:boolean=false) : Promise<string | undefined> {
|
|
130
|
-
if (name_or_address === undefined) {
|
|
131
|
-
return Account.Instance().default(genNewIfNotFound);
|
|
132
|
-
} else {
|
|
133
|
-
const r = await this.get(name_or_address);
|
|
134
|
-
if (r) {
|
|
135
|
-
return r.object;
|
|
136
|
-
} else {
|
|
137
|
-
if (IsValidAddress(name_or_address)) {
|
|
138
|
-
return name_or_address;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (genNewIfNotFound) {
|
|
142
|
-
const addr = await Account.Instance().gen(false);
|
|
143
|
-
await this.put(name_or_address, {object:addr, tags:[TagName.Account]});
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
123
|
async del(name:string) {
|
|
150
124
|
return await this.storage.del(name);
|
|
151
125
|
}
|