0% found this document useful (0 votes)
92 views

LAB 2: Generating Private, Public Keys and Addresses

This document provides instructions for generating a Bitcoin wallet by creating a private key, public key, and Bitcoin address. It explains: 1) How to generate a private key using Python code which produces a 256-bit random number represented by 64 hexadecimal digits. 2) How to derive a public key from the private key using elliptic curve multiplication. Python code is provided to generate both keys. 3) How to generate a Bitcoin address by applying SHA-256 and RIPEMD-160 hashes to the public key and encoding it in Base58 format. Python code generates the private key, public key, and Bitcoin address. 4) How to verify the checksum of the Bitcoin address by converting it to
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

LAB 2: Generating Private, Public Keys and Addresses

This document provides instructions for generating a Bitcoin wallet by creating a private key, public key, and Bitcoin address. It explains: 1) How to generate a private key using Python code which produces a 256-bit random number represented by 64 hexadecimal digits. 2) How to derive a public key from the private key using elliptic curve multiplication. Python code is provided to generate both keys. 3) How to generate a Bitcoin address by applying SHA-256 and RIPEMD-160 hashes to the public key and encoding it in Base58 format. Python code generates the private key, public key, and Bitcoin address. 4) How to verify the checksum of the Bitcoin address by converting it to
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB 2 : Generating private, public keys and addresses

Part I : Generating private keys


1. Write the following python code and execute it:
from bitcoin import *
my_private_key = random_key()
print(my_private_key)
This generate random numbers of 256 bits, known as private keys. It consists of 64
hexadecimal digitis, each of 4 bits.

Part II : Generating a pair of keys (private and public)


Public keys are derived from the private keys using the elliptic Curve formula:
public_key=private_key*G,
G is the generator of the Elliptic Curve.
The public key is a point of the curve, with two coordinates X and Y.
2. Write the following python code:
#import bitcoin
from bitcoin import *
#Generate a private key
my_private_key=random_key()
#display the private key
print("Private Key:%s\n" %my_private_key)
#Generate a public key, derived from private key
my_public_key=privtopub(my_private_key)
#display the public key
print("Public Key:%s\n" %my_public_key)

3. Run the code

Notice that the public key is larger than the private key. Why?
Part III: Generating a bitcoin address
4. Write the following code:
#import bitcoin
from bitcoin import *
#Generate a private key
my_private_key=random_key()
#display the private key
print("Private Key:%s\n" %my_private_key)
#Generate a public key, derived from private key
my_public_key=privtopub(my_private_key)
#display the public key
print("Public Key:%s\n" %my_public_key)
#Generate a Bitcoin Address
my_bitcoin_address=pubtoaddr(my_public_key)
#display the Bitcoin address
print("Bitcoin Address Key:%s\n" %my_bitcoin_address)
5. Run the code:

NB: Here the Bitcoin address is encoded in Base58 format, not in hexadecimal
format
6. Copy the same public key and pass it through a SHA-256 hash function, then
a RIPEMD-160 function:
Here is my public key:
Public
Key:04c2c95e5975ec319c82e7f64ead574a013396c55f0ff41070d66db902ed87b61e
f62e714bb3eb5488ae139fea6d19f05b36bcfce33a4c63f3dbb73af096c98db3
I am using the following website for hash functions:
https://www.fileformat.info/tool/hash.htm

- SHA-256(public_key)=
265a72a68d9e1fd642526b05c0522cbf418e4435838ac5ffd5e59c2b794cea7e
- RIPEMD-160(SHA-256(public_key))=
f3ecd1b6bb62b2bc29b502946276d8b50cc37d3d
7. Convert your Bitcoin address from Base58Check to Hexadecimal using the
following website: https://www.better-converter.com/Encoders-
Decoders/Base58Check-to-Hexadecimal-Decoder

8. Compare the result

The highlighted part is the checksum

Questions can be sent to [email protected]

You might also like