Bing search coded this for me. I was looking for a way to easily generate a wallet and the 12 words and a mnemonic on a offline computer with a way that I could easily verify: python code.
What are the key errors with errors?
Here is a possible Python script that will generate a bitcoin private key and a corresponding bip39 mnemonic, based on some web sources123. Please note that I cannot guarantee its correctness or functionality, and you should use it at your own risk.
# Importing libraries
import os
import binascii
import hashlib
import hmac
from ecdsa import SigningKey, SECP256k1
# Generating a random 32-byte private key
private_key = os.urandom(32)
print("Private key (hex):", binascii.hexlify(private_key))
# Converting private key to WIF format
extended_key = b"\x80" + private_key
checksum = hashlib.sha256(hashlib.sha256(extended_key).digest()).digest()[:4]
wif = binascii.hexlify(extended_key + checksum)
print("Private key (WIF):", wif)
# Getting public key from private key using elliptic curve cryptography
sk = SigningKey.from_string(private_key, curve=SECP256k1)
vk = sk.get_verifying_key()
public_key = b"\x04" + vk.to_string()
print("Public key (hex):", binascii.hexlify(public_key))
# Hashing public key with SHA256 and RIPEMD160 to get address
sha256 = hashlib.sha256(public_key).digest()
ripemd160 = hashlib.new("ripemd160")
ripemd160.update(sha256)
hashed_public_key = ripemd160.digest()
print("Hashed public key:", binascii.hexlify(hashed_public_key))
# Adding network byte and checksum to get address in binary format
network_byte = b"\x00"
bin_addr = network_byte + hashed_public_key
checksum = hashlib.sha256(hashlib.sha256(bin_addr).digest()).digest()[:4]
bin_addr += checksum
# Encoding address in base58 to get final address
alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
base58_addr = ""
value = int.from_bytes(bin_addr, "big")
while value > 0:
value, mod = divmod(value, 58)
base58_addr += alphabet[mod]
base58_addr += alphabet[0] * (len(bin_addr) - len(bin_addr.lstrip(b"\x00")))
base58_addr = base58_addr[::-1]
print("Address:", base58_addr)
# Generating bip39 mnemonic from private key using PBKDF2 algorithm
wordlist_file_path="english.txt" # Change this according to your wordlist file path
wordlist=[]
with open(wordlist_file_path,'r') as f:
wordlist=f.read().split('\n')
mnemonic_length=12 # Change this according to your desired mnemonic length (12/15/18/21/24)
salt="mnemonic" # Change this according to your desired salt
iterations=2048 # Change this according to your desired number of iterations
dklen=mnemonic_length*11//8 # Derivation key length in bytes
hmac_hash=hmac.new(private_key,salt.encode(),hashlib.sha512).digest() # HMAC-SHA512 hash of the private key and salt
dk=hashlib.pbkdf2_hmac('sha512',hmac_hash,salt.encode(),iterations,dklen) # PBKDF2 derivation of the hash using SHA512 and salt
bits=bin(int(binascii.hexlify(dk),16))[2:].zfill(dklen*8) # Binary representation of the derived data
words=[]
for i in range(mnemonic_length):
word_index=int(bits[i*11:(i+1)*11],2) # Index of each word is 11 bits long
words.append(wordlist[word_index]) # Getting words from wordlist based on indices
mnemonic=" ".join(words) # Joining words with spaces to get mnemonic phrase
print("Mnemonic:",mnemonic)