XRPLF / xrpl-py

A Python library to interact with the XRP Ledger (XRPL) blockchain
ISC License
145 stars 83 forks source link

Generate XRP Wallet address using 12 seed phrase #631

Closed minhanh1234 closed 10 months ago

minhanh1234 commented 11 months ago

Does anyone have a python code to generate a XRP wallet address using the a 12 seed phrase?

JST5000 commented 11 months ago

My guess is that the 12 seed phrase is a bip39 mnemonic. Unfortunately xrpl-py doesn't currently have support for that, but I believe you can get there by combining other libraries that can decode bip39 values and xrpl-py.

In order to get an account, if you can create a Wallet in xrpl-py, you'll be able to access the account address for it. Account addresses on the XRPL are derived from the public key corresponding to an account's original public/private key.

Here's the steps to turn a bip39 mnemonic into a Wallet in xrpl.js: In xrpl.js, these are the steps used to do that: https://github.com/XRPLF/xrpl.js/blob/bedb1f0f319d364909258d4c48455bded5bf73b8/packages/xrpl/src/Wallet/index.ts#L241-L256

From a quick google, this library seems to handle decoding bip39 mnemonics: https://pypi.org/project/bip39/

Keep in mind that XRPL uses two kinds of encodings for seeds. XRPL seeds usually start with s if encoded with secp256k1 (ex. ss1x3KLrSvfg7irFc1D929WXZ7z9H), and sEd if encoded with ED25519 (ex. sEd7N8SGDaUreeXw3FkRi3Q1okpXATq). If you follow a similar flow to the xrpl.js logic and end up with a different account than you expected, try explicitly setting the algorithm in the options to the other type of encoding. Older seeds tend to use secp256k1 while more recently generated seeds have started defaulting to ED25519 since it's a bit more efficient.

Another path if the above doens't work is to get the public / private keys, and use those to generate an xrpl-py Wallet from it's constructor. I'm not sure exactly how to do that from bip39 mnemonic unfortunately, but it's an alternative.

JST5000 commented 11 months ago

If you do write that logic, it'd be great to open a PR to the library so we can share it more broadly! Happy to try to answer any questions you have to the best of my ability.

JST5000 commented 10 months ago

Closing for now, feel free to re-open if needed.

JST5000 commented 10 months ago

@ckniffen Happened to find a better answer to your question @minhanh1234! - This response by @LimpidCrypto seems to be a way you can resolve your problem I believe: https://github.com/XRPLF/xrpl-py/issues/419#issuecomment-1213204776