Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
combine options into config and use unshift to push .use() options to…
Browse files Browse the repository at this point in the history
… front of list
  • Loading branch information
Bnonni committed Aug 27, 2024
1 parent 2969417 commit bac3ca4
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 185 deletions.
20 changes: 9 additions & 11 deletions packages/applicant/src/dcx-applicant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
DcxError,
DcxManager,
DcxManagerStatus,
dcxOptions,
DcxOptions,
DcxParams,
DcxValidated,
DwnError,
Expand Down Expand Up @@ -49,15 +47,14 @@ import { dcxApplicant } from './index.js';
* requests to 3rd party VC data provider. It also manages the setup and
* initialization of the Web5 connection, the DCX agent, DCX Identity Vault, and the DWN.
* @class DcxApplicant implements DcxManager; see {@link DcxManager} for more details.
* @param params DcxParams; see {@link DcxParams}: {@link DcxOptions}, {@link DcxConfig}
* @param params DcxParams; see {@link DcxParams} and {@link DcxConfig}
* @returns DcxApplicant
* @example
* const applicant = new Dcxapplicant({ options: dcxOptions, config: dcxConfig });
* applicant.initialize();
* applicant.setup();
*/
export class DcxApplicant implements DcxManager {
public options: DcxOptions = dcxOptions;
public config: DcxConfig = dcxConfig;
public status: DcxManagerStatus = {
setup : false,
Expand All @@ -68,9 +65,10 @@ export class DcxApplicant implements DcxManager {
public web5!: Web5;
public agent!: Web5PlatformAgent;

constructor(params: DcxParams = {}) {
this.options = params.options ? { ...this.options, ...params.options } : this.options;
this.config = params.config ? { ...this.config, ...params.config } : this.config;
constructor(params?: DcxParams) {
this.config = params?.config
? { ...this.config, ...params.config }
: this.config;
}

/**
Expand Down Expand Up @@ -336,8 +334,8 @@ export class DcxApplicant implements DcxManager {
}
Logger.debug('Sent application record to local dwn', applicant);

const manifest = OptionsUtil.findManifest({ manifests: this.options.manifests, id: data.manifest_id });
const { id: recipient } = OptionsUtil.findIssuer({ issuers: this.options.issuers, id: manifest?.issuer.id });
const manifest = OptionsUtil.findManifest({ manifests: this.config.options.manifests, id: data.manifest_id });
const { id: recipient } = OptionsUtil.findIssuer({ issuers: this.config.options.issuers, id: manifest?.issuer.id });

const { status: issuer } = await record.send(recipient);
if (DwnUtils.isFailure(issuer.code)) {
Expand All @@ -360,7 +358,7 @@ export class DcxApplicant implements DcxManager {
* @returns RecordsReadParams; see {@link RecordsReadParams}
*/
public async getManifests({ name, id }: Partial<TrustedIssuer>): Promise<GetManifestsResponse> {
const issuer = OptionsUtil.findIssuer({ issuers: this.options.issuers, name, id });
const issuer = OptionsUtil.findIssuer({ issuers: this.config.options.issuers, name, id });
const { records: query } = await this.queryRecords({ from: issuer.id, protocolPath: 'manifest', schema: manifestSchema.$id });
// TODO: application/response query
// const { records: query } = await this.queryRecords({ protocolPath: 'application/response', schema: responseSchema.$id, options: { author: issuer.id } });
Expand Down Expand Up @@ -408,7 +406,7 @@ export class DcxApplicant implements DcxManager {
});

// Toggle the initialization options based on the presence of a recovery phrase
const dwnEndpoints = this.options.dwns;
const dwnEndpoints = this.config.options.dwns;
const connectParams = !recoveryPhrase
? {
password,
Expand Down
51 changes: 29 additions & 22 deletions packages/common/src/dcx-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {
DcxHandshakeManifest,
DcxOptions,
EmailAddressManifest,
PhoneNumberManifest
Handler,
CredentialManifest,
PhoneNumberManifest,
Provider,
TrustedIssuer
} from './index.js';

export type DcxIssuerConfig = {
Expand All @@ -18,20 +21,20 @@ export type DcxApplicantConfig = {
web5RecoveryPhrase: string;
};

export type DcxOptionsConfig = {
handlers: Handler[];
providers: Provider[];
manifests: CredentialManifest[];
issuers: TrustedIssuer[];
gateways: string[];
dwns: string[];
};

export const MX = { name: 'mx', id: 'did:dht:kfcakjzahwimgo9zzjw6yknt9srdtkmfqbeybekcg3xzz1ztg95y' };
export const FF = { name: 'formfree', id: 'did:dht:hcf5e55bbm44s4oixp5z89wtxenxyk35su7f5pd4r5np93ikyowy' };

export const defaultTrustedIssuers = [MX, FF];

export const dcxConfig = {
DcxHandshakeManifest,
PhoneNumberManifest,
EmailAddressManifest,
issuers : defaultTrustedIssuers,
manifests : [DcxHandshakeManifest, PhoneNumberManifest, EmailAddressManifest],
dwnEndpoints : ['https://dwn.tbddev.org/beta'],
gatewayUris : ['https://diddht.tbddev.org/'],
issuer : {
issuer : {
cursorFile : 'issuer-cursor.json',
lastRecordIdFile : 'lastRecordId.issuer',
web5Password : process.env.ISSUER_WEB5_PASSWORD ?? '',
Expand All @@ -41,16 +44,20 @@ export const dcxConfig = {
applicant : {
web5Password : process.env.APPLICANT_WEB5_PASSWORD ?? '',
web5RecoveryPhrase : process.env.APPLICANT_WEB5_RECOVERY_PHRASE ?? '',
},
options : {
handlers : [],
providers : [],
manifests : [DcxHandshakeManifest, PhoneNumberManifest, EmailAddressManifest],
issuers : [MX, FF],
gateways : ['https://dwn.tbddev.org/beta'],
dwns : ['https://diddht.tbddev.org/'],
}
};

export const dcxOptions: DcxOptions = {
handlers : [],
providers : [],
manifests : dcxConfig.manifests,
issuers : dcxConfig.issuers,
gateways : dcxConfig.gatewayUris,
dwns : dcxConfig.dwnEndpoints,
};

export type DcxConfig = typeof dcxConfig & { [key: string]: any };
export type DcxConfig = {
options: DcxOptionsConfig;
issuer: DcxIssuerConfig;
applicant: DcxApplicantConfig;
[key: string]: any
};
2 changes: 1 addition & 1 deletion packages/common/src/dcx-dht-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class DidDhtManager {
* @returns DidRegistrationResult; see {@link DidRegistrationResult}
*/
public async publishDidDoc(gatewayUri: string): Promise<DidRegistrationResult> {
gatewayUri ??= dcxConfig.gatewayUris[0];
gatewayUri ??= dcxConfig.options.gateways[0];
return await DidDht.publish({ did: this.bearerDid, gatewayUri });
}

Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/dcx-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProtocolsConfigureResponse, ProtocolsQueryResponse, Web5 } from '@web5/api';
import {
DcxAgent,
DcxOptions,
DcxConfig,
RecordCreateParams,
RecordCreateResponse,
RecordsCreateParams,
Expand All @@ -22,7 +22,7 @@ export type InitializeParams = {
web5?: Web5
}
export interface DcxManager {
options : DcxOptions;
config : DcxConfig;
status : DcxManagerStatus;
setup() : Promise<void>;
initialize(params?: InitializeParams) : Promise<void>;
Expand Down
5 changes: 1 addition & 4 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * from './interfaces/options.js';
export * from './interfaces/pex.js';

export { EmailAddressManifest } from './manifests/email-address.js';
export { DcxHandshakeManifest } from './manifests/handshake.js';
export { PhoneNumberManifest } from './manifests/phone-number.js';
Expand All @@ -11,7 +8,6 @@ export { schema as manifestSchema } from './schemas/manifest.js';

export type * from './types/did.js';
export type * from './types/handlers.js';
export type * from './types/options.js';
export type * from './types/pex.js';
export type * from './types/server.js';
export type * from './types/web5.js';
Expand All @@ -26,6 +22,7 @@ export * from './utils/logger.js';
export * from './utils/mnemonic.js';
export * from './utils/objects.js';
export * from './utils/options.js';
export * from './utils/pex.js';
export * from './utils/string.js';
export * from './utils/time.js';

Expand Down
81 changes: 0 additions & 81 deletions packages/common/src/interfaces/pex.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/common/src/types/options.ts

This file was deleted.

6 changes: 4 additions & 2 deletions packages/common/src/types/pex.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DwnPaginationCursor, DwnResponseStatus } from '@web5/agent';
import { Record as DwnRecord } from '@web5/api';
import { PresentationDefinitionV2, VcDataModel, VerifiableCredential } from '@web5/credentials';
import { CredentialApplication, CredentialManifest, DcxConfig, DcxOptions } from '../index.js';
import { CredentialApplication, CredentialManifest, DcxConfig, DcxOptionsConfig } from '../index.js';

export type HandlerFunction = (...args: any[]) => any | Promise<any>;

export type AdditionalProperties = Record<string, any>;

Expand Down Expand Up @@ -115,7 +117,7 @@ export type ValidateVerifiablePresentationResponse = {

export type CreateCredentialApplicationParams = { presentationSubmission: PresentationSubmission; manifestId: string; };

export type DcxParams = { options?: DcxOptions; config?: DcxConfig };
export type DcxParams = { config?: DcxConfig };
export type DcxProtocolPath = 'manifest' | 'application/response' | 'response';
export type IssuerProcessRecordParams = { record: DwnRecord, manifest: CredentialManifest, providerId?: string };
export type ApplicantProcessRecordParams = { pex: PresentationExchangeParams, recipient: string }
Expand Down
8 changes: 4 additions & 4 deletions packages/common/src/utils/options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CredentialManifest, FF, TrustedIssuer, Manifest } from '../index.js';
import { CredentialManifest, FF, TrustedIssuer } from '../index.js';

export type FindMissingParams = { dwnManifests: CredentialManifest[], localManifests: CredentialManifest[] };
export type FindMissingResponse = { missing: CredentialManifest[] };
export type FindManifestsParams = Partial<CredentialManifest> & { manifests: Manifest[] };
export type FindManifestParams = FindManifestsParams;
export type FindMissingResponse = { missing: CredentialManifest[] };
export type FindManifestsParams = { manifests: CredentialManifest[]; name?: string; id?: string };
export type FindMissingParams = { dwnManifests: CredentialManifest[], localManifests: CredentialManifest[] };
export class OptionsUtil {
/**
*
Expand Down
Loading

0 comments on commit bac3ca4

Please sign in to comment.