Add bch-mnemonic protocol

This commit is contained in:
Jim Hamill
2025-06-27 06:44:14 +00:00
parent 1545255a4a
commit f01699a961
5 changed files with 217 additions and 14 deletions
+33 -10
View File
@@ -87,6 +87,7 @@ export class Mnemonic {
public static fromEntropy(
entropy: Uint8Array,
wordList: Array<string> = bip39WordListEnglish,
passphrase?: string,
) {
const mnemonic = encodeBip39MnemonicNonStandard(entropy, wordList);
@@ -94,7 +95,13 @@ export class Mnemonic {
throw new Error(mnemonic);
}
return this.fromPhrase(mnemonic.phrase, wordList);
// If there is a passphrase, amend it to the whole phrase.
// TODO: Consider whether passphrase should just be a separate parameter. It will probably simplify things.
const finalPhrase = passphrase
? `${mnemonic.phrase} ${passphrase}`
: mnemonic.phrase;
return this.fromPhrase(finalPhrase, wordList);
}
/**
@@ -283,11 +290,13 @@ export class Mnemonic {
*
* @returns Object mapping language names to their word lists for all matching languages
*/
public static detectLanguages(
public static detectLanguages<
WordLists extends Record<string, Array<string>>,
>(
words: Array<string>,
wordLists: { [name: string]: Array<string> },
wordLists: WordLists,
allowPartialMatches = false,
): { [name: string]: Array<string> } {
): Partial<WordLists> {
// Define our match function.
const isWordMatch = (word: string, wordList: Array<string>): boolean => {
if (allowPartialMatches && word.length === 0) return true;
@@ -303,7 +312,7 @@ export class Mnemonic {
Object.entries(wordLists).filter(([, wordList]) =>
words.every((word) => isWordMatch(word, wordList)),
),
);
) as Partial<WordLists>;
}
/**
@@ -326,10 +335,9 @@ export class Mnemonic {
* const completions = eligibleWords.filter(word => word.startsWith('acc'));
* ```
*/
public static getEligibleWords(
words: Array<string>,
wordLists: { [name: string]: Array<string> },
): Array<string> {
public static getEligibleWords<
WordLists extends Record<string, Array<string>>,
>(words: Array<string>, wordLists: WordLists): Array<string> {
// Given the words provided, find eligible languages/word-lists.
const matchingLanguages = this.detectLanguages(words, wordLists, true);
@@ -428,7 +436,7 @@ export class Mnemonic {
// And the BIP39 spec warns against their usage:
// https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#wordlists
// If we include this in the class, it cannot be tree-shaken by most tooling, so we define a stand-alone function instead.
export function getAllBIP39WordLists() {
export function getAllBIP39WordListsMappedToLanguageNames() {
return {
'Chinese Simplified': bip39WordListChineseSimplified,
'Chinese Traditional': bip39WordListChineseTraditional,
@@ -442,3 +450,18 @@ export function getAllBIP39WordLists() {
Spanish: bip39WordListSpanish,
};
}
export function getAllBIP39WordListsMappedToLanguageCodes() {
return {
'zh-CN': bip39WordListChineseSimplified, // Mainland China uses Simplified
'zh-TW': bip39WordListChineseTraditional, // Taiwan uses Traditional
cs: bip39WordListCzech,
en: bip39WordListEnglish,
fr: bip39WordListFrench,
it: bip39WordListItalian,
ja: bip39WordListJapanese,
ko: bip39WordListKorean,
pt: bip39WordListPortuguese,
es: bip39WordListSpanish,
};
}