-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy path10_sd_jwt_vc.ts
167 lines (156 loc) · 5.25 KB
/
10_sd_jwt_vc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import {
IJwk,
IJwkParams,
IResolver,
IssuerMetadata,
Jwk,
JwkType,
JwsVerificationOptions,
KeyBindingJwtBuilder,
KeyBindingJWTValidationOptions,
SdJwtVcBuilder,
Sha256Hasher,
Timestamp,
TypeMetadataHelper,
} from "@iota/identity-wasm/node";
import { exportJWK, generateKeyPair, JWK, JWTHeaderParameters, JWTPayload, SignJWT } from "jose";
const vc_metadata: TypeMetadataHelper = JSON.parse(`{
"vct": "https://example.com/education_credential",
"name": "Betelgeuse Education Credential - Preliminary Version",
"description": "This is our development version of the education credential. Don't panic.",
"claims": [
{
"path": ["name"],
"display": [
{
"lang": "de-DE",
"label": "Vor- und Nachname",
"description": "Der Name des Studenten"
},
{
"lang": "en-US",
"label": "Name",
"description": "The name of the student"
}
],
"sd": "allowed"
},
{
"path": ["address"],
"display": [
{
"lang": "de-DE",
"label": "Adresse",
"description": "Adresse zum Zeitpunkt des Abschlusses"
},
{
"lang": "en-US",
"label": "Address",
"description": "Address at the time of graduation"
}
],
"sd": "always"
},
{
"path": ["address", "street_address"],
"display": [
{
"lang": "de-DE",
"label": "Straße"
},
{
"lang": "en-US",
"label": "Street Address"
}
],
"sd": "always",
"svg_id": "address_street_address"
},
{
"path": ["degrees", null],
"display": [
{
"lang": "de-DE",
"label": "Abschluss",
"description": "Der Abschluss des Studenten"
},
{
"lang": "en-US",
"label": "Degree",
"description": "Degree earned by the student"
}
],
"sd": "allowed"
}
]
}`);
const keypair_jwk = async (): Promise<[JWK, JWK]> => {
const [sk, pk] = await generateKeyPair("ES256").then(res => [res.privateKey, res.publicKey]);
const sk_jwk = await exportJWK(sk);
const pk_jwk = await exportJWK(pk);
return [sk_jwk, pk_jwk];
};
const signer = async (header: object, payload: object, sk_jwk: JWK) => {
return new SignJWT(payload as JWTPayload)
.setProtectedHeader(header as JWTHeaderParameters)
.sign(sk_jwk)
.then(jws => new TextEncoder().encode(jws));
};
export async function sdJwtVc() {
const hasher = new Sha256Hasher();
const issuer = "https://example.com/";
const [sk_jwk, pk_jwk] = await keypair_jwk();
const issuer_public_jwk = { ...pk_jwk, kty: JwkType.Ec, kid: "key1" } as IJwk;
const issuer_signer = (header: object, payload: object) => signer(header, payload, sk_jwk);
const issuer_metadata = new IssuerMetadata(issuer, { jwks: { keys: [issuer_public_jwk] } });
const dummy_resolver = {
resolve: async (input: string) => {
if (input == "https://example.com/.well-known/jwt-vc-issuer/") {
return new TextEncoder().encode(JSON.stringify(issuer_metadata.toJSON()));
}
if (input == "https://example.com/.well-known/vct/education_credential") {
return new TextEncoder().encode(JSON.stringify(vc_metadata));
}
},
} as IResolver<string, Uint8Array>;
const [holder_sk, holder_pk] = await keypair_jwk();
const holder_public_jwk = { ...holder_pk, kty: JwkType.Ec, kid: "key2" } as IJwk;
const holder_signer = (header: object, payload: object) => signer(header, payload, holder_sk);
/// Issuer creates an SD-JWT VC.
let sd_jwt_vc = await new SdJwtVcBuilder({
name: "John Doe",
address: {
street_address: "A random street",
number: "3a",
},
degree: [],
}, hasher)
.header({ kid: "key1" })
.vct("https://example.com/education_credential")
.iat(Timestamp.nowUTC())
.iss(issuer)
.requireKeyBinding({ kid: holder_public_jwk.kid })
.makeConcealable("/address/street_address")
.makeConcealable("/address")
.finish({ sign: issuer_signer }, "ES256");
console.log(`issued SD-JWT VC: ${sd_jwt_vc.toString()}`);
// Holder receives its SD-JWT VC and attaches its keybinding JWT.
const kb_jwt = await new KeyBindingJwtBuilder()
.iat(Timestamp.nowUTC())
.header({ kid: holder_public_jwk.kid })
.nonce("abcdefghi")
.aud("https://example.com/verify")
.finish(sd_jwt_vc.asSdJwt(), "ES256", { sign: holder_signer });
const { disclosures, sdJwtVc } = sd_jwt_vc.intoPresentation(hasher).attachKeyBindingJwt(kb_jwt).finish();
console.log(`presented SD-JWT VC: ${sdJwtVc}`);
// Verifier checks the presented sdJwtVc.
await sdJwtVc.validate(dummy_resolver, hasher);
sdJwtVc.validateKeyBinding(
new Jwk(holder_public_jwk as IJwkParams),
hasher,
new KeyBindingJWTValidationOptions({ nonce: "abcdefghi", jwsOptions: new JwsVerificationOptions() }),
);
console.log("The presented SdJwtVc is valid!");
}