forked from blockchain/unused-My-Wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
electrum.js
205 lines (163 loc) · 6.53 KB
/
electrum.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var default_servers = ['localhost','ecdsa.org'];
var JSONRPCoverHTTP = (function (host, port) {
var polling_interval = 5000, // interval at which to poll
message_id = 0, // latest message_id for JSONRPC
unhandled_requests = {}, // in format { 'message_id': {'id', 'method', 'params'}}
lastSend = 0, // time of last message
connected = false, // true if last message had return status 200
processing = false, // true if unhandled_requests was non-empty after last request
event_handler; // function set in 'init' to handle messages from server
port = port || "8081";
return {
"init": function (handler) {
event_handler = handler;
},
"isConnected": function () {
return connected && host;
},
"isProcessing": function () {
return processing;
},
"getHost": function () {
return host;
},
"changeHost": function (new_host) {
if (host != new_host) {
connected = false;
unhandled_requests = {};
lastSend = 0;
this.onDisconnect("Changing host.");
host = new_host;
return true;
} else {
return false;
}
},
"poll": function poll() {
this.send([]);
},
"send": function send(messages, onSuccess, onError) {
var xhr = new XMLHttpRequest(),
new_unhandled_requests = {},
that = this;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (!connected) {
connected = true;
that.onConnect();
}
// misc.obj_merge(unhandled_requests, new_unhandled_requests);
if (xhr.responseText) {
var data = JSON.parse(xhr.responseText);
if (data.constructor === Array) {
for (var i=0; i < data.length; i++) {
that.handle_response(data[i], onSuccess);
}
} else {
that.handle_response(data ,onSuccess)
}
} else { // !xhr.responseText
console.log("Empty response.")
}
if (misc.obj_size(unhandled_requests)) {
console.log("Waiting for responses", misc.obj_keys(unhandled_requests));
if (!processing) that.onProcessing();
processing = true;
polling_interval = 3000;
} else {
if (processing) that.onIdling();
processing = false;
polling_interval = 10000;
}
} else { // xhr.status != 200
if (connected) {
connected = false;
that.onDisconnect("Status != 200.");
}
for (var v in new_unhandled_requests) {
delete unhandled_requests[v];
}
if (onError) {
onError();
}
polling_interval = 10000;
}
}
};
var outgoing = [];
for (var i = 0; i < messages.length; i++) {
var m = {
"id": message_id,
"method": messages[i][0],
"params": messages[i][1]
};
unhandled_requests[message_id] = m;
new_unhandled_requests[message_id] = m;
outgoing.push(m);
console.log("-->", m['method'], JSON.stringify(m));
message_id += 1;
}
var url = 'http://' + host + ":" + port + '/';
if (outgoing.length) {
xhr.open('POST', url, true);
xhr.send(JSON.stringify(outgoing));
} else { // !out
xhr.open('GET', url, true);
xhr.send();
}
lastSend = (new Date()).getTime();
},
"go": function go() {
var that = this;
setInterval(function () {
var now = (new Date()).getTime();
if ((now - lastSend) > polling_interval) {
that.poll();
}
}, 500);
},
"handle_response": function handle_response(r, onSuccess) {
if ('error' in r) { // it's an error
console.log("Error " + r['error']['code'] + ": " + r['error']['message']);
} else if (r['id'] in unhandled_requests) { // it's a response
r['method'] = unhandled_requests[r['id']]['method'];
r['params'] = unhandled_requests[r['id']]['params'];
delete unhandled_requests[r['id']];
delete r['id'];
}
console.log("<--", r['method'], JSON.stringify(r).length > 200 ? r : JSON.stringify(r));
onSuccess(r);
},
"subscribe_to_addrs": function (as) {
var subscribe_commands = [];
for (var i = 0; i < as.length; i++) {
subscribe_commands.push(['blockchain.address.subscribe', [as[i]]]);
}
this.send(subscribe_commands);
},
"onConnect": function () {
},
"onDisconnect": function (reason) {
},
"onProcessing": function () {
},
"onIdling": function () {
}
};
});
var ElectrumAPI = {
rpc: JSONRPCoverHTTP(default_servers[0]),
get_history : function () {
this.rpc.send([["blockchain.address.get_history", [MyWallet.getActiveAddresses()]]], function(response) {
console.log(response);
}, function(response) {
console.log(response);
});
}
}
console.log('Load Electrum');
BlockchainAPI.get_history = function(success, error, tx_filter, tx_page) {
console.log('Get History');
ElectrumAPI.get_history();
}