Skip to content

Commit 4ac6576

Browse files
refactor: split askForWallet and askForImportWallet
1 parent 12f3a2f commit 4ac6576

File tree

2 files changed

+70
-48
lines changed

2 files changed

+70
-48
lines changed

cli/src/cli-helpers/askForWallet.ts

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '../utils/keystore.js';
1313
import { AbortError } from '../utils/errors.js';
1414

15-
async function walletFromPrivateKey(spinner: Spinner) {
15+
async function createWalletFromPrivateKey(spinner: Spinner) {
1616
const { answer } = await spinner.prompt({
1717
type: 'password',
1818
name: 'answer',
@@ -24,7 +24,7 @@ async function walletFromPrivateKey(spinner: Spinner) {
2424
wallet = new Wallet(answer);
2525
} catch {
2626
spinner.log(color.error('Invalid wallet private key'));
27-
return walletFromPrivateKey(spinner);
27+
return createWalletFromPrivateKey(spinner);
2828
}
2929
return wallet;
3030
}
@@ -113,52 +113,15 @@ async function walletToPrivateKeyInConfig({
113113
spinner.log(`walletPrivateKey saved to ${color.file(CONFIG_FILE)}`);
114114
}
115115

116-
export async function askForWallet({
116+
async function askSaveWallet({
117117
spinner,
118-
importWallet = false,
118+
wallet,
119+
forceSave = false,
119120
}: {
120121
spinner: Spinner;
121-
importWallet?: boolean;
122-
}): Promise<AbstractSigner> {
123-
const config = await readIAppConfig();
124-
125-
const { walletPrivateKey, walletFileName } = config;
126-
if (!importWallet) {
127-
if (walletPrivateKey) {
128-
try {
129-
const wallet = new Wallet(walletPrivateKey);
130-
spinner.log(
131-
`Using saved walletPrivateKey ${color.comment(`(from ${color.file(CONFIG_FILE)})`)}`
132-
);
133-
return wallet;
134-
} catch {
135-
spinner.warn(
136-
`Invalid walletPrivateKey ${color.comment(`(in ${color.file(CONFIG_FILE)})`)}`
137-
);
138-
}
139-
} else if (walletFileName) {
140-
try {
141-
spinner.log(
142-
`Using wallet ${color.file(walletFileName)} ${color.comment(`(from ${color.file(KEYSTORE_PATH)})`)}`
143-
);
144-
const { address, isEncrypted } = await loadWalletInfoFromKeystore({
145-
walletFileName,
146-
});
147-
if (address && isEncrypted) {
148-
const wallet = await walletFromKeystore({ spinner, walletFileName });
149-
return wallet;
150-
}
151-
} catch (e) {
152-
if (e instanceof AbortError) throw e;
153-
spinner.warn(
154-
`Invalid walletFileName ${color.comment(`(in ${color.file(CONFIG_FILE)})`)}`
155-
);
156-
}
157-
}
158-
}
159-
160-
const wallet = await walletFromPrivateKey(spinner);
161-
122+
wallet: Wallet;
123+
forceSave?: boolean;
124+
}) {
162125
const { saveWalletAnswer } = await spinner.prompt([
163126
{
164127
type: 'select',
@@ -176,7 +139,7 @@ export async function askForWallet({
176139
value: 'config',
177140
description: `plain text private key in ${CONFIG_FILE}`,
178141
},
179-
...(!importWallet
142+
...(!forceSave
180143
? [
181144
{
182145
title: 'do not save',
@@ -192,5 +155,64 @@ export async function askForWallet({
192155
} else if (saveWalletAnswer === 'keystore') {
193156
await walletToKeyStore({ spinner, wallet });
194157
}
158+
}
159+
160+
export async function askForWallet({
161+
spinner,
162+
}: {
163+
spinner: Spinner;
164+
}): Promise<AbstractSigner> {
165+
const config = await readIAppConfig();
166+
167+
// try loading wallet from config
168+
const { walletPrivateKey, walletFileName } = config;
169+
if (walletPrivateKey) {
170+
try {
171+
const wallet = new Wallet(walletPrivateKey);
172+
spinner.log(
173+
`Using saved walletPrivateKey ${color.comment(`(from ${color.file(CONFIG_FILE)})`)}`
174+
);
175+
return wallet;
176+
} catch {
177+
spinner.warn(
178+
`Invalid walletPrivateKey ${color.comment(`(in ${color.file(CONFIG_FILE)})`)}`
179+
);
180+
}
181+
} else if (walletFileName) {
182+
try {
183+
spinner.log(
184+
`Using wallet ${color.file(walletFileName)} ${color.comment(`(from ${color.file(KEYSTORE_PATH)})`)}`
185+
);
186+
const { address, isEncrypted } = await loadWalletInfoFromKeystore({
187+
walletFileName,
188+
});
189+
if (address && isEncrypted) {
190+
const wallet = await walletFromKeystore({ spinner, walletFileName });
191+
return wallet;
192+
}
193+
} catch (e) {
194+
if (e instanceof AbortError) throw e;
195+
spinner.warn(
196+
`Invalid walletFileName ${color.comment(`(in ${color.file(CONFIG_FILE)})`)}`
197+
);
198+
}
199+
}
200+
201+
// if no wallet is found in config, ask for a new one
202+
const wallet = await createWalletFromPrivateKey(spinner);
203+
await askSaveWallet({
204+
spinner,
205+
wallet,
206+
});
207+
return wallet;
208+
}
209+
210+
export async function askForImportWallet({
211+
spinner,
212+
}: {
213+
spinner: Spinner;
214+
}): Promise<AbstractSigner> {
215+
const wallet = await createWalletFromPrivateKey(spinner);
216+
await askSaveWallet({ spinner, wallet, forceSave: true }); // always save when importing
195217
return wallet;
196218
}

cli/src/cmd/wallet-import.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { askForWallet } from '../cli-helpers/askForWallet.js';
1+
import { askForImportWallet } from '../cli-helpers/askForWallet.js';
22
import { handleCliError } from '../cli-helpers/handleCliError.js';
33
import { getSpinner } from '../cli-helpers/spinner.js';
44
import * as color from '../cli-helpers/color.js';
@@ -9,7 +9,7 @@ export async function walletImport() {
99
try {
1010
spinner.text = 'Importing wallet';
1111
await warnBeforeDeletePrivateKey({ spinner });
12-
const signer = await askForWallet({ spinner, importWallet: true });
12+
const signer = await askForImportWallet({ spinner });
1313
const address = await signer.getAddress();
1414
spinner.succeed(`Imported wallet ${color.emphasis(address)}`);
1515
} catch (error) {

0 commit comments

Comments
 (0)