Skip to content

Commit

Permalink
feat(indy-vdr): module registration (#1285)
Browse files Browse the repository at this point in the history
Signed-off-by: Karim Stekelenburg <[email protected]>
  • Loading branch information
karimStekelenburg authored Feb 13, 2023
1 parent d61f6ed commit 51030d4
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 43 deletions.
24 changes: 24 additions & 0 deletions packages/indy-vdr/src/IndyVdrModule.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
36 changes: 36 additions & 0 deletions packages/indy-vdr/src/IndyVdrModuleConfig.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
36 changes: 36 additions & 0 deletions packages/indy-vdr/src/__tests__/IndyVdrModule.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
15 changes: 15 additions & 0 deletions packages/indy-vdr/src/__tests__/IndyVdrModuleConfig.test.ts
Original file line number Diff line number Diff line change
@@ -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])
})
})
2 changes: 1 addition & 1 deletion packages/indy-vdr/src/dids/IndyVdrSovDidResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndyVdrPoolService>
const poolServiceMock = new IndyVdrPoolServiceMock()

jest.mock('../../pool/IndyVdrPool')
const IndyVdrPoolMock = IndyVdrPool as jest.Mock<IndyVdrPool>
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()
Expand Down
10 changes: 5 additions & 5 deletions packages/indy-vdr/src/pool/IndyVdrPoolService.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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))
}

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/indy-vdr/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 10 additions & 16 deletions packages/indy-vdr/tests/indy-vdr-anoncreds-registry.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -47,7 +41,7 @@ describe('IndyVdrAnonCredsRegistry', () => {

const schemaResult = await indyVdrAnonCredsRegistry.registerSchema(agent.context, {
options: {
didIndyNamespace: 'local:test',
didIndyNamespace: 'pool:localtest',
},
schema: {
attrNames: ['age'],
Expand All @@ -71,7 +65,7 @@ describe('IndyVdrAnonCredsRegistry', () => {
registrationMetadata: {},
schemaMetadata: {
indyLedgerSeqNo: expect.any(Number),
didIndyNamespace: 'local:test',
didIndyNamespace: 'pool:localtest',
},
})

Expand All @@ -89,7 +83,7 @@ describe('IndyVdrAnonCredsRegistry', () => {
schemaId: `TL1EaPFCZ8Si5aUrqScBDt:2:test:${dynamicVersion}`,
resolutionMetadata: {},
schemaMetadata: {
didIndyNamespace: 'local:test',
didIndyNamespace: 'pool:localtest',
indyLedgerSeqNo: expect.any(Number),
},
})
Expand All @@ -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: {
Expand Down Expand Up @@ -182,7 +176,7 @@ describe('IndyVdrAnonCredsRegistry', () => {
},
},
credentialDefinitionMetadata: {
didIndyNamespace: 'local:test',
didIndyNamespace: 'pool:localtest',
},
resolutionMetadata: {},
})
Expand Down
14 changes: 3 additions & 11 deletions packages/indy-vdr/tests/indy-vdr-did-resolver.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]))
Expand All @@ -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 () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/indy-vdr/tests/indy-vdr-pool.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -23,8 +25,6 @@ const config = {

let signerKey: Key

indyVdrPoolService.setPools([config])

describe('IndyVdrPoolService', () => {
beforeAll(async () => {
await indyVdrPoolService.connectToPools()
Expand Down

0 comments on commit 51030d4

Please sign in to comment.