Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Mnemonic to private key #419

Open
ctrl-Felix opened this issue Jul 29, 2022 · 6 comments
Open

Convert Mnemonic to private key #419

ctrl-Felix opened this issue Jul 29, 2022 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@ctrl-Felix
Copy link

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)

@LimpidCrypto
Copy link
Contributor

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)

@ctrl-Felix
Copy link
Author

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...

@LimpidCrypto
Copy link
Contributor

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.

@ctrl-Felix
Copy link
Author

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.

@LimpidCrypto
Copy link
Contributor

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

@mvadari mvadari added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Aug 15, 2022
@JST5000
Copy link
Contributor

JST5000 commented Aug 15, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants