-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: propose payload attachment in in snake_case JSON format (#775)
Signed-off-by: Mike Richardson <[email protected]>
- Loading branch information
1 parent
e617496
commit 6c2dfdb
Showing
7 changed files
with
80 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ import type { | |
} from '../../protocol' | ||
import type { V1CredentialPreview } from '../../protocol/v1/V1CredentialPreview' | ||
import type { CredentialExchangeRecord } from '../../repository/CredentialExchangeRecord' | ||
import type { CredPropose } from '../models/CredPropose' | ||
import type { | ||
FormatServiceCredentialAttachmentFormats, | ||
CredentialFormatSpec, | ||
|
@@ -34,6 +33,7 @@ import { Lifecycle, scoped } from 'tsyringe' | |
import { AgentConfig } from '../../../../agent/AgentConfig' | ||
import { EventEmitter } from '../../../../agent/EventEmitter' | ||
import { AriesFrameworkError } from '../../../../error' | ||
import { JsonTransformer } from '../../../../utils/JsonTransformer' | ||
import { MessageValidator } from '../../../../utils/MessageValidator' | ||
import { uuid } from '../../../../utils/uuid' | ||
import { IndyHolderService, IndyIssuerService } from '../../../indy' | ||
|
@@ -47,6 +47,7 @@ import { V2CredentialPreview } from '../../protocol/v2/V2CredentialPreview' | |
import { CredentialMetadataKeys } from '../../repository/CredentialMetadataTypes' | ||
import { CredentialRepository } from '../../repository/CredentialRepository' | ||
import { CredentialFormatService } from '../CredentialFormatService' | ||
import { CredPropose } from '../models/CredPropose' | ||
|
||
@scoped(Lifecycle.ContainerScoped) | ||
export class IndyCredentialFormatService extends CredentialFormatService { | ||
|
@@ -80,7 +81,7 @@ export class IndyCredentialFormatService extends CredentialFormatService { | |
* @returns object containing associated attachment, formats and filtersAttach elements | ||
* | ||
*/ | ||
public createProposal(options: ProposeCredentialOptions): FormatServiceProposeAttachmentFormats { | ||
public async createProposal(options: ProposeCredentialOptions): Promise<FormatServiceProposeAttachmentFormats> { | ||
const formats: CredentialFormatSpec = { | ||
attachId: this.generateId(), | ||
format: 'hlindy/[email protected]', | ||
|
@@ -89,7 +90,19 @@ export class IndyCredentialFormatService extends CredentialFormatService { | |
throw new AriesFrameworkError('Missing payload in createProposal') | ||
} | ||
|
||
const attachment: Attachment = this.getFormatData(options.credentialFormats.indy?.payload, formats.attachId) | ||
// Use class instance instead of interface, otherwise this causes interoperability problems | ||
let proposal = new CredPropose(options.credentialFormats.indy?.payload) | ||
|
||
try { | ||
await MessageValidator.validate(proposal) | ||
} catch (error) { | ||
throw new AriesFrameworkError(`Invalid credPropose class instance: ${proposal} in Indy Format Service`) | ||
} | ||
|
||
proposal = JsonTransformer.toJSON(proposal) | ||
|
||
const attachment = this.getFormatData(proposal, formats.attachId) | ||
|
||
const { previewWithAttachments } = this.getCredentialLinkedAttachments(options) | ||
|
||
return { format: formats, attachment, preview: previewWithAttachments } | ||
|
@@ -99,7 +112,9 @@ export class IndyCredentialFormatService extends CredentialFormatService { | |
options: ServiceAcceptProposalOptions, | ||
credentialRecord: CredentialExchangeRecord | ||
): Promise<void> { | ||
const credPropose = options.proposalAttachment?.getDataAsJson<CredPropose>() | ||
let credPropose = options.proposalAttachment?.getDataAsJson<CredPropose>() | ||
credPropose = JsonTransformer.fromJSON(credPropose, CredPropose) | ||
|
||
if (!credPropose) { | ||
throw new AriesFrameworkError('Missing indy credential proposal data payload') | ||
} | ||
|
@@ -246,17 +261,20 @@ export class IndyCredentialFormatService extends CredentialFormatService { | |
}) | ||
} | ||
|
||
if (!options.credentialFormats.indy.attributes) { | ||
throw new AriesFrameworkError('Missing attributes from credential proposal') | ||
} | ||
|
||
if (options.credentialFormats.indy && options.credentialFormats.indy.linkedAttachments) { | ||
// there are linked attachments so transform into the attribute field of the CredentialPreview object for | ||
// this proposal | ||
if (options.credentialFormats.indy.attributes) { | ||
previewWithAttachments = CredentialUtils.createAndLinkAttachmentsToPreview( | ||
options.credentialFormats.indy.linkedAttachments, | ||
new V2CredentialPreview({ | ||
attributes: options.credentialFormats.indy.attributes, | ||
}) | ||
) | ||
} | ||
previewWithAttachments = CredentialUtils.createAndLinkAttachmentsToPreview( | ||
options.credentialFormats.indy.linkedAttachments, | ||
new V2CredentialPreview({ | ||
attributes: options.credentialFormats.indy.attributes, | ||
}) | ||
) | ||
|
||
attachments = options.credentialFormats.indy.linkedAttachments.map( | ||
(linkedAttachment) => linkedAttachment.attachment | ||
) | ||
|
@@ -364,7 +382,7 @@ export class IndyCredentialFormatService extends CredentialFormatService { | |
} | ||
|
||
const attachmentId = options.attachId ? options.attachId : formats.attachId | ||
const issueAttachment: Attachment = this.getFormatData(credential, attachmentId) | ||
const issueAttachment = this.getFormatData(credential, attachmentId) | ||
return { format: formats, attachment: issueAttachment } | ||
} | ||
/** | ||
|
@@ -502,11 +520,11 @@ export class IndyCredentialFormatService extends CredentialFormatService { | |
|
||
private areProposalAndOfferDefinitionIdEqual(proposalAttachment?: Attachment, offerAttachment?: Attachment) { | ||
const credOffer = offerAttachment?.getDataAsJson<CredOffer>() | ||
const credPropose = proposalAttachment?.getDataAsJson<CredPropose>() | ||
let credPropose = proposalAttachment?.getDataAsJson<CredPropose>() | ||
credPropose = JsonTransformer.fromJSON(credPropose, CredPropose) | ||
|
||
const proposalCredentialDefinitionId = credPropose?.credentialDefinitionId | ||
const offerCredentialDefinitionId = credOffer?.cred_def_id | ||
|
||
return proposalCredentialDefinitionId === offerCredentialDefinitionId | ||
} | ||
|
||
|
@@ -561,7 +579,8 @@ export class IndyCredentialFormatService extends CredentialFormatService { | |
proposeAttachment?: Attachment | ||
) { | ||
const indyCredentialRequest = requestAttachment?.getDataAsJson<CredReq>() | ||
const indyCredentialProposal = proposeAttachment?.getDataAsJson<CredPropose>() | ||
let indyCredentialProposal = proposeAttachment?.getDataAsJson<CredPropose>() | ||
indyCredentialProposal = JsonTransformer.fromJSON(indyCredentialProposal, CredPropose) | ||
|
||
const indyCredentialOffer = offerAttachment?.getDataAsJson<CredOffer>() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,27 +81,27 @@ describe('V2 Credential Architecture', () => { | |
expect(type).toEqual('IndyCredentialFormatService') | ||
}) | ||
|
||
test('propose credential format service returns correct format and filters~attach', () => { | ||
test('propose credential format service returns correct format and filters~attach', async () => { | ||
const version: CredentialProtocolVersion = CredentialProtocolVersion.V2 | ||
const service: CredentialService = api.getService(version) | ||
const formatService: CredentialFormatService = service.getFormatService(CredentialFormatType.Indy) | ||
const { format: formats, attachment: filtersAttach } = formatService.createProposal(proposal) | ||
const { format: formats, attachment: filtersAttach } = await formatService.createProposal(proposal) | ||
|
||
expect(formats.attachId.length).toBeGreaterThan(0) | ||
expect(formats.format).toEqual('hlindy/[email protected]') | ||
expect(filtersAttach).toBeTruthy() | ||
}) | ||
test('propose credential format service transforms and validates CredPropose payload correctly', () => { | ||
test('propose credential format service transforms and validates CredPropose payload correctly', async () => { | ||
const version: CredentialProtocolVersion = CredentialProtocolVersion.V2 | ||
const service: CredentialService = api.getService(version) | ||
const formatService: CredentialFormatService = service.getFormatService(CredentialFormatType.Indy) | ||
const { format: formats, attachment: filtersAttach } = formatService.createProposal(proposal) | ||
const { format: formats, attachment: filtersAttach } = await formatService.createProposal(proposal) | ||
|
||
expect(formats.attachId.length).toBeGreaterThan(0) | ||
expect(formats.format).toEqual('hlindy/[email protected]') | ||
expect(filtersAttach).toBeTruthy() | ||
}) | ||
test('propose credential format service creates message with multiple formats', () => { | ||
test('propose credential format service creates message with multiple formats', async () => { | ||
const version: CredentialProtocolVersion = CredentialProtocolVersion.V2 | ||
const service: CredentialService = api.getService(version) | ||
|
||
|
@@ -111,7 +111,7 @@ describe('V2 Credential Architecture', () => { | |
expect(formats.length).toBe(1) // for now will be added to with jsonld | ||
const messageBuilder: CredentialMessageBuilder = new CredentialMessageBuilder() | ||
|
||
const v2Proposal = messageBuilder.createProposal(formats, multiFormatProposal) | ||
const v2Proposal = await messageBuilder.createProposal(formats, multiFormatProposal) | ||
|
||
expect(v2Proposal.message.formats.length).toBe(1) | ||
expect(v2Proposal.message.formats[0].format).toEqual('hlindy/[email protected]') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters