xrpl-py
xrpl-py copied to clipboard
Convert Mnemonic to private key
What is the best way to convert a mnemonic to a private key for Ripple? xrpl-py only takes the private key and also with other tools like bip-utils I'm not able to convert the seed to a key xrpl-py is accepting (I don't get a Key with ED and only in uppercase)
The Wallet
object only accepts a secret seed. If you want to use a private key, you would have to overwrite the class fields.
You can use https://github.com/trezor/python-mnemonic to convert mnemonic words to a seed.
wallet = Wallet(seed, 0)
The
Wallet
object only accepts a secret seed. If you want to use a private key, you would have to overwrite the class fields.You can use https://github.com/trezor/python-mnemonic to convert mnemonic words to a seed.
wallet = Wallet(seed, 0)
This wouldn't support derivating the seed and changing the address index, right?
I think mnemonic support is something that would be pretty useful here...
I honestly don't really get where you are coming from. So you have your mnemonic words and want to turn it into a secret seed? That would be possible with the library I suggested. But I have to agree with you, it would be very useful to have mnemonic support here, or at least a utility-function mnemonic_words_to_seed
.
Sorry, it was a bit unclear what I wrote. Mainly what I meant was to derive different keys by using the bip39 implementation. A example in js: https://runkit.com/wietsewind/ripple-mnemonic-wallet-generator
This would mean that you could use the mnemonic and derive a lot of sub addresses from that seed. Furthermore, if I use the tremor library I get a seed that is incompatible with Ripple as it seems. I keep getting ValueError: Invalid character '0'
when I use the seed generated by Trevor.
Alright I hope this will help you as long as there is no real support for mnemonic in this library. I decided to use bip_utils now.
from bip_utils import Bip39SeedGenerator, Bip44, Bip44Coins, Bip44Changes
from xrpl.wallet.main import Wallet
def mnemonic_to_wallet(mnemonic_words: str, index: int) -> Wallet:
SEED_BYTES = Bip39SeedGenerator(mnemonic_words).Generate()
bip44_ctx = Bip44.FromSeed(SEED_BYTES, Bip44Coins.RIPPLE)
bip44_addr_ctx = bip44_ctx.Purpose().Coin().Account(index).Change(Bip44Changes.CHAIN_EXT).AddressIndex(0)
wallet = Wallet.create()
wallet.classic_address = bip44_addr_ctx.PublicKey().ToAddress()
wallet.public_key = bip44_addr_ctx.PublicKey().RawCompressed().__str__().upper()
wallet.private_key = bip44_addr_ctx.PrivateKey().Raw().__str__().upper()
wallet.seed = ""
return wallet
mnemonic = "novel matter final only nice cheese address cradle civil crash great flame struggle consider crowd surface purpose saddle mango endless mixed trial tape wrap"
wallet = mnemonic_to_wallet(mnemonic, 0)
print(
f"""
Address: {wallet.classic_address}
Public key: {wallet.public_key}
Private key: {wallet.private_key}
"""
)
# Address: r9JynAPy1xUEW2bAYK36fy5dKKNNNKK1Z5
# Public key: 0203A564B266EE3F01AADD3A87289DDE215AAC70EF62F9019EE5B14967A370E1A9
# Private key: 0762EED5BA4F378FFA60621C6DEF72F4A0A579112ADA5F5D6B2A35EC27E893A5
If someone would like to make a PR to add this to the library, that would be very appreciated :) (It's just not something we can prioritize right now unfortunately)