Skip to content

Commit 8585e59

Browse files
committed
Re-writing ethereum.js. Added future/promises support.
1 parent ae1de65 commit 8585e59

File tree

8 files changed

+2537
-434
lines changed

8 files changed

+2537
-434
lines changed

mist/assets/debugger/debugger.qml

Lines changed: 425 additions & 423 deletions
Large diffs are not rendered by default.

mist/assets/ext/html_messaging.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// The magic return variable. The magic return variable will be set during the execution of the QML call.
2+
(function(window) {
3+
function message(type, data) {
4+
document.title = JSON.stringify({type: type, data: data});
5+
6+
return window.____returnData;
7+
}
8+
9+
function isPromise(o) {
10+
return typeof o === "object" && o.then
11+
}
12+
13+
window.eth = {
14+
_callbacks: {},
15+
_onCallbacks: {},
16+
prototype: Object(),
17+
18+
coinbase: function() {
19+
return new Promise(function(resolve, reject) {
20+
postData({call: "getCoinBase"}, function(coinbase) {
21+
resolve(coinbase);
22+
});
23+
});
24+
},
25+
26+
block: function(numberOrHash) {
27+
return new Promise(function(resolve, reject) {
28+
var func;
29+
if(typeof numberOrHash == "string") {
30+
func = "getBlockByHash";
31+
} else {
32+
func = "getBlockByNumber";
33+
}
34+
35+
postData({call: func, args: [numberOrHash]}, function(block) {
36+
if(block)
37+
resolve(block);
38+
else
39+
reject("not found");
40+
41+
});
42+
});
43+
},
44+
45+
transact: function(params) {
46+
if(params === undefined) {
47+
params = {};
48+
}
49+
50+
if(params.endowment !== undefined)
51+
params.value = params.endowment;
52+
if(params.code !== undefined)
53+
params.data = params.code;
54+
55+
56+
var promises = []
57+
if(isPromise(params.to)) {
58+
promises.push(params.to.then(function(_to) { params.to = _to; }));
59+
}
60+
if(isPromise(params.from)) {
61+
promises.push(params.from.then(function(_from) { params.from = _from; }));
62+
}
63+
64+
if(isPromise(params.data)) {
65+
promises.push(params.data.then(function(_code) { params.data = _code; }));
66+
} else {
67+
if(typeof params.data === "object") {
68+
data = "";
69+
for(var i = 0; i < params.data.length; i++) {
70+
data += params.data[i]
71+
}
72+
} else {
73+
data = params.data;
74+
}
75+
}
76+
77+
// Make sure everything is string
78+
var fields = ["value", "gas", "gasPrice"];
79+
for(var i = 0; i < fields.length; i++) {
80+
if(params[fields[i]] === undefined) {
81+
params[fields[i]] = "";
82+
}
83+
params[fields[i]] = params[fields[i]].toString();
84+
}
85+
86+
// Load promises then call the last "transact".
87+
return Q.all(promises).then(function() {
88+
return new Promise(function(resolve, reject) {
89+
postData({call: "transact", args: params}, function(data) {
90+
if(data[1])
91+
reject(data[0]);
92+
else
93+
resolve(data[0]);
94+
});
95+
});
96+
})
97+
},
98+
99+
compile: function(code) {
100+
return new Promise(function(resolve, reject) {
101+
postData({call: "compile", args: [code]}, function(data) {
102+
if(data[1])
103+
reject(data[0]);
104+
else
105+
resolve(data[0]);
106+
});
107+
});
108+
},
109+
110+
key: function() {
111+
return new Promise(function(resolve, reject) {
112+
postData({call: "getKey"}, function(k) {
113+
resolve(k);
114+
});
115+
});
116+
}
117+
};
118+
119+
function postData(data, cb) {
120+
data._seed = Math.floor(Math.random() * 1000000)
121+
if(cb) {
122+
eth._callbacks[data._seed] = cb;
123+
}
124+
125+
if(data.args === undefined) {
126+
data.args = [];
127+
}
128+
129+
navigator.qt.postMessage(JSON.stringify(data));
130+
}
131+
132+
navigator.qt.onmessage = function(ev) {
133+
var data = JSON.parse(ev.data)
134+
135+
if(data._event !== undefined) {
136+
eth.trigger(data._event, data.data);
137+
} else {
138+
if(data._seed) {
139+
var cb = eth._callbacks[data._seed];
140+
if(cb) {
141+
cb.call(this, data.data)
142+
143+
// Remove the "trigger" callback
144+
delete eth._callbacks[ev._seed];
145+
}
146+
}
147+
}
148+
}
149+
})(this);

mist/assets/ext/pre.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ navigator.qt.onmessage = function(ev) {
3535
}
3636
}
3737
}
38+
39+
if(typeof(Promise) === "undefined") {
40+
window.Promise = Q.Promise;
41+
}

0 commit comments

Comments
 (0)