Skip to content

Commit

Permalink
refactor: require native shared components implementation (#1349)
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Anene <[email protected]>
  • Loading branch information
Vickysomtee authored Mar 19, 2023
1 parent 5559996 commit 343ce6a
Show file tree
Hide file tree
Showing 30 changed files with 282 additions and 110 deletions.
5 changes: 5 additions & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"faber": "ts-node src/FaberInquirer.ts",
"refresh": "rm -rf ./node_modules ./yarn.lock && yarn"
},
"dependencies": {
"@hyperledger/indy-vdr-nodejs": "0.1.0-dev.12",
"@hyperledger/anoncreds-nodejs": "^0.1.0-dev.11",
"@hyperledger/aries-askar-nodejs": "^0.1.0-dev.6"
},
"devDependencies": {
"@aries-framework/anoncreds": "*",
"@aries-framework/anoncreds-rs": "*",
Expand Down
12 changes: 10 additions & 2 deletions demo/src/BaseAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
import { IndySdkAnonCredsRegistry, IndySdkModule, IndySdkSovDidResolver } from '@aries-framework/indy-sdk'
import { IndyVdrAnonCredsRegistry, IndyVdrModule, IndyVdrSovDidResolver } from '@aries-framework/indy-vdr'
import { agentDependencies, HttpInboundTransport } from '@aries-framework/node'
import { anoncreds } from '@hyperledger/anoncreds-nodejs'
import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
import { indyVdr } from '@hyperledger/indy-vdr-nodejs'
import { randomUUID } from 'crypto'
import indySdk from 'indy-sdk'

Expand Down Expand Up @@ -125,14 +128,19 @@ function getAskarAnonCredsIndyModules() {
anoncreds: new AnonCredsModule({
registries: [new IndyVdrAnonCredsRegistry()],
}),
anoncredsRs: new AnonCredsRsModule(),
anoncredsRs: new AnonCredsRsModule({
anoncreds,
}),
indyVdr: new IndyVdrModule({
indyVdr,
networks: [indyNetworkConfig],
}),
dids: new DidsModule({
resolvers: [new IndyVdrSovDidResolver()],
}),
askar: new AskarModule(),
askar: new AskarModule({
ariesAskar,
}),
} as const
}

Expand Down
4 changes: 2 additions & 2 deletions packages/anoncreds-rs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
"dependencies": {
"@aries-framework/core": "0.3.3",
"@aries-framework/anoncreds": "0.3.3",
"@hyperledger/anoncreds-shared": "^0.1.0-dev.10",
"@hyperledger/anoncreds-shared": "^0.1.0-dev.11",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"rxjs": "^7.2.0",
"tsyringe": "^4.7.0"
},
"devDependencies": {
"@hyperledger/anoncreds-nodejs": "^0.1.0-dev.10",
"@hyperledger/anoncreds-nodejs": "^0.1.0-dev.11",
"reflect-metadata": "^0.1.13",
"rimraf": "^4.0.7",
"typescript": "~4.9.4"
Expand Down
19 changes: 9 additions & 10 deletions packages/anoncreds-rs/src/AnonCredsRsModule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AnonCredsRsModuleConfigOptions } from './AnonCredsRsModuleConfig'
import type { DependencyManager, Module } from '@aries-framework/core'

