From 4a158e64b97595be0733d4277c28c462bd47c908 Mon Sep 17 00:00:00 2001 From: Ariel Gentile Date: Fri, 21 Jul 2023 09:46:36 -0300 Subject: [PATCH] fix(askar): in memory wallet creation (#1498) Signed-off-by: Ariel Gentile --- packages/askar/src/wallet/AskarWallet.ts | 6 +- .../askar/tests/askar-inmemory.e2e.test.ts | 66 +++++++++++++++++++ .../askar/tests/askar-postgres.e2e.test.ts | 41 ++---------- packages/askar/tests/helpers.ts | 47 +++++++++++-- 4 files changed, 118 insertions(+), 42 deletions(-) create mode 100644 packages/askar/tests/askar-inmemory.e2e.test.ts diff --git a/packages/askar/src/wallet/AskarWallet.ts b/packages/askar/src/wallet/AskarWallet.ts index 06d927ec38..89f429bee4 100644 --- a/packages/askar/src/wallet/AskarWallet.ts +++ b/packages/askar/src/wallet/AskarWallet.ts @@ -240,7 +240,11 @@ export class AskarWallet implements Wallet { this.walletConfig = walletConfig } catch (error) { - if (isAskarError(error) && error.code === AskarErrorCode.NotFound) { + if ( + isAskarError(error) && + (error.code === AskarErrorCode.NotFound || + (error.code === AskarErrorCode.Backend && walletConfig.storage?.inMemory)) + ) { const errorMessage = `Wallet '${walletConfig.id}' not found` this.logger.debug(errorMessage) diff --git a/packages/askar/tests/askar-inmemory.e2e.test.ts b/packages/askar/tests/askar-inmemory.e2e.test.ts new file mode 100644 index 0000000000..2261fa7efb --- /dev/null +++ b/packages/askar/tests/askar-inmemory.e2e.test.ts @@ -0,0 +1,66 @@ +/* eslint-disable @typescript-eslint/no-non-null-assertion */ +import type { SubjectMessage } from '../../../tests/transport/SubjectInboundTransport' + +import { Agent } from '@aries-framework/core' +import { Subject } from 'rxjs' + +import { describeRunInNodeVersion } from '../../../tests/runInVersion' +import { SubjectInboundTransport } from '../../../tests/transport/SubjectInboundTransport' +import { SubjectOutboundTransport } from '../../../tests/transport/SubjectOutboundTransport' + +import { e2eTest, getSqliteAgentOptions } from './helpers' + +const aliceInMemoryAgentOptions = getSqliteAgentOptions( + 'AgentsAlice', + { + endpoints: ['rxjs:alice'], + }, + true +) +const bobInMemoryAgentOptions = getSqliteAgentOptions( + 'AgentsBob', + { + endpoints: ['rxjs:bob'], + }, + true +) + +// FIXME: Re-include in tests when Askar NodeJS wrapper performance is improved +describeRunInNodeVersion([18], 'Askar In Memory agents', () => { + let aliceAgent: Agent + let bobAgent: Agent + + afterAll(async () => { + if (bobAgent) { + await bobAgent.shutdown() + await bobAgent.wallet.delete() + } + + if (aliceAgent) { + await aliceAgent.shutdown() + await aliceAgent.wallet.delete() + } + }) + + test('In memory Askar wallets E2E test', async () => { + const aliceMessages = new Subject() + const bobMessages = new Subject() + + const subjectMap = { + 'rxjs:alice': aliceMessages, + 'rxjs:bob': bobMessages, + } + + aliceAgent = new Agent(aliceInMemoryAgentOptions) + aliceAgent.registerInboundTransport(new SubjectInboundTransport(aliceMessages)) + aliceAgent.registerOutboundTransport(new SubjectOutboundTransport(subjectMap)) + await aliceAgent.initialize() + + bobAgent = new Agent(bobInMemoryAgentOptions) + bobAgent.registerInboundTransport(new SubjectInboundTransport(bobMessages)) + bobAgent.registerOutboundTransport(new SubjectOutboundTransport(subjectMap)) + await bobAgent.initialize() + + await e2eTest(aliceAgent, bobAgent) + }) +}) diff --git a/packages/askar/tests/askar-postgres.e2e.test.ts b/packages/askar/tests/askar-postgres.e2e.test.ts index c25f6d16c3..14219b6515 100644 --- a/packages/askar/tests/askar-postgres.e2e.test.ts +++ b/packages/askar/tests/askar-postgres.e2e.test.ts @@ -1,17 +1,15 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ import type { SubjectMessage } from '../../../tests/transport/SubjectInboundTransport' import type { AskarWalletPostgresStorageConfig } from '../src/wallet' -import type { ConnectionRecord } from '@aries-framework/core' -import { Agent, HandshakeProtocol } from '@aries-framework/core' +import { Agent } from '@aries-framework/core' import { Subject } from 'rxjs' import { describeRunInNodeVersion } from '../../../tests/runInVersion' import { SubjectInboundTransport } from '../../../tests/transport/SubjectInboundTransport' import { SubjectOutboundTransport } from '../../../tests/transport/SubjectOutboundTransport' -import { waitForBasicMessage } from '../../core/tests/helpers' -import { getPostgresAgentOptions } from './helpers' +import { e2eTest, getPostgresAgentOptions } from './helpers' const storageConfig: AskarWalletPostgresStorageConfig = { type: 'postgres', @@ -35,8 +33,6 @@ const bobPostgresAgentOptions = getPostgresAgentOptions('AgentsBob', storageConf describeRunInNodeVersion([18], 'Askar Postgres agents', () => { let aliceAgent: Agent let bobAgent: Agent - let aliceConnection: ConnectionRecord - let bobConnection: ConnectionRecord afterAll(async () => { if (bobAgent) { @@ -50,7 +46,7 @@ describeRunInNodeVersion([18], 'Askar Postgres agents', () => { } }) - test('make a connection between postgres agents', async () => { + test('Postgres Askar wallets E2E test', async () => { const aliceMessages = new Subject() const bobMessages = new Subject() @@ -69,35 +65,6 @@ describeRunInNodeVersion([18], 'Askar Postgres agents', () => { bobAgent.registerOutboundTransport(new SubjectOutboundTransport(subjectMap)) await bobAgent.initialize() - const aliceBobOutOfBandRecord = await aliceAgent.oob.createInvitation({ - handshakeProtocols: [HandshakeProtocol.Connections], - }) - - const { connectionRecord: bobConnectionAtBobAlice } = await bobAgent.oob.receiveInvitation( - aliceBobOutOfBandRecord.outOfBandInvitation - ) - bobConnection = await bobAgent.connections.returnWhenIsConnected(bobConnectionAtBobAlice!.id) - - const [aliceConnectionAtAliceBob] = await aliceAgent.connections.findAllByOutOfBandId(aliceBobOutOfBandRecord.id) - aliceConnection = await aliceAgent.connections.returnWhenIsConnected(aliceConnectionAtAliceBob!.id) - }) - - test('send a message to connection', async () => { - const message = 'hello, world' - await aliceAgent.basicMessages.sendMessage(aliceConnection.id, message) - - const basicMessage = await waitForBasicMessage(bobAgent, { - content: message, - }) - - expect(basicMessage.content).toBe(message) - }) - - test('can shutdown and re-initialize the same postgres agent', async () => { - expect(aliceAgent.isInitialized).toBe(true) - await aliceAgent.shutdown() - expect(aliceAgent.isInitialized).toBe(false) - await aliceAgent.initialize() - expect(aliceAgent.isInitialized).toBe(true) + await e2eTest(aliceAgent, bobAgent) }) }) diff --git a/packages/askar/tests/helpers.ts b/packages/askar/tests/helpers.ts index 8a71cd9bff..e02e5cd542 100644 --- a/packages/askar/tests/helpers.ts +++ b/packages/askar/tests/helpers.ts @@ -1,11 +1,12 @@ import type { AskarWalletPostgresStorageConfig } from '../src/wallet' -import type { InitConfig } from '@aries-framework/core' +import type { Agent, InitConfig } from '@aries-framework/core' -import { ConnectionsModule, LogLevel, utils } from '@aries-framework/core' +import { ConnectionsModule, HandshakeProtocol, LogLevel, utils } from '@aries-framework/core' import { ariesAskar } from '@hyperledger/aries-askar-nodejs' import { registerAriesAskar } from '@hyperledger/aries-askar-shared' import path from 'path' +import { waitForBasicMessage } from '../../core/tests/helpers' import { TestLogger } from '../../core/tests/logger' import { agentDependencies } from '../../node/src' import { AskarModule } from '../src/AskarModule' @@ -54,14 +55,14 @@ export function getPostgresAgentOptions( } as const } -export function getSqliteAgentOptions(name: string, extraConfig: Partial = {}) { +export function getSqliteAgentOptions(name: string, extraConfig: Partial = {}, inMemory?: boolean) { const random = utils.uuid().slice(0, 4) const config: InitConfig = { label: `SQLiteAgent: ${name} - ${random}`, walletConfig: { id: `SQLiteWallet${name} - ${random}`, key: `Key${name}`, - storage: { type: 'sqlite' }, + storage: { type: 'sqlite', inMemory }, }, autoUpdateStorageOnStartup: false, logger: new TestLogger(LogLevel.off, name), @@ -78,3 +79,41 @@ export function getSqliteAgentOptions(name: string, extraConfig: Partial