@sphereon/ssi-sdk.sd-jwt 0.33.1-next.3 → 0.33.1-next.68
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/dist/index.cjs +595 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +334 -0
- package/dist/index.d.ts +334 -4
- package/dist/index.js +562 -20
- package/dist/index.js.map +1 -1
- package/package.json +35 -24
- package/src/__tests__/sd-jwt-integrity.test.ts +1 -1
- package/src/__tests__/sd-jwt.test.ts +2 -1
- package/src/action-handler.ts +2 -2
- package/src/defaultCallbacks.ts +4 -3
- package/src/types.ts +1 -1
- package/src/utils.ts +4 -4
- package/dist/action-handler.d.ts +0 -89
- package/dist/action-handler.d.ts.map +0 -1
- package/dist/action-handler.js +0 -397
- package/dist/action-handler.js.map +0 -1
- package/dist/defaultCallbacks.d.ts +0 -6
- package/dist/defaultCallbacks.d.ts.map +0 -1
- package/dist/defaultCallbacks.js +0 -55
- package/dist/defaultCallbacks.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/trustAnchors.d.ts +0 -3
- package/dist/trustAnchors.d.ts.map +0 -1
- package/dist/trustAnchors.js +0 -20
- package/dist/trustAnchors.js.map +0 -1
- package/dist/types.d.ts +0 -234
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -10
- package/dist/types.js.map +0 -1
- package/dist/utils.d.ts +0 -18
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -85
- package/dist/utils.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,22 +1,564 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/action-handler.ts
|
|
5
|
+
import { SDJwt } from "@sd-jwt/core";
|
|
6
|
+
import { SDJwtVcInstance } from "@sd-jwt/sd-jwt-vc";
|
|
7
|
+
import { calculateJwkThumbprint, signatureAlgorithmFromKey } from "@sphereon/ssi-sdk-ext.key-utils";
|
|
8
|
+
import { decodeBase64url } from "@veramo/utils";
|
|
9
|
+
import Debug from "debug";
|
|
10
|
+
|
|
11
|
+
// src/defaultCallbacks.ts
|
|
12
|
+
import { digestMethodParams } from "@sphereon/ssi-sdk-ext.key-utils";
|
|
13
|
+
import { Loggers } from "@sphereon/ssi-types";
|
|
14
|
+
import { v4 } from "uuid";
|
|
15
|
+
import { fromString } from "uint8arrays/from-string";
|
|
16
|
+
var defaultGenerateDigest = /* @__PURE__ */ __name((data, alg) => {
|
|
17
|
+
return digestMethodParams(alg.includes("256") ? "SHA-256" : "SHA-512").hash(typeof data === "string" ? fromString(data, "utf-8") : new Uint8Array(data));
|
|
18
|
+
}, "defaultGenerateDigest");
|
|
19
|
+
var defaultGenerateSalt = /* @__PURE__ */ __name(() => {
|
|
20
|
+
return v4();
|
|
21
|
+
}, "defaultGenerateSalt");
|
|
22
|
+
var defaultVerifySignature = /* @__PURE__ */ __name((context) => async (data, signature, publicKey) => {
|
|
23
|
+
const result = await context.agent.jwtVerifyJwsSignature({
|
|
24
|
+
jws: `${data}.${signature}`,
|
|
25
|
+
jwk: publicKey
|
|
26
|
+
});
|
|
27
|
+
Loggers.DEFAULT.get("sd-jwt").info(`SD-JWT signature verified. Result: ${result.message}`);
|
|
28
|
+
return !result.error;
|
|
29
|
+
}, "defaultVerifySignature");
|
|
30
|
+
|
|
31
|
+
// src/trustAnchors.ts
|
|
32
|
+
var funkeTestCA = "-----BEGIN CERTIFICATE-----\nMIICeTCCAiCgAwIBAgIUB5E9QVZtmUYcDtCjKB/H3VQv72gwCgYIKoZIzj0EAwIwgYgxCzAJBgNVBAYTAkRFMQ8wDQYDVQQHDAZCZXJsaW4xHTAbBgNVBAoMFEJ1bmRlc2RydWNrZXJlaSBHbWJIMREwDwYDVQQLDAhUIENTIElERTE2MDQGA1UEAwwtU1BSSU5EIEZ1bmtlIEVVREkgV2FsbGV0IFByb3RvdHlwZSBJc3N1aW5nIENBMB4XDTI0MDUzMTA2NDgwOVoXDTM0MDUyOTA2NDgwOVowgYgxCzAJBgNVBAYTAkRFMQ8wDQYDVQQHDAZCZXJsaW4xHTAbBgNVBAoMFEJ1bmRlc2RydWNrZXJlaSBHbWJIMREwDwYDVQQLDAhUIENTIElERTE2MDQGA1UEAwwtU1BSSU5EIEZ1bmtlIEVVREkgV2FsbGV0IFByb3RvdHlwZSBJc3N1aW5nIENBMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEYGzdwFDnc7+Kn5ibAvCOM8ke77VQxqfMcwZL8IaIA+WCROcCfmY/giH92qMru5p/kyOivE0RC/IbdMONvDoUyaNmMGQwHQYDVR0OBBYEFNRWGMCJOOgOWIQYyXZiv6u7xZC+MB8GA1UdIwQYMBaAFNRWGMCJOOgOWIQYyXZiv6u7xZC+MBIGA1UdEwEB/wQIMAYBAf8CAQAwDgYDVR0PAQH/BAQDAgGGMAoGCCqGSM49BAMCA0cAMEQCIGEm7wkZKHt/atb4MdFnXW6yrnwMUT2u136gdtl10Y6hAiBuTFqvVYth1rbxzCP0xWZHmQK9kVyxn8GPfX27EIzzsw==\n-----END CERTIFICATE-----";
|
|
33
|
+
var sphereonCA = "-----BEGIN CERTIFICATE-----\nMIICCDCCAa6gAwIBAgITAPMgqwtYzWPBXaobHhxG9iSydTAKBggqhkjOPQQDAjBa\nMQswCQYDVQQGEwJOTDEkMCIGA1UECgwbU3BoZXJlb24gSW50ZXJuYXRpb25hbCBC\nLlYuMQswCQYDVQQLDAJJVDEYMBYGA1UEAwwPY2Euc3BoZXJlb24uY29tMB4XDTI0\nMDcyODIxMjY0OVoXDTM0MDcyODIxMjY0OVowWjELMAkGA1UEBhMCTkwxJDAiBgNV\nBAoMG1NwaGVyZW9uIEludGVybmF0aW9uYWwgQi5WLjELMAkGA1UECwwCSVQxGDAW\nBgNVBAMMD2NhLnNwaGVyZW9uLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IA\nBEiA0KeESSNrOcmCDga8YsBkUTgowZGwqvL2n91JUpAMdRSwvlVFdqdiLXnk2pQq\nT1vZnDG0I+x+iz2EbdsG0aajUzBRMB0GA1UdDgQWBBTnB8pdlVz5yKD+zuNkRR6A\nsywywTAOBgNVHQ8BAf8EBAMCAaYwDwYDVR0lBAgwBgYEVR0lADAPBgNVHRMBAf8E\nBTADAQH/MAoGCCqGSM49BAMCA0gAMEUCIHH7ie1OAAbff5262rzZVQa8J9zENG8A\nQlHHFydMdgaXAiEA1Ib82mhHIYDziE0DDbHEAXOs98al+7dpo8fPGVGTeKI=\n-----END CERTIFICATE-----";
|
|
34
|
+
|
|
35
|
+
// src/utils.ts
|
|
36
|
+
import { toString } from "uint8arrays/to-string";
|
|
37
|
+
async function fetchUrlWithErrorHandling(url) {
|
|
38
|
+
const response = await fetch(url);
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
throw new Error(`${response.status}: ${response.statusText}`);
|
|
41
|
+
}
|
|
42
|
+
return response;
|
|
43
|
+
}
|
|
44
|
+
__name(fetchUrlWithErrorHandling, "fetchUrlWithErrorHandling");
|
|
45
|
+
function extractHashAlgFromIntegrity(integrityValue) {
|
|
46
|
+
const val = integrityValue?.toLowerCase().trim().split("-")[0];
|
|
47
|
+
if (val === "sha256" || val === "sha384" || val === "sha512") {
|
|
48
|
+
return val;
|
|
49
|
+
}
|
|
50
|
+
return void 0;
|
|
51
|
+
}
|
|
52
|
+
__name(extractHashAlgFromIntegrity, "extractHashAlgFromIntegrity");
|
|
53
|
+
function extractHashFromIntegrity(integrityValue) {
|
|
54
|
+
return integrityValue?.toLowerCase().trim().split("-")[1];
|
|
55
|
+
}
|
|
56
|
+
__name(extractHashFromIntegrity, "extractHashFromIntegrity");
|
|
57
|
+
async function validateIntegrity({ input, integrityValue, hasher }) {
|
|
58
|
+
if (!integrityValue) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
const alg = extractHashAlgFromIntegrity(integrityValue);
|
|
62
|
+
if (!alg) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const calculatedHash = await createIntegrity({
|
|
66
|
+
hasher,
|
|
67
|
+
input,
|
|
68
|
+
alg
|
|
69
|
+
});
|
|
70
|
+
return calculatedHash == integrityValue;
|
|
71
|
+
}
|
|
72
|
+
__name(validateIntegrity, "validateIntegrity");
|
|
73
|
+
async function createIntegrity({ input, hasher, alg = "sha256" }) {
|
|
74
|
+
const calculatedHash = await hasher(typeof input === "string" ? input : JSON.stringify(input), alg);
|
|
75
|
+
return `${alg}-${toString(calculatedHash, "base64")}`;
|
|
76
|
+
}
|
|
77
|
+
__name(createIntegrity, "createIntegrity");
|
|
78
|
+
function assertValidTypeMetadata(metadata, vct) {
|
|
79
|
+
if (metadata.vct !== vct) {
|
|
80
|
+
throw new Error("VCT mismatch in metadata and credential");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
__name(assertValidTypeMetadata, "assertValidTypeMetadata");
|
|
84
|
+
|
|
85
|
+
// src/action-handler.ts
|
|
86
|
+
var debug = Debug("@sphereon/ssi-sdk.sd-jwt");
|
|
87
|
+
var SDJwtPlugin = class {
|
|
88
|
+
static {
|
|
89
|
+
__name(this, "SDJwtPlugin");
|
|
90
|
+
}
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
trustAnchorsInPEM;
|
|
93
|
+
registeredImplementations;
|
|
94
|
+
_signers;
|
|
95
|
+
_defaultSigner;
|
|
96
|
+
constructor(registeredImplementations, trustAnchorsInPEM) {
|
|
97
|
+
this.trustAnchorsInPEM = trustAnchorsInPEM ?? [];
|
|
98
|
+
if (!registeredImplementations) {
|
|
99
|
+
registeredImplementations = {};
|
|
100
|
+
}
|
|
101
|
+
if (typeof registeredImplementations?.hasher !== "function") {
|
|
102
|
+
registeredImplementations.hasher = defaultGenerateDigest;
|
|
103
|
+
}
|
|
104
|
+
if (typeof registeredImplementations?.saltGenerator !== "function") {
|
|
105
|
+
registeredImplementations.saltGenerator = defaultGenerateSalt;
|
|
106
|
+
}
|
|
107
|
+
this.registeredImplementations = registeredImplementations;
|
|
108
|
+
this._signers = registeredImplementations?.signers ?? {};
|
|
109
|
+
this._defaultSigner = registeredImplementations?.defaultSigner;
|
|
110
|
+
}
|
|
111
|
+
// map the methods your plugin is declaring to their implementation
|
|
112
|
+
methods = {
|
|
113
|
+
createSdJwtVc: this.createSdJwtVc.bind(this),
|
|
114
|
+
createSdJwtPresentation: this.createSdJwtPresentation.bind(this),
|
|
115
|
+
verifySdJwtVc: this.verifySdJwtVc.bind(this),
|
|
116
|
+
verifySdJwtPresentation: this.verifySdJwtPresentation.bind(this),
|
|
117
|
+
fetchSdJwtTypeMetadataFromVctUrl: this.fetchSdJwtTypeMetadataFromVctUrl.bind(this)
|
|
118
|
+
};
|
|
119
|
+
async getSignerForIdentifier(args, context) {
|
|
120
|
+
const { identifier, resolution } = args;
|
|
121
|
+
if (Object.keys(this._signers).includes(identifier) && typeof this._signers[identifier] === "function") {
|
|
122
|
+
return {
|
|
123
|
+
signer: this._signers[identifier]
|
|
124
|
+
};
|
|
125
|
+
} else if (typeof this._defaultSigner === "function") {
|
|
126
|
+
return {
|
|
127
|
+
signer: this._defaultSigner
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const signingKey = await this.getSignKey({
|
|
131
|
+
identifier,
|
|
132
|
+
vmRelationship: "assertionMethod",
|
|
133
|
+
resolution
|
|
134
|
+
}, context);
|
|
135
|
+
const { key, alg } = signingKey;
|
|
136
|
+
const signer = /* @__PURE__ */ __name(async (data) => {
|
|
137
|
+
return context.agent.keyManagerSign({
|
|
138
|
+
keyRef: key.kmsKeyRef,
|
|
139
|
+
data
|
|
140
|
+
});
|
|
141
|
+
}, "signer");
|
|
142
|
+
return {
|
|
143
|
+
signer,
|
|
144
|
+
alg,
|
|
145
|
+
signingKey
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Create a signed SD-JWT credential.
|
|
150
|
+
* @param args - Arguments necessary for the creation of a SD-JWT credential.
|
|
151
|
+
* @param context - This reserved param is automatically added and handled by the framework, *do not override*
|
|
152
|
+
* @returns A signed SD-JWT credential.
|
|
153
|
+
*/
|
|
154
|
+
async createSdJwtVc(args, context) {
|
|
155
|
+
const issuer = args.credentialPayload.iss;
|
|
156
|
+
if (!issuer) {
|
|
157
|
+
throw new Error("credential.issuer must not be empty");
|
|
158
|
+
}
|
|
159
|
+
const { alg, signer, signingKey } = await this.getSignerForIdentifier({
|
|
160
|
+
identifier: issuer,
|
|
161
|
+
resolution: args.resolution
|
|
162
|
+
}, context);
|
|
163
|
+
const sdjwt = new SDJwtVcInstance({
|
|
164
|
+
signer,
|
|
165
|
+
hasher: this.registeredImplementations.hasher,
|
|
166
|
+
saltGenerator: this.registeredImplementations.saltGenerator,
|
|
167
|
+
signAlg: alg ?? "ES256",
|
|
168
|
+
hashAlg: "sha-256"
|
|
169
|
+
});
|
|
170
|
+
const credential = await sdjwt.issue(args.credentialPayload, args.disclosureFrame, {
|
|
171
|
+
header: {
|
|
172
|
+
...signingKey?.key.kid !== void 0 && {
|
|
173
|
+
kid: signingKey.key.kid
|
|
174
|
+
},
|
|
175
|
+
...signingKey?.key.x5c !== void 0 && {
|
|
176
|
+
x5c: signingKey.key.x5c
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
return {
|
|
181
|
+
credential
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Get the key to sign the SD-JWT
|
|
186
|
+
* @param args - consists of twp arguments: identifier like a did and other forms of identifiers and vmRelationship which represents the purpose of the key
|
|
187
|
+
* @param context - agent instance
|
|
188
|
+
* @returns the key to sign the SD-JWT
|
|
189
|
+
*/
|
|
190
|
+
async getSignKey(args, context) {
|
|
191
|
+
const { identifier, resolution } = {
|
|
192
|
+
...args
|
|
193
|
+
};
|
|
194
|
+
if (resolution) {
|
|
195
|
+
const key = resolution.key;
|
|
196
|
+
const alg = await signatureAlgorithmFromKey({
|
|
197
|
+
key
|
|
198
|
+
});
|
|
199
|
+
switch (resolution.method) {
|
|
200
|
+
case "did":
|
|
201
|
+
debug(`Signing key ${key.publicKeyHex} found for identifier ${identifier}`);
|
|
202
|
+
return {
|
|
203
|
+
alg,
|
|
204
|
+
key: {
|
|
205
|
+
...key,
|
|
206
|
+
kmsKeyRef: resolution.kmsKeyRef,
|
|
207
|
+
kid: resolution.kid
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
default:
|
|
211
|
+
if (key.meta?.x509 && key.meta.x509.x5c) {
|
|
212
|
+
return {
|
|
213
|
+
alg,
|
|
214
|
+
key: {
|
|
215
|
+
kid: resolution.kid,
|
|
216
|
+
kmsKeyRef: resolution.kmsKeyRef,
|
|
217
|
+
x5c: key.meta.x509.x5c
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
} else if (key.meta?.jwkThumbprint) {
|
|
221
|
+
return {
|
|
222
|
+
alg,
|
|
223
|
+
key: {
|
|
224
|
+
kid: resolution.kid,
|
|
225
|
+
kmsKeyRef: resolution.kmsKeyRef,
|
|
226
|
+
jwkThumbprint: key.meta.jwkThumbprint
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
} else {
|
|
230
|
+
return {
|
|
231
|
+
alg,
|
|
232
|
+
key: {
|
|
233
|
+
kid: resolution.kid,
|
|
234
|
+
kmsKeyRef: resolution.kmsKeyRef
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
} else if (identifier.startsWith("did:")) {
|
|
240
|
+
const didIdentifier = await context.agent.identifierManagedGetByDid({
|
|
241
|
+
identifier
|
|
242
|
+
});
|
|
243
|
+
if (!didIdentifier) {
|
|
244
|
+
throw new Error(`No identifier found with the given did: ${identifier}`);
|
|
245
|
+
}
|
|
246
|
+
const key = didIdentifier.key;
|
|
247
|
+
const alg = await signatureAlgorithmFromKey({
|
|
248
|
+
key
|
|
249
|
+
});
|
|
250
|
+
debug(`Signing key ${key.publicKeyHex} found for identifier ${identifier}`);
|
|
251
|
+
return {
|
|
252
|
+
alg,
|
|
253
|
+
key: {
|
|
254
|
+
...key,
|
|
255
|
+
kmsKeyRef: didIdentifier.kmsKeyRef,
|
|
256
|
+
kid: didIdentifier.kid
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
} else {
|
|
260
|
+
const kidIdentifier = await context.agent.identifierManagedGetByKid({
|
|
261
|
+
identifier
|
|
262
|
+
});
|
|
263
|
+
if (!kidIdentifier) {
|
|
264
|
+
throw new Error(`No identifier found with the given kid: ${identifier}`);
|
|
265
|
+
}
|
|
266
|
+
const key = kidIdentifier.key;
|
|
267
|
+
const alg = await signatureAlgorithmFromKey({
|
|
268
|
+
key
|
|
269
|
+
});
|
|
270
|
+
if (key.meta?.x509 && key.meta.x509.x5c) {
|
|
271
|
+
return {
|
|
272
|
+
alg,
|
|
273
|
+
key: {
|
|
274
|
+
kid: kidIdentifier.kid,
|
|
275
|
+
kmsKeyRef: kidIdentifier.kmsKeyRef,
|
|
276
|
+
x5c: key.meta.x509.x5c
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
} else if (key.meta?.jwkThumbprint) {
|
|
280
|
+
return {
|
|
281
|
+
alg,
|
|
282
|
+
key: {
|
|
283
|
+
kid: kidIdentifier.kid,
|
|
284
|
+
kmsKeyRef: kidIdentifier.kmsKeyRef,
|
|
285
|
+
jwkThumbprint: key.meta.jwkThumbprint
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
} else {
|
|
289
|
+
return {
|
|
290
|
+
alg,
|
|
291
|
+
key: {
|
|
292
|
+
kid: kidIdentifier.kid,
|
|
293
|
+
kmsKeyRef: kidIdentifier.kmsKeyRef
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Create a signed SD-JWT presentation.
|
|
301
|
+
* @param args - Arguments necessary for the creation of a SD-JWT presentation.
|
|
302
|
+
* @param context - This reserved param is automatically added and handled by the framework, *do not override*
|
|
303
|
+
* @returns A signed SD-JWT presentation.
|
|
304
|
+
*/
|
|
305
|
+
async createSdJwtPresentation(args, context) {
|
|
306
|
+
const cred = await SDJwt.fromEncode(args.presentation, this.registeredImplementations.hasher);
|
|
307
|
+
const claims = await cred.getClaims(this.registeredImplementations.hasher);
|
|
308
|
+
let holder;
|
|
309
|
+
if (args.holder) {
|
|
310
|
+
holder = args.holder;
|
|
311
|
+
} else if (claims.cnf?.jwk) {
|
|
312
|
+
const jwk = claims.cnf.jwk;
|
|
313
|
+
holder = calculateJwkThumbprint({
|
|
314
|
+
jwk
|
|
315
|
+
});
|
|
316
|
+
} else if (claims.cnf?.kid) {
|
|
317
|
+
holder = claims.cnf?.kid;
|
|
318
|
+
} else if (claims.sub) {
|
|
319
|
+
holder = claims.sub;
|
|
320
|
+
} else {
|
|
321
|
+
throw new Error("invalid_argument: credential does not include a holder reference");
|
|
322
|
+
}
|
|
323
|
+
const { alg, signer } = await this.getSignerForIdentifier({
|
|
324
|
+
identifier: holder
|
|
325
|
+
}, context);
|
|
326
|
+
const sdjwt = new SDJwtVcInstance({
|
|
327
|
+
hasher: this.registeredImplementations.hasher ?? defaultGenerateDigest,
|
|
328
|
+
saltGenerator: this.registeredImplementations.saltGenerator,
|
|
329
|
+
kbSigner: signer,
|
|
330
|
+
kbSignAlg: alg ?? "ES256"
|
|
331
|
+
});
|
|
332
|
+
const presentation = await sdjwt.present(args.presentation, args.presentationFrame, {
|
|
333
|
+
kb: args.kb
|
|
334
|
+
});
|
|
335
|
+
return {
|
|
336
|
+
presentation
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Verify a signed SD-JWT credential.
|
|
341
|
+
* @param args - Arguments necessary for the verify a SD-JWT credential.
|
|
342
|
+
* @param context - This reserved param is automatically added and handled by the framework, *do not override*
|
|
343
|
+
* @returns
|
|
344
|
+
*/
|
|
345
|
+
async verifySdJwtVc(args, context) {
|
|
346
|
+
const verifier = /* @__PURE__ */ __name(async (data, signature) => this.verify(sdjwt, context, data, signature), "verifier");
|
|
347
|
+
const sdjwt = new SDJwtVcInstance({
|
|
348
|
+
verifier,
|
|
349
|
+
hasher: this.registeredImplementations.hasher ?? defaultGenerateDigest
|
|
350
|
+
});
|
|
351
|
+
const { header = {}, payload, kb } = await sdjwt.verify(args.credential);
|
|
352
|
+
return {
|
|
353
|
+
header,
|
|
354
|
+
payload,
|
|
355
|
+
kb
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Verify the key binding of a SD-JWT by validating the signature of the key bound to the SD-JWT
|
|
360
|
+
* @param sdjwt - SD-JWT instance
|
|
361
|
+
* @param context - This reserved param is automatically added and handled by the framework, *do not override*
|
|
362
|
+
* @param data - signed data
|
|
363
|
+
* @param signature - The signature
|
|
364
|
+
* @param payload - The payload of the SD-JWT
|
|
365
|
+
* @returns
|
|
366
|
+
*/
|
|
367
|
+
verifyKb(sdjwt, context, data, signature, payload) {
|
|
368
|
+
if (!payload.cnf) {
|
|
369
|
+
throw Error("other method than cnf is not supported yet");
|
|
370
|
+
}
|
|
371
|
+
return this.verifySignatureCallback(context)(data, signature, this.getJwk(payload));
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Validates the signature of a SD-JWT
|
|
375
|
+
* @param sdjwt - SD-JWT instance
|
|
376
|
+
* @param context - This reserved param is automatically added and handled by the framework, *do not override*
|
|
377
|
+
* @param data - signed data
|
|
378
|
+
* @param signature - The signature
|
|
379
|
+
* @returns
|
|
380
|
+
*/
|
|
381
|
+
async verify(sdjwt, context, data, signature, opts) {
|
|
382
|
+
const decodedVC = await sdjwt.decode(`${data}.${signature}`);
|
|
383
|
+
const issuer = decodedVC.jwt.payload.iss;
|
|
384
|
+
const header = decodedVC.jwt.header;
|
|
385
|
+
const x5c = header?.x5c;
|
|
386
|
+
let jwk = header.jwk;
|
|
387
|
+
if (x5c) {
|
|
388
|
+
const trustAnchors = /* @__PURE__ */ new Set([
|
|
389
|
+
...this.trustAnchorsInPEM
|
|
390
|
+
]);
|
|
391
|
+
if (trustAnchors.size === 0) {
|
|
392
|
+
trustAnchors.add(sphereonCA);
|
|
393
|
+
trustAnchors.add(funkeTestCA);
|
|
394
|
+
}
|
|
395
|
+
const certificateValidationResult = await context.agent.x509VerifyCertificateChain({
|
|
396
|
+
chain: x5c,
|
|
397
|
+
trustAnchors: Array.from(trustAnchors),
|
|
398
|
+
// TODO: Defaults to allowing untrusted certs! Fine for now, not when wallets go mainstream
|
|
399
|
+
opts: opts?.x5cValidation ?? {
|
|
400
|
+
trustRootWhenNoAnchors: true,
|
|
401
|
+
allowNoTrustAnchorsFound: true
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
if (certificateValidationResult.error || !certificateValidationResult?.certificateChain) {
|
|
405
|
+
return Promise.reject(Error(`Certificate chain validation failed. ${certificateValidationResult.message}`));
|
|
406
|
+
}
|
|
407
|
+
const certInfo = certificateValidationResult.certificateChain[0];
|
|
408
|
+
jwk = certInfo.publicKeyJWK;
|
|
409
|
+
}
|
|
410
|
+
if (!jwk && header.kid?.includes("did:")) {
|
|
411
|
+
const didDoc = await context.agent.resolveDid({
|
|
412
|
+
didUrl: header.kid
|
|
413
|
+
});
|
|
414
|
+
if (!didDoc) {
|
|
415
|
+
throw new Error("invalid_issuer: issuer did not resolve to a did document");
|
|
416
|
+
}
|
|
417
|
+
const didDocumentKey = didDoc.didDocument?.verificationMethod?.find((key) => key.id);
|
|
418
|
+
if (!didDocumentKey) {
|
|
419
|
+
throw new Error("invalid_issuer: issuer did document does not include referenced key");
|
|
420
|
+
}
|
|
421
|
+
jwk = didDocumentKey.publicKeyJwk;
|
|
422
|
+
}
|
|
423
|
+
if (!jwk && issuer.includes("did:")) {
|
|
424
|
+
const didDoc = await context.agent.resolveDid({
|
|
425
|
+
didUrl: issuer
|
|
426
|
+
});
|
|
427
|
+
if (!didDoc) {
|
|
428
|
+
throw new Error("invalid_issuer: issuer did not resolve to a did document");
|
|
429
|
+
}
|
|
430
|
+
const didDocumentKey = didDoc.didDocument?.verificationMethod?.find((key) => key.id);
|
|
431
|
+
if (!didDocumentKey) {
|
|
432
|
+
throw new Error("invalid_issuer: issuer did document does not include referenced key");
|
|
433
|
+
}
|
|
434
|
+
jwk = didDocumentKey.publicKeyJwk;
|
|
435
|
+
}
|
|
436
|
+
if (!jwk) {
|
|
437
|
+
throw new Error("No valid public key found for signature verification");
|
|
438
|
+
}
|
|
439
|
+
return this.verifySignatureCallback(context)(data, signature, jwk);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Verify a signed SD-JWT presentation.
|
|
443
|
+
* @param args - Arguments necessary for the verify a SD-JWT presentation.
|
|
444
|
+
* @param context - This reserved param is automatically added and handled by the framework, *do not override*
|
|
445
|
+
* @returns
|
|
446
|
+
*/
|
|
447
|
+
async verifySdJwtPresentation(args, context) {
|
|
448
|
+
let sdjwt;
|
|
449
|
+
const verifier = /* @__PURE__ */ __name(async (data, signature) => this.verify(sdjwt, context, data, signature), "verifier");
|
|
450
|
+
const verifierKb = /* @__PURE__ */ __name(async (data, signature, payload) => this.verifyKb(sdjwt, context, data, signature, payload), "verifierKb");
|
|
451
|
+
sdjwt = new SDJwtVcInstance({
|
|
452
|
+
verifier,
|
|
453
|
+
hasher: this.registeredImplementations.hasher,
|
|
454
|
+
kbVerifier: verifierKb
|
|
455
|
+
});
|
|
456
|
+
return sdjwt.verify(args.presentation, args.requiredClaimKeys, args.kb);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Fetch and validate Type Metadata.
|
|
460
|
+
* @param args - Arguments necessary for fetching and validating the type metadata.
|
|
461
|
+
* @param context - This reserved param is automatically added and handled by the framework, *do not override*
|
|
462
|
+
* @returns
|
|
463
|
+
*/
|
|
464
|
+
async fetchSdJwtTypeMetadataFromVctUrl(args, context) {
|
|
465
|
+
const { vct, vctIntegrity, opts } = args;
|
|
466
|
+
const url = new URL(vct);
|
|
467
|
+
const response = await fetchUrlWithErrorHandling(url.toString());
|
|
468
|
+
const metadata = await response.json();
|
|
469
|
+
assertValidTypeMetadata(metadata, vct);
|
|
470
|
+
const validate = /* @__PURE__ */ __name(async (vct2, input, integrityValue, hasher2) => {
|
|
471
|
+
if (hasher2 && integrityValue) {
|
|
472
|
+
const validation = await validateIntegrity({
|
|
473
|
+
integrityValue,
|
|
474
|
+
input,
|
|
475
|
+
hasher: hasher2
|
|
476
|
+
});
|
|
477
|
+
if (!validation) {
|
|
478
|
+
return Promise.reject(Error(`Integrity check failed for vct: ${vct2}, extends: ${metadata.extends}, integrity: ${integrityValue}}`));
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}, "validate");
|
|
482
|
+
const hasher = opts?.hasher ?? this.registeredImplementations.hasher ?? defaultGenerateDigest;
|
|
483
|
+
if (hasher) {
|
|
484
|
+
if (vctIntegrity) {
|
|
485
|
+
await validate(vct, metadata, vctIntegrity, hasher);
|
|
486
|
+
const vctValidation = await validateIntegrity({
|
|
487
|
+
integrityValue: vctIntegrity,
|
|
488
|
+
input: metadata,
|
|
489
|
+
hasher
|
|
490
|
+
});
|
|
491
|
+
if (!vctValidation) {
|
|
492
|
+
return Promise.reject(Error(`Integrity check failed for vct: ${vct}, integrity: ${vctIntegrity}`));
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (metadata["extends#integrity"]) {
|
|
496
|
+
const extendsMetadata = await this.fetchSdJwtTypeMetadataFromVctUrl({
|
|
497
|
+
vct: metadata["extends#integrity"],
|
|
498
|
+
opts
|
|
499
|
+
}, context);
|
|
500
|
+
await validate(vct, extendsMetadata, metadata["extends#integrity"], hasher);
|
|
501
|
+
}
|
|
502
|
+
if (metadata["schema_uri#integrity"]) {
|
|
503
|
+
const schemaResponse = await fetchUrlWithErrorHandling(metadata.schema_uri);
|
|
504
|
+
const schema = await schemaResponse.json();
|
|
505
|
+
await validate(vct, schema, metadata["schema_uri#integrity"], hasher);
|
|
506
|
+
}
|
|
507
|
+
metadata.display?.forEach((display) => {
|
|
508
|
+
const simpleLogoIntegrity = display.rendering?.simple?.logo?.["uri#integrity"];
|
|
509
|
+
if (simpleLogoIntegrity) {
|
|
510
|
+
console.log("TODO: Logo integrity check");
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
return metadata;
|
|
515
|
+
}
|
|
516
|
+
verifySignatureCallback(context) {
|
|
517
|
+
if (typeof this.registeredImplementations.verifySignature === "function") {
|
|
518
|
+
return this.registeredImplementations.verifySignature;
|
|
519
|
+
}
|
|
520
|
+
return defaultVerifySignature(context);
|
|
521
|
+
}
|
|
522
|
+
getJwk(payload) {
|
|
523
|
+
if (payload.cnf?.jwk !== void 0) {
|
|
524
|
+
return payload.cnf.jwk;
|
|
525
|
+
} else if (payload.cnf !== void 0 && "kid" in payload.cnf && typeof payload.cnf.kid === "string" && payload.cnf.kid.startsWith("did:jwk:")) {
|
|
526
|
+
const encoded = this.extractBase64FromDIDJwk(payload.cnf.kid);
|
|
527
|
+
const decoded = decodeBase64url(encoded);
|
|
528
|
+
const jwt = JSON.parse(decoded);
|
|
529
|
+
return jwt;
|
|
530
|
+
}
|
|
531
|
+
throw Error("Unable to extract JWK from SD-JWT payload");
|
|
532
|
+
}
|
|
533
|
+
extractBase64FromDIDJwk(did) {
|
|
534
|
+
const parts = did.split(":");
|
|
535
|
+
if (parts.length < 3) {
|
|
536
|
+
throw new Error("Invalid DID format");
|
|
537
|
+
}
|
|
538
|
+
return parts[2].split("#")[0];
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
// src/types.ts
|
|
543
|
+
import { contextHasPlugin } from "@sphereon/ssi-sdk.agent-config";
|
|
544
|
+
var sdJwtPluginContextMethods = [
|
|
545
|
+
"createSdJwtVc",
|
|
546
|
+
"createSdJwtPresentation",
|
|
547
|
+
"verifySdJwtVc",
|
|
548
|
+
"verifySdJwtPresentation"
|
|
549
|
+
];
|
|
550
|
+
function contextHasSDJwtPlugin(context) {
|
|
551
|
+
return contextHasPlugin(context, "verifySdJwtVc");
|
|
552
|
+
}
|
|
553
|
+
__name(contextHasSDJwtPlugin, "contextHasSDJwtPlugin");
|
|
554
|
+
export {
|
|
555
|
+
SDJwtPlugin,
|
|
556
|
+
assertValidTypeMetadata,
|
|
557
|
+
contextHasSDJwtPlugin,
|
|
558
|
+
createIntegrity,
|
|
559
|
+
extractHashFromIntegrity,
|
|
560
|
+
fetchUrlWithErrorHandling,
|
|
561
|
+
sdJwtPluginContextMethods,
|
|
562
|
+
validateIntegrity
|
|
15
563
|
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.SDJwtPlugin = void 0;
|
|
18
|
-
var action_handler_1 = require("./action-handler");
|
|
19
|
-
Object.defineProperty(exports, "SDJwtPlugin", { enumerable: true, get: function () { return action_handler_1.SDJwtPlugin; } });
|
|
20
|
-
__exportStar(require("./utils"), exports);
|
|
21
|
-
__exportStar(require("./types"), exports);
|
|
22
564
|
//# sourceMappingURL=index.js.map
|