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

Make project sd-jwt-vc compliant #114

Merged
merged 16 commits into from
Feb 28, 2024
10 changes: 9 additions & 1 deletion examples/core-example/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Issue a signed JWT credential with the specified claims and disclosures
// Return a Encoded SD JWT. Issuer send the credential to the holder
const credential = await sdjwt.issue(claims, disclosureFrame);
const credential = await sdjwt.issue(
{
iss: 'Issuer',
iat: new Date().getTime(),
vct: 'https://example.com',
...claims,
},
disclosureFrame,
);
console.log('encodedJwt:', credential);

// Holder Receive the credential from the issuer and validate it
Expand Down
10 changes: 9 additions & 1 deletion examples/core-example/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Issue a signed JWT credential with the specified claims and disclosures
// Return a Encoded SD JWT. Issuer send the credential to the holder
const credential = await sdjwt.issue(claims, disclosureFrame);
const credential = await sdjwt.issue(
{
iss: 'Issuer',
iat: new Date().getTime(),
vct: 'https://example.com',
...claims,
},
disclosureFrame,
);

// Holder Receive the credential from the issuer and validate it
// Return a boolean result
Expand Down
10 changes: 9 additions & 1 deletion examples/core-example/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Issue a signed JWT credential with the specified claims and disclosures
// Return a Encoded SD JWT. Issuer send the credential to the holder
const credential = await sdjwt.issue(claims, disclosureFrame);
const credential = await sdjwt.issue(
{
iss: 'Issuer',
iat: new Date().getTime(),
vct: 'https://example.com',
...claims,
},
disclosureFrame,
);
console.log('encodedJwt:', credential);

// Holder Receive the credential from the issuer and validate it
Expand Down
15 changes: 12 additions & 3 deletions examples/core-example/custom_header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Issue a signed JWT credential with the specified claims and disclosures
// Return a Encoded SD JWT. Issuer send the credential to the holder
const credential = await sdjwt.issue(claims, disclosureFrame, {
header: { typ: 'vc+sd-jwt', custom: 'data' }, // You can add custom header data to the SD JWT
});
const credential = await sdjwt.issue(
{
iss: 'Issuer',
iat: new Date().getTime(),
vct: 'https://example.com',
...claims,
},
disclosureFrame,
{
header: { typ: 'vc+sd-jwt', custom: 'data' }, // You can add custom header data to the SD JWT
},
);
console.log('encodedSdjwt:', credential);

// You can check the custom header data by decoding the SD JWT
Expand Down
10 changes: 9 additions & 1 deletion examples/core-example/decoy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ import { createSignerVerifier, digest, generateSalt } from './utils';
_sd: ['id'],
_sd_decoy: 1, // 1 decoy digest will be added in SD JWT
};
const credential = await sdjwt.issue(claims, disclosureFrame);
const credential = await sdjwt.issue(
{
iss: 'Issuer',
iat: new Date().getTime(),
vct: 'https://example.com',
...claims,
},
disclosureFrame,
);
console.log('encodedSdjwt:', credential);

// You can check the decoy digest in the SD JWT by decoding it
Expand Down
10 changes: 9 additions & 1 deletion examples/core-example/kb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ import { createSignerVerifier, digest, generateSalt } from './utils';
sd_hash: '1234',
};

const encodedSdjwt = await sdjwt.issue(claims, disclosureFrame);
const encodedSdjwt = await sdjwt.issue(
{
iss: 'Issuer',
iat: new Date().getTime(),
vct: 'https://example.com',
...claims,
},
disclosureFrame,
);
console.log('encodedSdjwt:', encodedSdjwt);
const sdjwttoken = await sdjwt.decode(encodedSdjwt);
console.log(sdjwttoken);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@sd-jwt/sd-jwt",
"name": "@sd-jwt/sd-jwt-vc",
"version": "0.3.0",
"description": "sd-jwt draft 7 implementation in typescript",
"scripts": {
Expand Down
23 changes: 19 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ import {
KB_JWT_TYP,
SDJWTCompact,
SDJWTConfig,
SD_JWT_TYP,
} from '@sd-jwt/types';

export * from './sdjwt';
export * from './kbjwt';
export * from './jwt';
export * from './decoy';

export class SDJwtInstance {
export interface SDJwtPayload {
// more entries
[key: string]: unknown;
}

export abstract class SDJwtInstance<ExtendedPayload extends SDJwtPayload> {
//header type
protected abstract type: string;

public static DEFAULT_hashAlg = 'sha-256';

private userConfig: SDJWTConfig = {};
Expand Down Expand Up @@ -62,7 +69,7 @@ export class SDJwtInstance {
return jwt.verify(this.userConfig.verifier);
}

public async issue<Payload extends Record<string, unknown>>(
public async issue<Payload extends ExtendedPayload>(
payload: Payload,
disclosureFrame?: DisclosureFrame<Payload>,
options?: {
Expand All @@ -81,6 +88,10 @@ export class SDJwtInstance {
throw new SDJWTException('sign alogrithm not specified');
}

if (disclosureFrame) {
this.validateReservedFields<Payload>(disclosureFrame);
}

const hasher = this.userConfig.hasher;
const hashAlg = this.userConfig.hashAlg ?? SDJwtInstance.DEFAULT_hashAlg;

Expand All @@ -94,7 +105,7 @@ export class SDJwtInstance {
const OptionHeader = options?.header ?? {};
const CustomHeader = this.userConfig.omitTyp
? OptionHeader
: { typ: SD_JWT_TYP, ...OptionHeader };
: { typ: this.type, ...OptionHeader };
const header = { ...CustomHeader, alg };
const jwt = new Jwt({
header,
Expand All @@ -113,6 +124,10 @@ export class SDJwtInstance {
return sdJwt.encodeSDJwt();
}

protected abstract validateReservedFields<T extends ExtendedPayload>(
disclosureFrame: DisclosureFrame<T>,
): void;

public async present(
encodedSDJwt: string,
presentationKeys?: string[],
Expand Down
Loading