diff --git a/README.md b/README.md index f3029e3..3bcceae 100644 --- a/README.md +++ b/README.md @@ -156,8 +156,6 @@ Under folder `/utils` are the files that handle all the communication with Dock `createCredential(registryId, credential)` -> Create new credential from registryId & credential schema. -`distributeCredential(credential)` -> Send a signed credential to a Did or email. - `issueRevokableCredential = async (credential, setRevokableCredential)` -> Entire process that carrie all previews functions. revokeCredential = async (registryId, credentialId) -> Revoke credit score credential with a given registry and credential ids. diff --git a/_credentials/equinet.js b/_credentials/equinet.js index ded5622..3e329d0 100644 --- a/_credentials/equinet.js +++ b/_credentials/equinet.js @@ -1,12 +1,9 @@ -import { v4 as uuidv4 } from 'uuid'; -import { dockUrl } from 'utils/constants'; import { validateEmail } from 'utils/validation'; export function createCreditScoreCredential({ receiverDid, recipientEmail, creditScore }) { console.log('Creating EquiNet - Credit Score Credential for:', receiverDid); const credentialPayload = { - url: `${dockUrl}/credentials`, body: { anchor: false, distribute: true, diff --git a/_credentials/forsur.js b/_credentials/forsur.js index 3af2f4b..1a6a4ad 100644 --- a/_credentials/forsur.js +++ b/_credentials/forsur.js @@ -1,5 +1,4 @@ import { v4 as uuidv4 } from 'uuid'; -import { dockUrl } from 'utils/constants'; import { validateEmail } from 'utils/validation'; export function createBiometricsCredential({ @@ -10,7 +9,6 @@ export function createBiometricsCredential({ console.log('Creating ForSur - Biometric Enrollment Credential for:', receiverDid); const credentialPayload = { - url: `${dockUrl}/credentials`, body: { anchor: false, algorithm: 'bbdt16', diff --git a/_credentials/quotient.js b/_credentials/quotient.js index 02a95b2..3312b41 100644 --- a/_credentials/quotient.js +++ b/_credentials/quotient.js @@ -1,5 +1,4 @@ import { v4 as uuidv4 } from 'uuid'; -import { dockUrl } from 'utils/constants'; import { validateEmail } from 'utils/validation'; export function createBankIdCredential({ @@ -12,7 +11,6 @@ export function createBankIdCredential({ console.log('Creating Quotient Bank Identity Credential for:', receiverDid); const credentialPayload = { - url: `${dockUrl}/credentials`, body: { anchor: false, algorithm: 'bbdt16', diff --git a/hooks/useRevoke.js b/hooks/useRevoke.js index 198e22a..257e59f 100644 --- a/hooks/useRevoke.js +++ b/hooks/useRevoke.js @@ -61,14 +61,8 @@ export const useRevoke = () => { } } - async function handleGetRegistry() { - const registry = await getRegistry(revokableCredential.registryId); - console.log('registry', registry); - } - return { loadingRevokation, - handleGetRegistry, handleRevoke, issueNewCredential } diff --git a/pages/api/create-credential-offer.js b/pages/api/create-credential-offer.js new file mode 100644 index 0000000..1938b08 --- /dev/null +++ b/pages/api/create-credential-offer.js @@ -0,0 +1,44 @@ +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'POST') { + console.log('Only post request allowed'); + res.status(400).json({}); + return; + } + + let result; + result = await fetch(`${dockUrl}/openid/issuers`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify(req.body), + }); + + const response = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + result = await fetch(`${dockUrl}/openid/credential-offers`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify({ + id: response.id, + }), + }); + + const offerResponse = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(offerResponse); +}; diff --git a/pages/api/handle-credentials.js b/pages/api/create-proof-request-object.js similarity index 75% rename from pages/api/handle-credentials.js rename to pages/api/create-proof-request-object.js index 561bd9d..74e8331 100644 --- a/pages/api/handle-credentials.js +++ b/pages/api/create-proof-request-object.js @@ -1,3 +1,5 @@ +import { dockUrl } from '../../utils/constants'; + export default async (req, res) => { if (req.method !== 'POST') { console.log('Only post request allowed'); @@ -5,22 +7,16 @@ export default async (req, res) => { return; } - const body = req.body; - - console.log('body::', body); - - const result = await fetch(body.url, { + const result = await fetch(`${dockUrl}/proof-requests`, { method: 'POST', headers: { 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, 'content-type': 'application/json', }, - body: JSON.stringify(body), + body: JSON.stringify(req.body), }); const response = await result.json(); - console.log('response:', result); - if (!result.ok) { const errorText = await result.text(); throw new Error(`API Error: ${result.status} - ${errorText}`); diff --git a/pages/api/create-proof-request.js b/pages/api/create-proof-request.js new file mode 100644 index 0000000..65cd129 --- /dev/null +++ b/pages/api/create-proof-request.js @@ -0,0 +1,30 @@ +import { generateNonce } from '../../utils/generate-nonce'; +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'POST') { + console.log('Only post request allowed'); + res.status(400).json({}); + return; + } + + const result = await fetch(`${dockUrl}/proof-templates/${req.body.proofTemplateID}/request`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify({ + nonce: `${generateNonce()}`, + domain: 'dock.io', + }), + }); + + const response = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(response); +}; diff --git a/pages/api/create-registry.js b/pages/api/create-registry.js new file mode 100644 index 0000000..6739c8b --- /dev/null +++ b/pages/api/create-registry.js @@ -0,0 +1,26 @@ +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'POST') { + console.log('Only post request allowed'); + res.status(400).json({}); + return; + } + + const result = await fetch(`${dockUrl}/registries`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify(req.body), + }); + + const response = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(response); +}; diff --git a/pages/api/find-registry.js b/pages/api/find-registry.js new file mode 100644 index 0000000..03b9649 --- /dev/null +++ b/pages/api/find-registry.js @@ -0,0 +1,29 @@ +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'GET') { + console.log('Only GET request allowed'); + res.status(400).json({}); + return; + } + + const result = await fetch( + `${dockUrl}/registries?did=${encodeURIComponent(req.query.did)}&type=${encodeURIComponent(req.query.searchType)}`, + { + method: 'GET', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + } + ); + + const response = await result.json(); + + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API get Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(response); +}; diff --git a/pages/api/handle-get.js b/pages/api/get-job.js similarity index 82% rename from pages/api/handle-get.js rename to pages/api/get-job.js index 146603b..773378f 100644 --- a/pages/api/handle-get.js +++ b/pages/api/get-job.js @@ -1,3 +1,5 @@ +import { dockUrl } from '../../utils/constants'; + export default async (req, res) => { if (req.method !== 'GET') { console.log('Only GET request allowed'); @@ -5,9 +7,7 @@ export default async (req, res) => { return; } - const url = req.query.url; - - const result = await fetch(url, { + const result = await fetch(`${dockUrl}/jobs/${req.query.id}`, { method: 'GET', headers: { 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, @@ -16,7 +16,6 @@ export default async (req, res) => { }); const response = await result.json(); - console.log('response:', result); if (!result.ok) { const errorText = await result.text(); diff --git a/pages/api/get-oid4vp-url.js b/pages/api/get-oid4vp-url.js new file mode 100644 index 0000000..034f55e --- /dev/null +++ b/pages/api/get-oid4vp-url.js @@ -0,0 +1,29 @@ +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'POST') { + console.log('Only post request allowed'); + res.status(400).json({}); + return; + } + + const result = await fetch(`${dockUrl}/openid/vp/${req.body.proofRequestId}/request-url`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify({ + ...req.body, + proofRequestId: undefined, + }), + }); + + const response = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(response); +}; diff --git a/pages/api/issue-biometric.js b/pages/api/issue-biometric.js new file mode 100644 index 0000000..babed96 --- /dev/null +++ b/pages/api/issue-biometric.js @@ -0,0 +1,39 @@ +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'POST') { + console.log('Only post request allowed'); + res.status(400).json({}); + return; + } + + const { issuer, type, subject, expirationDate } = req.body; + + const result = await fetch(`${dockUrl}/credentials`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify({ + anchor: false, + persist: false, + credential: { + name: type, + type: ['VerifiableCredential', type], + issuer, + expirationDate, + subject, + }, + algorithm: 'dockbbs+', + }), + }); + + const response = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(response); +}; diff --git a/pages/api/issue-credential.js b/pages/api/issue-credential.js new file mode 100644 index 0000000..b56d6a1 --- /dev/null +++ b/pages/api/issue-credential.js @@ -0,0 +1,26 @@ +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'POST') { + console.log('Only post request allowed'); + res.status(400).json({}); + return; + } + + const result = await fetch(`${dockUrl}/credentials`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify(req.body), + }); + + const response = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(response); +}; diff --git a/pages/api/revoke-action.js b/pages/api/revoke-action.js new file mode 100644 index 0000000..290ac83 --- /dev/null +++ b/pages/api/revoke-action.js @@ -0,0 +1,29 @@ +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'POST') { + console.log('Only post request allowed'); + res.status(400).json({}); + return; + } + + const result = await fetch(`${dockUrl}/registries/${req.body.registryId}`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify({ + ...req.body, + registryId: undefined, + }), + }); + + const response = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(response); +}; diff --git a/pages/api/verify-credential.js b/pages/api/verify-credential.js new file mode 100644 index 0000000..d464897 --- /dev/null +++ b/pages/api/verify-credential.js @@ -0,0 +1,26 @@ +import { dockUrl } from '../../utils/constants'; + +export default async (req, res) => { + if (req.method !== 'POST') { + console.log('Only post request allowed'); + res.status(400).json({}); + return; + } + + const result = await fetch(`${dockUrl}/verify`, { + method: 'POST', + headers: { + 'DOCK-API-TOKEN': `${process.env.DOCK_API_TOKEN}`, + 'content-type': 'application/json', + }, + body: JSON.stringify(req.body), + }); + + const response = await result.json(); + if (!result.ok) { + const errorText = await result.text(); + throw new Error(`API Error: ${result.status} - ${errorText}`); + } + + res.status(202).send(response); +}; diff --git a/pages/mdl.js b/pages/mdl.js index d5fad50..d5418ff 100644 --- a/pages/mdl.js +++ b/pages/mdl.js @@ -6,7 +6,7 @@ import axios from 'axios'; 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 { apiGetLocal, postRequestLocal } from 'utils/request'; import { dockUrl } from 'utils/constants'; const baseUrl = process.env.NEXT_PUBLIC_DOCK_API_URL; @@ -80,17 +80,15 @@ function OID4VPProofRequest({ title, desc, proofRequestSetupObject, onPres, setE const [isVerified, setIsVerified] = useState(false); async function createProofRequest() { - const { data: proofRequest } = await postRequest(`${dockUrl}/proof-requests`, { + const { data: proofRequest } = await postRequestLocal('create-proof-request-object', { did: process.env.NEXT_PUBLIC_QUOTIENT_ISSUER_ID, ...proofRequestSetupObject, }); - const { data: qrUrlData } = await postRequest( - `${dockUrl}/openid/vp/${proofRequest.id}/request-url`, - { - withRequestURI: true, - } - ); + const { data: qrUrlData } = await postRequestLocal('get-oid4vp-url', { + withRequestURI: true, + proofRequestId: proofRequest.id, + }); setProofRequest({ ...proofRequest, @@ -99,7 +97,7 @@ function OID4VPProofRequest({ title, desc, proofRequestSetupObject, onPres, setE const int = setInterval(async () => { try { - const { data: res } = await apiGet(`${dockUrl}/proof-requests/${proofRequest.id}`); + const { data: res } = await apiGetLocal(`handle-proof?proofID=${proofRequest.id}`); if (res.verified) { setProofRequest(res); setIsVerified(true); diff --git a/pages/openid.js b/pages/openid.js index e11b359..afb3819 100644 --- a/pages/openid.js +++ b/pages/openid.js @@ -1,13 +1,8 @@ -import OrganizationCard from 'components/org/organizationCard'; -import organizations from 'data/organizations'; import Image from 'next/image'; import React, { useEffect, useState } from 'react'; -import Link from 'next/link'; import { Separator } from 'components/ui/separator'; -import DemoFlow from 'components/demo-flow'; -import { Button } from 'components/ui/button'; import { QRCodeGenerator } from 'components/qrcode/qr-generator'; -import { apiGet, postRequest } from 'utils/request'; +import { apiGetLocal, postRequestLocal } from 'utils/request'; import { dockUrl } from 'utils/constants'; const credential = { @@ -154,17 +149,15 @@ function OID4VPProofRequest({ title, desc, proofRequestSetupObject }) { const [isVerified, setIsVerified] = useState(false); async function createProofRequest() { - const { data: proofRequest } = await postRequest(`${dockUrl}/proof-requests`, { + const { data: proofRequest } = await postRequestLocal('create-proof-request-object', { did: process.env.NEXT_PUBLIC_QUOTIENT_ISSUER_ID, ...proofRequestSetupObject, }); - const { data: qrUrlData } = await postRequest( - `${dockUrl}/openid/vp/${proofRequest.id}/request-url`, - { - withRequestURI: true, - } - ); + const { data: qrUrlData } = await postRequestLocal('get-oid4vp-url', { + withRequestURI: true, + proofRequestId: proofRequest.id, + }); setProofRequest({ ...proofRequest, @@ -173,7 +166,7 @@ function OID4VPProofRequest({ title, desc, proofRequestSetupObject }) { const int = setInterval(async () => { try { - const { data: res } = await apiGet(`${dockUrl}/proof-requests/${proofRequest.id}`); + const { data: res } = await apiGetLocal(`handle-proof?proofID=${proofRequest.id}`); if (res.verified) { setProofRequest(res); setIsVerified(true); @@ -246,7 +239,7 @@ export default function Home() { const [credentialOfferNoAuth, setCredentialOfferNoAuth] = useState(); async function createCredentialOffer() { - const { data: oidcIssuer } = await postRequest(`${dockUrl}/openid/issuers`, { + const { data: credentialOffer } = await postRequestLocal('create-credential-offer', { claimMap, credentialOptions: { credential: { @@ -258,10 +251,6 @@ export default function Home() { authProvider, }); - const { data: credentialOffer } = await postRequest(`${dockUrl}/openid/credential-offers`, { - id: oidcIssuer.id, - }); - setCredentialOffer(credentialOffer); } @@ -284,7 +273,7 @@ export default function Home() { } async function generateEBSIUrls() { - const { data: proofRequest } = await postRequest(`${dockUrl}/proof-requests`, { + const { data: proofRequest } = await postRequestLocal('create-proof-request-object', { did: process.env.NEXT_PUBLIC_QUOTIENT_ISSUER_ID, ...ebsiConformanceProofRequest, }); diff --git a/pages/test.js b/pages/test.js deleted file mode 100644 index fd17303..0000000 --- a/pages/test.js +++ /dev/null @@ -1,96 +0,0 @@ -import React, { useEffect, useState } from 'react'; -import { Button } from 'components/ui/button'; -import { useLocalStorage } from 'hooks/hooks'; -import { issueRevokableCredential } from 'utils/issue-crendentials'; -import { createCreditScoreCredential } from '_credentials/equinet'; -import { getRandomNumber } from 'utils'; -import { getRegistry, revokeCredential } from 'utils/dock-registries'; -import { toast } from 'sonner'; -import userStore from 'store/appStore'; - -export default function Test() { - const [revokableCredential, setRevokableCredential] = useLocalStorage('revokableCredential', ''); - const recipientEmail = userStore((state) => state.userEmail); - const [loadingRevokation, setLoadingRevokation] = useState(false); - - const credentialPayload = { - receiverDid: revokableCredential.userDid, - recipientEmail, - creditScore: getRandomNumber(700, 800), - }; - - async function handleRevoke() { - if (!revokableCredential.registryId || !revokableCredential.credentialId) { - toast.warning('There is no credential to revoke.'); - return; - } - - setLoadingRevokation(true); - try { - const revokation = await revokeCredential( - revokableCredential.registryId, - revokableCredential.credentialId - ); - if (revokation.status === 202) { - toast.success('Credential revoked successfully', { duration: 10000 }); - setTimeout(async () => { - await issueNewCredential(); - }, 3000); - } else { - setLoadingRevokation(false); - throw new Error('Revocation error, try again'); - } - } catch (error) { - setLoadingRevokation(false); - toast.error('Error revokating credential, try again.'); - } - } - - const createCredential = async (credential, payload, isRevocable) => { - const credentialObj = credential(payload); - await issueRevokableCredential(credentialObj.body, setRevokableCredential, isRevocable); - }; - - async function issueNewCredential() { - toast.info('Issuing new credential', { duration: 10000 }); - try { - await createCredential(createCreditScoreCredential, credentialPayload, true); - toast.success('Credentials issued successfully.'); - setLoadingRevokation(false); - } catch (error) { - console.log('Issuing error:', error); - toast.warning('Error issuing credentials, try again.'); - setLoadingRevokation(false); - } - } - - async function handleGetRegistry() { - const registry = await getRegistry(revokableCredential.registryId); - console.log('registry', registry); - } - - return ( -