diff --git a/pages/mdl.js b/pages/mdl.js new file mode 100644 index 0000000..f48d4dc --- /dev/null +++ b/pages/mdl.js @@ -0,0 +1,287 @@ +import Head from 'next/head'; +import Image from 'next/image'; +import React, { useEffect, useState } from 'react'; +import { Separator } from 'components/ui/separator'; +import { Button } from 'components/ui/button'; +import { QRCodeGenerator } from 'components/qrcode/qr-generator'; +import { apiGet, postRequest } from 'utils/request'; +import { dockUrl } from 'utils/constants'; + +const mdlProofRequest = { + name: 'Proof request', + nonce: '-vz4qxeHjbmcjvfVBKJ1PywWnLawwxLi50CVTrOAGmw=', + request: { + name: 'MDL age over 18 test', + purpose: 'To check if over 18 years old', + input_descriptors: [ + { + name: 'MDL age over 18 test', + purpose: 'To check if over 18 years old', + id: 'org.iso.18013.5.1.mDL', + format: { + mso_mdoc: { + alg: ['EdDSA', 'ES256'], + }, + }, + constraints: { + limit_disclosure: 'required', + fields: [ + { + path: ["$['org.iso.18013.5.1']['age_over_18']", 'age_over_18'], + intent_to_retain: false, + }, + ], + }, + }, + ], + }, +}; + +const mdlProofRequestName = { + name: 'Proof request', + nonce: '-vz4qxeHjbmcjvfVBKJ1PywWnLawwxLi50CVTrOAGmw=', + request: { + name: 'MDL get name test', + purpose: 'To get your name', + input_descriptors: [ + { + name: 'MDL get name test', + purpose: 'To get your name', + id: 'org.iso.18013.5.1.mDL', + format: { + mso_mdoc: { + alg: ['EdDSA', 'ES256'], + }, + }, + constraints: { + limit_disclosure: 'required', + fields: [ + { + path: ["$['org.iso.18013.5.1']['family_name']"], + intent_to_retain: false, + }, + { + path: ["$['org.iso.18013.5.1']['given_name']"], + intent_to_retain: false, + }, + ], + }, + }, + ], + }, +}; + +function OID4VPProofRequest({ title, desc, proofRequestSetupObject, onPres, setError }) { + const [proofRequest, setProofRequest] = useState(); + const [isVerified, setIsVerified] = useState(false); + + async function createProofRequest() { + const { data: proofRequest } = await postRequest(`${dockUrl}/proof-requests`, { + did: process.env.NEXT_PUBLIC_QUOTIENT_ISSUER_ID, + ...proofRequestSetupObject, + }); + + const { data: qrUrlData } = await postRequest( + `${dockUrl}/openid/vp/${proofRequest.id}/request-url`, + { + withRequestURI: true, + } + ); + + setProofRequest({ + ...proofRequest, + qrUrlData, + }); + + const int = setInterval(async () => { + try { + const { data: res } = await apiGet(`${dockUrl}/proof-requests/${proofRequest.id}`); + if (res.verified) { + setProofRequest(res); + setIsVerified(true); + clearInterval(int); + + if (onPres) { + onPres(res); + } + } + } catch (e) { + console.error(e); + } + }, 3000); + } + + useEffect(() => { + if (!proofRequest) { + createProofRequest(); + } + }); + + async function handleCredsRequest() { + console.log('handleCredsRequest', proofRequest); + + const credsApiRequest = { + protocol: 'openid4vp', + request: JSON.stringify({ + client_id: 'bank-demo.dock.io', + client_id_scheme: 'web-origin', + response_type: 'vp_token', + nonce: proofRequest.nonce, + presentation_definition: proofRequest.request, + }), + state: { + nonce: proofRequest.nonce, + private_key: 'kN37SKg-iu3N3wSXAhuBXxwDkbo5rvUFYCr9BCm34Qs=', + public_key: + 'BF4nDPpbH9jac22-pJfgFqGLj-Qh-vPA4Hmtry6CodaLzGcn0LTGWhBIz0LONTRJj4GRx1nd6pb8UeGR0lqQlVc=', + }, + }; + + console.log('credsApiRequest', credsApiRequest); + + try { + const credentialResponse = await navigator.identity.get({ + digital: { + providers: [ + { + protocol: credsApiRequest.protocol, + request: credsApiRequest.request, + }, + ], + }, + }); + + let responseForServer; + if (credentialResponse.constructor.name === 'DigitalCredential') { + const data = credentialResponse.data; + const protocol = credentialResponse.protocol; + responseForServer = { + protocol, + data, + state: credsApiRequest.state, + }; + } else if (credentialResponse.constructor.name === 'IdentityCredential') { + const data = credentialResponse.token; + const protocol = 'oid4vp'; + responseForServer = { + protocol, + data, + state: credsApiRequest.state, + }; + } else { + throw new Error('Unknown response type'); + } + + console.log('responseForServer', responseForServer); + + const dataObj = JSON.parse(responseForServer.data); + onPres(dataObj); // TEMP + } catch (e) { + console.error(e); + setError(e.message || 'unknown'); + } + } + + return ( +
{title}
+ {isVerified ? ( +{desc}
++ Verified: {res.verified ? 'true' : 'false'} ++ ) : ( +
+
+ {JSON.stringify(res.presentation || {}, null, 2)} +
Scan an above QR code or click the button
+ )} +