|
|
@ -91,10 +91,10 @@ module.exports = class UniversalDApp { |
|
|
|
if (!this.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) => { |
|
|
|
return passwordPromptCb((passphrase) => { |
|
|
|
this.executionContext.web3().personal.newAccount(passphrase, cb) |
|
|
|
this.executionContext.web3().personal.newAccount(passphrase, cb) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} else { |
|
|
|
} |
|
|
|
let privateKey |
|
|
|
let privateKey |
|
|
|
do { |
|
|
|
do { |
|
|
|
privateKey = crypto.randomBytes(32) |
|
|
|
privateKey = crypto.randomBytes(32) |
|
|
@ -102,7 +102,6 @@ module.exports = class UniversalDApp { |
|
|
|
this._addAccount(privateKey, '0x56BC75E2D63100000') |
|
|
|
this._addAccount(privateKey, '0x56BC75E2D63100000') |
|
|
|
cb(null, '0x' + privateToAddress(privateKey).toString('hex')) |
|
|
|
cb(null, '0x' + privateToAddress(privateKey).toString('hex')) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Add an account to the list of account (only for Javascript VM) */ |
|
|
|
/** Add an account to the list of account (only for Javascript VM) */ |
|
|
|
_addAccount (privateKey, balance) { |
|
|
|
_addAccount (privateKey, balance) { |
|
|
@ -110,7 +109,9 @@ module.exports = class UniversalDApp { |
|
|
|
throw new Error('_addAccount() cannot be called in non-VM mode') |
|
|
|
throw new Error('_addAccount() cannot be called in non-VM mode') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (this.accounts) { |
|
|
|
if (!this.accounts) { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
privateKey = Buffer.from(privateKey, 'hex') |
|
|
|
privateKey = Buffer.from(privateKey, 'hex') |
|
|
|
const address = privateToAddress(privateKey) |
|
|
|
const address = privateToAddress(privateKey) |
|
|
|
|
|
|
|
|
|
|
@ -119,14 +120,13 @@ module.exports = class UniversalDApp { |
|
|
|
stateManager.getAccount(address, (error, account) => { |
|
|
|
stateManager.getAccount(address, (error, account) => { |
|
|
|
if (error) return console.log(error) |
|
|
|
if (error) return console.log(error) |
|
|
|
account.balance = balance || '0xf00000000000000001' |
|
|
|
account.balance = balance || '0xf00000000000000001' |
|
|
|
stateManager.putAccount(address, account, function cb (error) { |
|
|
|
stateManager.putAccount(address, account, function cb(error) { |
|
|
|
if (error) console.log(error) |
|
|
|
if (error) console.log(error) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } |
|
|
|
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Return the list of accounts */ |
|
|
|
/** Return the list of accounts */ |
|
|
|
getAccounts (cb) { |
|
|
|
getAccounts (cb) { |
|
|
@ -171,40 +171,36 @@ module.exports = class UniversalDApp { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** Get the balance of an address */ |
|
|
|
/** Get the balance of an address */ |
|
|
|
getBalance (address, cb) { |
|
|
|
getBalance(address, cb) { |
|
|
|
address = stripHexPrefix(address) |
|
|
|
address = stripHexPrefix(address) |
|
|
|
|
|
|
|
|
|
|
|
if (!this.executionContext.isVM()) { |
|
|
|
if (!this.executionContext.isVM()) { |
|
|
|
this.executionContext.web3().eth.getBalance(address, (err, res) => { |
|
|
|
return this.executionContext.web3().eth.getBalance(address, (err, res) => { |
|
|
|
if (err) { |
|
|
|
if (err) { |
|
|
|
cb(err) |
|
|
|
return cb(err) |
|
|
|
} else { |
|
|
|
|
|
|
|
cb(null, res.toString(10)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
cb(null, res.toString(10)) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} else { |
|
|
|
} |
|
|
|
if (!this.accounts) { |
|
|
|
if (!this.accounts) { |
|
|
|
return cb('No accounts?') |
|
|
|
return cb('No accounts?') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => { |
|
|
|
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => { |
|
|
|
if (err) { |
|
|
|
if (err) { |
|
|
|
cb('Account not found') |
|
|
|
return cb('Account not found') |
|
|
|
} else { |
|
|
|
|
|
|
|
cb(null, new BN(res.balance).toString(10)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
cb(null, new BN(res.balance).toString(10)) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Get the balance of an address, and convert wei to ether */ |
|
|
|
/** Get the balance of an address, and convert wei to ether */ |
|
|
|
getBalanceInEther (address, callback) { |
|
|
|
getBalanceInEther (address, callback) { |
|
|
|
this.getBalance(address, (error, balance) => { |
|
|
|
this.getBalance(address, (error, balance) => { |
|
|
|
if (error) { |
|
|
|
if (error) { |
|
|
|
callback(error) |
|
|
|
return callback(error) |
|
|
|
} else { |
|
|
|
|
|
|
|
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether')) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether')) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -219,10 +215,7 @@ module.exports = class UniversalDApp { |
|
|
|
* @param {Function} callback - callback. |
|
|
|
* @param {Function} callback - callback. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
createContract (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, callback) |
|
|
|
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
|
|
|
|
|
|
|
|
callback(error, txResult) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -235,10 +228,7 @@ module.exports = class UniversalDApp { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) { |
|
|
|
callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) { |
|
|
|
const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure' |
|
|
|
const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure' |
|
|
|
this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, (error, txResult) => { |
|
|
|
this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, callback) |
|
|
|
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
|
|
|
|
|
|
|
|
callback(error, txResult) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -249,10 +239,7 @@ module.exports = class UniversalDApp { |
|
|
|
* @param {Function} callback - callback. |
|
|
|
* @param {Function} callback - callback. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
sendRawTransaction (to, data, confirmationCb, continueCb, promptCb, callback) { |
|
|
|
sendRawTransaction (to, data, confirmationCb, continueCb, promptCb, callback) { |
|
|
|
this.runTx({to, data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => { |
|
|
|
this.runTx({to, data, useCall: false}, confirmationCb, continueCb, promptCb, callback) |
|
|
|
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
|
|
|
|
|
|
|
|
callback(error, txResult) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
context () { |
|
|
|
context () { |
|
|
|