def bc_getutxo_blockr(address, ramount): try: r = requests.get('http://btc.blockr.io/api/v1/address/unspent/' + address + '?unconfirmed=1') if r.status_code == 200: #Process and format response from blockr.io unspents = r.json()['data']['unspent'] print "got unspent list (blockr)", unspents retval = [] avail = 0 for tx in unspents: txUsed = gettxout(tx['tx'], tx['n']) isUsed = ('result' in txUsed and txUsed['result'] == None) if tx['confirmations'] > 0 and not isUsed: tx['amount'] = int( decimal.Decimal(tx['amount']) * decimal.Decimal(1e8)) avail += tx['amount'] retval.append([tx['tx'], tx['n'], tx['amount']]) if avail >= ramount: return { "avail": avail, "utxos": retval, "error": "none" } return {"avail": avail, "error": "Low balance error"} else: return {"error": "Connection error", "code": r.status_code} except: return {"error": "Connection error", "code": r.status_code}
def bc_getutxo(address, ramount): try: r = requests.get('https://api.blocktrail.com/v1/btc/address/' + address + '/unspent-outputs?api_key=' + str(BTAPIKEY)) if r.status_code == 200: unspents = r.json()['data'] print "got unspent list (btrail)", unspents retval = [] avail = 0 for tx in unspents: txUsed = gettxout(tx['hash'], tx['index']) isUsed = ('result' in txUsed and txUsed['result'] == None) if tx['confirmations'] > 0 and not isUsed and tx[ 'multisig'] == None: avail += tx['value'] retval.append([tx['hash'], tx['index'], tx['value']]) if avail >= ramount: return { "avail": avail, "utxos": retval, "error": "none" } return {"avail": avail, "error": "Low balance error"} else: #return {"error": "Connection error", "code": r.status_code} return bc_getutxo_blockcypher(address, ramount) except: return bc_getutxo_blockcypher(address, ramount)
def bc_getutxo(address, ramount, page=1, retval=None, avail=0): if retval is None: retval = [] try: r = requests.get(BLOCKTRAIL_API_URL + '/address/' + address + '/unspent-outputs?api_key=' + str(BTAPIKEY) + '&limit=200&page=' + str(page)) if r.status_code == 200: response = r.json() unspents = response['data'] print "got unspent list (btrail)", response for tx in unspents: txUsed = gettxout(tx['hash'], tx['index']) isUsed = ('result' in txUsed and txUsed['result'] is None) if not isUsed and txUsed['result']['confirmations'] > 0 and tx[ 'multisig'] is None: avail += tx['value'] retval.append([tx['hash'], tx['index'], tx['value']]) if avail >= ramount: return { "avail": avail, "utxos": retval, "error": "none" } if int(response['total']) - (int(response['per_page']) * page) > 0: return bc_getutxo(address, ramount, page + 1, retval, avail) return {"avail": avail, "error": "Low balance error"} else: # return {"error": "Connection error", "code": r.status_code} return bc_getutxo_blockcypher(address, ramount) except: return bc_getutxo_blockcypher(address, ramount)
def bc_getutxo_blockcypher(address, ramount): try: r = requests.get('https://api.blockcypher.com/v1/btc/main/addrs/' + address + '?unspentOnly=true') if r.status_code == 200: unspents = r.json()['txrefs'] print "got unspent list (bcypher)", unspents retval = [] avail = 0 for tx in unspents: txUsed = gettxout(tx['tx_hash'], tx['tx_output_n']) isUsed = ('result' in txUsed and txUsed['result'] == None) if tx['confirmations'] > 0 and not isUsed: avail += tx['value'] retval.append( [tx['tx_hash'], tx['tx_output_n'], tx['value']]) if avail >= ramount: return { "avail": avail, "utxos": retval, "error": "none" } return {"avail": avail, "error": "Low balance error"} else: #return {"error": "Connection error", "code": r.status_code} return bc_getutxo_blockr(address, ramount) except: return bc_getutxo_blockr(address, ramount)
def hc_getunspentutxo(address, ramount, avail=0): retval = [] try: r = requests.get('http://127.0.0.1:3006/api/addr/' + address + '/utxo?onCache=1') if r.status_code == 200: unspents = r.json() for tx in unspents: txUsed = gettxout(tx['txid'], tx['vout'])['result'] isUsed = txUsed == None coinbaseHold = (txUsed['coinbase'] and txUsed['confirmations'] < 100) multisigSkip = ("scriptPubKey" in txUsed and txUsed['scriptPubKey']['type'] == "multisig") if not isUsed and not coinbaseHold and txUsed[ 'confirmations'] > 0 and not multisigSkip: avail += tx['amount'] * 1e8 retval.append(tx) if avail >= ramount: return { "avail": avail, "utxos": retval, "error": "none" } return { "avail": avail, "error": "Low balance error, account HC balance must be greater than " + str(ramount / 1e8) } else: return {"error": "Connection error", "code": r.status_code} except Exception as e: return {"error": "Connection error", "code": e}
def bc_getutxo_blockcypher(address, ramount): try: r = requests.get('https://api.blockcypher.com/v1/btc/main/addrs/'+address+'?unspentOnly=true', timeout=2) if r.status_code == 200: unspents = r.json()['txrefs'] print "got unspent list (bcypher)", unspents retval = [] avail = 0 for tx in unspents: txUsed=gettxout(tx['tx_hash'],tx['tx_output_n']) isUsed = ('result' in txUsed and txUsed['result']==None) if tx['confirmations'] > 0 and not isUsed: avail += tx['value'] retval.append([ tx['tx_hash'], tx['tx_output_n'], tx['value'] ]) if avail >= ramount: return {"avail": avail, "utxos": retval, "error": "none"} return {"avail": avail, "error": "Low balance error"} else: return {"error": "Connection error", "code": r.status_code} except Exception as e: if 'call' in e.message: msg=e.message.split("call: ")[1] ret=re.findall('{.+',str(msg)) try: msg=json.loads(ret[0]) except TypeError: msg=ret[0] except ValueError: #reverse the single/double quotes and strip leading u in output to make it json compatible msg=json.loads(ret[0].replace("'",'"').replace('u"','"')) return {"error": "Connection error", "code": msg['message']} else: return {"error": "Connection error", "code": e.message}
def bc_getutxo(address, ramount, page=1, retval=None, avail=0): if retval==None: retval=[] try: r = requests.get('https://chain.api.btc.com/v3/address/'+address+'/unspent?pagesize=50&page='+str(page), timeout=2) if r.status_code == 200: response = r.json()['data'] unspents = response['list'] print "got unspent list (btc)", response for tx in unspents: txUsed=gettxout(tx['tx_hash'],tx['tx_output_n'])['result'] isUsed = txUsed==None coinbaseHold = (txUsed['coinbase'] and txUsed['confirmations'] < 100) multisigSkip = ("scriptPubKey" in txUsed and txUsed['scriptPubKey']['type'] == "multisig") if not isUsed and not coinbaseHold and txUsed['confirmations'] > 0 and not multisigSkip: avail += tx['value'] retval.append([ tx['tx_hash'], tx['tx_output_n'], tx['value'] ]) if avail >= ramount: return {"avail": avail, "utxos": retval, "error": "none"} if int(response['total_count'])-(int(response['pagesize'])*page ) > 0: return bc_getutxo(address, ramount, page+1, retval, avail) return {"avail": avail, "error": "Low balance error"} else: return bc_getutxo_blockcypher(address, ramount) except: return bc_getutxo_blockcypher(address, ramount)
def bc_getutxo_blocktrail(address, ramount, page=1, retval=None, avail=0): #deprecated and migrated to btc.com api if retval==None: retval=[] try: r = requests.get('https://api.blocktrail.com/v1/btc/address/'+address+'/unspent-outputs?api_key='+str(BTAPIKEY)+'&limit=200&sort_dir=desc&page='+str(page), timeout=2) if r.status_code == 200: response = r.json() unspents = response['data'] print_debug(("got unspent list (btrail)", response),4) for tx in unspents: txUsed=gettxout(tx['hash'],tx['index']) isUsed = ('result' in txUsed and txUsed['result']==None) coinbaseHold = (tx['is_coinbase'] and tx['confirmations'] < 100) if not isUsed and not coinbaseHold and txUsed['result']['confirmations'] > 0 and tx['multisig']==None: avail += tx['value'] retval.append([ tx['hash'], tx['index'], tx['value'] ]) if avail >= ramount: return {"avail": avail, "utxos": retval, "error": "none"} if int(response['total'])-(int(response['per_page'])*page ) > 0: return bc_getutxo(address, ramount, page+1, retval, avail) return {"avail": avail, "error": "Low balance error"} else: #return {"error": "Connection error", "code": r.status_code} return bc_getutxo_blockcypher(address, ramount) except: return bc_getutxo_blockcypher(address, ramount)
def bc_getutxo_blockcypher(address, ramount): try: r = requests.get('https://api.blockcypher.com/v1/btc/main/addrs/'+address+'?unspentOnly=true') if r.status_code == 200: unspents = r.json()['txrefs'] print "got unspent list (bcypher)", unspents retval = [] avail = 0 for tx in unspents: txUsed=gettxout(tx['tx_hash'],tx['tx_output_n']) isUsed = ('result' in txUsed and txUsed['result']==None) if tx['confirmations'] > 0 and not isUsed: avail += tx['value'] retval.append([ tx['tx_hash'], tx['tx_output_n'], tx['value'] ]) if avail >= ramount: return {"avail": avail, "utxos": retval, "error": "none"} return {"avail": avail, "error": "Low balance error"} else: return {"error": "Connection error", "code": r.status_code} except Exception as e: if 'call' in e.message: msg=e.message.split("call: ")[1] ret=re.findall('{.+',str(msg)) try: msg=json.loads(ret[0]) except TypeError: msg=ret[0] except ValueError: #reverse the single/double quotes and strip leading u in output to make it json compatible msg=json.loads(ret[0].replace("'",'"').replace('u"','"')) return {"error": "Connection error", "code": msg['message']} else: return {"error": "Connection error", "code": e.message}
def bc_getutxo_blocktrail(address, ramount, page=1, retval=None, avail=0): #deprecated and migrated to btc.com api if retval==None: retval=[] try: r = requests.get('https://api.blocktrail.com/v1/btc/address/'+address+'/unspent-outputs?api_key='+str(BTAPIKEY)+'&limit=200&sort_dir=desc&page='+str(page)) if r.status_code == 200: response = r.json() unspents = response['data'] print "got unspent list (btrail)", response for tx in unspents: txUsed=gettxout(tx['hash'],tx['index']) isUsed = ('result' in txUsed and txUsed['result']==None) coinbaseHold = (tx['is_coinbase'] and tx['confirmations'] < 100) if not isUsed and not coinbaseHold and txUsed['result']['confirmations'] > 0 and tx['multisig']==None: avail += tx['value'] retval.append([ tx['hash'], tx['index'], tx['value'] ]) if avail >= ramount: return {"avail": avail, "utxos": retval, "error": "none"} if int(response['total'])-(int(response['per_page'])*page ) > 0: return bc_getutxo(address, ramount, page+1, retval, avail) return {"avail": avail, "error": "Low balance error"} else: #return {"error": "Connection error", "code": r.status_code} return bc_getutxo_blockcypher(address, ramount) except: return bc_getutxo_blockcypher(address, ramount)
def bc_getutxo(address, ramount, page=1, retval=None, avail=0): if retval==None: retval=[] try: r = requests.get('https://chain.api.btc.com/v3/address/'+address+'/unspent?pagesize=50&page='+str(page)) if r.status_code == 200: response = r.json()['data'] unspents = response['list'] print "got unspent list (btc)", response for tx in unspents: txUsed=gettxout(tx['tx_hash'],tx['tx_output_n'])['result'] isUsed = txUsed==None coinbaseHold = (txUsed['coinbase'] and txUsed['confirmations'] < 100) multisigSkip = ("scriptPubKey" in txUsed and txUsed['scriptPubKey']['type'] == "multisig") if not isUsed and not coinbaseHold and txUsed['confirmations'] > 0 and not multisigSkip: avail += tx['value'] retval.append([ tx['tx_hash'], tx['tx_output_n'], tx['value'] ]) if avail >= ramount: return {"avail": avail, "utxos": retval, "error": "none"} if int(response['total_count'])-(int(response['pagesize'])*page ) > 0: return bc_getutxo(address, ramount, page+1, retval, avail) return {"avail": avail, "error": "Low balance error"} else: return bc_getutxo_blockcypher(address, ramount) except: return bc_getutxo_blockcypher(address, ramount)
def bc_getutxo(address, ramount, page=1, retval=None, avail=0): if retval==None: retval=[] try: r = requests.get(BLOCKTRAIL_API_URL + '/address/'+address+'/unspent-outputs?api_key='+str(BTAPIKEY)+'&limit=200&page='+str(page)) if r.status_code == 200: response = r.json() unspents = response['data'] print "got unspent list (btrail)", response for tx in unspents: txUsed=gettxout(tx['hash'],tx['index']) isUsed = ('result' in txUsed and txUsed['result']==None) if not isUsed and txUsed['result']['confirmations'] > 0 and tx['multisig']==None: avail += tx['value'] retval.append([ tx['hash'], tx['index'], tx['value'] ]) if avail >= ramount: return {"avail": avail, "utxos": retval, "error": "none"} if int(response['total'])-(int(response['per_page'])*page ) > 0: return bc_getutxo(address, ramount, page+1, retval, avail) return {"avail": avail, "error": "Low balance error"} else: #return {"error": "Connection error", "code": r.status_code} return bc_getutxo_blockcypher(address, ramount) except: return bc_getutxo_blockcypher(address, ramount)
def bc_getutxo(address, ramount): try: r = requests.get(BLOCKCHAININFO_API_URL + '/unspent?active=' + address) if r.status_code == 200: avail = 0 retval = [] response = r.json() unspents = response['unspent_outputs'] print "got unspent list (blockchain)", response for tx in sorted(unspents, key=lambda i: i['value'], reverse=True): txUsed = gettxout(tx['tx_hash_big_endian'], tx['tx_output_n'])['result'] isUsed = txUsed == None if not isUsed: coinbaseHold = (txUsed['coinbase'] and txUsed['confirmations'] < 100) multisigSkip = ("scriptPubKey" in txUsed and txUsed['scriptPubKey']['type'] == "multisig") if not coinbaseHold and txUsed[ 'confirmations'] > 0 and not multisigSkip: avail += tx['value'] retval.append([ tx['tx_hash_big_endian'], tx['tx_output_n'], tx['value'] ]) if avail >= ramount: return { "avail": avail, "utxos": retval, "error": "none" } if ('notice' in response and 'Ignoring' in response['notice']): return bc_getutxo_btccom(address, ramount) else: return {"avail": avail, "error": "Low balance error"} else: return bc_getutxo_btccom(address, ramount) except: return bc_getutxo_btccom(address, ramount)
def bc_getutxo_blockcypher(address, ramount): try: r = requests.get(BLOCKCYPHER_API_URL + '/addrs/'+address+'?unspentOnly=true') if r.status_code == 200: unspents = r.json()['txrefs'] print "got unspent list (bcypher)", unspents retval = [] avail = 0 for tx in unspents: txUsed=gettxout(tx['tx_hash'],tx['tx_output_n']) isUsed = ('result' in txUsed and txUsed['result']==None) if tx['confirmations'] > 0 and not isUsed: avail += tx['value'] retval.append([ tx['tx_hash'], tx['tx_output_n'], tx['value'] ]) if avail >= ramount: return {"avail": avail, "utxos": retval, "error": "none"} return {"avail": avail, "error": "Low balance error"} else: return {"error": "Connection error", "code": r.status_code} except: return {"error": "Connection error", "code": r.status_code}
def bc_getbalance_explorer(address): retval = {} try: r = requests.get('http://127.0.0.1:3006/api/addr/' + address + '/utxo?onCache=1') if r.status_code == 200: unspents = r.json() balance = 0 for tx in unspents: txUsed = gettxout(tx['txid'], tx['vout'])['result'] isUsed = txUsed == None coinbaseHold = (txUsed['coinbase'] and txUsed['confirmations'] < 100) multisigSkip = ("scriptPubKey" in txUsed and txUsed['scriptPubKey']['type'] == "multisig") if not isUsed and not coinbaseHold and txUsed[ 'confirmations'] > 0 and not multisigSkip: balance += tx['amount'] * 1e8 return {"bal": balance, "error": None} else: return {"bal": 0, "error": r.status_code} except Exception as e: print e return {"bal": 0, "error": e}