From 467736248ec29a5a69111efad7b463d9692a478c Mon Sep 17 00:00:00 2001 From: Sai Ranjit Tummalapalli Date: Mon, 9 Oct 2023 16:37:53 +0530 Subject: [PATCH] fix: add agent param in all the methods (#9) * feat: add credentials and proofs modules with dedicated functions Signed-off-by: Sai Ranjit Tummalapalli * chore: upgrade aries-framework packages to 0.4.2 Signed-off-by: Sai Ranjit Tummalapalli * refactor: remove isWalletImportable method Signed-off-by: Sai Ranjit Tummalapalli * fix: provider import in proof module Signed-off-by: Sai Ranjit Tummalapalli * fix: add agent param in all the methods Signed-off-by: Sai Ranjit Tummalapalli --------- Signed-off-by: Sai Ranjit Tummalapalli --- packages/ssi/src/connections/connections.ts | 113 ++++++++++++++------ packages/ssi/src/credentials/credentials.ts | 46 ++++---- packages/ssi/src/proofs/proofs.ts | 54 ++++------ 3 files changed, 121 insertions(+), 92 deletions(-) diff --git a/packages/ssi/src/connections/connections.ts b/packages/ssi/src/connections/connections.ts index 547cddd..584396d 100644 --- a/packages/ssi/src/connections/connections.ts +++ b/packages/ssi/src/connections/connections.ts @@ -1,22 +1,28 @@ +import type { AdeyaAgent } from '../agent' import type { + AgentMessage, + ConnectionInvitationMessage, CreateLegacyInvitationConfig, CreateOutOfBandInvitationConfig, - ReceiveOutOfBandInvitationConfig + OutOfBandInvitation, + ReceiveOutOfBandInvitationConfig, + Routing } from '@aries-framework/core' -import { useAdeyaAgent } from '../providers' - /** * Creates an invitation with RFC 0160: Connection Protocol and returns it together with out-of-band record and invitationUrl. * + * @param agent agent instance * @param domain domain of the agent * @param config configuration of how a connection invitation should be created * @returns out-of-band record and connection invitation together with invitationUrl */ -export const createLegacyInvitation = async (domain: string, config?: CreateLegacyInvitationConfig) => { - const { agent } = useAdeyaAgent() - - const record = await agent?.oob.createLegacyInvitation(config) +export const createLegacyInvitation = async ( + agent: AdeyaAgent, + domain: string, + config?: CreateLegacyInvitationConfig +) => { + const record = await agent.oob.createLegacyInvitation(config) const invitationUrl = record.invitation.toUrl({ domain }) @@ -27,17 +33,40 @@ export const createLegacyInvitation = async (domain: string, config?: CreateLega } } +/** + * Creates a legacy connectionless invitation using the provided configuration. + * + * @param agent - The agent instance to use for creating the invitation. + * @param config - The configuration object for creating the invitation. + * @param config.recordId - Optional record ID for the invitation. + * @param config.message - The agent message to include in the invitation. + * @param config.domain - The domain to use for the invitation. + * @param config.routing - Optional routing information for the invitation. + * + * @returns A Promise that resolves to the created invitation. + */ +export const createLegacyConnectionlessInvitation = async ( + agent: AdeyaAgent, + config: { + recordId?: string | undefined + message: AgentMessage + domain: string + routing?: Routing | undefined + } +) => { + return agent.oob.createLegacyConnectionlessInvitation(config) +} + /** * Creates an out-of-band invitation for establishing a connection with another agent. * + * @param agent The agent instance to use for creating the invitation. * @param domain The domain to use for the invitation URL. * @param config Optional configuration for the invitation. * @returns An object containing the invitation record, the invitation object, and the invitation URL. */ -export const createInvitation = async (domain: string, config?: CreateOutOfBandInvitationConfig) => { - const { agent } = useAdeyaAgent() - - const record = await agent?.oob.createInvitation(config) +export const createInvitation = async (agent: AdeyaAgent, domain: string, config?: CreateOutOfBandInvitationConfig) => { + const record = await agent.oob.createInvitation(config) const invitationUrl = record.outOfBandInvitation.toUrl({ domain }) @@ -48,35 +77,57 @@ export const createInvitation = async (domain: string, config?: CreateOutOfBandI } } +/** + * Accepts a connection invitation message or out-of-band invitation and returns the connection record. + * + * @param agent The agent instance to use for accepting the invitation. + * @param invitation The connection invitation message or out-of-band invitation to accept. + * @param config Optional configuration for receiving out-of-band invitations. + * + * @returns The connection record. + */ +export const acceptInvitation = async ( + agent: AdeyaAgent, + invitation: ConnectionInvitationMessage | OutOfBandInvitation, + config?: ReceiveOutOfBandInvitationConfig +) => { + const record = await agent.oob.receiveInvitation(invitation, config) + + return record +} + /** * Parses an invitation from a URL using the Adeya agent. * + * @param agent The agent instance to use for parsing the invitation. * @param invitationUrl The URL of the invitation to parse. * @returns A Promise that resolves with the parsed invitation. */ -export const parseInvitationFromUrl = async (invitationUrl: string) => { - const { agent } = useAdeyaAgent() - +export const parseInvitationFromUrl = async (agent: AdeyaAgent, invitationUrl: string) => { return agent.oob.parseInvitation(invitationUrl) } /** * Accepts a connection invitation from a URL. * + * @param agent The agent instance to use for accepting the invitation. * @param invitationUrl The URL of the connection invitation. * @param config Optional configuration for receiving the out-of-band invitation. * @returns A Promise that resolves to the connection record and out of band record. * @throws An error if the invitation cannot be parsed from the URL or if the connection does not have an ID. */ -export const acceptInvitationFromUrl = async (invitationUrl: string, config?: ReceiveOutOfBandInvitationConfig) => { - const { agent } = useAdeyaAgent() - const invitation = await agent?.oob.parseInvitation(invitationUrl) +export const acceptInvitationFromUrl = async ( + agent: AdeyaAgent, + invitationUrl: string, + config?: ReceiveOutOfBandInvitationConfig +) => { + const invitation = await agent.oob.parseInvitation(invitationUrl) if (!invitation) { throw new Error('Could not parse invitation from URL') } - const record = await agent?.oob.receiveInvitation(invitation, config) + const record = await agent.oob.receiveInvitation(invitation, config) const connectionRecord = record?.connectionRecord if (!connectionRecord?.id) { throw new Error('Connection does not have an ID') @@ -88,48 +139,44 @@ export const acceptInvitationFromUrl = async (invitationUrl: string, config?: Re /** * Returns all connections from the agent. * + * @param agent The agent instance to use for retrieving the connections. * @returns A promise that resolves to an array of Connection objects. */ -export const getAllConnections = async () => { - const { agent } = useAdeyaAgent() - +export const getAllConnections = async (agent: AdeyaAgent) => { return agent.connections.getAll() } /** * Retrieves a connection record by connectionId. * + * @param agent The agent instance to use for retrieving the connection. * @param connectionId The ID of the connection to retrieve. * @returns A Promise that resolves to the connection object. */ -export const getConnectionById = async (connectionId: string) => { - const { agent } = useAdeyaAgent() - +export const getConnectionById = async (agent: AdeyaAgent, connectionId: string) => { return agent.connections.getById(connectionId) } /** * Finds a connection record by its ID. * + * @param agent The agent instance to use for finding the connection. * @param connectionId The ID of the connection to find. * @returns A Promise that resolves with the connection object, or null if not found. */ -export const findConnectionById = async (connectionId: string) => { - const { agent } = useAdeyaAgent() - +export const findConnectionById = async (agent: AdeyaAgent, connectionId: string) => { return await agent.connections.findById(connectionId) } /** * Finds an out-of-band record by its connection ID. * + * @param agent The agent instance to use for finding the out-of-band record. * @param connectionId The ID of the connection to find. * @returns A Promise that resolves to the out-of-band record with the given ID. */ -export const findOutOfBandRecordById = async (connectionId: string) => { - const { agent } = useAdeyaAgent() - - return await agent.oob.findById(connectionId) +export const findOutOfBandRecordById = async (agent: AdeyaAgent, connectionId: string) => { + return agent.oob.findById(connectionId) } /** @@ -138,9 +185,7 @@ export const findOutOfBandRecordById = async (connectionId: string) => { * @param connectionId The ID of the connection to be deleted. * @returns A boolean indicating whether the connection was successfully deleted or not. */ -export const deleteConnectionById = async (connectionId: string) => { - const { agent } = useAdeyaAgent() - +export const deleteConnectionById = async (agent: AdeyaAgent, connectionId: string) => { await agent.connections.deleteById(connectionId) return true } diff --git a/packages/ssi/src/credentials/credentials.ts b/packages/ssi/src/credentials/credentials.ts index 284cf8e..8b81c2d 100644 --- a/packages/ssi/src/credentials/credentials.ts +++ b/packages/ssi/src/credentials/credentials.ts @@ -1,3 +1,4 @@ +import type { AdeyaAgent } from '../agent' import type { V1CredentialProtocol, LegacyIndyCredentialFormatService, @@ -12,38 +13,36 @@ import type { V2CredentialProtocol } from '@aries-framework/core' -import { useAdeyaAgent } from '../providers' - /** * Retrieves all credential exchange records from the agent. * + * @param agent The agent instance to use for retrieving the credential exchange records. * @returns A promise that resolves to an array of credential exchange records. */ -export const getAllCredentialExchangeRecords = async () => { - const { agent } = useAdeyaAgent() - +export const getAllCredentialExchangeRecords = async (agent: AdeyaAgent) => { return agent.credentials.getAll() } /** * Retrieves the formatted data for a given credential record ID. * + * @param agent The agent instance to use for retrieving the formatted data. * @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() - +export const getFormattedCredentialData = async (agent: AdeyaAgent, credentialRecordId: string) => { return agent.credentials.getFormatData(credentialRecordId) } /** * Accepts a credential offer. * + * @param agent The agent instance to use for accepting the credential offer. * @param options - The options for accepting the credential offer. * @returns A promise that resolves with the accepted credential. */ export const acceptCredentialOffer = async ( + agent: AdeyaAgent, options: AcceptCredentialOfferOptions< ( | V1CredentialProtocol @@ -53,60 +52,56 @@ export const acceptCredentialOffer = async ( )[] > ) => { - const { agent } = useAdeyaAgent() - return agent.credentials.acceptOffer(options) } /** * Updates a credential exchange record. * + * @param agent The agent instance to use for updating the 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() - +export const updateCredentialExchangeRecord = async (agent: AdeyaAgent, credentialRecord: CredentialExchangeRecord) => { return agent.credentials.update(credentialRecord) } /** * Declines a credential offer. * + * @param agent The agent instance to use for declining the 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() - +export const declineCredentialOffer = async (agent: AdeyaAgent, credentialId: string) => { return agent.credentials.declineOffer(credentialId) } /** * Deletes a credential exchange record with the given ID. + * + * @param agent The agent instance to use for deleting the credential exchange record. * @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 ( + agent: AdeyaAgent, credentialRecordId: string, options?: DeleteCredentialOptions ) => { - const { agent } = useAdeyaAgent() - return agent.credentials.deleteById(credentialRecordId, options) } /** * Sends a problem report for a credential to Agent. * + * @param agent The agent instance to use for sending the problem report. * @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() - +export const sendCredentialProblemReport = async (agent: AdeyaAgent, options: SendCredentialProblemReportOptions) => { return agent.credentials.sendProblemReport(options) } @@ -115,12 +110,11 @@ export const sendCredentialProblemReport = async (options: SendCredentialProblem /** * Retrieves a W3C credential record by its ID. * + * @param agent The agent instance to use for retrieving the credential record. * @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() - +export const getW3cCredentialRecordById = async (agent: AdeyaAgent, credentialRecordId: string) => { return agent.w3cCredentials.getCredentialRecordById(credentialRecordId) } @@ -129,8 +123,6 @@ export const getW3cCredentialRecordById = async (credentialRecordId: string) => * * @returns A promise that resolves to an array of W3C credential records. */ -export const getAllW3cCredentialRecords = async () => { - const { agent } = useAdeyaAgent() - +export const getAllW3cCredentialRecords = async (agent: AdeyaAgent) => { return agent.w3cCredentials.getAllCredentialRecords() } diff --git a/packages/ssi/src/proofs/proofs.ts b/packages/ssi/src/proofs/proofs.ts index 58711cc..aef0a39 100644 --- a/packages/ssi/src/proofs/proofs.ts +++ b/packages/ssi/src/proofs/proofs.ts @@ -1,3 +1,4 @@ +import type { AdeyaAgent } from '../agent' import type { V1ProofProtocol, LegacyIndyProofFormatService, @@ -15,8 +16,6 @@ import type { V2ProofProtocol } from '@aries-framework/core' -import { useAdeyaAgent } from '../providers' - export type ProofFormats = ( | V1ProofProtocol | V2ProofProtocol<(LegacyIndyProofFormatService | AnonCredsProofFormatService)[]> @@ -25,110 +24,105 @@ export type ProofFormats = ( /** * Retrieves the formatted data for a proof record with the given ID. * + * @param agent The agent instance to use for retrieving the format data. * @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() - +export const getProofFormatData = async (agent: AdeyaAgent, proofRecordId: string) => { return agent.proofs.getFormatData(proofRecordId) } /** * Retrieves the available credentials for a proof request. * + * @param agent The agent instance to use for retrieving the credentials. * @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() - +export const getCredentialsForProofRequest = async ( + agent: AdeyaAgent, + options: GetCredentialsForProofRequestOptions +) => { return agent.proofs.getCredentialsForRequest(options) } /** * Select the credentials to be used for a proof request. * + * @param agent The agent instance to use for selecting the credentials. * @param options - The options for selecting the credentials. * @returns A promise that resolves to the selected credentials. */ export const selectCredentialsForProofRequest = async ( + agent: AdeyaAgent, options: SelectCredentialsForProofRequestOptions ) => { - const { agent } = useAdeyaAgent() - return agent.proofs.selectCredentialsForRequest(options) } /** * Retrieves the proof request agent message associated with the given proof record ID. * + * @param agent The agent instance to use for retrieving the proof request message. * @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() - +export const getProofRequestAgentMessage = async (agent: AdeyaAgent, proofRecordId: string) => { return agent.proofs.findRequestMessage(proofRecordId) } /** * Creates a proof request. * + * @param agent The agent instance to use for creating the 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() - +export const createProofRequest = async (agent: AdeyaAgent, options: CreateProofRequestOptions) => { return agent.proofs.createRequest(options) } /** * Requests a proof. * + * @param agent The agent instance to use for requesting the 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() - +export const requestProof = async (agent: AdeyaAgent, options: RequestProofOptions) => { return agent.proofs.requestProof(options) } /** * Update a proof exchange record. * + * @param agent The agent instance to use for updating the proof exchange record. * @param proofRecord The proof exchange record to update. * @returns void. */ -export const updateProofRecord = (proofRecord: ProofExchangeRecord) => { - const { agent } = useAdeyaAgent() - +export const updateProofRecord = (agent: AdeyaAgent, proofRecord: ProofExchangeRecord) => { return agent.proofs.update(proofRecord) } /** * Accepts a proof request . * + * @param agent The agent instance to use for accepting the 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() - +export const acceptProofRequest = async (agent: AdeyaAgent, options: AcceptProofRequestOptions) => { return agent.proofs.acceptRequest(options) } /** * Decline a proof request. * + * @param agent The agent instance to use for declining the 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() - +export const declineProofRequest = async (agent: AdeyaAgent, options: DeclineProofRequestOptions) => { return agent.proofs.declineRequest(options) } @@ -138,8 +132,6 @@ export const declineProofRequest = async (options: DeclineProofRequestOptions) = * @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() - +export const sendProofProblemReport = async (agent: AdeyaAgent, options: SendProofProblemReportOptions) => { return agent.proofs.sendProblemReport(options) }