forked from NxtChg/tsbw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_exp.js
69 lines (51 loc) · 2.1 KB
/
backend_exp.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
/*=============================================================================
Created by NxtChg ([email protected]), 2016-2018. License: Public Domain.
=============================================================================*/
// Back-end for blockexplorer.com API
var backend =
{
host: 'blockexplorer.com',
home_page: 'https://blockexplorer.com/',
adr_page: 'https://blockexplorer.com/address/',
tx_page: 'https://blockexplorer.com/tx/'
};//___________________________________________________________________________
function backend_balance_cb(res)
{
console.log(res);
this.balance_cb(parseFloat(res) / 1e8);
}//____________________________________________________________________________
function backend_unspent_cb(data)
{
var u, utxo = false; console.log(data);
try{ u = JSON.parse(data); } catch(e){ u = false; }
if(u !== false)
{
utxo = [];
for(var i = 0; i < u.length; i++)
{
utxo.push({txid: u[i].txid, n: u[i].vout, amount: u[i].amount * 1e8, script: u[i].scriptPubKey});
}
}
backend.unspent_cb(utxo);
}//____________________________________________________________________________
function backend_send_cb(res)
{
console.log(res);
try{ res = JSON.parse(res); } catch(e){ res = {}; }
backend.send_cb(typeof res.txid != 'undefined' ? '' : 'request failed');
}//____________________________________________________________________________
backend.get_balance = function(adr, cb)
{
this.balance_cb = cb;
js.ajax('GET', 'https://blockexplorer.com/api/addr/' + adr + '/balance', '', backend_balance_cb);
};//___________________________________________________________________________
backend.get_utxo = function(adr, cb)
{
this.unspent_cb = cb;
js.ajax('GET', 'https://blockexplorer.com/api/addr/' + adr + '/utxo', '', backend_unspent_cb);
};//___________________________________________________________________________
backend.send = function(tx, cb)
{
this.send_cb = cb;
js.ajax('POST', 'https://blockexplorer.com/api/tx/send', 'rawtx=' + tx, backend_send_cb);
};//___________________________________________________________________________