5
5
from config import BTAPIKEY
6
6
from rpcclient import gettxout
7
7
from cacher import *
8
+ import config
9
+
10
+ BLOCKCHAININFO_API_URL = "https://blockchain.info/"
11
+ BLOCKR_API_URL = "http://btc.blockr.io/api/v1"
12
+ BLOCKTRAIL_API_URL = "https://api.blocktrail.com/v1/btc/"
13
+ BLOCKCYPHER_API_URL = "https://api.blockcypher.com/v1/btc/main/"
14
+ BITGO_API_URL = "https://www.bitgo.com/api/v1/"
15
+ BLOCKONOMICS_API_URL = "https://www.blockonomics.co/api"
16
+
17
+ # note: "blockchain.info" does not support testnet
18
+ # note: "blockonomics" does not appear to support testnet
19
+ TESTNET_BLOCKR_API_URL = "http://tbtc.blockr.io/api/v1"
20
+ TESTNET_BLOCKTRAIL_API_URL = "https://api.blocktrail.com/v1/tbtc/"
21
+ TESTNET_BLOCKCYPHER_API_URL = "https://api.blockcypher.com/v1/btc/test3/"
22
+ TESTNET_BITGO_API_URL = "https://test.bitgo.com/api/v1/"
8
23
9
24
try :
10
25
expTime = config .BTCBAL_CACHE
@@ -15,7 +30,8 @@ def bc_getutxo(address, ramount, page=1, retval=None, avail=0):
15
30
if retval == None :
16
31
retval = []
17
32
try :
18
- r = requests .get ('https://api.blocktrail.com/v1/btc/address/' + address + '/unspent-outputs?api_key=' + str (BTAPIKEY )+ '&limit=200&page=' + str (page ))
33
+ apiUrl = BLOCKTRAIL_API_URL if not config .TESTNET else TESTNET_BLOCKTRAIL_API_URL
34
+ r = requests .get (apiUrlr + '/address/' + address + '/unspent-outputs?api_key=' + str (BTAPIKEY )+ '&limit=200&page=' + str (page ))
19
35
if r .status_code == 200 :
20
36
response = r .json ()
21
37
unspents = response ['data' ]
@@ -40,7 +56,8 @@ def bc_getutxo(address, ramount, page=1, retval=None, avail=0):
40
56
41
57
def bc_getutxo_blockcypher (address , ramount ):
42
58
try :
43
- r = requests .get ('https://api.blockcypher.com/v1/btc/main/addrs/' + address + '?unspentOnly=true' )
59
+ apiUrl = BLOCKCYPHER_API_URL if not config .TESTNET else TESTNET_BLOCKCYPHER_API_URL
60
+ r = requests .get (apiUrl + '/addrs/' + address + '?unspentOnly=true' )
44
61
45
62
if r .status_code == 200 :
46
63
unspents = r .json ()['txrefs' ]
@@ -65,7 +82,8 @@ def bc_getutxo_blockcypher(address, ramount):
65
82
66
83
def bc_getutxo_blockr (address , ramount ):
67
84
try :
68
- r = requests .get ('http://btc.blockr.io/api/v1/address/unspent/' + address + '?unconfirmed=1' )
85
+ apiUrl = BLOCKR_API_URL if not config .TESTNET else TESTNET_BLOCKR_API_URL
86
+ r = requests .get (apiUrl + '/address/unspent/' + address + '?unconfirmed=1' )
69
87
70
88
if r .status_code == 200 :
71
89
#Process and format response from blockr.io
@@ -92,8 +110,12 @@ def bc_getutxo_blockr(address, ramount):
92
110
93
111
94
112
def bc_getpubkey (address ):
113
+ # note: only supports mainnet
95
114
try :
96
- r = requests .get ('https://blockchain.info/q/pubkeyaddr/' + address )
115
+ if config .TESTNET :
116
+ return "error: tried using blockchain.info api with testnet enabled"
117
+
118
+ r = requests .get (BLOCKCHAININFO_API_URL + '/q/pubkeyaddr/' + address )
97
119
98
120
if r .status_code == 200 :
99
121
return str (r .text )
@@ -117,7 +139,8 @@ def bc_getbalance(address):
117
139
118
140
def bc_getbalance_bitgo (address ):
119
141
try :
120
- r = requests .get ('https://www.bitgo.com/api/v1/address/' + address )
142
+ apiUrl = BITGO_API_URL if not config .TESTNET else TESTNET_BITGO_API_URL
143
+ r = requests .get (apiUrl + '/address/' + address )
121
144
if r .status_code == 200 :
122
145
balance = int (r .json ()['balance' ])
123
146
return {"bal" :balance , "error" : None }
@@ -128,7 +151,8 @@ def bc_getbalance_bitgo(address):
128
151
129
152
def bc_getbalance_blockcypher (address ):
130
153
try :
131
- r = requests .get ('https://api.blockcypher.com/v1/btc/main/addrs/' + address + '/balance' )
154
+ apiUrl = BLOCKCYPHER_API_URL if not config .TESTNET else TESTNET_BLOCKCYPHER_API_URL
155
+ r = requests .get (apiUrl + '/addrs/' + address + '/balance' )
132
156
if r .status_code == 200 :
133
157
balance = int (r .json ()['balance' ])
134
158
return {"bal" :balance , "error" : None }
@@ -139,7 +163,8 @@ def bc_getbalance_blockcypher(address):
139
163
140
164
def bc_getbalance_blockr (address ):
141
165
try :
142
- r = requests .get ('http://btc.blockr.io/api/v1/address/balance/' + address )
166
+ apiUrl = BLOCKR_API_URL if not config .TESTNET else TESTNET_BLOCKR_API_URL
167
+ r = requests .get (apiUrl + '/address/balance/' + address )
143
168
if r .status_code == 200 :
144
169
balance = int (r .json ()['data' ]['balance' ]* 1e8 )
145
170
return {"bal" :balance , "error" : None }
@@ -222,7 +247,10 @@ def bc_getbulkbalance_blockonomics(addresses):
222
247
formatted = formatted + " " + address
223
248
224
249
try :
225
- r = requests .post ('https://www.blockonomics.co/api/balance' ,json .dumps ({"addr" :formatted }))
250
+ if config .TESTNET :
251
+ return "error: tried using blockonomics api with testnet enabled"
252
+
253
+ r = requests .post (apiUrl + '/balance' ,json .dumps ({"addr" :formatted }))
226
254
if r .status_code == 200 :
227
255
balances = r .json ()['response' ]
228
256
retval = {}
@@ -243,7 +271,8 @@ def bc_getbulkbalance_blockr(addresses):
243
271
formatted = formatted + "," + address
244
272
245
273
try :
246
- r = requests .get ('http://btc.blockr.io/api/v1/address/balance/' + formatted )
274
+ apiUrl = BLOCKR_API_URL if not config .TESTNET else TESTNET_BLOCKR_API_URL
275
+ r = requests .get (apiUrl + '/address/balance/' + formatted )
247
276
if r .status_code == 200 :
248
277
balances = r .json ()['data' ]
249
278
retval = {}
@@ -263,7 +292,10 @@ def bc_getbulkbalance_blockchain(addresses):
263
292
else :
264
293
formatted = formatted + "|" + address
265
294
try :
266
- r = requests .get ('https://blockchain.info/balance?active=' + formatted )
295
+ if config .TESTNET :
296
+ return "error: tried using blockchain.info api with testnet enabled"
297
+
298
+ r = requests .get (BLOCKCHAININFO_API_URL + '/balance?active=' + formatted )
267
299
if r .status_code == 200 :
268
300
balances = r .json ()
269
301
retval = {}
0 commit comments