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

Add support for bip39 with other wordlists #1804

Open
jacogr opened this issue Apr 12, 2023 · 1 comment
Open

Add support for bip39 with other wordlists #1804

jacogr opened this issue Apr 12, 2023 · 1 comment
Labels
-pr appreciated Help Wanted Tasks open for external contributions, suitable for independent or collaborative work. P2 - Medium Important but not urgent. Enhances functionality and value, scheduled after higher priorities.

Comments

@jacogr
Copy link
Member

jacogr commented Apr 12, 2023

We are pulling in the extra wordlists via #1803

This has been PR-ed previously in #1689 and there has been some discussion. However after giving it some thought, we could actually make this work.

part 1

  1. We need a wordlist option passed to the bip39 functions, e.g. last param of wordlist: string[] = DEFAULT_WORDLIST -

export function mnemonicToEntropy (mnemonic: string): Uint8Array {

export function entropyToMnemonic (entropy: Uint8Array): string {

  1. We change the DEAFULT_WORDLIST usage in these functions so be wordlist instead.
  2. Add actual tests for these in bip39.spec.ts to ensure we are actually ok.

part 2

Now the base generation is ok, but we may still use the wasm versions which doesn't have support for this. To get around it -

  1. Adapt the wasm/js check inside the exposed functions to also take in a last param of wordlist?: string[], onlyJs?: boolean

export function mnemonicValidate (mnemonic: string, onlyJs?: boolean): boolean {
return !hasBigInt || (!onlyJs && isReady())

export function mnemonicToMiniSecret (mnemonic: string, password = '', onlyJs?: boolean): Uint8Array {

export function mnemonicToLegacySeed (mnemonic: string, password = '', onlyJs?: boolean, byteLength: 32 | 64 = 32): Uint8Array {

export function mnemonicToEntropy (mnemonic: string, onlyJs?: boolean): Uint8Array {

export function mnemonicGenerate (numWords: 12 | 15 | 18 | 21 | 24 = 12, onlyJs?: boolean): string {

  1. For these treat the wordlist the same as onlyJs, i.e. when passed in we want to take the JS (non-WASM) route, i.e. it ties into the changes we made in bip39.ts. This looks something like !hasBigInt || (!wordlist && !onlyJs && isReady()) and the param passed on the JS-only path
  2. For all the above add tests...

part 3

Now we also need keyring support -

  1. Make the required wordlist-in changes to addFromMnemonic, addFromUri & createFromUri in
    /**
    * @name createFromUri
    * @summary Creates a Keypair from an suri
    * @description This creates a pair from the suri, but does not add it to the keyring
    */
    public createFromUri (_suri: string, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair {
    // here we only aut-add the dev phrase if we have a hard-derived path
    const suri = _suri.startsWith('//')
    ? `${DEV_PHRASE}${_suri}`
    : _suri;
    const { derivePath, password, path, phrase } = keyExtractSuri(suri);
    let seed: Uint8Array;
    const isPhraseHex = isHex(phrase, 256);
    if (isPhraseHex) {
    seed = hexToU8a(phrase);
    } else {
    const parts = phrase.split(' ');
    if ([12, 15, 18, 21, 24].includes(parts.length)) {
    seed = type === 'ethereum'
    ? mnemonicToLegacySeed(phrase, '', false, 64)
    : mnemonicToMiniSecret(phrase, password);
  2. Add tests...
@jacogr
Copy link
Member Author

jacogr commented Apr 13, 2023

#1805 adds support for the first part above.
#1814 adds support for the second part above.

@TarikGul TarikGul added Help Wanted Tasks open for external contributions, suitable for independent or collaborative work. P2 - Medium Important but not urgent. Enhances functionality and value, scheduled after higher priorities. labels Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-pr appreciated Help Wanted Tasks open for external contributions, suitable for independent or collaborative work. P2 - Medium Important but not urgent. Enhances functionality and value, scheduled after higher priorities.
Projects
Development

No branches or pull requests

2 participants