1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 26 1 3 11 1 1 1 1 1 1 4 4 1 1 4 1 1 1 152 1 9 9 9 1 1 1 1 1 1 1 1 1 1 5 1 1 1 1 1 1 1 1 1 1 | /* This file is part of ethereum.js. ethereum.js is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ethereum.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see <http://www.gnu.org/licenses/>. */ /** @file web3.js * @authors: * Jeffrey Wilcke <jeff@ethdev.com> * Marek Kotewicz <marek@ethdev.com> * Marian Oancea <marian@ethdev.com> * Fabian Vogelsteller <fabian@ethdev.com> * Gav Wood <g@ethdev.com> * @date 2014 */ var version = require('./version.json'); var net = require('./web3/net'); var eth = require('./web3/eth'); var db = require('./web3/db'); var shh = require('./web3/shh'); var watches = require('./web3/watches'); var Filter = require('./web3/filter'); var utils = require('./utils/utils'); var formatters = require('./web3/formatters'); var RequestManager = require('./web3/requestmanager'); var c = require('./utils/config'); var Method = require('./web3/method'); var Property = require('./web3/property'); var web3Methods = [ new Method({ name: 'sha3', call: 'web3_sha3', params: 1 }) ]; var web3Properties = [ new Property({ name: 'version.client', getter: 'web3_clientVersion' }), new Property({ name: 'version.network', getter: 'net_version', inputFormatter: utils.toDecimal }), new Property({ name: 'version.ethereum', getter: 'eth_protocolVersion', inputFormatter: utils.toDecimal }), new Property({ name: 'version.whisper', getter: 'shh_version', inputFormatter: utils.toDecimal }) ]; /// creates methods in a given object based on method description on input /// setups api calls for these methods var setupMethods = function (obj, methods) { methods.forEach(function (method) { method.attachToObject(obj); }); }; /// creates properties in a given object based on properties description on input /// setups api calls for these properties var setupProperties = function (obj, properties) { properties.forEach(function (property) { property.attachToObject(obj); }); }; /// setups web3 object, and it's in-browser executed methods var web3 = {}; web3.providers = {}; web3.version = {}; web3.version.api = version.version; web3.eth = {}; /*jshint maxparams:4 */ web3.eth.filter = function (fil, eventParams, options, formatter) { // if its event, treat it differently // TODO: simplify and remove Iif (fil._isEvent) { return fil(eventParams, options); } // what outputLogFormatter? that's wrong //return new Filter(fil, watches.eth(), formatters.outputLogFormatter); return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter); }; /*jshint maxparams:3 */ web3.shh = {}; web3.shh.filter = function (fil) { return new Filter(fil, watches.shh(), formatters.outputPostFormatter); }; web3.net = {}; web3.db = {}; web3.setProvider = function (provider) { RequestManager.getInstance().setProvider(provider); }; web3.reset = function () { RequestManager.getInstance().reset(); c.defaultBlock = 'latest'; c.defaultAccount = undefined; }; web3.toHex = utils.toHex; web3.toAscii = utils.toAscii; web3.fromAscii = utils.fromAscii; web3.toDecimal = utils.toDecimal; web3.fromDecimal = utils.fromDecimal; web3.toBigNumber = utils.toBigNumber; web3.toWei = utils.toWei; web3.fromWei = utils.fromWei; web3.isAddress = utils.isAddress; // ADD defaultblock Object.defineProperty(web3.eth, 'defaultBlock', { get: function () { return c.defaultBlock; }, set: function (val) { c.defaultBlock = val; return val; } }); Object.defineProperty(web3.eth, 'defaultAccount', { get: function () { return c.defaultAccount; }, set: function (val) { c.defaultAccount = val; return val; } }); /// setups all api methods setupMethods(web3, web3Methods); setupProperties(web3, web3Properties); setupMethods(web3.net, net.methods); setupProperties(web3.net, net.properties); setupMethods(web3.eth, eth.methods); setupProperties(web3.eth, eth.properties); setupMethods(web3.db, db.methods); setupMethods(web3.shh, shh.methods); module.exports = web3; |