2019-08-16 17:59:44 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
2019-09-19 16:31:15 +02:00
|
|
|
import sys
|
2019-08-16 17:59:44 +02:00
|
|
|
import redis
|
|
|
|
|
|
|
|
from hashlib import sha256
|
|
|
|
|
2019-09-19 16:31:15 +02:00
|
|
|
sys.path.append(os.path.join(os.environ['AIL_FLASK'], 'modules'))
|
2019-08-16 17:59:44 +02:00
|
|
|
import Flask_config
|
|
|
|
from Correlation import Correlation
|
|
|
|
|
|
|
|
r_serv_metadata = Flask_config.r_serv_metadata
|
|
|
|
|
|
|
|
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
2019-09-22 14:44:37 +02:00
|
|
|
#address_validation = {'bitcoin': 'base58', 'dash': 'base58'}
|
2019-08-16 17:59:44 +02:00
|
|
|
|
|
|
|
cryptocurrency = Correlation('cryptocurrency')
|
|
|
|
|
2019-09-19 16:31:15 +02:00
|
|
|
# http://rosettacode.org/wiki/Bitcoin/address_validation#Python
|
2019-08-16 17:59:44 +02:00
|
|
|
def decode_base58(bc, length):
|
|
|
|
n = 0
|
|
|
|
for char in bc:
|
|
|
|
n = n * 58 + digits58.index(char)
|
|
|
|
return n.to_bytes(length, 'big')
|
|
|
|
|
2019-09-19 16:31:15 +02:00
|
|
|
# http://rosettacode.org/wiki/Bitcoin/address_validation#Python
|
2019-09-22 14:44:37 +02:00
|
|
|
def check_base58_address(bc):
|
2019-08-16 17:59:44 +02:00
|
|
|
try:
|
|
|
|
bcbytes = decode_base58(bc, 25)
|
|
|
|
return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def verify_cryptocurrency_address(cryptocurrency_type, cryptocurrency_address):
|
|
|
|
if cryptocurrency_type == 'bitcoin':
|
2019-09-22 14:44:37 +02:00
|
|
|
return check_base58_address(cryptocurrency_address)
|
2019-08-16 17:59:44 +02:00
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def get_cryptocurrency(request_dict, cryptocurrency_type):
|
|
|
|
# basic verification
|
|
|
|
res = cryptocurrency.verify_correlation_field_request(request_dict, cryptocurrency_type)
|
|
|
|
if res:
|
|
|
|
return res
|
|
|
|
# cerify address
|
|
|
|
field_name = request_dict.get(cryptocurrency_type)
|
|
|
|
if not verify_cryptocurrency_address(cryptocurrency_type, field_name):
|
|
|
|
return ( {'status': 'error', 'reason': 'Invalid Cryptocurrency address'}, 400 )
|
|
|
|
|
|
|
|
return cryptocurrency.get_correlation(request_dict, cryptocurrency_type, field_name)
|
2019-09-19 16:31:15 +02:00
|
|
|
|
|
|
|
def save_cryptocurrency_data(cryptocurrency_name, date, item_path, cryptocurrency_address):
|
|
|
|
# create basic medata
|
|
|
|
if not r_serv_metadata.exists('cryptocurrency_metadata_{}:{}'.format(cryptocurrency_name, cryptocurrency_address)):
|
|
|
|
r_serv_metadata.hset('cryptocurrency_metadata_{}:{}'.format(cryptocurrency_name, cryptocurrency_address), 'first_seen', date)
|
|
|
|
r_serv_metadata.hset('cryptocurrency_metadata_{}:{}'.format(cryptocurrency_name, cryptocurrency_address), 'last_seen', date)
|
|
|
|
else:
|
|
|
|
last_seen = r_serv_metadata.hget('cryptocurrency_metadata_{}:{}'.format(cryptocurrency_name, cryptocurrency_address), 'last_seen')
|
|
|
|
if not last_seen:
|
|
|
|
r_serv_metadata.hset('cryptocurrency_metadata_{}:{}'.format(cryptocurrency_name, cryptocurrency_address), 'last_seen', date)
|
|
|
|
else:
|
|
|
|
if int(last_seen) < int(date):
|
|
|
|
r_serv_metadata.hset('cryptocurrency_metadata_{}:{}'.format(cryptocurrency_name, cryptocurrency_address), 'last_seen', date)
|
|
|
|
|
|
|
|
# global set
|
|
|
|
r_serv_metadata.sadd('set_cryptocurrency_{}:{}'.format(cryptocurrency_name, cryptocurrency_address), item_path)
|
|
|
|
|
|
|
|
# daily
|
|
|
|
r_serv_metadata.hincrby('cryptocurrency:{}:{}'.format(cryptocurrency_name, date), cryptocurrency_address, 1)
|
|
|
|
|
|
|
|
# all type
|
|
|
|
r_serv_metadata.zincrby('cryptocurrency_all:{}'.format(cryptocurrency_name), cryptocurrency_address, 1)
|
|
|
|
|
|
|
|
# item_metadata
|
|
|
|
r_serv_metadata.sadd('item_cryptocurrency_{}:{}'.format(cryptocurrency_name, item_path), cryptocurrency_address)
|