Skip to content

Commit

Permalink
feat(ACCOUNT): Add command for generating and printing bulk of Key Pa…
Browse files Browse the repository at this point in the history
…irs (#95)

* feat(ACCOUNT): Add command for generating and printing bulk of Key Pairs

* feat(ACCOUNT): Add command for generating and printing bulk of Key Pairs
  • Loading branch information
nduchak committed Aug 21, 2019
1 parent 454385d commit 1cb3e5b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
11 changes: 11 additions & 0 deletions bin/aecli-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ program
.description('Get account nonce')
.action(async (walletPath, ...arguments) => await Account.getAccountNonce(walletPath, utils.cli.getCmdFromArguments(arguments)))

// ## Initialize `generateKeyPairs` command
//
// You can use this command to generate KeyPair's.
//
// Example: `aecli account generate 10 --force
program
.command('generate <count>')
.option('--forcePrompt', 'Force prompting')
.description('Generate keyPairs')
.action(async (count, ...arguments) => await Account.generateKeyPairs(count, utils.cli.getCmdFromArguments(arguments)))


// Handle unknown command's
program.on('command:*', () => utils.errors.unknownCommandHandler(program)())
Expand Down
37 changes: 34 additions & 3 deletions bin/commands/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { generateKeyPair } from '@aeternity/aepp-sdk/es/utils/crypto'
import { generateSecureWallet, generateSecureWalletFromPrivKey } from '../utils/account'
import { HASH_TYPES } from '../utils/constant'
import { initClientByWalletFile } from '../utils/cli'
Expand All @@ -30,7 +31,7 @@ import { PROMPT_TYPE, prompt } from '../utils/prompt'
// ## `Sign` function
// this function allow you to `sign` transaction's
async function sign (walletPath, tx, options) {
let { json } = options
const { json } = options
try {
// Validate `tx` hash
if (tx.slice(0, 2) !== 'tx') { throw new Error('Invalid transaction hash') }
Expand Down Expand Up @@ -153,7 +154,7 @@ async function getAddress (walletPath, options) {
if (privateKey) {
if (forcePrompt || await prompt(PROMPT_TYPE.confirm, { message: 'Are you sure you want print your secret key?' })) {
printUnderscored('Secret Key', keypair.secretKey)
print({ publicKey: await client.address(), secretKey: keypair.secretKey})
print({ publicKey: await client.address(), secretKey: keypair.secretKey })
}
} else {
print({ publicKey: await client.address() })
Expand Down Expand Up @@ -244,6 +245,35 @@ async function createSecureWalletByPrivKey (walletPath, priv, { output, password
}
}

// ## 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
async function generateKeyPairs (count = 1, { forcePrompt, json }) {
try {
if (!Number.isInteger(+count)) {
throw new Error('Count must be an Number')
}
if (forcePrompt || await prompt(PROMPT_TYPE.confirm, { message: 'Are you sure you want print your secret key?' })) {
const accounts = Array.from(Array(parseInt(count))).map(_ => generateKeyPair(false))
if (json) {
print(JSON.stringify(accounts, null, 2))
} else {
accounts.forEach((acc, i) => {
printUnderscored('Account index', i)
printUnderscored('Public Key', acc.publicKey)
printUnderscored('Secret Key', acc.secretKey)
print('')
})
}
} else {
process.exit(0)
}
process.exit(0)
} catch (e) {
printError(e.message)
process.exit(1)
}
}

export const Account = {
spend,
getBalance,
Expand All @@ -252,5 +282,6 @@ export const Account = {
createSecureWallet,
createSecureWalletByPrivKey,
sign,
transferFunds
transferFunds,
generateKeyPairs
}
1 change: 0 additions & 1 deletion bin/utils/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import path from 'path'
import * as Crypto from '@aeternity/aepp-sdk/es/utils/crypto'
import { dump, getAddressFromPriv, recover } from '@aeternity/aepp-sdk/es/utils/keystore'

import { printUnderscored } from './print'
import { isFileExist, readJSONFile, writeFile } from './helpers'
import { PROMPT_TYPE, prompt } from './prompt'

Expand Down
2 changes: 1 addition & 1 deletion bin/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function print (msg, obj) {

// Print error helper
export function printError (msg, obj) {
console.log(msg, obj)
console.log(msg, obj || '')
}

// Print `underscored`
Expand Down
4 changes: 4 additions & 0 deletions test/cli/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,8 @@ describe('CLI Account Module', function () {
const nonce = await wallet.getAccountNonce(await wallet.address())
parseBlock(await execute(['account', 'nonce', WALLET_NAME, '--password', 'test'], { withOutReject: true }))['next_nonce'].should.equal(`${nonce}`)
})
it('Generate accounts', async () => {
const accounts = JSON.parse(await execute(['account', 'generate', 2, '--forcePrompt', '--json'], { withOutReject: true }))
accounts.length.should.be.equal(2)
})
})

0 comments on commit 1cb3e5b

Please sign in to comment.