Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 17
feat: added two new utils for mnemonic/english-word-indices conversion#300
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce170e3
feat: added two new utils for mnemonic/english-word-indices conversion
lwin-kyaw 91292ff
chore: updated CHANGELOG
lwin-kyaw 82b7398
fix: fixed lint and test
lwin-kyaw 02e4744
fix: fixed CHANGELOG
lwin-kyaw e6ce3df
chore: updated CHANGELOG
lwin-kyaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { | ||
| convertMnemonicToWordlistIndices, | ||
| uint8ArrayToMnemonic, | ||
| } from './mnemonic'; | ||
| const TWELVE_WORD_MNEMONIC = | ||
| 'bulk riot robust reward museum path chunk health rate soon zone wagon'; | ||
| const TWELVE_WORD_INDICES = [ | ||
| 240, 1489, 1498, 1477, 1165, 1287, 325, 849, 1425, 1658, 2046, 1970, | ||
| ]; | ||
| const TWENTY_FOUR_WORD_MNEMONIC = | ||
| 'abuse boss fly battle rubber wasp afraid hamster guide essence vibrant task banana pencil owner cube social job emotion member joy sting dash trouble'; | ||
| const TWENTY_FOUR_WORD_INDICES = [ | ||
| 9, 209, 719, 154, 1510, 1980, 36, 837, 828, 618, 1947, 1776, 145, 1301, 1265, | ||
| 427, 1647, 960, 582, 1110, 964, 1711, 445, 1864, | ||
| ]; | ||
| /** | ||
| * Encode BIP-39 word indices as little-endian Uint16 bytes. | ||
| * | ||
| * @param indices - Wordlist indices to encode. | ||
| * @returns The encoded bytes. | ||
| */ | ||
| function indicesToBytes(indices: number[]): Uint8Array { | ||
| return new Uint8Array(new Uint16Array(indices).buffer); | ||
| } | ||
| describe('uint8ArrayToMnemonic', () => { | ||
| it('throws for an empty array', () => { | ||
| expect(() => uint8ArrayToMnemonic(new Uint8Array())).toThrow( | ||
| 'The method uint8ArrayToMnemonic expects a non-empty array', | ||
| ); | ||
| }); | ||
| it('returns a 12-word mnemonic', () => { | ||
| expect(uint8ArrayToMnemonic(indicesToBytes(TWELVE_WORD_INDICES))).toBe( | ||
| TWELVE_WORD_MNEMONIC, | ||
| ); | ||
| }); | ||
| it('returns a 24-word mnemonic', () => { | ||
| expect(uint8ArrayToMnemonic(indicesToBytes(TWENTY_FOUR_WORD_INDICES))).toBe( | ||
| TWENTY_FOUR_WORD_MNEMONIC, | ||
| ); | ||
| }); | ||
| it('interprets bytes as little-endian word indices', () => { | ||
| expect( | ||
| uint8ArrayToMnemonic( | ||
| new Uint8Array([ | ||
| 240, 0, 209, 5, 218, 5, 197, 5, 141, 4, 7, 5, 69, 1, 81, 3, 145, 5, | ||
| 122, 6, 254, 7, 178, 7, | ||
| ]), | ||
| ), | ||
| ).toBe(TWELVE_WORD_MNEMONIC); | ||
| }); | ||
| }); | ||
| describe('convertMnemonicToWordlistIndices', () => { | ||
| it('converts a 12-word mnemonic', () => { | ||
| expect( | ||
| convertMnemonicToWordlistIndices(TWELVE_WORD_MNEMONIC), | ||
| ).toStrictEqual(indicesToBytes(TWELVE_WORD_INDICES)); | ||
| }); | ||
| it('converts a 24-word mnemonic', () => { | ||
| expect( | ||
| convertMnemonicToWordlistIndices(TWENTY_FOUR_WORD_MNEMONIC), | ||
| ).toStrictEqual(indicesToBytes(TWENTY_FOUR_WORD_INDICES)); | ||
| }); | ||
| it('encodes an unknown word as the Uint16 representation of -1', () => { | ||
| const mnemonicWithInvalidWord = | ||
| 'bulk riot robust reward notaword path chunk health rate soon zone wagon'; | ||
| const result = convertMnemonicToWordlistIndices(mnemonicWithInvalidWord); | ||
| const indices = new Uint16Array(result.buffer); | ||
| expect(Array.from(indices)).toStrictEqual([ | ||
| 240, 1489, 1498, 1477, 65535, 1287, 325, 849, 1425, 1658, 2046, 1970, | ||
| ]); | ||
| }); | ||
| it('round-trips with uint8ArrayToMnemonic', () => { | ||
| expect( | ||
| uint8ArrayToMnemonic( | ||
| convertMnemonicToWordlistIndices(TWELVE_WORD_MNEMONIC), | ||
| ), | ||
| ).toBe(TWELVE_WORD_MNEMONIC); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { wordlist } from '@metamask/scure-bip39/dist/wordlists/english'; | ||
| /** | ||
| * Transform a typed array containing mnemonic data to the seed phrase. | ||
| * @param uint8Array - Typed array containing mnemonic data. | ||
| * @returns The seed phrase. | ||
| */ | ||
| export function uint8ArrayToMnemonic(uint8Array: Uint8Array): string { | ||
| if (uint8Array.length === 0) { | ||
| throw new Error( | ||
| 'The method uint8ArrayToMnemonic expects a non-empty array', | ||
| ); | ||
| } | ||
| const recoveredIndices = Array.from( | ||
| new Uint16Array(new Uint8Array(uint8Array).buffer), | ||
| ); | ||
| return recoveredIndices.map((i) => wordlist[i]).join(' '); | ||
| } | ||
| /** | ||
| * Encodes a BIP-39 mnemonic as the indices of words in the English BIP-39 wordlist. | ||
| * | ||
| * @param mnemonic - The BIP-39 mnemonic. | ||
| * @returns The Unicode code points for the seed phrase formed from the words in the wordlist. | ||
| */ | ||
| export function convertMnemonicToWordlistIndices(mnemonic: string): Uint8Array { | ||
| const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word)); | ||
| return new Uint8Array(new Uint16Array(indices).buffer); | ||
| } | ||
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
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.