Skip to content

Commit 4f0e567

Browse files
author
carbonwallet
committed
Refactor for clarity.
1 parent 18f7ada commit 4f0e567

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

js/blockchain.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Here we hold all the interactions with the blockchain.
2+
var BLOCKCHAIN = new function () {
3+
4+
this.retrieveBalance = function(address, callback) {
5+
url = 'http://blockchain.info/q/addressbalance/';
6+
this.tx_fetch(url + address, callback);
7+
}
8+
9+
this.getUnspentOutputs = function(address, callback) {
10+
11+
var url = 'http://blockchain.info/unspent?address=' + address;
12+
13+
this.tx_fetch(url, callback);
14+
}
15+
16+
this.sendTX = function(tx, callback) {
17+
18+
url = 'http://blockchain.info/pushtx';
19+
postdata = 'tx=' + tx;
20+
if (url != null && url != "") {
21+
this.tx_fetch(url, callback, callback, postdata);
22+
}
23+
}
24+
25+
// Some cross-domain magic (to bypass Access-Control-Allow-Origin)
26+
this.tx_fetch = function(url, onSuccess, onError, postdata) {
27+
var useYQL = true;
28+
29+
if (useYQL) {
30+
var q = 'select * from html where url="'+url+'"';
31+
if (postdata) {
32+
q = 'use "http://brainwallet.github.com/js/htmlpost.xml" as htmlpost; ';
33+
q += 'select * from htmlpost where url="' + url + '" ';
34+
q += 'and postdata="' + postdata + '" and xpath="//p"';
35+
}
36+
url = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(q);
37+
}
38+
39+
$.ajax({
40+
url: url,
41+
success: function(res) {
42+
onSuccess(useYQL ? $(res).find('results').text() : res.responseText);
43+
},
44+
error:function (xhr, opt, err) {
45+
if (onError)
46+
onError(err);
47+
}
48+
});
49+
}
50+
}

0 commit comments

Comments
 (0)