2014-04-20 22:58:02 +00:00
|
|
|
// Main Ethereum library
|
|
|
|
window.eth = {
|
|
|
|
prototype: Object(),
|
|
|
|
|
2014-04-23 09:51:34 +00:00
|
|
|
// Retrieve block
|
|
|
|
//
|
|
|
|
// Either supply a number or a string. Type is determent for the lookup method
|
|
|
|
// string - Retrieves the block by looking up the hash
|
|
|
|
// number - Retrieves the block by looking up the block number
|
|
|
|
getBlock: function(numberOrHash, cb) {
|
|
|
|
var func;
|
|
|
|
if(typeof numberOrHash == "string") {
|
2014-04-24 12:43:00 +00:00
|
|
|
func = "getBlockByHash";
|
2014-04-23 09:51:34 +00:00
|
|
|
} else {
|
2014-04-24 12:43:00 +00:00
|
|
|
func = "getBlockByNumber";
|
2014-04-23 09:51:34 +00:00
|
|
|
}
|
2014-04-24 12:43:00 +00:00
|
|
|
postData({call: func, args: [numberOrHash]}, cb);
|
2014-04-23 09:51:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Create transaction
|
|
|
|
//
|
2014-05-02 11:35:12 +00:00
|
|
|
// Transact between two state objects
|
2014-04-29 23:44:12 +00:00
|
|
|
transact: function(sec, recipient, value, gas, gasPrice, data, cb) {
|
|
|
|
postData({call: "transact", args: [sec, recipient, value, gas, gasPrice, data]}, cb);
|
|
|
|
},
|
|
|
|
|
|
|
|
create: function(sec, value, gas, gasPrice, init, body, cb) {
|
|
|
|
postData({call: "create", args: [sec, value, gas, gasPrice, init, body]}, cb);
|
2014-04-23 09:51:34 +00:00
|
|
|
},
|
2014-04-23 22:01:22 +00:00
|
|
|
|
2014-04-30 12:54:59 +00:00
|
|
|
getStorageAt: function(address, storageAddress, cb) {
|
2014-04-24 12:43:00 +00:00
|
|
|
postData({call: "getStorage", args: [address, storageAddress]}, cb);
|
2014-04-23 22:01:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getKey: function(cb) {
|
2014-04-24 12:43:00 +00:00
|
|
|
postData({call: "getKey"}, cb);
|
|
|
|
},
|
|
|
|
|
2014-04-30 12:54:59 +00:00
|
|
|
getBalanceAt: function(address, cb) {
|
2014-04-29 23:44:12 +00:00
|
|
|
postData({call: "getBalance", args: [address]}, cb);
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: function(address, storageAddrOrCb, cb) {
|
2014-04-30 12:42:57 +00:00
|
|
|
var ev;
|
2014-04-29 23:44:12 +00:00
|
|
|
if(cb === undefined) {
|
|
|
|
cb = storageAddrOrCb;
|
|
|
|
storageAddrOrCb = "";
|
2014-04-30 12:42:57 +00:00
|
|
|
ev = "object:"+address;
|
2014-04-29 23:44:12 +00:00
|
|
|
} else {
|
2014-04-30 12:42:57 +00:00
|
|
|
ev = "storage:"+address+":"+storageAddrOrCb;
|
2014-04-29 23:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eth.on(ev, cb)
|
|
|
|
|
|
|
|
postData({call: "watch", args: [address, storageAddrOrCb]});
|
2014-04-25 23:47:04 +00:00
|
|
|
},
|
|
|
|
|
2014-04-29 23:44:12 +00:00
|
|
|
disconnect: function(address, storageAddrOrCb, cb) {
|
2014-04-30 12:42:57 +00:00
|
|
|
var ev;
|
2014-04-29 23:44:12 +00:00
|
|
|
if(cb === undefined) {
|
|
|
|
cb = storageAddrOrCb;
|
2014-04-30 12:42:57 +00:00
|
|
|
storageAddrOrCb = "";
|
|
|
|
ev = "object:"+address;
|
2014-04-29 23:44:12 +00:00
|
|
|
} else {
|
2014-04-30 12:42:57 +00:00
|
|
|
ev = "storage:"+address+":"+storageAddrOrCb;
|
2014-04-29 23:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eth.off(ev, cb)
|
|
|
|
|
|
|
|
postData({call: "disconnect", args: [address, storageAddrOrCb]});
|
|
|
|
},
|
2014-04-24 12:43:00 +00:00
|
|
|
|
2014-05-01 20:59:16 +00:00
|
|
|
set: function(props) {
|
|
|
|
postData({call: "set", args: props});
|
|
|
|
},
|
|
|
|
|
2014-04-24 12:43:00 +00:00
|
|
|
on: function(event, cb) {
|
|
|
|
if(eth._onCallbacks[event] === undefined) {
|
|
|
|
eth._onCallbacks[event] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
eth._onCallbacks[event].push(cb);
|
|
|
|
|
|
|
|
return this
|
|
|
|
},
|
2014-04-29 23:44:12 +00:00
|
|
|
|
2014-04-24 12:43:00 +00:00
|
|
|
off: function(event, cb) {
|
|
|
|
if(eth._onCallbacks[event] !== undefined) {
|
|
|
|
var callbacks = eth._onCallbacks[event];
|
|
|
|
for(var i = 0; i < callbacks.length; i++) {
|
|
|
|
if(callbacks[i] === cb) {
|
|
|
|
delete callbacks[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
|
|
|
trigger: function(event, data) {
|
|
|
|
var callbacks = eth._onCallbacks[event];
|
|
|
|
if(callbacks !== undefined) {
|
|
|
|
for(var i = 0; i < callbacks.length; i++) {
|
|
|
|
callbacks[i](data);
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 22:01:22 +00:00
|
|
|
},
|
2014-04-20 22:58:02 +00:00
|
|
|
}
|
|
|
|
window.eth._callbacks = {}
|
2014-04-24 12:43:00 +00:00
|
|
|
window.eth._onCallbacks = {}
|
2014-04-20 22:58:02 +00:00
|
|
|
|
|
|
|
function debug(/**/) {
|
|
|
|
var args = arguments;
|
|
|
|
var msg = ""
|
2014-04-23 09:51:34 +00:00
|
|
|
for(var i = 0; i < args.length; i++){
|
2014-05-02 10:08:52 +00:00
|
|
|
if(typeof args[i] === "object") {
|
2014-04-20 22:58:02 +00:00
|
|
|
msg += " " + JSON.stringify(args[i])
|
|
|
|
} else {
|
|
|
|
msg += args[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById("debug").innerHTML += "<br>" + msg
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:44:12 +00:00
|
|
|
// Helper function for generating pseudo callbacks and sending data to the QML part of the application
|
|
|
|
function postData(data, cb) {
|
|
|
|
data._seed = Math.floor(Math.random() * 1000000)
|
|
|
|
if(cb) {
|
|
|
|
eth._callbacks[data._seed] = cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(data.args === undefined) {
|
|
|
|
data.args = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
navigator.qt.postMessage(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
2014-04-20 22:58:02 +00:00
|
|
|
navigator.qt.onmessage = function(ev) {
|
|
|
|
var data = JSON.parse(ev.data)
|
|
|
|
|
2014-04-24 12:43:00 +00:00
|
|
|
if(data._event !== undefined) {
|
|
|
|
eth.trigger(data._event, data.data);
|
|
|
|
} else {
|
|
|
|
if(data._seed) {
|
|
|
|
var cb = eth._callbacks[data._seed];
|
|
|
|
if(cb) {
|
|
|
|
// Call the callback
|
|
|
|
cb(data.data);
|
|
|
|
// Remove the "trigger" callback
|
|
|
|
delete eth._callbacks[ev._seed];
|
|
|
|
}
|
2014-04-20 22:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 10:08:52 +00:00
|
|
|
|
|
|
|
window.eth._0 = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
|
|
String.prototype.pad = function(len) {
|
|
|
|
var bin = this.bin();
|
|
|
|
var l = bin.length;
|
|
|
|
if(l < 32) {
|
|
|
|
return eth._0.substr(0, 32 - bin.length) + bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
String.prototype.unpad = function() {
|
|
|
|
var i, l;
|
|
|
|
for(i = 0, l = this.length; i < l; i++) {
|
|
|
|
if(this[i] != "\0") {
|
|
|
|
return this.substr(i, this.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.substr(i, this.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
String.prototype.bin = function() {
|
|
|
|
if(this.substr(0, 2) == "0x") {
|
|
|
|
return this.hex2bin();
|
|
|
|
} else if(/^\d+$/.test(this)) {
|
|
|
|
return this.num2bin()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we'll return the "String" object instead of an actual string
|
|
|
|
return this.substr(0, this.length)
|
|
|
|
}
|
|
|
|
|
|
|
|
String.prototype.unbin = function() {
|
|
|
|
var i, l, o = '';
|
|
|
|
for(i = 0, l = this.length; i < l; i++) {
|
|
|
|
var n = this.charCodeAt(i).toString(16);
|
|
|
|
o += n.length < 2 ? '0' + n : n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "0x" + o;
|
|
|
|
}
|
|
|
|
|
|
|
|
String.prototype.hex2bin = function() {
|
|
|
|
bytes = []
|
|
|
|
|
2014-05-02 11:35:12 +00:00
|
|
|
for(var i=2; i< this.length-1; i+=2) {
|
2014-05-02 10:08:52 +00:00
|
|
|
bytes.push(parseInt(this.substr(i, 2), 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
return String.fromCharCode.apply(String, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
String.prototype.num2bin = function() {
|
|
|
|
return ("0x"+parseInt(this).toString(16)).bin()
|
|
|
|
}
|