Skip to content

Commit

Permalink
Merge pull request #144 from lukasjhan/upgrade/sdjwt
Browse files Browse the repository at this point in the history
feat: upgrade sd-jwt package version
  • Loading branch information
nklomp authored Mar 19, 2024
2 parents 8df52d7 + 7a6fb1c commit 6752448
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 46 deletions.
6 changes: 6 additions & 0 deletions lib/utils/ObjectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ export class ObjectUtils {
const fieldValues = data.map((item) => item[fieldName]);
return Array.from(new Set(fieldValues));
}
/**
* Receives an object and clone deep, return the cloned object
*/
public static cloneDeep<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
}
43 changes: 24 additions & 19 deletions lib/utils/sdJwt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { decodeDisclosuresInPayload } from '@sd-jwt/decode';
import { getDisclosuresForPresentationFrame, PresentationFrame } from '@sd-jwt/present';
import { Base64url } from '@sd-jwt/utils';
import { getSDAlgAndPayload, unpackObj } from '@sd-jwt/decode';
import { createHashMappingForSerializedDisclosure, selectDisclosures } from '@sd-jwt/present';
import { PresentationFrame } from '@sd-jwt/types';
import { uint8ArrayToBase64Url } from '@sd-jwt/utils';
import {
Hasher,
SdJwtDecodedDisclosure,
Expand All @@ -9,9 +10,11 @@ import {
SdJwtPresentationFrame,
} from '@sphereon/ssi-types';

import { ObjectUtils } from './ObjectUtils';

export function calculateSdHash(compactSdJwtVc: string, alg: string, hasher: Hasher): string {
const digest = hasher(compactSdJwtVc, alg);
return Base64url.encode(digest);
return uint8ArrayToBase64Url(digest);
}

/**
Expand All @@ -25,18 +28,19 @@ export function applySdJwtLimitDisclosure(
sdJwtDecodedVerifiableCredential: SdJwtDecodedVerifiableCredential,
presentationFrame: SdJwtPresentationFrame,
) {
const requiredDisclosures = getDisclosuresForPresentationFrame(
sdJwtDecodedVerifiableCredential.signedPayload,
presentationFrame as PresentationFrame<Record<string, unknown>>,
sdJwtDecodedVerifiableCredential.decodedPayload,
const SerializedDisclosures = sdJwtDecodedVerifiableCredential.disclosures.map((d) => ({
digest: d.digest,
encoded: d.encoded,
salt: d.decoded[0],
value: d.decoded.length === 3 ? d.decoded[2] : d.decoded[1],
key: d.decoded.length === 3 ? d.decoded[1] : undefined,
}));

const requiredDisclosures = selectDisclosures(
ObjectUtils.cloneDeep(sdJwtDecodedVerifiableCredential.signedPayload),
// Map to sd-jwt disclosure format
sdJwtDecodedVerifiableCredential.disclosures.map((d) => ({
digest: d.digest,
encoded: d.encoded,
salt: d.decoded[0],
value: d.decoded.length === 3 ? d.decoded[2] : d.decoded[1],
key: d.decoded.length === 3 ? d.decoded[1] : undefined,
})),
SerializedDisclosures,
presentationFrame as PresentationFrame<Record<string, unknown>>,
);

sdJwtDecodedVerifiableCredential.disclosures = requiredDisclosures.map((d) => ({
Expand All @@ -53,9 +57,10 @@ export function applySdJwtLimitDisclosure(
.filter((item, index) => index === 0 || index === sdJwtParts.length - 1 || includedDisclosures.includes(item))
.join('~');

const { payload } = getSDAlgAndPayload(ObjectUtils.cloneDeep(sdJwtDecodedVerifiableCredential.signedPayload));
const disclosureHashMap = createHashMappingForSerializedDisclosure(requiredDisclosures);
const { unpackedObj: decodedPayload } = unpackObj(payload, disclosureHashMap);

// Update the decoded / 'pretty' payload
sdJwtDecodedVerifiableCredential.decodedPayload = decodeDisclosuresInPayload(
sdJwtDecodedVerifiableCredential.signedPayload,
requiredDisclosures,
) as SdJwtDecodedVerifiableCredentialPayload;
sdJwtDecodedVerifiableCredential.decodedPayload = decodedPayload as SdJwtDecodedVerifiableCredentialPayload;
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
},
"dependencies": {
"@astronautlabs/jsonpath": "^1.1.2",
"@sd-jwt/decode": "^0.3.0",
"@sd-jwt/present": "^0.3.0",
"@sd-jwt/utils": "^0.3.0",
"@sd-jwt/decode": "^0.6.1",
"@sd-jwt/present": "^0.6.1",
"@sd-jwt/utils": "^0.6.1",
"@sd-jwt/types": "^0.6.1",
"@sphereon/pex-models": "^2.2.2",
"@sphereon/ssi-types": "0.18.1",
"ajv": "^8.12.0",
Expand Down
54 changes: 30 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,14 @@
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==

"@sd-jwt/[email protected]", "@sd-jwt/decode@^0.6.1":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@sd-jwt/decode/-/decode-0.6.1.tgz#141f7782df53bab7159a75d91ed4711e1c14a7ea"
integrity sha512-QgTIoYd5zyKKLgXB4xEYJTrvumVwtsj5Dog0v0L9UH9ZvHekDaeexS247X7A4iSdzTvmZzUpGskgABOa4D8NmQ==
dependencies:
"@sd-jwt/types" "0.6.1"
"@sd-jwt/utils" "0.6.1"

"@sd-jwt/decode@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@sd-jwt/decode/-/decode-0.2.0.tgz#44211418fd0884a160f8223feedfe04ae52398c4"
Expand All @@ -704,31 +712,24 @@
"@sd-jwt/types" "0.2.0"
"@sd-jwt/utils" "0.2.0"

"@sd-jwt/decode@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@sd-jwt/decode/-/decode-0.3.0.tgz#23627ce1b7c678a6ac685d7241e7f64e18bd9a8c"
integrity sha512-jCN1g3VzopiUxUtBZWq0Ojfzbg+wYkE1/gV86Xq7/gV8aNacCJo7Su5a3pYtoYg/rnH7ou1kwpD6vteQFkvXMQ==
dependencies:
"@sd-jwt/types" "0.3.0"
"@sd-jwt/utils" "0.3.0"

"@sd-jwt/present@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@sd-jwt/present/-/present-0.3.0.tgz#e054f66c0ec9c339570ec028e0f2291d75c279e3"
integrity sha512-dICPhH5hqOLXmuJMdTaA47ZMpCDkTzbWUQXsIgw0vma7Aj9Bc6ySNevPwlsUx4K8XBjPgYWwBM9tKdrs3tsCvQ==
"@sd-jwt/present@^0.6.1":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@sd-jwt/present/-/present-0.6.1.tgz#82b9188becb0fa240897c397d84a54d55c7d169e"
integrity sha512-QRD3TUDLj4PqQNZ70bBxh8FLLrOE9mY8V9qiZrJSsaDOLFs2p1CtZG+v9ig62fxFYJZMf4bWKwYjz+qqGAtxCg==
dependencies:
"@sd-jwt/types" "0.3.0"
"@sd-jwt/utils" "0.3.0"
"@sd-jwt/decode" "0.6.1"
"@sd-jwt/types" "0.6.1"
"@sd-jwt/utils" "0.6.1"

"@sd-jwt/[email protected]":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@sd-jwt/types/-/types-0.2.0.tgz#3cb50392e1b76ce69453f403c71c937a6e202352"
integrity sha512-16WFRcL/maG0/JxN9UCSx07/vJ2SDbGscv9gDLmFLgJzhJcGPer41XfI6aDfVARYP430wHFixChfY/n7qC1L/Q==

"@sd-jwt/types@0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@sd-jwt/types/-/types-0.3.0.tgz#12f2fa7b448f1f5e368ddfac8db2143ed58c38f7"
integrity sha512-JbpZICZ+nWPiKPKw+Veg5tf0Oftit4EzxhLJyvcd0u4R6IulNZvi6LCoUL7b2IT1H86eYPd/qB1KvSh43ByZOA==
"@sd-jwt/types@0.6.1", "@sd-jwt/types@^0.6.1":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@sd-jwt/types/-/types-0.6.1.tgz#fc4235e00cf40d35a21d6bc02e44e12d7162aa9b"
integrity sha512-LKpABZJGT77jNhOLvAHIkNNmGqXzyfwBT+6r+DN9zNzMx1CzuNR0qXk1GMUbast9iCfPkGbnEpUv/jHTBvlIvg==

"@sd-jwt/[email protected]":
version "0.2.0"
Expand All @@ -738,13 +739,13 @@
"@sd-jwt/types" "0.2.0"
buffer "*"

"@sd-jwt/utils@0.3.0", "@sd-jwt/utils@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@sd-jwt/utils/-/utils-0.3.0.tgz#73ce9809ccc98b35d5a6d1bf1ed34758bcdfb39d"
integrity sha512-jQNYxvyfLda9StVLeUqUZtv5csI6IuzcD6b55/wsC9xJgTuntZqf8vyJvuu4MwEJUFwm9PdGkCJXyl/nbpmNLw==
"@sd-jwt/utils@0.6.1", "@sd-jwt/utils@^0.6.1":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@sd-jwt/utils/-/utils-0.6.1.tgz#33273b20c9eb1954e4eab34118158b646b574ff9"
integrity sha512-1NHZ//+GecGQJb+gSdDicnrHG0DvACUk9jTnXA5yLZhlRjgkjyfJLNsCZesYeCyVp/SiyvIC9B+JwoY4kI0TwQ==
dependencies:
"@sd-jwt/types" "0.3.0"
buffer "*"
"@sd-jwt/types" "0.6.1"
js-base64 "^3.7.6"

"@sinclair/typebox@^0.27.8":
version "0.27.8"
Expand Down Expand Up @@ -3162,6 +3163,11 @@ jest@^29.7.0:
import-local "^3.0.2"
jest-cli "^29.7.0"

js-base64@^3.7.6:
version "3.7.7"
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.7.7.tgz#e51b84bf78fbf5702b9541e2cb7bfcb893b43e79"
integrity sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw==

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit 6752448

Please sign in to comment.