diff --git a/packages/indy-vdr/src/IndyVdrModule.ts b/packages/indy-vdr/src/IndyVdrModule.ts new file mode 100644 index 0000000000..6150435c51 --- /dev/null +++ b/packages/indy-vdr/src/IndyVdrModule.ts @@ -0,0 +1,24 @@ +import type { IndyVdrModuleConfigOptions } from './IndyVdrModuleConfig' +import type { DependencyManager, Module } from '@aries-framework/core' + +import { IndyVdrModuleConfig } from './IndyVdrModuleConfig' +import { IndyVdrPoolService } from './pool/IndyVdrPoolService' + +/** + * @public + * */ +export class IndyVdrModule implements Module { + public readonly config: IndyVdrModuleConfig + + public constructor(config: IndyVdrModuleConfigOptions) { + this.config = new IndyVdrModuleConfig(config) + } + + public register(dependencyManager: DependencyManager) { + // Config + dependencyManager.registerInstance(IndyVdrModuleConfig, this.config) + + // Services + dependencyManager.registerSingleton(IndyVdrPoolService) + } +} diff --git a/packages/indy-vdr/src/IndyVdrModuleConfig.ts b/packages/indy-vdr/src/IndyVdrModuleConfig.ts new file mode 100644 index 0000000000..6b6b2eaddb --- /dev/null +++ b/packages/indy-vdr/src/IndyVdrModuleConfig.ts @@ -0,0 +1,36 @@ +import type { IndyVdrPoolConfig } from './pool' + +export interface IndyVdrModuleConfigOptions { + /** + * Array of indy networks to connect to. + * + * [@default](https://github.com/default) [] + * + * @example + * ``` + * { + * isProduction: false, + * genesisTransactions: 'xxx', + * indyNamespace: 'localhost:test', + * transactionAuthorAgreement: { + * version: '1', + * acceptanceMechanism: 'accept' + * } + * } + * ``` + */ + networks: [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]] +} + +export class IndyVdrModuleConfig { + private options: IndyVdrModuleConfigOptions + + public constructor(options: IndyVdrModuleConfigOptions) { + this.options = options + } + + /** See {@link IndyVdrModuleConfigOptions.networks} */ + public get networks() { + return this.options.networks + } +} diff --git a/packages/indy-vdr/src/__tests__/IndyVdrModule.test.ts b/packages/indy-vdr/src/__tests__/IndyVdrModule.test.ts new file mode 100644 index 0000000000..4d904fa133 --- /dev/null +++ b/packages/indy-vdr/src/__tests__/IndyVdrModule.test.ts @@ -0,0 +1,36 @@ +import type { DependencyManager } from '@aries-framework/core' + +import { IndyVdrModule } from '../IndyVdrModule' +import { IndyVdrModuleConfig } from '../IndyVdrModuleConfig' +import { IndyVdrPoolService } from '../pool' + +const dependencyManager = { + registerInstance: jest.fn(), + registerSingleton: jest.fn(), +} as unknown as DependencyManager + +describe('IndyVdrModule', () => { + test('registers dependencies on the dependency manager', () => { + const indyVdrModule = new IndyVdrModule({ + networks: [ + { + isProduction: false, + genesisTransactions: 'xxx', + indyNamespace: 'localhost:test', + transactionAuthorAgreement: { + version: '1', + acceptanceMechanism: 'accept', + }, + }, + ], + }) + + indyVdrModule.register(dependencyManager) + + expect(dependencyManager.registerSingleton).toHaveBeenCalledTimes(1) + expect(dependencyManager.registerSingleton).toHaveBeenCalledWith(IndyVdrPoolService) + + expect(dependencyManager.registerInstance).toHaveBeenCalledTimes(1) + expect(dependencyManager.registerInstance).toHaveBeenCalledWith(IndyVdrModuleConfig, indyVdrModule.config) + }) +}) diff --git a/packages/indy-vdr/src/__tests__/IndyVdrModuleConfig.test.ts b/packages/indy-vdr/src/__tests__/IndyVdrModuleConfig.test.ts new file mode 100644 index 0000000000..db86ddb519 --- /dev/null +++ b/packages/indy-vdr/src/__tests__/IndyVdrModuleConfig.test.ts @@ -0,0 +1,15 @@ +import type { IndyVdrPoolConfig } from '../pool' + +import { IndyVdrModuleConfig } from '../IndyVdrModuleConfig' + +describe('IndyVdrModuleConfig', () => { + test('sets values', () => { + const networkConfig = {} as IndyVdrPoolConfig + + const config = new IndyVdrModuleConfig({ + networks: [networkConfig], + }) + + expect(config.networks).toEqual([networkConfig]) + }) +}) diff --git a/packages/indy-vdr/src/dids/IndyVdrSovDidResolver.ts b/packages/indy-vdr/src/dids/IndyVdrSovDidResolver.ts index 92fbefa20f..b563a3122e 100644 --- a/packages/indy-vdr/src/dids/IndyVdrSovDidResolver.ts +++ b/packages/indy-vdr/src/dids/IndyVdrSovDidResolver.ts @@ -4,7 +4,7 @@ import type { DidResolutionResult, ParsedDid, DidResolver, AgentContext } from ' import { GetAttribRequest, GetNymRequest } from '@hyperledger/indy-vdr-shared' import { IndyVdrError, IndyVdrNotFoundError } from '../error' -import { IndyVdrPoolService } from '../pool' +import { IndyVdrPoolService } from '../pool/IndyVdrPoolService' import { addServicesFromEndpointsAttrib, sovDidDocumentFromDid } from './didSovUtil' diff --git a/packages/indy-vdr/src/dids/__tests__/IndyVdrSovDidResolver.test.ts b/packages/indy-vdr/src/dids/__tests__/IndyVdrSovDidResolver.test.ts index 269aaa1a46..c8001ccd19 100644 --- a/packages/indy-vdr/src/dids/__tests__/IndyVdrSovDidResolver.test.ts +++ b/packages/indy-vdr/src/dids/__tests__/IndyVdrSovDidResolver.test.ts @@ -2,27 +2,23 @@ import { JsonTransformer } from '@aries-framework/core' import { parseDid } from '../../../../core/src/modules/dids/domain/parse' import { getAgentConfig, getAgentContext, mockProperty } from '../../../../core/tests/helpers' -import { IndyVdrPool, IndyVdrPoolService } from '../../pool' +import { IndyVdrPool } from '../../pool/IndyVdrPool' +import { IndyVdrPoolService } from '../../pool/IndyVdrPoolService' import { IndyVdrSovDidResolver } from '../IndyVdrSovDidResolver' import didSovR1xKJw17sUoXhejEpugMYJFixture from './__fixtures__/didSovR1xKJw17sUoXhejEpugMYJ.json' import didSovWJz9mHyW9BZksioQnRsrAoFixture from './__fixtures__/didSovWJz9mHyW9BZksioQnRsrAo.json' -jest.mock('../../pool/IndyVdrPoolService') -const IndyVdrPoolServiceMock = IndyVdrPoolService as jest.Mock -const poolServiceMock = new IndyVdrPoolServiceMock() - jest.mock('../../pool/IndyVdrPool') const IndyVdrPoolMock = IndyVdrPool as jest.Mock const poolMock = new IndyVdrPoolMock() mockProperty(poolMock, 'indyNamespace', 'local') -jest.spyOn(poolServiceMock, 'getPoolForDid').mockResolvedValue(poolMock) const agentConfig = getAgentConfig('IndyVdrSovDidResolver') const agentContext = getAgentContext({ agentConfig, - registerInstances: [[IndyVdrPoolService, poolServiceMock]], + registerInstances: [[IndyVdrPoolService, { getPoolForDid: jest.fn().mockReturnValue(poolMock) }]], }) const resolver = new IndyVdrSovDidResolver() diff --git a/packages/indy-vdr/src/pool/IndyVdrPoolService.ts b/packages/indy-vdr/src/pool/IndyVdrPoolService.ts index b6a1a0f989..3fa6177465 100644 --- a/packages/indy-vdr/src/pool/IndyVdrPoolService.ts +++ b/packages/indy-vdr/src/pool/IndyVdrPoolService.ts @@ -1,10 +1,10 @@ -import type { IndyVdrPoolConfig } from './IndyVdrPool' import type { AgentContext } from '@aries-framework/core' import type { GetNymResponse } from '@hyperledger/indy-vdr-shared' import { Logger, InjectionSymbols, injectable, inject, CacheModuleConfig } from '@aries-framework/core' import { GetNymRequest } from '@hyperledger/indy-vdr-shared' +import { IndyVdrModuleConfig } from '../IndyVdrModuleConfig' import { IndyVdrError, IndyVdrNotFoundError, IndyVdrNotConfiguredError } from '../error' import { isSelfCertifiedDid, DID_INDY_REGEX } from '../utils/did' import { allSettled, onlyFulfilled, onlyRejected } from '../utils/promises' @@ -22,13 +22,13 @@ export interface CachedDidResponse { export class IndyVdrPoolService { public pools: IndyVdrPool[] = [] private logger: Logger + private indyVdrModuleConfig: IndyVdrModuleConfig - public constructor(@inject(InjectionSymbols.Logger) logger: Logger) { + public constructor(@inject(InjectionSymbols.Logger) logger: Logger, indyVdrModuleConfig: IndyVdrModuleConfig) { this.logger = logger - } + this.indyVdrModuleConfig = indyVdrModuleConfig - public setPools(poolConfigs: IndyVdrPoolConfig[]) { - this.pools = poolConfigs.map((poolConfig) => new IndyVdrPool(poolConfig, this.logger)) + this.pools = this.indyVdrModuleConfig.networks.map((poolConfig) => new IndyVdrPool(poolConfig, this.logger)) } /** diff --git a/packages/indy-vdr/tests/helpers.ts b/packages/indy-vdr/tests/helpers.ts index 2ac21a7711..ecaf154ee9 100644 --- a/packages/indy-vdr/tests/helpers.ts +++ b/packages/indy-vdr/tests/helpers.ts @@ -4,8 +4,21 @@ import type { AgentContext, Key } from '@aries-framework/core' import { KeyType } from '@aries-framework/core' import { AttribRequest, NymRequest } from '@hyperledger/indy-vdr-shared' +import { genesisTransactions } from '../../core/tests/helpers' +import { IndyVdrModuleConfig } from '../src/IndyVdrModuleConfig' import { indyDidFromPublicKeyBase58 } from '../src/utils/did' +export const indyVdrModuleConfig = new IndyVdrModuleConfig({ + networks: [ + { + genesisTransactions, + indyNamespace: 'pool:localtest', + isProduction: false, + transactionAuthorAgreement: { version: '1', acceptanceMechanism: 'accept' }, + }, + ], +}) + export async function createDidOnLedger( indyVdrPoolService: IndyVdrPoolService, agentContext: AgentContext, diff --git a/packages/indy-vdr/tests/indy-vdr-anoncreds-registry.e2e.test.ts b/packages/indy-vdr/tests/indy-vdr-anoncreds-registry.e2e.test.ts index 9d214ea43d..d45597ce8f 100644 --- a/packages/indy-vdr/tests/indy-vdr-anoncreds-registry.e2e.test.ts +++ b/packages/indy-vdr/tests/indy-vdr-anoncreds-registry.e2e.test.ts @@ -1,21 +1,15 @@ import { Agent } from '@aries-framework/core' -import { agentDependencies, genesisTransactions, getAgentConfig } from '../../core/tests/helpers' +import { agentDependencies, getAgentConfig } from '../../core/tests/helpers' import { IndyVdrAnonCredsRegistry } from '../src/anoncreds/IndyVdrAnonCredsRegistry' import { IndyVdrPoolService } from '../src/pool' +import { indyVdrModuleConfig } from './helpers' + const agentConfig = getAgentConfig('IndyVdrAnonCredsRegistry') // TODO: update to module once available -const indyVdrPoolService = new IndyVdrPoolService(agentConfig.logger) -indyVdrPoolService.setPools([ - { - genesisTransactions, - indyNamespace: 'local:test', - isProduction: false, - transactionAuthorAgreement: { version: '1', acceptanceMechanism: 'accept' }, - }, -]) +const indyVdrPoolService = new IndyVdrPoolService(agentConfig.logger, indyVdrModuleConfig) const indyVdrAnonCredsRegistry = new IndyVdrAnonCredsRegistry() @@ -47,7 +41,7 @@ describe('IndyVdrAnonCredsRegistry', () => { const schemaResult = await indyVdrAnonCredsRegistry.registerSchema(agent.context, { options: { - didIndyNamespace: 'local:test', + didIndyNamespace: 'pool:localtest', }, schema: { attrNames: ['age'], @@ -71,7 +65,7 @@ describe('IndyVdrAnonCredsRegistry', () => { registrationMetadata: {}, schemaMetadata: { indyLedgerSeqNo: expect.any(Number), - didIndyNamespace: 'local:test', + didIndyNamespace: 'pool:localtest', }, }) @@ -89,7 +83,7 @@ describe('IndyVdrAnonCredsRegistry', () => { schemaId: `TL1EaPFCZ8Si5aUrqScBDt:2:test:${dynamicVersion}`, resolutionMetadata: {}, schemaMetadata: { - didIndyNamespace: 'local:test', + didIndyNamespace: 'pool:localtest', indyLedgerSeqNo: expect.any(Number), }, }) @@ -116,13 +110,13 @@ describe('IndyVdrAnonCredsRegistry', () => { }, }, options: { - didIndyNamespace: 'local:test', + didIndyNamespace: 'pool:localtest', }, }) expect(credentialDefinitionResult).toMatchObject({ credentialDefinitionMetadata: { - didIndyNamespace: 'local:test', + didIndyNamespace: 'pool:localtest', }, credentialDefinitionState: { credentialDefinition: { @@ -182,7 +176,7 @@ describe('IndyVdrAnonCredsRegistry', () => { }, }, credentialDefinitionMetadata: { - didIndyNamespace: 'local:test', + didIndyNamespace: 'pool:localtest', }, resolutionMetadata: {}, }) diff --git a/packages/indy-vdr/tests/indy-vdr-did-resolver.e2e.test.ts b/packages/indy-vdr/tests/indy-vdr-did-resolver.e2e.test.ts index 72a09afc83..e9f3aa4eab 100644 --- a/packages/indy-vdr/tests/indy-vdr-did-resolver.e2e.test.ts +++ b/packages/indy-vdr/tests/indy-vdr-did-resolver.e2e.test.ts @@ -10,13 +10,13 @@ import { } from '@aries-framework/core' import { parseDid } from '../../core/src/modules/dids/domain/parse' -import { agentDependencies, genesisTransactions, getAgentConfig, getAgentContext } from '../../core/tests/helpers' +import { agentDependencies, getAgentConfig, getAgentContext } from '../../core/tests/helpers' import testLogger from '../../core/tests/logger' import { IndyVdrSovDidResolver } from '../src/dids' import { IndyVdrPoolService } from '../src/pool/IndyVdrPoolService' import { indyDidFromPublicKeyBase58 } from '../src/utils/did' -import { createDidOnLedger } from './helpers' +import { createDidOnLedger, indyVdrModuleConfig } from './helpers' const logger = testLogger const wallet = new IndyWallet(agentDependencies, logger, new SigningProviderRegistry([])) @@ -25,26 +25,18 @@ const agentConfig = getAgentConfig('IndyVdrResolver E2E', { logger }) const cache = new InMemoryLruCache({ limit: 200 }) const indyVdrSovDidResolver = new IndyVdrSovDidResolver() -const config = { - isProduction: false, - genesisTransactions, - indyNamespace: `pool:localtest`, - transactionAuthorAgreement: { version: '1', acceptanceMechanism: 'accept' }, -} as const - let signerKey: Key const agentContext = getAgentContext({ wallet, agentConfig, registerInstances: [ - [IndyVdrPoolService, new IndyVdrPoolService(logger)], + [IndyVdrPoolService, new IndyVdrPoolService(logger, indyVdrModuleConfig)], [CacheModuleConfig, new CacheModuleConfig({ cache })], ], }) const indyVdrPoolService = agentContext.dependencyManager.resolve(IndyVdrPoolService) -indyVdrPoolService.setPools([config]) describe('IndyVdrSov', () => { beforeAll(async () => { diff --git a/packages/indy-vdr/tests/indy-vdr-pool.e2e.test.ts b/packages/indy-vdr/tests/indy-vdr-pool.e2e.test.ts index 95a3882ff1..fea5b8fe0f 100644 --- a/packages/indy-vdr/tests/indy-vdr-pool.e2e.test.ts +++ b/packages/indy-vdr/tests/indy-vdr-pool.e2e.test.ts @@ -9,7 +9,9 @@ import { IndyVdrPool } from '../src/pool' import { IndyVdrPoolService } from '../src/pool/IndyVdrPoolService' import { indyDidFromPublicKeyBase58 } from '../src/utils/did' -const indyVdrPoolService = new IndyVdrPoolService(testLogger) +import { indyVdrModuleConfig } from './helpers' + +const indyVdrPoolService = new IndyVdrPoolService(testLogger, indyVdrModuleConfig) const wallet = new IndyWallet(agentDependencies, testLogger, new SigningProviderRegistry([])) const agentConfig = getAgentConfig('IndyVdrPoolService') const agentContext = getAgentContext({ wallet, agentConfig }) @@ -23,8 +25,6 @@ const config = { let signerKey: Key -indyVdrPoolService.setPools([config]) - describe('IndyVdrPoolService', () => { beforeAll(async () => { await indyVdrPoolService.connectToPools()