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
* @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 */

@ -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 () {
}

@ -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 () {
}

@ -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)

Loading…
Cancel
Save