Refactor, Added documentation, moved from avj-cli to avj, update script and added tests
This commit is contained in:
22
source/template/identifier.ts
Normal file
22
source/template/identifier.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
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);
|
||||
};
|
||||
Reference in New Issue
Block a user