offchainMessageContentRestrictedAsciiOf1232BytesMax

function offchainMessageContentRestrictedAsciiOf1232BytesMax<TText>(
    text,
): OffchainMessageContentRestrictedAsciiOf1232BytesMax<TText>;

Combines asserting that the content of a v0 offchain message is restricted ASCII with coercing it to the OffchainMessageContentRestrictedAsciiOf1232BytesMax type. It's most useful with untrusted input.

Type Parameters

Type Parameter
TText extends string

Parameters

ParameterType
textTText

Returns

OffchainMessageContentRestrictedAsciiOf1232BytesMax<TText>

Example

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

[!TIP] When starting from known-good ASCII content as a string, it's more efficient to typecast it rather than to use the offchainMessageContentRestrictedAsciiOf1232BytesMax 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.RESTRICTED_ASCII_1232_BYTES_MAX,
        text: 'Hello world',
    } as OffchainMessageContentRestrictedAsciiOf1232BytesMax<'Hello world'>),
};

On this page