import binascii import hashlib import base58 import ecdsa def convert_pvk_Hex_to_WIF_Uncompressed(z): # Step 1: Get the private key in extended format, which is a hexadecimal string. private_key_static = z # Step 2: Add '80' in the front to select the MAINNET channel for the Bitcoin address. extended_key = "80" + private_key_static # Step 3: First process SHA-256. first_sha256 = hashlib.sha256(binascii.unhexlify(extended_key)).hexdigest() # Step 4: Second process SHA-256. second_sha256 = hashlib.sha256(binascii.unhexlify(first_sha256)).hexdigest() # Step 5-6: Add checksum info to the end of the extended key. final_key = extended_key + second_sha256[:8] # Step 7: Generate the Wallet Import Format (WIF) by base58 encoding the final key. WIF = base58.b58encode(binascii.unhexlify(final_key)).decode('utf-8') # Step 8: Display the private key in the WIF format for wallet import. print("Private Key WIF Uncompressed: " + WIF) def convert_pvk_Hex_to_WIF_Compressed(z): # Step 1: Get the private key in extended format, which is a hexadecimal string. private_key_static = z # Step 2: Add '80' in the front to select the MAINNET channel for the Bitcoin address. extended_key = "80" + private_key_static + '01' # Step 3: First process SHA-256. first_sha256 = hashlib.sha256(binascii.unhexlify(extended_key)).hexdigest() # Step 4: Second process SHA-256. second_sha256 = hashlib.sha256(binascii.unhexlify(first_sha256)).hexdigest() # Step 5-6: Add checksum info to the end of the extended key. final_key = extended_key + second_sha256[:8] # Step 7: Generate the Wallet Import Format (WIF) by base58 encoding the final key. WIFc = base58.b58encode(binascii.unhexlify(final_key)).decode('utf-8') # Step 8: Display the private key in the WIF format for wallet import. print("Private Key WIF Compressed: " + WIFc) def conv_pvkhex_to_bitcoinaddress_uncompressed(z): zk = ecdsa.SigningKey.from_string(binascii.unhexlify(z), curve=ecdsa.SECP256k1) zk_verify = zk.verifying_key # Result z_public_key = '\04' + zk.verifying_key.to_string().hex() # Add leading zero if the length is odd if len(z_public_key) % 2 != 0: z_public_key = '0' + z_public_key # Validate that z_public_key only contains valid hexadecimal characters if not all(c in string.hexdigits for c in z_public_key): raise ValueError("Invalid hexadecimal character in z_public_key") # Making SHA-256 of pub_key and using it for RIPEMD-160 ripemd160 = hashlib.new('ripemd160') ripemd160.update(hashlib.sha256(binascii.unhexlify(z_public_key)).digest()) ripemd160_result = ripemd160.hexdigest() # Adding network bytes on the start of the result step3 = '00' + ripemd160_result # Making SHA-256 of RIPEMD-160 with network bytes included second_sha256 = hashlib.sha256(binascii.unhexlify(step3)).hexdigest() # Making SHA-256 of the second SHA-256 third_sha256 = hashlib.sha256(binascii.unhexlify(second_sha256)).hexdigest() # Getting the first 4 bytes of the third SHA-256 step6 = third_sha256[:8] # Adding the 4 bytes at the end of Step3 to get the final hex data step7 = step3 + step6 # Making the Base58 encoding of Step7 to get the Bitcoin public address bitcoin_uncompressed_address_std = base58.b58encode(binascii.unhexlify(step7)).decode('utf-8') print("Bitcoin Address Uncompressed: " + bitcoin_uncompressed_address_std) def conv_pvkhex_to_bitcoinaddress_compressed(z): zk = ecdsa.SigningKey.from_string(binascii.unhexlify(z), curve=ecdsa.SECP256k1) zk_verify = zk.verifying_key # Get the ECDSA public key key_hex = zk.verifying_key.to_string().hex() if ord(binascii.unhexlify(key_hex[-2:])) % 2 == 0: # The last byte of the value for Y is even, so add '02' at the beginning public_key_compressed = '02' + key_hex[:64] else: # The last byte of the value for Y is odd, so add '03' at the beginning public_key_compressed = '03' + key_hex[:64] # Making SHA-256 of the compressed public key and RIPEMD-160 of this public_key_in_bytes = binascii.unhexlify(public_key_compressed) sha256_public_key_compressed = hashlib.sha256(public_key_in_bytes).digest() ripemd160 = hashlib.new('ripemd160') ripemd160.update(sha256_public_key_compressed) ripemd160_digest = ripemd160.digest() # Adding network bytes 0x00 public_key_compressed_bitcoin_network = b'00' + ripemd160_digest # Making checksum using SHA-256 twice and taking the first 4 bytes sha256_one = hashlib.sha256(public_key_compressed_bitcoin_network).digest() sha256_two = hashlib.sha256(sha256_one).digest() checksum = sha256_two[:4] # Concatenating network bytes and checksum bitcoin_compressed_address_hex = (public_key_compressed_bitcoin_network + checksum).hex() # Making the Base58 encoding of the compressed address bitcoin_compressed_address = base58.b58encode(binascii.unhexlify(bitcoin_compressed_address_hex)).decode('utf-8') print("Bitcoin Address Compressed: " + bitcoin_compressed_address) pvk_hex = "0081DD3ADE2A43A48B85ECE267C3A18E10C90AEF0166D71F3E95FC51C99EF801" print("Converting pvk: " + pvk_hex) convert_pvk_Hex_to_WIF_Uncompressed(pvk_hex) convert_pvk_Hex_to_WIF_Compressed(pvk_hex) conv_pvkhex_to_bitcoinaddress_uncompressed(pvk_hex) conv_pvkhex_to_bitcoinaddress_compressed(pvk_hex)
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |