diff --git a/libs/askar/src/agent.utils.ts b/libs/askar/src/agent.utils.ts index 1f70543daa7d42a4a97bc85848d5d85cfa753471..3483728faa84466c17afb2a9f05f6f2d3cc5f6cb 100644 --- a/libs/askar/src/agent.utils.ts +++ b/libs/askar/src/agent.utils.ts @@ -48,7 +48,6 @@ import { } from "@credo-ts/indy-vdr"; import { indyVdr } from "@hyperledger/indy-vdr-nodejs"; import { anoncreds } from "@hyperledger/anoncreds-nodejs"; -import { AskarModule } from "@credo-ts/askar"; import { ariesAskar } from "@hyperledger/aries-askar-nodejs"; import { Key as AskarKey, KeyAlgs } from "@hyperledger/aries-askar-shared"; import { IConfAgent } from "@ocm-engine/config"; @@ -75,6 +74,7 @@ export type SubjectMessage = { import { Request, Response, Express } from "express"; import url from "url"; import { JsonLdCredentialFormatService } from "./credo/JsonLdCredentialFormatService"; +import { CustomAskarModule } from "./credo/CustomAskarModule"; import { EntityNotFoundError } from "@ocm-engine/dtos"; export const importDidsToWallet = async ( @@ -254,7 +254,7 @@ export const getAskarAnonCredsIndyModules = (networks: any) => { new JwkDidResolver(), ], }), - askar: new AskarModule({ + askar: new CustomAskarModule({ ariesAskar, }), w3c: new W3cCredentialsModule(), diff --git a/libs/askar/src/credo/CustomAskarModule.ts b/libs/askar/src/credo/CustomAskarModule.ts new file mode 100644 index 0000000000000000000000000000000000000000..b4092b0196103abf2d56ca8bc8b361d8fb90f511 --- /dev/null +++ b/libs/askar/src/credo/CustomAskarModule.ts @@ -0,0 +1,15 @@ +import { AskarModule } from "@credo-ts/askar"; +import { DependencyManager, InjectionSymbols } from "@credo-ts/core"; +import { CustomAskarStorageService } from "./CustomAskarStorageService"; + +export class CustomAskarModule extends AskarModule { + public override register(dependencyManager: DependencyManager) { + super.register(dependencyManager); + + // re-inject custom askar storage service + dependencyManager.registerSingleton( + InjectionSymbols.StorageService, + CustomAskarStorageService, + ); + } +} diff --git a/libs/askar/src/credo/CustomAskarStorageService.ts b/libs/askar/src/credo/CustomAskarStorageService.ts new file mode 100644 index 0000000000000000000000000000000000000000..6a0ef0afdf5be86ba1f608cd2e33784a70515a81 --- /dev/null +++ b/libs/askar/src/credo/CustomAskarStorageService.ts @@ -0,0 +1,61 @@ +import { + AgentContext, + BaseRecord, + BaseRecordConstructor, + Query, + WalletError, +} from "@credo-ts/core"; +import { AskarStorageService } from "@credo-ts/askar"; +import { + askarQueryFromSearchQuery, + recordToInstance, +} from "@credo-ts/askar/build/storage/utils"; +import { assertAskarWallet } from "@credo-ts/askar/build/utils/assertAskarWallet"; +import { Scan } from "@hyperledger/aries-askar-shared"; + +interface PaginationParams { + offset?: number; + limit?: number; +} + +export type PaginatedQuery<T extends BaseRecord<any, any, any>> = Query<T> & + PaginationParams; + +export class CustomAskarStorageService< + T extends BaseRecord, +> extends AskarStorageService<T> { + /** @inheritDoc */ + public override async findByQuery( + agentContext: AgentContext, + recordClass: BaseRecordConstructor<T>, + paginatedQuery: PaginatedQuery<T>, + ): Promise<T[]> { + const wallet = agentContext.wallet; + assertAskarWallet(wallet); + + const { offset, limit, ...query } = paginatedQuery; + const askarQuery = askarQueryFromSearchQuery(query); + + const scan = new Scan({ + category: recordClass.type, + store: wallet.store, + tagFilter: askarQuery, + profile: wallet.profile, + offset, + limit, + }); + + const instances = []; + try { + const records = await scan.fetchAll(); + for (const record of records) { + instances.push(recordToInstance(record, recordClass)); + } + return instances; + } catch (error: any) { + throw new WalletError(`Error executing query. ${error.message}`, { + cause: error, + }); + } + } +}