Skip to content

Commit

Permalink
fix(account): don't ask password if it is not required
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 11, 2024
1 parent b6537b8 commit 54d70fb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ export async function getAddress(walletPath, options) {
const printPrivateKey = privateKey && (forcePrompt
|| await prompt(PROMPT_TYPE.confirm, { message: 'Are you sure you want print your secret key?' }));

const secretKey = printPrivateKey && await account.getSecretKey();
if (json) {
print({
publicKey: account.address,
...printPrivateKey && { secretKey: account.secretKey },
...printPrivateKey && { secretKey },
});
} else {
printUnderscored('Address', account.address);
if (printPrivateKey) printUnderscored('Secret Key', account.secretKey);
if (printPrivateKey) printUnderscored('Secret Key', secretKey);
}
}

Expand Down
33 changes: 26 additions & 7 deletions src/utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// That script contains helper function's for work with `cli`
import fs from 'fs-extra';
import {
AeSdk, Node, MemoryAccount, CompilerCli, CompilerCli8, CompilerHttpNode, recover,
AeSdk, Node, MemoryAccount, CompilerCli, CompilerCli8, CompilerHttpNode, recover, sign,
} from '@aeternity/aepp-sdk';
import { PROMPT_TYPE, prompt } from './prompt.js';
import { getFullPath } from './helpers.js';
Expand All @@ -29,17 +29,36 @@ export function initSdk({
}

export class AccountCli extends MemoryAccount {
constructor(secretKey) {
super(secretKey);
// TODO: remove after resolving https://github.com/aeternity/aepp-sdk-js/issues/1672
this.secretKey = secretKey;
#keyFile;

#password;

#secretKey;

constructor(keyFile, password) {
super(Buffer.alloc(64));
this.#keyFile = keyFile;
this.#password = password;
this.address = keyFile.public_key;
}

async getSecretKey() {
this.#secretKey ??= await recover(
this.#password ?? await prompt(PROMPT_TYPE.askPassword),
this.#keyFile,
);
return this.#secretKey;
}

async sign(data) {
const secretKey = await this.getSecretKey();
return sign(data, Buffer.from(secretKey, 'hex'));
}

// Get account file by path, decrypt it using password and return AccountCli
static async read(path, password) {
const keyFile = await fs.readJson(getFullPath(path));
password ??= await prompt(PROMPT_TYPE.askPassword);
return new AccountCli(await recover(password, keyFile));
return new AccountCli(keyFile, password);
}
}

Expand Down
15 changes: 8 additions & 7 deletions test/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ describe('Account Module', () => {
it('Create Wallet', async () => {
const createRes = await executeAccount(['create', walletName, '--password', 'test']);
expect(await fs.exists(walletName)).to.be.equal(true);
const resJson = await executeAccount(['address', walletName, '--password', 'test', '--json']);
const resJson = await executeAccount(['address', walletName, '--json']);
expect(resJson.publicKey).to.be.a('string');
expect(createRes).to.be.equal(`
Address _________________________________ ${resJson.publicKey}
Path ____________________________________ ${resolve(walletName)}
`.trim());
const res = await executeAccount(['address', walletName, '--password', 'test']);
const res = await executeAccount(['address', walletName]);
expect(res).to.be.equal(`
Address _________________________________ ${resJson.publicKey}
`.trim());
Expand All @@ -41,12 +41,12 @@ Address _________________________________ ${resJson.publicKey}
it('Create Wallet From Private Key', async () => {
await executeAccount(['create', walletName, '--password', 'test', keypair.secretKey, '--overwrite']);
expect(await fs.exists(walletName)).to.be.equal(true);
expect((await executeAccount(['address', walletName, '--password', 'test', '--json'])).publicKey)
expect((await executeAccount(['address', walletName, '--json'])).publicKey)
.to.equal(keypair.publicKey);
});

it('Check Wallet Address', async () => {
expect((await executeAccount(['address', WALLET_NAME, '--password', 'test', '--json'])).publicKey)
expect((await executeAccount(['address', WALLET_NAME, '--json'])).publicKey)
.to.equal(sdk.address);
});

Expand All @@ -62,15 +62,16 @@ Secret Key ______________________________ ${keypair.secretKey}

it('asks for password if it not provided', async () => {
const walletPath = 'test-artifacts/test-wallet-1.json';
prompts.inject(['test-password', 'test-password', 'y']);
prompts.inject(['test-password', 'y', 'test-password']);
const { publicKey } = await executeAccount(['create', walletPath, '--json']);
expect(await executeAccount(['address', walletPath, '--privateKey'])).to.include(publicKey);
});

it('don\'t asks for password if it is empty', async () => {
it('don\'t asks for password if provided password is empty string', async () => {
const name = 'test-artifacts/test-wallet-2.json';
await executeAccount(['create', name, '--password', '']);
expect((await executeAccount(['address', name, '--password', '', '--json'])).publicKey)
prompts.inject(['y']);
expect((await executeAccount(['address', name, '--password', '', '--privateKey', '--json'])).publicKey)
.to.be.a('string');
});

Expand Down

0 comments on commit 54d70fb

Please sign in to comment.