Skip to content

Commit

Permalink
refactor(account)!: remove useless output option
Browse files Browse the repository at this point in the history
BREAKING CHANGES: `output` options removed in `account create,save`
Put output value in wallet path instead.
```diff
- aecli account create my-wallet.json --output ./mykeys
+ aecli account create ./mykeys/my-wallet.json
```
  • Loading branch information
davidyuk committed Apr 24, 2023
1 parent 0290bc3 commit 74a8a37
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
16 changes: 8 additions & 8 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,9 @@ export async function getAccountNonce(walletPath, options) {

// ## Create secure `wallet` file
// This function allow you to generate `keypair` and write it to secure `ethereum` like key-file
export async function createSecureWallet(walletPath, {
output, password, overwrite, json,
}) {
export async function createSecureWallet(walletPath, { password, overwrite, json }) {
const { secretKey } = generateKeyPair(true);
const { publicKey, path } = await writeWallet(walletPath, secretKey, output, password, overwrite);
const { publicKey, path } = await writeWallet(walletPath, secretKey, password, overwrite);
if (json) {
print({
publicKey,
Expand All @@ -168,11 +166,13 @@ export async function createSecureWallet(walletPath, {

// ## Create secure `wallet` file from `private-key`
// This function allow you to generate `keypair` from `private-key` and write it to secure `ethereum` like key-file
export async function createSecureWalletByPrivKey(walletPath, secretKey, {
output, password, overwrite, json,
}) {
export async function createSecureWalletByPrivKey(
walletPath,
secretKey,
{ password, overwrite, json },
) {
secretKey = Buffer.from(secretKey.trim(), 'hex');
const { publicKey, path } = await writeWallet(walletPath, secretKey, output, password, overwrite);
const { publicKey, path } = await writeWallet(walletPath, secretKey, password, overwrite);
if (json) {
print({
publicKey,
Expand Down
16 changes: 8 additions & 8 deletions src/actions/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
// This script initialize all `contract` function

import fs from 'fs-extra';
import path from 'path';
import { encode } from '@aeternity/aepp-sdk';
import { initSdk, initSdkByWalletFile } from '../utils/cli';
import { print, printTransaction, printUnderscored } from '../utils/print';
import CliError from '../utils/CliError';
import { getFullPath } from '../utils/helpers';

const DESCRIPTOR_VERSION = 1;
const resolve = (filename) => path.resolve(process.cwd(), filename);

async function getContractParams({
descrPath, contractAddress, contractSource, contractBytecode, contractAci,
}, { dummyBytecode, descrMayNotExist } = {}) {
let descriptor = {};
if (descrPath && (!descrMayNotExist || await fs.exists(resolve(descrPath)))) {
descriptor = await fs.readJson(resolve(descrPath));
if (descrPath && (!descrMayNotExist || await fs.exists(getFullPath(descrPath)))) {
descriptor = await fs.readJson(getFullPath(descrPath));
if (descriptor.version !== DESCRIPTOR_VERSION) {
throw new CliError(`Unsupported contract descriptor: version ${descriptor.version}, supported ${DESCRIPTOR_VERSION}`);
}
Expand All @@ -29,8 +28,10 @@ async function getContractParams({
...dummyBytecode && { bytecode: 'cb_invalid-bytecode' },
...other,
...contractSource && { sourceCodePath: contractSource },
...contractBytecode && { bytecode: encode(await fs.readFile(resolve(contractBytecode)), 'cb') },
...contractAci && { aci: await fs.readJson(resolve(contractAci)) },
...contractBytecode && {
bytecode: encode(await fs.readFile(getFullPath(contractBytecode)), 'cb'),
},
...contractAci && { aci: await fs.readJson(getFullPath(contractAci)) },
};
}

Expand Down Expand Up @@ -75,8 +76,7 @@ export async function deploy(walletPath, args, options) {
const contract = await sdk.initializeContract(await getContractParams(options, { descrMayNotExist: true }));
const result = await contract.$deploy(args, options);
const filename = options.contractSource ?? options.contractBytecode;
options.descrPath ||= path
.resolve(process.cwd(), `${filename}.deploy.${result.address.slice(3)}.json`);
options.descrPath ??= getFullPath(`${filename}.deploy.${result.address.slice(3)}.json`);
const descriptor = {
version: DESCRIPTOR_VERSION,
address: result.address,
Expand Down
16 changes: 2 additions & 14 deletions src/commands/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,9 @@ addCommonOptions(program
// You can use this command to generate `keypair` and encrypt it by password.
// This command create `ethereum like keyfile`.
//
// You can use `--output ./keys` to set directory to save you key.
//
// Example: `aecli account create myWalletName --password testpassword`
//
// Example: `aecli account create myWalletName --password testpassword --output ./mykeys` --> create `key-file` in `mykeys` directory
// Example: `aecli account create ./mykeys/my-wallet.json --password testpassword`
addCommonOptions(program
.command('create <wallet_path>')
.option('-O, --output [output]', 'Output directory', '.')
.option('--overwrite', 'Overwrite if exist')
.description('Create a secure wallet')
.action(Account.createSecureWallet));
Expand All @@ -134,14 +129,9 @@ addCommonOptions(program
// You can use this command to generate `keypair` from `private-key` and encrypt it by password.
// This command create `ethereum like keyfile`.
//
// You can use `--output ./keys` to set directory to save you key
//
// Example: `aecli account save myWalletName 1902855723940510273412074210842018342148234 --password testpassword`
//
// Example: `aecli account save myWalletName 1902855723940510273412074210842018342148234 --password testpassword --output ./mykeys` --> create `key-file` in `mykeys` directory
// Example: `aecli account save ./mykeys/my-wallet.json 1902855723940510273412074210842018342148234 --password testpassword`
addCommonOptions(program
.command('save <wallet_path> <privkey>')
.option('-O, --output [output]', 'Output directory', '.')
.option('--overwrite', 'Overwrite if exist')
.description('Save a private keys string to a password protected file wallet')
.action(Account.createSecureWalletByPrivKey));
Expand All @@ -150,8 +140,6 @@ addCommonOptions(program
//
// You can use this command to get `account nonce`.
//
// You can use `--output ./keys` to set directory to save you key
//
// Example: `aecli account nonce myWalletName --password testpassword
addCommonOptions(program
.command('nonce <wallet_path>')
Expand Down
16 changes: 8 additions & 8 deletions src/utils/account.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// # Utils `account` Module
// That script contains helper function's for work with `account`
import fs from 'fs-extra';
import path from 'path';
import {
generateKeyPairFromSecret, getAddressFromPriv, dump, recover, encode, Encoding,
} from '@aeternity/aepp-sdk';
import { PROMPT_TYPE, prompt } from './prompt';
import { getFullPath } from './helpers';
import CliError from './CliError';

export async function writeWallet(name, secretKey, output, password, overwrite) {
const walletPath = path.resolve(process.cwd(), path.join(output, name));
if (!overwrite && await fs.exists(walletPath) && !await prompt(PROMPT_TYPE.askOverwrite)) {
throw new CliError(`Wallet already exist at ${walletPath}`);
export async function writeWallet(walletPath, secretKey, password, overwrite) {
const path = getFullPath(walletPath);
if (!overwrite && await fs.exists(path) && !await prompt(PROMPT_TYPE.askOverwrite)) {
throw new CliError(`Wallet already exist at ${path}`);
}
password ??= await prompt(PROMPT_TYPE.askPassword);
await fs.outputJson(walletPath, await dump(name, password, secretKey));
await fs.outputJson(path, await dump(path, password, secretKey));
const { publicKey } = generateKeyPairFromSecret(secretKey);
return { publicKey: encode(publicKey, Encoding.AccountAddress), path: walletPath };
return { publicKey: encode(publicKey, Encoding.AccountAddress), path };
}

// Get account file by path, decrypt it using password and return `keypair`
export async function getWalletByPathAndDecrypt(walletPath, password) {
const keyFile = await fs.readJson(path.resolve(process.cwd(), walletPath));
const keyFile = await fs.readJson(getFullPath(walletPath));

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

Expand Down
3 changes: 3 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// # Utils `helpers` Module
// That script contains base helper function

import { resolve } from 'path';
import { Encoding, decode as _decode } from '@aeternity/aepp-sdk';
import CliError from './CliError';

Expand Down Expand Up @@ -77,3 +78,5 @@ export function decode(data, requiredPrefix) {
}
return _decode(data);
}

export const getFullPath = (path) => resolve(process.cwd(), path);

0 comments on commit 74a8a37

Please sign in to comment.