@sd-jwt/present 0.3.2-next.98 → 0.4.0
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/CHANGELOG.md +12 -0
- package/dist/index.d.mts +11 -4
- package/dist/index.d.ts +11 -4
- package/dist/index.js +26 -21
- package/dist/index.mjs +24 -20
- package/package.json +6 -6
- package/src/present.ts +41 -25
- package/src/test/present.spec.ts +91 -20
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 0.4.0 (2024-03-08)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* add publish config ([#93](https://github.com/openwallet-foundation-labs/sd-jwt-js/issues/93)) ([2e4c5c1](https://github.com/openwallet-foundation-labs/sd-jwt-js/commit/2e4c5c176dc88e58e49d06783b7658d8ad872313))
|
|
12
|
+
* convert any usage into or typed version ([#80](https://github.com/openwallet-foundation-labs/sd-jwt-js/issues/80)) ([de4df54](https://github.com/openwallet-foundation-labs/sd-jwt-js/commit/de4df54f2a0a77fdbf97e10abac555a98e70c6e0))
|
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import { Hasher } from '@sd-jwt/types';
|
|
1
|
+
import { Hasher, PresentationFrame } from '@sd-jwt/types';
|
|
2
2
|
import { Disclosure } from '@sd-jwt/utils';
|
|
3
3
|
import { HasherSync } from '@sd-jwt/types/src/type';
|
|
4
4
|
|
|
5
5
|
declare const presentableKeys: (rawPayload: Record<string, unknown>, disclosures: Array<Disclosure>, hasher: Hasher) => Promise<string[]>;
|
|
6
6
|
declare const presentableKeysSync: (rawPayload: Record<string, unknown>, disclosures: Array<Disclosure>, hasher: HasherSync) => string[];
|
|
7
|
-
declare const present: (sdJwt: string,
|
|
8
|
-
declare const presentSync: (sdJwt: string,
|
|
7
|
+
declare const present: <T extends Record<string, unknown>>(sdJwt: string, presentFrame: PresentationFrame<T>, hasher: Hasher) => Promise<string>;
|
|
8
|
+
declare const presentSync: <T extends Record<string, unknown>>(sdJwt: string, presentFrame: PresentationFrame<T>, hasher: HasherSync) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Transform the object keys into an array of strings. We are not sorting the array in any way.
|
|
11
|
+
* @param obj The object to transform
|
|
12
|
+
* @param prefix The prefix to add to the keys
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
declare const transformPresentationFrame: <T extends object>(obj: PresentationFrame<T>, prefix?: string) => string[];
|
|
9
16
|
|
|
10
|
-
export { present, presentSync, presentableKeys, presentableKeysSync };
|
|
17
|
+
export { present, presentSync, presentableKeys, presentableKeysSync, transformPresentationFrame };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import { Hasher } from '@sd-jwt/types';
|
|
1
|
+
import { Hasher, PresentationFrame } from '@sd-jwt/types';
|
|
2
2
|
import { Disclosure } from '@sd-jwt/utils';
|
|
3
3
|
import { HasherSync } from '@sd-jwt/types/src/type';
|
|
4
4
|
|
|
5
5
|
declare const presentableKeys: (rawPayload: Record<string, unknown>, disclosures: Array<Disclosure>, hasher: Hasher) => Promise<string[]>;
|
|
6
6
|
declare const presentableKeysSync: (rawPayload: Record<string, unknown>, disclosures: Array<Disclosure>, hasher: HasherSync) => string[];
|
|
7
|
-
declare const present: (sdJwt: string,
|
|
8
|
-
declare const presentSync: (sdJwt: string,
|
|
7
|
+
declare const present: <T extends Record<string, unknown>>(sdJwt: string, presentFrame: PresentationFrame<T>, hasher: Hasher) => Promise<string>;
|
|
8
|
+
declare const presentSync: <T extends Record<string, unknown>>(sdJwt: string, presentFrame: PresentationFrame<T>, hasher: HasherSync) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Transform the object keys into an array of strings. We are not sorting the array in any way.
|
|
11
|
+
* @param obj The object to transform
|
|
12
|
+
* @param prefix The prefix to add to the keys
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
declare const transformPresentationFrame: <T extends object>(obj: PresentationFrame<T>, prefix?: string) => string[];
|
|
9
16
|
|
|
10
|
-
export { present, presentSync, presentableKeys, presentableKeysSync };
|
|
17
|
+
export { present, presentSync, presentableKeys, presentableKeysSync, transformPresentationFrame };
|
package/dist/index.js
CHANGED
|
@@ -43,13 +43,13 @@ __export(src_exports, {
|
|
|
43
43
|
present: () => present,
|
|
44
44
|
presentSync: () => presentSync,
|
|
45
45
|
presentableKeys: () => presentableKeys,
|
|
46
|
-
presentableKeysSync: () => presentableKeysSync
|
|
46
|
+
presentableKeysSync: () => presentableKeysSync,
|
|
47
|
+
transformPresentationFrame: () => transformPresentationFrame
|
|
47
48
|
});
|
|
48
49
|
module.exports = __toCommonJS(src_exports);
|
|
49
50
|
|
|
50
51
|
// src/present.ts
|
|
51
52
|
var import_types = require("@sd-jwt/types");
|
|
52
|
-
var import_utils = require("@sd-jwt/utils");
|
|
53
53
|
var import_decode = require("@sd-jwt/decode");
|
|
54
54
|
var presentableKeys = (rawPayload, disclosures, hasher) => __async(void 0, null, function* () {
|
|
55
55
|
const { disclosureKeymap } = yield (0, import_decode.unpack)(rawPayload, disclosures, hasher);
|
|
@@ -59,7 +59,7 @@ var presentableKeysSync = (rawPayload, disclosures, hasher) => {
|
|
|
59
59
|
const { disclosureKeymap } = (0, import_decode.unpackSync)(rawPayload, disclosures, hasher);
|
|
60
60
|
return Object.keys(disclosureKeymap).sort();
|
|
61
61
|
};
|
|
62
|
-
var present = (sdJwt,
|
|
62
|
+
var present = (sdJwt, presentFrame, hasher) => __async(void 0, null, function* () {
|
|
63
63
|
const { jwt, kbJwt } = (0, import_decode.splitSdJwt)(sdJwt);
|
|
64
64
|
const {
|
|
65
65
|
jwt: { payload },
|
|
@@ -67,23 +67,17 @@ var present = (sdJwt, keys, hasher) => __async(void 0, null, function* () {
|
|
|
67
67
|
} = yield (0, import_decode.decodeSdJwt)(sdJwt, hasher);
|
|
68
68
|
const { _sd_alg: alg } = (0, import_decode.getSDAlgAndPayload)(payload);
|
|
69
69
|
const hash = { alg, hasher };
|
|
70
|
+
const keys = transformPresentationFrame(presentFrame);
|
|
70
71
|
const hashmap = yield (0, import_decode.createHashMapping)(disclosures, hash);
|
|
71
72
|
const { disclosureKeymap } = yield (0, import_decode.unpack)(payload, disclosures, hasher);
|
|
72
|
-
const
|
|
73
|
-
const missingKeys = keys.filter((k) => !presentableKeys2.includes(k));
|
|
74
|
-
if (missingKeys.length > 0) {
|
|
75
|
-
throw new import_utils.SDJWTException(
|
|
76
|
-
`Invalid sd-jwt: invalid present keys: ${missingKeys.join(", ")}`
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]);
|
|
73
|
+
const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]).filter((d) => d !== void 0);
|
|
80
74
|
return [
|
|
81
75
|
jwt,
|
|
82
76
|
...presentedDisclosures.map((d) => d.encode()),
|
|
83
77
|
kbJwt != null ? kbJwt : ""
|
|
84
78
|
].join(import_types.SD_SEPARATOR);
|
|
85
79
|
});
|
|
86
|
-
var presentSync = (sdJwt,
|
|
80
|
+
var presentSync = (sdJwt, presentFrame, hasher) => {
|
|
87
81
|
const { jwt, kbJwt } = (0, import_decode.splitSdJwt)(sdJwt);
|
|
88
82
|
const {
|
|
89
83
|
jwt: { payload },
|
|
@@ -91,26 +85,37 @@ var presentSync = (sdJwt, keys, hasher) => {
|
|
|
91
85
|
} = (0, import_decode.decodeSdJwtSync)(sdJwt, hasher);
|
|
92
86
|
const { _sd_alg: alg } = (0, import_decode.getSDAlgAndPayload)(payload);
|
|
93
87
|
const hash = { alg, hasher };
|
|
88
|
+
const keys = transformPresentationFrame(presentFrame);
|
|
94
89
|
const hashmap = (0, import_decode.createHashMappingSync)(disclosures, hash);
|
|
95
90
|
const { disclosureKeymap } = (0, import_decode.unpackSync)(payload, disclosures, hasher);
|
|
96
|
-
const
|
|
97
|
-
const missingKeys = keys.filter((k) => !presentableKeys2.includes(k));
|
|
98
|
-
if (missingKeys.length > 0) {
|
|
99
|
-
throw new import_utils.SDJWTException(
|
|
100
|
-
`Invalid sd-jwt: invalid present keys: ${missingKeys.join(", ")}`
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]);
|
|
91
|
+
const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]).filter((d) => d !== void 0);
|
|
104
92
|
return [
|
|
105
93
|
jwt,
|
|
106
94
|
...presentedDisclosures.map((d) => d.encode()),
|
|
107
95
|
kbJwt != null ? kbJwt : ""
|
|
108
96
|
].join(import_types.SD_SEPARATOR);
|
|
109
97
|
};
|
|
98
|
+
var transformPresentationFrame = (obj, prefix = "") => {
|
|
99
|
+
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
100
|
+
const newPrefix = prefix ? `${prefix}.${key}` : key;
|
|
101
|
+
if (typeof value === "boolean") {
|
|
102
|
+
if (value) {
|
|
103
|
+
acc.push(newPrefix);
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
acc.push(
|
|
107
|
+
newPrefix,
|
|
108
|
+
...transformPresentationFrame(value, newPrefix)
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
return acc;
|
|
112
|
+
}, []);
|
|
113
|
+
};
|
|
110
114
|
// Annotate the CommonJS export names for ESM import in node:
|
|
111
115
|
0 && (module.exports = {
|
|
112
116
|
present,
|
|
113
117
|
presentSync,
|
|
114
118
|
presentableKeys,
|
|
115
|
-
presentableKeysSync
|
|
119
|
+
presentableKeysSync,
|
|
120
|
+
transformPresentationFrame
|
|
116
121
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -21,7 +21,6 @@ var __async = (__this, __arguments, generator) => {
|
|
|
21
21
|
|
|
22
22
|
// src/present.ts
|
|
23
23
|
import { SD_SEPARATOR } from "@sd-jwt/types";
|
|
24
|
-
import { SDJWTException } from "@sd-jwt/utils";
|
|
25
24
|
import {
|
|
26
25
|
createHashMapping,
|
|
27
26
|
decodeSdJwt,
|
|
@@ -40,7 +39,7 @@ var presentableKeysSync = (rawPayload, disclosures, hasher) => {
|
|
|
40
39
|
const { disclosureKeymap } = unpackSync(rawPayload, disclosures, hasher);
|
|
41
40
|
return Object.keys(disclosureKeymap).sort();
|
|
42
41
|
};
|
|
43
|
-
var present = (sdJwt,
|
|
42
|
+
var present = (sdJwt, presentFrame, hasher) => __async(void 0, null, function* () {
|
|
44
43
|
const { jwt, kbJwt } = splitSdJwt(sdJwt);
|
|
45
44
|
const {
|
|
46
45
|
jwt: { payload },
|
|
@@ -48,23 +47,17 @@ var present = (sdJwt, keys, hasher) => __async(void 0, null, function* () {
|
|
|
48
47
|
} = yield decodeSdJwt(sdJwt, hasher);
|
|
49
48
|
const { _sd_alg: alg } = getSDAlgAndPayload(payload);
|
|
50
49
|
const hash = { alg, hasher };
|
|
50
|
+
const keys = transformPresentationFrame(presentFrame);
|
|
51
51
|
const hashmap = yield createHashMapping(disclosures, hash);
|
|
52
52
|
const { disclosureKeymap } = yield unpack(payload, disclosures, hasher);
|
|
53
|
-
const
|
|
54
|
-
const missingKeys = keys.filter((k) => !presentableKeys2.includes(k));
|
|
55
|
-
if (missingKeys.length > 0) {
|
|
56
|
-
throw new SDJWTException(
|
|
57
|
-
`Invalid sd-jwt: invalid present keys: ${missingKeys.join(", ")}`
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]);
|
|
53
|
+
const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]).filter((d) => d !== void 0);
|
|
61
54
|
return [
|
|
62
55
|
jwt,
|
|
63
56
|
...presentedDisclosures.map((d) => d.encode()),
|
|
64
57
|
kbJwt != null ? kbJwt : ""
|
|
65
58
|
].join(SD_SEPARATOR);
|
|
66
59
|
});
|
|
67
|
-
var presentSync = (sdJwt,
|
|
60
|
+
var presentSync = (sdJwt, presentFrame, hasher) => {
|
|
68
61
|
const { jwt, kbJwt } = splitSdJwt(sdJwt);
|
|
69
62
|
const {
|
|
70
63
|
jwt: { payload },
|
|
@@ -72,25 +65,36 @@ var presentSync = (sdJwt, keys, hasher) => {
|
|
|
72
65
|
} = decodeSdJwtSync(sdJwt, hasher);
|
|
73
66
|
const { _sd_alg: alg } = getSDAlgAndPayload(payload);
|
|
74
67
|
const hash = { alg, hasher };
|
|
68
|
+
const keys = transformPresentationFrame(presentFrame);
|
|
75
69
|
const hashmap = createHashMappingSync(disclosures, hash);
|
|
76
70
|
const { disclosureKeymap } = unpackSync(payload, disclosures, hasher);
|
|
77
|
-
const
|
|
78
|
-
const missingKeys = keys.filter((k) => !presentableKeys2.includes(k));
|
|
79
|
-
if (missingKeys.length > 0) {
|
|
80
|
-
throw new SDJWTException(
|
|
81
|
-
`Invalid sd-jwt: invalid present keys: ${missingKeys.join(", ")}`
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]);
|
|
71
|
+
const presentedDisclosures = keys.map((k) => hashmap[disclosureKeymap[k]]).filter((d) => d !== void 0);
|
|
85
72
|
return [
|
|
86
73
|
jwt,
|
|
87
74
|
...presentedDisclosures.map((d) => d.encode()),
|
|
88
75
|
kbJwt != null ? kbJwt : ""
|
|
89
76
|
].join(SD_SEPARATOR);
|
|
90
77
|
};
|
|
78
|
+
var transformPresentationFrame = (obj, prefix = "") => {
|
|
79
|
+
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
80
|
+
const newPrefix = prefix ? `${prefix}.${key}` : key;
|
|
81
|
+
if (typeof value === "boolean") {
|
|
82
|
+
if (value) {
|
|
83
|
+
acc.push(newPrefix);
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
acc.push(
|
|
87
|
+
newPrefix,
|
|
88
|
+
...transformPresentationFrame(value, newPrefix)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
return acc;
|
|
92
|
+
}, []);
|
|
93
|
+
};
|
|
91
94
|
export {
|
|
92
95
|
present,
|
|
93
96
|
presentSync,
|
|
94
97
|
presentableKeys,
|
|
95
|
-
presentableKeysSync
|
|
98
|
+
presentableKeysSync,
|
|
99
|
+
transformPresentationFrame
|
|
96
100
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sd-jwt/present",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "sd-jwt draft 7 implementation in typescript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
},
|
|
39
39
|
"license": "Apache-2.0",
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@sd-jwt/crypto-nodejs": "0.
|
|
41
|
+
"@sd-jwt/crypto-nodejs": "0.4.0"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@sd-jwt/decode": "0.
|
|
45
|
-
"@sd-jwt/types": "0.
|
|
46
|
-
"@sd-jwt/utils": "0.
|
|
44
|
+
"@sd-jwt/decode": "0.4.0",
|
|
45
|
+
"@sd-jwt/types": "0.4.0",
|
|
46
|
+
"@sd-jwt/utils": "0.4.0"
|
|
47
47
|
},
|
|
48
48
|
"publishConfig": {
|
|
49
49
|
"access": "public"
|
|
@@ -61,5 +61,5 @@
|
|
|
61
61
|
"esm"
|
|
62
62
|
]
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "391a36550a66833b3d393ff0deb699b1b72cca59"
|
|
65
65
|
}
|
package/src/present.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Hasher, SD_SEPARATOR } from '@sd-jwt/types';
|
|
2
|
-
import { Disclosure
|
|
1
|
+
import { Hasher, PresentationFrame, SD_SEPARATOR } from '@sd-jwt/types';
|
|
2
|
+
import { Disclosure } from '@sd-jwt/utils';
|
|
3
3
|
import {
|
|
4
4
|
createHashMapping,
|
|
5
5
|
decodeSdJwt,
|
|
@@ -47,9 +47,9 @@ export const presentableKeysSync = (
|
|
|
47
47
|
return Object.keys(disclosureKeymap).sort();
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
export const present = async (
|
|
50
|
+
export const present = async <T extends Record<string, unknown>>(
|
|
51
51
|
sdJwt: string,
|
|
52
|
-
|
|
52
|
+
presentFrame: PresentationFrame<T>,
|
|
53
53
|
hasher: Hasher,
|
|
54
54
|
): Promise<string> => {
|
|
55
55
|
const { jwt, kbJwt } = splitSdJwt(sdJwt);
|
|
@@ -60,21 +60,15 @@ export const present = async (
|
|
|
60
60
|
|
|
61
61
|
const { _sd_alg: alg } = getSDAlgAndPayload(payload);
|
|
62
62
|
const hash = { alg, hasher };
|
|
63
|
+
const keys = transformPresentationFrame(presentFrame);
|
|
63
64
|
|
|
64
65
|
// hashmap: <digest> => <disclosure>
|
|
65
66
|
// to match the digest with the disclosure
|
|
66
67
|
const hashmap = await createHashMapping(disclosures, hash);
|
|
67
68
|
const { disclosureKeymap } = await unpack(payload, disclosures, hasher);
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
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]]);
|
|
69
|
+
const presentedDisclosures = keys
|
|
70
|
+
.map((k) => hashmap[disclosureKeymap[k]])
|
|
71
|
+
.filter((d) => d !== undefined);
|
|
78
72
|
|
|
79
73
|
return [
|
|
80
74
|
jwt,
|
|
@@ -83,9 +77,9 @@ export const present = async (
|
|
|
83
77
|
].join(SD_SEPARATOR);
|
|
84
78
|
};
|
|
85
79
|
|
|
86
|
-
export const presentSync = (
|
|
80
|
+
export const presentSync = <T extends Record<string, unknown>>(
|
|
87
81
|
sdJwt: string,
|
|
88
|
-
|
|
82
|
+
presentFrame: PresentationFrame<T>,
|
|
89
83
|
hasher: HasherSync,
|
|
90
84
|
): string => {
|
|
91
85
|
const { jwt, kbJwt } = splitSdJwt(sdJwt);
|
|
@@ -96,21 +90,16 @@ export const presentSync = (
|
|
|
96
90
|
|
|
97
91
|
const { _sd_alg: alg } = getSDAlgAndPayload(payload);
|
|
98
92
|
const hash = { alg, hasher };
|
|
93
|
+
const keys = transformPresentationFrame(presentFrame);
|
|
99
94
|
|
|
100
95
|
// hashmap: <digest> => <disclosure>
|
|
101
96
|
// to match the digest with the disclosure
|
|
102
97
|
const hashmap = createHashMappingSync(disclosures, hash);
|
|
103
98
|
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
99
|
|
|
113
|
-
const presentedDisclosures = keys
|
|
100
|
+
const presentedDisclosures = keys
|
|
101
|
+
.map((k) => hashmap[disclosureKeymap[k]])
|
|
102
|
+
.filter((d) => d !== undefined);
|
|
114
103
|
|
|
115
104
|
return [
|
|
116
105
|
jwt,
|
|
@@ -118,3 +107,30 @@ export const presentSync = (
|
|
|
118
107
|
kbJwt ?? '',
|
|
119
108
|
].join(SD_SEPARATOR);
|
|
120
109
|
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Transform the object keys into an array of strings. We are not sorting the array in any way.
|
|
113
|
+
* @param obj The object to transform
|
|
114
|
+
* @param prefix The prefix to add to the keys
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
export const transformPresentationFrame = <T extends object>(
|
|
118
|
+
obj: PresentationFrame<T>,
|
|
119
|
+
prefix = '',
|
|
120
|
+
): string[] => {
|
|
121
|
+
return Object.entries(obj).reduce<string[]>((acc, [key, value]) => {
|
|
122
|
+
const newPrefix = prefix ? `${prefix}.${key}` : key;
|
|
123
|
+
if (typeof value === 'boolean') {
|
|
124
|
+
// only add it, when it's true
|
|
125
|
+
if (value) {
|
|
126
|
+
acc.push(newPrefix);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
acc.push(
|
|
130
|
+
newPrefix,
|
|
131
|
+
...transformPresentationFrame(value as PresentationFrame<T>, newPrefix),
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return acc;
|
|
135
|
+
}, []);
|
|
136
|
+
};
|
package/src/test/present.spec.ts
CHANGED
|
@@ -5,8 +5,10 @@ import {
|
|
|
5
5
|
presentSync,
|
|
6
6
|
presentableKeys,
|
|
7
7
|
presentableKeysSync,
|
|
8
|
+
transformPresentationFrame,
|
|
8
9
|
} from '../index';
|
|
9
10
|
import { decodeSdJwt, decodeSdJwtSync } from '@sd-jwt/decode';
|
|
11
|
+
import { PresentationFrame } from '@sd-jwt/types';
|
|
10
12
|
|
|
11
13
|
describe('Present tests', () => {
|
|
12
14
|
test('presentableKeys', async () => {
|
|
@@ -38,11 +40,19 @@ describe('Present tests', () => {
|
|
|
38
40
|
'eyJ0eXAiOiJzZC1qd3QiLCJhbGciOiJFZERTQSJ9.eyJ0ZXN0Ijp7Il9zZCI6WyJqVEszMHNleDZhYV9kUk1KSWZDR056Q0FwbVB5MzRRNjNBa3QzS3hhSktzIl19LCJfc2QiOlsiME9nMi1ReG95eW1UOGNnVzZZUjVSSFpQLUJuR2tHUi1NM2ot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OGE1Yjc5MjMyYjAzYzBhIiwiMSJd~WyJiNWE2YjUzZGQwYTFmMGIwIiwienp6IiwieHh4Il0~',
|
|
59
77
|
);
|
|
60
78
|
});
|
|
61
79
|
|
|
62
|
-
test('
|
|
63
|
-
const
|
|
64
|
-
'
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
80
|
+
test('transform an object for a presentation to a list', () => {
|
|
81
|
+
const claims = {
|
|
82
|
+
firstname: 'John',
|
|
83
|
+
lastname: 'Doe',
|
|
84
|
+
ssn: '123-45-6789',
|
|
85
|
+
id: '1234',
|
|
86
|
+
data: {
|
|
87
|
+
firstname: 'John',
|
|
88
|
+
lastname: 'Doe',
|
|
89
|
+
ssn: '123-45-6789',
|
|
90
|
+
list: [{ r: 'd' }, 'b', 'c'],
|
|
91
|
+
list2: ['1', '2', '3'],
|
|
92
|
+
list3: ['1', null, 2],
|
|
93
|
+
},
|
|
94
|
+
data2: {
|
|
95
|
+
hi: 'bye',
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const presentFrame: PresentationFrame<typeof claims> = {
|
|
100
|
+
firstname: true,
|
|
101
|
+
data: {
|
|
102
|
+
firstname: true,
|
|
103
|
+
list: {
|
|
104
|
+
1: true,
|
|
105
|
+
0: {
|
|
106
|
+
r: true,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
list2: {
|
|
110
|
+
1: true,
|
|
111
|
+
},
|
|
112
|
+
list3: true,
|
|
113
|
+
},
|
|
114
|
+
data2: true,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const list = transformPresentationFrame<typeof claims>(presentFrame);
|
|
118
|
+
expect(list).toStrictEqual([
|
|
119
|
+
'firstname',
|
|
120
|
+
'data',
|
|
121
|
+
'data.firstname',
|
|
122
|
+
'data.list',
|
|
123
|
+
'data.list.0',
|
|
124
|
+
'data.list.0.r',
|
|
125
|
+
'data.list.1',
|
|
126
|
+
'data.list2',
|
|
127
|
+
'data.list2.1',
|
|
128
|
+
'data.list3',
|
|
129
|
+
'data2',
|
|
130
|
+
]);
|
|
70
131
|
});
|
|
71
132
|
|
|
72
|
-
test('
|
|
73
|
-
const
|
|
74
|
-
'
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
133
|
+
test('transform an object for a presentation to a list, but with faulty inputs', () => {
|
|
134
|
+
const claims = {
|
|
135
|
+
name: 'John',
|
|
136
|
+
address: {
|
|
137
|
+
city: 'New York',
|
|
138
|
+
street: '5th Avenue',
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const obj: PresentationFrame<typeof claims> = {
|
|
143
|
+
name: false,
|
|
144
|
+
address: {
|
|
145
|
+
city: true,
|
|
146
|
+
street: true,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
const list = transformPresentationFrame<typeof claims>(obj);
|
|
150
|
+
expect(list).toStrictEqual(['address', 'address.city', 'address.street']);
|
|
80
151
|
});
|
|
81
152
|
});
|