StringUtils

Git Source

Title: String Utilities Library

Provides string manipulation utilities for DotNS contracts.

Extends OpenZeppelin's Strings library with additional UTF-8 and conversion helpers.

Note: security-contact: admin@parity.io

Constants

MIN_LITE_SUFFIX_DIGITS

Minimum number of trailing digits required in a lite-person PoP label suffix.

Matches pallet_resources::MIN_LITE_USERNAME_DIGITS to avoid drift between what People Chain emits and what DotNS accepts.

uint256 internal constant MIN_LITE_SUFFIX_DIGITS = 2

MAX_DNS_LABEL_OCTETS

Maximum number of octets in a single DNS label.

RFC 1035 caps each label at 63 octets. Enforced inside @custom:function _isDnsLabel so every public validator (@custom:function isSingleLabel, @custom:function isNamePath,

Note: function: isSingleDotLiteLabel, @custom:function isLitePersonLabel) inherits the bound and oversized labels never reach the registrar.

uint256 internal constant MAX_DNS_LABEL_OCTETS = 63

Functions

strlen

Computes the character length of a UTF-8 encoded string.

Counts Unicode code points, not bytes. Handles multi-byte UTF-8 sequences:

  • 1 byte: 0x00-0x7F (ASCII)
  • 2 bytes: 0xC0-0xDF
  • 3 bytes: 0xE0-0xEF
  • 4 bytes: 0xF0-0xF7
  • 5 bytes: 0xF8-0xFB (rare, outside Unicode standard)
  • 6 bytes: 0xFC-0xFD (rare, outside Unicode standard)
function strlen(string memory value) internal pure returns (uint256 len);

Parameters

NameTypeDescription
valuestringThe UTF-8 encoded string to measure.

Returns

NameTypeDescription
lenuint256The number of Unicode characters in the string.

isSingleLabel

Validates that s is a single canonical DNS label.

Lowercase ASCII letters, digits, and hyphen only; hyphen may not be the first or last character; length must be in (0, MAX_DNS_LABEL_OCTETS]. No dots allowed; use

Note: function: isNamePath for dotted forms. Mirrors the label rules enforced at the registrar.

function isSingleLabel(string calldata value) internal pure returns (bool isValid);

Parameters

NameTypeDescription
valuestringCandidate label.

Returns

NameTypeDescription
isValidboolTrue if value is a canonical DNS label.

isSingleLabelMemory

Memory-location helper for @custom:function isSingleLabel, used where the candidate label is produced by an upstream string transformation (e.g. the output of

Note: function: stripDigits) so callers do not need a calldata round-trip.

function isSingleLabelMemory(string memory value) internal pure returns (bool isValid);

Parameters

NameTypeDescription
valuestringCandidate label held in memory.

Returns

NameTypeDescription
isValidboolTrue if value is a canonical DNS label.

stripDots

Removes dot separators from a dotted label.

Used by the PoP gateway boundary to normalise user-facing name.path input into the flat label expected by pricing and minting.

function stripDots(string calldata value) internal pure returns (string memory stripped);

Parameters

NameTypeDescription
valuestringCandidate dotted label.

Returns

NameTypeDescription
strippedstringLabel with all dots removed.

isSingleDotLiteLabel

Validates the gateway-facing lite input shape: stem.suffix.

Requires exactly one dot separator. The left segment must be a canonical DNS label and the right segment must be digits-only with exactly

Note: constant: MIN_LITE_SUFFIX_DIGITS characters.

function isSingleDotLiteLabel(string calldata value) internal pure returns (bool isValid);

Parameters

NameTypeDescription
valuestringCandidate dotted lite label.

Returns

NameTypeDescription
isValidboolTrue when value matches the gateway lite input shape.

isLitePersonLabel

Validates the lite-person PoP label format: <stem><digits>.

A lite-person label is a single DNS label whose trailing characters are digits, with at least @custom:constant MIN_LITE_SUFFIX_DIGITS digits at the tail. It Mirrors @custom:pallet pallet_resources::MIN_LITE_USERNAME_DIGITS. Lite and public registrations share the same namespace; the gateway strips any separator before calling so the on-chain label is a flat DNS label (e.g. alice42). First-to-mint wins at the ERC721 layer; cross-flow priority on the stripped stem is arbitrated by @custom:function IPopRules.reserveBaseNameForPop. Keeping a single namespace avoids the ambiguity dotli/dweb would otherwise see between andrew.47 (lite) and andrew owning 47 as a subname.

Note: constant: MIN_LITE_SUFFIX_DIGITS trailing digits.

function isLitePersonLabel(string calldata value) internal pure returns (bool isValid);

Parameters

NameTypeDescription
valuestringCandidate label.

Returns

NameTypeDescription
isValidboolTrue if the label is a DNS label with at least

isLitePersonLabelMemory

Memory-location helper for @custom:function isLitePersonLabel, used by controller-side normalisation paths.

Note: constant: MIN_LITE_SUFFIX_DIGITS trailing digits.

function isLitePersonLabelMemory(string memory value) internal pure returns (bool isValid);

Parameters

NameTypeDescription
valuestringCandidate label held in memory.

Returns

NameTypeDescription
isValidboolTrue if the label is a DNS label with at least

_isLitePersonLabel

function _isLitePersonLabel(bytes memory raw) private pure returns (bool isValid);

isNamePath

Validates that s is a dot-separated path of canonical DNS labels.

Each segment between dots must satisfy @custom:function isSingleLabel. Empty segments (leading, trailing, or consecutive dots) fail. Used when callers submit multi-label paths (e.g. alice.dot) rather than bare labels.

function isNamePath(string calldata value) internal pure returns (bool isValid);

Parameters

NameTypeDescription
valuestringCandidate name path.

Returns

NameTypeDescription
isValidboolTrue if every dot-separated segment is a canonical DNS label.

_isDnsLabel

function _isDnsLabel(
    bytes memory label,
    uint256 start,
    uint256 end
)
    private
    pure
    returns (bool isValid);

uintToString

Converts a uint256 to its decimal string representation.

Wraps OpenZeppelin's Strings.toString().

function uintToString(uint256 value) internal pure returns (string memory);

Parameters

NameTypeDescription
valueuint256The unsigned integer to convert.

Returns

NameTypeDescription
<none>stringThe decimal string representation.

addressToHex

Converts an address to its checksummed hexadecimal string representation.

Wraps OpenZeppelin's Strings.toHexString(). Returns lowercase hex with "0x" prefix.

function addressToHex(address account) internal pure returns (string memory);

Parameters

NameTypeDescription
accountaddressThe address to convert.

Returns

NameTypeDescription
<none>stringThe hexadecimal string representation (42 characters including "0x").

bytes32ToString

Converts a bytes32 value to a string, treating it as a null-terminated ASCII string.

Reads bytes until the first null byte (0x00) or end of bytes32. Useful for converting short strings stored in bytes32 back to string type.

function bytes32ToString(bytes32 _bytes32) internal pure returns (string memory);

Parameters

NameTypeDescription
_bytes32bytes32The bytes32 value containing a null-terminated ASCII string.

Returns

NameTypeDescription
<none>stringThe extracted string (up to 32 characters).