diff --git a/packages/core/src/agent/MessageReceiver.ts b/packages/core/src/agent/MessageReceiver.ts index a9e07cbf95..cf44968c4e 100644 --- a/packages/core/src/agent/MessageReceiver.ts +++ b/packages/core/src/agent/MessageReceiver.ts @@ -8,7 +8,8 @@ import type { TransportSession } from './TransportService' import { Lifecycle, scoped } from 'tsyringe' import { AriesFrameworkError } from '../error' -import { ConnectionService } from '../modules/connections/services/ConnectionService' +import { ConnectionRepository } from '../modules/connections' +import { DidRepository } from '../modules/dids/repository/DidRepository' import { ProblemReportError, ProblemReportMessage, ProblemReportReason } from '../modules/problem-reports' import { JsonTransformer } from '../utils/JsonTransformer' import { MessageValidator } from '../utils/MessageValidator' @@ -28,9 +29,10 @@ export class MessageReceiver { private envelopeService: EnvelopeService private transportService: TransportService private messageSender: MessageSender - private connectionService: ConnectionService private dispatcher: Dispatcher private logger: Logger + private didRepository: DidRepository + private connectionRepository: ConnectionRepository public readonly inboundTransports: InboundTransport[] = [] public constructor( @@ -38,15 +40,17 @@ export class MessageReceiver { envelopeService: EnvelopeService, transportService: TransportService, messageSender: MessageSender, - connectionService: ConnectionService, - dispatcher: Dispatcher + connectionRepository: ConnectionRepository, + dispatcher: Dispatcher, + didRepository: DidRepository ) { this.config = config this.envelopeService = envelopeService this.transportService = transportService this.messageSender = messageSender - this.connectionService = connectionService + this.connectionRepository = connectionRepository this.dispatcher = dispatcher + this.didRepository = didRepository this.logger = this.config.logger } @@ -77,21 +81,10 @@ export class MessageReceiver { } private async receiveEncryptedMessage(encryptedMessage: EncryptedMessage, session?: TransportSession) { - const { plaintextMessage, senderKey, recipientKey } = await this.decryptMessage(encryptedMessage) + const decryptedMessage = await this.decryptMessage(encryptedMessage) + const { plaintextMessage, senderKey, recipientKey } = decryptedMessage - let connection: ConnectionRecord | null = null - - // Only fetch connection if recipientKey and senderKey are present (AuthCrypt) - if (senderKey && recipientKey) { - connection = await this.connectionService.findByVerkey(recipientKey) - - // Throw error if the recipient key (ourKey) does not match the key of the connection record - if (connection && connection.theirKey !== null && connection.theirKey !== senderKey) { - throw new AriesFrameworkError( - `Inbound message senderKey '${senderKey}' is different from connection.theirKey '${connection.theirKey}'` - ) - } - } + const connection = await this.findConnectionByMessageKeys(decryptedMessage) this.logger.info( `Received message with type '${plaintextMessage['@type']}' from connection ${connection?.id} (${connection?.theirLabel})`, @@ -171,6 +164,55 @@ export class MessageReceiver { return message } + private async findConnectionByMessageKeys({ + recipientKey, + senderKey, + }: DecryptedMessageContext): Promise { + // We only fetch connections that are sent in AuthCrypt mode + if (!recipientKey || !senderKey) return null + + let connection: ConnectionRecord | null = null + + // Try to find the did records that holds the sender and recipient keys + const ourDidRecord = await this.didRepository.findByVerkey(recipientKey) + + // If both our did record and their did record is available we can find a matching did record + if (ourDidRecord) { + const theirDidRecord = await this.didRepository.findByVerkey(senderKey) + + if (theirDidRecord) { + connection = await this.connectionRepository.findSingleByQuery({ + did: ourDidRecord.id, + theirDid: theirDidRecord.id, + }) + } else { + connection = await this.connectionRepository.findSingleByQuery({ + did: ourDidRecord.id, + }) + + // If theirDidRecord was not found, and connection.theirDid is set, it means the sender is not authenticated + // to send messages to use + if (connection && connection.theirDid) { + throw new AriesFrameworkError(`Inbound message senderKey '${senderKey}' is different from connection did`) + } + } + } + + // If no connection was found, we search in the connection record, where legacy did documents are stored + if (!connection) { + connection = await this.connectionRepository.findByVerkey(recipientKey) + + // Throw error if the recipient key (ourKey) does not match the key of the connection record + if (connection && connection.theirKey !== null && connection.theirKey !== senderKey) { + throw new AriesFrameworkError( + `Inbound message senderKey '${senderKey}' is different from connection.theirKey '${connection.theirKey}'` + ) + } + } + + return connection + } + /** * Transform an plaintext DIDComm message into it's corresponding message class. Will look at all message types in the registered handlers. * diff --git a/packages/core/src/modules/connections/__tests__/ConnectionService.test.ts b/packages/core/src/modules/connections/__tests__/ConnectionService.test.ts index 1f09bac199..ed83591830 100644 --- a/packages/core/src/modules/connections/__tests__/ConnectionService.test.ts +++ b/packages/core/src/modules/connections/__tests__/ConnectionService.test.ts @@ -342,7 +342,7 @@ describe('ConnectionService', () => { verkey: 'my-key', role: ConnectionRole.Inviter, }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(connectionRecord)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(connectionRecord)) const theirDid = 'their-did' const theirVerkey = 'their-verkey' @@ -395,7 +395,7 @@ describe('ConnectionService', () => { senderVerkey: 'sender-verkey', }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(null)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(null)) return expect(connectionService.processRequest(messageContext)).rejects.toThrowError( 'Unable to process connection request: connection for verkey test-verkey not found' ) @@ -411,7 +411,7 @@ describe('ConnectionService', () => { role: ConnectionRole.Inviter, multiUseInvitation: true, }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(connectionRecord)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(connectionRecord)) const theirDid = 'their-did' const theirVerkey = 'their-verkey' @@ -458,7 +458,7 @@ describe('ConnectionService', () => { it(`throws an error when connection role is ${ConnectionRole.Invitee} and not ${ConnectionRole.Inviter}`, async () => { expect.assertions(1) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue( + mockFunction(connectionRepository.findByVerkey).mockReturnValue( Promise.resolve(getMockConnection({ role: ConnectionRole.Invitee })) ) @@ -482,7 +482,7 @@ describe('ConnectionService', () => { verkey: recipientVerkey, }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(connection)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(connection)) const connectionRequest = new ConnectionRequestMessage({ did: 'did', @@ -512,7 +512,7 @@ describe('ConnectionService', () => { role: ConnectionRole.Inviter, multiUseInvitation: true, }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(connectionRecord)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(connectionRecord)) const theirDidDoc = new DidDoc({ id: 'their-did', @@ -618,7 +618,7 @@ describe('ConnectionService', () => { serviceEndpoint: 'test', }), }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(connectionRecord)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(connectionRecord)) const otherPartyConnection = new Connection({ did: theirDid, @@ -667,7 +667,7 @@ describe('ConnectionService', () => { recipientVerkey: 'recipientVerkey', }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue( + mockFunction(connectionRepository.findByVerkey).mockReturnValue( Promise.resolve( getMockConnection({ role: ConnectionRole.Inviter, @@ -692,7 +692,7 @@ describe('ConnectionService', () => { role: ConnectionRole.Invitee, state: ConnectionState.Requested, }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(connectionRecord)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(connectionRecord)) const otherPartyConnection = new Connection({ did: theirDid, @@ -749,7 +749,7 @@ describe('ConnectionService', () => { recipientVerkey: 'test-verkey', senderVerkey: 'sender-verkey', }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(null)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(null)) return expect(connectionService.processResponse(messageContext)).rejects.toThrowError( 'Unable to process connection response: connection for verkey test-verkey not found' @@ -774,7 +774,7 @@ describe('ConnectionService', () => { theirDid: undefined, theirDidDoc: undefined, }) - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(connectionRecord)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(connectionRecord)) const otherPartyConnection = new Connection({ did: theirDid, @@ -1071,11 +1071,11 @@ describe('ConnectionService', () => { expect(result).toBe(expected) }) - it('getById should return value from connectionRepository.getSingleByQuery', async () => { + it('getByThreadId should return value from connectionRepository.getSingleByQuery', async () => { const expected = getMockConnection() - mockFunction(connectionRepository.getSingleByQuery).mockReturnValue(Promise.resolve(expected)) + mockFunction(connectionRepository.getByThreadId).mockReturnValue(Promise.resolve(expected)) const result = await connectionService.getByThreadId('threadId') - expect(connectionRepository.getSingleByQuery).toBeCalledWith({ threadId: 'threadId' }) + expect(connectionRepository.getByThreadId).toBeCalledWith('threadId') expect(result).toBe(expected) }) @@ -1091,18 +1091,18 @@ describe('ConnectionService', () => { it('findByVerkey should return value from connectionRepository.findSingleByQuery', async () => { const expected = getMockConnection() - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(expected)) + mockFunction(connectionRepository.findByVerkey).mockReturnValue(Promise.resolve(expected)) const result = await connectionService.findByVerkey('verkey') - expect(connectionRepository.findSingleByQuery).toBeCalledWith({ verkey: 'verkey' }) + expect(connectionRepository.findByVerkey).toBeCalledWith('verkey') expect(result).toBe(expected) }) it('findByTheirKey should return value from connectionRepository.findSingleByQuery', async () => { const expected = getMockConnection() - mockFunction(connectionRepository.findSingleByQuery).mockReturnValue(Promise.resolve(expected)) + mockFunction(connectionRepository.findByTheirKey).mockReturnValue(Promise.resolve(expected)) const result = await connectionService.findByTheirKey('theirKey') - expect(connectionRepository.findSingleByQuery).toBeCalledWith({ theirKey: 'theirKey' }) + expect(connectionRepository.findByTheirKey).toBeCalledWith('theirKey') expect(result).toBe(expected) }) diff --git a/packages/core/src/modules/connections/repository/ConnectionRecord.ts b/packages/core/src/modules/connections/repository/ConnectionRecord.ts index 4f0ed7079b..1f79123db6 100644 --- a/packages/core/src/modules/connections/repository/ConnectionRecord.ts +++ b/packages/core/src/modules/connections/repository/ConnectionRecord.ts @@ -41,6 +41,8 @@ export type DefaultConnectionTags = { verkey?: string theirKey?: string mediatorId?: string + did: string + theirDid?: string } export class ConnectionRecord @@ -112,6 +114,8 @@ export class ConnectionRecord verkey: this.verkey, theirKey: this.theirKey || undefined, mediatorId: this.mediatorId, + did: this.did, + theirDid: this.theirDid, } } diff --git a/packages/core/src/modules/connections/repository/ConnectionRepository.ts b/packages/core/src/modules/connections/repository/ConnectionRepository.ts index 4a4c583584..051d891db2 100644 --- a/packages/core/src/modules/connections/repository/ConnectionRepository.ts +++ b/packages/core/src/modules/connections/repository/ConnectionRepository.ts @@ -11,4 +11,33 @@ export class ConnectionRepository extends Repository { public constructor(@inject(InjectionSymbols.StorageService) storageService: StorageService) { super(ConnectionRecord, storageService) } + + public async findByDids({ ourDid, theirDid }: { ourDid: string; theirDid: string }) { + return this.findSingleByQuery({ + did: ourDid, + theirDid, + }) + } + + public findByVerkey(verkey: string): Promise { + return this.findSingleByQuery({ + verkey, + }) + } + + public findByTheirKey(verkey: string): Promise { + return this.findSingleByQuery({ + theirKey: verkey, + }) + } + + public findByInvitationKey(key: string): Promise { + return this.findSingleByQuery({ + invitationKey: key, + }) + } + + public getByThreadId(threadId: string): Promise { + return this.getSingleByQuery({ threadId }) + } } diff --git a/packages/core/src/modules/connections/services/ConnectionService.ts b/packages/core/src/modules/connections/services/ConnectionService.ts index a6dc2ea7a3..f4d95201e3 100644 --- a/packages/core/src/modules/connections/services/ConnectionService.ts +++ b/packages/core/src/modules/connections/services/ConnectionService.ts @@ -603,9 +603,7 @@ export class ConnectionService { * @throws {RecordDuplicateError} if multiple connections are found for the given verkey */ public findByVerkey(verkey: string): Promise { - return this.connectionRepository.findSingleByQuery({ - verkey, - }) + return this.connectionRepository.findByVerkey(verkey) } /** @@ -616,9 +614,7 @@ export class ConnectionService { * @throws {RecordDuplicateError} if multiple connections are found for the given verkey */ public findByTheirKey(verkey: string): Promise { - return this.connectionRepository.findSingleByQuery({ - theirKey: verkey, - }) + return this.connectionRepository.findByTheirKey(verkey) } /** @@ -629,9 +625,7 @@ export class ConnectionService { * @throws {RecordDuplicateError} if multiple connections are found for the given verkey */ public findByInvitationKey(key: string): Promise { - return this.connectionRepository.findSingleByQuery({ - invitationKey: key, - }) + return this.connectionRepository.findByInvitationKey(key) } /** @@ -643,7 +637,7 @@ export class ConnectionService { * @returns The connection record */ public getByThreadId(threadId: string): Promise { - return this.connectionRepository.getSingleByQuery({ threadId }) + return this.connectionRepository.getByThreadId(threadId) } private async createConnection(options: { diff --git a/packages/core/src/modules/dids/__tests__/DidResolverService.test.ts b/packages/core/src/modules/dids/__tests__/DidResolverService.test.ts index ffa2946dac..785c30d00c 100644 --- a/packages/core/src/modules/dids/__tests__/DidResolverService.test.ts +++ b/packages/core/src/modules/dids/__tests__/DidResolverService.test.ts @@ -1,5 +1,5 @@ import type { IndyLedgerService } from '../../ledger' -import type { DidDocumentRepository } from '../repository' +import type { DidRepository } from '../repository' import { getAgentConfig, mockProperty } from '../../../../tests/helpers' import { JsonTransformer } from '../../../utils/JsonTransformer' @@ -16,7 +16,7 @@ const agentConfig = getAgentConfig('DidResolverService') describe('DidResolverService', () => { const indyLedgerServiceMock = jest.fn() as unknown as IndyLedgerService - const didDocumentRepositoryMock = jest.fn() as unknown as DidDocumentRepository + const didDocumentRepositoryMock = jest.fn() as unknown as DidRepository const didResolverService = new DidResolverService(agentConfig, indyLedgerServiceMock, didDocumentRepositoryMock) it('should correctly find and call the correct resolver for a specified did', async () => { diff --git a/packages/core/src/modules/dids/__tests__/peer-did.test.ts b/packages/core/src/modules/dids/__tests__/peer-did.test.ts index 69c72ead3e..1ba50decf7 100644 --- a/packages/core/src/modules/dids/__tests__/peer-did.test.ts +++ b/packages/core/src/modules/dids/__tests__/peer-did.test.ts @@ -11,7 +11,7 @@ import { convertPublicKeyToX25519, getEd25519VerificationMethod } from '../domai import { getX25519VerificationMethod } from '../domain/key-type/x25519' import { DidKey } from '../methods/key' import { DidPeer, PeerDidNumAlgo } from '../methods/peer/DidPeer' -import { DidDocumentRecord, DidDocumentRepository } from '../repository' +import { DidRecord, DidRepository } from '../repository' import { DidResolverService } from '../services' import didPeer1zQmY from './__fixtures__/didPeer1zQmY.json' @@ -19,7 +19,7 @@ import didPeer1zQmY from './__fixtures__/didPeer1zQmY.json' describe('peer dids', () => { const config = getAgentConfig('Peer DIDs Lifecycle') - let didDocumentRepository: DidDocumentRepository + let didRepository: DidRepository let didResolverService: DidResolverService let wallet: IndyWallet @@ -28,11 +28,11 @@ describe('peer dids', () => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion await wallet.initialize(config.walletConfig!) - const storageService = new IndyStorageService(wallet, config) - didDocumentRepository = new DidDocumentRepository(storageService) + const storageService = new IndyStorageService(wallet, config) + didRepository = new DidRepository(storageService) - // Mocking IndyLedgerService as we're only interestd in the did:peer resolver - didResolverService = new DidResolverService(config, {} as unknown as IndyLedgerService, didDocumentRepository) + // Mocking IndyLedgerService as we're only interested in the did:peer resolver + didResolverService = new DidResolverService(config, {} as unknown as IndyLedgerService, didRepository) }) afterEach(async () => { @@ -102,15 +102,20 @@ describe('peer dids', () => { expect(peerDid.didDocument).toMatchObject(didPeer1zQmY) // Save the record to storage - const didDocumentRecord = new DidDocumentRecord({ + const didDocumentRecord = new DidRecord({ id: didPeer1zQmY.id, role: DidDocumentRole.Created, // It is important to take the did document from the PeerDid class // as it will have the id property didDocument: peerDid.didDocument, + tags: { + // We need to save the recipientKeys, so we can find the associated did + // of a key when we receive a message from another connection. + recipientKeys: peerDid.didDocument.recipientKeys, + }, }) - await didDocumentRepository.save(didDocumentRecord) + await didRepository.save(didDocumentRecord) }) test('receive a did and did document', async () => { @@ -134,17 +139,20 @@ describe('peer dids', () => { expect(didPeer.didDocument.toJSON()).toMatchObject(didPeer1zQmY) } - // If the method is a genesis doc (did:peer:1) we should store the document - // Otherwise this is not needed as we can generate it form the did itself - if (didPeer.numAlgo === PeerDidNumAlgo.GenesisDoc) { - const didDocumentRecord = new DidDocumentRecord({ - id: didPeer.did, - role: DidDocumentRole.Received, - didDocument: didPeer.didDocument, - }) + const didDocumentRecord = new DidRecord({ + id: didPeer.did, + role: DidDocumentRole.Received, + // If the method is a genesis doc (did:peer:1) we should store the document + // Otherwise we only need to store the did itself (as the did can be generated) + didDocument: didPeer.numAlgo === PeerDidNumAlgo.GenesisDoc ? didPeer.didDocument : undefined, + tags: { + // We need to save the recipientKeys, so we can find the associated did + // of a key when we receive a message from another connection. + recipientKeys: didPeer.didDocument.recipientKeys, + }, + }) - await didDocumentRepository.save(didDocumentRecord) - } + await didRepository.save(didDocumentRecord) // Then we save the did (not the did document) in the connection record // connectionRecord.theirDid = didPeer.did diff --git a/packages/core/src/modules/dids/domain/DidDocument.ts b/packages/core/src/modules/dids/domain/DidDocument.ts index dda46608a3..7602bc06ad 100644 --- a/packages/core/src/modules/dids/domain/DidDocument.ts +++ b/packages/core/src/modules/dids/domain/DidDocument.ts @@ -137,6 +137,14 @@ export class DidDocument { return services.sort((a, b) => b.priority - a.priority) } + public get recipientKeys(): string[] { + // Get a `recipientKeys` entries from the did document + return this.didCommServices.reduce( + (recipientKeys, service) => recipientKeys.concat(service.recipientKeys), + [] + ) + } + public toJSON() { return JsonTransformer.toJSON(this) } diff --git a/packages/core/src/modules/dids/methods/peer/PeerDidResolver.ts b/packages/core/src/modules/dids/methods/peer/PeerDidResolver.ts index aea1704f0b..2c6ae9a0dd 100644 --- a/packages/core/src/modules/dids/methods/peer/PeerDidResolver.ts +++ b/packages/core/src/modules/dids/methods/peer/PeerDidResolver.ts @@ -1,17 +1,19 @@ import type { DidDocument } from '../../domain' import type { DidResolver } from '../../domain/DidResolver' -import type { DidDocumentRepository } from '../../repository' +import type { DidRepository } from '../../repository' import type { DidResolutionResult } from '../../types' +import { AriesFrameworkError } from '../../../../error' + import { DidPeer, PeerDidNumAlgo } from './DidPeer' export class PeerDidResolver implements DidResolver { public readonly supportedMethods = ['peer'] - private didDocumentRepository: DidDocumentRepository + private didRepository: DidRepository - public constructor(didDocumentRepository: DidDocumentRepository) { - this.didDocumentRepository = didDocumentRepository + public constructor(didRepository: DidRepository) { + this.didRepository = didRepository } public async resolve(did: string): Promise { @@ -24,7 +26,12 @@ export class PeerDidResolver implements DidResolver { // For Method 1, retrieve from storage if (didPeer.numAlgo === PeerDidNumAlgo.GenesisDoc) { - const didDocumentRecord = await this.didDocumentRepository.getById(did) + const didDocumentRecord = await this.didRepository.getById(did) + + if (!didDocumentRecord.didDocument) { + throw new AriesFrameworkError(`Found did record for method 1 peer did (${did}), but no did document.`) + } + didDocument = didDocumentRecord.didDocument } else { didDocument = didPeer.didDocument diff --git a/packages/core/src/modules/dids/repository/DidDocumentRecord.ts b/packages/core/src/modules/dids/repository/DidRecord.ts similarity index 63% rename from packages/core/src/modules/dids/repository/DidDocumentRecord.ts rename to packages/core/src/modules/dids/repository/DidRecord.ts index 43db2b7b51..eaae6c0ab5 100644 --- a/packages/core/src/modules/dids/repository/DidDocumentRecord.ts +++ b/packages/core/src/modules/dids/repository/DidRecord.ts @@ -7,33 +7,32 @@ import { BaseRecord } from '../../../storage/BaseRecord' import { DidDocument } from '../domain' import { DidDocumentRole } from '../domain/DidDocumentRole' -export interface DidDocumentRecordProps { +export interface DidRecordProps { id: string role: DidDocumentRole - didDocument: DidDocument + didDocument?: DidDocument createdAt?: Date - tags?: CustomDidDocumentTags + tags?: CustomDidTags } -export type CustomDidDocumentTags = TagsBase +interface CustomDidTags extends TagsBase { + recipientKeys?: string[] +} -export type DefaultDidDocumentTags = TagsBase +export type DefaultDidTags = TagsBase -export class DidDocumentRecord - extends BaseRecord - implements DidDocumentRecordProps -{ +export class DidRecord extends BaseRecord implements DidRecordProps { @Type(() => DidDocument) @ValidateNested() - public didDocument!: DidDocument + public didDocument?: DidDocument @IsEnum(DidDocumentRole) public role!: DidDocumentRole public static readonly type = 'DidDocumentRecord' - public readonly type = DidDocumentRecord.type + public readonly type = DidRecord.type - public constructor(props: DidDocumentRecordProps) { + public constructor(props: DidRecordProps) { super() if (props) { diff --git a/packages/core/src/modules/dids/repository/DidDocumentRepository.ts b/packages/core/src/modules/dids/repository/DidRepository.ts similarity index 54% rename from packages/core/src/modules/dids/repository/DidDocumentRepository.ts rename to packages/core/src/modules/dids/repository/DidRepository.ts index 632e1fa50d..226347e3c0 100644 --- a/packages/core/src/modules/dids/repository/DidDocumentRepository.ts +++ b/packages/core/src/modules/dids/repository/DidRepository.ts @@ -4,11 +4,15 @@ import { InjectionSymbols } from '../../../constants' import { Repository } from '../../../storage/Repository' import { StorageService } from '../../../storage/StorageService' -import { DidDocumentRecord } from './DidDocumentRecord' +import { DidRecord } from './DidRecord' @scoped(Lifecycle.ContainerScoped) -export class DidDocumentRepository extends Repository { - public constructor(@inject(InjectionSymbols.StorageService) storageService: StorageService) { - super(DidDocumentRecord, storageService) +export class DidRepository extends Repository { + public constructor(@inject(InjectionSymbols.StorageService) storageService: StorageService) { + super(DidRecord, storageService) + } + + public findByVerkey(verkey: string) { + return this.findSingleByQuery({ recipientKeys: [verkey] }) } } diff --git a/packages/core/src/modules/dids/repository/index.ts b/packages/core/src/modules/dids/repository/index.ts index fda3a2c84e..2d9119d818 100644 --- a/packages/core/src/modules/dids/repository/index.ts +++ b/packages/core/src/modules/dids/repository/index.ts @@ -1,2 +1,2 @@ -export * from './DidDocumentRepository' -export * from './DidDocumentRecord' +export * from './DidRepository' +export * from './DidRecord' diff --git a/packages/core/src/modules/dids/services/DidResolverService.ts b/packages/core/src/modules/dids/services/DidResolverService.ts index 69175fe204..78320d6ed6 100644 --- a/packages/core/src/modules/dids/services/DidResolverService.ts +++ b/packages/core/src/modules/dids/services/DidResolverService.ts @@ -11,25 +11,21 @@ import { IndyDidResolver } from '../methods/indy/IndyDidResolver' import { KeyDidResolver } from '../methods/key/KeyDidResolver' import { PeerDidResolver } from '../methods/peer/PeerDidResolver' import { WebDidResolver } from '../methods/web/WebDidResolver' -import { DidDocumentRepository } from '../repository' +import { DidRepository } from '../repository' @scoped(Lifecycle.ContainerScoped) export class DidResolverService { private logger: Logger private resolvers: DidResolver[] - public constructor( - agentConfig: AgentConfig, - indyLedgerService: IndyLedgerService, - didDocumentRepository: DidDocumentRepository - ) { + public constructor(agentConfig: AgentConfig, indyLedgerService: IndyLedgerService, didRepository: DidRepository) { this.logger = agentConfig.logger this.resolvers = [ new IndyDidResolver(indyLedgerService), new WebDidResolver(), new KeyDidResolver(), - new PeerDidResolver(didDocumentRepository), + new PeerDidResolver(didRepository), ] }