synkrokonn-dev
Version:
Plugin-based cross-chain orchestration middleware for Web3 enterprise automation.
48 lines (47 loc) • 1.48 kB
JavaScript
import crypto from 'crypto';
export class HTLCHandler {
constructor() {
this.htlcStore = {};
}
createHTLC(sender, receiver, secret, timelock) {
const hashlock = crypto.createHash('sha256').update(secret).digest('hex');
const htlcId = crypto.randomUUID();
this.htlcStore[htlcId] = {
hashlock,
timelock: Date.now() + timelock,
sender,
receiver
};
console.log(`[HTLC] Created HTLC ${htlcId} with hash ${hashlock}`);
return htlcId;
}
claimHTLC(htlcId, secret) {
const htlc = this.htlcStore[htlcId];
if (!htlc)
return false;
const hash = crypto.createHash('sha256').update(secret).digest('hex');
if (hash !== htlc.hashlock)
return false;
if (Date.now() > htlc.timelock)
return false;
htlc.fulfilled = true;
htlc.secret = secret;
console.log(`[HTLC] Claimed HTLC ${htlcId} by ${htlc.receiver}`);
return true;
}
refundHTLC(htlcId) {
const htlc = this.htlcStore[htlcId];
if (!htlc)
return false;
if (htlc.fulfilled)
return false;
if (Date.now() < htlc.timelock)
return false;
delete this.htlcStore[htlcId];
console.log(`[HTLC] Refunded HTLC ${htlcId} to ${htlc.sender}`);
return true;
}
getHTLC(htlcId) {
return this.htlcStore[htlcId];
}
}