Skip to content

Commit

Permalink
feat!: updating to reusable Startable class
Browse files Browse the repository at this point in the history
Signed-off-by: Curtish <[email protected]>
  • Loading branch information
curtis-h committed Jan 3, 2025
1 parent 97fa0bd commit bb6c013
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 222 deletions.
15 changes: 2 additions & 13 deletions src/domain/buildingBlocks/Pluto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PeerDID } from "../../peer-did/PeerDID";
import { uuid } from "@stablelib/uuid";
import { Arrayable } from "../../utils";
import * as Backup from "../backup";
import { Startable } from "../protocols/Startable";

export namespace Pluto {
/**
Expand All @@ -32,19 +33,7 @@ export namespace Pluto {
* which will be implemented using this SDK. Implement this interface using your
* preferred underlying storage technology, most appropriate for your use case.
*/
export interface Pluto {
// TODO extend Startable.Controller (changes the interface)

/**
* Handle startup.
*/
start(): Promise<void>;

/**
* Handle teardown.
*/
stop?(): Promise<void>;

export interface Pluto extends Startable.IController {
/**
* create a Backup object from the stored data
*/
Expand Down
24 changes: 12 additions & 12 deletions src/domain/buildingBlocks/Pollux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export type CredentialOfferJWTBasePayload = {
options: {
challenge: string;
domain: string;
};
};
}
}

export type CredentialOfferPayloads = {
[CredentialType.AnonCreds]: Anoncreds.CredentialOfferType;
Expand Down Expand Up @@ -50,7 +50,7 @@ export type ProcessedCredentialOfferPayloads = {
export interface Pollux {

revealCredentialFields: (credential: Credential, fields: string[], linkSecret?: string) => Promise<{
[name: string]: any;
[name: string]: any
}>;

isCredentialRevoked: (credential: Credential) => Promise<boolean>;
Expand All @@ -71,12 +71,12 @@ export interface Pollux {
presentationDefinition: PresentationDefinitionRequest<CredentialType.JWT>,
credential: Credential,
privateKey: PrivateKey
): Promise<PresentationSubmission<CredentialType.JWT>>;
): Promise<PresentationSubmission<CredentialType.JWT>>
createPresentationSubmission(
presentationDefinition: PresentationDefinitionRequest<CredentialType.AnonCreds>,
credential: Credential,
privateKey: LinkSecret
): Promise<PresentationSubmission<CredentialType.AnonCreds>>;
): Promise<PresentationSubmission<CredentialType.AnonCreds>>

/**
* Process a PresentationSubmission, resolve the issuer did and verify the credential and the holder signature
Expand All @@ -88,15 +88,15 @@ export interface Pollux {
verifyPresentationSubmission(
presentationSubmission: PresentationSubmission<CredentialType.JWT>,
options?: Pollux.verifyPresentationSubmission.options.JWT
): Promise<boolean>;
): Promise<boolean>
verifyPresentationSubmission(
presentationSubmission: PresentationSubmission<CredentialType.AnonCreds>,
options?: Pollux.verifyPresentationSubmission.options.Anoncreds
): Promise<boolean>;
): Promise<boolean>
verifyPresentationSubmission(
presentationSubmission: PresentationSubmission,
options?: Pollux.verifyPresentationSubmission.options.JWT | Pollux.verifyPresentationSubmission.options.Anoncreds
): Promise<boolean>;
): Promise<boolean>

/**
* Creates a PresentationDefinitionRequest object for oob Verifications
Expand All @@ -108,7 +108,7 @@ export interface Pollux {
type: T,
claims: PresentationClaims<T>,
options: PresentationOptions
): Promise<PresentationDefinitionRequest<T>>;
): Promise<PresentationDefinitionRequest<T>>



Expand Down Expand Up @@ -138,12 +138,12 @@ export namespace Pollux {
export interface JWT {
presentationDefinitionRequest: PresentationDefinitionRequest<CredentialType.JWT>,
challenge?: string,
domain?: string;
domain?: string
}
export interface SDJWT {
issuer: DID,
presentationDefinitionRequest: PresentationDefinitionRequest<CredentialType.SDJWT>,
requiredClaims?: string[];
requiredClaims?: string[]
}
}
}
Expand All @@ -159,7 +159,7 @@ export namespace Pollux {
}
export interface SDJWT {
privateKey: PrivateKey;
presentationFrame: Record<string, boolean>;
presentationFrame: Record<string, boolean>
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as Backup from "./backup";
export * as Protocols from "./protocols";
export * from "./models";
export * from "./protocols";
export * from "./buildingBlocks/Apollo";
Expand Down
82 changes: 82 additions & 0 deletions src/domain/protocols/Startable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Define controls for managing entity lifecycle.
*/
export namespace Startable {
/**
* states for a Startable entity
*/
export enum State {
STOPPED = "stopped",
STARTING = "starting",
RUNNING = "running",
STOPPING = "stopping",
}

/**
* define the structure of a Startable entity
*/
export interface IController {
/**
* current status of the entity
*/
state: State;
/**
* handle the startup of an entity
*
* updates `state` according to lifecycle
*
* @returns {Promise<State>}
*/
start(): Promise<State>;
/**
* handle the teardown of an entity
*
* updates `state` according to lifecycle
*
* @returns {Promise<State>}
*/
stop(): Promise<State>;
}

export abstract class Controller implements IController {
public state = State.STOPPED;

/**
* internal method to define specific startup routine
*
* used by `start()` internally
*
* implement with `protected` to keep hidden from class interface
*/
protected abstract _start(): Promise<void>;

/**
* internal method to define teardown routine
*
* used by `stop()` internally
*
* implement with `protected` to keep hidden from class interface
*/
protected abstract _stop(): Promise<void>;

async start(): Promise<State> {
if (this.state === Startable.State.STOPPED) {
this.state = Startable.State.STARTING;
await this._start();
this.state = Startable.State.RUNNING;
}

return this.state;
}

async stop(): Promise<State> {
if (this.state === Startable.State.RUNNING) {
this.state = Startable.State.STOPPING;
await this._stop();
this.state = Startable.State.STOPPED;
}

return this.state;
}
}
}
1 change: 1 addition & 0 deletions src/domain/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./KeyRestoration";
export * from "./Startable";
42 changes: 11 additions & 31 deletions src/edge-agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ 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";
import { CreatePrismDID } from "./didFunctions/CreatePrismDID";
import { FetchApi } from "./helpers/FetchApi";
import { Task } from "../utils/tasks";
import { Startable } from "../utils/startable";
import { notNil } from "../utils";

/**
Expand All @@ -17,14 +17,7 @@ import { notNil } from "../utils";
* @class Agent
* @typedef {Agent}
*/
export default class Agent implements Startable.Controller {
/**
* Agent state
*
* @public
* @type {Startable.State}
*/
public state = Startable.State.STOPPED;
export default class Agent extends Startable.Controller {
public backup: AgentBackup;
public readonly pollux: Pollux;

Expand All @@ -45,6 +38,7 @@ export default class Agent implements Startable.Controller {
public readonly seed: Domain.Seed = apollo.createRandomSeed().seed,
public readonly api: Domain.Api = new FetchApi(),
) {
super();
this.pollux = new Pollux(apollo, castor);
this.backup = new AgentBackup(this);
}
Expand Down Expand Up @@ -78,31 +72,17 @@ export default class Agent implements Startable.Controller {
return agent;
}

/**
* Asyncronously start the agent
*
* @returns {Promise<AgentState>}
*/
start(): Promise<Startable.State> {
return Startable.start(this, async () => {
await this.pluto.start();
await this.pollux.start();
});
protected async _start() {
await this.pluto.start();
await this.pollux.start();
}

/**
* Asyncronously stop the agent and any side task that is running
*
* @returns {Promise<Startable.State>}
*/
stop(): Promise<Startable.State> {
return Startable.stop(this, async () => {
await this.pollux.stop();
protected async _stop() {
await this.pollux.stop();

if (notNil(this.pluto.stop)) {
await this.pluto.stop();
}
});
if (notNil(this.pluto.stop)) {
await this.pluto.stop();
}
}

/**
Expand Down
Loading

0 comments on commit bb6c013

Please sign in to comment.