aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/mist/assets/ext/ethereum.js/lib/web3.js
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/mist/assets/ext/ethereum.js/lib/web3.js')
-rw-r--r--cmd/mist/assets/ext/ethereum.js/lib/web3.js397
1 files changed, 72 insertions, 325 deletions
diff --git a/cmd/mist/assets/ext/ethereum.js/lib/web3.js b/cmd/mist/assets/ext/ethereum.js/lib/web3.js
index 2fe414259..41df75051 100644
--- a/cmd/mist/assets/ext/ethereum.js/lib/web3.js
+++ b/cmd/mist/assets/ext/ethereum.js/lib/web3.js
@@ -14,7 +14,7 @@
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 main.js
+/** @file web3.js
* @authors:
* Jeffrey Wilcke <jeff@ethdev.com>
* Marek Kotewicz <marek@ethdev.com>
@@ -23,51 +23,20 @@
* @date 2014
*/
-function flattenPromise (obj) {
- if (obj instanceof Promise) {
- return Promise.resolve(obj);
- }
-
- if (obj instanceof Array) {
- return new Promise(function (resolve) {
- var promises = obj.map(function (o) {
- return flattenPromise(o);
- });
-
- return Promise.all(promises).then(function (res) {
- for (var i = 0; i < obj.length; i++) {
- obj[i] = res[i];
- }
- resolve(obj);
- });
- });
- }
-
- if (obj instanceof Object) {
- return new Promise(function (resolve) {
- var keys = Object.keys(obj);
- var promises = keys.map(function (key) {
- return flattenPromise(obj[key]);
- });
-
- return Promise.all(promises).then(function (res) {
- for (var i = 0; i < keys.length; i++) {
- obj[keys[i]] = res[i];
- }
- resolve(obj);
- });
- });
- }
-
- return Promise.resolve(obj);
+if (process.env.NODE_ENV !== 'build') {
+ var BigNumber = require('bignumber.js');
}
+var utils = require('./utils');
+
+/// @returns an array of objects describing web3 api methods
var web3Methods = function () {
return [
{ name: 'sha3', call: 'web3_sha3' }
];
};
+/// @returns an array of objects describing web3.eth api methods
var ethMethods = function () {
var blockCall = function (args) {
return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber";
@@ -93,6 +62,7 @@ var ethMethods = function () {
{ name: 'transaction', call: transactionCall },
{ name: 'uncle', call: uncleCall },
{ name: 'compilers', call: 'eth_compilers' },
+ { name: 'flush', call: 'eth_flush' },
{ name: 'lll', call: 'eth_lll' },
{ name: 'solidity', call: 'eth_solidity' },
{ name: 'serpent', call: 'eth_serpent' },
@@ -101,13 +71,13 @@ var ethMethods = function () {
return methods;
};
+/// @returns an array of objects describing web3.eth api properties
var ethProperties = function () {
return [
{ name: 'coinbase', getter: 'eth_coinbase', setter: 'eth_setCoinbase' },
{ name: 'listening', getter: 'eth_listening', setter: 'eth_setListening' },
{ name: 'mining', getter: 'eth_mining', setter: 'eth_setMining' },
{ name: 'gasPrice', getter: 'eth_gasPrice' },
- { name: 'account', getter: 'eth_account' },
{ name: 'accounts', getter: 'eth_accounts' },
{ name: 'peerCount', getter: 'eth_peerCount' },
{ name: 'defaultBlock', getter: 'eth_defaultBlock', setter: 'eth_setDefaultBlock' },
@@ -115,6 +85,7 @@ var ethProperties = function () {
];
};
+/// @returns an array of objects describing web3.db api methods
var dbMethods = function () {
return [
{ name: 'put', call: 'db_put' },
@@ -124,6 +95,7 @@ var dbMethods = function () {
];
};
+/// @returns an array of objects describing web3.shh api methods
var shhMethods = function () {
return [
{ name: 'post', call: 'shh_post' },
@@ -134,6 +106,7 @@ var shhMethods = function () {
];
};
+/// @returns an array of objects describing web3.eth.watch api methods
var ethWatchMethods = function () {
var newFilter = function (args) {
return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter';
@@ -146,6 +119,7 @@ var ethWatchMethods = function () {
];
};
+/// @returns an array of objects describing web3.shh.watch api methods
var shhWatchMethods = function () {
return [
{ name: 'newFilter', call: 'shh_newFilter' },
@@ -154,57 +128,37 @@ var shhWatchMethods = function () {
];
};
+/// 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) {
obj[method.name] = function () {
- return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) {
- var call = typeof method.call === "function" ? method.call(args) : method.call;
- return {call: call, args: args};
- }).then(function (request) {
- return new Promise(function (resolve, reject) {
- web3.provider.send(request, function (err, result) {
- if (!err) {
- resolve(result);
- return;
- }
- reject(err);
- });
- });
- }).catch(function(err) {
- console.error(err);
+ var args = Array.prototype.slice.call(arguments);
+ var call = typeof method.call === 'function' ? method.call(args) : method.call;
+ return web3.provider.send({
+ method: call,
+ params: args
});
};
});
};
+/// 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) {
var proto = {};
proto.get = function () {
- return new Promise(function(resolve, reject) {
- web3.provider.send({call: property.getter}, function(err, result) {
- if (!err) {
- resolve(result);
- return;
- }
- reject(err);
- });
+ return web3.provider.send({
+ method: property.getter
});
};
+
if (property.setter) {
proto.set = function (val) {
- return flattenPromise([val]).then(function (args) {
- return new Promise(function (resolve) {
- web3.provider.send({call: property.setter, args: args}, function (err, result) {
- if (!err) {
- resolve(result);
- return;
- }
- reject(err);
- });
- });
- }).catch(function (err) {
- console.error(err);
+ return web3.provider.send({
+ method: property.setter,
+ params: [val]
});
};
}
@@ -212,133 +166,70 @@ var setupProperties = function (obj, properties) {
});
};
-// TODO: import from a dependency, don't duplicate.
-var hexToDec = function (hex) {
- return parseInt(hex, 16).toString();
-};
-
-var decToHex = function (dec) {
- return parseInt(dec).toString(16);
-};
-
-
+/// setups web3 object, and it's in-browser executed methods
var web3 = {
_callbacks: {},
_events: {},
providers: {},
- toHex: function(str) {
- var hex = "";
- for(var i = 0; i < str.length; i++) {
- var n = str.charCodeAt(i).toString(16);
- hex += n.length < 2 ? '0' + n : n;
- }
-
- return hex;
- },
-
- toAscii: function(hex) {
- // Find termination
- var str = "";
- var i = 0, l = hex.length;
- if (hex.substring(0, 2) === '0x')
- i = 2;
- for(; i < l; i+=2) {
- var code = hex.charCodeAt(i);
- if(code === 0) {
- break;
- }
-
- str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
- }
-
- return str;
- },
+ /// @returns ascii string representation of hex value prefixed with 0x
+ toAscii: utils.toAscii,
- fromAscii: function(str, pad) {
- pad = pad === undefined ? 32 : pad;
- var hex = this.toHex(str);
- while(hex.length < pad*2)
- hex += "00";
- return "0x" + hex;
- },
+ /// @returns hex representation (prefixed by 0x) of ascii string
+ fromAscii: utils.fromAscii,
+ /// @returns decimal representaton of hex value prefixed by 0x
toDecimal: function (val) {
- return hexToDec(val.substring(2));
+ // remove 0x and place 0, if it's required
+ val = val.length > 2 ? val.substring(2) : "0";
+ return (new BigNumber(val, 16).toString(10));
},
+ /// @returns hex representation (prefixed by 0x) of decimal value
fromDecimal: function (val) {
- return "0x" + decToHex(val);
+ return "0x" + (new BigNumber(val).toString(16));
},
- toEth: function(str) {
- var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
- var unit = 0;
- var units = [ 'wei', 'Kwei', 'Mwei', 'Gwei', 'szabo', 'finney', 'ether', 'grand', 'Mether', 'Gether', 'Tether', 'Pether', 'Eether', 'Zether', 'Yether', 'Nether', 'Dether', 'Vether', 'Uether' ];
- while (val > 3000 && unit < units.length - 1)
- {
- val /= 1000;
- unit++;
- }
- var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2);
- var replaceFunction = function($0, $1, $2) {
- return $1 + ',' + $2;
- };
-
- while (true) {
- var o = s;
- s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction);
- if (o === s)
- break;
- }
- return s + ' ' + units[unit];
- },
+ /// used to transform value/string to eth string
+ toEth: utils.toEth,
+ /// eth object prototype
eth: {
- prototype: Object(), // jshint ignore:line
- watch: function (params) {
- return new Filter(params, ethWatch);
+ contractFromAbi: function (abi) {
+ return function(addr) {
+ // Default to address of Config. TODO: rremove prior to genesis.
+ addr = addr || '0xc6d9d2cd449a754c494264e1809c50e34d64562b';
+ var ret = web3.eth.contract(addr, abi);
+ ret.address = addr;
+ return ret;
+ };
+ },
+
+ /// @param filter may be a string, object or event
+ /// @param indexed is optional, this is an object with optional event indexed params
+ /// @param options is optional, this is an object with optional event options ('max'...)
+ watch: function (filter, indexed, options) {
+ if (filter._isEvent) {
+ return filter(indexed, options);
+ }
+ return new web3.filter(filter, ethWatch);
}
},
- db: {
- prototype: Object() // jshint ignore:line
- },
+ /// db object prototype
+ db: {},
+ /// shh object prototype
shh: {
- prototype: Object(), // jshint ignore:line
- watch: function (params) {
- return new Filter(params, shhWatch);
+
+ /// @param filter may be a string, object or event
+ watch: function (filter, indexed) {
+ return new web3.filter(filter, shhWatch);
}
},
-
- on: function(event, id, cb) {
- if(web3._events[event] === undefined) {
- web3._events[event] = {};
- }
-
- web3._events[event][id] = cb;
- return this;
- },
-
- off: function(event, id) {
- if(web3._events[event] !== undefined) {
- delete web3._events[event][id];
- }
-
- return this;
- },
-
- trigger: function(event, id, data) {
- var callbacks = web3._events[event];
- if (!callbacks || !callbacks[id]) {
- return;
- }
- var cb = callbacks[id];
- cb(data);
- }
};
+/// setups all api methods
setupMethods(web3, web3Methods());
setupMethods(web3.eth, ethMethods());
setupProperties(web3.eth, ethProperties());
@@ -348,162 +239,18 @@ setupMethods(web3.shh, shhMethods());
var ethWatch = {
changed: 'eth_changed'
};
+
setupMethods(ethWatch, ethWatchMethods());
+
var shhWatch = {
changed: 'shh_changed'
};
-setupMethods(shhWatch, shhWatchMethods());
-
-var ProviderManager = function() {
- this.queued = [];
- this.polls = [];
- this.ready = false;
- this.provider = undefined;
- this.id = 1;
-
- var self = this;
- var poll = function () {
- if (self.provider && self.provider.poll) {
- self.polls.forEach(function (data) {
- data.data._id = self.id;
- self.id++;
- self.provider.poll(data.data, data.id);
- });
- }
- setTimeout(poll, 12000);
- };
- poll();
-};
-
-ProviderManager.prototype.send = function(data, cb) {
- data._id = this.id;
- if (cb) {
- web3._callbacks[data._id] = cb;
- }
-
- data.args = data.args || [];
- this.id++;
-
- if(this.provider !== undefined) {
- this.provider.send(data);
- } else {
- console.warn("provider is not set");
- this.queued.push(data);
- }
-};
-
-ProviderManager.prototype.set = function(provider) {
- if(this.provider !== undefined && this.provider.unload !== undefined) {
- this.provider.unload();
- }
-
- this.provider = provider;
- this.ready = true;
-};
-ProviderManager.prototype.sendQueued = function() {
- for(var i = 0; this.queued.length; i++) {
- // Resend
- this.send(this.queued[i]);
- }
-};
-
-ProviderManager.prototype.installed = function() {
- return this.provider !== undefined;
-};
-
-ProviderManager.prototype.startPolling = function (data, pollId) {
- if (!this.provider || !this.provider.poll) {
- return;
- }
- this.polls.push({data: data, id: pollId});
-};
-
-ProviderManager.prototype.stopPolling = function (pollId) {
- for (var i = this.polls.length; i--;) {
- var poll = this.polls[i];
- if (poll.id === pollId) {
- this.polls.splice(i, 1);
- }
- }
-};
-
-web3.provider = new ProviderManager();
+setupMethods(shhWatch, shhWatchMethods());
web3.setProvider = function(provider) {
- provider.onmessage = messageHandler;
web3.provider.set(provider);
- web3.provider.sendQueued();
};
-web3.haveProvider = function() {
- return !!web3.provider.provider;
-};
-
-var Filter = function(options, impl) {
- this.impl = impl;
- this.callbacks = [];
-
- var self = this;
- this.promise = impl.newFilter(options);
- this.promise.then(function (id) {
- self.id = id;
- web3.on(impl.changed, id, self.trigger.bind(self));
- web3.provider.startPolling({call: impl.changed, args: [id]}, id);
- });
-};
-
-Filter.prototype.arrived = function(callback) {
- this.changed(callback);
-};
-
-Filter.prototype.changed = function(callback) {
- var self = this;
- this.promise.then(function(id) {
- self.callbacks.push(callback);
- });
-};
-
-Filter.prototype.trigger = function(messages) {
- for(var i = 0; i < this.callbacks.length; i++) {
- this.callbacks[i].call(this, messages);
- }
-};
-
-Filter.prototype.uninstall = function() {
- var self = this;
- this.promise.then(function (id) {
- self.impl.uninstallFilter(id);
- web3.provider.stopPolling(id);
- web3.off(impl.changed, id);
- });
-};
-
-Filter.prototype.messages = function() {
- var self = this;
- return this.promise.then(function (id) {
- return self.impl.getMessages(id);
- });
-};
-
-Filter.prototype.logs = function () {
- return this.messages();
-};
-
-function messageHandler(data) {
- if(data._event !== undefined) {
- web3.trigger(data._event, data._id, data.data);
- return;
- }
-
- if(data._id) {
- var cb = web3._callbacks[data._id];
- if (cb) {
- cb.call(this, data.error, data.data);
- delete web3._callbacks[data._id];
- }
- }
-}
+module.exports = web3;
-if (typeof(module) !== "undefined")
- module.exports = web3;