Geek Culture
Published in

Geek Culture

Who Owned That Bitcoin Before You?

Traversing bitcoin’s public ledger in Python, for beginners

Photo by Freddie Collins on Unsplash

Learning how to code is hard

This article == self-learning motivation

This article != Python tutorial

The story

Designing a solution

Finding the right API

{
'hash': 'this_transaction_hash',
'total': 9950000,
'fees': 50000,
'inputs': [{
'prev_hash': 'prev_transaction_hash',
'output_value': 10000000,
'addresses': ['Alice_wallet_address'],
}],
'outputs': [{
'value': 9950000,
'addresses': ['Bob_wallet_address'],
}]
}

Step Zero: boring installation

Step One: query the API for one transaction

Step One Solution

import requestsEXAMPLE_TRANSACTION_HASH = '0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2'
response = requests.get('https://api.blockcypher.com/v1/btc/main/txs/' + EXAMPLE_TRANSACTION_HASH)
print(response.json())
{
"block_hash":"0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4",
"block_height":277316,
"block_index":64,
"hash":"0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2",
"addresses":[
"1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK",
"1GdK9UzpHBzqzX2A9JFP3Di4weBwqgmoQA"
],
"total":9950000,
"fees":50000,
"size":258,
"vsize":258,
"preference":"high",
"confirmed":"2013-12-27T23:11:54Z",
"received":"2013-12-27T23:11:54Z",
"ver":1,
"double_spend":false,
"vin_sz":1,
"vout_sz":2,
"confirmations":398396,
"confidence":1,
"inputs":[
{
"prev_hash":"7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18",
"output_index":0,
"script":"483045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e381301410484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf",
"output_value":10000000,
"sequence":4294967295,
"addresses":[
"1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK"
],
"script_type":"pay-to-pubkey-hash",
"age":277298
}
],
"outputs":[
{
"value":1500000,
"script":"76a914ab68025513c3dbd2f7b92a94e0581f5d50f654e788ac",
"addresses":[
"1GdK9UzpHBzqzX2A9JFP3Di4weBwqgmoQA"
],
"script_type":"pay-to-pubkey-hash"
},
{
"value":8450000,
"script":"76a9147f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a888ac",
"addresses":[
"1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK"
],
"script_type":"pay-to-pubkey-hash"
}
]
}

Step Two: Get Previous Transaction Hashes from API Response

What’s a previous transaction again?

So how do we get previous transaction hashes from the API response?

transaction_info = response.json()  # Blockcypher api response
inputs = transaction_info['inputs']
prev_hashes = []
for elt in inputs:
prev_hashes.append(elt['prev_hash'])
print(prev_hashes)
['7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18']

Step Three: Get Sender Wallet Address from API Response

transaction_info = response.json()  # Blockcypher api response
inputs = transaction_info['inputs']
sender_addresses = []
for elt in inputs:
sender_addresses += elt['addresses']
print(sender_addresses)
sender_addresses.append(elt['addresses'])

Step Four: Repeat!

import timedef intentionally_slow_function():
time.sleep(0.2) # waits for 0.2 seconds
... # does other stuff
num_transactions_back = 0
addresses_seen = set()
transaction_queue = [EXAMPLE_TRANSACTION_HASH]
while num_transactions_back < 10 and len(transaction_queue) > 0:
transaction_info = get_transaction_info_blockcypher(transaction_queue.pop(0))
transaction_queue += get_prev_hashes(transaction_info)
sender_addresses = get_sender_addresses(transaction_info)
addresses_seen.update(sender_addresses)
num_transactions_back += 1

Step Five: Check if payer was sketchy

SILK_ROAD_ADDRESS = '1HQ3Go3ggs8pFnXuHVHRytPCq5fGG8Hbhx'funds_from_silk_road = SILK_ROAD_ADDRESS in addresses_seen
print(f'You do {"" if funds_from_silk_road else "not (as far as we know) "}have funds from silk road!')

What just happened?

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Charlie Andrews-Jubelt

climber, engineer, feminist, MIT alumnus, TBI survivor, curious about many things!