|
| 1 | +import { bitsToBytes, bytesToBits, integersToBits } from "../bits/bitOps.js"; |
| 2 | +import { WORD_COUNTS } from "../constants/bip39.js"; |
| 3 | +import { sha256 } from "../crypto/crypto.js"; |
| 4 | +import { ErrorCode } from "../errors/errorCodes.js"; |
| 5 | +import { parseMnemonicWordsStrict } from "../parser/strictMnemonic.js"; |
| 6 | +import { loadEnglishWordlist } from "./englishWordlist.js"; |
| 7 | + |
| 8 | +export class MnemonicToEntropyError extends Error { |
| 9 | + code: ErrorCode; |
| 10 | + |
| 11 | + constructor(code: ErrorCode, message: string) { |
| 12 | + super(message); |
| 13 | + this.code = code; |
| 14 | + this.name = "MnemonicToEntropyError"; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +export class InvalidMnemonicFormatError extends MnemonicToEntropyError { |
| 19 | + constructor(message = "Invalid mnemonic format") { |
| 20 | + super(ErrorCode.ERR_INVALID_MNEMONIC_FORMAT, message); |
| 21 | + this.name = "InvalidMnemonicFormatError"; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +export class InvalidWordCountError extends MnemonicToEntropyError { |
| 26 | + constructor(message = "Invalid word count") { |
| 27 | + super(ErrorCode.ERR_INVALID_WORD_COUNT, message); |
| 28 | + this.name = "InvalidWordCountError"; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export class WordNotInListError extends MnemonicToEntropyError { |
| 33 | + constructor(message = "Word not in list") { |
| 34 | + super(ErrorCode.ERR_WORD_NOT_IN_LIST, message); |
| 35 | + this.name = "WordNotInListError"; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export class ChecksumMismatchError extends MnemonicToEntropyError { |
| 40 | + constructor(message = "Checksum mismatch") { |
| 41 | + super(ErrorCode.ERR_CHECKSUM_MISMATCH, message); |
| 42 | + this.name = "ChecksumMismatchError"; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const isValidWordCount = (count: number): boolean => |
| 47 | + (WORD_COUNTS as readonly number[]).includes(count); |
| 48 | + |
| 49 | +const checksumBitsForWordCount = (wordCount: number): number => |
| 50 | + wordCount === 0 ? 0 : (wordCount * 11) / 33; |
| 51 | + |
| 52 | +const arraysEqual = (a: number[], b: number[]): boolean => |
| 53 | + a.length === b.length && a.every((value, index) => value === b[index]); |
| 54 | + |
| 55 | +export const mnemonicToEntropy = (input: string | string[]): Uint8Array => { |
| 56 | + const parsed = parseMnemonicWordsStrict(input); |
| 57 | + if (!parsed.ok) { |
| 58 | + throw new InvalidMnemonicFormatError(); |
| 59 | + } |
| 60 | + |
| 61 | + const { words } = parsed; |
| 62 | + const wordCount = words.length; |
| 63 | + if (!isValidWordCount(wordCount)) { |
| 64 | + throw new InvalidWordCountError(); |
| 65 | + } |
| 66 | + |
| 67 | + const { wordToIndex } = loadEnglishWordlist(); |
| 68 | + const indices = words.map((word) => { |
| 69 | + const index = wordToIndex.get(word); |
| 70 | + if (index === undefined) { |
| 71 | + throw new WordNotInListError(`Word not in list: ${word}`); |
| 72 | + } |
| 73 | + return index; |
| 74 | + }); |
| 75 | + |
| 76 | + const bits = integersToBits(indices, 11); |
| 77 | + const checksumBits = checksumBitsForWordCount(wordCount); |
| 78 | + const entropyBits = bits.length - checksumBits; |
| 79 | + const entropyBitArray = bits.slice(0, entropyBits); |
| 80 | + const checksumBitArray = bits.slice(entropyBits); |
| 81 | + const entropy = bitsToBytes(entropyBitArray); |
| 82 | + |
| 83 | + const expectedChecksum = bytesToBits(sha256(entropy)).slice(0, checksumBits); |
| 84 | + if (!arraysEqual(checksumBitArray, expectedChecksum)) { |
| 85 | + throw new ChecksumMismatchError(); |
| 86 | + } |
| 87 | + |
| 88 | + return entropy; |
| 89 | +}; |
0 commit comments