Skip to content

Commit

Permalink
Fix/pass types (#183)
Browse files Browse the repository at this point in the history
Signed-off-by: Mirko Mollik <[email protected]>
  • Loading branch information
cre8 authored Mar 18, 2024
1 parent f19b76c commit 5517604
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 36 deletions.
5 changes: 4 additions & 1 deletion examples/sd-jwt-example/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Create a presentation using the issued credential and the presentation frame
// return a Encoded SD JWT. Holder send the presentation to the verifier
const presentation = await sdjwt.present(credential, presentationFrame);
const presentation = await sdjwt.present<typeof claims>(
credential,
presentationFrame,
);
console.log('presentedSDJwt:', presentation);

// Verifier Define the required claims that need to be verified in the presentation
Expand Down
5 changes: 4 additions & 1 deletion examples/sd-jwt-example/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Create a presentation using the issued credential and the presentation frame
// return a Encoded SD JWT. Holder send the presentation to the verifier
const presentation = await sdjwt.present(credential, presentationFrame);
const presentation = await sdjwt.present<typeof claims>(
credential,
presentationFrame,
);

// Verifier Define the required claims that need to be verified in the presentation
const requiredClaims = ['firstname', 'ssn', 'id'];
Expand Down
5 changes: 4 additions & 1 deletion examples/sd-jwt-example/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Create a presentation using the issued credential and the presentation frame
// return a Encoded SD JWT. Holder send the presentation to the verifier
const presentation = await sdjwt.present(credential, presentationFrame);
const presentation = await sdjwt.present<typeof claims>(
credential,
presentationFrame,
);
console.log('presentedSDJwt:', presentation);

// Verifier Define the required claims that need to be verified in the presentation
Expand Down
2 changes: 1 addition & 1 deletion examples/sd-jwt-example/kb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { createSignerVerifier, digest, generateSalt } from './utils';
const sdjwttoken = await sdjwt.decode(encodedSdjwt);
console.log(sdjwttoken);

const presentedSdJwt = await sdjwt.present(
const presentedSdJwt = await sdjwt.present<typeof claims>(
encodedSdjwt,
{ id: true },
{
Expand Down
5 changes: 4 additions & 1 deletion examples/sd-jwt-vc-example/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Create a presentation using the issued credential and the presentation frame
// return a Encoded SD JWT. Holder send the presentation to the verifier
const presentation = await sdjwt.present(credential, presentationFrame);
const presentation = await sdjwt.present<typeof claims>(
credential,
presentationFrame,
);
console.log('presentedSDJwt:', presentation);

// Verifier Define the required claims that need to be verified in the presentation
Expand Down
5 changes: 4 additions & 1 deletion examples/sd-jwt-vc-example/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Create a presentation using the issued credential and the presentation frame
// return a Encoded SD JWT. Holder send the presentation to the verifier
const presentation = await sdjwt.present(credential, presentationFrame);
const presentation = await sdjwt.present<typeof claims>(
credential,
presentationFrame,
);

// Verifier Define the required claims that need to be verified in the presentation
const requiredClaims = ['firstname', 'ssn', 'id'];
Expand Down
5 changes: 4 additions & 1 deletion examples/sd-jwt-vc-example/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ import { createSignerVerifier, digest, generateSalt } from './utils';

// Create a presentation using the issued credential and the presentation frame
// return a Encoded SD JWT. Holder send the presentation to the verifier
const presentation = await sdjwt.present(credential, presentationFrame);
const presentation = await sdjwt.present<typeof claims>(
credential,
presentationFrame,
);
console.log('presentedSDJwt:', presentation);

// Verifier Define the required claims that need to be verified in the presentation
Expand Down
2 changes: 1 addition & 1 deletion examples/sd-jwt-vc-example/kb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { createSignerVerifier, digest, generateSalt } from './utils';
const sdjwttoken = await sdjwt.decode(encodedSdjwt);
console.log(sdjwttoken);

const presentedSdJwt = await sdjwt.present(
const presentedSdJwt = await sdjwt.present<typeof claims>(
encodedSdjwt,
{ id: true },
{
Expand Down
38 changes: 21 additions & 17 deletions packages/core/src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('index', () => {

expect(credential).toBeDefined();

const presentation = await sdjwt.present(
const presentation = await sdjwt.present<typeof claims>(
credential,
{ foo: true },
{
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('index', () => {
},
);

const presentation = await sdjwt.present(
const presentation = await sdjwt.present<typeof claims>(
credential,
{ foo: true },
{
Expand Down Expand Up @@ -249,7 +249,7 @@ describe('index', () => {
},
);

const presentation = await sdjwt.present(
const presentation = await sdjwt.present<typeof claims>(
credential,
{ foo: true },
{
Expand Down Expand Up @@ -359,7 +359,7 @@ describe('index', () => {
},
);

const presentation = await sdjwt.present(
const presentation = await sdjwt.present<typeof claims>(
credential,
{ foo: true },
{
Expand Down Expand Up @@ -403,7 +403,7 @@ describe('index', () => {
},
);
try {
const presentation = await sdjwt.present(
const presentation = await sdjwt.present<typeof claims>(
credential,
{ foo: true },
{
Expand Down Expand Up @@ -445,7 +445,7 @@ describe('index', () => {
},
);

const presentation = await sdjwt.present(
const presentation = await sdjwt.present<typeof claims>(
credential,
{ foo: true },
{
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('index', () => {
},
);

const presentation = sdjwt.present(
const presentation = sdjwt.present<typeof claims>(
credential,
{ foo: true },
{
Expand Down Expand Up @@ -530,9 +530,9 @@ describe('index', () => {
expect(sdjwt.presentableKeys('')).rejects.toThrow('Hasher not found');
expect(sdjwt.getClaims('')).rejects.toThrow('Hasher not found');
expect(() => sdjwt.decode('')).toThrowError('Hasher not found');
expect(sdjwt.present(credential, { foo: true })).rejects.toThrow(
'Hasher not found',
);
expect(
sdjwt.present<typeof claims>(credential, { foo: true }),
).rejects.toThrow('Hasher not found');
});

test('presentableKeys', async () => {
Expand Down Expand Up @@ -581,15 +581,19 @@ describe('index', () => {
},
);

const presentation = await sdjwt.present(credential, undefined, {
kb: {
payload: {
aud: '1',
iat: 1,
nonce: '342',
const presentation = await sdjwt.present<typeof claims>(
credential,
undefined,
{
kb: {
payload: {
aud: '1',
iat: 1,
nonce: '342',
},
},
},
});
);

const decoded = await sdjwt.decode(presentation);
expect(decoded.jwt).toBeDefined();
Expand Down
7 changes: 5 additions & 2 deletions packages/core/test/app-e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ describe('App', () => {
firstname: true,
id: true,
};
const presentedSDJwt = await sdjwt.present(encodedSdjwt, presentationFrame);
const presentedSDJwt = await sdjwt.present<typeof claims>(
encodedSdjwt,
presentationFrame,
);
expect(presentedSDJwt).toBeDefined();

const presentationClaims = await sdjwt.getClaims(presentedSDJwt);
Expand Down Expand Up @@ -221,7 +224,7 @@ async function JSONtest(filename: string) {
payload: test.claims,
});

const presentedSDJwt = await sdjwt.present(
const presentedSDJwt = await sdjwt.present<typeof claims>(
encodedSdjwt,
test.presentationFrames,
);
Expand Down
13 changes: 8 additions & 5 deletions packages/present/src/present.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
unpackSync,
unpackObj,
} from '@sd-jwt/decode';
import type { HasherSync } from '@sd-jwt/types/src/type';
import type { Extensible, HasherSync } from '@sd-jwt/types/src/type';

// Presentable keys
// The presentable keys are the path of JSON object that are presentable in the SD JWT
Expand Down Expand Up @@ -119,8 +119,8 @@ export const presentSync = <T extends Record<string, unknown>>(
* @param prefix The prefix to add to the keys
* @returns
*/
export const transformPresentationFrame = <T extends object>(
obj: PresentationFrame<T>,
export const transformPresentationFrame = (
obj: PresentationFrame<Extensible>,
prefix = '',
): string[] => {
return Object.entries(obj).reduce<string[]>((acc, [key, value]) => {
Expand All @@ -130,10 +130,13 @@ export const transformPresentationFrame = <T extends object>(
if (value) {
acc.push(newPrefix);
}
} else {
} else if (typeof value === 'object' && value !== null) {
acc.push(
newPrefix,
...transformPresentationFrame(value as PresentationFrame<T>, newPrefix),
...transformPresentationFrame(
value as PresentationFrame<Extensible>,
newPrefix,
),
);
}
return acc;
Expand Down
7 changes: 5 additions & 2 deletions packages/sd-jwt-vc/test/app-e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ describe('App', () => {
firstname: true,
id: true,
};
const presentedSDJwt = await sdjwt.present(encodedSdjwt, presentationFrame);
const presentedSDJwt = await sdjwt.present<typeof claims>(
encodedSdjwt,
presentationFrame,
);
expect(presentedSDJwt).toBeDefined();

const presentationClaims = await sdjwt.getClaims(presentedSDJwt);
Expand Down Expand Up @@ -236,7 +239,7 @@ async function JSONtest(filename: string) {
payload,
});

const presentedSDJwt = await sdjwt.present(
const presentedSDJwt = await sdjwt.present<typeof claims>(
encodedSdjwt,
test.presentationFrames,
);
Expand Down
9 changes: 7 additions & 2 deletions packages/types/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ type Frame<Payload> = Payload extends Array<infer U>
>
: SD<Payload> & DECOY;

export type DisclosureFrame<T extends object> = Frame<T>;
/**
* This is a disclosureFrame type that is used to represent the structure of what is being disclosed.
*/
export type Extensible = Record<string, unknown | boolean>;

export type DisclosureFrame<T extends Extensible> = Frame<T>;

/**
* This is a presentationFrame type that is used to represent the structure of what is being presented.
Expand Down Expand Up @@ -208,4 +213,4 @@ type PFrame<Payload> = Payload extends Array<infer U>
: boolean;
};

export type PresentationFrame<T extends object> = PFrame<T>;
export type PresentationFrame<T extends Extensible> = PFrame<T>;

0 comments on commit 5517604

Please sign in to comment.