Fixed indent
Fixed indent so we can compare differences in PR.
This commit is contained in:
196
dist/ethereum.js
vendored
196
dist/ethereum.js
vendored
@ -63,30 +63,30 @@ if (XMLHttpRequest) xhr.prototype = XMLHttpRequest.prototype;
|
|||||||
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
|
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
|
||||||
|
|
||||||
|
|
||||||
var HttpRpcProvider = function (host) {
|
var HttpRpcProvider = function (host) {
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
this.host = host;
|
this.host = host;
|
||||||
};
|
};
|
||||||
|
|
||||||
function formatJsonRpcObject(object) {
|
function formatJsonRpcObject(object) {
|
||||||
return {
|
return {
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
method: object.call,
|
method: object.call,
|
||||||
params: object.args,
|
params: object.args,
|
||||||
id: object._id
|
id: object._id
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatJsonRpcMessage(message) {
|
function formatJsonRpcMessage(message) {
|
||||||
var object = JSON.parse(message);
|
var object = JSON.parse(message);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
_id: object.id,
|
_id: object.id,
|
||||||
data: object.result
|
data: object.result
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
|
HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
|
||||||
var data = formatJsonRpcObject(payload);
|
var data = formatJsonRpcObject(payload);
|
||||||
|
|
||||||
var request = new XMLHttpRequest();
|
var request = new XMLHttpRequest();
|
||||||
@ -97,18 +97,18 @@ HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
|
|||||||
cb(request);
|
cb(request);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpRpcProvider.prototype.send = function (payload) {
|
HttpRpcProvider.prototype.send = function (payload) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.sendRequest(payload, function (request) {
|
this.sendRequest(payload, function (request) {
|
||||||
self.handlers.forEach(function (handler) {
|
self.handlers.forEach(function (handler) {
|
||||||
handler.call(self, formatJsonRpcMessage(request.responseText));
|
handler.call(self, formatJsonRpcMessage(request.responseText));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpRpcProvider.prototype.poll = function (payload, id) {
|
HttpRpcProvider.prototype.poll = function (payload, id) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.sendRequest(payload, function (request) {
|
this.sendRequest(payload, function (request) {
|
||||||
var parsed = JSON.parse(request.responseText);
|
var parsed = JSON.parse(request.responseText);
|
||||||
@ -119,13 +119,13 @@ HttpRpcProvider.prototype.poll = function (payload, id) {
|
|||||||
handler.call(self, {_event: payload.call, _id: id, data: parsed.result});
|
handler.call(self, {_event: payload.call, _id: id, data: parsed.result});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", {
|
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", {
|
||||||
set: function (handler) {
|
set: function (handler) {
|
||||||
this.handlers.push(handler);
|
this.handlers.push(handler);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = HttpRpcProvider;
|
module.exports = HttpRpcProvider;
|
||||||
|
|
||||||
@ -155,11 +155,11 @@ along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function isPromise(o) {
|
function isPromise(o) {
|
||||||
return o instanceof Promise;
|
return o instanceof Promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
function flattenPromise (obj) {
|
function flattenPromise (obj) {
|
||||||
if (obj instanceof Promise) {
|
if (obj instanceof Promise) {
|
||||||
return Promise.resolve(obj);
|
return Promise.resolve(obj);
|
||||||
}
|
}
|
||||||
@ -196,9 +196,9 @@ function flattenPromise (obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve(obj);
|
return Promise.resolve(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
var ethMethods = function () {
|
var ethMethods = function () {
|
||||||
var blockCall = function (args) {
|
var blockCall = function (args) {
|
||||||
return typeof args[0] === "string" ? "blockByHash" : "blockByNumber";
|
return typeof args[0] === "string" ? "blockByHash" : "blockByNumber";
|
||||||
};
|
};
|
||||||
@ -224,9 +224,9 @@ var ethMethods = function () {
|
|||||||
{ name: 'compile', call: 'compile' }
|
{ name: 'compile', call: 'compile' }
|
||||||
];
|
];
|
||||||
return methods;
|
return methods;
|
||||||
};
|
};
|
||||||
|
|
||||||
var ethProperties = function () {
|
var ethProperties = function () {
|
||||||
return [
|
return [
|
||||||
{ name: 'coinbase', getter: 'coinbase', setter: 'setCoinbase' },
|
{ name: 'coinbase', getter: 'coinbase', setter: 'setCoinbase' },
|
||||||
{ name: 'listening', getter: 'listening', setter: 'setListening' },
|
{ name: 'listening', getter: 'listening', setter: 'setListening' },
|
||||||
@ -238,18 +238,18 @@ var ethProperties = function () {
|
|||||||
{ name: 'defaultBlock', getter: 'defaultBlock', setter: 'setDefaultBlock' },
|
{ name: 'defaultBlock', getter: 'defaultBlock', setter: 'setDefaultBlock' },
|
||||||
{ name: 'number', getter: 'number'}
|
{ name: 'number', getter: 'number'}
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var dbMethods = function () {
|
var dbMethods = function () {
|
||||||
return [
|
return [
|
||||||
{ name: 'put', call: 'put' },
|
{ name: 'put', call: 'put' },
|
||||||
{ name: 'get', call: 'get' },
|
{ name: 'get', call: 'get' },
|
||||||
{ name: 'putString', call: 'putString' },
|
{ name: 'putString', call: 'putString' },
|
||||||
{ name: 'getString', call: 'getString' }
|
{ name: 'getString', call: 'getString' }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var shhMethods = function () {
|
var shhMethods = function () {
|
||||||
return [
|
return [
|
||||||
{ name: 'post', call: 'post' },
|
{ name: 'post', call: 'post' },
|
||||||
{ name: 'newIdentity', call: 'newIdentity' },
|
{ name: 'newIdentity', call: 'newIdentity' },
|
||||||
@ -257,9 +257,9 @@ var shhMethods = function () {
|
|||||||
{ name: 'newGroup', call: 'newGroup' },
|
{ name: 'newGroup', call: 'newGroup' },
|
||||||
{ name: 'addToGroup', call: 'addToGroup' }
|
{ name: 'addToGroup', call: 'addToGroup' }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var ethWatchMethods = function () {
|
var ethWatchMethods = function () {
|
||||||
var newFilter = function (args) {
|
var newFilter = function (args) {
|
||||||
return typeof args[0] === 'string' ? 'newFilterString' : 'newFilter';
|
return typeof args[0] === 'string' ? 'newFilterString' : 'newFilter';
|
||||||
};
|
};
|
||||||
@ -269,17 +269,17 @@ var ethWatchMethods = function () {
|
|||||||
{ name: 'uninstallFilter', call: 'uninstallFilter' },
|
{ name: 'uninstallFilter', call: 'uninstallFilter' },
|
||||||
{ name: 'getMessages', call: 'getMessages' }
|
{ name: 'getMessages', call: 'getMessages' }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var shhWatchMethods = function () {
|
var shhWatchMethods = function () {
|
||||||
return [
|
return [
|
||||||
{ name: 'newFilter', call: 'shhNewFilter' },
|
{ name: 'newFilter', call: 'shhNewFilter' },
|
||||||
{ name: 'uninstallFilter', call: 'shhUninstallFilter' },
|
{ name: 'uninstallFilter', call: 'shhUninstallFilter' },
|
||||||
{ name: 'getMessage', call: 'shhGetMessages' }
|
{ name: 'getMessage', call: 'shhGetMessages' }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var setupMethods = function (obj, methods) {
|
var setupMethods = function (obj, methods) {
|
||||||
methods.forEach(function (method) {
|
methods.forEach(function (method) {
|
||||||
obj[method.name] = function () {
|
obj[method.name] = function () {
|
||||||
return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) {
|
return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) {
|
||||||
@ -300,9 +300,9 @@ var setupMethods = function (obj, methods) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var setupProperties = function (obj, properties) {
|
var setupProperties = function (obj, properties) {
|
||||||
properties.forEach(function (property) {
|
properties.forEach(function (property) {
|
||||||
var proto = {};
|
var proto = {};
|
||||||
proto.get = function () {
|
proto.get = function () {
|
||||||
@ -331,9 +331,9 @@ var setupProperties = function (obj, properties) {
|
|||||||
}
|
}
|
||||||
Object.defineProperty(obj, property.name, proto);
|
Object.defineProperty(obj, property.name, proto);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var web3 = {
|
var web3 = {
|
||||||
_callbacks: {},
|
_callbacks: {},
|
||||||
_events: {},
|
_events: {},
|
||||||
providers: {},
|
providers: {},
|
||||||
@ -418,24 +418,24 @@ var web3 = {
|
|||||||
var cb = callbacks[id];
|
var cb = callbacks[id];
|
||||||
cb(data);
|
cb(data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var eth = web3.eth;
|
var eth = web3.eth;
|
||||||
setupMethods(eth, ethMethods());
|
setupMethods(eth, ethMethods());
|
||||||
setupProperties(eth, ethProperties());
|
setupProperties(eth, ethProperties());
|
||||||
setupMethods(web3.db, dbMethods());
|
setupMethods(web3.db, dbMethods());
|
||||||
setupMethods(web3.shh, shhMethods());
|
setupMethods(web3.shh, shhMethods());
|
||||||
|
|
||||||
var ethWatch = {
|
var ethWatch = {
|
||||||
changed: 'changed'
|
changed: 'changed'
|
||||||
};
|
};
|
||||||
setupMethods(ethWatch, ethWatchMethods());
|
setupMethods(ethWatch, ethWatchMethods());
|
||||||
var shhWatch = {
|
var shhWatch = {
|
||||||
changed: 'shhChanged'
|
changed: 'shhChanged'
|
||||||
};
|
};
|
||||||
setupMethods(shhWatch, shhWatchMethods());
|
setupMethods(shhWatch, shhWatchMethods());
|
||||||
|
|
||||||
var ProviderManager = function() {
|
var ProviderManager = function() {
|
||||||
this.queued = [];
|
this.queued = [];
|
||||||
this.polls = [];
|
this.polls = [];
|
||||||
this.ready = false;
|
this.ready = false;
|
||||||
@ -454,9 +454,9 @@ var ProviderManager = function() {
|
|||||||
setTimeout(poll, 12000);
|
setTimeout(poll, 12000);
|
||||||
};
|
};
|
||||||
poll();
|
poll();
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.send = function(data, cb) {
|
ProviderManager.prototype.send = function(data, cb) {
|
||||||
data._id = this.id;
|
data._id = this.id;
|
||||||
if (cb) {
|
if (cb) {
|
||||||
web3._callbacks[data._id] = cb;
|
web3._callbacks[data._id] = cb;
|
||||||
@ -471,53 +471,53 @@ ProviderManager.prototype.send = function(data, cb) {
|
|||||||
console.warn("provider is not set");
|
console.warn("provider is not set");
|
||||||
this.queued.push(data);
|
this.queued.push(data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.set = function(provider) {
|
ProviderManager.prototype.set = function(provider) {
|
||||||
if(this.provider !== undefined && this.provider.unload !== undefined) {
|
if(this.provider !== undefined && this.provider.unload !== undefined) {
|
||||||
this.provider.unload();
|
this.provider.unload();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.ready = true;
|
this.ready = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.sendQueued = function() {
|
ProviderManager.prototype.sendQueued = function() {
|
||||||
for(var i = 0; this.queued.length; i++) {
|
for(var i = 0; this.queued.length; i++) {
|
||||||
// Resend
|
// Resend
|
||||||
this.send(this.queued[i]);
|
this.send(this.queued[i]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.installed = function() {
|
ProviderManager.prototype.installed = function() {
|
||||||
return this.provider !== undefined;
|
return this.provider !== undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.startPolling = function (data, pollId) {
|
ProviderManager.prototype.startPolling = function (data, pollId) {
|
||||||
if (!this.provider || !this.provider.poll) {
|
if (!this.provider || !this.provider.poll) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.polls.push({data: data, id: pollId});
|
this.polls.push({data: data, id: pollId});
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.stopPolling = function (pollId) {
|
ProviderManager.prototype.stopPolling = function (pollId) {
|
||||||
for (var i = this.polls.length; i--;) {
|
for (var i = this.polls.length; i--;) {
|
||||||
var poll = this.polls[i];
|
var poll = this.polls[i];
|
||||||
if (poll.id === pollId) {
|
if (poll.id === pollId) {
|
||||||
this.polls.splice(i, 1);
|
this.polls.splice(i, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
web3.provider = new ProviderManager();
|
web3.provider = new ProviderManager();
|
||||||
|
|
||||||
web3.setProvider = function(provider) {
|
web3.setProvider = function(provider) {
|
||||||
provider.onmessage = messageHandler;
|
provider.onmessage = messageHandler;
|
||||||
web3.provider.set(provider);
|
web3.provider.set(provider);
|
||||||
web3.provider.sendQueued();
|
web3.provider.sendQueued();
|
||||||
};
|
};
|
||||||
|
|
||||||
var Filter = function(options, impl) {
|
var Filter = function(options, impl) {
|
||||||
this.impl = impl;
|
this.impl = impl;
|
||||||
this.callbacks = [];
|
this.callbacks = [];
|
||||||
|
|
||||||
@ -528,42 +528,42 @@ var Filter = function(options, impl) {
|
|||||||
web3.on(impl.changed, id, self.trigger.bind(self));
|
web3.on(impl.changed, id, self.trigger.bind(self));
|
||||||
web3.provider.startPolling({call: impl.changed, args: [id]}, id);
|
web3.provider.startPolling({call: impl.changed, args: [id]}, id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.arrived = function(callback) {
|
Filter.prototype.arrived = function(callback) {
|
||||||
this.changed(callback);
|
this.changed(callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.changed = function(callback) {
|
Filter.prototype.changed = function(callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.promise.then(function(id) {
|
this.promise.then(function(id) {
|
||||||
self.callbacks.push(callback);
|
self.callbacks.push(callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.trigger = function(messages) {
|
Filter.prototype.trigger = function(messages) {
|
||||||
for(var i = 0; i < this.callbacks.length; i++) {
|
for(var i = 0; i < this.callbacks.length; i++) {
|
||||||
this.callbacks[i].call(this, messages);
|
this.callbacks[i].call(this, messages);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.uninstall = function() {
|
Filter.prototype.uninstall = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.promise.then(function (id) {
|
this.promise.then(function (id) {
|
||||||
self.impl.uninstallFilter(id);
|
self.impl.uninstallFilter(id);
|
||||||
web3.provider.stopPolling(id);
|
web3.provider.stopPolling(id);
|
||||||
web3.off(impl.changed, id);
|
web3.off(impl.changed, id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.messages = function() {
|
Filter.prototype.messages = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
return this.promise.then(function (id) {
|
return this.promise.then(function (id) {
|
||||||
return self.impl.getMessages(id);
|
return self.impl.getMessages(id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function messageHandler(data) {
|
function messageHandler(data) {
|
||||||
if(data._event !== undefined) {
|
if(data._event !== undefined) {
|
||||||
web3.trigger(data._event, data._id, data.data);
|
web3.trigger(data._event, data._id, data.data);
|
||||||
return;
|
return;
|
||||||
@ -576,16 +576,8 @@ function messageHandler(data) {
|
|||||||
delete web3._callbacks[data._id];
|
delete web3._callbacks[data._id];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// Install default provider
|
|
||||||
if(!web3.provider.installed()) {
|
|
||||||
var sock = new web3.providers.WebSocketProvider("ws://localhost:40404/eth");
|
|
||||||
|
|
||||||
web3.setProvider(sock);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = web3;
|
module.exports = web3;
|
||||||
|
|
||||||
@ -612,7 +604,7 @@ module.exports = web3;
|
|||||||
* @date 2014
|
* @date 2014
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var QtProvider = function() {
|
var QtProvider = function() {
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -621,17 +613,17 @@ var QtProvider = function() {
|
|||||||
handler.call(self, JSON.parse(message.data));
|
handler.call(self, JSON.parse(message.data));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
QtProvider.prototype.send = function(payload) {
|
QtProvider.prototype.send = function(payload) {
|
||||||
navigator.qt.postMessage(JSON.stringify(payload));
|
navigator.qt.postMessage(JSON.stringify(payload));
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(QtProvider.prototype, "onmessage", {
|
Object.defineProperty(QtProvider.prototype, "onmessage", {
|
||||||
set: function(handler) {
|
set: function(handler) {
|
||||||
this.handlers.push(handler);
|
this.handlers.push(handler);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = QtProvider;
|
module.exports = QtProvider;
|
||||||
|
|
||||||
@ -662,7 +654,7 @@ module.exports = QtProvider;
|
|||||||
var WebSocket = require('ws'); // jshint ignore:line
|
var WebSocket = require('ws'); // jshint ignore:line
|
||||||
|
|
||||||
|
|
||||||
var WebSocketProvider = function(host) {
|
var WebSocketProvider = function(host) {
|
||||||
// onmessage handlers
|
// onmessage handlers
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
// queue will be filled with messages if send is invoked before the ws is ready
|
// queue will be filled with messages if send is invoked before the ws is ready
|
||||||
@ -686,8 +678,8 @@ var WebSocketProvider = function(host) {
|
|||||||
self.send(self.queued[i]);
|
self.send(self.queued[i]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
WebSocketProvider.prototype.send = function(payload) {
|
WebSocketProvider.prototype.send = function(payload) {
|
||||||
if(this.ready) {
|
if(this.ready) {
|
||||||
var data = JSON.stringify(payload);
|
var data = JSON.stringify(payload);
|
||||||
|
|
||||||
@ -695,18 +687,18 @@ WebSocketProvider.prototype.send = function(payload) {
|
|||||||
} else {
|
} else {
|
||||||
this.queued.push(payload);
|
this.queued.push(payload);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocketProvider.prototype.onMessage = function(handler) {
|
WebSocketProvider.prototype.onMessage = function(handler) {
|
||||||
this.handlers.push(handler);
|
this.handlers.push(handler);
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocketProvider.prototype.unload = function() {
|
WebSocketProvider.prototype.unload = function() {
|
||||||
this.ws.close();
|
this.ws.close();
|
||||||
};
|
};
|
||||||
Object.defineProperty(WebSocketProvider.prototype, "onmessage", {
|
Object.defineProperty(WebSocketProvider.prototype, "onmessage", {
|
||||||
set: function(provider) { this.onMessage(provider); }
|
set: function(provider) { this.onMessage(provider); }
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = WebSocketProvider;
|
module.exports = WebSocketProvider;
|
||||||
|
|
||||||
|
@ -25,30 +25,30 @@
|
|||||||
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
|
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
|
||||||
|
|
||||||
|
|
||||||
var HttpRpcProvider = function (host) {
|
var HttpRpcProvider = function (host) {
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
this.host = host;
|
this.host = host;
|
||||||
};
|
};
|
||||||
|
|
||||||
function formatJsonRpcObject(object) {
|
function formatJsonRpcObject(object) {
|
||||||
return {
|
return {
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
method: object.call,
|
method: object.call,
|
||||||
params: object.args,
|
params: object.args,
|
||||||
id: object._id
|
id: object._id
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatJsonRpcMessage(message) {
|
function formatJsonRpcMessage(message) {
|
||||||
var object = JSON.parse(message);
|
var object = JSON.parse(message);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
_id: object.id,
|
_id: object.id,
|
||||||
data: object.result
|
data: object.result
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
|
HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
|
||||||
var data = formatJsonRpcObject(payload);
|
var data = formatJsonRpcObject(payload);
|
||||||
|
|
||||||
var request = new XMLHttpRequest();
|
var request = new XMLHttpRequest();
|
||||||
@ -59,18 +59,18 @@ HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
|
|||||||
cb(request);
|
cb(request);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpRpcProvider.prototype.send = function (payload) {
|
HttpRpcProvider.prototype.send = function (payload) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.sendRequest(payload, function (request) {
|
this.sendRequest(payload, function (request) {
|
||||||
self.handlers.forEach(function (handler) {
|
self.handlers.forEach(function (handler) {
|
||||||
handler.call(self, formatJsonRpcMessage(request.responseText));
|
handler.call(self, formatJsonRpcMessage(request.responseText));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpRpcProvider.prototype.poll = function (payload, id) {
|
HttpRpcProvider.prototype.poll = function (payload, id) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.sendRequest(payload, function (request) {
|
this.sendRequest(payload, function (request) {
|
||||||
var parsed = JSON.parse(request.responseText);
|
var parsed = JSON.parse(request.responseText);
|
||||||
@ -81,12 +81,12 @@ HttpRpcProvider.prototype.poll = function (payload, id) {
|
|||||||
handler.call(self, {_event: payload.call, _id: id, data: parsed.result});
|
handler.call(self, {_event: payload.call, _id: id, data: parsed.result});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", {
|
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", {
|
||||||
set: function (handler) {
|
set: function (handler) {
|
||||||
this.handlers.push(handler);
|
this.handlers.push(handler);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = HttpRpcProvider;
|
module.exports = HttpRpcProvider;
|
||||||
|
136
lib/main.js
136
lib/main.js
@ -23,11 +23,11 @@ along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function isPromise(o) {
|
function isPromise(o) {
|
||||||
return o instanceof Promise;
|
return o instanceof Promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
function flattenPromise (obj) {
|
function flattenPromise (obj) {
|
||||||
if (obj instanceof Promise) {
|
if (obj instanceof Promise) {
|
||||||
return Promise.resolve(obj);
|
return Promise.resolve(obj);
|
||||||
}
|
}
|
||||||
@ -64,9 +64,9 @@ function flattenPromise (obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve(obj);
|
return Promise.resolve(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
var ethMethods = function () {
|
var ethMethods = function () {
|
||||||
var blockCall = function (args) {
|
var blockCall = function (args) {
|
||||||
return typeof args[0] === "string" ? "blockByHash" : "blockByNumber";
|
return typeof args[0] === "string" ? "blockByHash" : "blockByNumber";
|
||||||
};
|
};
|
||||||
@ -92,9 +92,9 @@ var ethMethods = function () {
|
|||||||
{ name: 'compile', call: 'compile' }
|
{ name: 'compile', call: 'compile' }
|
||||||
];
|
];
|
||||||
return methods;
|
return methods;
|
||||||
};
|
};
|
||||||
|
|
||||||
var ethProperties = function () {
|
var ethProperties = function () {
|
||||||
return [
|
return [
|
||||||
{ name: 'coinbase', getter: 'coinbase', setter: 'setCoinbase' },
|
{ name: 'coinbase', getter: 'coinbase', setter: 'setCoinbase' },
|
||||||
{ name: 'listening', getter: 'listening', setter: 'setListening' },
|
{ name: 'listening', getter: 'listening', setter: 'setListening' },
|
||||||
@ -106,18 +106,18 @@ var ethProperties = function () {
|
|||||||
{ name: 'defaultBlock', getter: 'defaultBlock', setter: 'setDefaultBlock' },
|
{ name: 'defaultBlock', getter: 'defaultBlock', setter: 'setDefaultBlock' },
|
||||||
{ name: 'number', getter: 'number'}
|
{ name: 'number', getter: 'number'}
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var dbMethods = function () {
|
var dbMethods = function () {
|
||||||
return [
|
return [
|
||||||
{ name: 'put', call: 'put' },
|
{ name: 'put', call: 'put' },
|
||||||
{ name: 'get', call: 'get' },
|
{ name: 'get', call: 'get' },
|
||||||
{ name: 'putString', call: 'putString' },
|
{ name: 'putString', call: 'putString' },
|
||||||
{ name: 'getString', call: 'getString' }
|
{ name: 'getString', call: 'getString' }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var shhMethods = function () {
|
var shhMethods = function () {
|
||||||
return [
|
return [
|
||||||
{ name: 'post', call: 'post' },
|
{ name: 'post', call: 'post' },
|
||||||
{ name: 'newIdentity', call: 'newIdentity' },
|
{ name: 'newIdentity', call: 'newIdentity' },
|
||||||
@ -125,9 +125,9 @@ var shhMethods = function () {
|
|||||||
{ name: 'newGroup', call: 'newGroup' },
|
{ name: 'newGroup', call: 'newGroup' },
|
||||||
{ name: 'addToGroup', call: 'addToGroup' }
|
{ name: 'addToGroup', call: 'addToGroup' }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var ethWatchMethods = function () {
|
var ethWatchMethods = function () {
|
||||||
var newFilter = function (args) {
|
var newFilter = function (args) {
|
||||||
return typeof args[0] === 'string' ? 'newFilterString' : 'newFilter';
|
return typeof args[0] === 'string' ? 'newFilterString' : 'newFilter';
|
||||||
};
|
};
|
||||||
@ -137,17 +137,17 @@ var ethWatchMethods = function () {
|
|||||||
{ name: 'uninstallFilter', call: 'uninstallFilter' },
|
{ name: 'uninstallFilter', call: 'uninstallFilter' },
|
||||||
{ name: 'getMessages', call: 'getMessages' }
|
{ name: 'getMessages', call: 'getMessages' }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var shhWatchMethods = function () {
|
var shhWatchMethods = function () {
|
||||||
return [
|
return [
|
||||||
{ name: 'newFilter', call: 'shhNewFilter' },
|
{ name: 'newFilter', call: 'shhNewFilter' },
|
||||||
{ name: 'uninstallFilter', call: 'shhUninstallFilter' },
|
{ name: 'uninstallFilter', call: 'shhUninstallFilter' },
|
||||||
{ name: 'getMessage', call: 'shhGetMessages' }
|
{ name: 'getMessage', call: 'shhGetMessages' }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var setupMethods = function (obj, methods) {
|
var setupMethods = function (obj, methods) {
|
||||||
methods.forEach(function (method) {
|
methods.forEach(function (method) {
|
||||||
obj[method.name] = function () {
|
obj[method.name] = function () {
|
||||||
return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) {
|
return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) {
|
||||||
@ -168,9 +168,9 @@ var setupMethods = function (obj, methods) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var setupProperties = function (obj, properties) {
|
var setupProperties = function (obj, properties) {
|
||||||
properties.forEach(function (property) {
|
properties.forEach(function (property) {
|
||||||
var proto = {};
|
var proto = {};
|
||||||
proto.get = function () {
|
proto.get = function () {
|
||||||
@ -199,9 +199,9 @@ var setupProperties = function (obj, properties) {
|
|||||||
}
|
}
|
||||||
Object.defineProperty(obj, property.name, proto);
|
Object.defineProperty(obj, property.name, proto);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var web3 = {
|
var web3 = {
|
||||||
_callbacks: {},
|
_callbacks: {},
|
||||||
_events: {},
|
_events: {},
|
||||||
providers: {},
|
providers: {},
|
||||||
@ -286,24 +286,24 @@ var web3 = {
|
|||||||
var cb = callbacks[id];
|
var cb = callbacks[id];
|
||||||
cb(data);
|
cb(data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var eth = web3.eth;
|
var eth = web3.eth;
|
||||||
setupMethods(eth, ethMethods());
|
setupMethods(eth, ethMethods());
|
||||||
setupProperties(eth, ethProperties());
|
setupProperties(eth, ethProperties());
|
||||||
setupMethods(web3.db, dbMethods());
|
setupMethods(web3.db, dbMethods());
|
||||||
setupMethods(web3.shh, shhMethods());
|
setupMethods(web3.shh, shhMethods());
|
||||||
|
|
||||||
var ethWatch = {
|
var ethWatch = {
|
||||||
changed: 'changed'
|
changed: 'changed'
|
||||||
};
|
};
|
||||||
setupMethods(ethWatch, ethWatchMethods());
|
setupMethods(ethWatch, ethWatchMethods());
|
||||||
var shhWatch = {
|
var shhWatch = {
|
||||||
changed: 'shhChanged'
|
changed: 'shhChanged'
|
||||||
};
|
};
|
||||||
setupMethods(shhWatch, shhWatchMethods());
|
setupMethods(shhWatch, shhWatchMethods());
|
||||||
|
|
||||||
var ProviderManager = function() {
|
var ProviderManager = function() {
|
||||||
this.queued = [];
|
this.queued = [];
|
||||||
this.polls = [];
|
this.polls = [];
|
||||||
this.ready = false;
|
this.ready = false;
|
||||||
@ -322,9 +322,9 @@ var ProviderManager = function() {
|
|||||||
setTimeout(poll, 12000);
|
setTimeout(poll, 12000);
|
||||||
};
|
};
|
||||||
poll();
|
poll();
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.send = function(data, cb) {
|
ProviderManager.prototype.send = function(data, cb) {
|
||||||
data._id = this.id;
|
data._id = this.id;
|
||||||
if (cb) {
|
if (cb) {
|
||||||
web3._callbacks[data._id] = cb;
|
web3._callbacks[data._id] = cb;
|
||||||
@ -339,53 +339,53 @@ ProviderManager.prototype.send = function(data, cb) {
|
|||||||
console.warn("provider is not set");
|
console.warn("provider is not set");
|
||||||
this.queued.push(data);
|
this.queued.push(data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.set = function(provider) {
|
ProviderManager.prototype.set = function(provider) {
|
||||||
if(this.provider !== undefined && this.provider.unload !== undefined) {
|
if(this.provider !== undefined && this.provider.unload !== undefined) {
|
||||||
this.provider.unload();
|
this.provider.unload();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.ready = true;
|
this.ready = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.sendQueued = function() {
|
ProviderManager.prototype.sendQueued = function() {
|
||||||
for(var i = 0; this.queued.length; i++) {
|
for(var i = 0; this.queued.length; i++) {
|
||||||
// Resend
|
// Resend
|
||||||
this.send(this.queued[i]);
|
this.send(this.queued[i]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.installed = function() {
|
ProviderManager.prototype.installed = function() {
|
||||||
return this.provider !== undefined;
|
return this.provider !== undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.startPolling = function (data, pollId) {
|
ProviderManager.prototype.startPolling = function (data, pollId) {
|
||||||
if (!this.provider || !this.provider.poll) {
|
if (!this.provider || !this.provider.poll) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.polls.push({data: data, id: pollId});
|
this.polls.push({data: data, id: pollId});
|
||||||
};
|
};
|
||||||
|
|
||||||
ProviderManager.prototype.stopPolling = function (pollId) {
|
ProviderManager.prototype.stopPolling = function (pollId) {
|
||||||
for (var i = this.polls.length; i--;) {
|
for (var i = this.polls.length; i--;) {
|
||||||
var poll = this.polls[i];
|
var poll = this.polls[i];
|
||||||
if (poll.id === pollId) {
|
if (poll.id === pollId) {
|
||||||
this.polls.splice(i, 1);
|
this.polls.splice(i, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
web3.provider = new ProviderManager();
|
web3.provider = new ProviderManager();
|
||||||
|
|
||||||
web3.setProvider = function(provider) {
|
web3.setProvider = function(provider) {
|
||||||
provider.onmessage = messageHandler;
|
provider.onmessage = messageHandler;
|
||||||
web3.provider.set(provider);
|
web3.provider.set(provider);
|
||||||
web3.provider.sendQueued();
|
web3.provider.sendQueued();
|
||||||
};
|
};
|
||||||
|
|
||||||
var Filter = function(options, impl) {
|
var Filter = function(options, impl) {
|
||||||
this.impl = impl;
|
this.impl = impl;
|
||||||
this.callbacks = [];
|
this.callbacks = [];
|
||||||
|
|
||||||
@ -396,42 +396,42 @@ var Filter = function(options, impl) {
|
|||||||
web3.on(impl.changed, id, self.trigger.bind(self));
|
web3.on(impl.changed, id, self.trigger.bind(self));
|
||||||
web3.provider.startPolling({call: impl.changed, args: [id]}, id);
|
web3.provider.startPolling({call: impl.changed, args: [id]}, id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.arrived = function(callback) {
|
Filter.prototype.arrived = function(callback) {
|
||||||
this.changed(callback);
|
this.changed(callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.changed = function(callback) {
|
Filter.prototype.changed = function(callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.promise.then(function(id) {
|
this.promise.then(function(id) {
|
||||||
self.callbacks.push(callback);
|
self.callbacks.push(callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.trigger = function(messages) {
|
Filter.prototype.trigger = function(messages) {
|
||||||
for(var i = 0; i < this.callbacks.length; i++) {
|
for(var i = 0; i < this.callbacks.length; i++) {
|
||||||
this.callbacks[i].call(this, messages);
|
this.callbacks[i].call(this, messages);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.uninstall = function() {
|
Filter.prototype.uninstall = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.promise.then(function (id) {
|
this.promise.then(function (id) {
|
||||||
self.impl.uninstallFilter(id);
|
self.impl.uninstallFilter(id);
|
||||||
web3.provider.stopPolling(id);
|
web3.provider.stopPolling(id);
|
||||||
web3.off(impl.changed, id);
|
web3.off(impl.changed, id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Filter.prototype.messages = function() {
|
Filter.prototype.messages = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
return this.promise.then(function (id) {
|
return this.promise.then(function (id) {
|
||||||
return self.impl.getMessages(id);
|
return self.impl.getMessages(id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function messageHandler(data) {
|
function messageHandler(data) {
|
||||||
if(data._event !== undefined) {
|
if(data._event !== undefined) {
|
||||||
web3.trigger(data._event, data._id, data.data);
|
web3.trigger(data._event, data._id, data.data);
|
||||||
return;
|
return;
|
||||||
@ -444,15 +444,7 @@ function messageHandler(data) {
|
|||||||
delete web3._callbacks[data._id];
|
delete web3._callbacks[data._id];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// Install default provider
|
|
||||||
if(!web3.provider.installed()) {
|
|
||||||
var sock = new web3.providers.WebSocketProvider("ws://localhost:40404/eth");
|
|
||||||
|
|
||||||
web3.setProvider(sock);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = web3;
|
module.exports = web3;
|
||||||
|
12
lib/qt.js
12
lib/qt.js
@ -20,7 +20,7 @@
|
|||||||
* @date 2014
|
* @date 2014
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var QtProvider = function() {
|
var QtProvider = function() {
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -29,16 +29,16 @@ var QtProvider = function() {
|
|||||||
handler.call(self, JSON.parse(message.data));
|
handler.call(self, JSON.parse(message.data));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
QtProvider.prototype.send = function(payload) {
|
QtProvider.prototype.send = function(payload) {
|
||||||
navigator.qt.postMessage(JSON.stringify(payload));
|
navigator.qt.postMessage(JSON.stringify(payload));
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(QtProvider.prototype, "onmessage", {
|
Object.defineProperty(QtProvider.prototype, "onmessage", {
|
||||||
set: function(handler) {
|
set: function(handler) {
|
||||||
this.handlers.push(handler);
|
this.handlers.push(handler);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = QtProvider;
|
module.exports = QtProvider;
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
var WebSocket = require('ws'); // jshint ignore:line
|
var WebSocket = require('ws'); // jshint ignore:line
|
||||||
|
|
||||||
|
|
||||||
var WebSocketProvider = function(host) {
|
var WebSocketProvider = function(host) {
|
||||||
// onmessage handlers
|
// onmessage handlers
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
// queue will be filled with messages if send is invoked before the ws is ready
|
// queue will be filled with messages if send is invoked before the ws is ready
|
||||||
@ -48,8 +48,8 @@ var WebSocketProvider = function(host) {
|
|||||||
self.send(self.queued[i]);
|
self.send(self.queued[i]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
WebSocketProvider.prototype.send = function(payload) {
|
WebSocketProvider.prototype.send = function(payload) {
|
||||||
if(this.ready) {
|
if(this.ready) {
|
||||||
var data = JSON.stringify(payload);
|
var data = JSON.stringify(payload);
|
||||||
|
|
||||||
@ -57,17 +57,17 @@ WebSocketProvider.prototype.send = function(payload) {
|
|||||||
} else {
|
} else {
|
||||||
this.queued.push(payload);
|
this.queued.push(payload);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocketProvider.prototype.onMessage = function(handler) {
|
WebSocketProvider.prototype.onMessage = function(handler) {
|
||||||
this.handlers.push(handler);
|
this.handlers.push(handler);
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocketProvider.prototype.unload = function() {
|
WebSocketProvider.prototype.unload = function() {
|
||||||
this.ws.close();
|
this.ws.close();
|
||||||
};
|
};
|
||||||
Object.defineProperty(WebSocketProvider.prototype, "onmessage", {
|
Object.defineProperty(WebSocketProvider.prototype, "onmessage", {
|
||||||
set: function(provider) { this.onMessage(provider); }
|
set: function(provider) { this.onMessage(provider); }
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = WebSocketProvider;
|
module.exports = WebSocketProvider;
|
||||||
|
Reference in New Issue
Block a user