Kit
Functions

assertIsBlockhash

assertIsBlockhash(putativeBlockhash): asserts putativeBlockhash is Blockhash

From time to time you might acquire a string, that you expect to validate as a blockhash, from an untrusted network API or user input. Use this function to assert that such an arbitrary string is a base58-encoded blockhash.

Parameters

ParameterType
putativeBlockhashstring

Returns

asserts putativeBlockhash is Blockhash

Example

import { assertIsBlockhash } from '@solana/rpc-types';
 
// Imagine a function that determines whether a blockhash is fresh when a user submits a form.
function handleSubmit() {
    // We know only that what the user typed conforms to the `string` type.
    const blockhash: string = blockhashInput.value;
    try {
        // If this type assertion function doesn't throw, then
        // Typescript will upcast `blockhash` to `Blockhash`.
        assertIsBlockhash(blockhash);
        // At this point, `blockhash` is a `Blockhash` that can be used with the RPC.
        const { value: isValid } = await rpc.isBlockhashValid(blockhash).send();
    } catch (e) {
        // `blockhash` turned out not to be a base58-encoded blockhash
    }
}

On this page