Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_invalid_hash(self):
assert not is_valid_hash(self.invalid_hash), self.invalid_hash
def test_valid_hash(self):
assert is_valid_hash(self.valid_hash), self.valid_hash
def get_valid_metadata_identifier(coin_symbol, address, tx_hash, block_hash):
err_msg = 'Please supply only one of: address, tx_hash, or block_hash'
assert sum([1 for x in (address, tx_hash, block_hash) if x]) == 1, err_msg
if address:
assert is_valid_address_for_coinsymbol(
b58_address=address,
coin_symbol=coin_symbol), address
return dict(addrs=address)
elif tx_hash:
assert is_valid_hash(tx_hash), tx_hash
return dict(txs=tx_hash)
elif block_hash:
assert is_valid_block_representation(
block_representation=block_hash,
coin_symbol=coin_symbol)
return dict(blocks=block_hash)
else:
raise Exception('Logic Fail: This Should Not Be Possible')
def add_metadata_to_tx(request, coin_symbol, tx_hash):
if not is_valid_hash(tx_hash):
return Http404
initial = {}
form = BaseMetadataForm(initial=initial)
if request.method == 'POST':
form = BaseMetadataForm(data=request.POST)
if form.is_valid():
metadata_key = form.cleaned_data.get('metadata_key')
metadata_value = form.cleaned_data.get('metadata_value')
results = put_metadata(
metadata_dict={metadata_key: metadata_value},
tx_hash=tx_hash,
coin_symbol=coin_symbol,
api_key=BLOCKCYPHER_API_KEY,
private=False,
def home(request):
form = SearchForm(initial={
'search_string': '16Fg2yjwrbtC6fZp61EV9mNVKmwCzGasw5',
'coin_symbol': 'btc',
})
if request.method == 'POST':
form = SearchForm(data=request.POST)
if form.is_valid():
redirect_url = None
search_string = form.cleaned_data['search_string']
coin_symbol = form.cleaned_data['coin_symbol']
kwargs = {'coin_symbol': coin_symbol}
if is_valid_block_num(search_string):
kwargs['block_representation'] = search_string
redirect_url = reverse('block_overview', kwargs=kwargs)
elif is_valid_hash(search_string):
if coin_symbol in SHA_COINS:
if is_valid_sha_block_hash(search_string):
kwargs['block_representation'] = search_string
redirect_url = reverse('block_overview', kwargs=kwargs)
else:
kwargs['tx_hash'] = search_string
redirect_url = reverse('transaction_overview', kwargs=kwargs)
elif coin_symbol in SCRYPT_COINS:
# Try to see if it's a valid TX hash
tx_details = get_transaction_details(
tx_hash=search_string,
coin_symbol=coin_symbol,
limit=1,
api_key=BLOCKCYPHER_API_KEY,
)
if 'error' in tx_details:
def add_metadata_to_block(request, coin_symbol, block_hash):
if not is_valid_hash(block_hash):
return Http404
initial = {}
form = BaseMetadataForm(initial=initial)
if request.method == 'POST':
form = BaseMetadataForm(data=request.POST)
if form.is_valid():
metadata_key = form.cleaned_data.get('metadata_key')
metadata_value = form.cleaned_data.get('metadata_value')
results = put_metadata(
metadata_dict={metadata_key: metadata_value},
block_hash=block_hash,
coin_symbol=coin_symbol,
api_key=BLOCKCYPHER_API_KEY,
private=False,
def get_transactions_details(tx_hash_list, coin_symbol='btc', limit=None, api_key=None):
"""
Takes a list of tx_hashes, coin_symbol, and limit and returns the transaction details
Limit applies to both num inputs and num outputs.
TODO: add offsetting once supported
"""
for tx_hash in tx_hash_list:
assert is_valid_hash(tx_hash)
assert is_valid_coin_symbol(coin_symbol)
if len(tx_hash_list) == 0:
return []
elif len(tx_hash_list) == 1:
return [get_transaction_details(tx_hash=tx_hash_list[0],
coin_symbol=coin_symbol,
limit=limit,
api_key=api_key
)]
url = make_url(coin_symbol, **dict(txs=';'.join(tx_hash_list)))
params = {}
if api_key:
params['token'] = api_key
def get_tx_url(tx_hash, coin_symbol):
assert is_valid_coin_symbol(coin_symbol), coin_symbol
assert is_valid_hash(tx_hash), tx_hash
return 'https://live.blockcypher.com/%s/tx/%s/' % (coin_symbol, tx_hash)
def get_transaction_details(tx_hash, coin_symbol='btc', limit=None, tx_input_offset=None, tx_output_offset=None,
include_hex=False, show_confidence=False, confidence_only=False, api_key=None):
"""
Takes a tx_hash, coin_symbol, and limit and returns the transaction details
Optional:
- limit: # inputs/ouputs to include (applies to both)
- tx_input_offset: input offset
- tx_output_offset: output offset
- include_hex: include the raw TX hex
- show_confidence: adds confidence information to unconfirmed TXRefs.
- confidence_only: show only the confidence statistics and don't return the rest of the endpoint details (faster)
"""
assert is_valid_hash(tx_hash), tx_hash
assert is_valid_coin_symbol(coin_symbol), coin_symbol
added = 'txs/{}{}'.format(tx_hash, '/confidence' if confidence_only else '')
url = make_url(coin_symbol, added)
params = {}
if api_key:
params['token'] = api_key
if limit:
params['limit'] = limit
if tx_input_offset:
params['inStart'] = tx_input_offset
if tx_output_offset:
params['outStart'] = tx_output_offset
if include_hex:
params['includeHex'] = 'true'