ebellocchia / bip_utils

Generation of mnemonics, seeds, private/public keys and addresses for different types of cryptocurrencies
MIT License
292 stars 83 forks source link

STELLAR (XLM) address generation mistake #86

Closed customusername1 closed 1 year ago

customusername1 commented 1 year ago

Hello! Im trying to extract address with mnemonic for STELLAR (XLM) by using example Bip44.py I've modify string with bip44_mst_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.**STELLAR**)

Also try different Bip44Changes: bip44_chg_ctx = bip44_acc_ctx.Change(Bip44Changes.CHAIN_EXT)

from bip_utils import Bip39MnemonicGenerator, Bip39SeedGenerator, Bip39WordsNum, Bip44, Bip44Changes, Bip44Coins

ADDR_NUM: int = 1

# Generate random mnemonic
mnemonic = 'soda gap deny remind spawn include multiply weird reunion story escape embrace'
print(f"Mnemonic string: {mnemonic}")
# Generate seed from mnemonic
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()

# Construct from seed
bip44_mst_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.STELLAR)
# Print master key
print(f"Master key (bytes): {bip44_mst_ctx.PrivateKey().Raw().ToHex()}")
print(f"Master key (extended): {bip44_mst_ctx.PrivateKey().ToExtended()}")
print(f"Master key (WIF): {bip44_mst_ctx.PrivateKey().ToWif()}")

# Derive BIP44 account keys: m/44'/0'/0'
bip44_acc_ctx = bip44_mst_ctx.Purpose().Coin().Account(0)
# Derive BIP44 chain keys: m/44'/0'/0'/0
bip44_chg_ctx = bip44_acc_ctx.Change(Bip44Changes.CHAIN_EXT)

# Derive addresses: m/44'/0'/0'/0/i
print("Addresses:")
for i in range(ADDR_NUM):
    bip44_addr_ctx = bip44_chg_ctx.AddressIndex(i)
    print(f"  {i}. Address public key (extended): {bip44_addr_ctx.PublicKey().ToExtended()}")
    print(f"  {i}. Address private key (extended): {bip44_addr_ctx.PrivateKey().ToExtended()}")
    print(f"  {i}. Address: {bip44_addr_ctx.PublicKey().ToAddress()}")

And when we generate address from that mnemonic - we get next address: (with CHAIN_INT) - GAXROIMWJHF5C6X3D7YGZJI3TR7BVQFB3B554ETALAMZW7TR4XMJWLKS (with CHAIN_EXT) - GAOSFKVGUBJB5SXOU62IA6ELWF5NMIKIUQY6SC2XTKBUVZSHJECBJ3JA

But if I use bip39 with phrase - soda gap deny remind spawn include multiply weird reunion story escape embrace I can get correct address - GBSX44RNXZ6VX4E2PSKXHYEKV7LKIZENTMZIUNV2FR6ZOOQ65ASWYYLD This address generate when you creat XLM account, for example, with TrustWallet or other.

So how can I change, maybe Derivation Path to correct m/44'/148'/0' or fix this address generation problems?

ebellocchia commented 1 year ago

Hello, you can either stop at the account level, without deriving the next ones, or derive the default path from the master key using the DeriveDefaultPath method, e.g.:

# Stop at account level
print(f"Address: {bip44_mst_ctx.Purpose().Coin().Account(0).PublicKey().ToAddress()}")
# Use DeriveDefaultPath
print(f"Address: {bip44_mst_ctx.DeriveDefaultPath().PublicKey().ToAddress()}")

Regards, Emanuele

customusername1 commented 1 year ago

Greate, it's works. Thank you, Emanuele! You job is unbelievable!

Issues solved.