Skip to content

Commit

Permalink
#60 Refactor private key base command
Browse files Browse the repository at this point in the history
  • Loading branch information
ceres3idoo authored Mar 5, 2019
1 parent 8579426 commit 6e14e2b
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 105 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Release date: 2019-02-REPLEACE_ME.

### Added

- [CredentialBaseCommand](https://github.com/eidoo/hybrid-exchange-sdk/issues/60) Add `CredentialBaseCommand` to separate private key and keystore object utilities from `ABaseCommand`.
- [CLI generate keystore command](https://github.com/eidoo/hybrid-exchange-sdk/issues/50)
- [CLI create order command](https://github.com/eidoo/hybrid-exchange-sdk/issues/46)
- [Command factories](https://github.com/eidoo/hybrid-exchange-sdk/issues/40)
Expand Down
80 changes: 3 additions & 77 deletions src/commands/ABaseCommand.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
const inquirer = require('inquirer')
const { InvalidPrivateKeyError } = require('../services/PrivateKeyService')
const CommandError = require('./CommandError')

/**
* Class representing the command interface. */
class ABaseCommand {
/**
* Create a base command.
* @param {Object} logger The logger.
* @param {Object} privateKeyService The privateKeyService.
* @param {Object} privateKeyValidator The privateKeyValidator.
* @throws {TypeError} If some required property is missing.
* @param {Object} logger The logger.
* @throws {TypeError} If some required property is missing.
*/
constructor(logger, privateKeyService, privateKeyValidator) {
constructor(logger) {
if (!logger) {
throw new TypeError(`Invalid "logger" value: ${logger}`)
}
this.log = logger.child({ module: this.constructor.name })

if (!privateKeyService) {
const errorMessage = `Invalid "privateKeyService" value: ${privateKeyService}`
throw new TypeError(errorMessage)
}
this.privateKeyService = privateKeyService

if (!privateKeyValidator) {
const errorMessage = `Invalid "privateKeyValidator" value: ${privateKeyValidator}`
throw new TypeError(errorMessage)
}
this.privateKeyValidator = privateKeyValidator
}

async executeAsync(params) {
Expand Down Expand Up @@ -78,64 +62,6 @@ class ABaseCommand {
))
return formattedErrors
}

async promptMnemonic() {
const { mnemonic } = await inquirer.prompt([
{
type: 'input',
message: 'Enter mnemonic',
name: 'mnemonic',
},
])
this.log.debug({ fn: 'promptMnemonic' }, 'Input mnemonic done.')
return mnemonic
}

async promptKeyStorePassword() {
const { keyStorePassword } = await inquirer.prompt([
{
type: 'password',
message: 'Enter password to encrypt your keystore',
name: 'keyStorePassword',
mask: '*',
},
])
this.log.debug({ fn: 'promptKeyStorePassword' }, 'Input prompt KeyStore password.')
return keyStorePassword
}

/**
* It extract private key from file specified in privateKeyFilePath.
*
* @param {String} privateKeyFilePath The path of private key file.
*
* @throws {InvalidPrivateKeyFile} If does not exist the file.
*
*/
async extractPrivateKey (privateKeyFilePath) {
this.privateKeyValidator.validateFilePath({ privateKeyFilePath })
const privateKey = await this.privateKeyService.getPrivateKeyAsync(privateKeyFilePath)
this.privateKeyValidator.validatePrivateKey({ privateKey })
return privateKey
}

/**
* It gets address (EOA) from private key.s
*
* @param {String} personalWalletAddress The personal wallet address (EOA).
* @param {String} privateKey The private key.
*
* @throws {InvalidPrivateKeyError} If does not exist the file.
*/
getAddressFromPrivateKey(personalWalletAddress, privateKey) {
const personalWalletAddressFromPrivateKey = this.privateKeyService.getAddressFromPrivateKey(privateKey)
if (personalWalletAddress && personalWalletAddress !== personalWalletAddressFromPrivateKey) {
this.log.error({ fn: 'getAddressFromPrivateKey', personalWalletAddress },
'The private key does not match the personal wallet address.')
throw new InvalidPrivateKeyError(`The private key does not match the personal wallet address given in input:${personalWalletAddress}`)
}
return personalWalletAddressFromPrivateKey
}
}

module.exports = ABaseCommand
91 changes: 91 additions & 0 deletions src/commands/CredentialBasedCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const inquirer = require('inquirer')
const { InvalidPrivateKeyError } = require('../services/PrivateKeyService')
const ABaseCommand = require('./ABaseCommand')

/**
* Class representing class to be used if the command requires credentials.
*/
class CredentialBasedCommand extends ABaseCommand {
/**
* Create a base command.
* @param {Object} logger The logger.
* @param {Object} privateKeyService The privateKeyService.
* @param {Object} privateKeyValidator The privateKeyValidator.
* @throws {TypeError} If some required property is missing.
*/
constructor(logger, privateKeyService, privateKeyValidator) {
super(logger)

if (!privateKeyService) {
const errorMessage = `Invalid "privateKeyService" value: ${privateKeyService}`
throw new TypeError(errorMessage)
}
this.privateKeyService = privateKeyService

if (!privateKeyValidator) {
const errorMessage = `Invalid "privateKeyValidator" value: ${privateKeyValidator}`
throw new TypeError(errorMessage)
}
this.privateKeyValidator = privateKeyValidator
}

async promptMnemonic() {
const { mnemonic } = await inquirer.prompt([
{
type: 'input',
message: 'Enter mnemonic',
name: 'mnemonic',
},
])
this.log.debug({ fn: 'promptMnemonic' }, 'Input mnemonic done.')
return mnemonic
}

async promptKeyStorePassword() {
const { keyStorePassword } = await inquirer.prompt([
{
type: 'password',
message: 'Enter password to encrypt your keystore',
name: 'keyStorePassword',
mask: '*',
},
])
this.log.debug({ fn: 'promptKeyStorePassword' }, 'Input prompt KeyStore password.')
return keyStorePassword
}

/**
* It extract private key from file specified in privateKeyFilePath.
*
* @param {String} privateKeyFilePath The path of private key file.
*
* @throws {InvalidPrivateKeyFile} If does not exist the file.
*
*/
async extractPrivateKey (privateKeyFilePath) {
this.privateKeyValidator.validateFilePath({ privateKeyFilePath })
const privateKey = await this.privateKeyService.getPrivateKeyAsync(privateKeyFilePath)
this.privateKeyValidator.validatePrivateKey({ privateKey })
return privateKey
}

/**
* It gets address (EOA) from private key.s
*
* @param {String} personalWalletAddress The personal wallet address (EOA).
* @param {String} privateKey The private key.
*
* @throws {InvalidPrivateKeyError} If does not exist the file.
*/
getAddressFromPrivateKey(personalWalletAddress, privateKey) {
const personalWalletAddressFromPrivateKey = this.privateKeyService.getAddressFromPrivateKey(privateKey)
if (personalWalletAddress && personalWalletAddress !== personalWalletAddressFromPrivateKey) {
this.log.error({ fn: 'getAddressFromPrivateKey', personalWalletAddress },
'The private key does not match the personal wallet address.')
throw new InvalidPrivateKeyError(`The private key does not match the personal wallet address given in input:${personalWalletAddress}`)
}
return personalWalletAddressFromPrivateKey
}
}

module.exports = CredentialBasedCommand
7 changes: 4 additions & 3 deletions src/commands/ethereum-wallet/KeyStoreGenerateCommand.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')

const HD_PATH = "m/44'/60'/0/0"

/**
* Class representing KeyStoreGenerateCommand. */
class KeyStoreGenerateCommand extends ABaseCommand {
* Class representing KeyStoreGenerateCommand.
*/
class KeyStoreGenerateCommand extends CredentialBasedCommand {
/**
* Create a KeyStoreGenerateCommand.
* @param {Object} logger The logger helper.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/order/OrderCancelCommand.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')

/**
* Class representing OrderCancelCommand. */
class OrderCancelCommand extends ABaseCommand {
class OrderCancelCommand extends CredentialBasedCommand {
/**
* Create a OrderCancelCommand controller.
* @param {Object} logger The logger helper.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/order/OrderCreateCommand.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')

/**
* Class representing OrderCreateCommand. */
class OrderCreateCommand extends ABaseCommand {
class OrderCreateCommand extends CredentialBasedCommand {
/**
* Create a OrderCreateCommand controller.
* @param {Object} logger The logger helper.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/order/OrderSignCommand.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')
const validRequestType = require('../../models/Request').TYPES

/**
* Class representing orderSignCommand. */

class OrderSignCommand extends ABaseCommand {
class OrderSignCommand extends CredentialBasedCommand {
/**
* Create a signer controller.
* @param {Object} logger The logger helper.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/token/ApproveCommand.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')
const Erc20TokenServiceBuilder = require('../../factories/Erc20TokenServiceBuilder')

/**
* Class representing approve command.
*/
class ApproveCommand extends ABaseCommand {
class ApproveCommand extends CredentialBasedCommand {
/**
* Create an ApproveCommand controller.
* @param {Object} logger The logger helper.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/token/GetAllowanceCommand.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const _ = require('lodash')

const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')
const { Erc20TokenServiceBuilder } = require('../../factories')

/**
* Class representing GetAllowanceCommand. */
class GetAllowanceCommand extends ABaseCommand {
class GetAllowanceCommand extends CredentialBasedCommand {
/**
* Create a GetAllowanceCommand object.
* @param {Object} logger The logger helper.
Expand Down
6 changes: 3 additions & 3 deletions src/commands/trading-wallet/CreateWalletCommand.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')

/**
* Class representing CreateWalletCommand. */
class CreateWalletCommand extends ABaseCommand {
class CreateWalletCommand extends CredentialBasedCommand {
/**
* Create a signer controller.
* @param {Object} logger The logger helper.
* @param {Object} tradingWalletService The tradingWallet service.
* @param {Object} tradingWalletService The tradingWallet service.
* @param {Object} createWalletCommandValidator The CreateWalletCommand validator.
* @param {Object} privateKeyService The privateKeyService.
* @param {Object} privateKeyValidator The privateKeyValidator.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/trading-wallet/DepositEthCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')

/**
* Class representing DepositEthCommand.
*/
class DepositEthCommand extends ABaseCommand {
class DepositEthCommand extends CredentialBasedCommand {
/**
* Create a deposit ethereum command controller.
* @param {Object} logger The logger helper.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/trading-wallet/DepositTokenCommand.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Web3 = require('web3')

const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')
const Erc20TokenServiceBuilder = require('../../factories/Erc20TokenServiceBuilder')
const Erc20TokenTransactionBuilder = require('../../factories/Erc20TokenTransactionBuilder')
Expand All @@ -13,7 +13,7 @@ const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
/**
* Class representing DepositTokenCommand.
*/
class DepositTokenCommand extends ABaseCommand {
class DepositTokenCommand extends CredentialBasedCommand {
/**
* Create a deposit token command controller.
* @param {Object} logger The logger helper.
Expand Down
8 changes: 4 additions & 4 deletions src/commands/trading-wallet/GetAddressCommand.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const _ = require('lodash')

const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')

/**
* Class representing GetAddressCommand. */
class GetAddressCommand extends ABaseCommand {
class GetAddressCommand extends CredentialBasedCommand {
/**
* Create a signer controller.
* @param {Object} logger The logger helper.
* @param {Object} tradingWalletService The tradingWallet service.
* @param {Object} getAddressCommandValidator The getAddressCommand validator.
* @param {Object} privateKeyService The privateKeyService.
* @param {Object} privateKeyValidator The privateKeyValidator.
* @param {Object} privateKeyService The privateKeyService.
* @param {Object} privateKeyValidator The privateKeyValidator.
* @throws {TypeError} If some required property is missing.
*/
constructor(logger, tradingWalletService, getAddressCommandValidator,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/trading-wallet/GetBalanceCommand.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const _ = require('lodash')

const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')

/**
* Class representing GetBalanceCommand. */
class GetBalanceCommand extends ABaseCommand {
class GetBalanceCommand extends CredentialBasedCommand {
/**
* Create a GetBalanceCommand controller.
* @param {Object} logger The logger helper.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/trading-wallet/WithdrawCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const ABaseCommand = require('../ABaseCommand')
const CredentialBasedCommand = require('../CredentialBasedCommand')
const CommandArg = require('../../models/CommandArg')

/**
* Class representing WithdrawCommand.
*/
class WithdrawCommand extends ABaseCommand {
class WithdrawCommand extends CredentialBasedCommand {
/**
* Create a signer controller.
* @param {Object} logger The logger helper.
Expand Down

0 comments on commit 6e14e2b

Please sign in to comment.