offchainMessageContentUtf8Of65535BytesMax

function offchainMessageContentUtf8Of65535BytesMax<TText>(
    text,
): OffchainMessageContentUtf8Of65535BytesMax<TText>;

Combines asserting that the content of a v0 offchain message is UTF-8 of up to 65535 characters with coercing it to the OffchainMessageContentUtf8Of65535BytesMax type. It's most useful with untrusted input.

Type Parameters

Type Parameter
TText extends string

Parameters

ParameterType
textTText

Returns

OffchainMessageContentUtf8Of65535BytesMax<TText>

Example

import { OffchainMessageContentUtf8Of65535BytesMax, OffchainMessageV0 } from '@solana/offchain-messages';
 
function handleSubmit() {
    // We know only that what the user typed conforms to the `string` type.
    const text: string = textInput.value;
    try {
        const offchainMessage: OffchainMessageV0 = {
            content: OffchainMessageContentUtf8Of65535BytesMax(text),
            // ...
        };
    } catch (e) {
        // `text` turned out not to conform to
        // `OffchainMessageContentUtf8Of65535BytesMax`
    }
}

[!TIP] When starting from known-good UTF-8 content as a string up to 65535 bytes, it's more efficient to typecast it rather than to use the OffchainMessageContentUtf8Of65535BytesMax helper, because the helper unconditionally performs validation on its input.

import { OffchainMessageContentFormat, OffchainMessageV0 } from '@solana/offchain-messages';
 
const offchainMessage: OffchainMessageV0 = {
    /* ... */
    content: Object.freeze({
        format: OffchainMessageContentFormat.UTF8_65535_BYTES_MAX,
        text: '✌🏿cool',
    } as OffchainMessageContentUtf8Of65535BytesMax<'✌🏿cool'>),
};

On this page