Skip to content

Commit

Permalink
wip2
Browse files Browse the repository at this point in the history
Signed-off-by: Curtish <[email protected]>
  • Loading branch information
curtis-h committed Jan 9, 2025
1 parent d970d47 commit a6744c0
Show file tree
Hide file tree
Showing 74 changed files with 714 additions and 824 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"comma-dangle": 0,
"no-unexpected-multiline": "warn",
"prefer-const": "warn",
"@typescript-eslint/ban-types": "warn",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
24 changes: 0 additions & 24 deletions src/domain/buildingBlocks/Pollux.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/domain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export * from "./buildingBlocks/Apollo";
export * from "./buildingBlocks/Castor";
export * from "./buildingBlocks/Mercury";
export * from "./buildingBlocks/Pluto";
export * from "./buildingBlocks/Pollux";
export * from "./utils/JWT";
4 changes: 2 additions & 2 deletions src/domain/models/VerifiableCredential.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO remove this when removing fn from Agent
import { OEA } from "../../pollux/plugins/oea/types";
import { Claims as ACClaims } from "../../pollux/plugins/anoncreds/types";
import { OEA } from "../../plugins/internal/oea/types";
import { Claims as ACClaims } from "../../plugins/internal/anoncreds/types";

export type PresentationClaims<T extends CredentialType = CredentialType.JWT> =
T extends CredentialType.JWT ? OEA.JWTPresentationClaims :
Expand Down
2 changes: 1 addition & 1 deletion src/domain/protocols/Payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export interface Payload {
}

export namespace Payload {
export const make = (pid: string, data: any) => ({ pid, data });
export const make = (pid: string, data: any): Payload => ({ pid, data });
}
42 changes: 18 additions & 24 deletions src/edge-agent/Agent.Backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ import jweWasm from "jwe-wasm/jwe_rust_bg.wasm";
import * as Domain from "../domain";
import Agent from "./Agent";
import { Version } from "../domain/backup";
// import { isObject, validateSafe } from "../utils";
// import * as Domain from "../domain";
// import Agent from "./Agent";
import { isNil, isObject, notNil, validateSafe } from "../utils";


/**
* define Agent requirements for Backup
*/
// type BackupAgent = Pick<Agent, "apollo" | "pluto" | "pollux" | "seed">;
type BackupAgent = Pick<Agent, "apollo" | "pluto" | "seed">;
type BackupExclude = "messages" | "mediators" | "link_secret";

type MasterKey = Domain.PrivateKey & Domain.ExportableKey.Common & Domain.ExportableKey.JWK & Domain.ExportableKey.PEM;

