wative 1.2.7 → 1.2.10
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/package.json +23 -23
- package/src/account.ts +0 -864
- package/src/assets.ts +0 -399
- package/src/chain.ts +0 -28
- package/src/config.ts +0 -2074
- package/src/daemon-watcher.js +0 -181
- package/src/home_page.ts +0 -36
- package/src/index.d.ts +0 -12
- package/src/index.ts +0 -53
- package/src/network.ts +0 -819
- package/src/ssh/client.ts +0 -389
- package/src/ssh/config_manager.ts +0 -304
- package/src/ssh/index.ts +0 -66
- package/src/ssh/service_manager.ts +0 -685
- package/src/ssh/types.ts +0 -50
- package/src/ssh/utils.ts +0 -89
- package/src/ssh/wative_server.ts +0 -534
- package/src/tools.ts +0 -923
- package/src/tx_gas_utils.ts +0 -119
- package/src/utils.ts +0 -916
- package/src/wallet-cli.ts +0 -39
- package/src/wative.ts +0 -633
- package/src/web3.ts +0 -415
package/src/account.ts
DELETED
|
@@ -1,864 +0,0 @@
|
|
|
1
|
-
const chalk = require('chalk');
|
|
2
|
-
import * as fs from "fs";
|
|
3
|
-
import * as path from "path";
|
|
4
|
-
import {
|
|
5
|
-
getAccountName,
|
|
6
|
-
selectSomething,
|
|
7
|
-
inputPassword,
|
|
8
|
-
inputSomething,
|
|
9
|
-
tagValidator,
|
|
10
|
-
passphraseValidator,
|
|
11
|
-
expandAmountValidator,
|
|
12
|
-
lastAccountNoValidator,
|
|
13
|
-
getAccountIndex,
|
|
14
|
-
getAccountRange,
|
|
15
|
-
confirmSomething,
|
|
16
|
-
numberValidator,
|
|
17
|
-
formatAddr,
|
|
18
|
-
listWithLazyBalanceLoading,
|
|
19
|
-
inputPasswordWithoutValidator,
|
|
20
|
-
getKeystoreSlugByAccountName,
|
|
21
|
-
getAccountNameByKeystoreSlug,
|
|
22
|
-
inputMaskedPassword,
|
|
23
|
-
importMaskedPassphrase
|
|
24
|
-
} from "./utils";
|
|
25
|
-
import { getAccountAddress, getAccountBalance, getNetworkInfoByName, getNetworkTypeByName, getStatus, selectDefaultNetwork, showAllAccounts, switchNetworkByAccountName } from "./network";
|
|
26
|
-
import { getAssetList, showAssetsDetail, updateTokenBalanceOption } from './assets';
|
|
27
|
-
import { selectToolsOptions } from './tools';
|
|
28
|
-
import inquirer from "inquirer";
|
|
29
|
-
import { getChainType } from "./chain";
|
|
30
|
-
|
|
31
|
-
require("dotenv").config();
|
|
32
|
-
const cliProgress = require('cli-progress');
|
|
33
|
-
const { WativeCore } = require("wative-core");
|
|
34
|
-
|
|
35
|
-
const DEFAULT_ACCOUNT: any = {
|
|
36
|
-
keystore_slug: '',
|
|
37
|
-
default_account_index: 0
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const preAccountImport = async (keystore_path: string) => {
|
|
41
|
-
const networks = getNetworkInfo(keystore_path)
|
|
42
|
-
|
|
43
|
-
let account_name_list: string[] = [];
|
|
44
|
-
if (networks.accounts) {
|
|
45
|
-
account_name_list = networks.accounts;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
let account_name = await getAccountName('Account Name', account_name_list);
|
|
49
|
-
if (account_name === null) {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
let password = await inputPassword(`Please set a password for Account [${account_name}]`);
|
|
54
|
-
let selected_default_network = await selectDefaultNetwork(keystore_path);
|
|
55
|
-
|
|
56
|
-
let pre_account_info = {
|
|
57
|
-
account_name,
|
|
58
|
-
password,
|
|
59
|
-
selected_default_network
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
return pre_account_info;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
// account label
|
|
67
|
-
// cureent network
|
|
68
|
-
// data version 1: only evm or only solana 2: evm and solana
|
|
69
|
-
const importPassphrase = async (keystore_path: string, wative_core: typeof WativeCore) => {
|
|
70
|
-
let pre_account_info = await preAccountImport(keystore_path);
|
|
71
|
-
if (!pre_account_info) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
let passphrase = await importMaskedPassphrase('Please enter a mnemonic [split by space]', passphraseValidator);
|
|
76
|
-
passphrase = passphrase.replace(/\s+/g, " ");
|
|
77
|
-
|
|
78
|
-
let keystore_slug = getKeystoreSlugByAccountName(pre_account_info.account_name);
|
|
79
|
-
const passphrase_account = wative_core.account.generatePPAccount(keystore_slug, passphrase, pre_account_info.password);
|
|
80
|
-
if (!passphrase_account.status) {
|
|
81
|
-
console.log(
|
|
82
|
-
chalk.red(passphrase_account.output)
|
|
83
|
-
);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
let account_info_path = path.join(keystore_path, `accounts/${keystore_slug}.json`);
|
|
87
|
-
|
|
88
|
-
passphrase_account.output.default_network = pre_account_info.selected_default_network;
|
|
89
|
-
fs.writeFileSync(account_info_path, JSON.stringify(passphrase_account.output, null, 4));
|
|
90
|
-
|
|
91
|
-
const network_path = path.resolve(keystore_path, 'network.json');
|
|
92
|
-
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
93
|
-
networks.accounts.push(pre_account_info.account_name);
|
|
94
|
-
fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const createPrivateKeyAccount = async (keystore_path: string, wative_core: typeof WativeCore) => {
|
|
99
|
-
let pre_account_info = await preAccountImport(keystore_path);
|
|
100
|
-
if (!pre_account_info) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
let private_key = await inputMaskedPassword('Please enter a private key');
|
|
105
|
-
let keystore_slug = getKeystoreSlugByAccountName(pre_account_info.account_name);
|
|
106
|
-
await _importAccount(
|
|
107
|
-
keystore_path,
|
|
108
|
-
pre_account_info.password,
|
|
109
|
-
keystore_slug,
|
|
110
|
-
private_key,
|
|
111
|
-
pre_account_info.selected_default_network,
|
|
112
|
-
wative_core,
|
|
113
|
-
true
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
const network_path = path.resolve(keystore_path, 'network.json');
|
|
117
|
-
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
118
|
-
networks.accounts.push(pre_account_info.account_name);
|
|
119
|
-
|
|
120
|
-
fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const importPrivateKeyAccount = async (
|
|
124
|
-
keystore_path: string,
|
|
125
|
-
password: string,
|
|
126
|
-
keystore_slug: string,
|
|
127
|
-
wative_core: typeof WativeCore
|
|
128
|
-
) => {
|
|
129
|
-
let private_key = await inputSomething('Please enter a private key');
|
|
130
|
-
let account_info_path = path.join(keystore_path, `accounts/${keystore_slug}.json`);
|
|
131
|
-
let exist_account_info = JSON.parse(fs.readFileSync(account_info_path, 'utf8'));
|
|
132
|
-
|
|
133
|
-
await _importAccount(
|
|
134
|
-
keystore_path,
|
|
135
|
-
password,
|
|
136
|
-
keystore_slug,
|
|
137
|
-
private_key,
|
|
138
|
-
exist_account_info.default_network,
|
|
139
|
-
wative_core,
|
|
140
|
-
false
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const _importAccount = async (
|
|
145
|
-
keystore_path: string,
|
|
146
|
-
password: string,
|
|
147
|
-
keystore_slug: string,
|
|
148
|
-
private_key: string,
|
|
149
|
-
default_network: string,
|
|
150
|
-
wative_core: typeof WativeCore,
|
|
151
|
-
generate_keystore_slug: boolean
|
|
152
|
-
) => {
|
|
153
|
-
let network_info = await getNetworkInfoByName(keystore_path, default_network);
|
|
154
|
-
|
|
155
|
-
let network_type = getChainType(network_info.chainId.toString());
|
|
156
|
-
if (network_type === 'evm' && !private_key.startsWith('0x')) {
|
|
157
|
-
private_key = "0x" + private_key;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
let privatekey_account = await wative_core.account.generatePKAccount(keystore_slug, private_key, password, network_info.chainId.toString(), generate_keystore_slug);
|
|
161
|
-
if (!privatekey_account.status) {
|
|
162
|
-
console.log(
|
|
163
|
-
chalk.red(privatekey_account.output)
|
|
164
|
-
);
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
let account_info_path = path.join(keystore_path, `accounts/${keystore_slug}.json`);
|
|
168
|
-
|
|
169
|
-
let result: any;
|
|
170
|
-
if (fs.existsSync(account_info_path)) {
|
|
171
|
-
let exist_account_info = JSON.parse(fs.readFileSync(account_info_path, 'utf8'));
|
|
172
|
-
if ("data" in exist_account_info) {
|
|
173
|
-
const exist_account_address_list = exist_account_info.data.map((item: any) => {
|
|
174
|
-
return item.ciphertexts.address;
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
if (exist_account_address_list.includes(privatekey_account.output.ciphertexts.address)) {
|
|
178
|
-
console.log(
|
|
179
|
-
chalk.red("The account has already been imported")
|
|
180
|
-
);
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
exist_account_info.data.push({
|
|
185
|
-
ciphertexts: privatekey_account.output.ciphertexts
|
|
186
|
-
});
|
|
187
|
-
result = exist_account_info;
|
|
188
|
-
} else {
|
|
189
|
-
exist_account_info.data = [
|
|
190
|
-
{ ciphertexts: exist_account_info.ciphertexts }
|
|
191
|
-
]
|
|
192
|
-
delete exist_account_info.ciphertexts;
|
|
193
|
-
exist_account_info.data.push({
|
|
194
|
-
ciphertexts: privatekey_account.output.ciphertexts
|
|
195
|
-
});
|
|
196
|
-
result = exist_account_info;
|
|
197
|
-
}
|
|
198
|
-
} else {
|
|
199
|
-
privatekey_account.output.data = [
|
|
200
|
-
{ ciphertexts: privatekey_account.output.ciphertexts }
|
|
201
|
-
];
|
|
202
|
-
delete privatekey_account.output.ciphertexts;
|
|
203
|
-
privatekey_account.output.default_network = default_network;
|
|
204
|
-
result = privatekey_account.output;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
fs.writeFileSync(account_info_path, JSON.stringify(result, null, 4));
|
|
208
|
-
|
|
209
|
-
wative_core.account.reloadAccount();
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const createAccount = async (keystore_path: string, wative_core: typeof WativeCore) => {
|
|
213
|
-
const create_account_options = [
|
|
214
|
-
'> Import Passphrase',
|
|
215
|
-
'> Import Private Key',
|
|
216
|
-
new inquirer.Separator('----------------------------------'),
|
|
217
|
-
'> Back'
|
|
218
|
-
];
|
|
219
|
-
|
|
220
|
-
let selected_create_account = await selectSomething(create_account_options);
|
|
221
|
-
|
|
222
|
-
switch (selected_create_account) {
|
|
223
|
-
case '> Import Passphrase': {
|
|
224
|
-
await importPassphrase(keystore_path, wative_core);
|
|
225
|
-
break;
|
|
226
|
-
}
|
|
227
|
-
case '> Import Private Key': {
|
|
228
|
-
await createPrivateKeyAccount(keystore_path, wative_core);
|
|
229
|
-
break;
|
|
230
|
-
}
|
|
231
|
-
case '> Back': {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
wative_core.account.reloadAccount();
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export const getNetworkInfo = (keystore_path: string) => {
|
|
240
|
-
const network_path = path.resolve(keystore_path, 'network.json');
|
|
241
|
-
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
242
|
-
return networks;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export const getAccountInfo = (keystore_path: string, keystore_slug: string) => {
|
|
246
|
-
const account_info_path = path.join(keystore_path, `accounts/${keystore_slug}.json`);
|
|
247
|
-
const account_info = JSON.parse(fs.readFileSync(account_info_path, 'utf8'));
|
|
248
|
-
return account_info;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
export const saveAccountInfo = (keystore_path: string, keystore_slug: string, account_info: any) => {
|
|
252
|
-
const account_info_path = path.join(keystore_path, `accounts/${keystore_slug}.json`);
|
|
253
|
-
fs.writeFileSync(account_info_path, JSON.stringify(account_info, null, 4));
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export const loginIn = async (keystore_path: string, keystore_slug: string, wative_core: typeof WativeCore) => {
|
|
257
|
-
const is_login = wative_core.account.isLogin(keystore_slug);
|
|
258
|
-
if (is_login) {
|
|
259
|
-
return { isLoginIn: true };
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
for (let i = 0; i < 3; i++) {
|
|
263
|
-
let account_name = getAccountNameByKeystoreSlug(keystore_path, keystore_slug);
|
|
264
|
-
let password_name = 'WATIVE_PASSWD_' + keystore_slug;
|
|
265
|
-
let password: any;
|
|
266
|
-
if (process.env[password_name] && process.env[password_name] !== '') {
|
|
267
|
-
password = process.env[password_name];
|
|
268
|
-
} else if (process.env.WativePassword && process.env.WativePassword !== '') {
|
|
269
|
-
password = process.env.WativePassword;
|
|
270
|
-
} else {
|
|
271
|
-
password = await inputPasswordWithoutValidator(`Please input the password for Account [${account_name}]`);
|
|
272
|
-
}
|
|
273
|
-
if (!password) {
|
|
274
|
-
console.log(
|
|
275
|
-
chalk.red("Password can't be empty")
|
|
276
|
-
);
|
|
277
|
-
return { isLoginIn: false };
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const login_result = await wative_core.account.login(keystore_slug, password);
|
|
281
|
-
if (login_result.status) {
|
|
282
|
-
return { isLoginIn: true, password };
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const options = [
|
|
286
|
-
'> Try Again',
|
|
287
|
-
'> Back'
|
|
288
|
-
];
|
|
289
|
-
const selected_option = await selectSomething(options, login_result.output);
|
|
290
|
-
if (selected_option === '> Back') {
|
|
291
|
-
return { isLoginIn: false };
|
|
292
|
-
} else if (selected_option === '> Try Again') {
|
|
293
|
-
continue;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
return { isLoginIn: false };
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
const selectAccount = async (keystore_path: string, wative_core: typeof WativeCore) => {
|
|
300
|
-
const networks = getNetworkInfo(keystore_path);
|
|
301
|
-
const account_name_list = networks.accounts;
|
|
302
|
-
|
|
303
|
-
let select_account_options = [
|
|
304
|
-
'> Back',
|
|
305
|
-
new inquirer.Separator('----------------------------------')
|
|
306
|
-
];
|
|
307
|
-
|
|
308
|
-
const account_name_len = account_name_list.length;
|
|
309
|
-
if (account_name_len === 0) {
|
|
310
|
-
console.log(
|
|
311
|
-
chalk.red("No address here yet")
|
|
312
|
-
);
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
for (let i = 0; i < account_name_len; i++) {
|
|
317
|
-
const account_name = account_name_list[i];
|
|
318
|
-
const keystore_slug = getKeystoreSlugByAccountName(account_name);
|
|
319
|
-
const account_info = getAccountInfo(keystore_path, keystore_slug);
|
|
320
|
-
if ("data" in account_info) {
|
|
321
|
-
select_account_options.push(`${i + 1}) ${account_name} [${account_info.account_type}:${account_info.data.length}]`);
|
|
322
|
-
} else {
|
|
323
|
-
select_account_options.push(`${i + 1}) ${account_name} [${account_info.account_type}:1]`);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
const selected_account = await selectSomething(select_account_options, "Select an account");
|
|
328
|
-
if (selected_account === '> Back') {
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const selected_account_name = account_name_list[select_account_options.indexOf(selected_account) - 2];
|
|
333
|
-
const keystore_slug = getKeystoreSlugByAccountName(selected_account_name);
|
|
334
|
-
|
|
335
|
-
console.log(
|
|
336
|
-
chalk.green(`Keystore slug: [${keystore_slug}]`)
|
|
337
|
-
);
|
|
338
|
-
let login_result = await loginIn(keystore_path, keystore_slug, wative_core);
|
|
339
|
-
if (login_result.isLoginIn) {
|
|
340
|
-
await accountManager(keystore_path, keystore_slug, wative_core, login_result.password);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
await selectAccount(keystore_path, wative_core);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const setDisableAddress = async (keystore_path: string, keystore_slug: string, wative_core: typeof WativeCore) => {
|
|
347
|
-
const account_info = await getAccountInfo(keystore_path, keystore_slug);
|
|
348
|
-
|
|
349
|
-
let disable_address_options = [
|
|
350
|
-
"> Disabled Slots",
|
|
351
|
-
"> Add Single",
|
|
352
|
-
"> Add Range",
|
|
353
|
-
new inquirer.Separator('----------------------------------'),
|
|
354
|
-
'> Back'
|
|
355
|
-
];
|
|
356
|
-
|
|
357
|
-
const selected_disable_address = await selectSomething(disable_address_options);
|
|
358
|
-
switch (selected_disable_address) {
|
|
359
|
-
case '> Back': {
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
362
|
-
case "> Disabled Slots": {
|
|
363
|
-
let disabled_slots = account_info.disabled_slots;
|
|
364
|
-
if (!disabled_slots || disabled_slots.length === 0) {
|
|
365
|
-
console.log(
|
|
366
|
-
chalk.red("No disabled slots here yet")
|
|
367
|
-
)
|
|
368
|
-
break;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
let account_data = account_info.data;
|
|
372
|
-
|
|
373
|
-
let disabled_slots_options = [
|
|
374
|
-
'> Back',
|
|
375
|
-
new inquirer.Separator('----------------------------------'),
|
|
376
|
-
];
|
|
377
|
-
|
|
378
|
-
let default_network = account_info.default_network;
|
|
379
|
-
for (let i = 0; i < disabled_slots.length; i++) {
|
|
380
|
-
let from = disabled_slots[i].from;
|
|
381
|
-
let to = disabled_slots[i].to;
|
|
382
|
-
|
|
383
|
-
let from_default_network = default_network;
|
|
384
|
-
let to_default_network = default_network;
|
|
385
|
-
|
|
386
|
-
if ("default_network" in account_data[from]) {
|
|
387
|
-
from_default_network = account_data[from].default_network;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
if ("default_network" in account_data[to]) {
|
|
391
|
-
to_default_network = account_data[to].default_network;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
let from_network_type = getNetworkTypeByName(keystore_path, from_default_network);
|
|
395
|
-
let to_network_type = getNetworkTypeByName(keystore_path, to_default_network);
|
|
396
|
-
|
|
397
|
-
if (from_network_type !== "evm" && from_network_type !== "solana") {
|
|
398
|
-
throw new Error(`Invalid network type: ${from_network_type}`);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
if (to_network_type !== "evm" && to_network_type !== "solana") {
|
|
402
|
-
throw new Error(`Invalid network type: ${to_network_type}`);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
let from_address: string;
|
|
406
|
-
let to_address: string;
|
|
407
|
-
|
|
408
|
-
if (from_network_type === "evm") {
|
|
409
|
-
from_address = account_data[from].ciphertexts.evm.address;
|
|
410
|
-
} else {
|
|
411
|
-
from_address = account_data[to].ciphertexts.solana.address;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
if (to_network_type === "evm") {
|
|
415
|
-
to_address = account_data[to].ciphertexts.evm.address;
|
|
416
|
-
} else {
|
|
417
|
-
to_address = account_data[to].ciphertexts.solana.address;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
if (from === to) {
|
|
421
|
-
disabled_slots_options.push(`${i + 1}. ${from_address}[${from}]`);
|
|
422
|
-
} else {
|
|
423
|
-
disabled_slots_options.push(`${i + 1}. ${from_address}[${from}]-${to_address}[${to}]`);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
let selected_disabled_slots = await selectSomething(disabled_slots_options);
|
|
428
|
-
|
|
429
|
-
if (selected_disabled_slots === '> Back') {
|
|
430
|
-
break;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
let confirm = await confirmSomething(`Are you sure to delete the ${selected_disabled_slots}?`, true);
|
|
434
|
-
if (!confirm) {
|
|
435
|
-
break;
|
|
436
|
-
}
|
|
437
|
-
let selected_disabled_slots_index = disabled_slots_options.indexOf(selected_disabled_slots) - 2;
|
|
438
|
-
|
|
439
|
-
account_info.disabled_slots.splice(selected_disabled_slots_index, 1);
|
|
440
|
-
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
441
|
-
|
|
442
|
-
break;
|
|
443
|
-
}
|
|
444
|
-
case "> Add Single": {
|
|
445
|
-
let account_index_or_address = await getAccountIndex(account_info, "Input address or index");
|
|
446
|
-
if (account_index_or_address === null) {
|
|
447
|
-
console.log(chalk.red("Invalid account index"));
|
|
448
|
-
break;
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
if (account_info.disabled_slots) {
|
|
452
|
-
account_info.disabled_slots.push({
|
|
453
|
-
from: account_index_or_address,
|
|
454
|
-
to: account_index_or_address
|
|
455
|
-
});
|
|
456
|
-
} else {
|
|
457
|
-
account_info.disabled_slots = [{
|
|
458
|
-
from: account_index_or_address,
|
|
459
|
-
to: account_index_or_address
|
|
460
|
-
}];
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
464
|
-
break;
|
|
465
|
-
}
|
|
466
|
-
case "> Add Range": {
|
|
467
|
-
let account_range = await getAccountRange(account_info);
|
|
468
|
-
if (!account_range) {
|
|
469
|
-
console.log(chalk.red("Invalid account range"));
|
|
470
|
-
break;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
if (account_info.disabled_slots) {
|
|
474
|
-
account_info.disabled_slots.push(account_range);
|
|
475
|
-
} else {
|
|
476
|
-
account_info.disabled_slots = [account_range];
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
480
|
-
break;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
await setDisableAddress(keystore_path, keystore_slug, wative_core);
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
const accountManager = async (keystore_path: string, keystore_slug: string, wative_core: typeof WativeCore, password: string) => {
|
|
489
|
-
let account_info = await getAccountInfo(keystore_path, keystore_slug);
|
|
490
|
-
|
|
491
|
-
let account_manager_options: any[];
|
|
492
|
-
|
|
493
|
-
if (account_info.account_type === "PP") {
|
|
494
|
-
account_manager_options = [
|
|
495
|
-
'> Address List',
|
|
496
|
-
'> Expand Address',
|
|
497
|
-
'> Slice Address',
|
|
498
|
-
'> Disable Address',
|
|
499
|
-
'> Reset Account Password',
|
|
500
|
-
`> Switch Networks [${account_info.default_network}]`,
|
|
501
|
-
'> Remove',
|
|
502
|
-
new inquirer.Separator('----------------------------------'),
|
|
503
|
-
'> Back'
|
|
504
|
-
];
|
|
505
|
-
} else {
|
|
506
|
-
account_manager_options = [
|
|
507
|
-
'> Address List',
|
|
508
|
-
'> Import Private Key',
|
|
509
|
-
'> Remove Address',
|
|
510
|
-
'> Reset Account Password',
|
|
511
|
-
`> Switch Networks [${account_info.default_network}]`,
|
|
512
|
-
'> Remove',
|
|
513
|
-
new inquirer.Separator('----------------------------------'),
|
|
514
|
-
'> Back'
|
|
515
|
-
];
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
const selected_account_manager = await selectSomething(account_manager_options);
|
|
519
|
-
switch (selected_account_manager) {
|
|
520
|
-
case '> Address List': {
|
|
521
|
-
await showAccounts(keystore_path, keystore_slug, wative_core);
|
|
522
|
-
break;
|
|
523
|
-
}
|
|
524
|
-
case '> Expand Address': {
|
|
525
|
-
const count = await inputSomething('Expand accounts count', expandAmountValidator);
|
|
526
|
-
if (Number(count) > 2000) {
|
|
527
|
-
console.log(
|
|
528
|
-
chalk.red("Too many accounts, please input less than 2000")
|
|
529
|
-
);
|
|
530
|
-
break;
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
await expandAddress(keystore_path, keystore_slug, Number(count), wative_core);
|
|
534
|
-
break;
|
|
535
|
-
}
|
|
536
|
-
case '> Import Private Key': {
|
|
537
|
-
await importPrivateKeyAccount(keystore_path, password, keystore_slug, wative_core);
|
|
538
|
-
break;
|
|
539
|
-
}
|
|
540
|
-
case '> Slice Address': {
|
|
541
|
-
const last_account_no = await inputSomething('Please input last account number', lastAccountNoValidator);
|
|
542
|
-
await sliceAddress(keystore_path, keystore_slug, Number(last_account_no), wative_core);
|
|
543
|
-
break;
|
|
544
|
-
}
|
|
545
|
-
case '> Remove Address': {
|
|
546
|
-
await removeAddress(keystore_path, keystore_slug, wative_core);
|
|
547
|
-
break;
|
|
548
|
-
}
|
|
549
|
-
case '> Disable Address': {
|
|
550
|
-
await setDisableAddress(keystore_path, keystore_slug, wative_core);
|
|
551
|
-
break;
|
|
552
|
-
}
|
|
553
|
-
case '> Reset Account Password': {
|
|
554
|
-
let account_name = getAccountNameByKeystoreSlug(keystore_path, keystore_slug);
|
|
555
|
-
const password1 = await inputPassword(`Please input new password for Account [${account_name}]`);
|
|
556
|
-
const password2 = await inputPassword(`Please confirm new password for Account [${account_name}]`);
|
|
557
|
-
|
|
558
|
-
if (!password1 || !password2 || password1 !== password2) {
|
|
559
|
-
console.log(
|
|
560
|
-
chalk.red("Passwords do not match")
|
|
561
|
-
);
|
|
562
|
-
break;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
await wative_core.account.resetPassword(keystore_slug, password1);
|
|
566
|
-
return;
|
|
567
|
-
}
|
|
568
|
-
case `> Switch Networks [${account_info.default_network}]`: {
|
|
569
|
-
await switchNetworkByAccountName(keystore_path, keystore_slug, true);
|
|
570
|
-
break;
|
|
571
|
-
}
|
|
572
|
-
case '> Remove': {
|
|
573
|
-
await removeAccountName(keystore_path, keystore_slug);
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
case '> Back': {
|
|
577
|
-
return;
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
await accountManager(keystore_path, keystore_slug, wative_core, password);
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
const showAccounts = async (keystore_path: string, keystore_slug: string, wative_core: typeof WativeCore) => {
|
|
585
|
-
const account_info = await getAccountInfo(keystore_path, keystore_slug);
|
|
586
|
-
|
|
587
|
-
let option_accounts: any = [
|
|
588
|
-
'> Back',
|
|
589
|
-
new inquirer.Separator('----------------------------------'),
|
|
590
|
-
];
|
|
591
|
-
let option_account_addrs: any = [null, null];
|
|
592
|
-
let disabled_slots = account_info.disabled_slots;
|
|
593
|
-
let status = getStatus(keystore_path);
|
|
594
|
-
|
|
595
|
-
let default_network = account_info.default_network;
|
|
596
|
-
let network_info = getNetworkInfoByName(keystore_path, default_network);
|
|
597
|
-
let network_type = getChainType(network_info.chainId.toString());
|
|
598
|
-
|
|
599
|
-
let pad_index = account_info.data.length.toString().length + 1;
|
|
600
|
-
for (let i = 0; i < account_info.data.length; i++) {
|
|
601
|
-
let tag = account_info.data[i].tag ? `(${account_info.data[i].tag})` : "";
|
|
602
|
-
let is_disable_address = wative_core.account.checkIsDisableAddress(disabled_slots, i);
|
|
603
|
-
|
|
604
|
-
let account_address: string;
|
|
605
|
-
if (account_info.account_type === "PP") {
|
|
606
|
-
if (network_type === "evm") {
|
|
607
|
-
account_address = account_info.data[i].ciphertexts.evm.address;
|
|
608
|
-
} else {
|
|
609
|
-
account_address = account_info.data[i].ciphertexts.solana.address;
|
|
610
|
-
}
|
|
611
|
-
} else {
|
|
612
|
-
account_address = account_info.data[i].ciphertexts.address;
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
option_account_addrs.push(account_address);
|
|
616
|
-
if (is_disable_address) {
|
|
617
|
-
option_accounts.push(
|
|
618
|
-
new inquirer.Separator(chalk.yellow(`#${i.toString().padEnd(pad_index)} ${formatAddr(account_address, status.fullAddr)} ${tag}`))
|
|
619
|
-
)
|
|
620
|
-
} else {
|
|
621
|
-
option_accounts.push(`#${i.toString().padEnd(pad_index)} ${formatAddr(account_address, status.fullAddr)} ${tag}`);
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
let selected_default_account = DEFAULT_ACCOUNT.keystore_slug === keystore_slug ? option_accounts[DEFAULT_ACCOUNT.account_index] : option_accounts[0];
|
|
626
|
-
let direct_display = DEFAULT_ACCOUNT.keystore_slug === keystore_slug && DEFAULT_ACCOUNT.account_index > 0;
|
|
627
|
-
let asset_info_list = await getAssetList(keystore_path, network_info.chainId.toString());
|
|
628
|
-
|
|
629
|
-
let to_select_account: any;
|
|
630
|
-
if (direct_display && DEFAULT_ACCOUNT.account_index > 15) {
|
|
631
|
-
let option_accounts_slice1 = option_accounts.slice(0, DEFAULT_ACCOUNT.account_index - 15);
|
|
632
|
-
let option_accounts_slice2 = option_accounts.slice(DEFAULT_ACCOUNT.account_index - 15);
|
|
633
|
-
to_select_account = option_accounts_slice2.concat(option_accounts_slice1);
|
|
634
|
-
} else {
|
|
635
|
-
to_select_account = option_accounts.slice(0);
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
let selected_account = await listWithLazyBalanceLoading(
|
|
639
|
-
to_select_account,
|
|
640
|
-
network_type === "evm",
|
|
641
|
-
keystore_path,
|
|
642
|
-
asset_info_list,
|
|
643
|
-
network_info,
|
|
644
|
-
status.gasToken,
|
|
645
|
-
status.extendedToken,
|
|
646
|
-
direct_display,
|
|
647
|
-
"Select an account",
|
|
648
|
-
selected_default_account
|
|
649
|
-
);
|
|
650
|
-
|
|
651
|
-
if (selected_account === '> Back') {
|
|
652
|
-
return;
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
DEFAULT_ACCOUNT.keystore_slug = keystore_slug;
|
|
656
|
-
DEFAULT_ACCOUNT.account_index = option_accounts.indexOf(selected_account);
|
|
657
|
-
|
|
658
|
-
await showAccountDetail(keystore_path, keystore_slug, option_accounts.indexOf(selected_account) - 2, wative_core);
|
|
659
|
-
await showAccounts(keystore_path, keystore_slug, wative_core);
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
const showAccountDetail = async (keystore_path: string, keystore_slug: string, selected_account_id: number, wative_core: typeof WativeCore) => {
|
|
663
|
-
const account_info = await getAccountInfo(keystore_path, keystore_slug);
|
|
664
|
-
|
|
665
|
-
let default_network = account_info.default_network;
|
|
666
|
-
let account_address = getAccountAddress(keystore_path, keystore_slug, selected_account_id);
|
|
667
|
-
|
|
668
|
-
const network = await getNetworkInfoByName(keystore_path, default_network);
|
|
669
|
-
const balance_result = await getAccountBalance(
|
|
670
|
-
account_address,
|
|
671
|
-
keystore_path,
|
|
672
|
-
default_network
|
|
673
|
-
);
|
|
674
|
-
if (balance_result.status) {
|
|
675
|
-
console.log(
|
|
676
|
-
chalk.green(` Address No: #${selected_account_id}\n Network: ${default_network}\n ${network.nativeCurrency.symbol} Balance: ${balance_result.output}`)
|
|
677
|
-
);
|
|
678
|
-
} else {
|
|
679
|
-
console.log(
|
|
680
|
-
chalk.green(` Address No: #${selected_account_id}\n Network: ${default_network}\n ${network.nativeCurrency.symbol}`)
|
|
681
|
-
);
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
let tag = '> Tag';
|
|
685
|
-
if (
|
|
686
|
-
"tag" in account_info.data[selected_account_id] &&
|
|
687
|
-
account_info.data[selected_account_id].tag !== ""
|
|
688
|
-
) {
|
|
689
|
-
tag = `> Tag(${account_info.data[selected_account_id].tag})`;
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
let options_account_detail = [
|
|
693
|
-
'> Tools',
|
|
694
|
-
'> Show address',
|
|
695
|
-
'> Assets',
|
|
696
|
-
tag,
|
|
697
|
-
'> Reload balances',
|
|
698
|
-
'> Dump private key',
|
|
699
|
-
new inquirer.Separator('----------------------------------'),
|
|
700
|
-
'> Back'
|
|
701
|
-
];
|
|
702
|
-
|
|
703
|
-
let selected_account_detail = await selectSomething(options_account_detail);
|
|
704
|
-
|
|
705
|
-
switch (selected_account_detail) {
|
|
706
|
-
case '> Tools': {
|
|
707
|
-
await selectToolsOptions(keystore_path, keystore_slug, account_address, wative_core);
|
|
708
|
-
break;
|
|
709
|
-
}
|
|
710
|
-
case '> Show address': {
|
|
711
|
-
await showAllAccounts(keystore_path, keystore_slug, selected_account_id);
|
|
712
|
-
break;
|
|
713
|
-
}
|
|
714
|
-
case '> Assets': {
|
|
715
|
-
await showAssetsDetail(keystore_path, default_network, account_address);
|
|
716
|
-
// await showAssets(keystore_path, account_address, default_network);
|
|
717
|
-
break;
|
|
718
|
-
}
|
|
719
|
-
case '> Reload balances': {
|
|
720
|
-
await updateTokenBalanceOption(keystore_path, default_network, account_address);
|
|
721
|
-
break;
|
|
722
|
-
}
|
|
723
|
-
case tag: {
|
|
724
|
-
let current_tag = 'Current tag';
|
|
725
|
-
if ("tag" in account_info.data[selected_account_id] && account_info.data[selected_account_id].tag !== "") {
|
|
726
|
-
current_tag = `Current tag (${account_info.data[selected_account_id].tag})`;
|
|
727
|
-
}
|
|
728
|
-
let tag = await inputSomething(current_tag, tagValidator);
|
|
729
|
-
account_info.data[selected_account_id].tag = tag.replace(/\s+/g, " ").trim();
|
|
730
|
-
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
731
|
-
break;
|
|
732
|
-
}
|
|
733
|
-
case '> Dump private key': {
|
|
734
|
-
let account_address = getAccountAddress(keystore_path, keystore_slug, selected_account_id);
|
|
735
|
-
let private_key = await wative_core.account.showPrivateKey(account_address);
|
|
736
|
-
console.log("account: ", account_address);
|
|
737
|
-
console.log("private key: ", private_key);
|
|
738
|
-
break;
|
|
739
|
-
}
|
|
740
|
-
case '> Back': {
|
|
741
|
-
return;
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
const expandAddress = async (keystore_path: string, keystore_slug: string, expand_amount: number, wative_core: typeof WativeCore) => {
|
|
747
|
-
const account_info = await getAccountInfo(keystore_path, keystore_slug);
|
|
748
|
-
const account_len = account_info.data.length;
|
|
749
|
-
const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.legacy);
|
|
750
|
-
bar1.start(expand_amount, 0);
|
|
751
|
-
for (let i = account_len; i < account_len + expand_amount; i++) {
|
|
752
|
-
let sub_account = await wative_core.account.generateSubAccount(keystore_slug, i);
|
|
753
|
-
if (!sub_account.status) {
|
|
754
|
-
console.log(
|
|
755
|
-
chalk.red(sub_account.output)
|
|
756
|
-
);
|
|
757
|
-
return;
|
|
758
|
-
}
|
|
759
|
-
account_info.data.push({
|
|
760
|
-
ciphertexts: sub_account.output
|
|
761
|
-
});
|
|
762
|
-
bar1.increment();
|
|
763
|
-
}
|
|
764
|
-
bar1.stop();
|
|
765
|
-
|
|
766
|
-
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
767
|
-
wative_core.account.reloadAccount();
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
const sliceAddress = async (keystore_path: string, keystore_slug: string, last_account_no: number, wative_core: typeof WativeCore) => {
|
|
771
|
-
const account_info = await getAccountInfo(keystore_path, keystore_slug);
|
|
772
|
-
account_info.data = account_info.data.slice(0, last_account_no + 1);
|
|
773
|
-
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
774
|
-
wative_core.account.reloadAccount();
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
const removeAddress = async (keystore_path: string, keystore_slug: string, wative_core: typeof WativeCore) => {
|
|
778
|
-
let account_index = await inputSomething('Please input the accountIndex to remove this account', numberValidator);
|
|
779
|
-
if (account_index === "0") {
|
|
780
|
-
console.log(chalk.red("Can't remove the first account"));
|
|
781
|
-
return;
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
let account_info = await getAccountInfo(keystore_path, keystore_slug);
|
|
785
|
-
let isConfirm = await confirmSomething(`Are you sure you want to remove this account(${account_info.data[account_index].ciphertexts.address})`, true);
|
|
786
|
-
if (!isConfirm) {
|
|
787
|
-
return;
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
account_info.data.splice(Number(account_index), 1);
|
|
791
|
-
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
792
|
-
wative_core.account.reloadAccount();
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
const removeAccountName = async (keystore_path: string, keystore_slug: string) => {
|
|
796
|
-
let _account_name = await inputSomething('Please input the account name to remove this account');
|
|
797
|
-
let account_name = getAccountNameByKeystoreSlug(keystore_path, keystore_slug);
|
|
798
|
-
if (_account_name !== account_name) {
|
|
799
|
-
console.log(chalk.red("Can't remove this account"));
|
|
800
|
-
return;
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
const isConfirm = await confirmSomething(`Are you sure you want to remove this account(${account_name})`, true);
|
|
804
|
-
if (!isConfirm) {
|
|
805
|
-
return;
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
let keystore_slug_path = path.join(keystore_path, `accounts/${keystore_slug}.json`);
|
|
809
|
-
fs.rmSync(keystore_slug_path);
|
|
810
|
-
|
|
811
|
-
const network_path = path.resolve(keystore_path, 'network.json');
|
|
812
|
-
if (!fs.existsSync(network_path)) {
|
|
813
|
-
return;
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
817
|
-
let account_name_index = networks.accounts.indexOf(account_name);
|
|
818
|
-
networks.accounts.splice(account_name_index, 1);
|
|
819
|
-
fs.writeFileSync(network_path, JSON.stringify(networks, null, 2));
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
export const accountSetting = async (keystore_path: string, wative_core: typeof WativeCore) => {
|
|
823
|
-
const account_path = path.resolve(keystore_path, 'accounts');
|
|
824
|
-
if (!fs.existsSync(account_path)) {
|
|
825
|
-
fs.mkdirSync(account_path);
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
const networks = getNetworkInfo(keystore_path);
|
|
829
|
-
const account_name_list = networks.accounts;
|
|
830
|
-
|
|
831
|
-
let account_setting_options: any[];
|
|
832
|
-
if (account_name_list.length === 0) {
|
|
833
|
-
account_setting_options = [
|
|
834
|
-
'> Create a new account',
|
|
835
|
-
new inquirer.Separator('----------------------------------'),
|
|
836
|
-
'> Back'
|
|
837
|
-
];
|
|
838
|
-
} else {
|
|
839
|
-
account_setting_options = [
|
|
840
|
-
'> Select an account',
|
|
841
|
-
'> Create a new account',
|
|
842
|
-
new inquirer.Separator('----------------------------------'),
|
|
843
|
-
'> Back'
|
|
844
|
-
];
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
let selected_account_setting = await selectSomething(account_setting_options);
|
|
848
|
-
|
|
849
|
-
switch (selected_account_setting) {
|
|
850
|
-
case '> Select an account': {
|
|
851
|
-
await selectAccount(keystore_path, wative_core);
|
|
852
|
-
break;
|
|
853
|
-
}
|
|
854
|
-
case '> Create a new account': {
|
|
855
|
-
await createAccount(keystore_path, wative_core);
|
|
856
|
-
break;
|
|
857
|
-
}
|
|
858
|
-
case '> Back': {
|
|
859
|
-
return;
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
await accountSetting(keystore_path, wative_core);
|
|
864
|
-
}
|