move createvmaccount and newaccount to their own providers

pull/1/head
Iuri Matias 5 years ago
parent bd444d408b
commit 4ce1ecd42c
  1. 22
      src/blockchain/blockchain.js
  2. 6
      src/blockchain/providers/injected.js
  3. 9
      src/blockchain/providers/node.js
  4. 16
      src/blockchain/providers/vm.js

@ -375,31 +375,15 @@ class Blockchain {
* Create a VM Account * Create a VM Account
* @param {{privateKey: string, balance: string}} newAccount The new account to create * @param {{privateKey: string, balance: string}} newAccount The new account to create
*/ */
createVMAccount (newAccount) { createVMAccount(newAccount) {
const { privateKey, balance } = newAccount
if (this.executionContext.getProvider() !== 'vm') { 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') 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) return this.providers.vm.createVMAccount(newAccount)
const privKey = Buffer.from(privateKey, 'hex')
return '0x' + privateToAddress(privKey).toString('hex')
} }
newAccount (_password, passwordPromptCb, cb) { newAccount (_password, passwordPromptCb, cb) {
if (this.executionContext.isVM()) { return this.getCurrentProvider().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'))
}
if (!this.config.get('settings/personal-mode')) {
return cb('Not running in personal mode')
}
passwordPromptCb((passphrase) => {
this.executionContext.web3().personal.newAccount(passphrase, cb)
})
} }
/** Get the balance of an address, and convert wei to ether */ /** Get the balance of an address, and convert wei to ether */

@ -10,6 +10,12 @@ class InjectedProvider {
return this.executionContext.web3().eth.getAccounts(cb) return this.executionContext.web3().eth.getAccounts(cb)
} }
newAccount(passwordPromptCb, cb) {
passwordPromptCb((passphrase) => {
this.executionContext.web3().personal.newAccount(passphrase, cb)
})
}
resetEnvironment () { resetEnvironment () {
} }

@ -14,6 +14,15 @@ class NodeProvider {
return this.executionContext.web3().eth.getAccounts(cb) 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 () { resetEnvironment () {
} }

@ -43,6 +43,22 @@ class VMProvider {
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } 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) { getBalanceInEther (address, cb) {
address = stripHexPrefix(address) address = stripHexPrefix(address)

Loading…
Cancel
Save