Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Credential Schemas #340

Merged
merged 11 commits into from
Sep 21, 2023
Merged
13 changes: 13 additions & 0 deletions .changeset/real-baboons-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@learncard/learn-cloud-service': minor
'@learncard/network-brain-service': minor
'@learncard/network-plugin': minor
'@learncard/expiration-plugin': minor
'@learncard/learn-card-plugin': minor
'@learncard/didkit-plugin': minor
'@learncard/vc-plugin': minor
'@learncard/cli': minor
'@learncard/init': minor
---

Add support for credentialSchema checks
2 changes: 1 addition & 1 deletion packages/plugins/didkit/src/didkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let initialized = false;
let generating = false; // Mutex flag to allow first init call to acquire a lock

export const init = async (
arg: InitInput | Promise<InitInput> = 'https://cdn.filestackcontent.com/H678qYhTQLK7BPiqglZK'
arg: InitInput | Promise<InitInput> = 'https://cdn.filestackcontent.com/FScqkl2ETTSnK2K351TA'
) => {
// Do not return until we are done generating!
while (generating) await new Promise(res => setTimeout(res, 250));
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/didkit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type ProofOptions = {
created?: string;
challenge?: string;
domain?: string;
checks?: ('Proof' | 'JWS' | 'CredentialStatus')[];
checks?: ('proof' | 'JWS' | 'credentialStatus' | 'credentialSchema')[];
};

/** @group DIDKit Plugin */
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/expiration/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export const expirationPlugin = (
displayName: 'Expiration Extension',
description: "Adds a check to make sure credentials aren't expired when verifying them",
methods: {
verifyCredential: async (_learnCard, credential) => {
const verificationCheck = await learnCard.invoke.verifyCredential(credential);
verifyCredential: async (_learnCard, credential, options) => {
const verificationCheck = await learnCard.invoke.verifyCredential(credential, options);

if (credential.expirationDate && new Date() > new Date(credential.expirationDate)) {
verificationCheck.errors.push('expiration error: Credential is expired');
Expand Down
7 changes: 5 additions & 2 deletions packages/plugins/learn-card-network/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,11 @@ export const getVerifyBoostPlugin = async (
displayName: 'Verify Boost Extension',
description: 'Adds a check for validating Boost Credentials.',
methods: {
verifyCredential: async (_learnCard, credential) => {
const verificationCheck = await learnCard.invoke.verifyCredential(credential);
verifyCredential: async (_learnCard, credential, options) => {
const verificationCheck = await learnCard.invoke.verifyCredential(
credential,
options
);
const boostCredential = credential?.boostCredential;
try {
if (boostCredential) {
Expand Down
12 changes: 6 additions & 6 deletions packages/plugins/learn-card/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const transformErrorMessage = (error: string, credential: VC): string => {
if (error.startsWith('expiration')) {
return credential.expirationDate
? `Invalid β€’ Expired ${format(
new Date(credential.expirationDate),
'dd MMM yyyy'
).toUpperCase()}`
new Date(credential.expirationDate),
'dd MMM yyyy'
).toUpperCase()}`
: 'Invalid β€’ Expired';
}

Expand All @@ -29,9 +29,9 @@ const transformCheckMessage = (check: string, credential: VC): string => {
proof: 'Valid',
expiration: credential.expirationDate
? `Valid β€’ Expires ${format(
new Date(credential.expirationDate),
'dd MMM yyyy'
).toUpperCase()}`
new Date(credential.expirationDate),
'dd MMM yyyy'
).toUpperCase()}`
: 'Valid β€’ Does Not Expire',
}[check] || check
);
Expand Down
7 changes: 6 additions & 1 deletion packages/plugins/vc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export type VCDependentLearnCard = LearnCard<any, 'id', VCPluginDependentMethods
export type VCImplicitLearnCard = LearnCard<any, 'id', VCPluginMethods & VCPluginDependentMethods>;

/** @group VC Plugin */
export type VerifyExtension = { verifyCredential: (credential: VC) => Promise<VerificationCheck> };
export type VerifyExtension = {
verifyCredential: (
credential: VC,
options?: Partial<ProofOptions>
) => Promise<VerificationCheck>;
};

/** @group VC Plugin */
export type VCPlugin = Plugin<'VC', any, VCPluginMethods, 'id', VCPluginDependentMethods>;
10 changes: 9 additions & 1 deletion packages/plugins/vc/src/verifyCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ export const verifyCredential = (initLearnCard: VCDependentLearnCard) => {
return async (
_learnCard: VCImplicitLearnCard,
credential: VC,
options: Partial<ProofOptions> = {}
_options: Partial<ProofOptions> = {}
) => {
const options = _options;

if (!options.checks) {
options.checks = ['proof'];
if (credential.credentialStatus) options.checks.push('credentialStatus');
if (credential.credentialSchema) options.checks.push('credentialSchema');
}

return initLearnCard.invoke.verifyCredential(credential, options);
};
};
Loading