This example uses the bip39 JS library so it can be used in the context of a web app. Also, it uses the Stellar SDK, but could be adapted to any wallet SDK I think.
Basically, what I wanted to figure out was how can a crypto account be imported into a wallet application using a 24 word mnemonic phrase. It took me a few hours to figure this out. I relied on the following resources:
- Stellar’s basic wallet tutorial
- https://github.com/chatch/stellar-hd-wallet
- The albedo project’s account import code
This code snippet something I did in a VueJS project. It restores a public/private key pair from a mnemonic phrase.
import * as stellarSdk from "stellar-sdk";
import * as bip39 from "bip39";
// . . .
const phrase = '' // set to a 24 word prhase from user input
const wordlist = bip39.wordlists["english"];
const rawSecret = bip39.mnemonicToEntropy(phrase, wordlist);
const secret = stellarSdk.Keypair.fromRawEd25519Seed(
Buffer.from(rawSecret, "hex")
).secret();
console.log(secret);