ebellocchia / bip_utils

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

Derive XRP address problem #10

Closed sepisoltani closed 3 years ago

sepisoltani commented 3 years ago

Hi . Thanks for the great package. I derive BTC,LTC .... address like below from my seed and it works like a charm . But while I'm change it to produce XRP address it doesn't work.

import binascii
from bip_utils import Bip44, Bip44Coins, Bip44Changes
from bip_utils import (P2PKH, P2SH, P2WPKH, BchP2PKH, BchP2SH, AtomAddr, AvaxPChainAddr, AvaxXChainAddr, EthAddr, TrxAddr, XrpAddr)
# Seed bytes
seed_bytes = binascii.unhexlify(b"MY SEED")
# Create from seed
bip44_mst = Bip44.FromSeed(seed_bytes, Bip44Coins.TRON)
# Derive account 0 for Bitcoin: m/44'/0'/0'
bip44_acc = bip44_mst.Purpose().Coin().Account(0)
# Derive the external chain: m/44'/0'/0'/0
bip44_change = bip44_acc.Change(Bip44Changes.CHAIN_EXT)

bip44_addr = bip44_change.AddressIndex(20)
# Print extended keys and address
print(bip44_addr.PrivateKey().ToExtended())
print(bip44_addr.PublicKey().ToExtended())
print(bip44_addr.PublicKey().ToAddress())
print(TrxAddr.ToAddress(bip44_addr.PublicKey().RawUncompressed().ToBytes()))

I get this exception belongs to last line of above code :

ValueError: Public uncompressed key is required for Ethereum address

By the way , the address generated in this line successfully :


bip44_addr.PublicKey().ToAddress()

But is not a valid XRP address. ! Please help

ebellocchia commented 3 years ago

Hello, you are using Tron instead of Ripple. There is no need to use the TrxAddr class directly (in general, to use the Addr classes directly) because it is automatically called by the ToAddress function. The error is simply because you have to remove the first byte of the uncompressed key (which is automatically done by ToAddress).

sepisoltani commented 3 years ago

Thanks . I have changed the code to this:


import binascii
from bip_utils import Bip44, Bip44Coins, Bip44Changes
# Seed bytes
seed_bytes = binascii.unhexlify(b"MY SEED")
# Create from seed
bip44_mst = Bip44.FromSeed(seed_bytes, Bip44Coins.RIPPLE)
# Derive account 0 for Bitcoin: m/44'/0'/0'
bip44_acc = bip44_mst.Purpose().Coin().Account(0)
# Derive the external chain: m/44'/0'/0'/0
bip44_change = bip44_acc.Change(Bip44Changes.CHAIN_EXT)

bip44_addr = bip44_change.AddressIndex(20)
# Print extended keys and address
print(bip44_addr.PrivateKey().ToExtended())
print(bip44_addr.PublicKey().ToExtended())
print(bip44_addr.PublicKey().ToAddress())

But as I said, the generated address (rKGF4EmNpgjphTLqxDhq6zAXwkWG9jffEY) is not a valid XRP address. You can check it at https://xrpscan.com/ Is there any problem ?

ebellocchia commented 3 years ago

I'm not very expert in Ripple, but I think it's because the address has zero balance. I tried to generate an address from zero entropy:

mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()
bip44_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.RIPPLE)
bip44_addr_ctx = bip44_ctx.Purpose().Coin().Account(0).Change(Bip44Changes.CHAIN_EXT).AddressIndex(0)
print(bip44_addr_ctx.PublicKey().ToAddress())

The address generated is: rHsMGQEkVNJmpGWs8XUBoTBiAAbwxZN5v3 Which is the same generated by this website: https://iancoleman.io/bip39 And it's the same generated by TrustWallet from that seed. So I don't see a problem in address generation.