diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index 15f068af1d..368e700cf1 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -375,31 +375,15 @@ class Blockchain { * Create a VM Account * @param {{privateKey: string, balance: string}} newAccount The new account to create */ - createVMAccount (newAccount) { - const { privateKey, balance } = newAccount + createVMAccount(newAccount) { if (this.executionContext.getProvider() !== 'vm') { throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed') } - this.providers.vm._addAccount(privateKey, balance) - const privKey = Buffer.from(privateKey, 'hex') - return '0x' + privateToAddress(privKey).toString('hex') + return this.providers.vm.createVMAccount(newAccount) } newAccount (_password, passwordPromptCb, cb) { - if (this.executionContext.isVM()) { - let privateKey - do { - privateKey = crypto.randomBytes(32) - } while (!isValidPrivate(privateKey)) - this.providers.vm._addAccount(privateKey, '0x56BC75E2D63100000') - return cb(null, '0x' + privateToAddress(privateKey).toString('hex')) - } - if (!this.config.get('settings/personal-mode')) { - return cb('Not running in personal mode') - } - passwordPromptCb((passphrase) => { - this.executionContext.web3().personal.newAccount(passphrase, cb) - }) + return this.getCurrentProvider().newAccount(passwordPromptCb, cb) } /** Get the balance of an address, and convert wei to ether */ diff --git a/src/blockchain/providers/injected.js b/src/blockchain/providers/injected.js index 0663f69903..bd779e07ef 100644 --- a/src/blockchain/providers/injected.js +++ b/src/blockchain/providers/injected.js @@ -10,6 +10,12 @@ class InjectedProvider { return this.executionContext.web3().eth.getAccounts(cb) } + newAccount(passwordPromptCb, cb) { + passwordPromptCb((passphrase) => { + this.executionContext.web3().personal.newAccount(passphrase, cb) + }) + } + resetEnvironment () { } diff --git a/src/blockchain/providers/node.js b/src/blockchain/providers/node.js index a31f4e7882..729f3cf8c2 100644 --- a/src/blockchain/providers/node.js +++ b/src/blockchain/providers/node.js @@ -14,6 +14,15 @@ class NodeProvider { return this.executionContext.web3().eth.getAccounts(cb) } + newAccount(passwordPromptCb, cb) { + if (!this.config.get('settings/personal-mode')) { + return cb('Not running in personal mode') + } + passwordPromptCb((passphrase) => { + this.executionContext.web3().personal.newAccount(passphrase, cb) + }) + } + resetEnvironment () { } diff --git a/src/blockchain/providers/vm.js b/src/blockchain/providers/vm.js index 8697584f0e..f5a5f1fd3c 100644 --- a/src/blockchain/providers/vm.js +++ b/src/blockchain/providers/vm.js @@ -43,6 +43,22 @@ class VMProvider { this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } } + createVMAccount (passwordPromptCb, cb) { + const { privateKey, balance } = newAccount + this._addAccount(privateKey, balance) + const privKey = Buffer.from(privateKey, 'hex') + return '0x' + privateToAddress(privKey).toString('hex') + } + + newAccount(_passwordPromptCb, cb) { + let privateKey + do { + privateKey = crypto.randomBytes(32) + } while (!isValidPrivate(privateKey)) + this.providers.vm._addAccount(privateKey, '0x56BC75E2D63100000') + return cb(null, '0x' + privateToAddress(privateKey).toString('hex')) + } + getBalanceInEther (address, cb) { address = stripHexPrefix(address)