Skip to content

Commit

Permalink
refactor(proofs)!: generalize proofs api and improve consistency with…
Browse files Browse the repository at this point in the history
… credentials module (openwallet-foundation#1279)

Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra committed Feb 13, 2023
1 parent 51030d4 commit fb8d58b
Show file tree
Hide file tree
Showing 165 changed files with 7,121 additions and 6,319 deletions.
5 changes: 1 addition & 4 deletions demo/src/Alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ export class Alice extends BaseAgent {
}

public async acceptProofRequest(proofRecord: ProofExchangeRecord) {
const requestedCredentials = await this.agent.proofs.autoSelectCredentialsForProofRequest({
const requestedCredentials = await this.agent.proofs.selectCredentialsForRequest({
proofRecordId: proofRecord.id,
config: {
filterByPresentationPreview: true,
},
})

await this.agent.proofs.acceptRequest({
Expand Down
18 changes: 6 additions & 12 deletions demo/src/Faber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import type { ConnectionRecord, ConnectionStateChangedEvent } from '@aries-frame
import type { CredDef, Schema } from 'indy-sdk'
import type BottomBar from 'inquirer/lib/ui/bottom-bar'

import {
AttributeFilter,
ProofAttributeInfo,
utils,
V1CredentialPreview,
ConnectionEventTypes,
} from '@aries-framework/core'
import { utils, V1CredentialPreview, ConnectionEventTypes } from '@aries-framework/core'
import { ui } from 'inquirer'

import { BaseAgent } from './BaseAgent'
Expand Down Expand Up @@ -171,15 +165,16 @@ export class Faber extends BaseAgent {
private async newProofAttribute() {
await this.printProofFlow(greenText(`Creating new proof attribute for 'name' ...\n`))
const proofAttribute = {
name: new ProofAttributeInfo({
name: {
name: 'name',
restrictions: [
new AttributeFilter({
{
credentialDefinitionId: this.credentialDefinition?.id,
}),
},
],
}),
},
}

return proofAttribute
}

Expand All @@ -195,7 +190,6 @@ export class Faber extends BaseAgent {
indy: {
name: 'proof-request',
version: '1.0',
nonce: '1298236324864',
requestedAttributes: proofAttribute,
},
},
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"test": "jest",
"lint": "eslint --ignore-path .gitignore .",
"validate": "yarn lint && yarn check-types && yarn check-format",
"prepare": "husky install",
"run-mediator": "ts-node ./samples/mediator.ts",
"next-version-bump": "ts-node ./scripts/get-next-bump.ts"
},
Expand All @@ -48,7 +47,6 @@
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-prettier": "^3.4.0",
"express": "^4.17.1",
"husky": "^7.0.1",
"indy-sdk": "^1.16.0-dev-1636",
"jest": "^27.0.4",
"lerna": "^4.0.0",
Expand Down
114 changes: 62 additions & 52 deletions packages/anoncreds/src/formats/LegacyIndyCredentialFormatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import type { AnonCredsCredentialMetadata } from '../utils/metadata'
import type {
CredentialFormatService,
AgentContext,
FormatCreateProposalOptions,
FormatCreateProposalReturn,
FormatProcessOptions,
FormatAcceptProposalOptions,
FormatCreateOfferReturn,
FormatCreateOfferOptions,
FormatAcceptOfferOptions,
CredentialFormatCreateProposalOptions,
CredentialFormatCreateProposalReturn,
CredentialFormatProcessOptions,
CredentialFormatAcceptProposalOptions,
CredentialFormatCreateOfferReturn,
CredentialFormatCreateOfferOptions,
CredentialFormatAcceptOfferOptions,
CredentialFormatCreateReturn,
FormatAcceptRequestOptions,
FormatProcessCredentialOptions,
FormatAutoRespondProposalOptions,
FormatAutoRespondOfferOptions,
FormatAutoRespondRequestOptions,
FormatAutoRespondCredentialOptions,
CredentialFormatAcceptRequestOptions,
CredentialFormatProcessCredentialOptions,
CredentialFormatAutoRespondProposalOptions,
CredentialFormatAutoRespondOfferOptions,
CredentialFormatAutoRespondRequestOptions,
CredentialFormatAutoRespondCredentialOptions,
CredentialExchangeRecord,
CredentialPreviewAttributeOptions,
LinkedAttachment,
Expand Down Expand Up @@ -80,8 +80,8 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
*/
public async createProposal(
agentContext: AgentContext,
{ credentialFormats, credentialRecord }: FormatCreateProposalOptions<LegacyIndyCredentialFormat>
): Promise<FormatCreateProposalReturn> {
{ credentialFormats, credentialRecord }: CredentialFormatCreateProposalOptions<LegacyIndyCredentialFormat>
): Promise<CredentialFormatCreateProposalReturn> {
const format = new CredentialFormatSpec({
format: INDY_CRED_FILTER,
})
Expand All @@ -106,7 +106,7 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
}

const proposalJson = JsonTransformer.toJSON(proposal)
const attachment = this.getFormatData(proposalJson, format.attachId)
const attachment = this.getFormatData(proposalJson, format.attachmentId)

const { previewAttributes } = this.getCredentialLinkedAttachments(
indyFormat.attributes,
Expand All @@ -122,7 +122,10 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
return { format, attachment, previewAttributes }
}

public async processProposal(agentContext: AgentContext, { attachment }: FormatProcessOptions): Promise<void> {
public async processProposal(
agentContext: AgentContext,
{ attachment }: CredentialFormatProcessOptions
): Promise<void> {
const proposalJson = attachment.getDataAsJson()

// fromJSON also validates
Expand All @@ -132,12 +135,12 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
public async acceptProposal(
agentContext: AgentContext,
{
attachId,
attachmentId,
credentialFormats,
credentialRecord,
proposalAttachment,
}: FormatAcceptProposalOptions<LegacyIndyCredentialFormat>
): Promise<FormatCreateOfferReturn> {
}: CredentialFormatAcceptProposalOptions<LegacyIndyCredentialFormat>
): Promise<CredentialFormatCreateOfferReturn> {
const indyFormat = credentialFormats?.indy

const credentialProposal = JsonTransformer.fromJSON(proposalAttachment.getDataAsJson(), IndyCredPropose)
Expand All @@ -158,7 +161,7 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic

const { format, attachment, previewAttributes } = await this.createIndyOffer(agentContext, {
credentialRecord,
attachId,
attachmentId,
attributes,
credentialDefinitionId,
linkedAttachments: indyFormat?.linkedAttachments,
Expand All @@ -176,8 +179,12 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
*/
public async createOffer(
agentContext: AgentContext,
{ credentialFormats, credentialRecord, attachId }: FormatCreateOfferOptions<LegacyIndyCredentialFormat>
): Promise<FormatCreateOfferReturn> {
{
credentialFormats,
credentialRecord,
attachmentId,
}: CredentialFormatCreateOfferOptions<LegacyIndyCredentialFormat>
): Promise<CredentialFormatCreateOfferReturn> {
const indyFormat = credentialFormats.indy

if (!indyFormat) {
Expand All @@ -186,7 +193,7 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic

const { format, attachment, previewAttributes } = await this.createIndyOffer(agentContext, {
credentialRecord,
attachId,
attachmentId,
attributes: indyFormat.attributes,
credentialDefinitionId: indyFormat.credentialDefinitionId,
linkedAttachments: indyFormat.linkedAttachments,
Expand All @@ -195,7 +202,10 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
return { format, attachment, previewAttributes }
}

public async processOffer(agentContext: AgentContext, { attachment, credentialRecord }: FormatProcessOptions) {
public async processOffer(
agentContext: AgentContext,
{ attachment, credentialRecord }: CredentialFormatProcessOptions
) {
agentContext.config.logger.debug(`Processing indy credential offer for credential record ${credentialRecord.id}`)

const credOffer = attachment.getDataAsJson<AnonCredsCredentialOffer>()
Expand All @@ -211,10 +221,10 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
agentContext: AgentContext,
{
credentialRecord,
attachId,
attachmentId,
offerAttachment,
credentialFormats,
}: FormatAcceptOfferOptions<LegacyIndyCredentialFormat>
}: CredentialFormatAcceptOfferOptions<LegacyIndyCredentialFormat>
): Promise<CredentialFormatCreateReturn> {
const registryService = agentContext.dependencyManager.resolve(AnonCredsRegistryService)
const holderService = agentContext.dependencyManager.resolve<AnonCredsHolderService>(AnonCredsHolderServiceSymbol)
Expand Down Expand Up @@ -250,11 +260,11 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
})

const format = new CredentialFormatSpec({
attachId,
attachmentId,
format: INDY_CRED_REQUEST,
})

const attachment = this.getFormatData(credentialRequest, format.attachId)
const attachment = this.getFormatData(credentialRequest, format.attachmentId)
return { format, attachment }
}

Expand All @@ -269,18 +279,18 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
* We don't have any models to validate an indy request object, for now this method does nothing
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async processRequest(agentContext: AgentContext, options: FormatProcessOptions): Promise<void> {
public async processRequest(agentContext: AgentContext, options: CredentialFormatProcessOptions): Promise<void> {
// not needed for Indy
}

public async acceptRequest(
agentContext: AgentContext,
{
credentialRecord,
attachId,
attachmentId,
offerAttachment,
requestAttachment,
}: FormatAcceptRequestOptions<LegacyIndyCredentialFormat>
}: CredentialFormatAcceptRequestOptions<LegacyIndyCredentialFormat>
): Promise<CredentialFormatCreateReturn> {
// Assert credential attributes
const credentialAttributes = credentialRecord.credentialAttributes
Expand Down Expand Up @@ -314,11 +324,11 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
}

const format = new CredentialFormatSpec({
attachId,
attachmentId,
format: INDY_CRED,
})

const attachment = this.getFormatData(credential, format.attachId)
const attachment = this.getFormatData(credential, format.attachmentId)
return { format, attachment }
}

Expand All @@ -329,7 +339,7 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
*/
public async processCredential(
agentContext: AgentContext,
{ credentialRecord, attachment }: FormatProcessCredentialOptions
{ credentialRecord, attachment }: CredentialFormatProcessCredentialOptions
): Promise<void> {
const credentialRequestMetadata = credentialRecord.metadata.get<AnonCredsCredentialRequestMetadata>(
AnonCredsCredentialRequestMetadataKey
Expand Down Expand Up @@ -428,15 +438,15 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
}

/**
* Gets the attachment object for a given attachId. We need to get out the correct attachId for
* Gets the attachment object for a given attachmentId. We need to get out the correct attachmentId for
* indy and then find the corresponding attachment (if there is one)
* @param formats the formats object containing the attachId
* @param formats the formats object containing the attachmentId
* @param messageAttachments the attachments containing the payload
* @returns The Attachment if found or undefined
*
*/
public getAttachment(formats: CredentialFormatSpec[], messageAttachments: Attachment[]): Attachment | undefined {
const supportedAttachmentIds = formats.filter((f) => this.supportsFormat(f.format)).map((f) => f.attachId)
const supportedAttachmentIds = formats.filter((f) => this.supportsFormat(f.format)).map((f) => f.attachmentId)
const supportedAttachment = messageAttachments.find((attachment) => supportedAttachmentIds.includes(attachment.id))

return supportedAttachment
Expand All @@ -449,9 +459,9 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
await anonCredsHolderService.deleteCredential(agentContext, credentialRecordId)
}

public shouldAutoRespondToProposal(
public async shouldAutoRespondToProposal(
agentContext: AgentContext,
{ offerAttachment, proposalAttachment }: FormatAutoRespondProposalOptions
{ offerAttachment, proposalAttachment }: CredentialFormatAutoRespondProposalOptions
) {
const credentialProposalJson = proposalAttachment.getDataAsJson()
const credentialProposal = JsonTransformer.fromJSON(credentialProposalJson, IndyCredPropose)
Expand All @@ -464,9 +474,9 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
return credentialProposal.credentialDefinitionId === credentialOfferJson.cred_def_id
}

public shouldAutoRespondToOffer(
public async shouldAutoRespondToOffer(
agentContext: AgentContext,
{ offerAttachment, proposalAttachment }: FormatAutoRespondOfferOptions
{ offerAttachment, proposalAttachment }: CredentialFormatAutoRespondOfferOptions
) {
const credentialProposalJson = proposalAttachment.getDataAsJson()
const credentialProposal = JsonTransformer.fromJSON(credentialProposalJson, IndyCredPropose)
Expand All @@ -479,19 +489,19 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
return credentialProposal.credentialDefinitionId === credentialOfferJson.cred_def_id
}

public shouldAutoRespondToRequest(
public async shouldAutoRespondToRequest(
agentContext: AgentContext,
{ offerAttachment, requestAttachment }: FormatAutoRespondRequestOptions
{ offerAttachment, requestAttachment }: CredentialFormatAutoRespondRequestOptions
) {
const credentialOfferJson = offerAttachment.getDataAsJson<AnonCredsCredentialOffer>()
const credentialRequestJson = requestAttachment.getDataAsJson<AnonCredsCredentialRequest>()

return credentialOfferJson.cred_def_id == credentialRequestJson.cred_def_id
return credentialOfferJson.cred_def_id === credentialRequestJson.cred_def_id
}

public shouldAutoRespondToCredential(
public async shouldAutoRespondToCredential(
agentContext: AgentContext,
{ credentialRecord, requestAttachment, credentialAttachment }: FormatAutoRespondCredentialOptions
{ credentialRecord, requestAttachment, credentialAttachment }: CredentialFormatAutoRespondCredentialOptions
) {
const credentialJson = credentialAttachment.getDataAsJson<AnonCredsCredential>()
const credentialRequestJson = requestAttachment.getDataAsJson<AnonCredsCredentialRequest>()
Expand All @@ -511,24 +521,24 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
agentContext: AgentContext,
{
credentialRecord,
attachId,
attachmentId,
credentialDefinitionId,
attributes,
linkedAttachments,
}: {
credentialDefinitionId: string
credentialRecord: CredentialExchangeRecord
attachId?: string
attachmentId?: string
attributes: CredentialPreviewAttributeOptions[]
linkedAttachments?: LinkedAttachment[]
}
): Promise<FormatCreateOfferReturn> {
): Promise<CredentialFormatCreateOfferReturn> {
const anonCredsIssuerService =
agentContext.dependencyManager.resolve<AnonCredsIssuerService>(AnonCredsIssuerServiceSymbol)

// if the proposal has an attachment Id use that, otherwise the generated id of the formats object
const format = new CredentialFormatSpec({
attachId: attachId,
attachmentId: attachmentId,
format: INDY_CRED_ABSTRACT,
})

Expand All @@ -548,7 +558,7 @@ export class LegacyIndyCredentialFormatService implements CredentialFormatServic
credentialDefinitionId: offer.cred_def_id,
})

const attachment = this.getFormatData(offer, format.attachId)
const attachment = this.getFormatData(offer, format.attachmentId)

return { format, attachment, previewAttributes }
}
Expand Down
3 changes: 0 additions & 3 deletions packages/bbs-signatures/tests/bbs-signatures.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,7 @@ describeSkipNode17And18('BBS W3cCredentialService', () => {

const result = await w3cCredentialService.verifyPresentation(agentContext, {
presentation: vp,
proofType: 'Ed25519Signature2018',
challenge: 'e950bfe5-d7ec-4303-ad61-6983fb976ac9',
verificationMethod:
'did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL#z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL',
})

expect(result.verified).toBe(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describeSkipNode17And18('credentials, BBS+ signature', () => {

testLogger.test('Alice sends (v2, Indy) credential proposal to Faber')

const credentialExchangeRecord: CredentialExchangeRecord = await aliceAgent.credentials.proposeCredential({
const credentialExchangeRecord = await aliceAgent.credentials.proposeCredential({
connectionId: aliceConnection.id,
protocolVersion: 'v2',
credentialFormats: {
Expand Down
Loading

0 comments on commit fb8d58b

Please sign in to comment.