Skip to content

Commit

Permalink
refactor!: module to api and module config (#943)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>

BREAKING CHANGE: All module api classes have been renamed from `XXXModule` to `XXXApi`. A module now represents a module plugin, and is separate from the API of a module. If you previously imported e.g. the `CredentialsModule` class, you should now import the `CredentialsApi` class
  • Loading branch information
TimoGlastra committed Aug 26, 2022
1 parent ce90744 commit e018c66
Show file tree
Hide file tree
Showing 121 changed files with 4,941 additions and 3,920 deletions.
89 changes: 53 additions & 36 deletions packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@ import { CacheRepository } from '../cache'
import { InjectionSymbols } from '../constants'
import { JwsService } from '../crypto/JwsService'
import { AriesFrameworkError } from '../error'
import { BasicMessagesModule } from '../modules/basic-messages/BasicMessagesModule'
import { ConnectionsModule } from '../modules/connections/ConnectionsModule'
import { CredentialsModule } from '../modules/credentials/CredentialsModule'
import { DidsModule } from '../modules/dids/DidsModule'
import { BasicMessagesModule } from '../modules/basic-messages'
import { ConnectionsModule } from '../modules/connections'
import { CredentialsModule } from '../modules/credentials'
import { DidsModule } from '../modules/dids'
import { DiscoverFeaturesModule } from '../modules/discover-features'
import { GenericRecordsModule } from '../modules/generic-records/GenericRecordsModule'
import { IndyModule } from '../modules/indy/module'
import { LedgerModule } from '../modules/ledger/LedgerModule'
import { OutOfBandModule } from '../modules/oob/OutOfBandModule'
import { ProofsModule } from '../modules/proofs/ProofsModule'
import { QuestionAnswerModule } from '../modules/question-answer/QuestionAnswerModule'
import { MediatorModule } from '../modules/routing/MediatorModule'
import { RecipientModule } from '../modules/routing/RecipientModule'
import { W3cVcModule } from '../modules/vc/module'
import { GenericRecordsModule } from '../modules/generic-records'
import { IndyModule } from '../modules/indy'
import { LedgerModule } from '../modules/ledger'
import { OutOfBandModule } from '../modules/oob'
import { ProofsModule } from '../modules/proofs'
import { QuestionAnswerModule } from '../modules/question-answer'
import { MediatorModule, RecipientModule } from '../modules/routing'
import { W3cVcModule } from '../modules/vc'
import { DependencyManager } from '../plugins'
import { DidCommMessageRepository, StorageUpdateService, StorageVersionRepository } from '../storage'
import { InMemoryMessageRepository } from '../storage/InMemoryMessageRepository'
import { IndyStorageService } from '../storage/IndyStorageService'
import { WalletModule } from '../wallet'
import { IndyWallet } from '../wallet/IndyWallet'
import { WalletModule } from '../wallet/WalletModule'

import { AgentConfig } from './AgentConfig'
import { BaseAgent } from './BaseAgent'
Expand Down Expand Up @@ -94,14 +93,12 @@ export class Agent extends BaseAgent {
}

public async initialize() {
const { connectToIndyLedgersOnStartup, mediatorConnectionsInvite } = this.agentConfig

await super.initialize()

// set the pools on the ledger.
this.ledger.setPools(this.agentContext.config.indyLedgers)
this.ledger.setPools(this.ledger.config.indyLedgers)
// As long as value isn't false we will async connect to all genesis pools on startup
if (connectToIndyLedgersOnStartup) {
if (this.ledger.config.connectToIndyLedgersOnStartup) {
this.ledger.connectToPools().catch((error) => {
this.logger.warn('Error connecting to ledger, will try to reconnect when needed.', { error })
})
Expand All @@ -118,9 +115,13 @@ export class Agent extends BaseAgent {
// Connect to mediator through provided invitation if provided in config
// Also requests mediation ans sets as default mediator
// Because this requires the connections module, we do this in the agent constructor
if (mediatorConnectionsInvite) {
this.logger.debug('Provision mediation with invitation', { mediatorConnectionsInvite })
const mediationConnection = await this.getMediationConnection(mediatorConnectionsInvite)
if (this.mediationRecipient.config.mediatorInvitationUrl) {
this.logger.debug('Provision mediation with invitation', {
mediatorInvitationUrl: this.mediationRecipient.config.mediatorInvitationUrl,
})
const mediationConnection = await this.getMediationConnection(
this.mediationRecipient.config.mediatorInvitationUrl
)
await this.mediationRecipient.provision(mediationConnection)
}
await this.mediator.initialize()
Expand Down Expand Up @@ -182,21 +183,37 @@ export class Agent extends BaseAgent {

// Register all modules
dependencyManager.registerModules(
ConnectionsModule,
CredentialsModule,
ProofsModule,
MediatorModule,
RecipientModule,
BasicMessagesModule,
QuestionAnswerModule,
GenericRecordsModule,
LedgerModule,
DiscoverFeaturesModule,
DidsModule,
WalletModule,
OutOfBandModule,
IndyModule,
W3cVcModule
new ConnectionsModule({
autoAcceptConnections: this.agentConfig.autoAcceptConnections,
}),
new CredentialsModule({
autoAcceptCredentials: this.agentConfig.autoAcceptCredentials,
}),
new ProofsModule({
autoAcceptProofs: this.agentConfig.autoAcceptProofs,
}),
new MediatorModule({
autoAcceptMediationRequests: this.agentConfig.autoAcceptMediationRequests,
}),
new RecipientModule({
maximumMessagePickup: this.agentConfig.maximumMessagePickup,
mediatorInvitationUrl: this.agentConfig.mediatorConnectionsInvite,
mediatorPickupStrategy: this.agentConfig.mediatorPickupStrategy,
mediatorPollingInterval: this.agentConfig.mediatorPollingInterval,
}),
new BasicMessagesModule(),
new QuestionAnswerModule(),
new GenericRecordsModule(),
new LedgerModule({
connectToIndyLedgersOnStartup: this.agentConfig.connectToIndyLedgersOnStartup,
indyLedgers: this.agentConfig.indyLedgers,
}),
new DiscoverFeaturesModule(),
new DidsModule(),
new WalletModule(),
new OutOfBandModule(),
new IndyModule(),
new W3cVcModule()
)

// TODO: contextCorrelationId for base wallet
Expand Down
45 changes: 45 additions & 0 deletions packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,51 @@ export class AgentConfig {
}
}

/**
* @deprecated use connectToIndyLedgersOnStartup from the `LedgerModuleConfig` class
*/
public get connectToIndyLedgersOnStartup() {
return this.initConfig.connectToIndyLedgersOnStartup ?? true
}

/**
* @todo remove once did registrar module is available
*/
public get publicDidSeed() {
return this.initConfig.publicDidSeed
}

/**
* @deprecated use indyLedgers from the `LedgerModuleConfig` class
*/
public get indyLedgers() {
return this.initConfig.indyLedgers ?? []
}

/**
* @todo move to context configuration
*/
public get walletConfig() {
return this.initConfig.walletConfig
}

/**
* @deprecated use autoAcceptConnections from the `ConnectionsModuleConfig` class
*/
public get autoAcceptConnections() {
return this.initConfig.autoAcceptConnections ?? false
}

/**
* @deprecated use autoAcceptProofs from the `ProofsModuleConfig` class
*/
public get autoAcceptProofs() {
return this.initConfig.autoAcceptProofs ?? AutoAcceptProof.Never
}

/**
* @deprecated use autoAcceptCredentials from the `CredentialsModuleConfig` class
*/
public get autoAcceptCredentials() {
return this.initConfig.autoAcceptCredentials ?? AutoAcceptCredential.Never
}
Expand All @@ -63,14 +84,23 @@ export class AgentConfig {
return this.initConfig.didCommMimeType ?? DidCommMimeType.V0
}

/**
* @deprecated use mediatorPollingInterval from the `RecipientModuleConfig` class
*/
public get mediatorPollingInterval() {
return this.initConfig.mediatorPollingInterval ?? 5000
}

/**
* @deprecated use mediatorPickupStrategy from the `RecipientModuleConfig` class
*/
public get mediatorPickupStrategy() {
return this.initConfig.mediatorPickupStrategy
}

/**
* @deprecated use maximumMessagePickup from the `RecipientModuleConfig` class
*/
public get maximumMessagePickup() {
return this.initConfig.maximumMessagePickup ?? 10
}
Expand All @@ -85,18 +115,30 @@ export class AgentConfig {
return this.initConfig.endpoints as [string, ...string[]]
}

/**
* @deprecated use mediatorInvitationUrl from the `RecipientModuleConfig` class
*/
public get mediatorConnectionsInvite() {
return this.initConfig.mediatorConnectionsInvite
}

/**
* @deprecated use autoAcceptMediationRequests from the `MediatorModuleConfig` class
*/
public get autoAcceptMediationRequests() {
return this.initConfig.autoAcceptMediationRequests ?? false
}

/**
* @deprecated you can use `RecipientApi.setDefaultMediator` to set the default mediator.
*/
public get defaultMediatorId() {
return this.initConfig.defaultMediatorId
}

/**
* @deprecated you can set the `default` tag to `false` (or remove it completely) to clear the default mediator.
*/
public get clearDefaultMediator() {
return this.initConfig.clearDefaultMediator ?? false
}
Expand All @@ -105,6 +147,9 @@ export class AgentConfig {
return this.initConfig.useLegacyDidSovPrefix ?? false
}

/**
* @todo move to context configuration
*/
public get connectionImageUrl() {
return this.initConfig.connectionImageUrl
}
Expand Down
78 changes: 39 additions & 39 deletions packages/core/src/agent/BaseAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import type { AgentConfig } from './AgentConfig'
import type { TransportSession } from './TransportService'

import { AriesFrameworkError } from '../error'
import { BasicMessagesModule } from '../modules/basic-messages/BasicMessagesModule'
import { ConnectionsModule } from '../modules/connections/ConnectionsModule'
import { CredentialsModule } from '../modules/credentials/CredentialsModule'
import { DidsModule } from '../modules/dids/DidsModule'
import { DiscoverFeaturesModule } from '../modules/discover-features'
import { GenericRecordsModule } from '../modules/generic-records/GenericRecordsModule'
import { LedgerModule } from '../modules/ledger/LedgerModule'
import { OutOfBandModule } from '../modules/oob/OutOfBandModule'
import { ProofsModule } from '../modules/proofs/ProofsModule'
import { QuestionAnswerModule } from '../modules/question-answer/QuestionAnswerModule'
import { MediatorModule } from '../modules/routing/MediatorModule'
import { RecipientModule } from '../modules/routing/RecipientModule'
import { BasicMessagesApi } from '../modules/basic-messages/BasicMessagesApi'
import { ConnectionsApi } from '../modules/connections/ConnectionsApi'
import { CredentialsApi } from '../modules/credentials/CredentialsApi'
import { DidsApi } from '../modules/dids/DidsApi'
import { DiscoverFeaturesApi } from '../modules/discover-features'
import { GenericRecordsApi } from '../modules/generic-records/GenericRecordsApi'
import { LedgerApi } from '../modules/ledger/LedgerApi'
import { OutOfBandApi } from '../modules/oob/OutOfBandApi'
import { ProofsApi } from '../modules/proofs/ProofsApi'
import { QuestionAnswerApi } from '../modules/question-answer/QuestionAnswerApi'
import { MediatorApi } from '../modules/routing/MediatorApi'
import { RecipientApi } from '../modules/routing/RecipientApi'
import { StorageUpdateService } from '../storage'
import { UpdateAssistant } from '../storage/migration/UpdateAssistant'
import { DEFAULT_UPDATE_CONFIG } from '../storage/migration/updates'
import { WalletModule } from '../wallet/WalletModule'
import { WalletApi } from '../wallet/WalletApi'
import { WalletError } from '../wallet/error'

import { EventEmitter } from './EventEmitter'
Expand All @@ -39,19 +39,19 @@ export abstract class BaseAgent {
protected _isInitialized = false
protected agentContext: AgentContext

public readonly connections: ConnectionsModule
public readonly proofs: ProofsModule
public readonly basicMessages: BasicMessagesModule
public readonly genericRecords: GenericRecordsModule
public readonly ledger: LedgerModule
public readonly questionAnswer!: QuestionAnswerModule
public readonly credentials: CredentialsModule
public readonly mediationRecipient: RecipientModule
public readonly mediator: MediatorModule
public readonly discovery: DiscoverFeaturesModule
public readonly dids: DidsModule
public readonly wallet: WalletModule
public readonly oob: OutOfBandModule
public readonly connections: ConnectionsApi
public readonly proofs: ProofsApi
public readonly basicMessages: BasicMessagesApi
public readonly genericRecords: GenericRecordsApi
public readonly ledger: LedgerApi
public readonly questionAnswer!: QuestionAnswerApi
public readonly credentials: CredentialsApi
public readonly mediationRecipient: RecipientApi
public readonly mediator: MediatorApi
public readonly discovery: DiscoverFeaturesApi
public readonly dids: DidsApi
public readonly wallet: WalletApi
public readonly oob: OutOfBandApi

public constructor(agentConfig: AgentConfig, dependencyManager: DependencyManager) {
this.dependencyManager = dependencyManager
Expand Down Expand Up @@ -81,19 +81,19 @@ export abstract class BaseAgent {
this.agentContext = this.dependencyManager.resolve(AgentContext)

// We set the modules in the constructor because that allows to set them as read-only
this.connections = this.dependencyManager.resolve(ConnectionsModule)
this.credentials = this.dependencyManager.resolve(CredentialsModule) as CredentialsModule
this.proofs = this.dependencyManager.resolve(ProofsModule)
this.mediator = this.dependencyManager.resolve(MediatorModule)
this.mediationRecipient = this.dependencyManager.resolve(RecipientModule)
this.basicMessages = this.dependencyManager.resolve(BasicMessagesModule)
this.questionAnswer = this.dependencyManager.resolve(QuestionAnswerModule)
this.genericRecords = this.dependencyManager.resolve(GenericRecordsModule)
this.ledger = this.dependencyManager.resolve(LedgerModule)
this.discovery = this.dependencyManager.resolve(DiscoverFeaturesModule)
this.dids = this.dependencyManager.resolve(DidsModule)
this.wallet = this.dependencyManager.resolve(WalletModule)
this.oob = this.dependencyManager.resolve(OutOfBandModule)
this.connections = this.dependencyManager.resolve(ConnectionsApi)
this.credentials = this.dependencyManager.resolve(CredentialsApi) as CredentialsApi
this.proofs = this.dependencyManager.resolve(ProofsApi)
this.mediator = this.dependencyManager.resolve(MediatorApi)
this.mediationRecipient = this.dependencyManager.resolve(RecipientApi)
this.basicMessages = this.dependencyManager.resolve(BasicMessagesApi)
this.questionAnswer = this.dependencyManager.resolve(QuestionAnswerApi)
this.genericRecords = this.dependencyManager.resolve(GenericRecordsApi)
this.ledger = this.dependencyManager.resolve(LedgerApi)
this.discovery = this.dependencyManager.resolve(DiscoverFeaturesApi)
this.dids = this.dependencyManager.resolve(DidsApi)
this.wallet = this.dependencyManager.resolve(WalletApi)
this.oob = this.dependencyManager.resolve(OutOfBandApi)
}

public get isInitialized() {
Expand Down
Loading

0 comments on commit e018c66

Please sign in to comment.