API reference › @evolu/common › Type › MnemonicFromString

const MnemonicFromString: transform(
  "MnemonicFromString",
  String,
  Mnemonic,
  {
    // NFKD first, because some letters that aren't uppercase, such as the
    // modifier letter capital A, decompose to an uppercase ASCII letter.
    from: (value: string) =>
      ok(value.normalize("NFKD").toLowerCase().trim().split(/\s+/u).join(" ")),
    to: (value: Mnemonic) => value,
  },
);

Defined in: packages/common/src/Type.ts:10274

A Mnemonic from text a user typed or pasted.

It converts the text to lowercase NFKD and separates its words with single spaces before validating it, so a mnemonic still restores when a phone's keyboard capitalized its first word, or when it was pasted with line breaks or other spaces between its words. Its encoding is the canonical spelling.

Text that isn't a mnemonic fails with a TransformOutputError of a MnemonicError, which omits the text. As with Mnemonic, a non-string fails with the String error, which keeps the input, and Types built around it keep the input in their own errors.

Example

import {
  assertEqual,
  assertErr,
  assertOk,
  MnemonicFromString,
} from "@evolu/common";

const mnemonic =
  "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";

// As a phone's keyboard capitalized it, with a line break pasted in.
const typed = `Abandon${mnemonic.slice("abandon".length).replace(" ", "\n")}`;
assertOk(MnemonicFromString.fromUnknown(typed), mnemonic);

const invalid = MnemonicFromString.fromUnknown("Abandon abandon");
assertErr(invalid, {
  type: "MnemonicFromString",
  outputError: { type: "Mnemonic" },
});
assertEqual(
  MnemonicFromString.formatError(invalid.error),
  "The value is not a valid English BIP39 mnemonic.",
);