wowok_agent 1.2.40 → 1.2.41
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 +199 -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/package.json +1 -1
- 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 +212 -92
- package/src/local/index.ts +32 -29
- package/src/local/local.ts +0 -26
- package/tsconfig.tsbuildinfo +1 -1
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,226 @@ 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?: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) {
|
|
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);
|
|
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
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
const f = s.find(v => v.address === address_or_name || v.name === address_or_name);
|
|
205
|
+
if (f) {
|
|
206
|
+
f.suspended = suspend;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
await this.storage.put(AccountKey, JSON.stringify(s));
|
|
96
210
|
}
|
|
211
|
+
}
|
|
97
212
|
|
|
98
|
-
|
|
213
|
+
async faucet(address_or_name?:string) {
|
|
214
|
+
const a = await this.get(address_or_name);
|
|
99
215
|
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
transaction: txb,
|
|
103
|
-
signer: Ed25519Keypair.fromSecretKey(fromHEX(secret)),
|
|
104
|
-
options:{showObjectChanges:true},
|
|
105
|
-
});
|
|
216
|
+
if (a) {
|
|
217
|
+
await requestSuiFromFaucetV0({host:getFaucetHost('testnet'), recipient:a.address}).catch(e => {})
|
|
106
218
|
}
|
|
107
219
|
}
|
|
108
220
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
221
|
+
async sign_and_commit(txb: TransactionBlock, address_or_name?:string) : Promise<CallResponse | undefined> {
|
|
222
|
+
const a = await this.get_imp(address_or_name);
|
|
223
|
+
console.log('sign_and_commit', a, address_or_name);
|
|
224
|
+
if (a) {
|
|
225
|
+
const pair = Ed25519Keypair.fromSecretKey(fromHEX(a.secret!));
|
|
226
|
+
if (pair) {
|
|
227
|
+
return await Protocol.Client().signAndExecuteTransaction({
|
|
228
|
+
transaction: txb,
|
|
229
|
+
signer: pair,
|
|
230
|
+
options:{showObjectChanges:true},
|
|
231
|
+
});
|
|
232
|
+
}
|
|
113
233
|
}
|
|
234
|
+
}
|
|
114
235
|
|
|
115
|
-
|
|
116
|
-
|
|
236
|
+
// token_type is 0x2::sui::SUI, if not specified.
|
|
237
|
+
balance = async (address_or_name?:string, token_type?:string) : Promise<CoinBalance | undefined> => {
|
|
238
|
+
const a = await this.get(address_or_name);
|
|
239
|
+
const token_type_ = token_type ?? '0x2::sui::SUI';
|
|
240
|
+
if (a) {
|
|
241
|
+
return await Protocol.Client().getBalance({owner: a.address, coinType:token_type_});
|
|
117
242
|
}
|
|
118
243
|
}
|
|
119
244
|
|
|
120
245
|
// 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;
|
|
246
|
+
coin = async (token_type?:string, address_or_name?:string) : Promise<CoinStruct[] | undefined> => {
|
|
247
|
+
const a = await this.get(address_or_name);
|
|
248
|
+
const token_type_ = token_type ?? '0x2::sui::SUI';
|
|
249
|
+
if (a) {
|
|
250
|
+
return (await Protocol.Client().getCoins({owner: a.address, coinType:token_type_})).data;
|
|
127
251
|
}
|
|
128
252
|
}
|
|
129
253
|
|
|
130
|
-
get_coin_object = async (txb: TransactionBlock, balance_required:string | bigint | number,
|
|
131
|
-
|
|
132
|
-
address = await this.default();
|
|
133
|
-
}
|
|
254
|
+
get_coin_object = async (txb: TransactionBlock, balance_required:string | bigint | number, address_or_name?:string, token_type?:string) : Promise<TransactionResult | undefined> => {
|
|
255
|
+
const a = await this.get(address_or_name);
|
|
134
256
|
|
|
135
|
-
if (
|
|
257
|
+
if (a) {
|
|
136
258
|
const b = BigInt(balance_required);
|
|
137
259
|
|
|
138
260
|
if (b >= BigInt(0)) {
|
|
139
261
|
if (!token_type || token_type === '0x2::sui::SUI' || token_type === '0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI') {
|
|
140
262
|
return txb.splitCoins(txb.gas, [b]);
|
|
141
263
|
} else {
|
|
142
|
-
const r = await Protocol.Client().getCoins({owner: address, coinType:token_type});
|
|
264
|
+
const r = await Protocol.Client().getCoins({owner: a.address , coinType:token_type});
|
|
143
265
|
const objects : string[] = []; var current = BigInt(0);
|
|
144
266
|
for (let i = 0; i < r.data.length; ++ i) {
|
|
145
267
|
current += BigInt(r.data[i].balance);
|
|
@@ -160,41 +282,39 @@ export class Account {
|
|
|
160
282
|
}
|
|
161
283
|
}
|
|
162
284
|
}
|
|
163
|
-
async transfer(
|
|
164
|
-
const
|
|
165
|
-
if (!
|
|
166
|
-
const
|
|
167
|
-
if (!
|
|
285
|
+
async transfer(amount:number|string, token_type?:string, to_address_or_name?:string, from_address_or_name?:string) : Promise<CallResponse | undefined> {
|
|
286
|
+
const [from, to]= await this.get_many_imp([from_address_or_name, to_address_or_name]);
|
|
287
|
+
if (!from) ERROR(Errors.InvalidParam, `Invalid from address or name ${from_address_or_name}`);
|
|
288
|
+
const to_address = to?.address ?? to_address_or_name;
|
|
289
|
+
if (!to_address) ERROR(Errors.InvalidParam, `Invalid to address or name ${to_address_or_name}`);
|
|
168
290
|
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
291
|
+
const pair = Ed25519Keypair.fromSecretKey(fromHEX(from.secret!))
|
|
292
|
+
if (pair) {
|
|
293
|
+
const txb = new TransactionBlock();
|
|
294
|
+
const coin = await this.get_coin_object(txb, amount, from.address, token_type);
|
|
295
|
+
if (coin) {
|
|
296
|
+
txb.transferObjects([coin as TransactionObjectArgument], to_address)
|
|
297
|
+
const r = await Protocol.Client().signAndExecuteTransaction({
|
|
298
|
+
transaction: txb,
|
|
299
|
+
signer: pair,
|
|
300
|
+
options:{showObjectChanges:true},
|
|
301
|
+
});
|
|
302
|
+
return r;
|
|
303
|
+
}
|
|
179
304
|
}
|
|
180
305
|
}
|
|
181
306
|
|
|
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;
|
|
307
|
+
coinObject_with_balance = async(balance_required:string | bigint | number, address_or_name?:string, token_type?:string) : Promise<string | undefined> => {
|
|
308
|
+
const a = await this.get_imp(address_or_name);
|
|
309
|
+
if (!a) return undefined;
|
|
190
310
|
|
|
191
|
-
const pair = Ed25519Keypair.fromSecretKey(fromHEX(secret))
|
|
311
|
+
const pair = Ed25519Keypair.fromSecretKey(fromHEX(a.secret!))
|
|
192
312
|
if (!pair) return undefined;
|
|
193
313
|
|
|
194
314
|
const txb = new TransactionBlock();
|
|
195
|
-
const res = await this.get_coin_object(txb, balance_required, address, token_type);
|
|
315
|
+
const res = await this.get_coin_object(txb, balance_required, a.address, token_type);
|
|
196
316
|
if (res) {
|
|
197
|
-
txb.transferObjects([res as TransactionObjectArgument], address)
|
|
317
|
+
txb.transferObjects([res as TransactionObjectArgument], a.address)
|
|
198
318
|
const r = await Protocol.Client().signAndExecuteTransaction({
|
|
199
319
|
transaction: txb,
|
|
200
320
|
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?: {name:string, 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.name, op.name.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_from, op.transfer.name_or_address_to);
|
|
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
|
}
|