Skip to content

Commit

Permalink
fix: remove broken crypto genkey command
Browse files Browse the repository at this point in the history
Use `account create` instead of `crypto genkey`
  • Loading branch information
davidyuk committed Jul 6, 2022
1 parent a7d86b0 commit 32e5b0f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
12 changes: 7 additions & 5 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This script initialize all `account` function
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -20,7 +20,7 @@

import { Crypto, AmountFormatter } from '@aeternity/aepp-sdk';

import { generateSecureWallet, generateSecureWalletFromPrivKey } from '../utils/account';
import { writeWallet } from '../utils/account';
import { initSdkByWalletFile } from '../utils/cli';
import { print, printTransaction, printUnderscored } from '../utils/print';
import { readFile } from '../utils/helpers';
Expand Down Expand Up @@ -204,7 +204,8 @@ export async function getAccountNonce(walletPath, options) {
export async function createSecureWallet(walletPath, {
output, password, overwrite, json,
}) {
const { publicKey, path } = await generateSecureWallet(walletPath, { output, password, overwrite });
const { secretKey } = Crypto.generateKeyPair(true);
const { publicKey, path } = await writeWallet(walletPath, secretKey, output, password, overwrite);
if (json) {
print({
publicKey,
Expand All @@ -218,10 +219,11 @@ 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, priv, {
export async function createSecureWalletByPrivKey(walletPath, secretKey, {
output, password, overwrite, json,
}) {
const { publicKey, path } = await generateSecureWalletFromPrivKey(walletPath, priv, { output, password, overwrite });
secretKey = Buffer.from(secretKey.trim(), 'hex');
const { publicKey, path } = await writeWallet(walletPath, secretKey, output, password, overwrite);
if (json) {
print({
publicKey,
Expand Down
13 changes: 0 additions & 13 deletions src/commands/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import path from 'path';
import { Crypto } from '@aeternity/aepp-sdk';
import { prompt, PROMPT_TYPE } from '../utils/prompt';
import { getCmdFromArguments } from '../utils/cli';
import { generateSecureWallet } from '../utils/account';

// ## Key Extraction (from node nodes)
async function extractReadableKeys(dir, options) {
Expand All @@ -40,11 +39,6 @@ async function extractReadableKeys(dir, options) {
console.log(`Public key (hex): ${decryptedPub.toString('hex')}`);
}

// ## Key Pair Generation
async function generateKeyPair(name, { output, password } = {}) {
await generateSecureWallet(name, { output, password });
}

// ## Transaction Deserialization
//
// This helper function deserialized the transaction `tx` and prints the result.
Expand Down Expand Up @@ -113,13 +107,6 @@ program
.option('-i, --input [directory]', 'Directory where to look for keys', '.')
.action((dir, ...args) => extractReadableKeys(dir, getCmdFromArguments(args)));

program
.command('genkey <keyname>')
.description('Generate keypair')
.option('-o, --output [directory]', 'Output directory for the keys', '.')
.option('-p, --password [directory]', 'Password for keypair', '.')
.action((keyname, ...args) => generateKeyPair(keyname, getCmdFromArguments(args)));

program
.command('sign <tx> [privkey]')
.option('-p, --password [password]', 'password of the private key')
Expand Down
16 changes: 2 additions & 14 deletions src/utils/account.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 `account`
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -22,7 +22,7 @@ import { Crypto, Keystore, TxBuilderHelper } from '@aeternity/aepp-sdk';
import { readJSONFile } from './helpers';
import { PROMPT_TYPE, prompt } from './prompt';

async function writeWallet(name, secretKey, output, password, overwrite) {
export async function writeWallet(name, secretKey, output, password, overwrite) {
const walletPath = path.resolve(process.cwd(), path.join(output, name));
if (!overwrite && fs.existsSync(walletPath) && !await prompt(PROMPT_TYPE.askOverwrite)) {
throw new Error(`Wallet already exist at ${walletPath}`);
Expand All @@ -33,18 +33,6 @@ async function writeWallet(name, secretKey, output, password, overwrite) {
return { publicKey: TxBuilderHelper.encode(publicKey, 'ak'), path: walletPath };
}

// Generate `keypair` encrypt it using password and write to `ethereum` keystore file
export async function generateSecureWallet(name, { output = '', password, overwrite }) {
const { secretKey } = Crypto.generateKeyPair(true);
return writeWallet(name, secretKey, output, password, overwrite);
}

// Generate `keypair` from `PRIVATE KEY` encrypt it using password and to `ethereum` keystore file
export async function generateSecureWalletFromPrivKey(name, secretKey, { output = '', password, overwrite }) {
secretKey = Buffer.from(secretKey.trim(), 'hex');
return writeWallet(name, secretKey, output, password, overwrite);
}

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

0 comments on commit 32e5b0f

Please sign in to comment.