/** * Copyright © 2023-2026 Blockchain Commons, LLC * Copyright © 2025-2026 Parity Technologies * * * Bits format * Ported from seedtool-cli-rust/src/formats/bits.rs */ import type { Cli } from "../cli.js"; import type { InputFormat, OutputFormat } from "./format.js"; import { Seed } from "../seed.js"; import { sha256DeterministicRandomString } from "../random.js"; import { dataToInts, digitsToData } from "../util.js"; /** * Binary format handler. * NOT round-trippable: input is entropy source. * Bits 0-1 (compatible with https://iancoleman.io/bip39/). */ export class BitsFormat implements InputFormat, OutputFormat { name(): string { return "bits"; } roundTrippable(): boolean { return false; } processInput(state: Cli): Cli { const input = state.expectInput(); // Syntax check only - validates that all characters are 0-1 digitsToData(input, 0, 1); // Use SHA256 deterministic random to generate seed from input const data = sha256DeterministicRandomString(input, state.count); state.seed = Seed.new(data); return state; } processOutput(state: Cli): string { return dataToInts(state.expectSeed().data(), 0, 1, ""); } }