Skip to content

Commit

Permalink
refactor(proofs)!: createRequest for connectionless proof request (#1034
Browse files Browse the repository at this point in the history
)

Signed-off-by: Ariel Gentile <[email protected]>
  • Loading branch information
genaris authored Sep 22, 2022
1 parent 26bb9c9 commit 5a286b7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 59 deletions.
32 changes: 6 additions & 26 deletions packages/core/src/modules/proofs/ProofsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ProofService } from './ProofService'
import type {
AcceptPresentationOptions,
AcceptProposalOptions,
OutOfBandRequestOptions,
CreateProofRequestOptions,
ProposeProofOptions,
RequestProofOptions,
ServiceMap,
Expand All @@ -15,7 +15,6 @@ import type {
GetRequestedCredentialsForProofRequest,
} from './models/ModuleOptions'
import type {
CreateOutOfBandRequestOptions,
CreatePresentationOptions,
CreateProposalOptions,
CreateRequestOptions,
Expand Down Expand Up @@ -60,7 +59,7 @@ export interface ProofsApi<PFs extends ProofFormat[], PSs extends ProofService<P
declineRequest(proofRecordId: string): Promise<ProofRecord>

// out of band
createOutOfBandRequest(options: OutOfBandRequestOptions<PFs, PSs>): Promise<{
createRequest(options: CreateProofRequestOptions<PFs, PSs>): Promise<{
message: AgentMessage
proofRecord: ProofRecord
}>
Expand Down Expand Up @@ -333,39 +332,20 @@ export class ProofsApi<
}
}

public async createOutOfBandRequest(options: OutOfBandRequestOptions<PFs, PSs>): Promise<{
public async createRequest(options: CreateProofRequestOptions<PFs, PSs>): Promise<{
message: AgentMessage
proofRecord: ProofRecord
}> {
const service = this.getService(options.protocolVersion)

const createProofRequest: CreateOutOfBandRequestOptions<PFs> = {
const createProofRequest: CreateRequestOptions<PFs> = {
proofFormats: options.proofFormats,
autoAcceptProof: options.autoAcceptProof,
comment: options.comment,
parentThreadId: options.parentThreadId,
}

const { message, proofRecord } = await service.createRequest(this.agentContext, createProofRequest)

// Create and set ~service decorator

const routing = await this.routingService.getRouting(this.agentContext)
message.service = new ServiceDecorator({
serviceEndpoint: routing.endpoints[0],
recipientKeys: [routing.recipientKey.publicKeyBase58],
routingKeys: routing.routingKeys.map((key) => key.publicKeyBase58),
})
// Save ~service decorator to record (to remember our verkey)

await service.saveOrUpdatePresentationMessage(this.agentContext, {
message,
proofRecord: proofRecord,
role: DidCommMessageRole.Sender,
})

await service.update(this.agentContext, proofRecord)

return { proofRecord, message }
return await service.createRequest(this.agentContext, createProofRequest)
}

public async declineRequest(proofRecordId: string): Promise<ProofRecord> {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/modules/proofs/ProofsApiOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ export interface RequestProofOptions<
parentThreadId?: string
}

export interface OutOfBandRequestOptions<
export interface CreateProofRequestOptions<
PFs extends ProofFormat[] = ProofFormat[],
PSs extends ProofService[] = ProofService[]
> {
protocolVersion: ProtocolVersionType<PSs>
proofFormats: ProofFormatPayload<PFs, 'createOutOfBandRequest'>
proofFormats: ProofFormatPayload<PFs, 'createRequest'>
comment?: string
autoAcceptProof?: AutoAcceptProof
parentThreadId?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ export interface CreateRequestAsResponseOptions<PFs extends ProofFormat[]> exten
proofFormats: ProofFormatPayload<PFs, 'createRequestAsResponse'>
}

// ----- Out Of Band Proof ----- //
export interface CreateOutOfBandRequestOptions<PFs extends ProofFormat[]> extends BaseOptions {
proofFormats: ProofFormatPayload<PFs, 'createOutOfBandRequest'>
}

export interface CreateRequestOptions<PFs extends ProofFormat[]> extends BaseOptions {
connectionRecord?: ConnectionRecord
proofFormats: ProofFormatPayload<PFs, 'createRequest'>
Expand Down
41 changes: 28 additions & 13 deletions packages/core/tests/v1-connectionless-proofs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SubjectMessage } from '../../../tests/transport/SubjectInboundTransport'
import type { ProofStateChangedEvent } from '../src/modules/proofs'
import type { OutOfBandRequestOptions } from '../src/modules/proofs/ProofsApiOptions'
import type { CreateProofRequestOptions } from '../src/modules/proofs/ProofsApiOptions'
import type { IndyProofFormat } from '../src/modules/proofs/formats/indy/IndyProofFormat'
import type { V1ProofService } from '../src/modules/proofs/protocol/v1'

Expand Down Expand Up @@ -81,7 +81,7 @@ describe('Present Proof', () => {
}),
}

const outOfBandRequestOptions: OutOfBandRequestOptions<[IndyProofFormat], [V1ProofService]> = {
const outOfBandRequestOptions: CreateProofRequestOptions<[IndyProofFormat], [V1ProofService]> = {
protocolVersion: ProofProtocolVersion.V1,
proofFormats: {
indy: {
Expand All @@ -99,11 +99,14 @@ describe('Present Proof', () => {
})

// eslint-disable-next-line prefer-const
let { proofRecord: faberProofRecord, message } = await faberAgent.proofs.createOutOfBandRequest(
outOfBandRequestOptions
)
let { proofRecord: faberProofRecord, message } = await faberAgent.proofs.createRequest(outOfBandRequestOptions)

await aliceAgent.receiveMessage(message.toJSON())
const { message: requestMessage } = await faberAgent.oob.createLegacyConnectionlessInvitation({
recordId: faberProofRecord.id,
message,
domain: 'https://a-domain.com',
})
await aliceAgent.receiveMessage(requestMessage.toJSON())

testLogger.test('Alice waits for presentation request from Faber')
let aliceProofRecord = await aliceProofRecordPromise
Expand Down Expand Up @@ -179,7 +182,7 @@ describe('Present Proof', () => {
}),
}

const outOfBandRequestOptions: OutOfBandRequestOptions<[IndyProofFormat], [V1ProofService]> = {
const outOfBandRequestOptions: CreateProofRequestOptions<[IndyProofFormat], [V1ProofService]> = {
protocolVersion: ProofProtocolVersion.V1,
proofFormats: {
indy: {
Expand All @@ -202,9 +205,15 @@ describe('Present Proof', () => {
})

// eslint-disable-next-line prefer-const
let { message } = await faberAgent.proofs.createOutOfBandRequest(outOfBandRequestOptions)
let { message, proofRecord: faberProofRecord } = await faberAgent.proofs.createRequest(outOfBandRequestOptions)

const { message: requestMessage } = await faberAgent.oob.createLegacyConnectionlessInvitation({
recordId: faberProofRecord.id,
message,
domain: 'https://a-domain.com',
})

await aliceAgent.receiveMessage(message.toJSON())
await aliceAgent.receiveMessage(requestMessage.toJSON())

await aliceProofRecordPromise

Expand Down Expand Up @@ -339,7 +348,7 @@ describe('Present Proof', () => {
}),
}

const outOfBandRequestOptions: OutOfBandRequestOptions<[IndyProofFormat], [V1ProofService]> = {
const outOfBandRequestOptions: CreateProofRequestOptions<[IndyProofFormat], [V1ProofService]> = {
protocolVersion: ProofProtocolVersion.V1,
proofFormats: {
indy: {
Expand All @@ -362,22 +371,28 @@ describe('Present Proof', () => {
})

// eslint-disable-next-line prefer-const
let { message } = await faberAgent.proofs.createOutOfBandRequest(outOfBandRequestOptions)
let { message, proofRecord: faberProofRecord } = await faberAgent.proofs.createRequest(outOfBandRequestOptions)

const { message: requestMessage } = await faberAgent.oob.createLegacyConnectionlessInvitation({
recordId: faberProofRecord.id,
message,
domain: 'https://a-domain.com',
})

const mediationRecord = await faberAgent.mediationRecipient.findDefaultMediator()
if (!mediationRecord) {
throw new Error('Faber agent has no default mediator')
}

expect(message).toMatchObject({
expect(requestMessage).toMatchObject({
service: {
recipientKeys: [expect.any(String)],
routingKeys: mediationRecord.routingKeys,
serviceEndpoint: mediationRecord.endpoint,
},
})

await aliceAgent.receiveMessage(message.toJSON())
await aliceAgent.receiveMessage(requestMessage.toJSON())

await aliceProofRecordPromise

Expand Down
41 changes: 28 additions & 13 deletions packages/core/tests/v2-connectionless-proofs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SubjectMessage } from '../../../tests/transport/SubjectInboundTransport'
import type { ProofStateChangedEvent } from '../src/modules/proofs'
import type { OutOfBandRequestOptions } from '../src/modules/proofs/ProofsApiOptions'
import type { CreateProofRequestOptions } from '../src/modules/proofs/ProofsApiOptions'
import type { IndyProofFormat } from '../src/modules/proofs/formats/indy/IndyProofFormat'
import type { V2ProofService } from '../src/modules/proofs/protocol/v2'

Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Present Proof', () => {
}),
}

const outOfBandRequestOptions: OutOfBandRequestOptions<[IndyProofFormat], [V2ProofService]> = {
const outOfBandRequestOptions: CreateProofRequestOptions<[IndyProofFormat], [V2ProofService]> = {
protocolVersion: ProofProtocolVersion.V2,
proofFormats: {
indy: {
Expand All @@ -97,11 +97,15 @@ describe('Present Proof', () => {
})

// eslint-disable-next-line prefer-const
let { proofRecord: faberProofRecord, message } = await faberAgent.proofs.createOutOfBandRequest(
outOfBandRequestOptions
)
let { proofRecord: faberProofRecord, message } = await faberAgent.proofs.createRequest(outOfBandRequestOptions)

const { message: requestMessage } = await faberAgent.oob.createLegacyConnectionlessInvitation({
recordId: faberProofRecord.id,
message,
domain: 'https://a-domain.com',
})

await aliceAgent.receiveMessage(message.toJSON())
await aliceAgent.receiveMessage(requestMessage.toJSON())

testLogger.test('Alice waits for presentation request from Faber')
let aliceProofRecord = await aliceProofRecordPromise
Expand Down Expand Up @@ -178,7 +182,7 @@ describe('Present Proof', () => {
}),
}

const outOfBandRequestOptions: OutOfBandRequestOptions<[IndyProofFormat], [V2ProofService]> = {
const outOfBandRequestOptions: CreateProofRequestOptions<[IndyProofFormat], [V2ProofService]> = {
protocolVersion: ProofProtocolVersion.V2,
proofFormats: {
indy: {
Expand All @@ -201,9 +205,14 @@ describe('Present Proof', () => {
})

// eslint-disable-next-line prefer-const
let { message } = await faberAgent.proofs.createOutOfBandRequest(outOfBandRequestOptions)
let { message, proofRecord: faberProofRecord } = await faberAgent.proofs.createRequest(outOfBandRequestOptions)

await aliceAgent.receiveMessage(message.toJSON())
const { message: requestMessage } = await faberAgent.oob.createLegacyConnectionlessInvitation({
recordId: faberProofRecord.id,
message,
domain: 'https://a-domain.com',
})
await aliceAgent.receiveMessage(requestMessage.toJSON())

await aliceProofRecordPromise

Expand Down Expand Up @@ -339,7 +348,7 @@ describe('Present Proof', () => {
}),
}

const outOfBandRequestOptions: OutOfBandRequestOptions<[IndyProofFormat], [V2ProofService]> = {
const outOfBandRequestOptions: CreateProofRequestOptions<[IndyProofFormat], [V2ProofService]> = {
protocolVersion: ProofProtocolVersion.V2,
proofFormats: {
indy: {
Expand All @@ -362,22 +371,28 @@ describe('Present Proof', () => {
})

// eslint-disable-next-line prefer-const
let { message } = await faberAgent.proofs.createOutOfBandRequest(outOfBandRequestOptions)
let { message, proofRecord: faberProofRecord } = await faberAgent.proofs.createRequest(outOfBandRequestOptions)

const { message: requestMessage } = await faberAgent.oob.createLegacyConnectionlessInvitation({
recordId: faberProofRecord.id,
message,
domain: 'https://a-domain.com',
})

const mediationRecord = await faberAgent.mediationRecipient.findDefaultMediator()
if (!mediationRecord) {
throw new Error('Faber agent has no default mediator')
}

expect(message).toMatchObject({
expect(requestMessage).toMatchObject({
service: {
recipientKeys: [expect.any(String)],
routingKeys: mediationRecord.routingKeys,
serviceEndpoint: mediationRecord.endpoint,
},
})

await aliceAgent.receiveMessage(message.toJSON())
await aliceAgent.receiveMessage(requestMessage.toJSON())

await aliceProofRecordPromise

Expand Down

0 comments on commit 5a286b7

Please sign in to comment.