offchainMessageContentUtf8Of1232BytesMax

function offchainMessageContentUtf8Of1232BytesMax<TText>(
    text,
): OffchainMessageContentUtf8Of1232BytesMax<TText>;

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

Type Parameters

Type Parameter
TText extends string

Parameters

ParameterType
textTText

Returns

OffchainMessageContentUtf8Of1232BytesMax<TText>

Example

import { OffchainMessageContentUtf8Of1232BytesMax, 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: OffchainMessageContentUtf8Of1232BytesMax(text),
            // ...
        };
    } catch (e) {
        // `text` turned out not to conform to
        // `OffchainMessageContentUtf8Of1232BytesMax`
    }
}

[!TIP] When starting from known-good UTF-8 content as a string up to 1232 bytes, it's more efficient to typecast it rather than to use the offchainMessageContentUtf8Of1232BytesMax 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_1232_BYTES_MAX,
        text: '✌🏿cool',
    } as OffchainMessageContentUtf8Of1232BytesMax<'✌🏿cool'>),
};

On this page