Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

52 private key builder #53

Merged
merged 4 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/factories/PrivateKeyServiceBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const logger = require('../logger')
const { PrivateKeyService } = require('../services/PrivateKeyService')
const PrivateKeyValidator = require('../validators/PrivateKeyValidator')

const privateKeyValidator = new PrivateKeyValidator(logger)

/**
* Class representing a simple factory to build PrivateKeyService object.
*/

class PrivateKeyServiceBuilder {
static build() {
return new PrivateKeyService(privateKeyValidator, logger)
}
}

module.exports = PrivateKeyServiceBuilder
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const PrivateKeyServiceBuilder = require('../../../factories/PrivateKeyServiceBuilder')
const KeyStoreGenerateCommand = require('../../../commands/ethereum-wallet/KeyStoreGenerateCommand')
// eslint-disable-next-line
const KeyStoreGenerateCommandValidator = require('../../../validators/commands/ethereum-wallet/KeyStoreGenerateCommandValidator')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')

const privateKeyValidator = new PrivateKeyValidator(logger)
const privateKeyService = new PrivateKeyService()

const privateKeyService = PrivateKeyServiceBuilder.build()
/**
* Class representing a simple factory to build KeyStoreGenerateCommandBuilder object.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/factories/commands/order/OrderCancelCommandBuilder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')
const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')

const OrderCancelCommand = require('../../../commands/order/OrderCancelCommand')
const OrderCancelCommandValidator = require('../../../validators/commands/order/OrderCancelCommandValidator')
const OrderService = require('../../../services/OrderService')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')

const orderService = new OrderService(logger)
const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)

/**
Expand Down
7 changes: 4 additions & 3 deletions src/factories/commands/order/OrderCreateCommandBuilder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')
const OrderCreateCommand = require('../../../commands/order/OrderCreateCommand')
const OrderCreateCommandValidator = require('../../../validators/commands/order/OrderCreateCommandValidator')
const OrderService = require('../../../services/OrderService')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')

const orderService = new OrderService(logger)
const privateKeyService = new PrivateKeyService(logger)

const privateKeyService = PrivateKeyServiceBuilder.build()

const privateKeyValidator = new PrivateKeyValidator(logger)

/**
Expand Down
4 changes: 2 additions & 2 deletions src/factories/commands/order/OrderSignCommandBuilder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')
const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')

const OrderSignCommand = require('../../../commands/order/OrderSignCommand')
const OrderSignCommandValidator = require('../../../validators/commands/order/OrderSignCommandValidator')
const OrderSignerHelper = require('../../../helpers/OrderSignerHelper')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')

const ordersignerHelper = new OrderSignerHelper(logger)
const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)

/**
Expand Down
5 changes: 2 additions & 3 deletions src/factories/commands/token/ApproveCommandBuilder.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const ApproveCommand = require('../../../commands/token/ApproveCommand')
const ApproveCommandValidator = require('../../../validators/commands/token/ApproveCommandValidator')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')

const PrivateKeyServiceBuilder = require('../../../factories/PrivateKeyServiceBuilder')
const logger = require('../../../logger')

const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)

/**
Expand Down
5 changes: 2 additions & 3 deletions src/factories/commands/token/GetAllowanceCommandBuilder.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')
const GetAllowanceCommand = require('../../../commands/token/GetAllowanceCommand')
const GetAllowanceCommandValidator = require('../../../validators/commands/token/GetAllowanceCommandValidator')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')

const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const CreateWalletCommand = require('../../../commands/trading-wallet/CreateWalletCommand')
const CreateWalletCommandValidator = require('../../../validators/commands/trading-wallet/CreateWalletCommandValidator')
const GetAllowanceCommandValidator = require('../../../validators/commands/token/GetAllowanceCommandValidator')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')
const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')

const privateKeyService = PrivateKeyServiceBuilder.build()

const privateKeyService = new PrivateKeyService(logger)
const privateKeyValidator = new PrivateKeyValidator(logger)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const DepositEthCommand = require('../../../commands/trading-wallet/DepositEthCommand')
const DepositEthCommandValidator = require('../../../validators/commands/trading-wallet/DepositEthCommandValidator')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')
const TradingWalletServiceBuilder = require('../../TradingWalletServiceBuilder')
const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')

const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)
const tradingWalletService = TradingWalletServiceBuilder.build()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')
const DepositTokenCommand = require('../../../commands/trading-wallet/DepositTokenCommand')
const DepositTokenCommandValidator = require('../../../validators/commands/trading-wallet/DepositTokenCommandValidator')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')
const TradingWalletServiceBuilder = require('../../TradingWalletServiceBuilder')

const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)
const tradingWalletService = TradingWalletServiceBuilder.build()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')
const GetAddressCommand = require('../../../commands/trading-wallet/GetAddressCommand')
const GetAddressCommandValidator = require('../../../validators/commands/trading-wallet/GetAddressCommandValidator')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')
const TradingWalletServiceBuilder = require('../../TradingWalletServiceBuilder')

const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)
const tradingWalletService = TradingWalletServiceBuilder.build()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')
const GetBalanceCommand = require('../../../commands/trading-wallet/GetBalanceCommand')
const GetBalanceCommandValidator = require('../../../validators/commands/trading-wallet/GetBalanceCommandValidator')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')
const TradingWalletServiceBuilder = require('../../TradingWalletServiceBuilder')

const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)
const tradingWalletService = TradingWalletServiceBuilder.build()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const logger = require('../../../logger')
const { PrivateKeyService } = require('../../../services/PrivateKeyService')

const PrivateKeyServiceBuilder = require('../../../../src/factories/PrivateKeyServiceBuilder')
const WithdrawCommand = require('../../../commands/trading-wallet/WithdrawCommand')
const WithdrawCommandValidator = require('../../../validators/commands/trading-wallet/WithdrawCommandValidator')
const PrivateKeyValidator = require('../../../validators/PrivateKeyValidator')
const TradingWalletServiceBuilder = require('../../TradingWalletServiceBuilder')

const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()
const privateKeyValidator = new PrivateKeyValidator(logger)
const tradingWalletService = TradingWalletServiceBuilder.build()

Expand Down
6 changes: 1 addition & 5 deletions src/services/PrivateKeyService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ const writeFileAsync = promisify(fs.writeFile)
const readFileAsync = promisify(fs.readFile)

const { BaseError } = require('../utils/errors')
const log = require('../logger')
const PrivateKeyValidator = require('../validators/PrivateKeyValidator')

const privateKeyValidatorInstance = new PrivateKeyValidator(log)

class EthereumAddressError extends BaseError {}
class InvalidPrivateKeyError extends BaseError {}
Expand All @@ -26,7 +22,7 @@ const encoding = 'utf8'
* Class representing a service that manage private key.
*/
class PrivateKeyService {
constructor(privateKeyValidator = privateKeyValidatorInstance, logger = log) {
constructor(privateKeyValidator, logger) {
if (!privateKeyValidator) {
throw new TypeError(`Invalid "privateKeyValidator" value: ${privateKeyValidator}`)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/commands/createWalletCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Web3 = require('web3')
const sandbox = require('sinon').createSandbox()
const { EidooEthApiLib } = require('@eidoo/ethapi-lib')

const { PrivateKeyService } = require('../../../src/services/PrivateKeyService')
const PrivateKeyServiceBuilder = require('../../../src/factories/PrivateKeyServiceBuilder')
const { TransactionLib } = require('../../../src/lib/TransactionLib')
const CreateWalletCommand = require('../../../src/commands/trading-wallet/CreateWalletCommand')
const CreateWalletCommandValidator = require('../../../src/validators/commands/trading-wallet/CreateWalletCommandValidator')
Expand Down Expand Up @@ -32,7 +32,7 @@ const tradingWalletTransactionBuilder = new TradingWalletTransactionBuilder(
const tradingWalletService = new TradingWalletService(web3, transactionLib, tradingWalletTransactionBuilder, logger)

const privateKeyValidator = new PrivateKeyValidator(logger)
const privateKeyService = new PrivateKeyService(logger)
const privateKeyService = PrivateKeyServiceBuilder.build()

const createWalletCommand = new CreateWalletCommand(logger, tradingWalletService,
createWalletCommandValidator, privateKeyService, privateKeyValidator)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/commands/getAddressCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const sandbox = require('sinon').createSandbox()

const { exchange } = require('../../../src/config')
const { PrivateKeyService } = require('../../../src/services/PrivateKeyService')

const logger = require('../../../src/logger')
const TradingWalletServiceBuilder = require('../../../src/factories/TradingWalletServiceBuilder')
Expand All @@ -15,8 +14,9 @@ const getAddressCommandValidator = new GetAddressCommandValidator(logger)
const tradingWalletService = TradingWalletServiceBuilder.build()

const privateKeyValidator = new PrivateKeyValidator(logger)
const privateKeyService = new PrivateKeyService(logger)
const PrivateKeyServiceBuilder = require('../../../src/factories/PrivateKeyServiceBuilder')

const privateKeyService = PrivateKeyServiceBuilder.build()
const getAddressCommand = new GetAddressCommand(logger, tradingWalletService,
getAddressCommandValidator, privateKeyService, privateKeyValidator)

Expand Down
5 changes: 2 additions & 3 deletions tests/unit/commands/getTradingWalletBalanceCommand.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-env node, jest */
const sandbox = require('sinon').createSandbox()

const { PrivateKeyService } = require('../../../src/services/PrivateKeyService')

const PrivateKeyServiceBuilder = require('../../../src/factories/PrivateKeyServiceBuilder')
const logger = require('../../../src/logger')
const TradingWalletServiceBuilder = require('../../../src/factories/TradingWalletServiceBuilder')
const PrivateKeyValidator = require('../../../src/validators/PrivateKeyValidator')
Expand All @@ -13,9 +12,9 @@ const GetBalanceCommand = require('../../../src/commands/trading-wallet/GetBalan
const getBalanceCommandValidator = new GetBalanceCommandValidator(logger)

const tradingWalletService = TradingWalletServiceBuilder.build()
const privateKeyService = PrivateKeyServiceBuilder.build()

const privateKeyValidator = new PrivateKeyValidator(logger)
const privateKeyService = new PrivateKeyService(logger)

const getBalanceCommand = new GetBalanceCommand(logger, tradingWalletService,
getBalanceCommandValidator, privateKeyService, privateKeyValidator)
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/commands/signCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const MockCancelOrderDataToSign = require('../../factories/MockCancelOrderDataToSign')
const MockCreateOrderDataToSign = require('../../factories/MockCreateOrderDataToSign')
const logger = require('../../../src/logger')
const { PrivateKeyService } = require('../../../src/services/PrivateKeyService')
const PrivateKeyServiceBuilder = require('../../../src/factories/PrivateKeyServiceBuilder')
const OrderSignerHelper = require('../../../src/helpers/OrderSignerHelper')
const PrivateKeyValidator = require('../../../src/validators/PrivateKeyValidator')
const OrderSignCommandValidator = require('../../../src/validators/commands/order/OrderSignCommandValidator')
Expand All @@ -11,7 +11,8 @@ const OrderSignCommand = require('../../../src/commands/order/OrderSignCommand')
const orderSignCommandValidator = new OrderSignCommandValidator(logger)
const orderSignerHelper = new OrderSignerHelper(logger)
const privateKeyValidator = new PrivateKeyValidator(logger)
const privateKeyService = new PrivateKeyService(logger)

const privateKeyService = PrivateKeyServiceBuilder.build()

const signCommand = new OrderSignCommand(logger, orderSignerHelper,
orderSignCommandValidator, privateKeyService, privateKeyValidator)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/services/PrivateKeyService.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* global describe, expect, test */
const keythereum = require('keythereum')
const ethereumUtil = require('ethereumjs-util')
const PrivateKeyServiceBuilder = require('../../../src/factories/PrivateKeyServiceBuilder')

const { InvalidPrivateKeyFile, InvalidMnemonicError,
PrivateKeyService } = require('../../../src/services/PrivateKeyService')
const { InvalidPrivateKeyFile, InvalidMnemonicError } = require('../../../src/services/PrivateKeyService')

let privateKeyService

beforeEach(() => {
privateKeyService = new PrivateKeyService()
privateKeyService = PrivateKeyServiceBuilder.build()
})

describe('getPrivateKey', () => {
Expand Down