Skip to content

Commit

Permalink
refactor(core)!: simplify get creds for proof api (#523)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `ProofsModule.getRequestedCredentialsForProofRequest` expected some low level message objects as input. This is not in line with the public API of the rest of the framework and has been simplified to only require a proof record id and optionally a boolean whether the retrieved credentials should be filtered based on the proof proposal (if available).

Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Nov 8, 2021
1 parent 1b4d8d6 commit ba9698d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
34 changes: 29 additions & 5 deletions packages/core/src/modules/proofs/ProofsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,30 @@ export class ProofsModule {
* If restrictions allow, self attested attributes will be used.
*
*
* @param proofRequest The proof request to build the requested credentials object from
* @param presentationProposal Optional presentation proposal to improve credential selection algorithm
* @param proofRecordId the id of the proof request to get the matching credentials for
* @param config optional configuration for credential selection process. Use `filterByPresentationPreview` (default `true`) to only include
* credentials that match the presentation preview from the presentation proposal (if available).
* @returns RetrievedCredentials object
*/
public async getRequestedCredentialsForProofRequest(
proofRequest: ProofRequest,
presentationProposal?: PresentationPreview
proofRecordId: string,
config?: GetRequestedCredentialsConfig
): Promise<RetrievedCredentials> {
return this.proofService.getRequestedCredentialsForProofRequest(proofRequest, presentationProposal)
const proofRecord = await this.proofService.getById(proofRecordId)

const indyProofRequest = proofRecord.requestMessage?.indyProofRequest
const presentationPreview = config?.filterByPresentationPreview
? proofRecord.proposalMessage?.presentationProposal
: undefined

if (!indyProofRequest) {
throw new AriesFrameworkError(
'Unable to get requested credentials for proof request. No proof request message was found or the proof request message does not contain an indy proof request.'
)
}

return this.proofService.getRequestedCredentialsForProofRequest(indyProofRequest, presentationPreview)
}

/**
Expand Down Expand Up @@ -422,3 +437,12 @@ export interface ProofRequestConfig {
comment?: string
autoAcceptProof?: AutoAcceptProof
}

export interface GetRequestedCredentialsConfig {
/**
* Whether to filter the retrieved credentials using the presentation preview.
* This configuration will only have effect if a presentation proposal message is available
* containing a presentation preview.
*/
filterByPresentationPreview?: boolean
}
11 changes: 4 additions & 7 deletions packages/core/tests/connectionless-proofs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import testLogger from './logger'

describe('Present Proof', () => {
test('Faber starts with connection-less proof requests to Alice', async () => {
const { aliceAgent, faberAgent, aliceReplay, credDefId, faberReplay, presentationPreview } = await setupProofsTest(
const { aliceAgent, faberAgent, aliceReplay, credDefId, faberReplay } = await setupProofsTest(
'Faber connection-less Proofs',
'Alice connection-less Proofs',
AutoAcceptProof.Never
Expand Down Expand Up @@ -59,12 +59,9 @@ describe('Present Proof', () => {
})

testLogger.test('Alice accepts presentation request from Faber')
const indyProofRequest = aliceProofRecord.requestMessage?.indyProofRequest
const retrievedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
indyProofRequest!,
presentationPreview
)
const retrievedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(aliceProofRecord.id, {
filterByPresentationPreview: true,
})
const requestedCredentials = aliceAgent.proofs.autoSelectCredentialsForProofRequest(retrievedCredentials)
await aliceAgent.proofs.acceptRequest(aliceProofRecord.id, requestedCredentials)

Expand Down
6 changes: 1 addition & 5 deletions packages/core/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,7 @@ export async function presentProof({
state: ProofState.RequestReceived,
})

const indyProofRequest = holderRecord.requestMessage?.indyProofRequest
if (!indyProofRequest) {
throw new Error('indyProofRequest missing')
}
const retrievedCredentials = await holderAgent.proofs.getRequestedCredentialsForProofRequest(indyProofRequest)
const retrievedCredentials = await holderAgent.proofs.getRequestedCredentialsForProofRequest(holderRecord.id)
const requestedCredentials = holderAgent.proofs.autoSelectCredentialsForProofRequest(retrievedCredentials)
await holderAgent.proofs.acceptRequest(holderRecord.id, requestedCredentials)

Expand Down
9 changes: 3 additions & 6 deletions packages/core/tests/proofs-auto-accept.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,9 @@ describe('Auto accept present proof', () => {
})

testLogger.test('Alice accepts presentation request from Faber')
const indyProofRequest = aliceProofRecord.requestMessage?.indyProofRequest
const retrievedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
indyProofRequest!,
presentationPreview
)
const retrievedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(aliceProofRecord.id, {
filterByPresentationPreview: true,
})
const requestedCredentials = aliceAgent.proofs.autoSelectCredentialsForProofRequest(retrievedCredentials)
await aliceAgent.proofs.acceptRequest(aliceProofRecord.id, requestedCredentials)

Expand Down
18 changes: 7 additions & 11 deletions packages/core/tests/proofs.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Agent, ConnectionRecord, PresentationPreview, ProofRequest } from '../src'
import type { Agent, ConnectionRecord, PresentationPreview } from '../src'
import type { CredDefId } from 'indy-sdk'

import {
Expand Down Expand Up @@ -95,11 +95,9 @@ describe('Present Proof', () => {

// Alice retrieves the requested credentials and accepts the presentation request
testLogger.test('Alice accepts presentation request from Faber')
const indyProofRequest = aliceProofRecord.requestMessage?.indyProofRequest as ProofRequest
const retrievedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(
indyProofRequest,
presentationPreview
)
const retrievedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(aliceProofRecord.id, {
filterByPresentationPreview: true,
})
const requestedCredentials = aliceAgent.proofs.autoSelectCredentialsForProofRequest(retrievedCredentials)
await aliceAgent.proofs.acceptRequest(aliceProofRecord.id, requestedCredentials)

Expand Down Expand Up @@ -254,11 +252,9 @@ describe('Present Proof', () => {

// Alice retrieves the requested credentials and accepts the presentation request
testLogger.test('Alice accepts presentation request from Faber')
const indyProofRequest = aliceProofRecord.requestMessage?.indyProofRequest as ProofRequest
const retrievedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(
indyProofRequest,
presentationPreview
)
const retrievedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(aliceProofRecord.id, {
filterByPresentationPreview: true,
})
const requestedCredentials = aliceAgent.proofs.autoSelectCredentialsForProofRequest(retrievedCredentials)
await aliceAgent.proofs.acceptRequest(aliceProofRecord.id, requestedCredentials)

Expand Down

0 comments on commit ba9698d

Please sign in to comment.