assertIsSignatureBytes

function assertIsSignatureBytes(
    putativeSignatureBytes,
): asserts putativeSignatureBytes is SignatureBytes;

Asserts that an arbitrary ReadonlyUint8Array is an Ed25519 signature.

Useful when you receive a ReadonlyUint8Array from an external interface (like the browser wallets' signMessage API) that you expect to represent an Ed25519 signature.

Parameters

ParameterType
putativeSignatureBytesReadonlyUint8Array

Returns

asserts putativeSignatureBytes is SignatureBytes

Example

import { assertIsSignatureBytes } from '@solana/keys';
 
// Imagine a function that verifies a signature.
function verifySignature() {
    // We know only that the input conforms to the `ReadonlyUint8Array` type.
    const signatureBytes: ReadonlyUint8Array = signatureBytesInput;
    try {
        // If this type assertion function doesn't throw, then
        // Typescript will upcast `signatureBytes` to `SignatureBytes`.
        assertIsSignatureBytes(signatureBytes);
        // At this point, `signatureBytes` is a `SignatureBytes` that can be used with `verifySignature`.
        if (!(await verifySignature(publicKey, signatureBytes, data))) {
            throw new Error('The data were *not* signed by the private key associated with `publicKey`');
        }
    } catch (e) {
        // `signatureBytes` turned out not to be a 64-byte Ed25519 signature
    }
}

On this page