-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
91 lines (74 loc) · 2.8 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
import { EdDSAPCDPackage } from "@pcd/eddsa-pcd"
import { getWithoutProvingUrl, openPassportPopup, usePassportPopupMessages } from "@pcd/passport-interface"
import { useCallback, useEffect, useState } from "react"
/**
* This page allows users to get an EdDSA PCD containing a color as a message signed by
* the issuer. If the signature is valid the color is stored in the server and the background color
* of this page will be changed.
*/
export default function App() {
const [passportPCDString] = usePassportPopupMessages()
const [bgColor, setBgColor] = useState<string>()
// Get the latest color stored in the server.
useEffect(() => {
;(async () => {
const response = await fetch(`http://localhost:${process.env.SERVER_PORT}/color`, {
method: "GET",
mode: "cors"
})
if (response.status === 404) {
return
}
if (!response.ok) {
alert("Some error occurred")
return
}
const { color } = await response.json()
setBgColor(color)
})()
}, [])
// Store the color in the server if its PCD is valid
// and then updates the background color of this page.
useEffect(() => {
;(async () => {
if (passportPCDString) {
const { pcd } = JSON.parse(passportPCDString)
const response = await fetch(`http://localhost:${process.env.SERVER_PORT}/color`, {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
pcd
})
})
if (!response.ok) {
alert("Some error occurred")
return
}
const { color } = await response.json()
if (bgColor === color) {
alert("The color is the same as the current one")
return
}
setBgColor(color)
}
})()
}, [passportPCDString])
// Update the background color.
useEffect(() => {
const appElement = document.getElementById("app")!
appElement.style.backgroundColor = bgColor
}, [bgColor])
// Get the EdDSA PCD with the color signed by the issuer.
const getEdDSAPCD = useCallback(() => {
const url = getWithoutProvingUrl(
process.env.PCDPASS_URL as string,
window.location.origin + "/popup",
EdDSAPCDPackage.name
)
openPassportPopup("/popup", url)
}, [])
return <button onClick={getEdDSAPCD}>Get a PCD signature with your color</button>
}