Skip to content

Commit

Permalink
Moved method out of the component
Browse files Browse the repository at this point in the history
  • Loading branch information
orhoj committed Aug 31, 2023
1 parent 7501435 commit f794a5c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ import {
fetchCredentialMetadata,
fetchCredentialSchema,
fetchLocalization,
findNextUnusedVerifiableCredentialIndex,
getCredentialRegistryContractAddress,
isVerifiableCredentialInContract,
} from '@shared/utils/verifiable-credential-helpers';
import { APIVerifiableCredential } from '@concordium/browser-wallet-api-helpers';
import { grpcClientAtom, networkConfigurationAtom } from '@popup/store/settings';
import { MetadataUrl } from '@concordium/browser-wallet-api-helpers/lib/wallet-api-types';
import { parse } from '@shared/utils/payload-helpers';
import { logError } from '@shared/utils/log-helpers';
import { ConcordiumHdWallet, ContractAddress } from '@concordium/web-sdk';
import { addToastAtom } from '@popup/state';
import { VerifiableCredentialCard } from '../VerifiableCredential/VerifiableCredentialCard';

Expand Down Expand Up @@ -172,25 +171,6 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
[metadata, i18n]
);

async function findNextUnusedVerifiableCredentialIndex(
localIndex: number,
issuer: ContractAddress,
hdWallet: ConcordiumHdWallet
) {
let index = localIndex;
let credentialAlreadyExists = true;

do {
const credentialHolderId = hdWallet.getVerifiableCredentialPublicKey(issuer, index).toString('hex');
credentialAlreadyExists = await isVerifiableCredentialInContract(client, credentialHolderId, issuer);
if (credentialAlreadyExists) {
index += 1;
}
} while (credentialAlreadyExists);

return index;
}

async function addCredential(credentialSchema: VerifiableCredentialSchema) {
if (!wallet) {
throw new Error('Wallet is unexpectedly missing');
Expand All @@ -207,7 +187,7 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
// the next unused index based on that.
let nextUnusedIndex: number;
try {
nextUnusedIndex = await findNextUnusedVerifiableCredentialIndex(index, issuer, wallet);
nextUnusedIndex = await findNextUnusedVerifiableCredentialIndex(client, index, issuer, wallet);
} catch (e) {
addToast(t('error.findingNextIndex'));
logError(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { CcdAmount, UpdateContractPayload, ConcordiumGRPCClient, ContractAddress, sha256 } from '@concordium/web-sdk';
import {
CcdAmount,
UpdateContractPayload,
ConcordiumGRPCClient,
ContractAddress,
sha256,
ConcordiumHdWallet,
} from '@concordium/web-sdk';
import * as ed from '@noble/ed25519';
import {
MetadataUrl,
Expand Down Expand Up @@ -1159,3 +1166,32 @@ export function getCredentialIdFromSubjectDID(did: string) {
}
return split[split.length - 1];
}

/**
* Finds the next unused verifiable credential index, i.e. the next index that has not
* been used as a credential holder id in the provided issuer contract.
* @param client the GRPC client for accessing a node
* @param localIndex the currently best guess on the next index, based on what is currently stored locally
* @param issuer the issuer credential registry contract
* @param hdWallet the key derivation wallet
* @returns the next unused verifiable credential index
*/
export async function findNextUnusedVerifiableCredentialIndex(
client: ConcordiumGRPCClient,
localIndex: number,
issuer: ContractAddress,
hdWallet: ConcordiumHdWallet
): Promise<number> {
let index = localIndex;
let credentialAlreadyExists = true;

do {
const credentialHolderId = hdWallet.getVerifiableCredentialPublicKey(issuer, index).toString('hex');
credentialAlreadyExists = await isVerifiableCredentialInContract(client, credentialHolderId, issuer);
if (credentialAlreadyExists) {
index += 1;
}
} while (credentialAlreadyExists);

return index;
}

0 comments on commit f794a5c

Please sign in to comment.