var $ = require('jquery'); var EthJSVM = require('ethereumjs-vm'); var Trie = require('merkle-patricia-tree'); var ethJSUtil = require('ethereumjs-util'); var EthJSTX = require('ethereumjs-tx'); var ethJSABI = require('ethereumjs-abi'); var EthJSBlock = require('ethereumjs-block'); var web3 = require('./web3-adapter.js'); function UniversalDApp (contracts, options) { this.options = options || {}; this.$el = $('
'); this.contracts = contracts; this.renderOutputModifier = options.renderOutputModifier || function(name, content) { return content; }; if (options.vm) { this.accounts = {}; this.BN = ethJSUtil.BN; this.stateTrie = new Trie(); this.vm = new EthJSVM(this.stateTrie, null, { activatePrecompiles: true }); this.addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511'); this.addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c'); } else if (!web3.currentProvider) { var host = options.host || "localhost"; var port = options.port || "8545"; var rpc_url = options.getWeb3endpoint ? options.getWeb3endpoint() : ('http://' + host + ':' + port); web3.setProvider( new web3.providers.HttpProvider( rpc_url ) ); } } UniversalDApp.prototype.addAccount = function (privateKey, balance) { if (this.accounts) { privateKey = new Buffer(privateKey, 'hex'); var address = ethJSUtil.privateToAddress(privateKey); // FIXME: we don't care about the callback, but we should still make this proper this.vm.stateManager.putAccountBalance(address, balance || 'f00000000000000001', function cb() {} ); this.accounts['0x' + address.toString('hex')] = { privateKey: privateKey, nonce: 0 }; } }; UniversalDApp.prototype.getAccounts = function (cb) { if (!this.vm) { web3.eth.getAccounts(cb); } else { if (!this.accounts) return cb("No accounts?"); cb(null, Object.keys(this.accounts)); } }; UniversalDApp.prototype.getBalance = function (address, cb) { address = ethJSUtil.stripHexPrefix(address); if (!this.vm) { web3.eth.getBalance(address, function (err, res) { if (err) { cb(err); } else { cb(null, res.toString(10)); } }); } else { if (!this.accounts) return cb("No accounts?"); this.vm.stateManager.getAccountBalance(new Buffer(address, 'hex'), function (err, res) { if (err) { cb("Account not found"); } else { cb(null, new ethJSUtil.BN(res).toString(10)); } }); } }; UniversalDApp.prototype.render = function () { if (this.contracts.length == 0) { this.$el.append( this.getABIInputForm() ); } else { for (var c in this.contracts) { var $contractEl = $('
'); if (this.contracts[c].address) { this.getInstanceInterface(this.contracts[c], this.contracts[c].address, $contractEl ); } else { var $title = $('').text( this.contracts[c].name ); if (this.contracts[c].bytecode) { $title.append($('
').text((this.contracts[c].bytecode.length / 2) + ' bytes')); } $contractEl.append( $title ).append( this.getCreateInterface( $contractEl, this.contracts[c]) ); } this.$el.append(this.renderOutputModifier(this.contracts[c].name, $contractEl)); } } $legend = $('
') .append( $('
').text('Attach') ) .append( $('
').text('Transact') ) .append( $('
').text('Call') ); this.$el.append( $('
') .html("Universal ÐApp powered by The Blockchain") ); this.$el.append( $legend ); return this.$el; }; UniversalDApp.prototype.getContractByName = function(contractName) { for (var c in this.contracts) if (this.contracts[c].name == contractName) return this.contracts[c]; return null; }; UniversalDApp.prototype.getABIInputForm = function (cb){ var self = this; var $el = $('
'); var $jsonInput = $('