aboutsummaryrefslogtreecommitdiffstats
path: root/ethereal/assets/ethereum.js
blob: 173eaff22675fd116357a3baca4a188eea1dfefc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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;
    }

    navigator.qt.postMessage(JSON.stringify(data));
}

// Main Ethereum library
window.eth = {
    prototype: Object(),

    // 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") {
                        func =  "getBlockByHash"
                } else {
                        func =  "getBlockByNumber"
                }
                postData({call: func, args: [numberOrHash]}, cb)
        },

    // 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) {
        postData({call: "createTx", args: [recipient, value, gas, gasPrice, data]}, cb)
    },
}
window.eth._callbacks = {}

function debug(/**/) {
    var args = arguments;
    var msg = ""
    for(var i = 0; i < args.length; i++){
        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)

    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];
        }
    }
}