feat(wallet): derive PrivateKey/Wallet from BIP-39 mnemonic#429
Open
ozpool wants to merge 1 commit into
Open
Conversation
Closes solana-foundation#297. Two community members have requested an in-tree way to recover a Solana key from a mnemonic; today every consumer rolls their own combination of a BIP-39 library and SLIP-0010 derivation, with the extra friction that BIP-32 is secp256k1-only and does not apply to ed25519. This adds: - PrivateKeyFromMnemonic(mnemonic, passphrase) — Phantom default path m/44'/501'/0'/0' - PrivateKeyFromMnemonicAtPath(mnemonic, passphrase, path) - PrivateKeyFromSeedAtPath(seed, path) — for callers with their own seed - NewWalletFromMnemonic(mnemonic, passphrase) - SolanaDerivationPath constant ("m/44'/501'/0'/0'") Mnemonic -> seed uses tyler-smith/go-bip39 (the de facto Go BIP-39 implementation, also used by btcsuite/btcd/btcutil/hdkeychain and cosmos-sdk forks). Seed -> ed25519 key uses an in-tree SLIP-0010 implementation, since SLIP-0010 ed25519 only defines hardened derivation and is small enough not to warrant pulling in a generic HD-key library. Final seed -> keypair goes through the existing oasisprotocol/curve25519-voi ed25519, so the result is bit-identical to NewRandomPrivateKey output and works with the existing Sign / Validate / PublicKey methods unchanged. Tests cover: - the official SLIP-0010 ed25519 vectors (vector 1: m, m/0', m/0'/1', m/0'/1'/2', m/0'/1'/2'/2', m/0'/1'/2'/2'/1000000000'; vector 2: m, m/0') from satoshilabs/slips - derivation-path parsing (empty, default, h/H/' suffixes, non-hardened rejection, malformed input) - mnemonic validation, determinism, passphrase sensitivity, path sensitivity, and end-to-end Sign/Verify against the derived key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #297. Two community members have asked for an in-tree way to recover a Solana key from a mnemonic. Today every consumer rolls their own combination of a BIP-39 library and SLIP-0010 derivation, with the extra friction that BIP-32 is secp256k1-only and does not apply to ed25519.
This adds:
PrivateKeyFromMnemonic(mnemonic, passphrase)— uses the Phantom default pathm/44'/501'/0'/0'PrivateKeyFromMnemonicAtPath(mnemonic, passphrase, path)— custom SLIP-0010 pathPrivateKeyFromSeedAtPath(seed, path)— for callers that already have their own seed bytesNewWalletFromMnemonic(mnemonic, passphrase)— convenience wrapper around the existingWallettypeSolanaDerivationPathconstant ("m/44'/501'/0'/0'")Design notes
github.com/tyler-smith/go-bip39(the de facto Go BIP-39 implementation, also used bybtcsuite/btcd/btcutil/hdkeychainand Cosmos SDK forks). Single new direct dependency.oasisprotocol/curve25519-voied25519, so the result is bit-identical toNewRandomPrivateKeyoutput and works with the existingSign/Validate/PublicKeymethods unchanged.Test plan
m,m/0',m/0'/1',m/0'/1'/2',m/0'/1'/2'/2',m/0'/1'/2'/2'/1000000000'm,m/0'h/H/'suffixes, non-hardened rejection, malformed input)go vet ./...(only pre-existing warnings inrpc/ws/examples/...)gofmt -lclean