From c9a00084c204e31f501e9fcfb75d21169b9317c5 Mon Sep 17 00:00:00 2001 From: Sai Ranjit Tummalapalli Date: Fri, 6 Oct 2023 17:30:53 +0530 Subject: [PATCH] feat: add credentials and proofs modules with dedicated functions Signed-off-by: Sai Ranjit Tummalapalli --- packages/ssi/src/credentials/credentials.ts | 136 ++++++++++++++++++ packages/ssi/src/credentials/index.ts | 1 + packages/ssi/src/index.ts | 2 + packages/ssi/src/proofs/index.ts | 1 + packages/ssi/src/proofs/proofs.ts | 145 ++++++++++++++++++++ 5 files changed, 285 insertions(+) create mode 100644 packages/ssi/src/credentials/credentials.ts create mode 100644 packages/ssi/src/credentials/index.ts create mode 100644 packages/ssi/src/proofs/index.ts create mode 100644 packages/ssi/src/proofs/proofs.ts diff --git a/packages/ssi/src/credentials/credentials.ts b/packages/ssi/src/credentials/credentials.ts new file mode 100644 index 0000000..284cf8e --- /dev/null +++ b/packages/ssi/src/credentials/credentials.ts @@ -0,0 +1,136 @@ +import type { + V1CredentialProtocol, + LegacyIndyCredentialFormatService, + AnonCredsCredentialFormatService +} from '@aries-framework/anoncreds' +import type { + AcceptCredentialOfferOptions, + CredentialExchangeRecord, + DeleteCredentialOptions, + JsonLdCredentialFormatService, + SendCredentialProblemReportOptions, + V2CredentialProtocol +} from '@aries-framework/core' + +import { useAdeyaAgent } from '../providers' + +/** + * Retrieves all credential exchange records from the agent. + * + * @returns A promise that resolves to an array of credential exchange records. + */ +export const getAllCredentialExchangeRecords = async () => { + const { agent } = useAdeyaAgent() + + return agent.credentials.getAll() +} + +/** + * Retrieves the formatted data for a given credential record ID. + * + * @param credentialRecordId The ID of the credential record to retrieve formatted data for. + * @returns A Promise that resolves with the formatted data for the given credential record ID. + */ +export const getFormattedCredentialData = async (credentialRecordId: string) => { + const { agent } = useAdeyaAgent() + + return agent.credentials.getFormatData(credentialRecordId) +} + +/** + * Accepts a credential offer. + * + * @param options - The options for accepting the credential offer. + * @returns A promise that resolves with the accepted credential. + */ +export const acceptCredentialOffer = async ( + options: AcceptCredentialOfferOptions< + ( + | V1CredentialProtocol + | V2CredentialProtocol< + (LegacyIndyCredentialFormatService | AnonCredsCredentialFormatService | JsonLdCredentialFormatService)[] + > + )[] + > +) => { + const { agent } = useAdeyaAgent() + + return agent.credentials.acceptOffer(options) +} + +/** + * Updates a credential exchange record. + * + * @param credentialRecord The credential exchange record to update. + * @returns A promise that resolves with the updated credential exchange record. + */ +export const updateCredentialExchangeRecord = async (credentialRecord: CredentialExchangeRecord) => { + const { agent } = useAdeyaAgent() + + return agent.credentials.update(credentialRecord) +} + +/** + * Declines a credential offer. + * + * @param credentialId The ID of the credential offer to decline. + * @returns A Promise that resolves CredentialExchangeRecord when the credential offer has been declined. + */ +export const declineCredentialOffer = async (credentialId: string) => { + const { agent } = useAdeyaAgent() + + return agent.credentials.declineOffer(credentialId) +} + +/** + * Deletes a credential exchange record with the given ID. + * @param credentialRecordId The ID of the credential exchange record to delete. + * @param options Optional parameters for deleting the credential exchange record. + * + * @returns void + */ +export const deleteCredentialExchangeRecordById = async ( + credentialRecordId: string, + options?: DeleteCredentialOptions +) => { + const { agent } = useAdeyaAgent() + + return agent.credentials.deleteById(credentialRecordId, options) +} + +/** + * Sends a problem report for a credential to Agent. + * + * @param options - The options for sending the problem report. + * @returns A Promise that resolves CredentialExchangeRecord when the problem report has been sent. + */ +export const sendCredentialProblemReport = async (options: SendCredentialProblemReportOptions) => { + const { agent } = useAdeyaAgent() + + return agent.credentials.sendProblemReport(options) +} + +// W3C Credential + +/** + * Retrieves a W3C credential record by its ID. + * + * @param credentialRecordId The ID of the credential record to retrieve. + * @returns A Promise that resolves to the retrieved w3c credential record. + */ +export const getW3cCredentialRecordById = async (credentialRecordId: string) => { + const { agent } = useAdeyaAgent() + + return agent.w3cCredentials.getCredentialRecordById(credentialRecordId) +} + +/** + * Retrieves all W3C credential records from the agent. + * + * @returns A promise that resolves to an array of W3C credential records. + */ +export const getAllW3cCredentialRecords = async () => { + const { agent } = useAdeyaAgent() + + return agent.w3cCredentials.getAllCredentialRecords() +} diff --git a/packages/ssi/src/credentials/index.ts b/packages/ssi/src/credentials/index.ts new file mode 100644 index 0000000..5ea729f --- /dev/null +++ b/packages/ssi/src/credentials/index.ts @@ -0,0 +1 @@ +export * from './credentials' diff --git a/packages/ssi/src/index.ts b/packages/ssi/src/index.ts index eafcc30..51a59b8 100644 --- a/packages/ssi/src/index.ts +++ b/packages/ssi/src/index.ts @@ -50,6 +50,8 @@ export * from './providers' export * from './hooks' export * from './wallet' export * from './connections' +export * from './credentials' +export * from './proofs' export { initializeAgent, AdeyaAgent } from './agent' // Core export { diff --git a/packages/ssi/src/proofs/index.ts b/packages/ssi/src/proofs/index.ts new file mode 100644 index 0000000..e7beffd --- /dev/null +++ b/packages/ssi/src/proofs/index.ts @@ -0,0 +1 @@ +export * from './proofs' diff --git a/packages/ssi/src/proofs/proofs.ts b/packages/ssi/src/proofs/proofs.ts new file mode 100644 index 0000000..fd25c10 --- /dev/null +++ b/packages/ssi/src/proofs/proofs.ts @@ -0,0 +1,145 @@ +import type { + V1ProofProtocol, + LegacyIndyProofFormatService, + AnonCredsProofFormatService +} from '@aries-framework/anoncreds' +import type { + AcceptProofRequestOptions, + CreateProofRequestOptions, + DeclineProofRequestOptions, + GetCredentialsForProofRequestOptions, + ProofExchangeRecord, + RequestProofOptions, + SelectCredentialsForProofRequestOptions, + SendProofProblemReportOptions, + V2ProofProtocol +} from '@aries-framework/core' + +import { useAdeyaAgent } from 'src/providers' + +export type ProofFormats = ( + | V1ProofProtocol + | V2ProofProtocol<(LegacyIndyProofFormatService | AnonCredsProofFormatService)[]> +)[] + +/** + * Retrieves the formatted data for a proof record with the given ID. + * + * @param proofRecordId The ID of the proof record to retrieve format data for. + * @returns A Promise that resolves with the format data for the proof record. + */ +export const getProofFormatData = async (proofRecordId: string) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.getFormatData(proofRecordId) +} + +/** + * Retrieves the available credentials for a proof request. + * + * @param options The options for retrieving the credentials. + * @returns A Promise that resolves with the credentials for the proof request. + */ +export const getCredentialsForProofRequest = async (options: GetCredentialsForProofRequestOptions) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.getCredentialsForRequest(options) +} + +/** + * Select the credentials to be used for a proof request. + * + * @param options - The options for selecting the credentials. + * @returns A promise that resolves to the selected credentials. + */ +export const selectCredentialsForProofRequest = async ( + options: SelectCredentialsForProofRequestOptions +) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.selectCredentialsForRequest(options) +} + +/** + * Retrieves the proof request agent message associated with the given proof record ID. + * + * @param proofRecordId The ID of the proof record to retrieve the request message for. + * @returns A Promise that resolves to the proof request message. + */ +export const getProofRequestAgentMessage = async (proofRecordId: string) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.findRequestMessage(proofRecordId) +} + +/** + * Creates a proof request. + * + * @param options - The options for creating the proof request. + * @returns A promise that resolves to the created proof request. + */ +export const createProofRequest = async (options: CreateProofRequestOptions) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.createRequest(options) +} + +/** + * Requests a proof. + * + * @param options - The options for requesting the proof. + * @returns A Promise that resolves with the ProofExchangeRecord + */ +export const requestProof = async (options: RequestProofOptions) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.requestProof(options) +} + +/** + * Update a proof exchange record. + * + * @param proofRecord The proof exchange record to update. + * @returns void. + */ +export const updateProofRecord = (proofRecord: ProofExchangeRecord) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.update(proofRecord) +} + +/** + * Accepts a proof request . + * + * @param options - The options for accepting the proof request. + * @returns A Promise that resolves with the result of accepting the proof request. + */ +export const acceptProofRequest = async (options: AcceptProofRequestOptions) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.acceptRequest(options) +} + +/** + * Decline a proof request. + * + * @param options - The options for declining the proof request. + * @returns A Promise that resolves ProofExchangeRecord of declining the proof request. + */ +export const declineProofRequest = async (options: DeclineProofRequestOptions) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.declineRequest(options) +} + +/** + * Sends a problem report for a proof to Agent. + * + * @param options - The options for sending the problem report. + * @returns A Promise that resolves ProofExchangeRecord when the problem report has been sent. + */ +export const sendProofProblemReport = async (options: SendProofProblemReportOptions) => { + const { agent } = useAdeyaAgent() + + return agent.proofs.sendProblemReport(options) +}