Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DCKA-2517 Check for existing revocation registry before creating a new one. #22

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pages/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ export default function Test() {
}
}

const createCredential = async (credential, payload) => {
const createCredential = async (credential, payload, isRevocable) => {
const credentialObj = credential(payload);
await issueRevokableCredential(credentialObj.body, setRevokableCredential);
await issueRevokableCredential(credentialObj.body, setRevokableCredential, isRevocable);
};

async function issueNewCredential() {
toast.info('Issuing new credential', { duration: 10000 });
try {
await createCredential(createCreditScoreCredential, credentialPayload);
await createCredential(createCreditScoreCredential, credentialPayload, true);
toast.success('Credentials issued successfully.');
setLoadingRevokation(false);
} catch (error) {
Expand All @@ -70,7 +70,7 @@ export default function Test() {
}

return (
<div className="mainContainer ta-c pt-10">
<div className="pt-10 mainContainer ta-c">
<div className="ta-c">
<h1 className="text-2xl font-bold">Helper functions</h1>
</div>
Expand Down
48 changes: 27 additions & 21 deletions utils/dock-registries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { postRequest, apiGet } from "./request";
import { dockUrl } from "./constants";
import { toast } from 'sonner';
import { postRequest, apiGet } from './request';
import { dockUrl } from './constants';
/**
* Creates a new registry by calling the Registries API endpoint.
*
Expand All @@ -12,7 +12,7 @@ export async function createRegistry(policyDid, type) {
const data = {
addOnly: false,
policy: [policyDid],
type: type,
type,
};

try {
Expand All @@ -21,13 +21,11 @@ export async function createRegistry(policyDid, type) {
data,
);

console.log("Registries :", response);
console.log('Registries :', response);
if (response.data) {
return response.data
} else {
throw new Error('Data is not present on registries response')
}

return response.data;
}
throw new Error('Data is not present on registries response');
} catch (error) {
console.error(error);
}
Expand All @@ -37,9 +35,9 @@ export async function createRegistry(policyDid, type) {

export async function getRegistry(registryId) {
try {
return apiGet(`${dockUrl}/registries/${registryId}`)
return apiGet(`${dockUrl}/registries/${registryId}`);
} catch (error) {
toast.error('Invalid crendential registry.')
toast.error('Invalid crendential registry.');
}
}

Expand All @@ -52,23 +50,21 @@ export async function getRegistry(registryId) {
*/

export const revokeCredential = async (registryId, credentialId) => {

const payload = {
"action": "revoke",
"credentialIds": [
action: 'revoke',
credentialIds: [
credentialId
]
}
};

try {
return await postRequest(
`${dockUrl}/registries/${registryId}`,
payload,
);
} catch (error) {
toast.warning('Error revoking this credential')
toast.warning('Error revoking this credential');
}

};

/**
Expand All @@ -79,25 +75,35 @@ export const revokeCredential = async (registryId, credentialId) => {
* @returns {Promise<any>} A promise that resolves to the API response if successful, or rejects with an error.
*/
export async function unrevoke(registryId, credential) {
console.log("unrevoke:start:", { registryId, credential });
console.log('unrevoke:start:', { registryId, credential });

const url = `registries/${registryId}`;

const data = {
action: "unrevoke",
action: 'unrevoke',
credentialIds: [credential.id],
};

try {
const response = await postRequest({
url: url,
url,
body: data,
});

return response;
} catch (error) {
console.error("unrevoke:error", error);
console.error('unrevoke:error', error);
}

return undefined;
}

export async function getExistingRegistry(did, searchType) {
const registriesResponse = await apiGet(encodeURIComponent(`${dockUrl}/registries?did=${encodeURIComponent(did)}&type=${searchType}&limit=1`));

if (!registriesResponse || !registriesResponse.data || registriesResponse.data.length === 0) {
return null;
}

return registriesResponse.data[0];
}
20 changes: 13 additions & 7 deletions utils/issue-crendentials.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { toast } from 'sonner';
import { createRegistry } from './dock-registries';
import { data } from 'autoprefixer';
import { createRegistry, getExistingRegistry } from './dock-registries';
import { createCredential } from './dock-credentials';
import { waitForJobCompletion } from './dock-jobs';

Expand All @@ -10,19 +11,24 @@ export const issueRevokableCredential = async (credential, setRevokableCredentia
let registry = null;
if (isRevocable) {
const type = credentialPayload.algorithm === 'dockbbs+' ? 'DockVBAccumulator2022' : 'StatusList2021Entry';
// CREATING REGISTRY
registry = await createRegistry(_credential.issuer.id, type);
// WAITING FOR REGISTRY JOB CONFIRMATION
await waitForJobCompletion(registry.id);
registry = await getExistingRegistry(_credential.issuer.id, type);

if (!registry) {
// CREATING REGISTRY
const newRegistry = await createRegistry(_credential.issuer.id, type);
// WAITING FOR REGISTRY JOB CONFIRMATION
await waitForJobCompletion(newRegistry.id);
registry = newRegistry.data;
}
}

// SIGNING CREDENTIAL
const signed = await createCredential(registry?.data?.id, credentialPayload);
const signed = await createCredential(registry?.id, credentialPayload);

if (isRevocable) {
console.log(signed);
setRevokableCredential({
registryId: registry.data.id,
registryId: registry.id,
credentialId: signed.data.id,
userDid: _credential.subject.id
});
Expand Down
Loading