I have created a tool which generates seed phases and then convert it to Litecoin p2pkh address, I want to generate bech32 address along with p2pkh addresses. Can someone please help by modifying my given code. I’m attaching the files, you can run them by running main.py.
Main.py:
import subprocess
import sys
def main():
try:
seedgen_proc = subprocess.Popen([“python”, “seedgen.py”], stdout=subprocess.PIPE)
privatekey_proc = subprocess.Popen([“python”, “privatekey.py”], stdin=seedgen_proc.stdout, stdout=subprocess.PIPE)
while True:
seedgen_output = seedgen_proc.stdout.readline().decode().strip()
if not seedgen_output:
break
privatekey_output = privatekey_proc.stdout.readline().decode().strip()
if not privatekey_output:
break
print(privatekey_output)
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)
finally:
seedgen_proc.terminate()
privatekey_proc.terminate()
if name == “main”:
main()
privatekey.py:
import sys
import hashlib
import base58
from mnemonic import Mnemonic
import bip32utils
def generate_litecoin_address(phrase):
mnemon = Mnemonic(‘english’)
seed = mnemon.to_seed(phrase)
root_key = bip32utils.BIP32Key.fromEntropy(seed, public=False, testnet=False)
# Derivation path for Litecoin: m/44’/2’/0’/0/0 (mainnet)
child_key = root_key.ChildKey(44 + bip32utils.BIP32_HARDEN).ChildKey(2 + bip32utils.BIP32_HARDEN).ChildKey(0 + bip32utils.BIP32_HARDEN).ChildKey(0).ChildKey(0)
litecoin_public_hex = child_key.PublicKey().hex()
hash160 = hashlib.new(‘ripemd160’, hashlib.sha256(bytes.fromhex(litecoin_public_hex)).digest()).digest()
litecoin_address = base58.b58encode_check(b’\x30’ + hash160).decode(‘utf-8’)
return litecoin_address
if name == “main”:
for line in sys.stdin:
phrase = line.strip() # Read phrase from stdin
litecoin_address = generate_litecoin_address(phrase)
print(f"Phrase: {phrase}\nAddress: {litecoin_address}")
sys.stdout.flush() # Flush stdout to ensure immediate printing
Seedgen.py: from bip_utils import Bip39MnemonicGenerator
def generate_mnemonic():
mnemonic = Bip39MnemonicGenerator().FromWordsNumber(12)
return mnemonic
def seed_generator():
while True:
recovery_phrase = generate_mnemonic()
print(recovery_phrase) # Print generated seed
if name == “main”:
seed_generator()