How to generate Litecoin native segwit address using seed phrase?

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()

1 Like

This would be different:

litecoin_address = base58.b58encode_check(b’\x30’ + hash160).decode(‘utf-8’)

This is where you’re encoding an address, and if you want a native segwit address, this needs to change to something else. I’m unfamiliar with base58 lib?

To modify your code to generate bech32 addresses along with p2pkh addresses, you’ll need to make changes in your privatekey.py file. You’ll need to use the litecoin_address variable and convert it to a bech32 address using a library like bitcoinlib. Additionally, ensure to handle the bech32 addresses alongside the p2pkh addresses in your main.py file to print both types of addresses.