-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.ts
129 lines (112 loc) · 3.91 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env npx ts-node
import "@total-typescript/ts-reset";
import "./dotenv";
import "./fetchPolyfill";
import { program } from "commander";
import ora from "ora";
import { Account, pickAccounts } from "./ui/pickAccounts";
import { getAccountInfos } from "./getAccountsInfo";
import { getAccountsAndNetwork } from "./ui/getAccounts";
import { display } from "./displayAccounts";
import { unionWith } from "lodash";
import { showTransferAll } from "./actions/transferAll/ui";
import { askForExtraAccounts, extraAccount } from "./ui/extraAccount";
import { equalSigner, getDefaultSigners } from "./genSigners";
import { detectAccountIssues, fixAccountIssues } from "./issues";
import { ec } from "starknet-410";
import { getRpcNodeUrlsForNetworkId } from "./getProvider";
/** Helps with debugging payloads containing BigInt */
(BigInt.prototype as any).toJSON = function () {
return `${this.toString()}n`;
};
program
.name("Argent X CLI")
.description("CLI to recover Argent X accounts")
.version("0.8.0");
program.parse();
/**
* 1. Choose network
* 2. Ask if user wants to recover by seed or by private key
* 3. If seed, ask for seed. If private key, ask for private key.
* 4. Let user pick account addresses to recover.
* 5. Scan for balances after asking which assets to scan for.
* 6. Scan for accounts with problems (available upgrade)
* 7. Allow user to send all funds to a single new address.
* 8. Show all transactions that get executed to the user.
* 9. Wait for confirmation.
*/
(async () => {
const spinner = ora();
const mainnetRpcNodeUrls = getRpcNodeUrlsForNetworkId("mainnet-alpha");
const testnetRpcNodeUrls = getRpcNodeUrlsForNetworkId("goerli-alpha");
spinner.info(`Mainnet RPC: ${mainnetRpcNodeUrls.join(", ")}`);
spinner.info(`Testnet RPC: ${testnetRpcNodeUrls.join(", ")}`);
let { accounts, networkId, privateKey, seed } = await getAccountsAndNetwork(
spinner
);
spinner.succeed("Found " + accounts.length + " wallets");
if (accounts.length === 0) {
accounts = await extraAccount(networkId);
} else if (await askForExtraAccounts()) {
accounts = unionWith(
accounts,
await extraAccount(networkId),
(a, b) => a.address === b.address
);
}
const accountInfos = await getAccountInfos(
accounts.map((x) => x.address),
networkId,
spinner
);
const accountWithSigner: Account[] = accounts.map((account, i) => ({
...account,
...accountInfos[i],
}));
// find missing signers
if (accountWithSigner.some((x) => !x.privateKey)) {
spinner.start("Trying to find missing private keys");
if (seed) {
const defaultSigners = getDefaultSigners(seed);
accountWithSigner
.filter((x) => !x.privateKey)
.forEach((x) => {
const signer = defaultSigners.find(
(y) => x.signer && equalSigner(x.signer, y.signer)
);
if (signer) {
x.privateKey = signer.privateKey;
}
});
}
if (privateKey) {
const defaultSigner = ec.getStarkKey(ec.getKeyPair(privateKey));
spinner.info(`Public key: ${defaultSigner}`);
accountWithSigner
.filter((x) => !x.privateKey)
.forEach((x) => {
if (x.signer && equalSigner(x.signer, defaultSigner)) {
x.privateKey = privateKey;
}
});
}
if (accountWithSigner.some((x) => !x.privateKey)) {
spinner.warn(
"Could not find all private keys. Continuing with missing private keys"
);
} else {
spinner.succeed("Found all signers");
}
}
const filteredAccountWithSigner = await pickAccounts(
accountWithSigner,
networkId
);
display(filteredAccountWithSigner);
const issues = await detectAccountIssues(filteredAccountWithSigner);
await fixAccountIssues(accountWithSigner, networkId, issues);
await showTransferAll(filteredAccountWithSigner);
})().catch((e) => {
console.error("An error occured", e);
process.exit(1);
});