import type { Serializer } from '@metaplex-foundation/umi-serializers-core'; import { InvalidBaseStringError } from './errors'; /** * A string serializer that uses base16 encoding. * @category Serializers */ export const base16: Serializer = { description: 'base16', fixedSize: null, maxSize: null, serialize(value: string) { const lowercaseValue = value.toLowerCase(); if (!lowercaseValue.match(/^[0123456789abcdef]*$/)) { throw new InvalidBaseStringError(value, 16); } const matches = lowercaseValue.match(/.{1,2}/g); return Uint8Array.from( matches ? matches.map((byte: string) => parseInt(byte, 16)) : [] ); }, deserialize(buffer, offset = 0) { const value = buffer .slice(offset) .reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); return [value, buffer.length]; }, };