Skip to content

Commit

Permalink
use multiprocess service for verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ichub committed Nov 13, 2024
1 parent 3fba964 commit 8debdd3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
5 changes: 4 additions & 1 deletion apps/passport-server/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export async function startServices(
rollbarService,
discordService
);
const credentialSubservice = await startCredentialSubservice(context.dbPool);
const credentialSubservice = await startCredentialSubservice(
context.dbPool,
multiprocessService
);
const provingService = await startProvingService(rollbarService);
const emailService = startEmailService(context, apis.emailAPI);
const emailTokenService = startEmailTokenService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { LRUCache } from "lru-cache";
import { Pool } from "postgres-pool";
import { loadZupassEdDSAPublicKey } from "../../issuanceService";
import { MultiProcessService } from "../../multiProcessService";
import { traced } from "../../telemetryService";

/**
Expand All @@ -22,11 +23,17 @@ export class CredentialSubservice {
private verificationCache: LRUCache<string, Promise<VerifiedCredential>>;
private zupassPublicKey: EdDSAPublicKey;
private dbPool: Pool | undefined;
private multiProcessService: MultiProcessService;

public constructor(zupassPublicKey: EdDSAPublicKey, dbPool?: Pool) {
public constructor(
zupassPublicKey: EdDSAPublicKey,
multiProcessService: MultiProcessService,
dbPool?: Pool
) {
this.verificationCache = new LRUCache({ max: 20000 });
this.zupassPublicKey = zupassPublicKey;
this.dbPool = dbPool;
this.multiProcessService = multiProcessService;
}

public tryVerify(
Expand All @@ -46,7 +53,10 @@ export class CredentialSubservice {
if (cached) {
return cached;
}
const promise = verifyCredential(credential).catch((err) => {
const promise = verifyCredential(
credential,
this.multiProcessService.verifySignaturePCD
).catch((err) => {
this.verificationCache.delete(key);
throw err;
});
Expand Down Expand Up @@ -105,13 +115,18 @@ export class CredentialSubservice {
}

export async function startCredentialSubservice(
dbPool: Pool
dbPool: Pool,
multiProcessService: MultiProcessService
): Promise<CredentialSubservice> {
const zupassEddsaPublicKey = await loadZupassEdDSAPublicKey();

if (!zupassEddsaPublicKey) {
throw new Error("Missing generic issuance zupass public key");
}

return new CredentialSubservice(zupassEddsaPublicKey, dbPool);
return new CredentialSubservice(
zupassEddsaPublicKey,
multiProcessService,
dbPool
);
}
19 changes: 15 additions & 4 deletions packages/lib/passport-interface/src/Credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,26 @@ export class VerificationError extends Error {}
* application code has access to but library code such as this does not).
*/
export async function verifyCredential(
credential: Credential
credential: Credential,
verifySignature?: (
signature: SerializedPCD<SemaphoreSignaturePCD>
) => Promise<boolean>
): Promise<VerifiedCredential> {
if (credential.type !== SemaphoreSignaturePCDPackage.name) {
throw new VerificationError(`Credential is not a Semaphore Signature PCD`);
}
// Ensure that the signature part of the credential verifies.

const pcd = await SemaphoreSignaturePCDPackage.deserialize(credential.pcd);
if (!(await SemaphoreSignaturePCDPackage.verify(pcd))) {
throw new VerificationError(`Could not verify signature PCD`);

// Ensure that the signature part of the credential verifies.
if (verifySignature) {
if (!(await verifySignature(credential))) {
throw new VerificationError(`Could not verify signature PCD`);
}
} else {
if (!(await SemaphoreSignaturePCDPackage.verify(pcd))) {
throw new VerificationError(`Could not verify signature PCD`);
}
}

// Parse data from the Semaphore Signature claim. Will throw if the message
Expand Down

0 comments on commit 8debdd3

Please sign in to comment.