Skip to content

Commit c9e5154

Browse files
authored
Merge pull request #76 from arda-org/fix-no-password
Make new-wallet work if no password and --from param
2 parents c3946e9 + f8b8f97 commit c9e5154

File tree

4 files changed

+67
-81
lines changed

4 files changed

+67
-81
lines changed

xsuite/src/cli/cmd.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ test("new-wallet --wallet wallet.json --password 1234", async () => {
9898
]);
9999
});
100100

101+
test("new-wallet --wallet wallet.json --from-pem wallet.pem", async () => {
102+
const walletPath = path.resolve("wallet.json");
103+
stdoutInt.start();
104+
input.inject("1234", "1234");
105+
await run(`new-wallet --wallet ${walletPath} --from-pem ${pemPath}`);
106+
const keystore = Keystore.fromFile_unsafe(walletPath, "1234");
107+
const secretKey = UserSecretKey.fromPem(
108+
fs.readFileSync(pemPath, "utf8"),
109+
).hex();
110+
stdoutInt.stop();
111+
expect(keystore.getSecretKey()).toEqual(secretKey);
112+
expect(stdoutInt.data.split("\n")).toEqual([
113+
`Creating keystore wallet at "${walletPath}"...`,
114+
"Enter password: ",
115+
"Re-enter password: ",
116+
"",
117+
chalk.green(`Wallet created at "${walletPath}".`),
118+
"",
119+
chalk.bold.blue("Address:") + ` ${keystore.newSigner()}`,
120+
"",
121+
]);
122+
});
123+
101124
test("new-wallet --wallet wallet.json --password 1234 --from-pem wallet.pem", async () => {
102125
const walletPath = path.resolve("wallet.json");
103126
stdoutInt.start();
@@ -178,7 +201,8 @@ test("new-wallet --wallet wallet.json --password 1234 --from-wallet keystore_mne
178201

179202
test("request-xegld --wallet wallet.json", async () => {
180203
const walletPath = path.resolve("wallet.json");
181-
const signer = Keystore.createFile_unsafe(walletPath, "1234").newSigner();
204+
fs.copyFileSync(mneKeystorePath, walletPath);
205+
const signer = Keystore.fromFile_unsafe(walletPath, "1234").newSigner();
182206
const address = signer.toString();
183207
let balances: number[] = [];
184208
const server = setupServer(

xsuite/src/cli/newWalletCmd.ts

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import fs from "node:fs";
22
import path from "node:path";
3-
import { UserSecretKey, UserWallet } from "@multiversx/sdk-wallet";
3+
import { UserSecretKey, UserWallet, Mnemonic } from "@multiversx/sdk-wallet";
44
import chalk from "chalk";
55
import { Command } from "commander";
6-
import { log } from "../_stdio";
6+
import { input, log } from "../_stdio";
77
import { Keystore } from "../world/signer";
88
import { logError, logSuccess } from "./helpers";
99

@@ -34,45 +34,49 @@ const action = async ({
3434
logError(`Wallet already exists at "${walletPath}".`);
3535
return;
3636
}
37-
let keystore: Keystore;
3837
if (password === undefined) {
39-
try {
40-
keystore = await Keystore.createFile(walletPath);
41-
log();
42-
} catch (err: any) {
43-
logError(err.message);
38+
log(`Creating keystore wallet at "${walletPath}"...`);
39+
password = await input.hidden("Enter password: ");
40+
const passwordAgain = await input.hidden("Re-enter password: ");
41+
if (password !== passwordAgain) {
42+
logError("Passwords do not match.");
4443
return;
4544
}
46-
} else {
47-
let data;
48-
if (fromPemPath && fromWalletPath) {
49-
throw new Error(
50-
"Both --from-pem and --from-wallet options cannot be set simultaneously.",
51-
);
52-
}
53-
if (fromPemPath) {
54-
const secretKey = UserSecretKey.fromPem(
55-
fs.readFileSync(fromPemPath, "utf8"),
56-
);
57-
const wallet = UserWallet.fromSecretKey({ secretKey, password });
58-
data = wallet.toJSON();
59-
}
60-
if (fromWalletPath) {
61-
const oldKeystore = await Keystore.fromFile(fromWalletPath);
62-
const newWallet =
63-
oldKeystore.kind === "mnemonic"
64-
? UserWallet.fromMnemonic({
65-
mnemonic: oldKeystore.getMnemonicWords().join(" "),
66-
password,
67-
})
68-
: UserWallet.fromSecretKey({
69-
secretKey: UserSecretKey.fromString(oldKeystore.getSecretKey()),
70-
password,
71-
});
72-
data = newWallet.toJSON();
73-
}
74-
keystore = Keystore.createFile_unsafe(walletPath, password, data);
45+
log();
46+
}
47+
let data;
48+
if (fromPemPath && fromWalletPath) {
49+
throw new Error(
50+
"Both --from-pem and --from-wallet options cannot be set simultaneously.",
51+
);
52+
}
53+
if (fromPemPath) {
54+
const secretKey = UserSecretKey.fromPem(
55+
fs.readFileSync(fromPemPath, "utf8"),
56+
);
57+
const wallet = UserWallet.fromSecretKey({ secretKey, password });
58+
data = wallet.toJSON();
59+
}
60+
if (fromWalletPath) {
61+
const oldKeystore = await Keystore.fromFile(fromWalletPath);
62+
const newWallet =
63+
oldKeystore.kind === "mnemonic"
64+
? UserWallet.fromMnemonic({
65+
mnemonic: oldKeystore.getMnemonicWords().join(" "),
66+
password,
67+
})
68+
: UserWallet.fromSecretKey({
69+
secretKey: UserSecretKey.fromString(oldKeystore.getSecretKey()),
70+
password,
71+
});
72+
data = newWallet.toJSON();
73+
}
74+
if (data === undefined) {
75+
const mnemonic = Mnemonic.generate().toString();
76+
data = UserWallet.fromMnemonic({ mnemonic, password }).toJSON();
7577
}
78+
fs.writeFileSync(walletPath, JSON.stringify(data), "utf8");
79+
const keystore = new Keystore(data, password);
7680
logSuccess(`Wallet created at "${walletPath}".`);
7781
log();
7882
log(chalk.bold.blue("Address:") + ` ${keystore.newSigner()}`);

xsuite/src/world/signer.test.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs";
22
import path from "node:path";
33
import { afterEach, beforeEach, expect, test } from "@jest/globals";
44
import { input, stdoutInt } from "../_stdio";
5-
import { Keystore, KeystoreSigner } from "./signer";
5+
import { KeystoreSigner } from "./signer";
66

77
const tmpDir = "/tmp/xsuite-tests";
88
const walletPath = path.resolve(tmpDir, "wallet.json");
@@ -72,24 +72,3 @@ test("KeystoreSigner.fromFile - ENOENT", async () => {
7272
"",
7373
]);
7474
});
75-
76-
test("KeystoreSigner.createFile", async () => {
77-
stdoutInt.start();
78-
input.inject("1234", "1234");
79-
const keystore = await Keystore.createFile(walletPath);
80-
const signer = KeystoreSigner.fromFile_unsafe(walletPath, "1234");
81-
stdoutInt.stop();
82-
expect(stdoutInt.data.split("\n")).toEqual([
83-
`Creating keystore wallet at "${walletPath}"...`,
84-
"Enter password: ",
85-
"Re-enter password: ",
86-
"",
87-
]);
88-
expect(keystore.newSigner().toString()).toEqual(signer.toString());
89-
});
90-
91-
test("Keystore.createFile_unsafe", async () => {
92-
const keystore = Keystore.createFile_unsafe(walletPath, "1234");
93-
const signer = KeystoreSigner.fromFile_unsafe(walletPath, "1234");
94-
expect(keystore.newSigner().toString()).toEqual(signer.toString());
95-
});

xsuite/src/world/signer.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from "node:fs";
22
import path from "node:path";
33
import {
44
UserSigner as BaseUserSigner,
5-
Mnemonic,
65
UserWallet,
76
} from "@multiversx/sdk-wallet";
87
import { input, log } from "../_stdio";
@@ -64,26 +63,6 @@ export class Keystore {
6463
this.kind = getSupportedKeystoreKind(data);
6564
}
6665

67-
static async createFile(filePath: string, data?: any) {
68-
filePath = path.resolve(filePath);
69-
log(`Creating keystore wallet at "${filePath}"...`);
70-
const password = await input.hidden("Enter password: ");
71-
const passwordAgain = await input.hidden("Re-enter password: ");
72-
if (password !== passwordAgain) {
73-
throw new Error("Passwords do not match.");
74-
}
75-
return this.createFile_unsafe(filePath, password, data);
76-
}
77-
78-
static createFile_unsafe(filePath: string, password: string, data?: any) {
79-
if (data === undefined) {
80-
const mnemonic = Mnemonic.generate().toString();
81-
data = UserWallet.fromMnemonic({ mnemonic, password }).toJSON();
82-
}
83-
fs.writeFileSync(filePath, JSON.stringify(data), "utf8");
84-
return new Keystore(data, password);
85-
}
86-
8766
static async fromFile(filePath: string) {
8867
filePath = path.resolve(filePath);
8968
log(`Loading keystore wallet at "${filePath}"...`);

0 commit comments

Comments
 (0)