Kit
Functions

assertIsAddress

assertIsAddress(putativeAddress): asserts putativeAddress is Address<string>

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

Parameters

ParameterType
putativeAddressstring

Returns

asserts putativeAddress is Address<string>

Example

import { assertIsAddress } from '@solana/addresses';
 
// Imagine a function that fetches an account's balance when a user submits a form.
function handleSubmit() {
    // We know only that what the user typed conforms to the `string` type.
    const address: string = accountAddressInput.value;
    try {
        // If this type assertion function doesn't throw, then
        // Typescript will upcast `address` to `Address`.
        assertIsAddress(address);
        // At this point, `address` is an `Address` that can be used with the RPC.
        const balanceInLamports = await rpc.getBalance(address).send();
    } catch (e) {
        // `address` turned out not to be a base58-encoded address
    }
}

On this page