Skip to content

Commit

Permalink
fix: Improve error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
elribonazo committed Mar 28, 2024
1 parent ce7e641 commit 7ece65e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
2 changes: 0 additions & 2 deletions demos/next/src/components/Message.tsx
Expand Up @@ -298,10 +298,8 @@ export function Message({ message }) {
app.agent.instance?.handlePresentation(SDK.Presentation.fromMessage(message))
.then((valid) => {
setOptions({ valid: valid })
debugger;
})
.catch((err) => {
debugger;
setOptions({ valid: false, reason: err.message });
})
}}>Verify the Proof</button>
Expand Down
2 changes: 0 additions & 2 deletions demos/next/src/pages/credentials.tsx
Expand Up @@ -36,8 +36,6 @@ function Credential({ credential }) {
setClaims(disclosed)
}

debugger;

return <div className="w-full mt-5 p-6 bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700">
<h1 className="mb-4 text-1xl font-extrabold tracking-tight leading-none text-gray-900 dark:text-white">
Credential type {credential.credentialType}
Expand Down
1 change: 0 additions & 1 deletion src/domain/models/MessageAttachment.ts
Expand Up @@ -59,7 +59,6 @@ export class AttachmentDescriptor {
description?: string

): AttachmentDescriptor {
debugger;
const jsonString = typeof payload === "string" ? payload : JSON.stringify(payload)
const encoded = base64.baseEncode(Buffer.from(jsonString));
const attachment: AttachmentBase64 = {
Expand Down
31 changes: 24 additions & 7 deletions src/pollux/Pollux.ts
Expand Up @@ -5,6 +5,7 @@ import { Pollux as IPollux, PresentationOptions } from "../domain/buildingBlocks
import { InvalidJWTString, InvalidVerifyCredentialError, InvalidVerifyFormatError, } from "../domain/models/errors/Pollux";
import { base64url, base64 } from "multiformats/bases/base64";
import { AnoncredsLoader } from "./AnoncredsLoader";

import {
AttachmentDescriptor,
CredentialRequestOptions,
Expand All @@ -22,20 +23,18 @@ import {
PrivateKey,
PresentationSubmission,
DescriptorItem,
Authentication,
CastorError,
SubmissionDescriptorFormat,
DefinitionFormat,
PresentationClaims,
} from "../domain";
import { AnonCredsCredential } from "./models/AnonCredsVerifiableCredential";

import { AnonCredsCredential } from "./models/AnonCredsVerifiableCredential";
import { JWTCredential } from "./models/JWTVerifiableCredential";
import { JWT } from "../apollo/utils/jwt/JWT";
import { Anoncreds } from "../domain/models/Anoncreds";
import { ApiImpl } from "../prism-agent/helpers/ApiImpl";
import { PresentationRequest } from "./models/PresentationRequest";
import { ProofTypes } from "../prism-agent/protocols/types";
import { Secp256k1PrivateKey } from "../apollo/utils/Secp256k1PrivateKey";
import { DescriptorPath } from "./utils/DescriptorPath";

Expand Down Expand Up @@ -283,6 +282,8 @@ export default class Pollux implements IPollux {
const optional = field.optional;
if (!optional) {
let validClaim = false;
let reason = null;

while (paths.length && !validClaim) {
const [path] = paths.splice(0, 1);
if (path) {
Expand All @@ -291,17 +292,33 @@ export default class Pollux implements IPollux {
const filter = field.filter;
if (filter.pattern) {
const pattern = new RegExp(filter.pattern);
validClaim = pattern.test(fieldInVC) || fieldInVC === filter.pattern;
if (pattern.test(fieldInVC) || fieldInVC === filter.pattern) {
validClaim = true
} else {
reason = `Expected the ${path} field to be ${filter.pattern} but got ${fieldInVC}`
}

} else if (filter.enum) {
if (filter.enum.includes(fieldInVC)) {
validClaim = true
} else {
reason = `Expected the ${path} field to be one of ${filter.enum.join(", ")} but got ${fieldInVC}`
}
validClaim = filter.enum.includes(fieldInVC)
} else if (filter.pattern && fieldInVC === filter.pattern) {
validClaim = fieldInVC === filter.pattern;
} else if (filter.const && fieldInVC === filter.pattern) {
if (fieldInVC === filter.const) {
validClaim = true
} else {
reason = `Expected the ${path} field to be ${filter.const} but got ${fieldInVC}`
}
validClaim = fieldInVC === filter.const;
}

}
}
}
if (!validClaim) {
throw new InvalidVerifyCredentialError(vc, `Invalid Claim: ${field.path.join(',')} paths are not found or have unexpected value.`);
throw new InvalidVerifyCredentialError(vc, `Invalid Claim: ${reason || 'paths are not found or have unexpected value'}.`);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/prism-agent/Agent.Credentials.ts
Expand Up @@ -306,7 +306,6 @@ export class AgentCredentials implements AgentCredentialsClass {
const options = {
presentationDefinitionRequest
}
debugger;
const verified = await this.pollux.verifyPresentationSubmission(
presentationSubmission,
options
Expand Down

0 comments on commit 7ece65e

Please sign in to comment.