2019-11-22 15:30:11 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-05-25 16:11:55 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2019-11-22 15:30:11 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import requests
|
|
|
|
|
2022-11-28 15:01:40 +01:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2019-11-22 15:30:11 +01:00
|
|
|
|
2023-05-25 16:11:55 +02:00
|
|
|
logger = logging.getLogger()
|
|
|
|
|
2022-11-28 15:01:40 +01:00
|
|
|
blockchain_all = 'https://blockchain.info/rawaddr'
|
2019-11-22 15:30:11 +01:00
|
|
|
|
|
|
|
# pre-alpha script
|
|
|
|
|
|
|
|
# default nb_transactions = 50
|
|
|
|
def get_bitcoin_info(bitcoin_address, nb_transaction=50):
|
|
|
|
dict_btc = {}
|
|
|
|
set_btc_in = set()
|
|
|
|
set_btc_out = set()
|
|
|
|
try:
|
2023-05-25 16:11:55 +02:00
|
|
|
req = requests.get(f'{blockchain_all}/{bitcoin_address}?limit={nb_transaction}')
|
2019-11-22 15:30:11 +01:00
|
|
|
jreq = req.json()
|
|
|
|
except Exception as e:
|
2023-05-25 16:11:55 +02:00
|
|
|
logger.warning(e)
|
|
|
|
return dict_btc
|
|
|
|
|
|
|
|
if not jreq.get('n_tx'):
|
|
|
|
logger.critical(json.dumps(jreq))
|
2019-11-22 15:30:11 +01:00
|
|
|
return dict_btc
|
|
|
|
|
|
|
|
dict_btc['n_tx'] = jreq['n_tx']
|
|
|
|
dict_btc['total_received'] = float(jreq['total_received'] / 100000000)
|
|
|
|
dict_btc['total_sent'] = float(jreq['total_sent'] / 100000000)
|
|
|
|
dict_btc['final_balance'] = float(jreq['final_balance'] / 100000000)
|
|
|
|
|
|
|
|
for transaction in jreq['txs']:
|
2023-05-25 16:11:55 +02:00
|
|
|
for t_input in transaction['inputs']:
|
|
|
|
if 'addr' in t_input['prev_out']:
|
|
|
|
if t_input['prev_out']['addr'] != bitcoin_address:
|
|
|
|
set_btc_in.add(t_input['prev_out']['addr'])
|
2019-11-22 15:30:11 +01:00
|
|
|
for output in transaction['out']:
|
2020-01-21 10:59:11 +01:00
|
|
|
if 'addr' in output:
|
|
|
|
if output['addr'] != bitcoin_address:
|
|
|
|
set_btc_out.add(output['addr'])
|
2019-11-22 15:30:11 +01:00
|
|
|
|
|
|
|
dict_btc['btc_in'] = filter_btc_seen(set_btc_in)
|
|
|
|
dict_btc['btc_out'] = filter_btc_seen(set_btc_out)
|
|
|
|
return dict_btc
|
|
|
|
|
|
|
|
# filter btc seen in ail
|
|
|
|
def filter_btc_seen(btc_addr_set):
|
2024-04-09 14:22:11 +02:00
|
|
|
from lib.objects import CryptoCurrencies
|
|
|
|
|
2019-11-22 15:30:11 +01:00
|
|
|
list_seen_btc = []
|
|
|
|
for btc_addr in btc_addr_set:
|
2024-04-09 14:22:11 +02:00
|
|
|
cryptocurrency = CryptoCurrencies.CryptoCurrency(btc_addr, 'bitcoin')
|
2022-11-28 15:01:40 +01:00
|
|
|
if cryptocurrency.exists():
|
2019-11-22 15:30:11 +01:00
|
|
|
list_seen_btc.append(btc_addr)
|
|
|
|
return list_seen_btc
|