anon-identity
Version:
Decentralized identity framework with DIDs, Verifiable Credentials, and privacy-preserving selective disclosure
192 lines • 7.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageFactory = void 0;
const types_1 = require("./types");
const message_protocol_1 = require("./message-protocol");
/**
* Factory class for creating common message types
*/
class MessageFactory {
/**
* Creates a delegation request message
*/
static createDelegationRequest(from, to, requestedScopes, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.DELEGATION_REQUEST, from, to, {
requestedScopes,
serviceDID: options.serviceDID,
duration: options.duration,
purpose: options.purpose,
constraints: options.constraints
}, { replyTo: options.replyTo });
}
/**
* Creates a delegation grant message
*/
static createDelegationGrant(from, to, credential, grantedScopes, expiresAt, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.DELEGATION_GRANT, from, to, {
credential,
grantedScopes,
expiresAt,
limitations: options.limitations
}, { replyTo: options.replyTo });
}
/**
* Creates a delegation deny message
*/
static createDelegationDeny(from, to, reason, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.DELEGATION_DENY, from, to, {
reason,
violations: options.violations,
suggestedScopes: options.suggestedScopes,
retryAfter: options.retryAfter
}, { replyTo: options.replyTo });
}
/**
* Creates a status query message
*/
static createStatusQuery(from, to, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.QUERY_STATUS, from, to, {
includeChain: options.includeChain,
includeScopes: options.includeScopes,
includeMetrics: options.includeMetrics
}, { replyTo: options.replyTo });
}
/**
* Creates a status response message
*/
static createStatusResponse(from, to, status, delegationDepth, remainingDepth, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.RESPONSE_STATUS, from, to, {
status,
delegationDepth,
remainingDepth,
scopes: options.scopes,
metrics: options.metrics
}, { replyTo: options.replyTo });
}
/**
* Creates a ping message
*/
static createPing(from, to, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.PING, from, to, {
timestamp: options.timestamp || new Date()
}, {
replyTo: options.replyTo,
metadata: options.metadata
});
}
/**
* Creates a pong message (response to ping)
*/
static createPong(from, to, originalPingId, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.PONG, from, to, {
timestamp: new Date(),
agentStatus: options.agentStatus || 'active',
responseTime: options.responseTime
}, {
replyTo: originalPingId,
metadata: options.metadata
});
}
/**
* Creates an error message
*/
static createError(from, to, error, originalMessageId, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.ERROR, from, to, {
error,
errorCode: options.errorCode,
originalMessageId,
details: options.details,
timestamp: new Date()
}, { replyTo: options.replyTo || originalMessageId });
}
/**
* Creates an acknowledgment message
*/
static createAck(from, to, originalMessageId, status = 'received', options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.ACK, from, to, {
status,
originalMessageId,
timestamp: new Date(),
details: options.details
}, {
replyTo: originalMessageId,
metadata: options.metadata
});
}
/**
* Creates a revocation notification message
*/
static createRevocationNotification(from, to, revokedAgentDID, reason, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.NOTIFY_REVOCATION, from, to, {
revokedAgentDID,
reason,
effectiveDate: options.effectiveDate || new Date(),
cascading: options.cascading || false
}, { metadata: options.metadata });
}
/**
* Creates an expiration notification message
*/
static createExpirationNotification(from, to, agentDID, expirationDate, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.NOTIFY_EXPIRATION, from, to, {
agentDID,
expirationDate,
gracePeriod: options.gracePeriod,
autoRenew: options.autoRenew || false,
notificationTime: new Date()
}, { metadata: options.metadata });
}
/**
* Creates a policy change notification message
*/
static createPolicyChangeNotification(from, to, policyId, changeType, options = {}) {
return message_protocol_1.MessageProtocol.createMessage(types_1.AgentMessageType.NOTIFY_POLICY_CHANGE, from, to, {
policyId,
changeType,
effectiveDate: options.effectiveDate || new Date(),
affectedAgents: options.affectedAgents,
description: options.description,
notificationTime: new Date()
}, { metadata: options.metadata });
}
/**
* Creates a batch of messages with common properties
*/
static createBatch(from, recipients, messageType, payload, options = {}) {
const batchId = options.batchId || `batch_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
return recipients.map(to => {
return message_protocol_1.MessageProtocol.createMessage(messageType, from, to, payload, {
metadata: {
...options.metadata,
batchId,
batchSize: recipients.length
}
});
});
}
/**
* Utility to check if a message is of a specific type with type safety
*/
static isMessageType(message, type) {
return message.type === type;
}
/**
* Extracts common message metadata
*/
static extractMessageInfo(message) {
const now = new Date();
const age = now.getTime() - message.timestamp.getTime();
const isExpired = message.expiresAt ? now > message.expiresAt : false;
return {
id: message.id,
type: message.type,
from: message.from,
to: message.to,
timestamp: message.timestamp,
isExpired,
age
};
}
}
exports.MessageFactory = MessageFactory;
//# sourceMappingURL=message-factory.js.map