Skip to content

Commit

Permalink
refactor(client): small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Aug 31, 2023
1 parent 09c1a57 commit c5b0bb5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions apps/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "client",
"version": "0.1.0",
"private": true,
"source": "src/index.html",
"scripts": {
"start": "parcel --cache-dir .parcel-cache/ --no-cache --open",
Expand Down
28 changes: 13 additions & 15 deletions apps/client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { EdDSAPCDPackage } from "@pcd/eddsa-pcd"
import { deserialize, EdDSAPCDPackage, init as initEdDSAPCD, verify } from "@pcd/eddsa-pcd"
import { getWithoutProvingUrl, openPassportPopup, usePassportPopupMessages } from "@pcd/passport-interface"
import React from "react"
import { useCallback, useEffect, useState } from "react"

/**
* This component consumes a message signed with an EdDSA key. If you have provided a valid signature,
* it uses the color as the background of the web app page. This background is preserved as long as
* This component consumes a message signed with an EdDSA key. If you have provided a valid signature,
* it uses the color as the background of the web app page. This background is preserved as long as
* you do not consume a signature on a different colour.
*/
export default function App() {
Expand All @@ -16,8 +15,8 @@ export default function App() {
// Get the color extracted from the latest valid message signed with an EdDSA key.
const getLatestConsumedColor = async () => {
const response = await fetch(`http://localhost:${process.env.SERVER_PORT}/color/get`, {
method: 'GET',
mode: 'cors'
method: "GET",
mode: "cors"
})

if (!!response.ok) {
Expand All @@ -37,29 +36,28 @@ export default function App() {
}, [bgColor])

useEffect(() => {
; (async () => {
// @ts-ignore
await EdDSAPCDPackage.init({})
;(async () => {
await initEdDSAPCD()

if (passportPCDString) {
const { pcd: serializedPCD } = JSON.parse(passportPCDString)

const pcd = await EdDSAPCDPackage.deserialize(serializedPCD)
const pcd = await deserialize(serializedPCD)

if (await EdDSAPCDPackage.verify(pcd)) {
if (await verify(pcd)) {
// Get PCD claim (color).
const color = pcd.claim.message[0].toString(16)

// Store consumed color on the server.
await fetch(`http://localhost:${process.env.SERVER_PORT}/color/set`, {
method: 'POST',
mode: 'cors',
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
color: color
}),
})
})

alert(`The signature is valid, #${color} will be used as a background color!`)
Expand All @@ -82,5 +80,5 @@ export default function App() {
openPassportPopup("/popup", url)
}, [])

return <><button onClick={getEdDSAPCD}>Get PCD signature</button></>
return <button onClick={getEdDSAPCD}>Get PCD signature</button>
}

0 comments on commit c5b0bb5

Please sign in to comment.