createDecoderThatConsumesEntireByteArray

function createDecoderThatConsumesEntireByteArray<T>(
    decoder,
): Decoder<T>;

Create a Decoder that asserts that the bytes provided to decode or read are fully consumed by the inner decoder

Type Parameters

Type ParameterDescription
TThe type of the decoder

Parameters

ParameterTypeDescription
decoderDecoder<T>A decoder to wrap

Returns

Decoder<T>

A new decoder that will throw if provided with a byte array that it does not fully consume

Remarks

Note that this compares the offset after encoding to the length of the input byte array

The offset parameter to decode and read is still considered, and will affect the new offset that is compared to the byte array length

The error that is thrown by the returned decoder is a SolanaError with the code SOLANA_ERROR__CODECS__EXPECTED_DECODER_TO_CONSUME_ENTIRE_BYTE_ARRAY

Example

Create a decoder that decodes a u32 (4 bytes) and ensures the entire byte array is consumed

const decoder = createDecoderThatUsesExactByteArray(getU32Decoder());
decoder.decode(new Uint8Array([0, 0, 0, 0])); // 0
decoder.decode(new Uint8Array([0, 0, 0, 0, 0])); // throws
 
// with an offset
decoder.decode(new Uint8Array([0, 0, 0, 0, 0]), 1); // 0
decoder.decode(new Uint8Array([0, 0, 0, 0, 0, 0]), 1); // throws

On this page