mcard-js
Version:
MCard - Content-addressable storage with cryptographic hashing, handle resolution, and vector search for Node.js and browsers
46 lines • 1.7 kB
JavaScript
import { EVENT_CONSTANTS, ALGORITHM_HIERARCHY } from './constants';
import { GTime } from './GTime';
import { HashValidator } from './hash/HashValidator';
/**
* Generate a collision event for the given card.
*/
export async function generateCollisionEvent(card) {
const currentHashFunction = GTime.getHashAlgorithm(card.g_time);
const nextAlgo = nextHashFunction(currentHashFunction);
// Compute upgraded hash
const upgradedHash = await HashValidator.computeHash(card.content, nextAlgo);
const event = {
[]: EVENT_CONSTANTS.COLLISION_EVENT_TYPE,
[]: card.hash,
[]: card.g_time,
[]: card.g_time, // Using original card's time as per Python logic reference
[]: card.content.length,
[]: nextAlgo,
[]: upgradedHash
};
return JSON.stringify(event);
}
/**
* Generate a duplication event for the given card.
*/
export function generateDuplicationEvent(card) {
const event = {
[]: EVENT_CONSTANTS.DUPLICATE_EVENT_TYPE,
[]: card.hash,
[]: card.g_time
};
return JSON.stringify(event);
}
/**
* Get the next hash function in the hierarchy.
*/
function nextHashFunction(current) {
const currentLower = current.toLowerCase();
const entry = ALGORITHM_HIERARCHY[currentLower];
if (entry && entry.next) {
return entry.next;
}
// Fallback logic
return 'sha256';
}
//# sourceMappingURL=EventProducer.js.map