-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.js
68 lines (57 loc) · 1.48 KB
/
hub.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
58
59
60
61
62
63
64
65
66
67
68
var STORAGE_KEY = 'backbonecabal-demo';
var wallets = [];
var connectedFrame = null;
if (window.parent == window) {
window.location.replace('https://demo.' + window.location.hostname);
}
function loadWallets() {
wallets = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
return wallets;
}
function getWallet(origin) {
for (var i in wallets) {
var wallet = wallets[i];
if (wallet.origin === origin) {
return wallet;
}
}
return null;
}
var callbacks = {
registerWallet: function(event, reply) {
var origin = event.origin;
loadWallets();
if (getWallet(origin)) {
console.log('Already registered', origin);
return reply();
}
wallets.push({ origin: origin, name: event.data.name });
localStorage.setItem(STORAGE_KEY, JSON.stringify(wallets));
reply();
},
getWallets: function(event, reply) {
var _wallets = loadWallets();
reply(_wallets);
},
};
function receiveMessage(event) {
if (callbacks[event.data.command]) {
function reply(response) {
event.source.postMessage({
response: response,
id: event.data.id
}, event.origin);
}
try {
callbacks[event.data.command](event, reply);
} catch (e) {
event.source.postMessage({
error: e.message,
id: event.data.id
}, event.origin);
}
} else if (event.data.command) {
console.error('Unknown command', event.data.command);
}
}
window.addEventListener('message', receiveMessage, false);