import { id } from "../hash/index.js"; import { assertArgument, toUtf8String } from "../utils/index.js"; import { Wordlist } from "./wordlist.js"; const data = "}aE#4A=Yv&co#4N#6G=cJ&SM#66|/Z#4t&kn~46#4K~4q%b9=IR#7l,mB#7W_X2*dl}Uo~7s}Uf&Iw#9c&cw~6O&H6&wx&IG%v5=IQ~8a&Pv#47$PR&50%Ko&QM&3l#5f,D9#4L|/H&tQ;v0~6n]nN> = { zh_cn: null, zh_tw: null } const Checks: Record = { zh_cn: "0x17bcc4d8547e5a7135e365d1ab443aaae95e76d8230c2782c67305d4f21497a1", zh_tw: "0x51e720e90c7b87bec1d70eb6e74a21a449bd3ec9c020b01d3a40ed991b60ce5d" } const codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const style = "~!@#$%^&*_-=[]{}|;:,.()<>?" function loadWords(locale: string): Array { if (_wordlist[locale] != null) { return _wordlist[locale] as Array; } const wordlist = []; let deltaOffset = 0; for (let i = 0; i < 2048; i++) { const s = style.indexOf(data[i * 3]); const bytes = [ 228 + (s >> 2), 128 + codes.indexOf(data[i * 3 + 1]), 128 + codes.indexOf(data[i * 3 + 2]), ]; if (locale === "zh_tw") { const common = s % 4; for (let i = common; i < 3; i++) { bytes[i] = codes.indexOf(deltaData[deltaOffset++]) + ((i == 0) ? 228: 128); } } wordlist.push(toUtf8String(new Uint8Array(bytes))); } // Verify the computed list matches the official list const checksum = id(wordlist.join("\n") + "\n"); /* c8 ignore start */ if (checksum !== Checks[locale]) { throw new Error(`BIP39 Wordlist for ${ locale } (Chinese) FAILED`); } /* c8 ignore stop */ _wordlist[locale] = wordlist; return wordlist; } class LangZh extends Wordlist { constructor(country: string) { super("zh_" + country); } getWord(index: number): string { const words = loadWords(this.locale); assertArgument(index >= 0 && index < words.length, `invalid word index: ${ index }`, "index", index); return words[index]; } getWordIndex(word: string): number { return loadWords(this.locale).indexOf(word); } split(mnemonic: string): Array { mnemonic = mnemonic.replace(/(?:\u3000| )+/g, ""); return mnemonic.split(""); } } export const langZhCn = new LangZh("cn"); export const langZhTw = new LangZh("tw");