@sd-jwt/decode
Advanced tools
Comparing version 0.1.2-alpha.7 to 0.1.2-alpha.8
import type { AsyncHasher, Hasher } from '@sd-jwt/types'; | ||
import type { Disclosure, DisclosureWithDigest } from '@sd-jwt/types'; | ||
import type { Disclosure } from '@sd-jwt/types'; | ||
import { HasherAlgorithm } from '@sd-jwt/utils'; | ||
export type CalculateDigestReturnType<HasherImplementation extends Hasher | AsyncHasher> = ReturnType<HasherImplementation> extends Promise<any> ? Promise<DisclosureWithDigest> : DisclosureWithDigest; | ||
export type CalculateDigestReturnType<HasherImplementation extends Hasher | AsyncHasher> = ReturnType<HasherImplementation> extends Promise<any> ? Promise<string> : string; | ||
export declare const disclosureCalculateDigest: <HI extends Hasher | AsyncHasher>(disclosure: Disclosure, algorithm: HasherAlgorithm, hasher: HI) => CalculateDigestReturnType<HI>; |
@@ -1,3 +0,3 @@ | ||
import type { AsyncHasher, Hasher } from '@sd-jwt/types'; | ||
export declare const decodeSdJwtVc: <HI extends Hasher | AsyncHasher>(compact: string, hasher: HI) => { | ||
import type { AsyncHasher, DisclosureWithDigest, Hasher } from '@sd-jwt/types'; | ||
interface DecodeSdJwtVcResult { | ||
compactSdJwtVc: string; | ||
@@ -7,13 +7,7 @@ signedPayload: Record<string, unknown>; | ||
signature: Uint8Array; | ||
keyBinding: { | ||
header: Record<string, unknown>; | ||
payload: Record<string, unknown>; | ||
signature: Uint8Array; | ||
} | undefined; | ||
disclosures: { | ||
digest: import("../disclosures/calculateDigest").CalculateDigestReturnType<HI>; | ||
decoded: import("@sd-jwt/types").Disclosure; | ||
encoded: import("@sd-jwt/types").DisclosureArray; | ||
}[]; | ||
keyBinding?: Record<string, unknown>; | ||
disclosures: Array<DisclosureWithDigest>; | ||
decodedPayload: Record<string, unknown>; | ||
}; | ||
} | ||
export declare const decodeSdJwtVc: <HI extends Hasher | AsyncHasher>(compact: string, hasher: HI) => HI extends AsyncHasher ? Promise<DecodeSdJwtVcResult> : DecodeSdJwtVcResult; | ||
export {}; |
@@ -5,16 +5,17 @@ "use strict"; | ||
const utils_1 = require("@sd-jwt/utils"); | ||
const utils_2 = require("@sd-jwt/utils"); | ||
const fromCompact_1 = require("./fromCompact"); | ||
const utils_2 = require("@sd-jwt/utils"); | ||
const utils_3 = require("@sd-jwt/utils"); | ||
const calculateDigest_1 = require("../disclosures/calculateDigest"); | ||
const toArray_1 = require("../disclosures/toArray"); | ||
const decodeSdJwtVc = (compact, hasher) => { | ||
var _a, _b; | ||
const { header, payload, signature, keyBinding, disclosures } = (0, fromCompact_1.sdJwtVcFromCompact)(compact); | ||
const hasherAlgorithm = (_a = (0, utils_2.getValueByKeyAnyLevel)(payload, '_sd_alg')) !== null && _a !== void 0 ? _a : utils_1.HasherAlgorithm.Sha256; | ||
const disclosureWithDigests = (_b = disclosures === null || disclosures === void 0 ? void 0 : disclosures.map((d) => ({ | ||
digest: (0, calculateDigest_1.disclosureCalculateDigest)(d, hasherAlgorithm, hasher), | ||
decoded: d, | ||
encoded: (0, toArray_1.disclosureToArray)(d) | ||
}))) !== null && _b !== void 0 ? _b : []; | ||
return { | ||
const hasherAlgorithm = (_a = (0, utils_3.getValueByKeyAnyLevel)(payload, '_sd_alg')) !== null && _a !== void 0 ? _a : utils_2.HasherAlgorithm.Sha256; | ||
const disclosuresWithDigestsResult = ((_b = disclosures === null || disclosures === void 0 ? void 0 : disclosures.map((disclosure) => { | ||
const digestResult = (0, calculateDigest_1.disclosureCalculateDigest)(disclosure, hasherAlgorithm, hasher); | ||
return (0, utils_1.isPromise)(digestResult) | ||
? digestResult.then((digest) => (Object.assign(Object.assign({}, disclosure), { digest }))) | ||
: Object.assign(Object.assign({}, disclosure), { digest: digestResult }); | ||
})) !== null && _b !== void 0 ? _b : []); | ||
const basePayload = { | ||
compactSdJwtVc: compact, | ||
@@ -24,8 +25,15 @@ signedPayload: payload, | ||
signature, | ||
keyBinding, | ||
disclosures: disclosureWithDigests, | ||
decodedPayload: (0, utils_2.swapClaims)(payload, disclosureWithDigests.map((d) => (Object.assign(Object.assign({}, d.decoded), { digest: d.digest })))) | ||
keyBinding | ||
}; | ||
if (isAsyncCalculateDigestReturnType(disclosuresWithDigestsResult)) { | ||
return Promise.all(disclosuresWithDigestsResult).then((disclosureWithDigests) => (Object.assign(Object.assign({}, basePayload), { disclosures: disclosureWithDigests, decodedPayload: (0, utils_3.swapClaims)(basePayload.signedPayload, disclosureWithDigests) }))); | ||
} | ||
else { | ||
return Object.assign(Object.assign({}, basePayload), { disclosures: disclosuresWithDigestsResult, decodedPayload: (0, utils_3.swapClaims)(basePayload.signedPayload, disclosuresWithDigestsResult) }); | ||
} | ||
}; | ||
exports.decodeSdJwtVc = decodeSdJwtVc; | ||
function isAsyncCalculateDigestReturnType(disclosureWithDigests) { | ||
return (0, utils_1.isPromise)(disclosureWithDigests[0]); | ||
} | ||
//# sourceMappingURL=decode.js.map |
{ | ||
"name": "@sd-jwt/decode", | ||
"version": "0.1.2-alpha.7", | ||
"version": "0.1.2-alpha.8", | ||
"description": "Decode implementation of sd-jwt Draft 06 and sd-jwt-vc Draft 01", | ||
@@ -40,4 +40,4 @@ "license": "(MIT OR Apache-2.0)", | ||
"dependencies": { | ||
"@sd-jwt/types": "0.1.2-alpha.7", | ||
"@sd-jwt/utils": "0.1.2-alpha.7" | ||
"@sd-jwt/types": "0.1.2-alpha.8", | ||
"@sd-jwt/utils": "0.1.2-alpha.8" | ||
}, | ||
@@ -49,3 +49,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "c970f98dd86e25732fe49034a3f6c9d4e9dbab49" | ||
"gitHead": "e030a05d603046d98044eba671a2182263b8aced" | ||
} |
Sorry, the diff of this file is not supported yet
31047
368
+ Added@sd-jwt/types@0.1.2-alpha.8(transitive)
+ Added@sd-jwt/utils@0.1.2-alpha.8(transitive)
- Removed@sd-jwt/types@0.1.2-alpha.7(transitive)
- Removed@sd-jwt/utils@0.1.2-alpha.7(transitive)
Updated@sd-jwt/types@0.1.2-alpha.8
Updated@sd-jwt/utils@0.1.2-alpha.8