Skip to content

Commit

Permalink
feat(proofs): add getRequestedCredentialsForProofRequest (#1028)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Richardson <[email protected]>
  • Loading branch information
NB-MikeRichardson authored Sep 22, 2022
1 parent 82a17a3 commit 26bb9c9
Show file tree
Hide file tree
Showing 4 changed files with 413 additions and 281 deletions.
30 changes: 29 additions & 1 deletion packages/core/src/modules/proofs/ProofsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import type {
} from './ProofsApiOptions'
import type { ProofFormat } from './formats/ProofFormat'
import type { IndyProofFormat } from './formats/indy/IndyProofFormat'
import type { AutoSelectCredentialsForProofRequestOptions } from './models/ModuleOptions'
import type {
AutoSelectCredentialsForProofRequestOptions,
GetRequestedCredentialsForProofRequest,
} from './models/ModuleOptions'
import type {
CreateOutOfBandRequestOptions,
CreatePresentationOptions,
Expand Down Expand Up @@ -70,6 +73,11 @@ export interface ProofsApi<PFs extends ProofFormat[], PSs extends ProofService<P
options: AutoSelectCredentialsForProofRequestOptions
): Promise<FormatRequestedCredentialReturn<PFs>>

// Get Requested Credentials
getRequestedCredentialsForProofRequest(
options: AutoSelectCredentialsForProofRequestOptions
): Promise<FormatRetrievedCredentialOptions<PFs>>

sendProblemReport(proofRecordId: string, message: string): Promise<ProofRecord>

// Record Methods
Expand Down Expand Up @@ -446,6 +454,26 @@ export class ProofsApi<
return await service.autoSelectCredentialsForProofRequest(retrievedCredentials)
}

/**
* Create a {@link RetrievedCredentials} object. Given input proof request and presentation proposal,
* use credentials in the wallet to build indy requested credentials object for input to proof creation.
* If restrictions allow, self attested attributes will be used.
*
* @param options multiple properties like proof record id and optional configuration
* @returns RetrievedCredentials object
*/
public async getRequestedCredentialsForProofRequest(
options: GetRequestedCredentialsForProofRequest
): Promise<FormatRetrievedCredentialOptions<PFs>> {
const record = await this.getById(options.proofRecordId)
const service = this.getService(record.protocolVersion)

return await service.getRequestedCredentialsForProofRequest(this.agentContext, {
proofRecord: record,
config: options.config,
})
}

/**
* Send problem report message for a proof record
*
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/modules/proofs/models/ModuleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export interface AutoSelectCredentialsForProofRequestOptions {
proofRecordId: string
config?: GetRequestedCredentialsConfig
}

export type GetRequestedCredentialsForProofRequest = AutoSelectCredentialsForProofRequestOptions
74 changes: 74 additions & 0 deletions packages/core/tests/v2-indy-proofs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,80 @@ describe('Present Proof', () => {
})
})

test('Alice provides credentials via call to getRequestedCredentials', async () => {
const attributes = {
name: new ProofAttributeInfo({
name: 'name',
restrictions: [
new AttributeFilter({
credentialDefinitionId: credDefId,
}),
],
}),
image_0: new ProofAttributeInfo({
name: 'image_0',
restrictions: [
new AttributeFilter({
credentialDefinitionId: credDefId,
}),
],
}),
}

// Sample predicates
const predicates = {
age: new ProofPredicateInfo({
name: 'age',
predicateType: PredicateType.GreaterThanOrEqualTo,
predicateValue: 50,
restrictions: [
new AttributeFilter({
credentialDefinitionId: credDefId,
}),
],
}),
}

const requestProofsOptions: RequestProofOptions = {
protocolVersion: ProofProtocolVersion.V2,
connectionId: faberConnection.id,
proofFormats: {
indy: {
name: 'proof-request',
version: '1.0',
nonce: '1298236324864',
requestedAttributes: attributes,
requestedPredicates: predicates,
},
},
}

const aliceProofRecordPromise = waitForProofRecord(aliceAgent, {
state: ProofState.RequestReceived,
})

// Faber sends a presentation request to Alice
testLogger.test('Faber sends a presentation request to Alice')
faberProofRecord = await faberAgent.proofs.requestProof(requestProofsOptions)

// Alice waits for presentation request from Faber
testLogger.test('Alice waits for presentation request from Faber')
aliceProofRecord = await aliceProofRecordPromise

const retrievedCredentials = await faberAgent.proofs.getRequestedCredentialsForProofRequest({
proofRecordId: faberProofRecord.id,
config: {},
})

if (retrievedCredentials.proofFormats.indy) {
const keys = Object.keys(retrievedCredentials.proofFormats.indy?.requestedAttributes)
expect(keys).toContain('name')
expect(keys).toContain('image_0')
} else {
fail()
}
})

test('Faber starts with proof request to Alice but gets Problem Reported', async () => {
const attributes = {
name: new ProofAttributeInfo({
Expand Down
Loading

0 comments on commit 26bb9c9

Please sign in to comment.