A Step By Step Bitcoin Address Example

A money transfer transaction must include sender and receiver account information and transaction amount. On the other hand, a bitcoin transaction includes sender and receiver bitcoin address information. Everything turns around private key and public key pair, where did bitcoin address come from? What is the role of bitcoin address in bitcoin transactions? I attempt to explain how a bitcoin address is generated with a concrete example in this post.

Public key calculation

Suppose that you’ve chosen the following private key.


🙋‍♂️ You may consider to enroll my top-rated cryptography course on Udemy

Cryptography Basiscs From Scratch In Python

privateKey = 11253563012059685825953619222107823549092147699031672238385790369351542642469

Base point is a coordinate on the elliptic curve that bitcoin protocol consumes. It is publicly known. Additionally, modulo and order of group are publicly known information for bitcoin protocol, too. But these are not focus of this post.

x0 = 55066263022277343669578718895168534326250603453777594175500187360389116729240
y0 = 32670510020758816978083085130507043184471273380659243275938904335757337482424

Public key will be the following coordinates. We have used both point addition and double and add method rules to find the public key. Public key calculation is a fast operation.

public key = 36422191471907241029883925342251831624200921388586025344128047678873736520530, 20277110887056303803699431755396003735040374760118964734768299847012543114150

Here, we need to convert coordinates of public key to hex. Python provides hex command for this transformation but it prepends 0x prefix. We can specify the starting index to the end to remove that prefix. Additionally, we need to add 04 prefix to coordinates.

publicKeyHex = "04"+hex(publicKey[0])[2:]+hex(publicKey[1])[2:]

This will produce following public key demonstration.

public key (hex): 0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6

Hashing the public key

Now, we need to apply a series of hash functions to hex version of public key. I’ve written the following generalized function for hashing.

def hexStringToByte(content):
 return codecs.decode(content.encode("utf-8"), 'hex')

def hashHex(algorithm, content):
 my_sha = hashlib.new(algorithm)
 my_sha.update(hexStringToByte(content))
 return my_sha.hexdigest()

Firstly, we’ll digest the public key with SHA-256 and RIPEMD160, respectively. Finally, we need to add 00 prefix to double hashed value.





output = hashHex('sha256', publicKeyHex)
print("apply sha-256 to public key: ",output)

output = hashHex('ripemd160', output)
print("apply ripemd160 to sha-256 applied public key: ", output)

output = "00"+output
print("add network bytes to ripemd160 applied hash - extended ripemd160: ", output,"\n")

This produces the following hashes.

apply sha-256 to public key hex: 600ffe422b4e00731a59557a5cca46cc183944191006324a447bdb2d98d4b408

apply ripemd160 to sha-256 applied public key: 010966776006953d5567439e5e39f86a0d273bee

add network bytes to ripemd160 applied hash – extended ripemd160: 00010966776006953d5567439e5e39f86a0d273bee

Checksum

We’ve calculated the hash of public key in previous section. We’ll apply two times SHA-256 to hash of public key. And only first 8 digit of this new hash concerns us.

checksum = hashHex('sha256', hashHex('sha256', output))
checksum = checksum[0:8]

That would be the checksum

extract first 8 characters as checksum: d61967f6

Address

We will append this checksum to hash of public key.

address = output+checksum

In this way, we can create the raw address.

checksum appended public key hash: 00010966776006953d5567439e5e39f86a0d273beed61967f6





Finally, we need to apply base-58 encoding to the raw address. I’ve found an excellent implementation of  this encoding. I’ve directly adapted it.

address = base58.b58encode(hexStringToByte(address))
print("this is your bitcoin address:",str(address)[2:len(address)-2])

Bitcoin address calculation is finally over. You can send and receice bitcoins if you have this kind of address.

this is your bitcoin address: 16UwLL9Risc3QfPqBUvKofHmBQ7wM

bitcoin-community
An address refers to unique identity in bitcoin

Conclusion

So, we’ve picked up just a really random private key. Then, calculate public key from known private key. After then, we’ve applied several hash algorithms to public key and retrieved our bitcoin address. Additionally, we’ll sign every transaction we’ve involved in with our private key whereas bitcoin network users verify these transactions with our public key.

bitcoin-address
A Step by Step Bitcoin Address Example

I’ve pushed the source code of this post to the GitHub. Please consider to star the repository if you like this post. Also, I captured this topic as a video lecture.


Support this blog if you do like!

Buy me a coffee      Buy me a coffee


1 Comment

  1. so simplified that anyone including the ones doesnot know how to code can understand the concept very well. thanks for detailed explanation

Comments are closed.