forked from NxtChg/tsbw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend_bci.js
59 lines (43 loc) · 1.98 KB
/
backend_bci.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
/*=============================================================================
Created by NxtChg ([email protected]), 2016. License: Public Domain.
=============================================================================*/
// Back-end for blockchain.info API
var backend = { link: '<a href="https://blockchain.info/" target=_blank>blockchain.info</a>', adr_page: 'https://blockchain.info/address/' };
function backend_balance_cb(res)
{
console.log(res); this.balance_cb(parseFloat(res) / 1e8);
}//____________________________________________________________________________
function backend_unspent_cb(data)
{
var utxo = false; console.log(data);
try{ data = JSON.parse(data); } catch(e){ data = {}; }
if(typeof data.unspent_outputs != 'undefined')
{
var u = data.unspent_outputs; utxo = [];
for(var i = 0; i < u.length; i++)
{
utxo.push({txid: u[i].tx_hash_big_endian, n: u[i].tx_output_n, amount: u[i].value, script: u[i].script});
}
}
backend.unspent_cb(utxo);
}//____________________________________________________________________________
function backend_send_cb(res)
{
console.log(res);
backend.send_cb(res.indexOf('Transaction Submitted') == 0 ? '' : res);
}//____________________________________________________________________________
backend.get_balance = function(adr, cb)
{
this.balance_cb = cb;
js.ajax('GET', 'https://blockchain.info/q/addressbalance/' + adr, 'cors=true', backend_balance_cb);
};//___________________________________________________________________________
backend.get_utxo = function(adr, cb)
{
this.unspent_cb = cb;
js.ajax('GET', 'https://blockchain.info/unspent?active=' + adr, 'cors=true', backend_unspent_cb);
};//___________________________________________________________________________
backend.send = function(tx, cb)
{
this.send_cb = cb;
js.ajax('POST', 'https://blockchain.info/pushtx', 'tx=' + tx, backend_send_cb);
};//___________________________________________________________________________