-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
106 lines (95 loc) · 2.78 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function Lightwallet(config) {
this.target = config === undefined || config.target === undefined ? 'http://localhost:8100' : config.target;
this.nonce = Math.floor(new Date()*1000);
this.tasks = {};
if (window.parent == window) throw 'Plugin must be child of lightwallet';
else
window.onmessage = (e) => {
if (e.source == window) return;
if (typeof this.tasks[e.data.nonce] !== 'undefined') {
if (e.data.topic == 'error')
this.tasks[e.data.nonce](e.data.value, e.data);
else
this.tasks[e.data.nonce](false, e.data);
delete this.tasks[e.data.nonce];
}
};
};
Lightwallet.prototype.send = function(data) {
data.nonce = this.nonce++;
window.parent.postMessage(data, this.target);
return new Promise((resolve, reject) => {
this.tasks[data.nonce] = function(error, data) {
if (error)
reject(Error(error));
else
resolve(data);
};
});
};
Lightwallet.prototype.sign = function(text, avatar) {
return this.send({
query: 'sign',
params: {
avatar: avatar,
text: text
}
}).then(response => response.value);
};
Lightwallet.prototype.verify = function(content, address, signature) {
return this.send({
query: 'verify',
params: {
text: content,
address: address,
signature: signature
}
}).then(response => response.value);
};
Lightwallet.prototype.createMIT = function(avatar, symbol, content, raw) {
if(typeof raw === 'undefined')
raw=false;
return this.send({
query: 'create-mit',
params: {
symbol: symbol,
content: content,
avatar: avatar,
raw: raw
}
}).then(response => response.value);
};
Lightwallet.prototype.unlock = function() {
return this.send({
query: 'unlock'
}).then(response => response.value);
};
Lightwallet.prototype.getNetwork = function() {
return this.send({
query: 'network'
}).then(response => response.value);
};
Lightwallet.prototype.getAddresses = function() {
return this.send({
query: 'addresses'
}).then(response => response.value);
};
Lightwallet.prototype.getAvatars = function() {
return this.send({
query: 'avatars'
}).then(response => response.value);
};
Lightwallet.prototype.broadcast = function(tx) {
return this.send({
query: 'broadcast',
params: {
tx: tx
}
}).then(response => response.value);
};
Lightwallet.prototype.getPermissions = function() {
return this.send({
query: 'permissions'
});
};
module.exports = Lightwallet;