export type BackupOptions = {
Expand All @@ -33,23 +28,22 @@ export class AgentBackup {
) {}

/**
* Creates a JWE (JSON Web Encryption) containing the backup data stored in Pluto.
* The data can optionally be encrypted using a custom master key, compressed,
* and filtered to exclude specified fields.
*
* @param {BackupOptions} [options] - Optional settings for the backup.
* @param {Version} [options.version] - Specifies the version of the backup data.
* @param {MasterKey} [options.key] - Custom master key used for encrypting the backup.
* @param {boolean} [options.compress] - If true, compresses the JWE using DEFLATE.
* @param {BackupExclude[]} [options.excludes] - Keys to exclude from the backup data
* (e.g., "messages", "mediators", "link_secret"). Arrays are cleared, and strings are set to empty strings.
*
* @returns {Promise<string>} - A promise that resolves to the JWE string.
*
* @see restore - Method to restore data from a JWE string.
*/
* Creates a JWE (JSON Web Encryption) containing the backup data stored in Pluto.
* The data can optionally be encrypted using a custom master key, compressed,
* and filtered to exclude specified fields.
*
* @param {BackupOptions} [options] - Optional settings for the backup.
* @param {Version} [options.version] - Specifies the version of the backup data.
* @param {MasterKey} [options.key] - Custom master key used for encrypting the backup.
* @param {boolean} [options.compress] - If true, compresses the JWE using DEFLATE.
* @param {BackupExclude[]} [options.excludes] - Keys to exclude from the backup data
* (e.g., "messages", "mediators", "link_secret"). Arrays are cleared, and strings are set to empty strings.
*
* @returns {Promise<string>} - A promise that resolves to the JWE string.
*
* @see restore - Method to restore data from a JWE string.
*/
async createJWE(options?: BackupOptions): Promise<string> {
// const backup = await this.Agent.pluto.backup();
let backup = await this.Agent.pluto.backup(options?.version);

if (options?.excludes && Array.isArray(options.excludes)) {
Expand Down Expand Up @@ -85,7 +79,6 @@ export class AgentBackup {
*/
async restore(jwe: string, options?: BackupOptions) {
const masterSk = await this.masterSk(options);
// const masterSk = await this.masterSk();
const jwk = masterSk.to.JWK();
const JWE = await this.getJWE();
const decoded = JWE.decrypt(
Expand Down Expand Up @@ -153,8 +146,9 @@ export class AgentBackup {
* @returns JWK
*/
private async masterSk(options?: BackupOptions) {
if (notNil(options?.key)) {
return options.key;
const optKey = options?.key;
if (notNil(optKey)) {
return optKey;
}

const masterKey = this.Agent.apollo.createPrivateKey({
Expand Down
22 changes: 9 additions & 13 deletions src/edge-agent/Agent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Domain from "../domain";
import Apollo from "../apollo";
import Castor from "../castor";
import Pollux from "../pollux";
import { Startable } from "../domain/protocols/Startable";
import { AgentBackup } from "./Agent.Backup";
import { SignWithDID } from "./didFunctions/Sign";
Expand All @@ -10,6 +9,7 @@ import { FetchApi } from "./helpers/FetchApi";
import { Task } from "../utils/tasks";
import { notNil } from "../utils";
import { RevealCredentialFields } from "./helpers/RevealCredentialFields";
import { RunProtocol } from "./helpers/RunProtocol";

/**
* Edge agent implementation
Expand All @@ -20,7 +20,6 @@ import { RevealCredentialFields } from "./helpers/RevealCredentialFields";
*/
export default class Agent extends Startable.Controller {
public backup: AgentBackup;
public readonly pollux: Domain.Pollux;

/**
* Creates an instance of Agent.
Expand All @@ -41,14 +40,6 @@ export default class Agent extends Startable.Controller {
) {
super();
this.backup = new AgentBackup(this);
// this.pollux = new Pollux(apollo, castor);
this.pollux = new Pollux({
Apollo: this.apollo,
Castor: this.castor,
Pluto: this.pluto,
Seed: this.seed,
Api: this.api,
});
}

/**
Expand Down Expand Up @@ -103,8 +94,14 @@ export default class Agent extends Startable.Controller {
return this.runTask(task);
}

isCredentialRevoked(credential: Domain.Credential) {
return this.pollux.handle("revocation-check", "prism/jwt", credential);
async isCredentialRevoked(credential: Domain.Credential): Promise<boolean> {
const result = await this.runTask(new RunProtocol({
type: "revocation-check",
pid: "prism/jwt",
data: { credential }
}));

return result.data;
}

private runTask<T>(task: Task<T>) {
Expand All @@ -113,7 +110,6 @@ export default class Agent extends Startable.Controller {
Apollo: this.apollo,
Castor: this.castor,
Pluto: this.pluto,
Pollux: this.pollux,
Seed: this.seed,
});

Expand Down
58 changes: 27 additions & 31 deletions src/edge-agent/connectionsManager/ConnectionsManager.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { DID, Message, MessageDirection } from "../../domain";
import { Castor } from "../../domain/buildingBlocks/Castor";
import { Mercury } from "../../domain/buildingBlocks/Mercury";
import { Pluto } from "../../domain/buildingBlocks/Pluto";
import { DIDPair } from "../../domain/models/DIDPair";
import { AgentError } from "../../domain/models/Errors";
import { AgentMessageEvents } from "../Agent.MessageEvents";
import { CancellableTask } from "../helpers/Task";
import {
AgentMessageEvents as AgentMessageEventsClass,
AgentOptions,
ConnectionsManager as ConnectionsManagerClass,
ListenerKey,
MediatorHandler,
} from "../types";
import { ProtocolType } from "../protocols/ProtocolTypes";
import { RevocationNotification } from "../protocols/revocation/RevocationNotfiication";
import { IssueCredential } from "../protocols/issueCredential/IssueCredential";
import { HandleIssueCredential } from "../didcomm/HandleIssueCredential";
import { Task } from "../../utils/tasks";
import PlugPol from "../../pollux";
import DIDCommAgent from "../didcomm/Agent";


/**
Expand All @@ -29,7 +24,7 @@ import PlugPol from "../../pollux";
* @class ConnectionsManager
* @typedef {ConnectionsManager}
*/
export class ConnectionsManager implements ConnectionsManagerClass {
export class ConnectionsManager {
/**
* An array with cancellable tasks, mainly used to store one or multiple didcomm
* connections in storage implementation at the same time. All of them can be cancelled
Expand Down Expand Up @@ -58,6 +53,8 @@ export class ConnectionsManager implements ConnectionsManagerClass {
*/
public events: AgentMessageEventsClass;

public pairings: DIDPair[] = [];

/**
* Creates an instance of ConnectionsManager.
*
Expand All @@ -69,17 +66,16 @@ export class ConnectionsManager implements ConnectionsManagerClass {
* @param {DIDPair[]} [pairings=[]]
*/
constructor(
public castor: Castor,
public mercury: Mercury,
public pluto: Pluto,
public pollux: PlugPol,
public mediationHandler: MediatorHandler,
public pairings: DIDPair[] = [],
private readonly agent: DIDCommAgent,
public options?: AgentOptions
) {
this.events = new AgentMessageEvents();
}

get mediationHandler(): MediatorHandler {
return this.agent.mediationHandler;
}

get withWebsocketsExperiment() {
return this.options?.experiments?.liveMode === true;
}
Expand All @@ -93,7 +89,7 @@ export class ConnectionsManager implements ConnectionsManagerClass {
*/
async startMediator(): Promise<void> {
const mediationHandler =
await this.mediationHandler.bootRegisteredMediator();
await this.agent.mediationHandler.bootRegisteredMediator();

if (!mediationHandler) {
throw new AgentError.NoMediatorAvailableError();
Expand Down Expand Up @@ -121,7 +117,7 @@ export class ConnectionsManager implements ConnectionsManagerClass {
*/
async awaitMessageResponse(id: string): Promise<Message | undefined> {
console.log("Deprecated, use agent.addListener('THREAD-{{Your thread || messageId}}', fn), this method does not support live-mode.");
const messages = await this.mediationHandler.pickupUnreadMessages(10);
const messages = await this.agent.mediationHandler.pickupUnreadMessages(10);
return messages.find(x => x.message.thid === id)?.message;
}

Expand All @@ -134,7 +130,7 @@ export class ConnectionsManager implements ConnectionsManagerClass {
attachmentId: string;
message: Message;
}[] = []): Promise<void> {
if (!this.mediationHandler.mediator) {
if (!this.agent.mediationHandler.mediator) {
throw new AgentError.NoMediatorAvailableError();
}

Expand All @@ -144,11 +140,11 @@ export class ConnectionsManager implements ConnectionsManagerClass {
const messageIds = received.map(x => x.attachmentId);

if (messages.length > 0) {
await this.pluto.storeMessages(messages);
await this.agent.pluto.storeMessages(messages);
}

const revokeMessages = messages.filter(x => x.piuri === ProtocolType.PrismRevocation);
const allMessages = await this.pluto.getAllMessages();
const allMessages = await this.agent.pluto.getAllMessages();

for (const message of revokeMessages) {
const revokeMessage = RevocationNotification.fromMessage(message);
Expand All @@ -162,17 +158,17 @@ export class ConnectionsManager implements ConnectionsManagerClass {
if (matchingMessages.length > 0) {
for (const message of matchingMessages) {
const issueCredential = IssueCredential.fromMessage(message);
const ctx = Task.Context.make({ Pluto: this.pluto, Pollux: this.pollux });
// const ctx = Task.Context.make({ Pluto: this.agent.pluto, Pollux: this.pollux });
const task = new HandleIssueCredential({ issueCredential });
const credential = await ctx.run(task);
const credential = await (this.agent as any).runTask(task);

await this.pluto.revokeCredential(credential);
await this.agent.pluto.revokeCredential(credential);
this.events.emit(ListenerKey.REVOKE, credential);
}
}
}
if (messageIds.length) {
await this.mediationHandler.registerMessagesAsRead(messageIds);
await this.agent.mediationHandler.registerMessagesAsRead(messageIds);
}

this.events.emit(ListenerKey.MESSAGE, messages);
Expand All @@ -194,7 +190,7 @@ export class ConnectionsManager implements ConnectionsManagerClass {
}

const storeDIDPairTask = new CancellableTask<DIDPair>(async () => {
await this.pluto.storeDIDPair(paired.host, paired.receiver, paired.name);
await this.agent.pluto.storeDIDPair(paired.host, paired.receiver, paired.name);
this.events.emit(ListenerKey.CONNECTION, paired);
return paired;
});
Expand Down Expand Up @@ -242,7 +238,7 @@ export class ConnectionsManager implements ConnectionsManagerClass {
* @returns {Promise<void>}
*/
async registerMediator(hostDID: DID): Promise<void> {
await this.mediationHandler.achieveMediation(hostDID);
await this.agent.mediationHandler.achieveMediation(hostDID);
}

/**
Expand All @@ -254,8 +250,8 @@ export class ConnectionsManager implements ConnectionsManagerClass {
*/
async sendMessage(message: Message): Promise<Message | undefined> {
message.direction = MessageDirection.SENT;
await this.pluto.storeMessage(message);
return this.mercury.sendMessageParseMessage(message);
await this.agent.pluto.storeMessage(message);
return this.agent.mercury.sendMessageParseMessage(message);
}

/**
Expand All @@ -264,11 +260,11 @@ export class ConnectionsManager implements ConnectionsManagerClass {
* @param {number} iterationPeriod
*/
async startFetchingMessages(iterationPeriod: number): Promise<void> {
if (this.cancellable || !this.mediationHandler.mediator) {
if (this.cancellable || !this.agent.mediationHandler.mediator) {
return;
}
const currentMediator = this.mediationHandler.mediator.mediatorDID;
const resolvedMediator = await this.castor.resolveDID(currentMediator.toString());
const currentMediator = this.agent.mediationHandler.mediator.mediatorDID;
const resolvedMediator = await this.agent.castor.resolveDID(currentMediator.toString());
const hasWebsocket = resolvedMediator.services.find(({ serviceEndpoint: { uri } }) =>
(
uri.startsWith("ws://") ||
Expand All @@ -277,7 +273,7 @@ export class ConnectionsManager implements ConnectionsManagerClass {
);
if (hasWebsocket && this.withWebsocketsExperiment) {
this.cancellable = new CancellableTask(async (signal) => {
this.mediationHandler.listenUnreadMessages(
this.agent.mediationHandler.listenUnreadMessages(
signal,
hasWebsocket.serviceEndpoint.uri,
(messages) => this.processMessages(messages)
Expand All @@ -286,7 +282,7 @@ export class ConnectionsManager implements ConnectionsManagerClass {
} else {
const timeInterval = Math.max(iterationPeriod, 5) * 1000;
this.cancellable = new CancellableTask(async () => {
const unreadMessages = await this.mediationHandler.pickupUnreadMessages(10);
const unreadMessages = await this.agent.mediationHandler.pickupUnreadMessages(10);
await this.processMessages(unreadMessages);
}, timeInterval);
}
Expand Down
Loading

0 comments on commit a6744c0

Please sign in to comment.