Skip to content
IC Reactor

isPrincipalText

isPrincipalText(value): boolean

Defined in: core/src/utils/helper.ts:426

Whether value is the text of a principal: a user, canister or the anonymous principal, as a person pastes it into a form.

It is true exactly when value is the canonical text of a principal of at most 29 bytes, the most the Internet Computer accepts: lowercase, grouped by dashes, with a matching checksum and no whitespace, so trim what a person typed first. Principal.fromText also reads the JSON form {"__principal__":"aaaaa-aa"}, with whitespace around it; this refuses it, so text that passes is the principal’s own text, fit to show, compare or put in a URL. Use it where an input is validated, instead of a try around Principal.fromText. A DisplayReactor then takes the text as it is; a Reactor takes Principal.fromText(text).

It returns a boolean, not a type guard, so a string it refuses is still typed a string afterwards.

unknown

Anything; only a string can pass.

boolean

true for the text of a principal.

import { isPrincipalText } from "@ic-reactor/core"
isPrincipalText("ryjl3-tyaaa-aaaaa-aaaba-cai") // true: a canister
isPrincipalText("aaaaa-aa") // true: the management canister
isPrincipalText("2vxsx-fae") // true: the anonymous principal
isPrincipalText("ryjl3-tyaaa") // false: the checksum does not match
isPrincipalText(" aaaaa-aa") // false: trim first
isPrincipalText("RYJL3-TYAAA-AAAAA-AAABA-CAI") // false: not canonical
isPrincipalText('{"__principal__":"aaaaa-aa"}') // false: JSON, not text