Commit 4660dabd authored by Grandschtroumpf's avatar Grandschtroumpf

Udapp as a class

parent c19ea94f
var async = require('async') const async = require('async')
var ethJSUtil = require('ethereumjs-util') const ethJSUtil = require('ethereumjs-util')
var BN = ethJSUtil.BN const { BN, privateToAddress, isValidPrivate, stripHexPrefix } = ethJSUtil
var crypto = require('crypto') const crypto = require('crypto')
import { EventEmitter } from 'events';
var TxRunner = require('./execution/txRunner') const TxRunner = require('./execution/txRunner')
var txHelper = require('./execution/txHelper') const txHelper = require('./execution/txHelper')
var EventManager = require('./eventManager') const EventManager = require('./eventManager')
var executionContext = require('./execution/execution-context') const executionContext = require('./execution/execution-context')
function UniversalDApp (registry) { module.exports = class UniversalDApp {
constructor (config) {
this.events = new EventEmitter()
this.event = new EventManager() this.event = new EventManager()
var self = this this.config = config
self._deps = {
config: registry.get('config').api this.txRunner = new TxRunner({}, {
} config: config,
self._txRunnerAPI = {
config: self._deps.config,
detectNetwork: (cb) => { detectNetwork: (cb) => {
executionContext.detectNetwork(cb) executionContext.detectNetwork(cb)
}, },
personalMode: () => { personalMode: () => {
return self._deps.config.get('settings/personal-mode') return executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
} }
} })
self.txRunner = new TxRunner({}, self._txRunnerAPI) this.accounts = {}
self.accounts = {}
self.resetEnvironment()
executionContext.event.register('contextChanged', this.resetEnvironment.bind(this)) executionContext.event.register('contextChanged', this.resetEnvironment.bind(this))
} }
// TODO : event should be triggered by Udapp instead of TxListener
/** Listen on New Transaction. (Cannot be done inside constructor because txlistener doesn't exist yet) */
startListening (txlistener) {
txlistener.event.register('newTransaction', (tx) => {
this.events.emit('newTransaction', tx)
})
}
UniversalDApp.prototype.resetEnvironment = function () { resetEnvironment () {
this.accounts = {} this.accounts = {}
if (executionContext.isVM()) { if (executionContext.isVM()) {
this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000') this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000')
...@@ -47,7 +55,7 @@ UniversalDApp.prototype.resetEnvironment = function () { ...@@ -47,7 +55,7 @@ UniversalDApp.prototype.resetEnvironment = function () {
executionContext.detectNetwork(cb) executionContext.detectNetwork(cb)
}, },
personalMode: () => { personalMode: () => {
return this._deps.config.get('settings/personal-mode') return executionContext.getProvider() === 'web3' ? this._deps.config.get('settings/personal-mode') : false
} }
}) })
this.txRunner.event.register('transactionBroadcasted', (txhash) => { this.txRunner.event.register('transactionBroadcasted', (txhash) => {
...@@ -56,46 +64,53 @@ UniversalDApp.prototype.resetEnvironment = function () { ...@@ -56,46 +64,53 @@ UniversalDApp.prototype.resetEnvironment = function () {
this.event.trigger('transactionBroadcasted', [txhash, network.name]) this.event.trigger('transactionBroadcasted', [txhash, network.name])
}) })
}) })
} }
UniversalDApp.prototype.resetAPI = function (transactionContextAPI) { resetAPI (transactionContextAPI) {
this.transactionContextAPI = transactionContextAPI this.transactionContextAPI = transactionContextAPI
} }
UniversalDApp.prototype.createVMAccount = function (privateKey, balance, cb) { /**
* Create a VM Account
* @param {{privateKey: string, balance: string}} newAccount The new account to create
*/
createVMAccount (newAccount) {
const { privateKey, balance } = newAccount
if (executionContext.getProvider() !== 'vm') {
throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed')
}
this._addAccount(privateKey, balance) this._addAccount(privateKey, balance)
privateKey = Buffer.from(privateKey, 'hex') const privKey = Buffer.from(privateKey, 'hex')
cb(null, '0x' + ethJSUtil.privateToAddress(privateKey).toString('hex')) return '0x' + ethJSUtil.privateToAddress(privKey).toString('hex')
} }
UniversalDApp.prototype.newAccount = function (password, passwordPromptCb, cb) { newAccount (password, passwordPromptCb, cb) {
if (!executionContext.isVM()) { if (!executionContext.isVM()) {
if (!this._deps.config.get('settings/personal-mode')) { if (!this.config.get('settings/personal-mode')) {
return cb('Not running in personal mode') return cb('Not running in personal mode')
} }
passwordPromptCb((passphrase) => { passwordPromptCb((passphrase) => {
executionContext.web3().personal.newAccount(passphrase, cb) executionContext.web3().personal.newAccount(passphrase, cb)
}) })
} else { } else {
var privateKey let privateKey
do { do {
privateKey = crypto.randomBytes(32) privateKey = crypto.randomBytes(32)
} while (!ethJSUtil.isValidPrivate(privateKey)) } while (!ethJSUtil.isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000') this._addAccount(privateKey, '0x56BC75E2D63100000')
cb(null, '0x' + ethJSUtil.privateToAddress(privateKey).toString('hex')) cb(null, '0x' + ethJSUtil.privateToAddress(privateKey).toString('hex'))
} }
} }
UniversalDApp.prototype._addAccount = function (privateKey, balance) {
var self = this
/** Add an account to the list of account (only for Javascript VM) */
_addAccount (privateKey, balance) {
if (!executionContext.isVM()) { if (!executionContext.isVM()) {
throw new Error('_addAccount() cannot be called in non-VM mode') throw new Error('_addAccount() cannot be called in non-VM mode')
} }
if (self.accounts) { if (this.accounts) {
privateKey = Buffer.from(privateKey, 'hex') privateKey = Buffer.from(privateKey, 'hex')
var address = ethJSUtil.privateToAddress(privateKey) const address = ethJSUtil.privateToAddress(privateKey)
// FIXME: we don't care about the callback, but we should still make this proper // FIXME: we don't care about the callback, but we should still make this proper
let stateManager = executionContext.vm().stateManager let stateManager = executionContext.vm().stateManager
...@@ -106,37 +121,59 @@ UniversalDApp.prototype._addAccount = function (privateKey, balance) { ...@@ -106,37 +121,59 @@ UniversalDApp.prototype._addAccount = function (privateKey, balance) {
if (error) console.log(error) if (error) console.log(error)
}) })
}) })
self.accounts['0x' + address.toString('hex')] = { privateKey: privateKey, nonce: 0 }
}
}
UniversalDApp.prototype.getAccounts = function (cb) { this.accounts['0x' + address.toString('hex')] = { privateKey, nonce: 0 }
var self = this }
}
if (!executionContext.isVM()) { /** Return the list of accounts */
// Weirdness of web3: listAccounts() is sync, `getListAccounts()` is async getAccounts (cb) {
// See: https://github.com/ethereum/web3.js/issues/442 return new Promise((resolve, reject) => {
if (this._deps.config.get('settings/personal-mode')) { const provider = executionContext.getProvider()
return executionContext.web3().personal.getListAccounts(cb) switch (provider) {
} else { case 'vm': {
executionContext.web3().eth.getAccounts(cb) if (!this.accounts) {
if (cb) cb('No accounts?')
reject('No accounts?')
return
} }
if (cb) cb(null, Object.keys(this.accounts))
resolve(Object.keys(this.accounts))
}
break
case 'web3': {
if (this.config.get('settings/personal-mode')) {
return executionContext.web3().personal.getListAccounts((error, accounts) => {
if (cb) cb(error, accounts)
if (error) return reject(error)
resolve(accounts)
})
} else { } else {
if (!self.accounts) { executionContext.web3().eth.getAccounts((error, accounts) => {
return cb('No accounts?') if (cb) cb(error, accounts)
if (error) return reject(error)
resolve(accounts)
})
} }
cb(null, Object.keys(self.accounts))
} }
} break
case 'injected': {
UniversalDApp.prototype.getBalance = function (address, cb) { executionContext.web3().eth.getAccounts((error, accounts) => {
var self = this if (cb) cb(error, accounts)
if (error) return reject(error)
resolve(accounts)
})
}
}
})
}
/** Get the balance of an address */
getBalance (address, cb) {
address = ethJSUtil.stripHexPrefix(address) address = ethJSUtil.stripHexPrefix(address)
if (!executionContext.isVM()) { if (!executionContext.isVM()) {
executionContext.web3().eth.getBalance(address, function (err, res) { executionContext.web3().eth.getBalance(address, (err, res) => {
if (err) { if (err) {
cb(err) cb(err)
} else { } else {
...@@ -144,11 +181,11 @@ UniversalDApp.prototype.getBalance = function (address, cb) { ...@@ -144,11 +181,11 @@ UniversalDApp.prototype.getBalance = function (address, cb) {
} }
}) })
} else { } else {
if (!self.accounts) { if (!this.accounts) {
return cb('No accounts?') return cb('No accounts?')
} }
executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), function (err, res) { executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => {
if (err) { if (err) {
cb('Account not found') cb('Account not found')
} else { } else {
...@@ -156,37 +193,37 @@ UniversalDApp.prototype.getBalance = function (address, cb) { ...@@ -156,37 +193,37 @@ UniversalDApp.prototype.getBalance = function (address, cb) {
} }
}) })
} }
} }
UniversalDApp.prototype.getBalanceInEther = function (address, callback) { /** Get the balance of an address, and convert wei to ether */
var self = this getBalanceInEther (address, callback) {
self.getBalance(address, (error, balance) => { this.getBalance(address, (error, balance) => {
if (error) { if (error) {
callback(error) callback(error)
} else { } else {
callback(null, executionContext.web3().fromWei(balance, 'ether')) callback(null, executionContext.web3().fromWei(balance, 'ether'))
} }
}) })
} }
UniversalDApp.prototype.pendingTransactionsCount = function () { pendingTransactionsCount () {
return Object.keys(this.txRunner.pendingTxs).length return Object.keys(this.txRunner.pendingTxs).length
} }
/** /**
* deploy the given contract * deploy the given contract
* *
* @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). * @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ).
* @param {Function} callback - callback. * @param {Function} callback - callback.
*/ */
UniversalDApp.prototype.createContract = function (data, confirmationCb, continueCb, promptCb, callback) { createContract (data, confirmationCb, continueCb, promptCb, callback) {
this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => { this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => {
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case) // see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult) callback(error, txResult)
}) })
} }
/** /**
* call the current given contract * call the current given contract
* *
* @param {String} to - address of the contract to call. * @param {String} to - address of the contract to call.
...@@ -194,50 +231,79 @@ UniversalDApp.prototype.createContract = function (data, confirmationCb, continu ...@@ -194,50 +231,79 @@ UniversalDApp.prototype.createContract = function (data, confirmationCb, continu
* @param {Object} funAbi - abi definition of the function to call. * @param {Object} funAbi - abi definition of the function to call.
* @param {Function} callback - callback. * @param {Function} callback - callback.
*/ */
UniversalDApp.prototype.callFunction = function (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) { callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) {
this.runTx({to: to, data: data, useCall: funAbi.constant}, confirmationCb, continueCb, promptCb, (error, txResult) => { this.runTx({to: to, data: data, useCall: funAbi.constant}, confirmationCb, continueCb, promptCb, (error, txResult) => {
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case) // see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult) callback(error, txResult)
}) })
} }
UniversalDApp.prototype.context = function () { context () {
return (executionContext.isVM() ? 'memory' : 'blockchain') return (executionContext.isVM() ? 'memory' : 'blockchain')
} }
UniversalDApp.prototype.getABI = function (contract) { getABI (contract) {
return txHelper.sortAbiFunction(contract.abi) return txHelper.sortAbiFunction(contract.abi)
} }
UniversalDApp.prototype.getFallbackInterface = function (contractABI) { getFallbackInterface (contractABI) {
return txHelper.getFallbackInterface(contractABI) return txHelper.getFallbackInterface(contractABI)
} }
UniversalDApp.prototype.getInputs = function (funABI) { getInputs (funABI) {
if (!funABI.inputs) { if (!funABI.inputs) {
return '' return ''
} }
return txHelper.inputParametersDeclarationToString(funABI.inputs) return txHelper.inputParametersDeclarationToString(funABI.inputs)
} }
/** /**
* This function send a tx only to javascript VM or testnet, will return an error for the mainnet
* SHOULD BE TAKEN CAREFULLY!
*
* @param {Object} tx - transaction.
*/
sendTransaction (tx) {
return new Promise((resolve, reject) => {
executionContext.detectNetwork((error, network) => {
if (error) return reject(error)
if (network.name === 'Main' && network.id === '1') {
return reject(new Error('It is not allowed to make this action against mainnet'))
}
this.silentRunTx(tx, (error, result) => {
if (error) return reject(error)
resolve({
transactionHash: result.transactionHash,
status: result.result.status,
gasUsed: '0x' + result.result.gasUsed.toString('hex'),
error: result.result.vm.exceptionError,
return: result.result.vm.return ? '0x' + result.result.vm.return.toString('hex') : '0x',
createdAddress: result.result.createdAddress ? '0x' + result.result.createdAddress.toString('hex') : undefined
})
})
})
})
}
/**
* This function send a tx without alerting the user (if mainnet or if gas estimation too high). * This function send a tx without alerting the user (if mainnet or if gas estimation too high).
* SHOULD BE TAKEN CAREFULLY! * SHOULD BE TAKEN CAREFULLY!
* *
* @param {Object} tx - transaction. * @param {Object} tx - transaction.
* @param {Function} callback - callback. * @param {Function} callback - callback.
*/ */
UniversalDApp.prototype.silentRunTx = function (tx, cb) { silentRunTx (tx, cb) {
if (!executionContext.isVM()) return cb('Cannot silently send transaction through a web3 provider') if (!executionContext.isVM()) return cb('Cannot silently send transaction through a web3 provider')
this.txRunner.rawRun( this.txRunner.rawRun(
tx, tx,
(network, tx, gasEstimation, continueTxExecution, cancelCb) => { continueTxExecution() }, (network, tx, gasEstimation, continueTxExecution, cancelCb) => { continueTxExecution() },
(error, continueTxExecution, cancelCb) => { if (error) { cb(error) } else { continueTxExecution() } }, (error, continueTxExecution, cancelCb) => { if (error) { cb(error) } else { continueTxExecution() } },
(okCb, cancelCb) => { okCb() }, (okCb, cancelCb) => { okCb() },
cb) cb
} )
}
UniversalDApp.prototype.runTx = function (args, confirmationCb, continueCb, promptCb, cb) { runTx (args, confirmationCb, continueCb, promptCb, cb) {
const self = this const self = this
async.waterfall([ async.waterfall([
function getGasLimit (next) { function getGasLimit (next) {
...@@ -278,9 +344,9 @@ UniversalDApp.prototype.runTx = function (args, confirmationCb, continueCb, prom ...@@ -278,9 +344,9 @@ UniversalDApp.prototype.runTx = function (args, confirmationCb, continueCb, prom
}) })
}, },
function runTransaction (fromAddress, value, gasLimit, next) { function runTransaction (fromAddress, value, gasLimit, next) {
var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: value, gasLimit: gasLimit, timestamp: args.data.timestamp } const tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: value, gasLimit: gasLimit, timestamp: args.data.timestamp }
var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName, contractABI: args.data.contractABI, linkReferences: args.data.linkReferences } const payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName, contractABI: args.data.contractABI, linkReferences: args.data.linkReferences }
var timestamp = Date.now() const timestamp = Date.now()
if (tx.timestamp) { if (tx.timestamp) {
timestamp = tx.timestamp timestamp = tx.timestamp
} }
...@@ -302,6 +368,5 @@ UniversalDApp.prototype.runTx = function (args, confirmationCb, continueCb, prom ...@@ -302,6 +368,5 @@ UniversalDApp.prototype.runTx = function (args, confirmationCb, continueCb, prom
) )
} }
], cb) ], cb)
}
} }
\ No newline at end of file
module.exports = UniversalDApp
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment