-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject_script.js
57 lines (53 loc) · 1.83 KB
/
inject_script.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
console.log('Navano injected script started');
// TODO: use proper Promise structures, instead of grungy wait loops.
let signature = '';
let waitingForSignature = false;
let address = '';
let waitingForAddress = false;
window.nano = {
version: '0.0.1',
getAddress: async function() {
if (waitingForAddress) {
console.log('Already waiting for address, skipping');
return;
}
console.log('inject_script: nano.getAddress');
window.postMessage({type:'nano', action:'getAddress'});
waitingForAddress = true;
while (waitingForAddress) {
await new Promise(r => setTimeout(r, 100)); // sleep
}
let tmp_address = address;
address = '';
return tmp_address;
},
signMessage: async function(message) {
if (waitingForSignature) {
console.log('Already waiting for signature, skipping');
return;
}
console.log('inject_script: nano.signMessage', message);
window.postMessage({type:'nano', action:'signMessage', message});
waitingForSignature = true;
while (waitingForSignature) {
await new Promise(r => setTimeout(r, 100)); // sleep
}
let tmp_signature = signature;
signature = '';
return tmp_signature;
}
};
window.addEventListener('message', function (event) {
console.log('inject_script: message event:', event);
// Filter on 'nanoResponse' event type.
if (event.data.type && (event.data.type == 'nanoResponse') && typeof chrome.app.isInstalled !== 'undefined') {
if (waitingForSignature && event.data.action && event.data.action == 'signMessage') {
signature = event.data.response.signature;
waitingForSignature = false;
}
if (waitingForAddress && event.data.action && event.data.action == 'getAddress') {
address = event.data.response.address;
waitingForAddress = false;
}
}
}, false);