From 5cd93a061889b0f2b9047ed6798f91aeb132dcea Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 22 Oct 2014 11:38:33 +0200 Subject: [PATCH 01/11] http provider --- http.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.js | 33 ++++++++++++++++++--------------- 2 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 http.js diff --git a/http.js b/http.js new file mode 100644 index 000000000..88869c689 --- /dev/null +++ b/http.js @@ -0,0 +1,52 @@ +(function () { + var HttpProvider = function (host) { + this.handlers = []; + this.host = host; + }; + + //TODO unify the format of object passed to 'send method' + function formatJsonRpcObject(object) { + return { + jsonrpc: '2.0', + method: object.call, + params: object.args, + id: object._id + } + }; + + //TODO unify the format of output messages, maybe there should be objects instead + function formatJsonRpcMessage(message) { + var object = JSON.parse(message); + + return JSON.stringify({ + _id: object.id, + data: object.result + }); + }; + + HttpProvider.prototype.send = function (payload) { + var data = formatJsonRpcObject(payload); + + var request = new XMLHttpRequest(); + request.open("POST", this.host, true); + request.send(JSON.stringify(data)); + var self = this; + request.onreadystatechange = function () { + if (request.readyState === 4) { + self.handlers.forEach(function (handler) { + handler.call(self, formatJsonRpcMessage(request.responseText)); + }); + } + } + }; + + Object.defineProperty(HttpProvider.prototype, "onmessage", { + set: function (handler) { + this.handlers.push(handler); + } + }); + + if (typeof(web3) !== "undefined" && web3.providers !== undefined) { + web3.providers.HttpProvider = HttpProvider; + } +})(); diff --git a/main.js b/main.js index 79449c703..708e9b0e6 100644 --- a/main.js +++ b/main.js @@ -49,37 +49,40 @@ eth: { prototype: Object(), - + //TODO solve the issue with numberOrHash impl block: function(numberOrHash) { return new Promise(function(resolve, reject) { - /* - var func; - if(typeof numberOrHash == "string") { - func = "getBlockByHash"; - } else { - func = "getBlockByNumber"; - } - */ - - web3.provider.send({call: /*func*/"block", args: [numberOrHash]}, function(block) { + var args = typeof numberOrHash === "string" ? [0, numberOrHash] : [numberOrHash, ""]; + web3.provider.send({call: "block", args: args}, function(block) { if(block) resolve(block); else reject("not found"); - }); }); }, transaction: function(numberOrHash, nth) { return new Promise(function(resolve, reject) { - reject("`transaction` not yet implemented") + var args = typeof numberOrHash === "string" ? [0, numberOrHash, nth] : [numberOrHash, "", nth]; + web3.provider.send({call: "transaction", args: args}, function(block) { + if(block) + resolve(block); + else + reject("not found"); + }); }); }, uncle: function(numberOrHash, nth) { return new Promise(function(resolve, reject) { - reject("`uncle` not yet implemented") + var args = typeof numberOrHash === "string" ? [0, numberOrHash, nth] : [numberOrHash, "", nth]; + web3.provider.send({call: "uncle", args: args}, function(block) { + if(block) + resolve(block); + else + reject("not found"); + }); }); }, @@ -128,7 +131,7 @@ return Promise.all(promises).then(function() { return new Promise(function(resolve, reject) { params.data = params.data.join(""); - web3.provider.send({call: "transact", args: ["0x"+params]}, function(data) { + web3.provider.send({call: "transact", args: [params]}, function(data) { if(data[1]) reject(data[0]); else From ea3048421fb54abd98f610d79294634015d0e58e Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 22 Oct 2014 15:58:24 +0200 Subject: [PATCH 02/11] block, transaction, uncle by hash and number separately --- http.js | 2 -- index.html | 2 ++ main.js | 13 ++++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/http.js b/http.js index 88869c689..4f6296d08 100644 --- a/http.js +++ b/http.js @@ -4,7 +4,6 @@ this.host = host; }; - //TODO unify the format of object passed to 'send method' function formatJsonRpcObject(object) { return { jsonrpc: '2.0', @@ -14,7 +13,6 @@ } }; - //TODO unify the format of output messages, maybe there should be objects instead function formatJsonRpcMessage(message) { var object = JSON.parse(message); diff --git a/index.html b/index.html index fc658c00a..6d27af1df 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,9 @@ + - +