60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
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<Map<string, ScriptHashData>> => {
|
|
const scriptHashes = await engine.listScriptHashes();
|
|
const scriptHashDataByScriptHash = new Map<string, ScriptHashData>();
|
|
|
|
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<string, ScriptHashData>,
|
|
): 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<string, ScriptHashData>,
|
|
): UnspentOutputWithMetadata => ({
|
|
...utxo,
|
|
...getUnspentOutputMetadata(utxo, scriptHashDataByScriptHash),
|
|
});
|