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

feat: connection type #994

Merged
merged 22 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
54 changes: 54 additions & 0 deletions packages/core/src/modules/connections/ConnectionsModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DependencyManager } from '../../plugins'
import type { Key } from '../dids'
import type { OutOfBandRecord } from '../oob/repository'
import type { ConnectionType } from './models'
import type { ConnectionRecord } from './repository/ConnectionRecord'
import type { Routing } from './services'

Expand Down Expand Up @@ -197,6 +198,59 @@ export class ConnectionsModule {
return this.connectionService.getAll()
}

/**
* Allows for the addition of connectionType to the record.
* Either updates or creates an array of string conection types
* @param connectionId
* @param type
* @throws {RecordNotFoundError} If no record is found
*/
public async addConnectionType(connectionId: string, type: ConnectionType | string) {
const record = await this.getById(connectionId)

const tags = (record.getTag('connectionType') as string[]) || ([] as string[])
record.setTag('connectionType', [type, ...tags])
await this.connectionService.update(record)
}
/**
* Removes the given tag from the given record found by connectionId, if the tag exists otherwise does nothing
* @param connectionId
* @param type
* @throws {RecordNotFoundError} If no record is found
*/
public async removeConnectionType(connectionId: string, type: ConnectionType | string) {
const record = await this.getById(connectionId)

const tags = (record.getTag('connectionType') as string[]) || ([] as string[])

const newTags = tags.filter((value: string) => {
if (value != type) return value
})
record.setTag('connectionType', [...newTags])

await this.connectionService.update(record)
}
/**
* Gets the known connection types for the record matching the given connectionId
* @param connectionId
* @returns An array of known connection types or null if none exist
* @throws {RecordNotFoundError} If no record is found
*/
public async getConnectionTypes(connectionId: string) {
const record = await this.getById(connectionId)
const tags = record.getTag('connectionType') as string[]
return tags || null
}

/**
*
* @param connectionTypes An array of connection types to query for a match for
* @returns a promise of ab array of connection records
*/
public async findAllByConnectionType(connectionTypes: [ConnectionType | string]) {
return this.connectionService.findAllByConnectionType(connectionTypes)
}

/**
* Retrieve a connection record by id
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum ConnectionType {
Mediator = 'mediator',
}
1 change: 1 addition & 0 deletions packages/core/src/modules/connections/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './DidExchangeState'
export * from './DidExchangeRole'
export * from './HandshakeProtocol'
export * from './did'
export * from './ConnectionType'
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TagsBase } from '../../../storage/BaseRecord'
import type { HandshakeProtocol } from '../models'
import type { ConnectionType } from '../models/ConnectionType'
import type { ConnectionMetadata } from './ConnectionMetadataTypes'

import { AriesFrameworkError } from '../../../error'
Expand Down Expand Up @@ -37,6 +38,7 @@ export type DefaultConnectionTags = {
theirDid?: string
outOfBandId?: string
invitationDid?: string
connectionType?: [ConnectionType | string]
}

export class ConnectionRecord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { OutOfBandDidCommService } from '../../oob/domain/OutOfBandDidCommS
import type { OutOfBandRecord } from '../../oob/repository'
import type { ConnectionStateChangedEvent } from '../ConnectionEvents'
import type { ConnectionProblemReportMessage } from '../messages'
import type { ConnectionType } from '../models'
import type { ConnectionRecordProps } from '../repository/ConnectionRecord'

import { firstValueFrom, ReplaySubject } from 'rxjs'
Expand Down Expand Up @@ -583,6 +584,10 @@ export class ConnectionService {
return this.connectionRepository.findByQuery({ outOfBandId })
}

public async findAllByConnectionType(connectionType: [ConnectionType | string]) {
return this.connectionRepository.findByQuery({ connectionType })
}

public async findByInvitationDid(invitationDid: string) {
return this.connectionRepository.findByQuery({ invitationDid })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { KeyType } from '../../../crypto'
import { AriesFrameworkError } from '../../../error'
import { injectable } from '../../../plugins'
import { JsonTransformer } from '../../../utils'
import { ConnectionType } from '../../connections/models/ConnectionType'
import { ConnectionMetadataKeys } from '../../connections/repository/ConnectionMetadataTypes'
import { ConnectionService } from '../../connections/services/ConnectionService'
import { Key } from '../../dids'
Expand Down Expand Up @@ -90,6 +91,9 @@ export class MediationRecipientService {
role: MediationRole.Recipient,
connectionId: connection.id,
})
connection.setTag('connectionType', [ConnectionType.Mediator])
await this.connectionService.update(connection)

await this.mediationRepository.save(mediationRecord)
this.emitStateChangedEvent(mediationRecord, null)

Expand Down
11 changes: 10 additions & 1 deletion packages/core/tests/oob-mediation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SubjectInboundTransport } from '../../../tests/transport/SubjectInbound
import { SubjectOutboundTransport } from '../../../tests/transport/SubjectOutboundTransport'
import { Agent } from '../src/agent/Agent'
import { DidExchangeState, HandshakeProtocol } from '../src/modules/connections'
import { ConnectionType } from '../src/modules/connections/models/ConnectionType'
import { MediationState, MediatorPickupStrategy } from '../src/modules/routing'

import { getBaseConfig, waitForBasicMessage } from './helpers'
Expand Down Expand Up @@ -90,8 +91,16 @@ describe('out of band with mediation', () => {
mediatorAliceConnection = await mediatorAgent.connections.returnWhenIsConnected(mediatorAliceConnection!.id)
expect(mediatorAliceConnection.state).toBe(DidExchangeState.Completed)

// ========== Set meadiation between Alice and Mediator agents ==========
// ========== Set mediation between Alice and Mediator agents ==========
const mediationRecord = await aliceAgent.mediationRecipient.requestAndAwaitGrant(aliceMediatorConnection)
const connectonTypes = await aliceAgent.connections.getConnectionTypes(mediationRecord.connectionId)
expect(connectonTypes).toContain(ConnectionType.Mediator)
await aliceAgent.connections.addConnectionType(mediationRecord.connectionId, 'test')
expect(await aliceAgent.connections.getConnectionTypes(mediationRecord.connectionId)).toContain('test')
await aliceAgent.connections.removeConnectionType(mediationRecord.connectionId, 'test')
expect(await aliceAgent.connections.getConnectionTypes(mediationRecord.connectionId)).toEqual([
ConnectionType.Mediator,
])
expect(mediationRecord.state).toBe(MediationState.Granted)

await aliceAgent.mediationRecipient.setDefaultMediator(mediationRecord)
Expand Down