Skip to content

Commit

Permalink
fix: add agent param in all the methods (#9)
Browse files Browse the repository at this point in the history
* feat: add credentials and proofs modules with dedicated functions

Signed-off-by: Sai Ranjit Tummalapalli <sairanjit.tummalapalli@ayanworks.com>

* chore: upgrade aries-framework packages to 0.4.2

Signed-off-by: Sai Ranjit Tummalapalli <sairanjit.tummalapalli@ayanworks.com>

* refactor: remove isWalletImportable method

Signed-off-by: Sai Ranjit Tummalapalli <sairanjit.tummalapalli@ayanworks.com>

* fix: provider import in proof module

Signed-off-by: Sai Ranjit Tummalapalli <sairanjit.tummalapalli@ayanworks.com>

* fix: add agent param in all the methods

Signed-off-by: Sai Ranjit Tummalapalli <sairanjit.tummalapalli@ayanworks.com>

---------

Signed-off-by: Sai Ranjit Tummalapalli <sairanjit.tummalapalli@ayanworks.com>
  • Loading branch information
sairanjit authored Oct 9, 2023
1 parent c9d02cb commit 4677362
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 92 deletions.
113 changes: 79 additions & 34 deletions packages/ssi/src/connections/connections.ts
Original file line number Diff line number Diff line change
@@ -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 })

Expand All @@ -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 })

Expand All @@ -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')
Expand All @@ -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)
}

/**
Expand All @@ -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
}
46 changes: 19 additions & 27 deletions packages/ssi/src/credentials/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AdeyaAgent } from '../agent'
import type {
V1CredentialProtocol,
LegacyIndyCredentialFormatService,
Expand All @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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()
}
Loading

0 comments on commit 4677362

Please sign in to comment.