import type { Engine } from "@xo-cash/engine"; import type { ScriptHashData, UnspentOutputData } from "@xo-cash/state"; /** * Template and output identifiers resolved from script hash storage. */ export type UnspentOutputMetadata = { templateIdentifier?: string; outputIdentifier?: string; }; export type UnspentOutputWithMetadata = UnspentOutputData & UnspentOutputMetadata; /** * Builds a lookup map from script hash to its stored metadata. */ export const buildScriptHashDataMap = async ( engine: Engine, ): Promise> => { const scriptHashes = await engine.listScriptHashes(); const scriptHashDataByScriptHash = new Map(); for (const scriptHashRow of scriptHashes) { scriptHashDataByScriptHash.set(scriptHashRow.scriptHash, scriptHashRow); } return scriptHashDataByScriptHash; }; /** * Resolves template/output metadata for a single UTXO via its script hash. */ export const getUnspentOutputMetadata = ( utxo: UnspentOutputData, scriptHashDataByScriptHash: Map, ): UnspentOutputMetadata => { const scriptRow = scriptHashDataByScriptHash.get(utxo.scriptHash); if (scriptRow === undefined) { return {}; } return { templateIdentifier: scriptRow.templateIdentifier, outputIdentifier: scriptRow.outputIdentifier, }; }; /** * Returns a UTXO enriched with template/output metadata from script hash storage. */ export const enrichUnspentOutput = ( utxo: UnspentOutputData, scriptHashDataByScriptHash: Map, ): UnspentOutputWithMetadata => ({ ...utxo, ...getUnspentOutputMetadata(utxo, scriptHashDataByScriptHash), });