import {
Expand All @@ -6,20 +7,18 @@ import {
AnonCredsVerifierServiceSymbol,
} from '@aries-framework/anoncreds'

import { AnonCredsRsModuleConfig } from './AnonCredsRsModuleConfig'
import { AnonCredsRsHolderService, AnonCredsRsIssuerService, AnonCredsRsVerifierService } from './services'

export class AnonCredsRsModule implements Module {
public readonly config: AnonCredsRsModuleConfig

public constructor(config: AnonCredsRsModuleConfigOptions) {
this.config = new AnonCredsRsModuleConfig(config)
}

public register(dependencyManager: DependencyManager) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
require('@hyperledger/anoncreds-nodejs')
} catch (error) {
try {
require('@hyperledger/anoncreds-react-native')
} catch (error) {
throw new Error('Could not load anoncreds bindings')
}
}
dependencyManager.registerInstance(AnonCredsRsModuleConfig, this.config)

// Register services
dependencyManager.registerSingleton(AnonCredsHolderServiceSymbol, AnonCredsRsHolderService)
Expand Down
58 changes: 58 additions & 0 deletions packages/anoncreds-rs/src/AnonCredsRsModuleConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { Anoncreds } from '@hyperledger/anoncreds-shared'

/**
* @public
* AnonCredsRsModuleConfigOptions defines the interface for the options of the AnonCredsRsModuleConfig class.
*/
export interface AnonCredsRsModuleConfigOptions {
/**
*
* ## Node.JS
*
* ```ts
* import { anoncreds } from '@hyperledger/anoncreds-nodejs'
*
* const agent = new Agent({
* config: {},
* dependencies: agentDependencies,
* modules: {
* anoncredsRs: new AnoncredsRsModule({
* anoncreds,
* })
* }
* })
* ```
*
* ## React Native
*
* ```ts
* import { anoncreds } from '@hyperledger/anoncreds-react-native'
*
* const agent = new Agent({
* config: {},
* dependencies: agentDependencies,
* modules: {
* anoncredsRs: new AnoncredsRsModule({
* anoncreds,
* })
* }
* })
* ```
*/
anoncreds: Anoncreds
}

/**
* @public
*/
export class AnonCredsRsModuleConfig {
private options: AnonCredsRsModuleConfigOptions

public constructor(options: AnonCredsRsModuleConfigOptions) {
this.options = options
}

public get anoncreds() {
return this.options.anoncreds
}
}
4 changes: 0 additions & 4 deletions packages/anoncreds-rs/src/types.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/anoncreds/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"devDependencies": {
"@aries-framework/node": "0.3.3",
"@hyperledger/anoncreds-nodejs": "^0.1.0-dev.11",
"indy-sdk": "^1.16.0-dev-1636",
"rimraf": "^4.0.7",
"rxjs": "^7.8.0",
Expand Down
23 changes: 7 additions & 16 deletions packages/anoncreds/tests/legacyAnonCredsSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ import {
V2ProofProtocol,
DidsModule,
} from '@aries-framework/core'
import { anoncreds } from '@hyperledger/anoncreds-nodejs'
import { randomUUID } from 'crypto'

import { AnonCredsRsModule } from '../../anoncreds-rs/src'
import { AskarModule } from '../../askar/src'
import { askarModuleConfig } from '../../askar/tests/helpers'
import { sleep } from '../../core/src/utils/sleep'
import { uuid } from '../../core/src/utils/uuid'
import { setupSubjectTransports, setupEventReplaySubjects } from '../../core/tests'
import {
getAgentOptions,
importExistingIndyDidFromPrivateKey,
makeConnection,
publicDidSeed,
genesisTransactions,
taaVersion,
taaAcceptanceMechanism,
waitForCredentialRecordSubject,
waitForProofExchangeRecordSubject,
} from '../../core/tests/helpers'
Expand All @@ -67,6 +65,7 @@ import {
IndyVdrIndyDidResolver,
IndyVdrIndyDidRegistrar,
} from '../../indy-vdr/src'
import { indyVdrModuleConfig } from '../../indy-vdr/tests/helpers'
import {
V1CredentialProtocol,
V1ProofProtocol,
Expand Down Expand Up @@ -130,14 +129,6 @@ export const getAskarAnonCredsIndyModules = ({
const legacyIndyCredentialFormatService = new LegacyIndyCredentialFormatService()
const legacyIndyProofFormatService = new LegacyIndyProofFormatService()

const indyNetworkConfig = {
id: `localhost-${uuid()}`,
isProduction: false,
genesisTransactions,
indyNamespace: 'pool:localtest',
transactionAuthorAgreement: { version: taaVersion, acceptanceMechanism: taaAcceptanceMechanism },
}

const modules = {
credentials: new CredentialsModule({
autoAcceptCredentials,
Expand All @@ -164,15 +155,15 @@ export const getAskarAnonCredsIndyModules = ({
anoncreds: new AnonCredsModule({
registries: [new IndyVdrAnonCredsRegistry()],
}),
anoncredsRs: new AnonCredsRsModule(),
indyVdr: new IndyVdrModule({
networks: [indyNetworkConfig],
anoncredsRs: new AnonCredsRsModule({
anoncreds,
}),
indyVdr: new IndyVdrModule(indyVdrModuleConfig),
dids: new DidsModule({
resolvers: [new IndyVdrSovDidResolver(), new IndyVdrIndyDidResolver()],
registrars: [new IndyVdrIndyDidRegistrar()],
}),
askar: new AskarModule(),
askar: new AskarModule(askarModuleConfig),
cache: new CacheModule({
cache: new InMemoryLruCache({ limit: 100 }),
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/askar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"dependencies": {
"@aries-framework/core": "0.3.3",
"@hyperledger/aries-askar-shared": "^0.1.0-dev.5",
"@hyperledger/aries-askar-shared": "^0.1.0-dev.6",
"bn.js": "^5.2.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand All @@ -34,7 +34,7 @@
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@hyperledger/aries-askar-nodejs": "^0.1.0-dev.5",
"@hyperledger/aries-askar-nodejs": "^0.1.0-dev.6",
"reflect-metadata": "^0.1.13",
"rimraf": "^4.0.7",
"typescript": "~4.9.4"
Expand Down
19 changes: 9 additions & 10 deletions packages/askar/src/AskarModule.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import type { AskarModuleConfigOptions } from './AskarModuleConfig'
import type { DependencyManager, Module } from '@aries-framework/core'

import { AriesFrameworkError, InjectionSymbols } from '@aries-framework/core'

import { AskarModuleConfig } from './AskarModuleConfig'
import { AskarStorageService } from './storage'
import { AskarWallet } from './wallet'

export class AskarModule implements Module {
public readonly config: AskarModuleConfig

public constructor(config: AskarModuleConfigOptions) {
this.config = new AskarModuleConfig(config)
}

public register(dependencyManager: DependencyManager) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
require('@hyperledger/aries-askar-nodejs')
} catch (error) {
try {
require('@hyperledger/aries-askar-react-native')
} catch (error) {
throw new Error('Could not load aries-askar bindings')
}
}
dependencyManager.registerInstance(AskarModuleConfig, this.config)

if (dependencyManager.isRegistered(InjectionSymbols.Wallet)) {
throw new AriesFrameworkError('There is an instance of Wallet already registered')
Expand Down
54 changes: 54 additions & 0 deletions packages/askar/src/AskarModuleConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { AriesAskar } from '@hyperledger/aries-askar-shared'

export interface AskarModuleConfigOptions {
/**
*
* ## Node.JS
*
* ```ts
* import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
*
* const agent = new Agent({
* config: {},
* dependencies: agentDependencies,
* modules: {
* ariesAskar: new AskarModule({
* ariesAskar,
* })
* }
* })
* ```
*
* ## React Native
*
* ```ts
* import { ariesAskar } from '@hyperledger/aries-askar-react-native'
*
* const agent = new Agent({
* config: {},
* dependencies: agentDependencies,
* modules: {
* ariesAskar: new AskarModule({
* ariesAskar,
* })
* }
* })
* ```
*/
ariesAskar: AriesAskar
}

/**
* @public
*/
export class AskarModuleConfig {
private options: AskarModuleConfigOptions

public constructor(options: AskarModuleConfigOptions) {
this.options = options
}

public get ariesAskar() {
return this.options.ariesAskar
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
RecordDuplicateError,
RecordNotFoundError,
} from '@aries-framework/core'
import { ariesAskar } from '@hyperledger/aries-askar-shared'
import { ariesAskar } from '@hyperledger/aries-askar-nodejs'

import { describeRunInNodeVersion } from '../../../../../tests/runInVersion'
import { TestRecord } from '../../../../core/src/storage/__tests__/TestRecord'
Expand All @@ -30,8 +30,7 @@ describeRunInNodeVersion([18], 'AskarStorageService', () => {
wallet,
agentConfig,
})
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.createAndOpen(agentConfig.walletConfig!)
await wallet.createAndOpen(agentConfig.walletConfig)
storageService = new AskarStorageService<TestRecord>()
})

Expand Down
3 changes: 0 additions & 3 deletions packages/askar/src/types.ts

This file was deleted.

8 changes: 6 additions & 2 deletions packages/askar/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import type { AskarWalletPostgresStorageConfig } from '../src/wallet'
import type { InitConfig } from '@aries-framework/core'

import { LogLevel } from '@aries-framework/core'
import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
import path from 'path'

import { TestLogger } from '../../core/tests/logger'
import { agentDependencies } from '../../node/src'
import { AskarModule } from '../src/AskarModule'
import { AskarModuleConfig } from '../src/AskarModuleConfig'

export const askarModuleConfig = new AskarModuleConfig({ ariesAskar })

export const genesisPath = process.env.GENESIS_TXN_PATH
? path.resolve(process.env.GENESIS_TXN_PATH)
Expand Down Expand Up @@ -34,7 +38,7 @@ export function getPostgresAgentOptions(
return {
config,
dependencies: agentDependencies,
modules: { askar: new AskarModule() },
modules: { askar: new AskarModule(askarModuleConfig) },
} as const
}

Expand All @@ -54,6 +58,6 @@ export function getSqliteAgentOptions(name: string, extraConfig: Partial<InitCon
return {
config,
dependencies: agentDependencies,
modules: { askar: new AskarModule() },
modules: { askar: new AskarModule(askarModuleConfig) },
} as const
}
Loading

0 comments on commit 343ce6a

Please sign in to comment.