Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(credentials): separate offer interfaces #819

Merged
merged 6 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import type {
AcceptRequestOptions,
NegotiateOfferOptions,
NegotiateProposalOptions,
OfferCredentialOptions,
RequestCredentialOptions,
} from './CredentialsModuleOptions'
import type { FormatServiceAcceptProposeCredentialFormats } from './formats/models/CredentialFormatServiceOptions'
import type { CredentialPreviewAttribute } from './models/CredentialPreviewAttribute'
import type { V1CredentialPreview } from './protocol/v1/V1CredentialPreview'
import type { ProposeCredentialMessageOptions } from './protocol/v1/messages'
Expand Down Expand Up @@ -46,9 +46,13 @@ export interface ServiceAcceptOfferOptions extends AcceptOfferOptions {
}
}

export interface ServiceOfferCredentialOptions extends OfferCredentialOptions {
export interface ServiceOfferCredentialOptions {
autoAcceptCredential?: AutoAcceptCredential
comment?: string
credentialRecordId?: string
connection?: ConnectionRecord
attachId?: string
credentialFormats: FormatServiceAcceptProposeCredentialFormats
}

export interface ServiceAcceptProposalOptions extends AcceptProposalOptions {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/modules/credentials/CredentialsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ServiceDecorator } from '../../decorators/service/ServiceDecorator'
import { AriesFrameworkError } from '../../error'
import { DidCommMessageRole } from '../../storage'
import { DidCommMessageRepository } from '../../storage/didcomm/DidCommMessageRepository'
import { getIndyDidFromVerficationMethod } from '../../utils/did'
import { getIndyDidFromVerificationMethod } from '../../utils/did'
import { ConnectionService } from '../connections/services'
import { DidResolverService, findVerificationMethodByKeyType } from '../dids'
import { MediationRecipientService } from '../routing'
Expand Down Expand Up @@ -252,7 +252,7 @@ export class CredentialsModule implements CredentialsModule {
'Invalid DidDocument: Missing verification method with type Ed25519VerificationKey2018 to use as indy holder did'
)
}
const indyDid = getIndyDidFromVerficationMethod(verificationMethod)
const indyDid = getIndyDidFromVerificationMethod(verificationMethod)
NB-MikeRichardson marked this conversation as resolved.
Show resolved Hide resolved

