@kya-os/mcp-i
Version:
COMING SOON:Production-ready MCP Identity with automatic registration, key rotation, and optimized performance
103 lines (102 loc) • 3.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyRotationManager = void 0;
const crypto_1 = require("./crypto");
class KeyRotationManager {
constructor(identity, transport, policy = {}) {
this.identity = identity;
this.transport = transport;
this.policy = policy;
this.signatureCount = 0;
this.keyCreatedAt = new Date(identity.registeredAt);
}
checkKeyHealth() {
const now = new Date();
const age = now.getTime() - this.keyCreatedAt.getTime();
const maxAge = this.policy.maxAge || 90 * 24 * 60 * 60 * 1000;
const maxSignatures = this.policy.maxSignatures || 1000000;
const shouldRotate = age > maxAge || this.signatureCount > maxSignatures;
return {
age,
signatureCount: this.signatureCount,
shouldRotate,
lastRotated: this.lastRotatedAt
};
}
incrementSignatureCount() {
this.signatureCount++;
}
async rotateKeys(reason = 'scheduled') {
try {
const newKeyPair = await (0, crypto_1.generateKeyPair)();
const timestamp = Date.now();
const message = `rotate-keys:${this.identity.did}:${timestamp}:${reason}`;
const signature = await (0, crypto_1.sign)(message, this.identity.privateKey);
const rotationRequest = {
newPublicKey: {
type: 'Ed25519VerificationKey2020',
publicKeyBase64: newKeyPair.publicKey
},
rotationReason: reason,
signedStatement: signature,
timestamp
};
const registryUrl = this.getRegistryUrl();
const agentId = this.extractAgentId();
const response = await this.transport.post(`${registryUrl}/api/agents/${agentId}/rotate-key`, rotationRequest, {
headers: {
'Authorization': `DID-Auth ${signature}`,
'Content-Type': 'application/json'
}
});
if (response.data.success) {
this.identity.publicKey = newKeyPair.publicKey;
this.identity.privateKey = newKeyPair.privateKey;
this.lastRotatedAt = new Date();
this.keyCreatedAt = new Date();
this.signatureCount = 0;
return {
success: true,
newKeyId: response.data.newKeyId,
oldKeyId: response.data.oldKeyId,
gracePeriodEnd: new Date(response.data.gracePeriodEnd)
};
}
else {
return {
success: false,
error: response.data.error || 'Key rotation failed'
};
}
}
catch (error) {
return {
success: false,
error: error.message || 'Key rotation error'
};
}
}
setupAutoRotation(callback) {
const checkInterval = 24 * 60 * 60 * 1000;
return setInterval(async () => {
const health = this.checkKeyHealth();
if (health.shouldRotate) {
const result = await this.rotateKeys('auto-rotation');
if (callback) {
callback(result);
}
}
}, checkInterval);
}
getRegistryUrl() {
return 'https://knowthat.ai';
}
extractAgentId() {
if (this.identity.agentId) {
return this.identity.agentId;
}
const parts = this.identity.did.split(':');
return parts[parts.length - 1];
}
}
exports.KeyRotationManager = KeyRotationManager;