From a6f820706388b726ae2f29ef8dee202cd8a42c5e Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 8 Apr 2016 23:42:38 +0100 Subject: [PATCH 1/8] Add newAccount() method to universal-dapp for creating a new account --- src/universal-dapp.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 602fee3aee..5f32039c2f 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -7,6 +7,7 @@ var ethJSABI = require('ethereumjs-abi'); var EthJSBlock = require('ethereumjs-block'); var BN = ethJSUtil.BN; var EventManager = require('./lib/eventManager'); +var crypto = require('crypto'); /* trigger debugRequested @@ -49,6 +50,14 @@ UniversalDApp.prototype.reset = function (contracts, getAddress, getValue, getGa } }; +UniversalDApp.prototype.newAccount = function () { + var privateKey; + do { + privateKey = crypto.randomBytes(32); + } while (!ethJSUtil.isValidPrivate(privateKey)); + this._addAccount(privateKey); +}; + UniversalDApp.prototype.addAccount = function (privateKey, balance) { var self = this; From fcbed88ffaa1a1aa5331d704c3825c5b5a53a92a Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 8 Apr 2016 23:56:30 +0100 Subject: [PATCH 2/8] Make addAccount() internal to the VM mode --- src/universal-dapp.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 5f32039c2f..000075e05b 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -42,11 +42,11 @@ UniversalDApp.prototype.reset = function (contracts, getAddress, getValue, getGa this.renderOutputModifier = renderer; this.accounts = {}; if (this.executionContext.isVM()) { - this.addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511'); - this.addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c'); - this.addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a'); - this.addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea'); - this.addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce'); + this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511'); + this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c'); + this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a'); + this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea'); + this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce'); } }; @@ -58,9 +58,13 @@ UniversalDApp.prototype.newAccount = function () { this._addAccount(privateKey); }; -UniversalDApp.prototype.addAccount = function (privateKey, balance) { +UniversalDApp.prototype._addAccount = function (privateKey, balance) { var self = this; + if (!self.executionContext.isVM()) { + throw new Error('_addAccount() cannot be called in non-VM mode'); + } + if (self.accounts) { privateKey = new Buffer(privateKey, 'hex'); var address = ethJSUtil.privateToAddress(privateKey); From 9439b087f0c930006786d33e549c67f3c5edc198 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 12 Apr 2016 14:20:14 +0100 Subject: [PATCH 3/8] Use the personal account API in Web3 --- src/universal-dapp.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 000075e05b..442b68b389 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -50,12 +50,16 @@ UniversalDApp.prototype.reset = function (contracts, getAddress, getValue, getGa } }; -UniversalDApp.prototype.newAccount = function () { - var privateKey; - do { - privateKey = crypto.randomBytes(32); - } while (!ethJSUtil.isValidPrivate(privateKey)); - this._addAccount(privateKey); +UniversalDApp.prototype.newAccount = function (password) { + if (!this.executionContext.isVM()) { + this.web3.personal.newAccount(password); + } else { + var privateKey; + do { + privateKey = crypto.randomBytes(32); + } while (!ethJSUtil.isValidPrivate(privateKey)); + this._addAccount(privateKey); + } }; UniversalDApp.prototype._addAccount = function (privateKey, balance) { @@ -80,7 +84,7 @@ UniversalDApp.prototype.getAccounts = function (cb) { var self = this; if (!self.executionContext.isVM()) { - self.web3.eth.getAccounts(cb); + self.web3.personal.getAccounts(cb); } else { if (!self.accounts) { return cb('No accounts?'); From 1d4cf17b8b20bf8bfe86303c41328e22efaf60e0 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 26 Apr 2016 16:35:26 +0100 Subject: [PATCH 4/8] Fallback to eth.getAccounts() if personal.listAccounts() fails --- src/universal-dapp.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 442b68b389..1ddd9729b7 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -84,7 +84,18 @@ UniversalDApp.prototype.getAccounts = function (cb) { var self = this; if (!self.executionContext.isVM()) { - self.web3.personal.getAccounts(cb); + // TOOD: remove the try/catch if this is fixed: https://github.com/ethereum/web3.js/issues/442 + try { + self.web3.personal.listAccounts(function (err, res) { + if (err) { + self.web3.eth.getAccounts(cb); + } else { + cb(err, res); + } + }); + } catch (e) { + self.web3.eth.getAccounts(cb); + } } else { if (!self.accounts) { return cb('No accounts?'); From e6cee02ee741faae1c8326d99b9fb92e94ee9781 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 29 Jul 2016 18:02:38 +0100 Subject: [PATCH 5/8] Use getListAccounts() for async calls --- src/universal-dapp.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 1ddd9729b7..5a9eb35a00 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -84,18 +84,15 @@ UniversalDApp.prototype.getAccounts = function (cb) { var self = this; if (!self.executionContext.isVM()) { - // TOOD: remove the try/catch if this is fixed: https://github.com/ethereum/web3.js/issues/442 - try { - self.web3.personal.listAccounts(function (err, res) { - if (err) { - self.web3.eth.getAccounts(cb); - } else { - cb(err, res); - } - }); - } catch (e) { - self.web3.eth.getAccounts(cb); - } + // Weirdness of web3: listAccounts() is sync, `getListAccounts()` is async + // See: https://github.com/ethereum/web3.js/issues/442 + self.web3.personal.getListAccounts(function (err, res) { + if (err) { + self.web3.eth.getAccounts(cb); + } else { + cb(err, res); + } + }); } else { if (!self.accounts) { return cb('No accounts?'); From 255849e5936cff5d02c3dc6350cf291fe7c82f91 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 29 Jul 2016 18:08:15 +0100 Subject: [PATCH 6/8] Introduce personalMode switch to control eth vs. personal accounts from outside --- src/universal-dapp.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 5a9eb35a00..9c7ed115df 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -18,6 +18,7 @@ function UniversalDApp (executionContext, options, txdebugger) { self.options = options || {}; self.$el = $('
'); + self.personalMode = self.options.personalMode || false; self.contracts; self.getAddress; self.getValue; @@ -52,6 +53,9 @@ UniversalDApp.prototype.reset = function (contracts, getAddress, getValue, getGa UniversalDApp.prototype.newAccount = function (password) { if (!this.executionContext.isVM()) { + if (!this.personalMode) { + throw new Error('Not running in personal mode'); + } this.web3.personal.newAccount(password); } else { var privateKey; @@ -86,13 +90,11 @@ UniversalDApp.prototype.getAccounts = function (cb) { if (!self.executionContext.isVM()) { // Weirdness of web3: listAccounts() is sync, `getListAccounts()` is async // See: https://github.com/ethereum/web3.js/issues/442 - self.web3.personal.getListAccounts(function (err, res) { - if (err) { - self.web3.eth.getAccounts(cb); - } else { - cb(err, res); - } - }); + if (self.personalMode) { + self.web3.personal.getListAccounts(cb); + } else { + self.web3.eth.getAccounts(cb); + } } else { if (!self.accounts) { return cb('No accounts?'); From 6c8dd998a4b586b021bd835918f3459af12a6b55 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 29 Jul 2016 18:09:35 +0100 Subject: [PATCH 7/8] Support web3.personal.unlockAccountAndSendTransaction --- src/universal-dapp.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 9c7ed115df..2b58c4fe41 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -730,7 +730,9 @@ UniversalDApp.prototype.runTx = function (data, args, cb) { tx.gas = resp; - self.web3.eth.sendTransaction(tx, function (err, resp) { + var sendTransaction = self.personalMode ? self.web3.personal.unlockAccountAndSendTransaction : self.web3.eth.sendTransaction; + + sendTransaction(tx, function (err, resp) { if (err) { return cb(err, resp); } From eb43b7b0dae13a96b25652322c4d6472478a904d Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 29 Jul 2016 18:10:14 +0100 Subject: [PATCH 8/8] Require web3.js#35bcace for web3.personal.unlockAccountAndSendTransaction --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e05c62359e..0ac8d24046 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "semver": "^5.3.0", "solc": "https://github.com/ethereum/solc-js", "tape": "^4.5.1", - "web3": "^0.16.0", + "web3": "https://github.com/ethereum/web3.js#35bcace7173fda742ddb1bba84c8a70f0fa849e0", "webworkify": "^1.2.1", "yo-yo": "^1.2.2", "yo-yoify": "^3.3.0",