const requestOptions: RequestCredentialOptions = {
comment: options.comment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,12 @@ describe('CredentialService', () => {
offerOptions = {
comment: 'some comment',
connection,
connectionId: connection.id,
credentialFormats: {
indy: {
attributes: credentialPreview.attributes,
credentialDefinitionId: 'Th7MpTaRZVRYnPiabds81Y:3:CL:17:TAG',
},
},
protocolVersion: CredentialProtocolVersion.V1,
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { IndyIssuerService } from '../../indy/services/IndyIssuerService'
import { IndyLedgerService } from '../../ledger/services'
import { MediationRecipientService } from '../../routing/services/MediationRecipientService'
import { CredentialEventTypes } from '../CredentialEvents'
import { CredentialProtocolVersion } from '../CredentialProtocolVersion'
import { CredentialState } from '../CredentialState'
import { IndyCredentialFormatService } from '../formats/indy/IndyCredentialFormatService'
import { V1CredentialPreview } from '../protocol/v1/V1CredentialPreview'
Expand Down Expand Up @@ -136,19 +135,20 @@ describe('CredentialService', () => {
beforeEach(async () => {
offerOptions = {
comment: 'some comment',
connectionId: connection.id,
connection,
credentialFormats: {
indy: {
attributes: credentialPreview.attributes,
credentialDefinitionId: 'Th7MpTaRZVRYnPiabds81Y:3:CL:17:TAG',
},
},
protocolVersion: CredentialProtocolVersion.V1,
}
})

test(`creates credential record in ${CredentialState.OfferSent} state with offer, thread ID`, async () => {
const repositorySaveSpy = jest.spyOn(credentialRepository, 'save')

// when
await credentialService.createOffer(offerOptions)

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import type { Attachment } from '../../../../decorators/attachment/Attachment'
import type { Logger } from '../../../../logger'
import type {
NegotiateProposalOptions,
OfferCredentialOptions,
ProposeCredentialOptions,
RequestCredentialOptions,
} from '../../CredentialsModuleOptions'
import type {
ServiceAcceptCredentialOptions,
ServiceAcceptOfferOptions as ServiceOfferOptions,
ServiceAcceptProposalOptions,
ServiceAcceptRequestOptions,
ServiceOfferCredentialOptions,
Expand Down Expand Up @@ -315,7 +313,7 @@ export class IndyCredentialFormatService extends CredentialFormatService {
* @returns The created credential offer
*/
private async createCredentialOffer(
proposal: ServiceOfferOptions | NegotiateProposalOptions | OfferCredentialOptions
proposal: NegotiateProposalOptions | ServiceOfferCredentialOptions
): Promise<CredOffer> {
if (!proposal.credentialFormats?.indy?.credentialDefinitionId) {
throw new AriesFrameworkError('Missing Credential Definition id')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ export class V1CredentialService extends CredentialService {
attributes: preview.attributes,
},
},
protocolVersion: CredentialProtocolVersion.V1,
}

const { attachment: offersAttach } = await this.formatService.createOffer(options)
Expand Down Expand Up @@ -679,7 +678,6 @@ export class V1CredentialService extends CredentialService {
attributes: credentialPreview.attributes,
},
},
protocolVersion: CredentialProtocolVersion.V1,
}

const { attachment: offersAttach } = await this.formatService.createOffer(options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createOutboundMessage, createOutboundServiceMessage } from '../../../..
import { ServiceDecorator } from '../../../../../decorators/service/ServiceDecorator'
import { AriesFrameworkError } from '../../../../../error/AriesFrameworkError'
import { DidCommMessageRole } from '../../../../../storage'
import { getIndyDidFromVerficationMethod } from '../../../../../utils/did'
import { getIndyDidFromVerificationMethod } from '../../../../../utils/did'
import { findVerificationMethodByKeyType } from '../../../../dids'
import { V1OfferCredentialMessage, V1ProposeCredentialMessage } from '../messages'

Expand Down Expand Up @@ -82,7 +82,7 @@ export class V1OfferCredentialHandler implements Handler {
'Invalid DidDocument: Missing verification method with type Ed25519VerificationKey2018 to use as indy holder did'
)
}
const indyDid = getIndyDidFromVerficationMethod(verificationMethod)
const indyDid = getIndyDidFromVerificationMethod(verificationMethod)

const { message, credentialRecord } = await this.credentialService.createRequest(record, {
holderDid: indyDid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class CredentialMessageBuilder {
): Promise<{ credentialRecord: CredentialExchangeRecord; message: V2OfferCredentialMessage }> {
if (formatServices.length === 0) throw new AriesFrameworkError('no format services provided to createOffer')

const { autoAcceptCredential, comment, connectionId } = options
const { autoAcceptCredential, comment, connection } = options

const formats: CredentialFormatSpec[] = []
const offerAttachments: Attachment[] = []
Expand Down Expand Up @@ -244,7 +244,7 @@ export class CredentialMessageBuilder {
const message = new V2OfferCredentialMessage(messageProps)

const recordProps: CredentialExchangeRecordProps = {
connectionId,
connectionId: connection?.id,
threadId: message.threadId,
autoAcceptCredential,
state: CredentialState.OfferSent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ export class V2CredentialService extends CredentialService {
const options: ServiceOfferCredentialOptions = {
credentialFormats: proposal.credentialFormats,
comment: proposal.comment,
protocolVersion: credentialRecord.protocolVersion,
}
const message = await this.createOfferAsResponse(credentialRecord, options)

Expand Down Expand Up @@ -341,7 +340,7 @@ export class V2CredentialService extends CredentialService {
public async createOffer(
options: ServiceOfferCredentialOptions
): Promise<CredentialProtocolMsgReturnType<V2OfferCredentialMessage>> {
const connection = options.connectionId ? await this.connectionService.getById(options.connectionId) : undefined
const connection = options.connection
connection?.assertReady()

const formats = this.getFormats(options.credentialFormats)
Expand All @@ -355,7 +354,7 @@ export class V2CredentialService extends CredentialService {
formats,
options
)
credentialRecord.connectionId = options.connectionId
credentialRecord.connectionId = connection?.id

await this.credentialRepository.save(credentialRecord)
await this.emitEvent(credentialRecord)
Expand Down Expand Up @@ -391,7 +390,6 @@ export class V2CredentialService extends CredentialService {

options = {
credentialFormats: acceptProposalOptions.credentialFormats,
protocolVersion: CredentialProtocolVersion.V2,
comment: acceptProposalOptions.comment,
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createOutboundMessage, createOutboundServiceMessage } from '../../../..
import { ServiceDecorator } from '../../../../../decorators/service/ServiceDecorator'
import { AriesFrameworkError } from '../../../../../error/AriesFrameworkError'
import { DidCommMessageRole } from '../../../../../storage'
import { getIndyDidFromVerficationMethod } from '../../../../../utils/did'
import { getIndyDidFromVerificationMethod } from '../../../../../utils/did'
import { findVerificationMethodByKeyType } from '../../../../dids'
import { V2OfferCredentialMessage } from '../messages/V2OfferCredentialMessage'
import { V2ProposeCredentialMessage } from '../messages/V2ProposeCredentialMessage'
Expand Down Expand Up @@ -88,7 +88,7 @@ export class V2OfferCredentialHandler implements Handler {
'Invalid DidDocument: Missing verification method with type Ed25519VerificationKey2018 to use as indy holder did'
)
}
const indyDid = getIndyDidFromVerficationMethod(verificationMethod)
const indyDid = getIndyDidFromVerificationMethod(verificationMethod)
const { message, credentialRecord } = await this.credentialService.createRequest(record, {
holderDid: indyDid,
})
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/utils/__tests__/did.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
getIndyDidFromVerficationMethod,
getIndyDidFromVerificationMethod,
isAbbreviatedVerkey,
isDid,
isDidIdentifier,
Expand Down Expand Up @@ -176,12 +176,12 @@ describe('Utils | Did', () => {
})
})

describe('getIndyDidFromVerficationMethod()', () => {
expect(getIndyDidFromVerficationMethod(verificationMethod)).toBe(indyDid)
describe('getIndyDidFromVerificationMethod()', () => {
expect(getIndyDidFromVerificationMethod(verificationMethod)).toBe(indyDid)

test.each(invalidVerificationMethod)('throw error when invalid public key in verification method', (method) => {
expect(() => {
getIndyDidFromVerficationMethod(method)
getIndyDidFromVerificationMethod(method)
}).toThrow()
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function isDidIdentifier(identifier: string): boolean {
* @param verificationMethod
* @returns indy did
*/
export function getIndyDidFromVerficationMethod(verificationMethod: VerificationMethod): string {
export function getIndyDidFromVerificationMethod(verificationMethod: VerificationMethod): string {
if (!verificationMethod?.publicKeyBase58) {
throw new Error(`Unable to get publicKeyBase58 from verification method`)
}
Expand Down