|
|
|
@ -63,30 +63,30 @@ if (XMLHttpRequest) xhr.prototype = XMLHttpRequest.prototype; |
|
|
|
|
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var HttpRpcProvider = function (host) { |
|
|
|
|
var HttpRpcProvider = function (host) { |
|
|
|
|
this.handlers = []; |
|
|
|
|
this.host = host; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
function formatJsonRpcObject(object) { |
|
|
|
|
function formatJsonRpcObject(object) { |
|
|
|
|
return { |
|
|
|
|
jsonrpc: '2.0', |
|
|
|
|
method: object.call, |
|
|
|
|
params: object.args, |
|
|
|
|
id: object._id |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function formatJsonRpcMessage(message) { |
|
|
|
|
function formatJsonRpcMessage(message) { |
|
|
|
|
var object = JSON.parse(message); |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
_id: object.id, |
|
|
|
|
data: object.result |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
HttpRpcProvider.prototype.sendRequest = function (payload, cb) { |
|
|
|
|
HttpRpcProvider.prototype.sendRequest = function (payload, cb) { |
|
|
|
|
var data = formatJsonRpcObject(payload); |
|
|
|
|
|
|
|
|
|
var request = new XMLHttpRequest(); |
|
|
|
@ -97,18 +97,18 @@ HttpRpcProvider.prototype.sendRequest = function (payload, cb) { |
|
|
|
|
cb(request); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
HttpRpcProvider.prototype.send = function (payload) { |
|
|
|
|
HttpRpcProvider.prototype.send = function (payload) { |
|
|
|
|
var self = this; |
|
|
|
|
this.sendRequest(payload, function (request) { |
|
|
|
|
self.handlers.forEach(function (handler) { |
|
|
|
|
handler.call(self, formatJsonRpcMessage(request.responseText)); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
HttpRpcProvider.prototype.poll = function (payload, id) { |
|
|
|
|
HttpRpcProvider.prototype.poll = function (payload, id) { |
|
|
|
|
var self = this; |
|
|
|
|
this.sendRequest(payload, function (request) { |
|
|
|
|
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}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", { |
|
|
|
|
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", { |
|
|
|
|
set: function (handler) { |
|
|
|
|
this.handlers.push(handler); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function flattenPromise (obj) { |
|
|
|
|
function flattenPromise (obj) { |
|
|
|
|
if (obj instanceof Promise) { |
|
|
|
|
return Promise.resolve(obj); |
|
|
|
|
} |
|
|
|
@ -196,9 +196,9 @@ function flattenPromise (obj) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Promise.resolve(obj); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var ethMethods = function () { |
|
|
|
|
var ethMethods = function () { |
|
|
|
|
var blockCall = function (args) { |
|
|
|
|
return typeof args[0] === "string" ? "blockByHash" : "blockByNumber"; |
|
|
|
|
}; |
|
|
|
@ -224,9 +224,9 @@ var ethMethods = function () { |
|
|
|
|
{ name: 'compile', call: 'compile' } |
|
|
|
|
]; |
|
|
|
|
return methods; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var ethProperties = function () { |
|
|
|
|
var ethProperties = function () { |
|
|
|
|
return [ |
|
|
|
|
{ name: 'coinbase', getter: 'coinbase', setter: 'setCoinbase' }, |
|
|
|
|
{ name: 'listening', getter: 'listening', setter: 'setListening' }, |
|
|
|
@ -238,18 +238,18 @@ var ethProperties = function () { |
|
|
|
|
{ name: 'defaultBlock', getter: 'defaultBlock', setter: 'setDefaultBlock' }, |
|
|
|
|
{ name: 'number', getter: 'number'} |
|
|
|
|
]; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var dbMethods = function () { |
|
|
|
|
var dbMethods = function () { |
|
|
|
|
return [ |
|
|
|
|
{ name: 'put', call: 'put' }, |
|
|
|
|
{ name: 'get', call: 'get' }, |
|
|
|
|
{ name: 'putString', call: 'putString' }, |
|
|
|
|
{ name: 'getString', call: 'getString' } |
|
|
|
|
]; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var shhMethods = function () { |
|
|
|
|
var shhMethods = function () { |
|
|
|
|
return [ |
|
|
|
|
{ name: 'post', call: 'post' }, |
|
|
|
|
{ name: 'newIdentity', call: 'newIdentity' }, |
|
|
|
@ -257,9 +257,9 @@ var shhMethods = function () { |
|
|
|
|
{ name: 'newGroup', call: 'newGroup' }, |
|
|
|
|
{ name: 'addToGroup', call: 'addToGroup' } |
|
|
|
|
]; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var ethWatchMethods = function () { |
|
|
|
|
var ethWatchMethods = function () { |
|
|
|
|
var newFilter = function (args) { |
|
|
|
|
return typeof args[0] === 'string' ? 'newFilterString' : 'newFilter'; |
|
|
|
|
}; |
|
|
|
@ -269,17 +269,17 @@ var ethWatchMethods = function () { |
|
|
|
|
{ name: 'uninstallFilter', call: 'uninstallFilter' }, |
|
|
|
|
{ name: 'getMessages', call: 'getMessages' } |
|
|
|
|
]; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var shhWatchMethods = function () { |
|
|
|
|
var shhWatchMethods = function () { |
|
|
|
|
return [ |
|
|
|
|
{ name: 'newFilter', call: 'shhNewFilter' }, |
|
|
|
|
{ name: 'uninstallFilter', call: 'shhUninstallFilter' }, |
|
|
|
|
{ name: 'getMessage', call: 'shhGetMessages' } |
|
|
|
|
]; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var setupMethods = function (obj, methods) { |
|
|
|
|
var setupMethods = function (obj, methods) { |
|
|
|
|
methods.forEach(function (method) { |
|
|
|
|
obj[method.name] = function () { |
|
|
|
|
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) { |
|
|
|
|
var proto = {}; |
|
|
|
|
proto.get = function () { |
|
|
|
@ -331,9 +331,9 @@ var setupProperties = function (obj, properties) { |
|
|
|
|
} |
|
|
|
|
Object.defineProperty(obj, property.name, proto); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var web3 = { |
|
|
|
|
var web3 = { |
|
|
|
|
_callbacks: {}, |
|
|
|
|
_events: {}, |
|
|
|
|
providers: {}, |
|
|
|
@ -418,24 +418,24 @@ var web3 = { |
|
|
|
|
var cb = callbacks[id]; |
|
|
|
|
cb(data); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var eth = web3.eth; |
|
|
|
|
setupMethods(eth, ethMethods()); |
|
|
|
|
setupProperties(eth, ethProperties()); |
|
|
|
|
setupMethods(web3.db, dbMethods()); |
|
|
|
|
setupMethods(web3.shh, shhMethods()); |
|
|
|
|
var eth = web3.eth; |
|
|
|
|
setupMethods(eth, ethMethods()); |
|
|
|
|
setupProperties(eth, ethProperties()); |
|
|
|
|
setupMethods(web3.db, dbMethods()); |
|
|
|
|
setupMethods(web3.shh, shhMethods()); |
|
|
|
|
|
|
|
|
|
var ethWatch = { |
|
|
|
|
var ethWatch = { |
|
|
|
|
changed: 'changed' |
|
|
|
|
}; |
|
|
|
|
setupMethods(ethWatch, ethWatchMethods()); |
|
|
|
|
var shhWatch = { |
|
|
|
|
}; |
|
|
|
|
setupMethods(ethWatch, ethWatchMethods()); |
|
|
|
|
var shhWatch = { |
|
|
|
|
changed: 'shhChanged' |
|
|
|
|
}; |
|
|
|
|
setupMethods(shhWatch, shhWatchMethods()); |
|
|
|
|
}; |
|
|
|
|
setupMethods(shhWatch, shhWatchMethods()); |
|
|
|
|
|
|
|
|
|
var ProviderManager = function() { |
|
|
|
|
var ProviderManager = function() { |
|
|
|
|
this.queued = []; |
|
|
|
|
this.polls = []; |
|
|
|
|
this.ready = false; |
|
|
|
@ -454,9 +454,9 @@ var ProviderManager = function() { |
|
|
|
|
setTimeout(poll, 12000); |
|
|
|
|
}; |
|
|
|
|
poll(); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
ProviderManager.prototype.send = function(data, cb) { |
|
|
|
|
ProviderManager.prototype.send = function(data, cb) { |
|
|
|
|
data._id = this.id; |
|
|
|
|
if (cb) { |
|
|
|
|
web3._callbacks[data._id] = cb; |
|
|
|
@ -471,53 +471,53 @@ ProviderManager.prototype.send = function(data, cb) { |
|
|
|
|
console.warn("provider is not set"); |
|
|
|
|
this.queued.push(data); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
ProviderManager.prototype.set = function(provider) { |
|
|
|
|
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() { |
|
|
|
|
ProviderManager.prototype.sendQueued = function() { |
|
|
|
|
for(var i = 0; this.queued.length; i++) { |
|
|
|
|
// Resend
|
|
|
|
|
this.send(this.queued[i]); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
ProviderManager.prototype.installed = function() { |
|
|
|
|
ProviderManager.prototype.installed = function() { |
|
|
|
|
return this.provider !== undefined; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
ProviderManager.prototype.startPolling = function (data, pollId) { |
|
|
|
|
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) { |
|
|
|
|
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(); |
|
|
|
|
web3.provider = new ProviderManager(); |
|
|
|
|
|
|
|
|
|
web3.setProvider = function(provider) { |
|
|
|
|
web3.setProvider = function(provider) { |
|
|
|
|
provider.onmessage = messageHandler; |
|
|
|
|
web3.provider.set(provider); |
|
|
|
|
web3.provider.sendQueued(); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var Filter = function(options, impl) { |
|
|
|
|
var Filter = function(options, impl) { |
|
|
|
|
this.impl = impl; |
|
|
|
|
this.callbacks = []; |
|
|
|
|
|
|
|
|
@ -528,42 +528,42 @@ var Filter = function(options, impl) { |
|
|
|
|
web3.on(impl.changed, id, self.trigger.bind(self)); |
|
|
|
|
web3.provider.startPolling({call: impl.changed, args: [id]}, id); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Filter.prototype.arrived = function(callback) { |
|
|
|
|
Filter.prototype.arrived = function(callback) { |
|
|
|
|
this.changed(callback); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Filter.prototype.changed = function(callback) { |
|
|
|
|
Filter.prototype.changed = function(callback) { |
|
|
|
|
var self = this; |
|
|
|
|
this.promise.then(function(id) { |
|
|
|
|
self.callbacks.push(callback); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Filter.prototype.trigger = function(messages) { |
|
|
|
|
Filter.prototype.trigger = function(messages) { |
|
|
|
|
for(var i = 0; i < this.callbacks.length; i++) { |
|
|
|
|
this.callbacks[i].call(this, messages); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Filter.prototype.uninstall = function() { |
|
|
|
|
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() { |
|
|
|
|
Filter.prototype.messages = function() { |
|
|
|
|
var self = this; |
|
|
|
|
return this.promise.then(function (id) { |
|
|
|
|
return self.impl.getMessages(id); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
function messageHandler(data) { |
|
|
|
|
function messageHandler(data) { |
|
|
|
|
if(data._event !== undefined) { |
|
|
|
|
web3.trigger(data._event, data._id, data.data); |
|
|
|
|
return; |
|
|
|
@ -576,16 +576,8 @@ function messageHandler(data) { |
|
|
|
|
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; |
|
|
|
|
|
|
|
|
@ -612,7 +604,7 @@ module.exports = web3; |
|
|
|
|
* @date 2014 |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
var QtProvider = function() { |
|
|
|
|
var QtProvider = function() { |
|
|
|
|
this.handlers = []; |
|
|
|
|
|
|
|
|
|
var self = this; |
|
|
|
@ -621,17 +613,17 @@ var QtProvider = function() { |
|
|
|
|
handler.call(self, JSON.parse(message.data)); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
QtProvider.prototype.send = function(payload) { |
|
|
|
|
QtProvider.prototype.send = function(payload) { |
|
|
|
|
navigator.qt.postMessage(JSON.stringify(payload)); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Object.defineProperty(QtProvider.prototype, "onmessage", { |
|
|
|
|
Object.defineProperty(QtProvider.prototype, "onmessage", { |
|
|
|
|
set: function(handler) { |
|
|
|
|
this.handlers.push(handler); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
module.exports = QtProvider; |
|
|
|
|
|
|
|
|
@ -662,7 +654,7 @@ module.exports = QtProvider; |
|
|
|
|
var WebSocket = require('ws'); // jshint ignore:line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var WebSocketProvider = function(host) { |
|
|
|
|
var WebSocketProvider = function(host) { |
|
|
|
|
// onmessage handlers
|
|
|
|
|
this.handlers = []; |
|
|
|
|
// 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]); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
WebSocketProvider.prototype.send = function(payload) { |
|
|
|
|
}; |
|
|
|
|
WebSocketProvider.prototype.send = function(payload) { |
|
|
|
|
if(this.ready) { |
|
|
|
|
var data = JSON.stringify(payload); |
|
|
|
|
|
|
|
|
@ -695,18 +687,18 @@ WebSocketProvider.prototype.send = function(payload) { |
|
|
|
|
} else { |
|
|
|
|
this.queued.push(payload); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
WebSocketProvider.prototype.onMessage = function(handler) { |
|
|
|
|
WebSocketProvider.prototype.onMessage = function(handler) { |
|
|
|
|
this.handlers.push(handler); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
WebSocketProvider.prototype.unload = function() { |
|
|
|
|
WebSocketProvider.prototype.unload = function() { |
|
|
|
|
this.ws.close(); |
|
|
|
|
}; |
|
|
|
|
Object.defineProperty(WebSocketProvider.prototype, "onmessage", { |
|
|
|
|
}; |
|
|
|
|
Object.defineProperty(WebSocketProvider.prototype, "onmessage", { |
|
|
|
|
set: function(provider) { this.onMessage(provider); } |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
module.exports = WebSocketProvider; |
|
|
|
|
|
|
|
|
|