Skip to content

Commit

Permalink
fix: tests and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
elribonazo committed Apr 25, 2024
1 parent 27002ec commit afd2810
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/pollux/Pollux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export default class Pollux implements IPollux {
if (!(privateKey instanceof Secp256k1PrivateKey)) {
throw new CastorError.InvalidKeyError()
}
if (!presentationDefinitionRequest || !presentationDefinitionRequest.options || !presentationDefinitionRequest.presentation_definition) {
throw new Error("Invalid Presentation Definition object")
}
const { presentation_definition, options: { challenge, domain } } = presentationDefinitionRequest;

const descriptorItems: DescriptorItem[] = presentation_definition.input_descriptors.map(
Expand Down Expand Up @@ -99,7 +102,7 @@ export default class Pollux implements IPollux {
iss: subject,
aud: domain,
nbf: Date.parse(new Date().toISOString()),
nonce: Buffer.from(privateKey.sign(Buffer.from(challenge))).toString('hex'),
nonce: challenge,
vp: credential.presentation(),
}

Expand Down Expand Up @@ -226,7 +229,7 @@ export default class Pollux implements IPollux {
}

private validJWTPresentationSubmissionOptions(options: any): options is IPollux.verifyPresentationSubmission.options.JWT {
return options && options.presentationDefinitionRequest !== undefined;
return options && options.presentationDefinitionRequest && typeof options.presentationDefinitionRequest === "object" ? true : false;
}

async verifyPresentationSubmission(
Expand Down
2 changes: 1 addition & 1 deletion src/pollux/models/JWTVerifiableCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class JWTCredential
if (typeof payload[JWTVerifiableCredentialProperties.aud] !== 'undefined' &&
(typeof payload[JWTVerifiableCredentialProperties.aud] !== 'string' &&
!Array.isArray(payload[JWTVerifiableCredentialProperties.aud]))) {
throw new InvalidCredentialError("Invalid aud in presentation payload should be string");
throw new InvalidCredentialError("Invalid aud in credential payload should be string");
}


Expand Down
2 changes: 1 addition & 1 deletion tests/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ describe("Agent Tests", () => {

expect(result).to.have.property("from", request.to);
expect(result).to.have.property("to", request.from);
expect(result).to.have.property("thid", request.thid);
expect(result).to.have.property("thid", request.thid || request.id);
});

test("Attachment format - not JWT - throws", () => {
Expand Down
10 changes: 1 addition & 9 deletions tests/pollux/Pollux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ describe("Pollux", () => {
expect(pollux.verifyPresentationSubmission(presentationSubmissionJSON, {
presentationDefinitionRequest: presentationDefinition
})).to.eventually.be.rejectedWith(
`Verification failed for credential (${issuedJWS.slice(0, 10)}...): reason -> Invalid Submission, $.verifiablePresentation[0] does not contain valid signature for '${challenge}'`
`Verification failed for credential (${issuedJWS.slice(0, 10)}...): reason -> Invalid Holder Presentation JWS Signature`
);
})

Expand Down Expand Up @@ -1749,14 +1749,6 @@ describe("Pollux", () => {
);
})

it("Should throw an error if the actual presentationSubmission.verifiablePresentation is not an array", async () => {
expect(pollux.verifyPresentationSubmission({ presentation_submission: {}, verifiablePresentation: null } as any, {
presentationDefinitionRequest: null as any
})).to.eventually.be.rejectedWith(
`Verification format is invalid: reason -> PresentationSubmission format is invalid`
);
})

it("Should throw an error if the actual presentationSubmission options presentationDefinitionRequest is not an undefined", async () => {
expect(pollux.verifyPresentationSubmission(
{ presentation_submission: {}, verifiablePresentation: [] } as any,
Expand Down

0 comments on commit afd2810

Please sign in to comment.