forked from dsias/blockchain.info
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchainapi.js
More file actions
326 lines (270 loc) · 10.9 KB
/
Copy pathblockchainapi.js
File metadata and controls
326 lines (270 loc) · 10.9 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var BlockchainAPI = new function() {
var BlockchainAPI = this;
this.get_history = function(success, error, tx_filter, offset, n) {
MyWallet.setLoadingText('Loading transactions');
var clientTime=(new Date()).getTime();
if (!tx_filter) tx_filter = 0;
if (!offset) offset = 0;
if (!n) n = 0;
var data = {active : MyWallet.getActiveAddresses().join('|'), format : 'json', filter : tx_filter, offset : offset, no_compact : true, ct : clientTime, n : n};
$.ajax({
type: "POST",
dataType: 'json',
url: root +'multiaddr',
data: data,
success: function(obj) {
if (obj.error != null) {
MyWallet.makeNotice('error', 'misc-error', obj.error);
}
MyWallet.handleNTPResponse(obj, clientTime);
try {
//Cache results to show next login
if (offset == 0 && tx_filter == 0) {
MyStore.put('multiaddr', JSON.stringify(obj));
}
success(obj);
} catch (e) {
MyWallet.makeNotice('error', 'misc-error', e);
error();
}
},
error : function(data) {
if (data.responseText)
MyWallet.makeNotice('error', 'misc-error', data.responseText);
else
MyWallet.makeNotice('error', 'misc-error', 'Error Downloading Wallet Balance');
error();
}
});
}
//Get the balances of multi addresses (Used for archived)
this.get_balances = function(addresses, success, error) {
MyWallet.setLoadingText('Getting Balances');
$.ajax({
type: "POST",
url: root + 'multiaddr',
dataType: 'json',
data : {active : addresses.join('|'), simple : true, format : 'json'},
success: function(obj) {
for (var key in obj) {
if (MyWallet.addressExists(key))
MyWallet.setAddressBalance(key, obj[key].final_balance);
}
success(obj);
},
error : function(e) {
error(e.responseText);
}
});
}
//Get the balance of an array of addresses
this.get_balance = function(addresses, success, error) {
MyWallet.setLoadingText('Getting Balance');
this.get_balances(addresses, function(obj){
var balance = 0;
for (var key in obj) {
balance += obj[key].final_balance;
}
success(balance);
}, error);
}
this.get_ticker = function() {
MyWallet.setLoadingText('Getting Ticker Data');
$.ajax({
type: "GET",
dataType: 'json',
url: root +'ticker',
data: {format : 'json'},
success: function(data) {
var container = $('#send-ticker ul').empty();
for (var code in data) {
container.append('<li><div style="width:35px;padding-left:10px;font-weight:bold;display:inline-block">'+code+'</div> <i class="icon-user" style="background-image:url('+ resource + ((data[code]['15m'] >= data[code]['24h']) ? 'up_green.png' : 'down_red.png') + ');width:14px;background-position:0px"></i>' + data[code]['15m'] +'</li>');
}
},
error : function(e) {
console.log(e);
}
});
}
this.resolve_firstbits = function(addr, success, error) {
MyWallet.setLoadingText('Querying Firstbits');
$.ajax({
type: "GET",
url: root + 'q/resolvefirstbits/'+addr,
data : {format : 'plain'},
success: function(data) {
if (data == null || data.length == 0)
error();
else
success(data);
},
error : function(e) {
error(e.responseText);
}
});
}
this.get_rejection_reason = function(hexhash, got_reason, not_rejected, error) {
MyWallet.setLoadingText('Querying Rejection Reason');
$.ajax({
type: "GET",
url: root + 'q/rejected/'+hexhash,
data : {format : 'plain'},
success: function(data) {
if (data == null || data.length == 0)
error();
else
got_reason(data);
},
error : function(e) {
if (e.status == 404)
not_rejected();
else
error(e.responseText);
}
});
}
this.push_tx = function(tx, note, success, error) {
try {
MyWallet.setLoadingText('Pushing Transaction');
var transactions = MyWallet.getTransactions();
//Record the first transactions we know if it doesn't change then our new transactions wasn't push out propoerly
if (transactions.length > 0)
var first_tx_index = transactions[0].txIndex;
var s = tx.serialize();
var tx_hash = Crypto.util.bytesToHex(Crypto.SHA256(Crypto.SHA256(s, {asBytes: true}), {asBytes: true}).reverse());
function did_push() {
success(); //Call success to enable send button again
function call_history() {
MyWallet.get_history(function() {
if (transactions.length == 0 || transactions[0].txIndex == first_tx_index) {
BlockchainAPI.get_rejection_reason(tx_hash, function(reason) {
MyWallet.makeNotice('error', 'tx-error', reason);
}, function() {
if (transactions.length == 0 || transactions[0].txIndex == first_tx_index) {
MyWallet.get_history();
}
}, function() {
if (transactions.length == 0 || transactions[0].txIndex == first_tx_index) {
MyWallet.makeNotice('error', 'tx-error', 'Unknown Error Pushing Transaction');
}
});
} else {
playSound('beep');
}
}, function() {
MyWallet.makeNotice('error', 'tx-error', 'Unable to determine if transaction was submitted. Please re-login.');
});
}
//Otherwise we set an interval to set for a transaction
setTimeout(function() {
if (transactions.length == 0 || transactions[0].txIndex == first_tx_index) {
call_history();
}
}, 3000);
};
function push_normal() {
var hex = Crypto.util.bytesToHex(s);
var post_data = {
format : "plain",
tx: hex,
hash : tx_hash
};
if (note) {
post_data.note = note;
}
$.ajax({
type: "POST",
url: root + 'pushtx',
data : post_data,
success: function() {
did_push();
},
error : function(e) {
error(e ? e.responseText : null);
}
});
}
try {
var buffer = new ArrayBuffer(s.length);
var int8_array = new Int8Array(buffer);
int8_array.set(s);
var blob = new Blob([buffer], {type : 'application/octet-stream'});
if (blob.size != s.length)
throw 'Inconsistent Data Sizes (blob : ' + blob.size + ' s : ' + s.length + ' buffer : ' + buffer.byteLength + ')';
var fd = new FormData();
fd.append('txbytes', blob);
if (note) {
fd.append('note', note);
}
fd.append('format', 'plain');
fd.append('hash', tx_hash);
$.ajax({
url: root + 'pushtx',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(){
did_push();
},
error : function(e) {
if (!e.responseText || e.responseText.indexOf('Parse:') == 0) {
push_normal();
} else {
error(e ? e.responseText : null);
}
}
});
} catch (e) {
console.log(e);
push_normal();
}
} catch (e) {
console.log(e);
error(e);
}
}
this.get_unspent = function(fromAddresses, success, error, confirmations) {
//Get unspent outputs
MyWallet.setLoadingText('Getting Unspent Outputs');
$.ajax({
type: "POST",
dataType: 'json',
url: root +'unspent',
data: {active : fromAddresses.join('|'), format : 'json', confirmations : confirmations ? confirmations : 0},
success: function(obj) {
try {
if (obj.error != null) {
throw obj.error;
}
if (obj.notice != null) {
MyWallet.makeNotice('notice', 'misc-notice', obj.notice);
}
//Save the unspent cache
MyStore.put('unspent', JSON.stringify(obj));
success(obj);
} catch (e) {
error(e);
}
},
error: function (data) {
//Try and look for unspent outputs in the cache
MyStore.get('unspent', function(cache) {
try {
if (cache != null) {
var obj = $.parseJSON(cache);
success(obj);
} else {
if (data.responseText)
throw data.responseText;
else
throw 'Error Contacting Server. No unspent outputs available in cache.';
}
} catch (e) {
error(e);
}
});
}
});
}
}