-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathavian.js
39 lines (35 loc) · 887 Bytes
/
avian.js
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
var backgroundStream;
window.addEventListener('load', function () {
backgroundStream = new PostMessageStream.WindowPostMessageStream({
name: 'avian',
target: 'background',
});
});
var avian = {
call: (data) => {
return notifyContent(data.method, data.args)
},
getBalance: () => {
return notifyContent("getbalance", {})
},
getAddress: () => {
return notifyContent("getaddress", {})
},
sendto: (addr, amount) => {
return notifyContent("sendto", {addr, amount})
}
}
async function notifyContent(method, args) {
backgroundStream.write({ method, args });
const res = await parseStream(backgroundStream)
return res.result;
}
// TODO: Fix Event leaking
function parseStream(stream) {
return new Promise((resolve, reject) => {
stream.on('data', (e) => {
if(e.error) reject(e.error);
else resolve(e)
});
});
}