2014-04-20 17:58:02 -05: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;
|
|
|
|
}
|
|
|
|
|
2014-04-23 17:01:22 -05:00
|
|
|
if(data.args === undefined) {
|
2014-04-24 07:43:00 -05:00
|
|
|
data.args = [];
|
2014-04-23 17:01:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-20 17:58:02 -05:00
|
|
|
navigator.qt.postMessage(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main Ethereum library
|
|
|
|
window.eth = {
|
|
|
|
prototype: Object(),
|
|
|
|
|
2014-04-23 04:51:34 -05: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 07:43:00 -05:00
|
|
|
func = "getBlockByHash";
|
2014-04-23 04:51:34 -05:00
|
|
|
} else {
|
2014-04-24 07:43:00 -05:00
|
|
|
func = "getBlockByNumber";
|
2014-04-23 04:51:34 -05:00
|
|
|
}
|
2014-04-24 07:43:00 -05:00
|
|
|
postData({call: func, args: [numberOrHash]}, cb);
|
2014-04-23 04:51:34 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Create transaction
|
|
|
|
//
|
|
|
|
// Creates a transaction with the current account
|
|
|
|
// If no recipient is set, the Ethereum API will see it as a contract creation
|
|
|
|
createTx: function(recipient, value, gas, gasPrice, data, cb) {
|
2014-04-24 07:43:00 -05:00
|
|
|
postData({call: "createTx", args: [recipient, value, gas, gasPrice, data]}, cb);
|
2014-04-23 04:51:34 -05:00
|
|
|
},
|
2014-04-23 17:01:22 -05:00
|
|
|
|
|
|
|
getStorage: function(address, storageAddress, cb) {
|
2014-04-24 07:43:00 -05:00
|
|
|
postData({call: "getStorage", args: [address, storageAddress]}, cb);
|
2014-04-23 17:01:22 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
getKey: function(cb) {
|
2014-04-24 07:43:00 -05:00
|
|
|
postData({call: "getKey"}, cb);
|
|
|
|
},
|
|
|
|
|
2014-04-25 18:47:04 -05:00
|
|
|
watch: function(address) {
|
|
|
|
postData({call: "watch", args: [address]});
|
|
|
|
},
|
|
|
|
|
2014-04-24 07:43:00 -05:00
|
|
|
|
|
|
|
on: function(event, cb) {
|
|
|
|
if(eth._onCallbacks[event] === undefined) {
|
|
|
|
eth._onCallbacks[event] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
eth._onCallbacks[event].push(cb);
|
|
|
|
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
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 17:01:22 -05:00
|
|
|
},
|
2014-04-20 17:58:02 -05:00
|
|
|
}
|
|
|
|
window.eth._callbacks = {}
|
2014-04-24 07:43:00 -05:00
|
|
|
window.eth._onCallbacks = {}
|
2014-04-20 17:58:02 -05:00
|
|
|
|
|
|
|
function debug(/**/) {
|
|
|
|
var args = arguments;
|
|
|
|
var msg = ""
|
2014-04-23 04:51:34 -05:00
|
|
|
for(var i = 0; i < args.length; i++){
|
2014-04-20 17:58:02 -05:00
|
|
|
if(typeof args[i] == "object") {
|
|
|
|
msg += " " + JSON.stringify(args[i])
|
|
|
|
} else {
|
|
|
|
msg += args[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById("debug").innerHTML += "<br>" + msg
|
|
|
|
}
|
|
|
|
|
|
|
|
navigator.qt.onmessage = function(ev) {
|
|
|
|
var data = JSON.parse(ev.data)
|
|
|
|
|
2014-04-24 07:43:00 -05: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 17:58:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|