generated from credential-handler/chapi-demo-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet-worker.html
230 lines (190 loc) · 7.2 KB
/
wallet-worker.html
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html lang="en">
<head>
<title>Minimal Dev Out-of-Band Wallet - Worker</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified Materialize JS/CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="config.js"></script>
<script src="js-helpers.js"></script>
<script src="mock-user-management.js"></script>
<!--Simple cookie lib-->
<script src="https://cdn.jsdelivr.net/npm/js-cookie@beta/dist/js.cookie.min.js"></script>
</head>
<body>
<div id="get" class="container hide">
<h5>Wallet get() event</h5>
<div class="card-panel hide logged-in">
<div>
<p>Origin <span class="requestOrigin"></span> is requesting information:</p>
<p><span id="requestReason"></span></p>
</div>
<div class="userArea">
<p><strong>Logged in:</strong> <span class="username"></span></p>
<a class="waves-effect waves-light btn-small logoutButton">Reset and Logout</a>
<h6>Wallet Contents:</h6>
<ol class="walletContents"></ol>
</div>
</div>
<div class="card-panel hide logged-out">
<p>
In order to share a credential with the requesting party:
</p>
<ol>
<li>Register a wallet with your browser (for example,
the <a href="https://wallet-oob.example.chapi.io/">Demo Out-of-Band Wallet</a>).</li>
<li>Click the <strong>Login</strong> button.</li>
<li>Click on a Share button next to an appropriate credential.</li>
</ol>
<a class="waves-effect waves-light btn loginButton">Login</a>
</div>
</div>
<div id="store" class="container hide">
<h5>Wallet store() event</h5>
<div class="card-panel hide logged-in">
<div id="confirm">
<p>Origin <span class="requestOrigin"></span> is offering this credential to you:</p>
<p>Do you wish to store this credential?</p>
<p><strong>type:</strong> <span id="credentialType"></span></p>
<p><strong>issuer:</strong> <span id="credentialIssuer"></span></p>
<a class="waves-effect waves-light btn" id="confirmButton">Confirm</a>
<a class="waves-effect waves-light btn" id="cancelButton">Cancel</a>
</div>
<div class="userArea hide">
<p><strong>Logged in:</strong> <span class="username"></span></p>
<a class="waves-effect waves-light btn-small logoutButton">Reset and Logout</a>
<p><strong>Credential stored!</strong></p>
<h6>Wallet Contents:</h6>
<ol class="walletContents"></ol>
<a class="waves-effect waves-light btn center" id="doneButton">Done</a>
</div>
</div>
<div class="card-panel hide logged-out">
<p>
In order to store a credential:
</p>
<ol>
<li>Register a wallet with your browser (for example,
the <a href="https://wallet-oob.example.chapi.io/">Demo Out-of-Band Wallet</a>).</li>
<li>Click the <strong>Login</strong> button.</li>
</ol>
<a class="waves-effect waves-light btn loginButton">Login</a>
</div>
</div>
<script>
function handleRequest() {
let request;
try {
const parsed = new URL(window.location.href);
request = JSON.parse(parsed.searchParams.get('request'));
} catch(e) {
console.error('Error parsing credential request.');
throw e;
}
console.log('Received credential request', request);
const {credentialRequestOrigin, protocols} = request;
document.querySelectorAll('.requestOrigin').forEach(
e => e.innerHTML = credentialRequestOrigin);
// parse VP/VPR from "fakeX" query params (not a part of OID4VP nor vcapi,
// but as the demo issuer / verifier do not actually implement those
// protocols at this time we simulate it this way
if(protocols.OID4VC) {
// OID4VC storage request
const url = protocols.OID4VC;
const parsed = new URL(url);
const vpJSON = parsed.searchParams.get('fake');
if(!vpJSON) {
throw new Error(
'Expected "fake" query param in OID4VC / vcapi URL for demo purposes.');
}
const vp = JSON.parse(vpJSON);
storeCredential(vp);
} else if(protocols.OID4VP) {
// OID4VP query
const url = protocols.OID4VP;
const parsed = new URL(url);
const vprJSON = parsed.searchParams.get('fake');
if(!vprJSON) {
throw new Error(
'Expected "fake" query param in OID4VP URL for demo purposes.');
}
const vpr = JSON.parse(vprJSON);
shareCredential(vpr);
} else {
// exchange URL store or query
const url = protocols.vcapi;
const parsed = new URL(url);
if(parsed.searchParams.get('fakevp')) {
const vpJSON = parsed.searchParams.get('fakevp');
if(!vpJSON) {
throw new Error(
'Expected "fakevp" query param in vcapi URL for demo purposes.');
}
const vp = JSON.parse(vpJSON);
storeCredential(vp);
} else {
const vprJSON = parsed.searchParams.get('fakevpr');
if(!vprJSON) {
throw new Error(
'Expected "fakevpr" query param in vcapi URL for demo purposes.');
}
const vpr = JSON.parse(vprJSON);
shareCredential(vpr);
}
}
}
onDocumentReady(() => {
document.querySelectorAll('.loginButton').forEach(
e => e.addEventListener('click', login));
document.querySelectorAll('.logoutButton').forEach(
e => e.addEventListener('click', logout));
refreshUserArea();
handleRequest();
});
function shareCredential(vpr) {
document.getElementById('get').classList.remove('hide');
const query = Array.isArray(vpr.query) ? vpr.query[0] : vpr.query;
if(!query.type === 'QueryByExample') {
throw new Error(
'Only QueryByExample requests are supported in demo wallet.');
}
console.log('query', query);
const requestReason = query.credentialQuery.reason;
document.getElementById('requestReason').innerHTML = requestReason;
refreshUserArea({
shareButton: {
text: 'Share',
sourceEvent: event
}
});
}
function storeCredential(vp) {
document.getElementById('store').classList.remove('hide');
const vc = Array.isArray(vp.verifiableCredential) ?
vp.verifiableCredential[0] : vp.verifiableCredential;
console.log('vc', vc, getCredentialType(vc));
document.getElementById('credentialType').innerHTML = getCredentialType(vc);
document.getElementById('credentialIssuer').innerHTML = vc.issuer;
// set up the event handlers for the buttons
document.getElementById('cancelButton').addEventListener('click', () => {
// do nothing, close the window
window.close();
});
document.getElementById('confirmButton').addEventListener('click', () => {
document.querySelectorAll('.userArea').forEach(
e => e.classList.remove('hide'));
document.getElementById('confirm').classList.add('hide');
storeInWallet(vp); // in mock-user-management.js
refreshUserArea();
});
document.getElementById('doneButton').addEventListener('click', () => {
window.close();
});
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>