-
Notifications
You must be signed in to change notification settings - Fork 8
/
App.tsx
199 lines (183 loc) · 6.59 KB
/
App.tsx
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
import { AuthorizeResult, createClient, CreateCredentialResult, ExtensionClient } from "signify-polaris-web";
import { FormEvent, useEffect, useState } from "react";
export function App() {
const [extensionId, setExtensionId] = useState<string | false | null>(null);
const [error, setError] = useState<string | null>(null);
const [authorizeResult, setAuthorizeResult] = useState<AuthorizeResult | null>(null);
const [headers, setHeaders] = useState<Record<string, string> | null>(null);
const [pending, setPending] = useState(false);
const [url, setUrl] = useState(window.location.href);
const [method, setMethod] = useState("GET");
const [dataDigest, setDataDigest] = useState('');
const [digestAlgo, setDigestAlgo] = useState('SHA-256');
const [attestCredResult, setAttestCredResult] = useState<CreateCredentialResult | null>(null);
const [extensionClient, setExtensionClient] = useState<ExtensionClient | null>(null)
useEffect(() => {
const client = createClient();
setExtensionClient(client)
client.isExtensionInstalled().then((result) => setExtensionId(result));
}, []);
async function handleAuthorize(ev: FormEvent) {
ev.preventDefault();
setError(null);
setAuthorizeResult(null);
setPending(true);
try {
const result = await extensionClient.authorize({ message: `Message ${Date.now()}` });
console.log('authorize result: ', result)
setAuthorizeResult(result);
} catch (error) {
setError(error.message ?? "Something went wrong");
} finally {
setPending(false);
}
}
async function handleSignHeaders(ev: FormEvent) {
ev.preventDefault();
setError(null);
setPending(true);
setHeaders(null);
try {
const result = await extensionClient.signRequest({ url, method });
console.log('signRequest result: ', result)
setHeaders(result.headers);
} catch (error) {
setError(error.message ?? "Something went wrong");
} finally {
setPending(false);
}
}
async function handleAttestCredential(ev: FormEvent) {
ev.preventDefault();
setError(null);
setPending(true);
try {
//// example of data attestation schema:
//// https://github.com/provenant-dev/public-schema/blob/main/attestation/attestation.schema.json
let schemaSaid = 'ENDcMNUZjag27T_GTxiCmB2kYstg_kqipqz39906E_FD'
let credData = { digest: dataDigest, digestAlgo: 'SHA-256' }
const result = await extensionClient.createDataAttestationCredential({
credData: credData,
schemaSaid: schemaSaid
});
console.log('create data attestation credential result: ', result)
setAttestCredResult(result);
} catch (error) {
setError(error.message ?? "Something went wrong");
} finally {
setPending(false);
}
}
async function handleDownloadCredential(ev: FormEvent) {
ev.preventDefault();
setError(null);
setPending(true);
try {
let credSAID = attestCredResult?.acdc?._ked?.d
const credential = await extensionClient.getCredential(credSAID, true);
console.log('get credential result: ', credential)
if (!credential?.credential) {
setError("Unable to get credential");
return
}
const blob = new Blob([credential.credential], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'attestation-credential.cesr';
link.click();
URL.revokeObjectURL(url);
} catch (error) {
setError(error.message ?? "Something went wrong");
} finally {
setPending(false);
}
}
return (
<div style={{ maxWidth: 800, margin: "auto" }}>
<section>
<h1>Request login</h1>
<form id="login-form" onSubmit={handleAuthorize}>
<button type="submit" disabled={pending}>
Request AID or Credential
</button>
</form>
</section>
<section>
<h1>Request signed headers</h1>
<form id="headers-form" onSubmit={handleSignHeaders}>
<div>
<label htmlFor="url">URL</label>
<input id="url" value={url} onChange={(ev) => setUrl(ev.currentTarget.value)} style={{ marginLeft: '10px' }} />
</div>
<div style={{ marginTop: '10px' }}>
<label htmlFor="method">Method</label>
<input id="method" value={method} onChange={(ev) => setMethod(ev.currentTarget.value)} style={{ marginLeft: '10px' }} />
</div>
<div style={{ marginTop: '10px' }}>
<button type="submit" disabled={!authorizeResult}>
Request Signed Headers
</button>
</div>
</form>
</section>
<section>
<h1>Current state</h1>
<pre style={{ maxHeight: 400, overflowY: "scroll" }}>
<code>
{JSON.stringify(
{
isInstalled: extensionId,
headers,
error,
},
null,
2,
)}
</code>
</pre>
<h1>Current credential</h1>
<pre style={{ maxHeight: 400, overflowY: "scroll" }}>
<code>
{JSON.stringify(
{
credential: authorizeResult?.credential,
},
null,
2,
)}
</code>
</pre>
</section>
<section>
<h1>Create Data Attestation Credential</h1>
<form id="headers-form" onSubmit={handleAttestCredential}>
<div>
<label htmlFor="dataDigest">Data Digest</label>
<input id="dataDigest" value={dataDigest} onChange={(ev) => setDataDigest(ev.currentTarget.value)} style={{ marginLeft: '10px' }} />
</div>
<div style={{ marginTop: '10px' }}>
<label htmlFor="digestAlgo">Digest Algorithm</label>
<input id="digestAlgo"
value={digestAlgo}
onChange={(ev) => setDigestAlgo(ev.currentTarget.value)}
style={{ marginLeft: '10px' }}
placeholder="e.g. SHA-256"
/>
</div>
<div style={{ marginTop: '10px' }}>
<button type="submit" disabled={!authorizeResult || !dataDigest}>
Attest with Selected AID
</button>
<button type="submit"
disabled={!authorizeResult || !attestCredResult}
style={{ marginLeft: '10px' }}
onClick={handleDownloadCredential}>
Download Attestation Credential
</button>
</div>
</form>
</section>
</div>
);
}