@sd-jwt/present 0.3.0 → 0.3.2-next.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './present';
package/src/present.ts ADDED
@@ -0,0 +1,120 @@
1
+ import { Hasher, SD_SEPARATOR } from '@sd-jwt/types';
2
+ import { Disclosure, SDJWTException } from '@sd-jwt/utils';
3
+ import {
4
+ createHashMapping,
5
+ decodeSdJwt,
6
+ getSDAlgAndPayload,
7
+ splitSdJwt,
8
+ unpack,
9
+ createHashMappingSync,
10
+ decodeSdJwtSync,
11
+ unpackSync,
12
+ } from '@sd-jwt/decode';
13
+ import { HasherSync } from '@sd-jwt/types/src/type';
14
+
15
+ // Presentable keys
16
+ // The presentable keys are the path of JSON object that are presentable in the SD JWT
17
+ // e.g. if the SD JWT has the following payload and set sd like this:
18
+ // {
19
+ // "foo": "bar", // sd
20
+ // "arr": [ // sd
21
+ // "1", // sd
22
+ // "2",
23
+ // {
24
+ // "a": "1" // sd
25
+ // }
26
+ // ],
27
+ // "test": {
28
+ // "zzz": "xxx" // sd
29
+ // }
30
+ // }
31
+ // The presentable keys are: ["arr", "arr.0", "arr.2.a", "foo", "test.zzz"]
32
+ export const presentableKeys = async (
33
+ rawPayload: Record<string, unknown>,
34
+ disclosures: Array<Disclosure>,
35
+ hasher: Hasher,
36
+ ): Promise<string[]> => {
37
+ const { disclosureKeymap } = await unpack(rawPayload, disclosures, hasher);
38
+ return Object.keys(disclosureKeymap).sort();
39
+ };
40
+
41
+ export const presentableKeysSync = (
42
+ rawPayload: Record<string, unknown>,
43
+ disclosures: Array<Disclosure>,
44
+ hasher: HasherSync,
45
+ ): string[] => {
46
+ const { disclosureKeymap } = unpackSync(rawPayload, disclosures, hasher);
47
+ return Object.keys(disclosureKeymap).sort();
48
+ };
49
+
50
+ export const present = async (
51
+ sdJwt: string,
52
+ keys: string[],
53
+ hasher: Hasher,
54
+ ): Promise<string> => {
55
+ const { jwt, kbJwt } = splitSdJwt(sdJwt);
56
+ const {
57
+ jwt: { payload },
58
+ disclosures,
59
+ } = await decodeSdJwt(sdJwt, hasher);
60
+
61
+ const { _sd_alg: alg } = getSDAlgAndPayload(payload);
62
+ const hash = { alg, hasher };
63
+
64
+ // hashmap: <digest> => <disclosure>
65
+ // to match the digest with the disclosure
66
+ const hashmap = await createHashMapping(disclosures, hash);
67
+ const { disclosureKeymap } = await unpack(payload, disclosures, hasher);
68
+ const presentableKeys = Object.keys(disclosureKeymap);
69
+
70
+ const missingKeys = keys.filter((k) => !presentableKeys.includes(k));
71
+ if (missingKeys.length > 0) {
72
+ throw new SDJWTException(
73
+ `Invalid sd-jwt: invalid present keys: ${missingKeys.join(', ')}`,
74
+ );
75
+ }
76
+
77
+ const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]);
78
+
79
+ return [
80
+ jwt,
81
+ ...presentedDisclosures.map((d) => d.encode()),
82
+ kbJwt ?? '',
83
+ ].join(SD_SEPARATOR);
84
+ };
85
+
86
+ export const presentSync = (
87
+ sdJwt: string,
88
+ keys: string[],
89
+ hasher: HasherSync,
90
+ ): string => {
91
+ const { jwt, kbJwt } = splitSdJwt(sdJwt);
92
+ const {
93
+ jwt: { payload },
94
+ disclosures,
95
+ } = decodeSdJwtSync(sdJwt, hasher);
96
+
97
+ const { _sd_alg: alg } = getSDAlgAndPayload(payload);
98
+ const hash = { alg, hasher };
99
+
100
+ // hashmap: <digest> => <disclosure>
101
+ // to match the digest with the disclosure
102
+ const hashmap = createHashMappingSync(disclosures, hash);
103
+ const { disclosureKeymap } = unpackSync(payload, disclosures, hasher);
104
+ const presentableKeys = Object.keys(disclosureKeymap);
105
+
106
+ const missingKeys = keys.filter((k) => !presentableKeys.includes(k));
107
+ if (missingKeys.length > 0) {
108
+ throw new SDJWTException(
109
+ `Invalid sd-jwt: invalid present keys: ${missingKeys.join(', ')}`,
110
+ );
111
+ }
112
+
113
+ const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]);
114
+
115
+ return [
116
+ jwt,
117
+ ...presentedDisclosures.map((d) => d.encode()),
118
+ kbJwt ?? '',
119
+ ].join(SD_SEPARATOR);
120
+ };
@@ -0,0 +1,81 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { digest } from '@sd-jwt/crypto-nodejs';
3
+ import {
4
+ present,
5
+ presentSync,
6
+ presentableKeys,
7
+ presentableKeysSync,
8
+ } from '../index';
9
+ import { decodeSdJwt, decodeSdJwtSync } from '@sd-jwt/decode';
10
+
11
+ describe('Present tests', () => {
12
+ test('presentableKeys', async () => {
13
+ const sdjwt =
14
+ 'eyJ0eXAiOiJzZC1qd3QiLCJhbGciOiJFZERTQSJ9.eyJ0ZXN0Ijp7Il9zZCI6WyJqVEszMHNleDZhYV9kUk1KSWZDR056Q0FwbVB5MzRRNjNBa3QzS3hhSktzIl19LCJfc2QiOlsiME9nMi1ReG95eW1UOGNnVzZZUjVSSFpQLUJuR2tHUi1NM2otLV92RWlzSSIsIkcwZ3lHNnExVFMyUlQxMkZ3X2RRRDVVcjlZc1AwZlVWOXVtQWdGMC1jQ1EiXSwiX3NkX2FsZyI6InNoYS0yNTYifQ.ggEyE4SeDO2Hu3tol3VLmi7NQj56yKzKQDaafocgkLrUBdivghohtzrfcbrMN7CRufJ_Cnh0EL54kymXLGTdDQ~WyIwNGU0MjAzOWU4ZWFiOWRjIiwiYSIsIjEiXQ~WyIwOGE1Yjc5MjMyYjAzYzBhIiwiMSJd~WyJiNWE2YjUzZGQwYTFmMGIwIiwienp6IiwieHh4Il0~WyIxYzdmOTE4ZTE0MjA2NzZiIiwiZm9vIiwiYmFyIl0~WyJmZjYxYzQ5ZGU2NjFiYzMxIiwiYXJyIixbeyIuLi4iOiJTSG96VW5KNUpkd0ZtTjVCbXB5dXZCWGZfZWRjckVvcExPYThTVlBFUmg0In0sIjIiLHsiX3NkIjpbIkpuODNhZkp0OGx4NG1FMzZpRkZyS2U2R2VnN0dlVUQ4Z3UwdVo3NnRZcW8iXX1dXQ~';
15
+ const decodedSdJwt = await decodeSdJwt(sdjwt, digest);
16
+ const keys = await presentableKeys(
17
+ decodedSdJwt.jwt.payload,
18
+ decodedSdJwt.disclosures,
19
+ digest,
20
+ );
21
+ expect(keys).toStrictEqual(['arr', 'arr.0', 'arr.2.a', 'foo', 'test.zzz']);
22
+ });
23
+
24
+ test('presentableKeys sync', () => {
25
+ const sdjwt =
26
+ 'eyJ0eXAiOiJzZC1qd3QiLCJhbGciOiJFZERTQSJ9.eyJ0ZXN0Ijp7Il9zZCI6WyJqVEszMHNleDZhYV9kUk1KSWZDR056Q0FwbVB5MzRRNjNBa3QzS3hhSktzIl19LCJfc2QiOlsiME9nMi1ReG95eW1UOGNnVzZZUjVSSFpQLUJuR2tHUi1NM2otLV92RWlzSSIsIkcwZ3lHNnExVFMyUlQxMkZ3X2RRRDVVcjlZc1AwZlVWOXVtQWdGMC1jQ1EiXSwiX3NkX2FsZyI6InNoYS0yNTYifQ.ggEyE4SeDO2Hu3tol3VLmi7NQj56yKzKQDaafocgkLrUBdivghohtzrfcbrMN7CRufJ_Cnh0EL54kymXLGTdDQ~WyIwNGU0MjAzOWU4ZWFiOWRjIiwiYSIsIjEiXQ~WyIwOGE1Yjc5MjMyYjAzYzBhIiwiMSJd~WyJiNWE2YjUzZGQwYTFmMGIwIiwienp6IiwieHh4Il0~WyIxYzdmOTE4ZTE0MjA2NzZiIiwiZm9vIiwiYmFyIl0~WyJmZjYxYzQ5ZGU2NjFiYzMxIiwiYXJyIixbeyIuLi4iOiJTSG96VW5KNUpkd0ZtTjVCbXB5dXZCWGZfZWRjckVvcExPYThTVlBFUmg0In0sIjIiLHsiX3NkIjpbIkpuODNhZkp0OGx4NG1FMzZpRkZyS2U2R2VnN0dlVUQ4Z3UwdVo3NnRZcW8iXX1dXQ~';
27
+ const decodedSdJwt = decodeSdJwtSync(sdjwt, digest);
28
+ const keys = presentableKeysSync(
29
+ decodedSdJwt.jwt.payload,
30
+ decodedSdJwt.disclosures,
31
+ digest,
32
+ );
33
+ expect(keys).toStrictEqual(['arr', 'arr.0', 'arr.2.a', 'foo', 'test.zzz']);
34
+ });
35
+
36
+ test('present', async () => {
37
+ const sdjwt =
38
+ 'eyJ0eXAiOiJzZC1qd3QiLCJhbGciOiJFZERTQSJ9.eyJ0ZXN0Ijp7Il9zZCI6WyJqVEszMHNleDZhYV9kUk1KSWZDR056Q0FwbVB5MzRRNjNBa3QzS3hhSktzIl19LCJfc2QiOlsiME9nMi1ReG95eW1UOGNnVzZZUjVSSFpQLUJuR2tHUi1NM2otLV92RWlzSSIsIkcwZ3lHNnExVFMyUlQxMkZ3X2RRRDVVcjlZc1AwZlVWOXVtQWdGMC1jQ1EiXSwiX3NkX2FsZyI6InNoYS0yNTYifQ.ggEyE4SeDO2Hu3tol3VLmi7NQj56yKzKQDaafocgkLrUBdivghohtzrfcbrMN7CRufJ_Cnh0EL54kymXLGTdDQ~WyIwNGU0MjAzOWU4ZWFiOWRjIiwiYSIsIjEiXQ~WyIwOGE1Yjc5MjMyYjAzYzBhIiwiMSJd~WyJiNWE2YjUzZGQwYTFmMGIwIiwienp6IiwieHh4Il0~WyIxYzdmOTE4ZTE0MjA2NzZiIiwiZm9vIiwiYmFyIl0~WyJmZjYxYzQ5ZGU2NjFiYzMxIiwiYXJyIixbeyIuLi4iOiJTSG96VW5KNUpkd0ZtTjVCbXB5dXZCWGZfZWRjckVvcExPYThTVlBFUmg0In0sIjIiLHsiX3NkIjpbIkpuODNhZkp0OGx4NG1FMzZpRkZyS2U2R2VnN0dlVUQ4Z3UwdVo3NnRZcW8iXX1dXQ~';
39
+ const presentedSdJwt = await present(
40
+ sdjwt,
41
+ ['foo', 'arr.0', 'test.zzz'],
42
+ digest,
43
+ );
44
+ expect(presentedSdJwt).toStrictEqual(
45
+ 'eyJ0eXAiOiJzZC1qd3QiLCJhbGciOiJFZERTQSJ9.eyJ0ZXN0Ijp7Il9zZCI6WyJqVEsz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beyIuLi4iOiJTSG96VW5KNUpkd0ZtTjVCbXB5dXZCWGZfZWRjckVvcExPYThTVlBFUmg0In0sIjIiLHsiX3NkIjpbIkpuODNhZkp0OGx4NG1FMzZpRkZyS2U2R2VnN0dlVUQ4Z3UwdVo3NnRZcW8iXX1dXQ~';
52
+ const presentedSdJwt = presentSync(
53
+ sdjwt,
54
+ ['foo', 'arr.0', 'test.zzz'],
55
+ digest,
56
+ );
57
+ expect(presentedSdJwt).toStrictEqual(
58
+ 'eyJ0eXAiOiJzZC1qd3QiLCJhbGciOiJFZERTQSJ9.eyJ0ZXN0Ijp7Il9zZCI6WyJqVEszMHNleDZhYV9kUk1KSWZDR056Q0FwbVB5MzRRNjNBa3QzS3hhSktzIl19LCJfc2QiOlsiME9nMi1ReG95eW1UOGNnVzZZUjVSSFpQLUJuR2tHUi1NM2otLV92RWlzSSIsIkcwZ3lHNnExVFMyUlQxMkZ3X2RRRDVVcjlZc1AwZlVWOXVtQWdGMC1jQ1EiXSwiX3NkX2FsZyI6InNoYS0yNTYifQ.ggEyE4SeDO2Hu3tol3VLmi7NQj56yKzKQDaafocgkLrUBdivghohtzrfcbrMN7CRufJ_Cnh0EL54kymXLGTdDQ~WyIxYzdmOTE4ZTE0MjA2NzZiIiwiZm9vIiwiYmFyIl0~WyIwOGE1Yjc5MjMyYjAzYzBhIiwiMSJd~WyJiNWE2YjUzZGQwYTFmMGIwIiwienp6IiwieHh4Il0~',
59
+ );
60
+ });
61
+
62
+ test('present error', async () => {
63
+ const sdjwt =
64
+ 'eyJ0eXAiOiJzZC1qd3QiLCJhbGciOiJFZERTQSJ9.eyJ0ZXN0Ijp7Il9zZCI6WyJqVEszMHNleDZhYV9kUk1KSWZDR056Q0FwbVB5MzRRNjNBa3QzS3hhSktzIl19LCJfc2QiOlsiME9nMi1ReG95eW1UOGNnVzZZUjVSSFpQLUJuR2tHUi1NM2otLV92RWlzSSIsIkcwZ3lHNnExVFMyUlQxMkZ3X2RRRDVVcjlZc1Aw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hIiwiMSJd~WyJiNWE2YjUzZGQwYTFmMGIwIiwienp6IiwieHh4Il0~WyIxYzdmOTE4ZTE0MjA2NzZiIiwiZm9vIiwiYmFyIl0~WyJmZjYxYzQ5ZGU2NjFiYzMxIiwiYXJyIixbeyIuLi4iOiJTSG96VW5KNUpkd0ZtTjVCbXB5dXZCWGZfZWRjckVvcExPYThTVlBFUmg0In0sIjIiLHsiX3NkIjpbIkpuODNhZkp0OGx4NG1FMzZpRkZyS2U2R2VnN0dlVUQ4Z3UwdVo3NnRZcW8iXX1dXQ~';
75
+ try {
76
+ presentSync(sdjwt, ['notthekey'], digest);
77
+ } catch (e) {
78
+ expect(e).toBeDefined();
79
+ }
80
+ });
81
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ }
7
+ }
@@ -0,0 +1,4 @@
1
+ // vite.config.ts
2
+ import { allEnvs } from '../../vitest.shared';
3
+
4
+ export default allEnvs;
@@ -1,43 +0,0 @@
1
- import { DisclosureWithDigest } from '@sd-jwt/types';
2
- /**
3
- * Mapping from a digest to the corresponding disclosure and its parent disclosures.
4
- */
5
- export type DisclosureMap = {
6
- [digest: string]: {
7
- disclosure: DisclosureWithDigest;
8
- parentDisclosures: DisclosureWithDigest[];
9
- };
10
- };
11
- /**
12
- * Get a mapping in the structure of the pretty payload, to indicate which digests should be disclosed for a
13
- * given entry.
14
- *
15
- * For example if you call this method with the following payload:
16
- * ```ts
17
- * {
18
- * _sd: ['iss_digest', 'nested_field_digest'],
19
- * }
20
- * ```
21
- *
22
- * It can return the following mapping:
23
- * ```ts
24
- * {
25
- * iss: 'iss_digest',
26
- * nested_field: {
27
- * __digest: 'nested_field_digest',
28
- * more_nested_field: {
29
- * // index 1 is null, as it's always in the payload, so doesn't need to be disclosed
30
- * // separately
31
- * a: ['a_0_digest', null, 'a_2_digest'],
32
- * }
33
- * }
34
- * }
35
- * ```
36
- *
37
- * This method will recursively call itself and `getArrayPayloadDisclosureMapping` if the value of a property is an object or array.
38
- */
39
- export declare function getPayloadDisclosureMapping(payload: any, map: DisclosureMap): any[] | Record<string, unknown> | null;
40
- /**
41
- * Get a mapping from a digest to the corresponding disclosure and its parent disclosures.
42
- */
43
- export declare const getDisclosureMap: (disclosures: DisclosureWithDigest[]) => DisclosureMap;
@@ -1,276 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDisclosureMap = exports.getPayloadDisclosureMapping = void 0;
4
- const utils_1 = require("@sd-jwt/utils");
5
- const traverse_1 = require("./traverse");
6
- /**
7
- * Returns an array that includes the digests that should be disclosed for each item in the array.
8
- *
9
- * E.g. if the following array is passed:
10
- * ```ts
11
- * [ { '...': <SD_HASH_DIGEST_1> }, 'string_value', { '...': <SD_HASH_DIGEST_2> } ]
12
- * ```
13
- *
14
- * The return value will be:
15
- * ```ts
16
- * ['<SD_HASH_DIGEST_1>', null, '<SD_HASH_DIGEST_2>']
17
- * ```
18
- *
19
- * The second value will be null, as it's already disclosed, and thus there's no digests that
20
- * need to be disclosed to reveal it. For the other values, it will include the digest that needs
21
- * to be disclosed to reveal that array entry.
22
- *
23
- * In the case the array entry contains nested disclosures, the value will not be a digest, but rather
24
- * the structure of the nested disclosures.
25
- *
26
- * Let's say the `<SD_HASH_DIGEST_1>` is the digest of the following disclosure:
27
- * ```ts
28
- * {
29
- * // `<SD_HASH_DIGEST_3>` is the digest of the dateOfBirth property
30
- * _sd: ['<SD_HASH_DIGEST_3>'],
31
- * name: 'Jane Doe',
32
- * }
33
- * ```
34
- *
35
- * In this case the return value will be:
36
- * ```ts
37
- * [{ __digest: '<SD_HASH_DIGEST_1>', dateOfBirth: '<SD_HASH_DIGEST_3>' }, null, '<SD_HASH_DIGEST_2>']
38
- * ```
39
- * The `__digest` property indicates the digest of the encapsulating disclosure, and it being an object
40
- * indicates that there's nested disclosures that need to be revealed.
41
- *
42
- * In the end the result value is an array, and all the string values in the return value are the digests.
43
- * This allows you to easily build a path of digests to disclose to reveal a certain value.
44
- *
45
- * The return value can be endlessly nested, and will also call `getPayloadDisclosureMapping` recursively
46
- * if the inner values are not arrays, but objects. That method in turn can call this method if the value
47
- * of an object property is an array.
48
- */
49
- function getArrayPayloadDisclosureMapping(array, map) {
50
- const arrayPayloadDisclosureMapping = [];
51
- // Loop through all the payload values of the array
52
- for (const item of array) {
53
- // If the item is an object, (both array and object are objects)
54
- // it means there may be some values in here that need to be disclosed to
55
- // reveal this array entry
56
- if (item instanceof Object) {
57
- // if Array item is { '...': <SD_HASH_DIGEST> }
58
- // It means this item can be disclosed.
59
- if ('...' in item) {
60
- const digest = item['...'];
61
- if (typeof digest !== 'string') {
62
- throw new Error(`Expected value of key '...' to be of type string, but found ${typeof digest}`);
63
- }
64
- // Look up disclosure. It's valid that the disclosure is not present (decoy digests)
65
- const disclosed = map[digest];
66
- if (disclosed) {
67
- // value is always the last item in the disclosure array
68
- const value = disclosed.disclosure.value;
69
- // Recursively look if the disclosed value contains any disclosure references
70
- // of itself. Based on the type we can decide how to handle it.
71
- if ((0, utils_1.isObject)(value)) {
72
- // Get nested disclosures for the object value
73
- const unpacked = getPayloadDisclosureMapping(value, map);
74
- // If there's any nested disclosures, we need to include both this item's
75
- // disclosure, as well as the nested disclosures
76
- if (unpacked && Object.keys(unpacked).length > 0) {
77
- arrayPayloadDisclosureMapping.push(Object.assign(Object.assign({}, unpacked), { __digest: digest }));
78
- }
79
- else {
80
- arrayPayloadDisclosureMapping.push(digest);
81
- }
82
- }
83
- else if (Array.isArray(value)) {
84
- // Get nested disclosures for the array value
85
- const nestedUnpackedArray = getArrayPayloadDisclosureMapping(value, map);
86
- // If all entries are null, it means there's no nested disclosures
87
- // And thus we push the digest directly
88
- if (nestedUnpackedArray.every((item) => item === null)) {
89
- arrayPayloadDisclosureMapping.push(digest);
90
- }
91
- else {
92
- arrayPayloadDisclosureMapping.push(Object.assign(Object.assign({}, nestedUnpackedArray), {
93
- // __digest is for encapsulating disclosure
94
- __digest: digest }));
95
- }
96
- }
97
- // If the value is not an object or a array, it means there's no nested disclosures
98
- // and thus we can push the digest directly
99
- else {
100
- arrayPayloadDisclosureMapping.push(digest);
101
- }
102
- }
103
- else {
104
- // Value is not a disclosure for an array ('...') so we unpack the object recursively
105
- const claims = getPayloadDisclosureMapping(item, map);
106
- if (claims && Object.keys(claims).length > 0) {
107
- arrayPayloadDisclosureMapping.push(claims);
108
- }
109
- else {
110
- arrayPayloadDisclosureMapping.push(null);
111
- }
112
- }
113
- }
114
- }
115
- // If the value is not an Object it means the actual value is disclosed
116
- // in the array directly (so we don't need to disclose anything to reveal it)
117
- else {
118
- arrayPayloadDisclosureMapping.push(null);
119
- }
120
- }
121
- return arrayPayloadDisclosureMapping;
122
- }
123
- /**
124
- * Get a mapping in the structure of the pretty payload, to indicate which digests should be disclosed for a
125
- * given entry.
126
- *
127
- * For example if you call this method with the following payload:
128
- * ```ts
129
- * {
130
- * _sd: ['iss_digest', 'nested_field_digest'],
131
- * }
132
- * ```
133
- *
134
- * It can return the following mapping:
135
- * ```ts
136
- * {
137
- * iss: 'iss_digest',
138
- * nested_field: {
139
- * __digest: 'nested_field_digest',
140
- * more_nested_field: {
141
- * // index 1 is null, as it's always in the payload, so doesn't need to be disclosed
142
- * // separately
143
- * a: ['a_0_digest', null, 'a_2_digest'],
144
- * }
145
- * }
146
- * }
147
- * ```
148
- *
149
- * This method will recursively call itself and `getArrayPayloadDisclosureMapping` if the value of a property is an object or array.
150
- */
151
- function getPayloadDisclosureMapping(payload, map) {
152
- // Handle array
153
- if (payload instanceof Array) {
154
- return getArrayPayloadDisclosureMapping(payload, map);
155
- }
156
- // Not an array or object, so it means the top-level value is already disclosed
157
- if (!(0, utils_1.isObject)(payload)) {
158
- return null;
159
- }
160
- const payloadDisclosureMapping = {};
161
- for (const key in payload) {
162
- // if obj property value is an object or array
163
- // recursively unpack
164
- if (key !== '_sd' && key !== '...' && payload[key] instanceof Object) {
165
- const claim = getPayloadDisclosureMapping(payload[key], map);
166
- if (claim && Object.keys(claim).length > 0) {
167
- payloadDisclosureMapping[key] = claim;
168
- }
169
- }
170
- }
171
- // If the payload contains a _sd property, it means there's disclosures
172
- if (payload._sd) {
173
- if (!Array.isArray(payload._sd)) {
174
- throw new Error(`Expect value of '_sd' to be of type array, but found ${typeof payload._sd}`);
175
- }
176
- // We are going to resolve all digests
177
- for (const digest of payload._sd) {
178
- if (typeof digest !== 'string') {
179
- throw new Error(`Expected entries in '_sd' property to be of type string, found ${typeof digest}`);
180
- }
181
- // Look up disclosure. It's valid that the disclosure is not present (decoy digests)
182
- const disclosed = map[digest];
183
- if (disclosed) {
184
- // value is always the last item in the disclosure array
185
- // We know this is an object, so the associated disclosure MUST have length 3
186
- const value = disclosed.disclosure.value;
187
- const key = disclosed.disclosure.key;
188
- if (!key) {
189
- throw new Error(`Expected disclosure for value ${value} to have a key, but key was undefined`);
190
- }
191
- // This checks if there's a nested disclosure anywhere down the tree
192
- // So when a disclosure value is an object or array, it can contain disclosures
193
- // of itself (using `_sd` and `...` keys)
194
- if ((0, utils_1.isObject)(value)) {
195
- const unpacked = getPayloadDisclosureMapping(value, map);
196
- if (unpacked && Object.keys(unpacked).length > 0) {
197
- payloadDisclosureMapping[key] = Object.assign(Object.assign({}, unpacked), { __digest: digest });
198
- }
199
- // If there's no nested disclosures, we add the digest directly
200
- else {
201
- payloadDisclosureMapping[key] = digest;
202
- }
203
- }
204
- else if (Array.isArray(value)) {
205
- // Get nested disclosures for the array value
206
- const nestedUnpackedArray = getArrayPayloadDisclosureMapping(value, map);
207
- // If all entries are null, it means there's no nested disclosures
208
- // And thus we push the digest directly
209
- if (nestedUnpackedArray.every((item) => item === null)) {
210
- payloadDisclosureMapping[key] = digest;
211
- }
212
- else {
213
- payloadDisclosureMapping[key] = Object.assign(Object.assign({}, nestedUnpackedArray), {
214
- // __digest is for encapsulating disclosure
215
- __digest: digest });
216
- }
217
- }
218
- else {
219
- payloadDisclosureMapping[key] = digest;
220
- }
221
- }
222
- }
223
- }
224
- return payloadDisclosureMapping;
225
- }
226
- exports.getPayloadDisclosureMapping = getPayloadDisclosureMapping;
227
- const getParentDisclosure = (disclosure, digestMap) => {
228
- const parent = digestMap[disclosure.digest];
229
- if (!parent) {
230
- return [];
231
- }
232
- if (digestMap[disclosure.digest]) {
233
- return [parent].concat(getParentDisclosure(parent, digestMap));
234
- }
235
- return [parent];
236
- };
237
- /**
238
- * Get a mapping from a digest to the corresponding disclosure and its parent disclosures.
239
- */
240
- const getDisclosureMap = (disclosures) => {
241
- const map = {};
242
- const parentMap = {};
243
- for (const disclosure of disclosures) {
244
- const value = disclosure.value;
245
- (0, traverse_1.traverseNodes)(value).forEach(({ path, value }) => {
246
- const lastPathItem = path[path.length - 1];
247
- if (lastPathItem === '_sd') {
248
- if (!Array.isArray(value)) {
249
- throw new Error(`Expect value of '_sd' to be of type array, but found ${typeof value}`);
250
- }
251
- value.forEach((digest) => {
252
- if (typeof digest !== 'string') {
253
- throw new Error(`Expected entries in '_sd' property to be of type string, found ${typeof digest}`);
254
- }
255
- parentMap[digest] = disclosure;
256
- });
257
- }
258
- else if (lastPathItem === '...') {
259
- if (typeof value !== 'string') {
260
- throw new Error(`Expected value of '...' to be of type string, but found ${typeof value}`);
261
- }
262
- parentMap[value] = disclosure;
263
- }
264
- });
265
- }
266
- for (const disclosure of disclosures) {
267
- const parent = getParentDisclosure(disclosure, parentMap);
268
- map[disclosure.digest] = {
269
- disclosure,
270
- parentDisclosures: parent
271
- };
272
- }
273
- return map;
274
- };
275
- exports.getDisclosureMap = getDisclosureMap;
276
- //# sourceMappingURL=disclosureMapping.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"disclosureMapping.js","sourceRoot":"","sources":["../src/disclosureMapping.ts"],"names":[],"mappings":";;;AAEA,yCAAwC;AACxC,yCAA0C;AAY1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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EAAE;oBACrB,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;wBAC7B,MAAM,IAAI,KAAK,CACX,kEAAkE,OAAO,MAAM,EAAE,CACpF,CAAA;oBACL,CAAC;oBACD,SAAS,CAAC,MAAM,CAAC,GAAG,UAAU,CAAA;gBAClC,CAAC,CAAC,CAAA;YACN,CAAC;iBAAM,IAAI,YAAY,KAAK,KAAK,EAAE,CAAC;gBAChC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;oBAC5B,MAAM,IAAI,KAAK,CACX,2DAA2D,OAAO,KAAK,EAAE,CAC5E,CAAA;gBACL,CAAC;gBACD,SAAS,CAAC,KAAK,CAAC,GAAG,UAAU,CAAA;YACjC,CAAC;QACL,CAAC,CAAC,CAAA;IACN,CAAC;IAED,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE,CAAC;QACnC,MAAM,MAAM,GAAG,mBAAmB,CAAC,UAAU,EAAE,SAAS,CAAC,CAAA;QAEzD,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG;YACrB,UAAU;YACV,iBAAiB,EAAE,MAAM;SAC5B,CAAA;IACL,CAAC;IAED,OAAO,GAAG,CAAA;AACd,CAAC,CAAA;AAhDY,QAAA,gBAAgB,oBAgD5B"}
package/build/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export { getDisclosuresForPresentationFrame } from './presentationFrame';
2
- export { PresentationFrame } from './types';
package/build/index.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDisclosuresForPresentationFrame = void 0;
4
- var presentationFrame_1 = require("./presentationFrame");
5
- Object.defineProperty(exports, "getDisclosuresForPresentationFrame", { enumerable: true, get: function () { return presentationFrame_1.getDisclosuresForPresentationFrame; } });
6
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yDAAwE;AAA/D,uIAAA,kCAAkC,OAAA"}
@@ -1,3 +0,0 @@
1
- import { DisclosureWithDigest } from '@sd-jwt/types';
2
- import { PresentationFrame } from './types';
3
- export declare function getDisclosuresForPresentationFrame<Payload extends Record<string, unknown> = Record<string, unknown>>(signedPayload: Payload, presentationFrame: PresentationFrame<Payload>, prettyClaims: Payload, disclosures?: Array<DisclosureWithDigest>): Array<DisclosureWithDigest>;
@@ -1,64 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDisclosuresForPresentationFrame = void 0;
4
- const utils_1 = require("@sd-jwt/utils");
5
- const disclosureMapping_1 = require("./disclosureMapping");
6
- const traverse_1 = require("./traverse");
7
- function getDisclosuresForPresentationFrame(signedPayload, presentationFrame, prettyClaims, disclosures = []) {
8
- const requiredDisclosureDigests = new Set();
9
- const disclosureMap = (0, disclosureMapping_1.getDisclosureMap)(disclosures);
10
- const payloadDisclosureMapping = (0, disclosureMapping_1.getPayloadDisclosureMapping)(signedPayload, disclosureMap);
11
- // No disclosures needed
12
- if (payloadDisclosureMapping === null) {
13
- if (disclosures.length > 0) {
14
- throw new Error('Payload disclosure mapping is null, but disclosures are present.');
15
- }
16
- return [];
17
- }
18
- for (const node of (0, traverse_1.traverseNodes)(presentationFrame)) {
19
- // We only want to process leaf nodes here
20
- if (!node.isLeaf)
21
- continue;
22
- if (typeof node.value !== 'boolean') {
23
- throw new Error(`Expected leaf value in presentation frame to be of type boolean, but found ${typeof node.value}`);
24
- }
25
- // If the value is false, it means we don't want to disclose it
26
- if (node.value === false)
27
- continue;
28
- if (!(0, utils_1.hasByPath)(prettyClaims, node.path)) {
29
- throw new Error(`Path ${node.path.join('.')} from presentation frame is not present in pretty SD-JWT payload. The presentation frame may only include properties that are present in the SD-JWT payload.`);
30
- }
31
- let path = [...node.path];
32
- while (!(0, utils_1.hasByPath)(payloadDisclosureMapping, path)) {
33
- if (path.pop() === undefined)
34
- break;
35
- }
36
- // There are no disclosures on this path, meaning the property is disclosed by default in the signed payload
37
- if (path.length === 0)
38
- continue;
39
- const disclosure = (0, utils_1.getByPath)(payloadDisclosureMapping, path);
40
- // If disclosure is string, it means it's already the digest
41
- if (typeof disclosure === 'string')
42
- requiredDisclosureDigests.add(disclosure);
43
- // Otherwise we want to get all the child digests as well
44
- else {
45
- for (const nestedItem of (0, traverse_1.traverseNodes)(disclosure)) {
46
- if (!nestedItem.isLeaf ||
47
- typeof nestedItem.value !== 'string') {
48
- continue;
49
- }
50
- requiredDisclosureDigests.add(nestedItem.value);
51
- }
52
- }
53
- }
54
- for (const disclosureDigest of requiredDisclosureDigests.values()) {
55
- const disclosure = disclosureMap[disclosureDigest];
56
- if (!disclosure) {
57
- throw new Error('disclosure not found');
58
- }
59
- disclosure.parentDisclosures.forEach((d) => requiredDisclosureDigests.add(d.digest));
60
- }
61
- return Array.from(requiredDisclosureDigests).map((digest) => disclosureMap[digest].disclosure);
62
- }
63
- exports.getDisclosuresForPresentationFrame = getDisclosuresForPresentationFrame;
64
- //# sourceMappingURL=presentationFrame.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"presentationFrame.js","sourceRoot":"","sources":["../src/presentationFrame.ts"],"names":[],"mappings":";;;AAAA,yCAAoD;AAEpD,2DAG4B;AAC5B,yCAA0C;AAG1C,SAAgB,kCAAkC,CAG9C,aAAsB,EACtB,iBAA6C,EAC7C,YAAqB,EACrB,cAA2C,EAAE;IAE7C,MAAM,yBAAyB,GAAG,IAAI,GAAG,EAAU,CAAA;IACnD,MAAM,aAAa,GAAG,IAAA,oCAAgB,EAAC,WAAW,CAAC,CAAA;IACnD,MAAM,wBAAwB,GAAG,IAAA,+CAA2B,EACxD,aAAa,EACb,aAAa,CAChB,CAAA;IAED,wBAAwB;IACxB,IAAI,wBAAwB,KAAK,IAAI,EAAE,CAAC;QACpC,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CACX,kEAAkE,CACrE,C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CAAC,KAAK,KAAK,QAAQ,EACtC,CAAC;oBACC,SAAQ;gBACZ,CAAC;gBACD,yBAAyB,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;YACnD,CAAC;QACL,CAAC;IACL,CAAC;IAED,KAAK,MAAM,gBAAgB,IAAI,yBAAyB,CAAC,MAAM,EAAE,EAAE,CAAC;QAChE,MAAM,UAAU,GAAG,aAAa,CAAC,gBAAgB,CAAC,CAAA;QAElD,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAA;QAC3C,CAAC;QAED,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CACvC,yBAAyB,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAC1C,CAAA;IACL,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC,GAAG,CAC5C,CAAC,MAAM,EAAE,EAAE,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,UAAU,CAC/C,CAAA;AACL,CAAC;AAxFD,gFAwFC"}
@@ -1,8 +0,0 @@
1
- export declare function traverseNodes(object: any, path?: PathNode[]): LeafNode[];
2
- type PathNode = string | number;
3
- interface LeafNode {
4
- path: PathNode[];
5
- value: any;
6
- isLeaf: boolean;
7
- }
8
- export {};