-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBrigeAPI.js
76 lines (68 loc) · 2.16 KB
/
BrigeAPI.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
69
70
71
72
73
74
75
76
/* global window */
import TinyIdentitys from './TinyIdentitys';
const genId = () => (Date.now() * 100 + Math.floor(Math.random() * 100)).toString(32);
export default class BrigeAPI {
static sendAsync(payload) {
payload.id = payload.id || genId();
return new Promise((resolve, reject) => {
BrigeAPI.callbacks.set(payload.id, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
switch (payload.type) {
case 'requestSignature':
return BrigeAPI.postMessage('signEOS', payload.id, {
data: JSON.stringify(payload.payload.transaction)
});
case 'requestArbitrarySignature':
return BrigeAPI.postMessage('signEOSMsg', payload.id, {
data: JSON.stringify(payload.payload)
});
case 'getOrRequestIdentity':
case 'identityFromPermissions':
case 'authenticate':
return BrigeAPI.sendResponse(payload.id, TinyIdentitys.eos);
case 'forgetIdentity':
case 'requestAddNetwork':
return BrigeAPI.sendResponse(payload.id, true);
case 'getVersion':
return BrigeAPI.sendResponse(payload.id, '9.6.0');
case 'getPublicKey':
return BrigeAPI.sendResponse(payload.id, TinyIdentitys.eos.publicKey);
case 'linkAccount':
case 'hasAccountFor':
case 'requestTransfer':
case 'createTransaction':
// all resolve to false
return BrigeAPI.sendResponse(payload.id, false);
default:
return BrigeAPI.sendError(payload.id, new Error('unknow method'));
}
});
}
static sendResponse(id, result) {
const callback = BrigeAPI.callbacks.get(id);
if (callback) {
callback(null, result);
BrigeAPI.callbacks.delete(id);
}
}
static sendError(id, error) {
const callback = BrigeAPI.callbacks.get(id);
if (callback) {
callback(error, null);
BrigeAPI.callbacks.delete(id);
}
}
static postMessage(handler, id, data) {
window.tinyBrige[handler]({
name: handler,
object: data,
id
});
}
}
BrigeAPI.callbacks = new Map();