Merge pull request #126 from axic/patch/vm-multiaddr

Support multiple accounts in VM mode
pull/1/head
chriseth 9 years ago
commit 2d8ed0b5d5
  1. 18
      assets/js/app.js
  2. 38
      assets/js/universal-dapp.js

@ -684,22 +684,22 @@
}}); }});
var $contractOutput = dapp.render(); var $contractOutput = dapp.render();
$txOrigin = $('#txorigin'); $txOrigin = $('#txorigin');
if (executionContext === 'vm') { function renderAccounts(err, accounts) {
$txOrigin.empty();
var addr = '0x' + dapp.address.toString('hex');
$txOrigin.val(addr);
$txOrigin.append($('<option />').val(addr).text(addr));
} else web3.eth.getAccounts(function(err, accounts) {
if (err) if (err)
renderError(err.message); renderError(err.message);
if (accounts && accounts[0]){ if (accounts && accounts[0]){
$txOrigin.empty(); $txOrigin.empty();
for( var a in accounts) { $txOrigin.append($('<option />').val(accounts[a]).text(accounts[a])); } for( var a in accounts) { $txOrigin.append($('<option />').val(accounts[a]).text(accounts[a])); }
$txOrigin.val(accounts[0]); $txOrigin.val(accounts[0]);
} else $txOrigin.val('unknown'); } else $txOrigin.val('unknown');
}); }
if (executionContext === 'vm') {
dapp.getAccounts(renderAccounts);
} else {
web3.eth.getAccounts(renderAccounts);
}
$contractOutput.find('.title').click(function(ev){ $(this).closest('.contract').toggleClass('hide'); }); $contractOutput.find('.title').click(function(ev){ $(this).closest('.contract').toggleClass('hide'); });
$('#output').append( $contractOutput ); $('#output').append( $contractOutput );

@ -7,16 +7,14 @@ function UniversalDApp (contracts, options) {
if (!options.vm && web3.currentProvider) { if (!options.vm && web3.currentProvider) {
} else if (options.vm) { } else if (options.vm) {
this.accounts = {}
this.BN = EthJS.BN; this.BN = EthJS.BN;
this.stateTrie = new EthJS.Trie(); this.stateTrie = new EthJS.Trie();
this.vm = new EthJS.VM(this.stateTrie); this.vm = new EthJS.VM(this.stateTrie);
this.secretKey = '3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511'
this.publicKey = '0406cc661590d48ee972944b35ad13ff03c7876eae3fd191e8a2f77311b0a3c6613407b5005e63d7d8d76b89d5f900cde691497688bb281e07a5052ff61edebdc0' this.addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511')
this.address = ethUtil.pubToAddress(new Buffer(this.publicKey, 'hex'), true); this.addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c')
this.account = new EthJS.Account();
this.account.balance = 'f00000000000000001';
this.nonce = 0;
this.vm.stateManager.trie.put(this.address, this.account.serialize());
} else { } else {
var host = options.host || "localhost"; var host = options.host || "localhost";
var port = options.port || "8545"; var port = options.port || "8545";
@ -25,6 +23,26 @@ function UniversalDApp (contracts, options) {
} }
} }
UniversalDApp.prototype.addAccount = function (privateKey, balance) {
if (this.accounts) {
privateKey = new Buffer(privateKey, 'hex')
var address = EthJS.Util.privateToAddress(privateKey);
var account = new EthJS.Account();
account.balance = balance || 'f00000000000000001';
this.vm.stateManager.trie.put(address, account.serialize());
this.accounts['0x' + address.toString('hex')] = { privateKey: privateKey, nonce: 0 };
}
};
UniversalDApp.prototype.getAccounts = function (cb) {
if (!this.accounts) return cb("No accounts?");
cb(null, Object.keys(this.accounts));
};
UniversalDApp.prototype.render = function () { UniversalDApp.prototype.render = function () {
if (this.contracts.length == 0) { if (this.contracts.length == 0) {
this.$el.append( this.getABIInputForm() ); this.$el.append( this.getABIInputForm() );
@ -437,15 +455,17 @@ UniversalDApp.prototype.runTx = function( data, args, cb) {
} }
} else { } else {
try { try {
var address = this.options.getAddress ? this.options.getAddress() : this.getAccounts()[0];
var account = this.accounts[address];
var tx = new EthJS.Tx({ var tx = new EthJS.Tx({
nonce: new Buffer([this.nonce++]), //@todo count beyond 255 nonce: new Buffer([account.nonce++]), //@todo count beyond 255
gasPrice: 1, gasPrice: 1,
gasLimit: 3000000000, //plenty gasLimit: 3000000000, //plenty
to: to, to: to,
value: value, value: value,
data: new Buffer(data.slice(2), 'hex') data: new Buffer(data.slice(2), 'hex')
}); });
tx.sign(new Buffer(this.secretKey, 'hex')); tx.sign(account.privateKey);
this.vm.runTx({tx: tx, skipBalance: true, skipNonce: true, enableHomestead: true}, cb); this.vm.runTx({tx: tx, skipBalance: true, skipNonce: true, enableHomestead: true}, cb);
} catch (e) { } catch (e) {
cb( e, null ); cb( e, null );

Loading…
Cancel
Save