UNPKG

mcard-js

Version:

MCard - Content-addressable storage with cryptographic hashing, handle resolution, and vector search for Node.js and browsers

48 lines (46 loc) 1.47 kB
import { ALGORITHM_HIERARCHY, EVENT_CONSTANTS } from "./chunk-KEC7YFG3.js"; import { GTime, HashValidator } from "./chunk-7NKII2JA.js"; import "./chunk-MLKGABMK.js"; // src/model/EventProducer.ts async function generateCollisionEvent(card) { const currentHashFunction = GTime.getHashAlgorithm(card.g_time); const nextAlgo = nextHashFunction(currentHashFunction); const upgradedHash = await HashValidator.computeHash(card.content, nextAlgo); const event = { [EVENT_CONSTANTS.TYPE]: EVENT_CONSTANTS.COLLISION_EVENT_TYPE, [EVENT_CONSTANTS.HASH]: card.hash, [EVENT_CONSTANTS.FIRST_G_TIME]: card.g_time, [EVENT_CONSTANTS.COLLISION_TIME]: card.g_time, // Using original card's time as per Python logic reference [EVENT_CONSTANTS.CONTENT_SIZE]: card.content.length, [EVENT_CONSTANTS.UPGRADED_FUNCTION]: nextAlgo, [EVENT_CONSTANTS.UPGRADED_HASH]: upgradedHash }; return JSON.stringify(event); } function generateDuplicationEvent(card) { const event = { [EVENT_CONSTANTS.TYPE]: EVENT_CONSTANTS.DUPLICATE_EVENT_TYPE, [EVENT_CONSTANTS.HASH]: card.hash, [EVENT_CONSTANTS.DUPLICATE_TIME]: card.g_time }; return JSON.stringify(event); } function nextHashFunction(current) { const currentLower = current.toLowerCase(); const entry = ALGORITHM_HIERARCHY[currentLower]; if (entry && entry.next) { return entry.next; } return "sha256"; } export { generateCollisionEvent, generateDuplicationEvent };