Files
xo-cash-utils/source/template/identifier.ts

23 lines
802 B
TypeScript

import { binToHex, sha256, utf8ToBin } from '@bitauth/libauth';
import type { XOTemplate } from '@xo-cash/types';
import { serializeTemplate } from './serialization.ts';
/**
* Generates a deterministic template identifier by hashing the template.
*
* Note: This expects a template that has been validated by `parseTemplate`.
*
* @param template - The template to generate an identifier for.
* @returns The sha256 hex identifier for the template.
*/
export const generateTemplateIdentifier = (template: XOTemplate): string => {
// Serialize the template.
const serializedTemplate = serializeTemplate(template);
// Hash the serialized template.
const hash = sha256.hash(utf8ToBin(serializedTemplate));
// Convert the hash to hex and return it.
return binToHex(hash);
};