Skip to content

Commit

Permalink
feat(proofs): present proof as nested protocol (#972)
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <[email protected]>
  • Loading branch information
genaris authored Aug 11, 2022
1 parent 0128c2a commit 52247d9
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/core/src/modules/proofs/ProofsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class ProofsModule {
config?: {
comment?: string
autoAcceptProof?: AutoAcceptProof
parentThreadId?: string
}
): Promise<ProofRecord> {
const connection = await this.connectionService.getById(connectionId)
Expand Down Expand Up @@ -396,6 +397,7 @@ export class ProofsModule {
})
presentationProblemReportMessage.setThread({
threadId: record.threadId,
parentThreadId: record.parentThreadId,
})
const outboundMessage = createOutboundMessage(connection, presentationProblemReportMessage)
await this.messageSender.sendMessage(outboundMessage)
Expand Down Expand Up @@ -445,6 +447,30 @@ export class ProofsModule {
return this.proofService.deleteById(proofId)
}

/**
* Retrieve a proof record by connection id and thread id
*
* @param connectionId The connection id
* @param threadId The thread id
* @throws {RecordNotFoundError} If no record is found
* @throws {RecordDuplicateError} If multiple records are found
* @returns The proof record
*/
public async getByThreadAndConnectionId(threadId: string, connectionId?: string): Promise<ProofRecord> {
return this.proofService.getByThreadAndConnectionId(threadId, connectionId)
}

/**
* Retrieve proof records by connection id and parent thread id
*
* @param connectionId The connection id
* @param parentThreadId The parent thread id
* @returns List containing all proof records matching the given query
*/
public async getByParentThreadAndConnectionId(parentThreadId: string, connectionId?: string): Promise<ProofRecord[]> {
return this.proofService.getByParentThreadAndConnectionId(parentThreadId, connectionId)
}

private registerHandlers(dispatcher: Dispatcher) {
dispatcher.registerHandler(
new ProposePresentationHandler(this.proofService, this.agentConfig, this.proofResponseCoordinator)
Expand Down Expand Up @@ -486,6 +512,7 @@ export type CreateProofRequestOptions = Partial<
export interface ProofRequestConfig {
comment?: string
autoAcceptProof?: AutoAcceptProof
parentThreadId?: string
}

export interface GetRequestedCredentialsConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PresentationPreview } from './PresentationPreview'
export interface ProposePresentationMessageOptions {
id?: string
comment?: string
parentThreadId?: string
presentationProposal: PresentationPreview
}

Expand All @@ -24,6 +25,11 @@ export class ProposePresentationMessage extends AgentMessage {
if (options) {
this.id = options.id ?? this.generateId()
this.comment = options.comment
if (options.parentThreadId) {
this.setThread({
parentThreadId: options.parentThreadId,
})
}
this.presentationProposal = options.presentationProposal
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ProofRequest } from '../models'
export interface RequestPresentationOptions {
id?: string
comment?: string
parentThreadId?: string
requestPresentationAttachments: Attachment[]
}

Expand All @@ -28,6 +29,11 @@ export class RequestPresentationMessage extends AgentMessage {
this.id = options.id ?? this.generateId()
this.comment = options.comment
this.requestPresentationAttachments = options.requestPresentationAttachments
if (options.parentThreadId) {
this.setThread({
parentThreadId: options.parentThreadId,
})
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/modules/proofs/repository/ProofRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ProofRecordProps {
state: ProofState
connectionId?: string
threadId: string
parentThreadId?: string
presentationId?: string
tags?: CustomProofTags
autoAcceptProof?: AutoAcceptProof
Expand All @@ -31,13 +32,15 @@ export interface ProofRecordProps {
export type CustomProofTags = TagsBase
export type DefaultProofTags = {
threadId: string
parentThreadId?: string
connectionId?: string
state: ProofState
}

export class ProofRecord extends BaseRecord<DefaultProofTags, CustomProofTags> {
public connectionId?: string
public threadId!: string
public parentThreadId?: string
public isVerified?: boolean
public presentationId?: string
public state!: ProofState
Expand Down Expand Up @@ -68,6 +71,7 @@ export class ProofRecord extends BaseRecord<DefaultProofTags, CustomProofTags> {
this.state = props.state
this.connectionId = props.connectionId
this.threadId = props.threadId
this.parentThreadId = props.parentThreadId
this.presentationId = props.presentationId
this.autoAcceptProof = props.autoAcceptProof
this._tags = props.tags ?? {}
Expand All @@ -79,6 +83,7 @@ export class ProofRecord extends BaseRecord<DefaultProofTags, CustomProofTags> {
return {
...this._tags,
threadId: this.threadId,
parentThreadId: this.parentThreadId,
connectionId: this.connectionId,
state: this.state,
}
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/modules/proofs/services/ProofService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class ProofService {
config?: {
comment?: string
autoAcceptProof?: AutoAcceptProof
parentThreadId?: string
}
): Promise<ProofProtocolMsgReturnType<ProposePresentationMessage>> {
// Assert
Expand All @@ -119,12 +120,14 @@ export class ProofService {
const proposalMessage = new ProposePresentationMessage({
comment: config?.comment,
presentationProposal,
parentThreadId: config?.parentThreadId,
})

// Create record
const proofRecord = new ProofRecord({
connectionId: connectionRecord.id,
threadId: proposalMessage.threadId,
parentThreadId: proposalMessage.thread?.parentThreadId,
state: ProofState.ProposalSent,
proposalMessage,
autoAcceptProof: config?.autoAcceptProof,
Expand Down Expand Up @@ -218,6 +221,7 @@ export class ProofService {
proofRecord = new ProofRecord({
connectionId: connection?.id,
threadId: proposalMessage.threadId,
parentThreadId: proposalMessage.thread?.parentThreadId,
proposalMessage,
state: ProofState.ProposalReceived,
})
Expand Down Expand Up @@ -294,6 +298,7 @@ export class ProofService {
config?: {
comment?: string
autoAcceptProof?: AutoAcceptProof
parentThreadId?: string
}
): Promise<ProofProtocolMsgReturnType<RequestPresentationMessage>> {
this.logger.debug(`Creating proof request`)
Expand All @@ -315,12 +320,14 @@ export class ProofService {
const requestPresentationMessage = new RequestPresentationMessage({
comment: config?.comment,
requestPresentationAttachments: [attachment],
parentThreadId: config?.parentThreadId,
})

// Create record
const proofRecord = new ProofRecord({
connectionId: connectionRecord?.id,
threadId: requestPresentationMessage.threadId,
parentThreadId: requestPresentationMessage.thread?.parentThreadId,
requestMessage: requestPresentationMessage,
state: ProofState.RequestSent,
autoAcceptProof: config?.autoAcceptProof,
Expand Down Expand Up @@ -383,6 +390,7 @@ export class ProofService {
proofRecord = new ProofRecord({
connectionId: connection?.id,
threadId: proofRequestMessage.threadId,
parentThreadId: proofRequestMessage.thread?.parentThreadId,
requestMessage: proofRequestMessage,
state: ProofState.RequestReceived,
})
Expand Down Expand Up @@ -976,6 +984,17 @@ export class ProofService {
return this.proofRepository.getSingleByQuery({ threadId, connectionId })
}

/**
* Retrieve proof records by connection id and parent thread id
*
* @param connectionId The connection id
* @param parentThreadId The parent thread id
* @returns List containing all proof records matching the given query
*/
public async getByParentThreadAndConnectionId(parentThreadId: string, connectionId?: string): Promise<ProofRecord[]> {
return this.proofRepository.findByQuery({ parentThreadId, connectionId })
}

public update(proofRecord: ProofRecord) {
return this.proofRepository.update(proofRecord)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export async function waitForProofRecord(
agent: Agent,
options: {
threadId?: string
parentThreadId?: string
state?: ProofState
previousState?: ProofState | null
timeoutMs?: number
Expand All @@ -152,11 +153,13 @@ export function waitForProofRecordSubject(
subject: ReplaySubject<ProofStateChangedEvent> | Observable<ProofStateChangedEvent>,
{
threadId,
parentThreadId,
state,
previousState,
timeoutMs = 10000,
}: {
threadId?: string
parentThreadId?: string
state?: ProofState
previousState?: ProofState | null
timeoutMs?: number
Expand All @@ -167,6 +170,7 @@ export function waitForProofRecordSubject(
observable.pipe(
filter((e) => previousState === undefined || e.payload.previousState === previousState),
filter((e) => threadId === undefined || e.payload.proofRecord.threadId === threadId),
filter((e) => parentThreadId === undefined || e.payload.proofRecord.parentThreadId === parentThreadId),
filter((e) => state === undefined || e.payload.proofRecord.state === state),
timeout(timeoutMs),
catchError(() => {
Expand Down
Loading

0 comments on commit 52247d9

Please sign in to comment.