Skip to content

Commit 8e0a39f

Browse files
committed
Updated to use ethereum.js
1 parent df5603d commit 8e0a39f

13 files changed

Lines changed: 742 additions & 76 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile ~/.gitignore_global
6+
7+
/tmp
8+
*/**/*un~
9+
*un~
10+
.DS_Store
11+
*/**/.DS_Store
12+
ethereum/ethereum
13+
ethereal/ethereal
14+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Ethereum JavaScript API
2+
3+
This is the Ethereum compatible JavaScript API using `Promise`s
4+
which implements the [Generic JSON RPC](https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC) spec.
5+
6+
For an example see `index.html`.
7+
8+
**Please note this repo is in it's early stage.**
9+
10+
If you'd like to run a WebSocket ethereum node check out
11+
[go-ethereum](https://github.com/ethereum/go-ethereum).
12+
13+
To install ethereum and spawn a node:
14+
15+
```
16+
go get github.com/ethereum/go-ethereum/ethereum
17+
ethereum -ws -loglevel=4
18+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(function () {
2+
var HttpRpcProvider = function (host) {
3+
this.handlers = [];
4+
this.host = host;
5+
};
6+
7+
function formatJsonRpcObject(object) {
8+
return {
9+
jsonrpc: '2.0',
10+
method: object.call,
11+
params: object.args,
12+
id: object._id
13+
}
14+
};
15+
16+
function formatJsonRpcMessage(message) {
17+
var object = JSON.parse(message);
18+
19+
return {
20+
_id: object.id,
21+
data: object.result
22+
};
23+
};
24+
25+
HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
26+
var data = formatJsonRpcObject(payload);
27+
28+
var request = new XMLHttpRequest();
29+
request.open("POST", this.host, true);
30+
request.send(JSON.stringify(data));
31+
request.onreadystatechange = function () {
32+
if (request.readyState === 4 && cb) {
33+
cb(request);
34+
}
35+
}
36+
};
37+
38+
HttpRpcProvider.prototype.send = function (payload) {
39+
var self = this;
40+
this.sendRequest(payload, function (request) {
41+
self.handlers.forEach(function (handler) {
42+
handler.call(self, formatJsonRpcMessage(request.responseText));
43+
});
44+
});
45+
};
46+
47+
HttpRpcProvider.prototype.poll = function (payload, id) {
48+
var self = this;
49+
this.sendRequest(payload, function (request) {
50+
var parsed = JSON.parse(request.responseText);
51+
if (parsed.result instanceof Array ? parsed.result.length === 0 : !parsed.result) {
52+
return;
53+
}
54+
self.handlers.forEach(function (handler) {
55+
handler.call(self, {_event: payload.call, _id: id, data: parsed.result});
56+
});
57+
});
58+
};
59+
60+
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", {
61+
set: function (handler) {
62+
this.handlers.push(handler);
63+
}
64+
});
65+
66+
if (typeof(web3) !== "undefined" && web3.providers !== undefined) {
67+
web3.providers.HttpRpcProvider = HttpRpcProvider;
68+
}
69+
})();
70+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!doctype>
2+
<html>
3+
4+
<head>
5+
<script type="text/javascript" src="main.js"></script>
6+
<script type="text/javascript" src="websocket.js"></script>
7+
<script type="text/javascript" src="qt.js"></script>
8+
<script type="text/javascript" src="httprpc.js"></script>
9+
<script type="text/javascript">
10+
function registerName() {
11+
var name = document.querySelector("#name").value;
12+
name = web3.fromAscii(name);
13+
14+
var eth = web3.eth;
15+
eth.transact({to: "NameReg", gas: "10000", gasPrice: eth.gasPrice, data: [web3.fromAscii("register"), name]}).then(function(tx) {
16+
document.querySelector("#result").innerHTML = "Registered name. Please wait for the next block to come through.";
17+
}, function(err) {
18+
console.log(err);
19+
});
20+
}
21+
</script>
22+
</head>
23+
24+
<body>
25+
26+
<h1>std::name_reg</h1>
27+
<input type="text" id="name"></input>
28+
<input type="submit" onClick="registerName();"></input>
29+
<div id="result"></div>
30+
31+
</body>
32+
33+
</html>

0 commit comments

Comments
 (0)