import { AnyCodec, BytesCodec, BytesLike, createBytesCodec, createFixedBytesCodec, FixedBytesCodec, PackParam, UnpackResult, } from "./base"; import { bytify, hexify } from "./bytes"; import { byteVecOf, option, table, vector } from "./molecule"; export function createFixedHexBytesCodec( byteLength: number ): FixedBytesCodec { return createFixedBytesCodec({ byteLength, pack: (hex) => bytify(hex), unpack: (buf) => hexify(buf), }); } /** * placeholder codec, generally used as a placeholder * ``` * // for example, when some BytesOpt is not used, it will be filled with this codec * // option BytesOpt (Bytes); * const UnusedBytesOpt = UnknownOpt * ``` */ // export const UnusedOpt = option(Unknown); // vector Bytes export const Bytes = byteVecOf({ pack: bytify, unpack: hexify }); export const BytesOpt = option(Bytes); export const BytesVec = vector(Bytes); export const Byte32 = createFixedHexBytesCodec(32); export const Byte32Vec = vector(Byte32); export function WitnessArgsOf< LockCodec extends AnyCodec, InputTypeCodec extends AnyCodec, OutputTypeCodec extends AnyCodec >(payload: { lock: LockCodec; input_type: InputTypeCodec; output_type: OutputTypeCodec; }): BytesCodec< { lock?: UnpackResult; input_type?: UnpackResult; output_type?: UnpackResult; }, { lock?: PackParam; input_type?: PackParam; output_type?: PackParam; } > { return table( { lock: option(byteVecOf(payload.lock)), input_type: option(byteVecOf(payload.input_type)), output_type: option(byteVecOf(payload.output_type)), }, ["lock", "input_type", "output_type"] ); } const HexifyCodec = createBytesCodec({ pack: bytify, unpack: hexify, }); /** * * @example * ```ts * // secp256k1 lock witness * WitnessArgs.pack({ lock: '0x' + '00'.repeat(65) }) * ``` */ export const WitnessArgs = WitnessArgsOf({ lock: HexifyCodec, input_type: HexifyCodec, output_type: HexifyCodec, });