Scripts to accompany the book

Balance_bitcoin

A script for finding the balance of several bitcoin addresses

#!/usr/bin/env python


import os.path

import urllib2

import json


datain = raw_input("Enter the path to your 'address' file:")


if os.path.exists(datain):

	data = open(datain, "r")

	outfile = open("balances.txt", 'w')

	print "Extracting balances, please wait....."

	for line in data:

		bal = "https://blockchain.info/balance?active=%s" % line.rstrip()

		balance = urllib2.urlopen(bal)

		data2 = json.loads(balance.read())

		print line

		print data2[line.rstrip()]['final_balance']

		outfile.write(str(data2) + "\n")

	outfile.close()

	data.close()

else:

	sorry = "Sorry, not a valid path, please re-run the program"

	print sorry

Download py file

Miner

A simple script to demonstrate the mining process

import hashlib
text = ("94a89e8799ce8e3a0e876261e1fb10326ba1cf08")
for nonce in range(10000000):
    input = text+str(nonce)
    hash = hashlib.sha256(input.encode()).hexdigest()
    print (input, hash)
    if hash.startswith("0000"):
        print ("Found Hash")
        break

Download py file


Unix

unix time convertor

import time

rawtime = str(raw_input("Whats the value from the hex?"))

flip = rawtime[::-1]

u_time = int(flip, 16)

final = time.ctime(int(u_time))

print final

Download py file


Unspent_n

Discover the unspent Transactions associated with an address

import json
import requests

address = str(raw_input("Enter the bitcoin address? "))

myfile = open('unspent_%s.txt' % address, 'w')

resp = requests.get('https://blockchain.info/unspent?active=%s' % address)

utxo_set = json.loads(resp.text)["unspent_outputs"]

myfile.write('TX_ID TX_Number Amount' + '\n' + '\n')

for utxo in utxo_set:
	myfile.write("%s %d %ld Satoshis" % (utxo['tx_hash_big_endian'], utxo['tx_output_n'], utxo['value']) + '\n')

myfile.write('\n' + '\n')

myfile = open('balance_%s.txt' % address, 'w')

	
balance = requests.get('https://blockchain.info/balance?active=%s' % address)

for line in balance:
	myfile.write(line + '\n')
	

Download py file


Regular Expressions

Regex for some of the primary cryptocurrencies

Bitcoin [13][a-km-zA-HJ-NP-Z1-9]{25,34}
Bch [13][a-km-zA-HJ-NP-Z1-9]{33}
Litecoin [LM3][a-km-zA-HJ-NP-Z1-9]{26,33}
Ethereum 0x[a-fA-F0-9]{40}
Monero 4[0-9AB][1-9A-HJ-NP-Za-km-z]{93}
Dash X[1-9A-HJ-NP-Za-km-z]{33}
Ripple r[0-9a-zA-Z]{33}
Neo A[0-9a-zA-Z]{33}
Dogecoin D{1}[5-9A-HJ-NP-U]{1}[1-9A-HJ-NP-Za-kmz]{32}