Skip to content

Commit

Permalink
feat: upgrade SD-JWT package
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas <[email protected]>
  • Loading branch information
lukasjhan committed Mar 7, 2024
1 parent 282b8de commit 6563973
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/ssi-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build:clean": "tsc --build --clean && tsc --build"
},
"dependencies": {
"@sd-jwt/decode": "^0.2.0",
"@sd-jwt/decode": "0.3.2-next.108",
"jwt-decode": "^3.1.2"
},
"devDependencies": {
Expand Down
42 changes: 27 additions & 15 deletions packages/ssi-types/src/types/sd-jwt-vc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OriginalType, WrappedVerifiableCredential, WrappedVerifiablePresentation } from './vc'
import { decodeSdJwtVc as _decodeSdJwtVc } from '@sd-jwt/decode'
import { decodeSdJwtSync, decodeSdJwt, getClaims, getClaimsSync } from '@sd-jwt/decode'

type JsonValue = string | number | boolean | { [x: string]: JsonValue | undefined } | Array<JsonValue>

Expand Down Expand Up @@ -189,7 +189,12 @@ export type AsyncHasher = (data: string, alg: string) => Promise<Uint8Array>
* this method hides the actual implementation of SD-JWT (which is currently based on @sd-jwt/core)
*/
export function decodeSdJwtVc(compactSdJwtVc: CompactSdJwtVc, hasher: Hasher): SdJwtDecodedVerifiableCredential {
const { signedPayload, decodedPayload, disclosures } = _decodeSdJwtVc(compactSdJwtVc, hasher)
const { jwt, disclosures } = decodeSdJwtSync(compactSdJwtVc, hasher)

const signedPayload = jwt.payload as SdJwtSignedVerifiableCredentialPayload
const decodedPayload = getClaimsSync(signedPayload, disclosures, hasher)

const { _sd_alg } = signedPayload

return {
compactSdJwtVc,
Expand All @@ -198,11 +203,11 @@ export function decodeSdJwtVc(compactSdJwtVc: CompactSdJwtVc, hasher: Hasher): S
const decoded = d.key ? [d.salt, d.key, d.value] : [d.salt, d.value]
return {
decoded: decoded as SdJwtDecodedDisclosure,
digest: d.digest,
encoded: d.encoded,
digest: d.digestSync({ hasher, alg: _sd_alg ?? 'sha-256' }),
encoded: d.encode(),
} satisfies SdJwtDisclosure
}),
signedPayload: signedPayload as SdJwtDecodedVerifiableCredentialPayload,
signedPayload: signedPayload as SdJwtSignedVerifiableCredentialPayload,
}
}

Expand All @@ -214,19 +219,26 @@ export function decodeSdJwtVc(compactSdJwtVc: CompactSdJwtVc, hasher: Hasher): S
* this method hides the actual implementation of SD-JWT (which is currently based on @sd-jwt/core)
*/
export async function decodeSdJwtVcAsync(compactSdJwtVc: CompactSdJwtVc, hasher: AsyncHasher): Promise<SdJwtDecodedVerifiableCredential> {
const { signedPayload, decodedPayload, disclosures } = await _decodeSdJwtVc(compactSdJwtVc, hasher)
const { jwt, disclosures } = await decodeSdJwt(compactSdJwtVc, hasher)

const signedPayload = jwt.payload as SdJwtSignedVerifiableCredentialPayload
const decodedPayload = await getClaims(signedPayload, disclosures, hasher)

const { _sd_alg } = signedPayload

return {
compactSdJwtVc,
decodedPayload: decodedPayload as SdJwtDecodedVerifiableCredentialPayload,
disclosures: disclosures.map((d) => {
const decoded = d.key ? [d.salt, d.key, d.value] : [d.salt, d.value]
return {
decoded: decoded as SdJwtDecodedDisclosure,
digest: d.digest,
encoded: d.encoded,
} satisfies SdJwtDisclosure
}),
signedPayload: signedPayload as SdJwtDecodedVerifiableCredentialPayload,
disclosures: await Promise.all(
disclosures.map(async (d) => {
const decoded = d.key ? [d.salt, d.key, d.value] : [d.salt, d.value]
return {
decoded: decoded as SdJwtDecodedDisclosure,
digest: await d.digest({ hasher, alg: _sd_alg ?? 'sha-256' }),
encoded: d.encode(),
} satisfies SdJwtDisclosure
})
),
signedPayload: signedPayload as SdJwtSignedVerifiableCredentialPayload,
}
}
43 changes: 25 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6563973

Please sign in to comment.