Skip to content

Commit

Permalink
fix(account): don't ask for password if it is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 20, 2023
1 parent db5ae9d commit daefd21
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 3 deletions.
151 changes: 151 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"eslint-config-airbnb-base": "^15.0.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"sinon": "^15.0.3",
"standard-version": "^9.5.0"
},
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function writeWallet(name, secretKey, output, password, overwrite)
if (!overwrite && await fs.exists(walletPath) && !await prompt(PROMPT_TYPE.askOverwrite)) {
throw new CliError(`Wallet already exist at ${walletPath}`);
}
password ||= await prompt(PROMPT_TYPE.askPassword);
password ??= await prompt(PROMPT_TYPE.askPassword);
await fs.outputJson(walletPath, await dump(name, password, secretKey));
const { publicKey } = generateKeyPairFromSecret(secretKey);
return { publicKey: encode(publicKey, Encoding.AccountAddress), path: walletPath };
Expand All @@ -39,7 +39,7 @@ export async function writeWallet(name, secretKey, output, password, overwrite)
export async function getWalletByPathAndDecrypt(walletPath, password) {
const keyFile = await fs.readJson(path.resolve(process.cwd(), walletPath));

password ||= await prompt(PROMPT_TYPE.askPassword);
password ??= await prompt(PROMPT_TYPE.askPassword);

const privKey = await recover(password, keyFile);

Expand Down
20 changes: 19 additions & 1 deletion test/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import fs from 'fs-extra';
import { before, describe, it } from 'mocha';
import { expect } from 'chai';
import { expect, assert } from 'chai';
import { stub } from 'sinon';
import { generateKeyPair } from '@aeternity/aepp-sdk';
import { getSdk, executeProgram, WALLET_NAME } from './index';
import accountProgram from '../src/commands/account';
import * as promptModule from '../src/utils/prompt';

const executeAccount = (args) => executeProgram(accountProgram, args);
const walletName = 'test-artifacts/test-wallet.json';
Expand Down Expand Up @@ -62,6 +64,22 @@ describe('Account Module', () => {
.to.equal(keypair.secretKey);
});

it('asks for password if it not provided', async () => {
const stubbedPrompt = stub(promptModule, 'prompt');
stubbedPrompt.throwsException(new Error('stubbed'));
await expect(executeAccount(['create', 'test-artifacts/test-wallet-1.json']))
.to.be.eventually.rejectedWith('stubbed');
assert(stubbedPrompt.alwaysCalledWith(promptModule.PROMPT_TYPE.askPassword));
stubbedPrompt.restore();
});

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

it('Check Wallet Balance', async () => {
const balance = await sdk.getBalance(sdk.address);
expect((await executeAccount(['balance', WALLET_NAME, '--password', 'test', '--json'])).balance)
Expand Down

0 comments on commit daefd21

Please sign in to comment.