From e08542c2536d2385c6feae3dafbf01de1b57f59f Mon Sep 17 00:00:00 2001 From: Berend Sliedrecht Date: Thu, 22 Feb 2024 12:07:00 +0100 Subject: [PATCH 1/3] fix: convert any usage into or typed version Signed-off-by: Berend Sliedrecht --- biome.json | 5 +-- packages/core/src/jwt.ts | 16 ++++---- packages/core/src/kbjwt.ts | 2 +- packages/core/src/sdjwt.ts | 49 +++++++++++------------ packages/core/src/test/kbjwt.spec.ts | 4 +- packages/decode/src/decode.ts | 60 ++++++++++++++-------------- packages/present/src/present.ts | 8 ++-- packages/utils/src/disclosure.ts | 2 +- 8 files changed, 72 insertions(+), 74 deletions(-) diff --git a/biome.json b/biome.json index 22eb47d..b048131 100644 --- a/biome.json +++ b/biome.json @@ -20,10 +20,7 @@ "linter": { "enabled": true, "rules": { - "recommended": true, - "suspicious": { - "noExplicitAny": "off" - } + "recommended": true } } } diff --git a/packages/core/src/jwt.ts b/packages/core/src/jwt.ts index 35d2bbd..d480f61 100644 --- a/packages/core/src/jwt.ts +++ b/packages/core/src/jwt.ts @@ -3,8 +3,8 @@ import { Base64urlString, Signer, Verifier } from '@hopae/sd-jwt-type'; import { decodeJwt } from '@hopae/sd-jwt-decode'; export type JwtData< - Header extends Record, - Payload extends Record, + Header extends Record, + Payload extends Record, > = { header?: Header; payload?: Payload; @@ -14,8 +14,8 @@ export type JwtData< // This class is used to create and verify JWT // Contains header, payload, and signature export class Jwt< - Header extends Record = Record, - Payload extends Record = Record, + Header extends Record = Record, + Payload extends Record = Record, > { public header?: Header; public payload?: Payload; @@ -28,8 +28,8 @@ export class Jwt< } public static decodeJWT< - Header extends Record = Record, - Payload extends Record = Record, + Header extends Record = Record, + Payload extends Record = Record, >( jwt: string, ): { header: Header; payload: Payload; signature: Base64urlString } { @@ -37,8 +37,8 @@ export class Jwt< } public static fromEncode< - Header extends Record = Record, - Payload extends Record = Record, + Header extends Record = Record, + Payload extends Record = Record, >(encodedJwt: string): Jwt { const { header, payload, signature } = Jwt.decodeJWT( encodedJwt, diff --git a/packages/core/src/kbjwt.ts b/packages/core/src/kbjwt.ts index 37ba722..e021df8 100644 --- a/packages/core/src/kbjwt.ts +++ b/packages/core/src/kbjwt.ts @@ -15,7 +15,7 @@ export class KBJwt< !this.payload?.aud || !this.payload?.nonce || // this is for backward compatibility with version 06 - !(this.payload?.sd_hash || (this.payload as any)?._sd_hash) + !(this.payload?.sd_hash || this.payload?._sd_hash) ) { throw new SDJWTException('Invalid Key Binding Jwt'); } diff --git a/packages/core/src/sdjwt.ts b/packages/core/src/sdjwt.ts index a4b1e12..5ffb116 100644 --- a/packages/core/src/sdjwt.ts +++ b/packages/core/src/sdjwt.ts @@ -22,24 +22,24 @@ import { } from '@hopae/sd-jwt-decode'; export type SDJwtData< - Header extends Record, - Payload extends Record, + Header extends Record, + Payload extends Record, KBHeader extends kbHeader = kbHeader, KBPayload extends kbPayload = kbPayload, > = { jwt?: Jwt; - disclosures?: Array>; + disclosures?: Array; kbJwt?: KBJwt; }; export class SDJwt< - Header extends Record = Record, - Payload extends Record = Record, + Header extends Record = Record, + Payload extends Record = Record, KBHeader extends kbHeader = kbHeader, KBPayload extends kbPayload = kbPayload, > { public jwt?: Jwt; - public disclosures?: Array>; + public disclosures?: Array; public kbJwt?: KBJwt; constructor(data?: SDJwtData) { @@ -49,8 +49,8 @@ export class SDJwt< } public static async decodeSDJwt< - Header extends Record = Record, - Payload extends Record = Record, + Header extends Record = Record, + Payload extends Record = Record, KBHeader extends kbHeader = kbHeader, KBPayload extends kbPayload = kbPayload, >( @@ -58,7 +58,7 @@ export class SDJwt< hasher: Hasher, ): Promise<{ jwt: Jwt; - disclosures: Array>; + disclosures: Array; kbJwt?: KBJwt; }> { const [encodedJwt, ...encodedDisclosures] = sdjwt.split(SD_SEPARATOR); @@ -79,7 +79,7 @@ export class SDJwt< const { _sd_alg } = getSDAlgAndPayload(jwt.payload); const disclosures = await Promise.all( - encodedDisclosures.map((ed) => + (encodedDisclosures as Array).map((ed) => Disclosure.fromEncode(ed, { alg: _sd_alg, hasher }), ), ); @@ -92,8 +92,8 @@ export class SDJwt< } public static async fromEncode< - Header extends Record = Record, - Payload extends Record = Record, + Header extends Record = Record, + Payload extends Record = Record, KBHeader extends kbHeader = kbHeader, KBPayload extends kbPayload = kbPayload, >( @@ -194,7 +194,7 @@ export class SDJwt< } } -export const listKeys = (obj: any, prefix = '') => { +export const listKeys = (obj: Record, prefix = '') => { const keys: string[] = []; for (const key in obj) { if (obj[key] === undefined) continue; @@ -202,18 +202,18 @@ export const listKeys = (obj: any, prefix = '') => { keys.push(newKey); if (obj[key] && typeof obj[key] === 'object' && obj[key] !== null) { - keys.push(...listKeys(obj[key], newKey)); + keys.push(...listKeys(obj[key] as Record, newKey)); } } return keys; }; -export const pack = async ( +export const pack = async >( claims: T, disclosureFrame: DisclosureFrame | undefined, hash: HasherAndAlg, saltGenerator: SaltGenerator, -): Promise<{ packedClaims: any; disclosures: Array> }> => { +): Promise<{ packedClaims: unknown; disclosures: Array }> => { if (!disclosureFrame) { return { packedClaims: claims, @@ -225,16 +225,15 @@ export const pack = async ( const decoyCount = disclosureFrame[SD_DECOY] ?? 0; if (Array.isArray(claims)) { - const packedClaims: any[] = []; - const disclosures: any[] = []; - const recursivePackedClaims: any = {}; + const packedClaims: Array> = []; + const disclosures: Array = []; + const recursivePackedClaims: Record = {}; for (const key in disclosureFrame) { if (key !== SD_DIGEST) { const idx = parseInt(key); const packed = await pack( claims[idx], - // @ts-ignore disclosureFrame[idx], hash, saltGenerator, @@ -244,11 +243,10 @@ export const pack = async ( } } - for (let i = 0; i < (claims as any[]).length; i++) { + for (let i = 0; i < claims.length; i++) { const claim = recursivePackedClaims[i] ? recursivePackedClaims[i] : claims[i]; - // @ts-ignore if (sd.includes(i)) { const salt = await saltGenerator(16); const disclosure = new Disclosure([salt, claim]); @@ -266,9 +264,10 @@ export const pack = async ( return { packedClaims, disclosures }; } - const packedClaims: any = {}; - const disclosures: any[] = []; - const recursivePackedClaims: any = {}; + const packedClaims: Record = {}; + const disclosures: Array = []; + const recursivePackedClaims: Record = {}; + for (const key in disclosureFrame) { if (key !== SD_DIGEST) { const packed = await pack( diff --git a/packages/core/src/test/kbjwt.spec.ts b/packages/core/src/test/kbjwt.spec.ts index 9c7fbe7..3121fe4 100644 --- a/packages/core/src/test/kbjwt.spec.ts +++ b/packages/core/src/test/kbjwt.spec.ts @@ -253,8 +253,8 @@ describe('KB JWT', () => { }, }); - (kbJwt.payload as any)._sd_hash = 'hash'; - (kbJwt.payload as any).sd_hash = undefined; + (kbJwt.payload as Record)._sd_hash = 'hash'; + (kbJwt.payload as Record).sd_hash = undefined; const encodedKbJwt = await kbJwt.sign(testSigner); const decoded = KBJwt.fromKBEncode(encodedKbJwt); diff --git a/packages/decode/src/decode.ts b/packages/decode/src/decode.ts index 2e1c5b2..221790b 100644 --- a/packages/decode/src/decode.ts +++ b/packages/decode/src/decode.ts @@ -13,8 +13,8 @@ import { import { HasherAndAlgSync, HasherSync } from '@hopae/sd-jwt-type/src/type'; export const decodeJwt = < - H extends Record, - T extends Record, + H extends Record, + T extends Record, >( jwt: string, ): { header: H; payload: T; signature: string } => { @@ -131,8 +131,8 @@ export const decodeSdJwtSync = ( // Get the claims from jwt and disclosures // The digested values are matched with the disclosures and the claims are extracted export const getClaims = async ( - rawPayload: any, - disclosures: Array>, + rawPayload: Record, + disclosures: Array, hasher: Hasher, ): Promise => { const { unpackedObj } = await unpack(rawPayload, disclosures, hasher); @@ -140,8 +140,8 @@ export const getClaims = async ( }; export const getClaimsSync = ( - rawPayload: any, - disclosures: Array>, + rawPayload: Record, + disclosures: Array, hasher: HasherSync, ): T => { const { unpackedObj } = unpackSync(rawPayload, disclosures, hasher); @@ -149,12 +149,12 @@ export const getClaimsSync = ( }; export const unpackArray = ( - arr: Array, - map: Record>, + arr: Array, + map: Record, prefix = '', -): { unpackedObj: any; disclosureKeymap: Record } => { +): { unpackedObj: unknown; disclosureKeymap: Record } => { const keys: Record = {}; - const unpackedArray: any[] = []; + const unpackedArray: unknown[] = []; arr.forEach((item, idx) => { if (item instanceof Object) { if (item[SD_LIST_KEY]) { @@ -190,10 +190,10 @@ export const unpackArray = ( }; export const unpackObj = ( - obj: any, - map: Record>, + obj: unknown, + map: Record, prefix = '', -): { unpackedObj: any; disclosureKeymap: Record } => { +): { unpackedObj: unknown; disclosureKeymap: Record } => { const keys: Record = {}; if (obj instanceof Object) { if (Array.isArray(obj)) { @@ -217,8 +217,10 @@ export const unpackObj = ( } } - const { _sd, ...payload } = obj; - const claims: any = {}; + const { _sd, ...payload } = obj as Record & { + _sd?: Array; + }; + const claims = {}; if (_sd) { for (const hash of _sd) { const disclosed = map[hash]; @@ -247,10 +249,10 @@ export const unpackObj = ( // Creates a mapping of the digests of the disclosures to the actual disclosures export const createHashMapping = async ( - disclosures: Array>, + disclosures: Array, hash: HasherAndAlg, ) => { - const map: Record> = {}; + const map: Record = {}; for (let i = 0; i < disclosures.length; i++) { const disclosure = disclosures[i]; const digest = await disclosure.digest(hash); @@ -260,10 +262,10 @@ export const createHashMapping = async ( }; export const createHashMappingSync = ( - disclosures: Array>, + disclosures: Array, hash: HasherAndAlgSync, ) => { - const map: Record> = {}; + const map: Record = {}; for (let i = 0; i < disclosures.length; i++) { const disclosure = disclosures[i]; const digest = disclosure.digestSync(hash); @@ -273,7 +275,7 @@ export const createHashMappingSync = ( }; // Extract _sd_alg. If it is not present, it is assumed to be sha-256 -export const getSDAlgAndPayload = (sdjwtPayload: any) => { +export const getSDAlgAndPayload = (sdjwtPayload: Record) => { const { _sd_alg, ...payload } = sdjwtPayload; if (typeof _sd_alg !== 'string') { // This is for compatibility @@ -285,8 +287,8 @@ export const getSDAlgAndPayload = (sdjwtPayload: any) => { // Match the digests of the disclosures with the claims and extract the claims // unpack function use unpackObj and unpackArray to recursively unpack the claims export const unpack = async ( - sdjwtPayload: any, - disclosures: Array>, + sdjwtPayload: Record, + disclosures: Array, hasher: Hasher, ) => { const { _sd_alg, payload } = getSDAlgAndPayload(sdjwtPayload); @@ -297,8 +299,8 @@ export const unpack = async ( }; export const unpackSync = ( - sdjwtPayload: any, - disclosures: Array>, + sdjwtPayload: Record, + disclosures: Array, hasher: HasherSync, ) => { const { _sd_alg, payload } = getSDAlgAndPayload(sdjwtPayload); @@ -312,14 +314,14 @@ export const unpackSync = ( // It is a combination of the decoded jwt, the disclosures and the keybinding jwt export type DecodedSDJwt = { jwt: { - header: Record; - payload: Record; // raw payload of sd-jwt + header: Record; + payload: Record; // raw payload of sd-jwt signature: string; }; - disclosures: Array>; + disclosures: Array; kbJwt?: { - header: Record; - payload: Record; + header: Record; + payload: Record; signature: string; }; }; diff --git a/packages/present/src/present.ts b/packages/present/src/present.ts index 2a16dab..efc67ee 100644 --- a/packages/present/src/present.ts +++ b/packages/present/src/present.ts @@ -30,8 +30,8 @@ import { HasherSync } from '@hopae/sd-jwt-type/src/type'; // } // The presentable keys are: ["arr", "arr.0", "arr.2.a", "foo", "test.zzz"] export const presentableKeys = async ( - rawPayload: any, - disclosures: Array>, + rawPayload: unknown, + disclosures: Array, hasher: Hasher, ): Promise => { const { disclosureKeymap } = await unpack(rawPayload, disclosures, hasher); @@ -39,8 +39,8 @@ export const presentableKeys = async ( }; export const presentableKeysSync = ( - rawPayload: any, - disclosures: Array>, + rawPayload: unknown, + disclosures: Array, hasher: HasherSync, ): string[] => { const { disclosureKeymap } = unpackSync(rawPayload, disclosures, hasher); diff --git a/packages/utils/src/disclosure.ts b/packages/utils/src/disclosure.ts index 8e86984..e332ecc 100644 --- a/packages/utils/src/disclosure.ts +++ b/packages/utils/src/disclosure.ts @@ -10,7 +10,7 @@ import { HasherAndAlgSync, } from '@hopae/sd-jwt-type'; -export class Disclosure { +export class Disclosure { public salt: string; public key?: string; public value: T; From 72ed4cb4cf2b788c9c6c605d747ff20e4e04d012 Mon Sep 17 00:00:00 2001 From: Berend Sliedrecht Date: Thu, 22 Feb 2024 12:12:04 +0100 Subject: [PATCH 2/3] chore: ran biome linter Signed-off-by: Berend Sliedrecht --- examples/core-example/decode.ts | 2 +- examples/core-example/utils.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/core-example/decode.ts b/examples/core-example/decode.ts index d162510..47277e2 100644 --- a/examples/core-example/decode.ts +++ b/examples/core-example/decode.ts @@ -17,7 +17,7 @@ import { createSignerVerifier, digest, generateSalt } from './utils'; }); // this is an example of SD JWT - const data = `eyJhbGciOiAiRVMyNTYiLCAia2lkIjogImRvYy1zaWduZXItMDUtMjUtMjAyMiIsICJ0eXAiOiAidmMrc2Qtand0In0.eyJfc2QiOiBbIjA5dktySk1PbHlUV00wc2pwdV9wZE9CVkJRMk0xeTNLaHBINTE1blhrcFkiLCAiMnJzakdiYUMwa3k4bVQwcEpyUGlvV1RxMF9kYXcxc1g3NnBvVWxnQ3diSSIsICJFa084ZGhXMGRIRUpidlVIbEVfVkNldUM5dVJFTE9pZUxaaGg3WGJVVHRBIiwgIklsRHpJS2VpWmREd3BxcEs2WmZieXBoRnZ6NUZnbldhLXNONndxUVhDaXciLCAiSnpZakg0c3ZsaUgwUjNQeUVNZmVadTZKdDY5dTVxZWhabzdGN0VQWWxTRSIsICJQb3JGYnBLdVZ1Nnh5bUphZ3ZrRnNGWEFiUm9jMkpHbEFVQTJCQTRvN2NJIiwgIlRHZjRvTGJnd2Q1SlFhSHlLVlFaVTlVZEdFMHc1cnREc3JaemZVYW9tTG8iLCAiamRyVEU4WWNiWTRFaWZ1Z2loaUFlX0JQZWt4SlFaSUNlaVVRd1k5UXF4SSIsICJqc3U5eVZ1bHdRUWxoRmxNXzNKbHpNYVNGemdsaFFHMERwZmF5UXdMVUs0Il0sICJpc3MiOiAiaHR0cHM6Ly9leGFtcGxlLmNvbS9pc3N1ZXIiLCAiaWF0IjogMTY4MzAwMDAwMCwgImV4cCI6IDE4ODMwMDAwMDAsICJkY3QiOiAiaHR0cHM6Ly9jcmVkZW50aWFscy5leGFtcGxlLmNvbS9pZGVudGl0eV9jcmVkZW50aWFsIiw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Q72WszrCq0GuqZnbws3MApQyzA41I5DSJmenUfsADtqW8FbI_N04FP1wZDF_JtV6a6Ke3Z7apkoTLA~WyIyR0xDNDJzS1F2ZUNmR2ZyeU5STjl3IiwgImdpdmVuX25hbWUiLCAiSm9obiJd~WyJlbHVWNU9nM2dTTklJOEVZbnN4QV9BIiwgImZhbWlseV9uYW1lIiwgIkRvZSJd~WyI2SWo3dE0tYTVpVlBHYm9TNXRtdlZBIiwgImVtYWlsIiwgImpvaG5kb2VAZXhhbXBsZS5jb20iXQ~WyJlSThaV205UW5LUHBOUGVOZW5IZGhRIiwgInBob25lX251bWJlciIsICIrMS0yMDItNTU1LTAxMDEiXQ~WyJRZ19PNjR6cUF4ZTQxMmExMDhpcm9BIiwgImFkZHJlc3MiLCB7InN0cmVldF9hZGRyZXNzIjogIjEyMyBNYWluIFN0IiwgImxvY2FsaXR5IjogIkFueXRvd24iLCAicmVnaW9uIjogIkFueXN0YXRlIiwgImNvdW50cnkiOiAiVVMifV0~WyJBSngtMDk1VlBycFR0TjRRTU9xUk9BIiwgImJpcnRoZGF0ZSIsICIxOTQwLTAxLTAxIl0~WyJQYzMzSk0yTGNoY1VfbEhnZ3ZfdWZRIiwgImlzX292ZXJfMTgiLCB0cnVlXQ~WyJHMDJOU3JRZmpGWFE3SW8wOXN5YWpBIiwgImlzX292ZXJfMjEiLCB0cnVlXQ~WyJsa2x4RjVqTVlsR1RQVW92TU5JdkNBIiwgImlzX292ZXJfNjUiLCB0cnVlXQ~"; const decodedObject = await sdjwt.decode(data); console.log(decodedObject); diff --git a/examples/core-example/utils.ts b/examples/core-example/utils.ts index 1913a31..794422a 100644 --- a/examples/core-example/utils.ts +++ b/examples/core-example/utils.ts @@ -26,7 +26,7 @@ export const generateSalt = (length: number): string => { export const digest = async ( data: string, - algorithm: string = 'SHA-256', + algorithm = 'SHA-256', ): Promise => { const nodeAlg = toNodeCryptoAlg(algorithm); const hash = Crypto.createHash(nodeAlg); From 1b88b35c8844ba36d6d3b18eed5bdf1f6a6accf9 Mon Sep 17 00:00:00 2001 From: Berend Sliedrecht Date: Thu, 22 Feb 2024 12:26:09 +0100 Subject: [PATCH 3/3] fix: make library buildable again Signed-off-by: Berend Sliedrecht --- packages/core/src/index.ts | 2 +- packages/core/src/kbjwt.ts | 5 ++++- packages/core/src/sdjwt.ts | 11 ++++++++++- packages/decode/src/decode.ts | 16 ++++++++-------- packages/present/src/present.ts | 4 ++-- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 1dd2681..a0a7c3f 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -62,7 +62,7 @@ export class SDJwtInstance { return jwt.verify(this.userConfig.verifier); } - public async issue( + public async issue>( payload: Payload, disclosureFrame?: DisclosureFrame, options?: { diff --git a/packages/core/src/kbjwt.ts b/packages/core/src/kbjwt.ts index e021df8..7718e60 100644 --- a/packages/core/src/kbjwt.ts +++ b/packages/core/src/kbjwt.ts @@ -15,7 +15,10 @@ export class KBJwt< !this.payload?.aud || !this.payload?.nonce || // this is for backward compatibility with version 06 - !(this.payload?.sd_hash || this.payload?._sd_hash) + !( + this.payload?.sd_hash || + (this.payload as Record | undefined)?._sd_hash + ) ) { throw new SDJWTException('Invalid Key Binding Jwt'); } diff --git a/packages/core/src/sdjwt.ts b/packages/core/src/sdjwt.ts index 5ffb116..84f622d 100644 --- a/packages/core/src/sdjwt.ts +++ b/packages/core/src/sdjwt.ts @@ -64,6 +64,10 @@ export class SDJwt< const [encodedJwt, ...encodedDisclosures] = sdjwt.split(SD_SEPARATOR); const jwt = Jwt.fromEncode(encodedJwt); + if (!jwt.payload) { + throw new Error('Payload is undefined on the JWT. Invalid state reached'); + } + if (encodedDisclosures.length === 0) { return { jwt, @@ -213,7 +217,10 @@ export const pack = async >( disclosureFrame: DisclosureFrame | undefined, hash: HasherAndAlg, saltGenerator: SaltGenerator, -): Promise<{ packedClaims: unknown; disclosures: Array }> => { +): Promise<{ + packedClaims: Record | Array>; + disclosures: Array; +}> => { if (!disclosureFrame) { return { packedClaims: claims, @@ -247,6 +254,8 @@ export const pack = async >( const claim = recursivePackedClaims[i] ? recursivePackedClaims[i] : claims[i]; + // TODO: should this actually be the `i` or `claim`? + // @ts-ignore if (sd.includes(i)) { const salt = await saltGenerator(16); const disclosure = new Disclosure([salt, claim]); diff --git a/packages/decode/src/decode.ts b/packages/decode/src/decode.ts index 221790b..ee76940 100644 --- a/packages/decode/src/decode.ts +++ b/packages/decode/src/decode.ts @@ -156,9 +156,9 @@ export const unpackArray = ( const keys: Record = {}; const unpackedArray: unknown[] = []; arr.forEach((item, idx) => { - if (item instanceof Object) { - if (item[SD_LIST_KEY]) { - const hash = item[SD_LIST_KEY]; + if (typeof item === 'object' && item !== null) { + const hash = (item as Record)[SD_LIST_KEY]; + if (hash) { const disclosed = map[hash]; if (disclosed) { const presentKey = prefix ? `${prefix}.${idx}` : `${idx}`; @@ -195,7 +195,7 @@ export const unpackObj = ( prefix = '', ): { unpackedObj: unknown; disclosureKeymap: Record } => { const keys: Record = {}; - if (obj instanceof Object) { + if (typeof obj === 'object') { if (Array.isArray(obj)) { return unpackArray(obj, map, prefix); } @@ -204,15 +204,15 @@ export const unpackObj = ( if ( key !== SD_DIGEST && key !== SD_LIST_KEY && - obj[key] instanceof Object + typeof (obj as Record)[key] === 'object' ) { const newKey = prefix ? `${prefix}.${key}` : key; const { unpackedObj, disclosureKeymap: disclosureKeys } = unpackObj( - obj[key], + (obj as Record)[key], map, newKey, ); - obj[key] = unpackedObj; + (obj as Record)[key] = unpackedObj; Object.assign(keys, disclosureKeys); } } @@ -220,7 +220,7 @@ export const unpackObj = ( const { _sd, ...payload } = obj as Record & { _sd?: Array; }; - const claims = {}; + const claims: Record = {}; if (_sd) { for (const hash of _sd) { const disclosed = map[hash]; diff --git a/packages/present/src/present.ts b/packages/present/src/present.ts index efc67ee..2a3fcb2 100644 --- a/packages/present/src/present.ts +++ b/packages/present/src/present.ts @@ -30,7 +30,7 @@ import { HasherSync } from '@hopae/sd-jwt-type/src/type'; // } // The presentable keys are: ["arr", "arr.0", "arr.2.a", "foo", "test.zzz"] export const presentableKeys = async ( - rawPayload: unknown, + rawPayload: Record, disclosures: Array, hasher: Hasher, ): Promise => { @@ -39,7 +39,7 @@ export const presentableKeys = async ( }; export const presentableKeysSync = ( - rawPayload: unknown, + rawPayload: Record, disclosures: Array, hasher: HasherSync, ): string[] => {