import { Codec, createCodec, metadata, ScaleDecodeError } from "../common/mod.js" export function constant(value: T, codec: Pick, "encode">): Codec export function constant(value: T, pattern?: Uint8Array): Codec export function constant(value: T, c?: Pick, "encode"> | Uint8Array): Codec { const pattern = c && (c instanceof Uint8Array ? c : c.encode(value)) return createCodec({ _metadata: metadata("$.constant", constant, value, ...pattern ? [pattern] : []), // We could set `_staticSize` to `pattern.length`, but in this case it will // usually more efficient to insert `pattern` dynamically, rather than // manually copy the bytes. _staticSize: 0, _encode(buffer) { if (pattern) { buffer.insertArray(pattern) } }, _decode(buffer) { if (pattern) { const got = buffer.array.subarray(buffer.index, buffer.index += pattern.length) for (let i = 0; i < pattern.length; i++) { if (pattern[i] !== got[i]) { throw new ScaleDecodeError(this, buffer, `Invalid pattern; expected ${hex(pattern)}, got ${hex(got)}`) } } } return value }, _assert(assert) { assert.equals(this, value) }, }) } function hex(pattern: Uint8Array) { let str = "0x" for (let i = 0; i < pattern.length; i++) { str += pattern[i]!.toString(16).padStart(2, "0") } return str }