diff --git a/integration-tests/e2e-tests/src/abilities/WalletSdk.ts b/integration-tests/e2e-tests/src/abilities/WalletSdk.ts index 4f67b222f..b49311fdc 100644 --- a/integration-tests/e2e-tests/src/abilities/WalletSdk.ts +++ b/integration-tests/e2e-tests/src/abilities/WalletSdk.ts @@ -1,11 +1,11 @@ import { Ability, Discardable, Initialisable, Interaction, Question, QuestionAdapter } from "@serenity-js/core" -import SDK from "@hyperledger/identus-edge-agent-sdk" +import type SDK from "@hyperledger/identus-edge-agent-sdk" import axios from "axios" import { CloudAgentConfiguration } from "../configuration/CloudAgentConfiguration" import InMemoryStore from "../configuration/inmemory" import { randomUUID, UUID } from "crypto" - +let instance: typeof import("@hyperledger/identus-edge-agent-sdk").default; // fallback in any case of dangling sdk agents export const agentList: Map = new Map() @@ -15,6 +15,8 @@ export class WalletSdk extends Ability implements Initialisable, Discardable { messages: MessageQueue = new MessageQueue() id: UUID = randomUUID() + + static async withANewInstance(): Promise { return new WalletSdk() } @@ -75,6 +77,11 @@ export class WalletSdk extends Ability implements Initialisable, Discardable { }) } + static async loadSDK() { + instance ??= require("@hyperledger/identus-edge-agent-sdk"); + return instance + } + async discard(): Promise { agentList.delete(this.id) if (this.isInitialised()) { @@ -84,22 +91,29 @@ export class WalletSdk extends Ability implements Initialisable, Discardable { } async createSdk(seed: SDK.Domain.Seed = undefined) { - const { Agent, Apollo, Domain, ListenerKey, } = SDK + const { + Agent, + Apollo, + Domain, + ListenerKey, + Store, + Pluto + } = await WalletSdk.loadSDK() const apollo = new Apollo() - this.store = new SDK.Store({ + this.store = new Store({ name: [...Array(30)].map(() => Math.random().toString(36)[2]).join(""), storage: InMemoryStore, password: "random12434", ignoreDuplicate: true }) - const pluto = new SDK.Pluto(this.store, apollo) + const pluto = new Pluto(this.store, apollo) const mediatorDID = Domain.DID.fromString(await WalletSdk.getMediatorDidThroughOob()) this.sdk = Agent.initialize({ seed, apollo, pluto, mediatorDID }) this.sdk.addListener( - ListenerKey.MESSAGE, (messages: SDK.Domain.Message[]) => { + ListenerKey.MESSAGE, async (messages: SDK.Domain.Message[]) => { for (const message of messages) { - this.messages.enqueue(message) + await this.messages.enqueue(message) } } ) @@ -139,12 +153,12 @@ class MessageQueue { receivedMessages: string[] = [] - enqueue(message: SDK.Domain.Message) { + async enqueue(message: SDK.Domain.Message) { this.queue.push(message) // auto start processing messages if (!this.processingId) { - this.processMessages() + await this.processMessages() } } @@ -162,7 +176,8 @@ class MessageQueue { return this.queue.length } - processMessages() { + async processMessages() { + const SDK = await WalletSdk.loadSDK(); this.processingId = setInterval(() => { if (!this.isEmpty()) { const message: SDK.Domain.Message = this.dequeue() @@ -175,7 +190,6 @@ class MessageQueue { this.receivedMessages.push(message.id) - if (piUri === SDK.ProtocolType.DidcommOfferCredential) { this.credentialOfferStack.push(message) } else if (piUri === SDK.ProtocolType.DidcommRequestPresentation) { diff --git a/integration-tests/e2e-tests/src/steps/EdgeAgentSteps.ts b/integration-tests/e2e-tests/src/steps/EdgeAgentSteps.ts index 70f3fb0c7..9575f8b24 100644 --- a/integration-tests/e2e-tests/src/steps/EdgeAgentSteps.ts +++ b/integration-tests/e2e-tests/src/steps/EdgeAgentSteps.ts @@ -3,7 +3,6 @@ import { Actor, Notepad } from "@serenity-js/core" import { EdgeAgentWorkflow } from "../workflow/EdgeAgentWorkflow" import { CloudAgentWorkflow } from "../workflow/CloudAgentWorkflow" import { Utils } from "../Utils" -import SDK from "@hyperledger/identus-edge-agent-sdk" Given("{actor} has '{int}' jwt credentials issued by {actor}", async function (edgeAgent: Actor, numberOfIssuedCredentials: number, cloudAgent: Actor) { @@ -180,6 +179,7 @@ Then("{actor} is dismissed", Then("{actor} will request {actor} to verify the anonymous credential", async function (verifierEdgeAgent: Actor, holderEdgeAgent: Actor) { + const SDK = await EdgeAgentWorkflow.instance; await EdgeAgentWorkflow.createPeerDids(holderEdgeAgent, 1) const holderDID = await holderEdgeAgent.answer(Notepad.notes().get("lastPeerDID")) const claims = { @@ -197,6 +197,7 @@ Then("{actor} will request {actor} to verify the anonymous credential", Then("{actor} will request {actor} to verify the JWT credential", async function (verifierEdgeAgent: Actor, holderEdgeAgent: Actor) { + const SDK = await EdgeAgentWorkflow.instance; await EdgeAgentWorkflow.createPeerDids(holderEdgeAgent, 1) const holderDID = await holderEdgeAgent.answer(Notepad.notes().get("lastPeerDID")) const claims = { diff --git a/integration-tests/e2e-tests/src/workflow/EdgeAgentWorkflow.ts b/integration-tests/e2e-tests/src/workflow/EdgeAgentWorkflow.ts index f4b334c32..8784e816a 100644 --- a/integration-tests/e2e-tests/src/workflow/EdgeAgentWorkflow.ts +++ b/integration-tests/e2e-tests/src/workflow/EdgeAgentWorkflow.ts @@ -1,4 +1,4 @@ -import SDK from "@hyperledger/identus-edge-agent-sdk" +import type SDK from "@hyperledger/identus-edge-agent-sdk" import { Actor, Duration, Notepad, TakeNotes, Wait } from "@serenity-js/core" import { Ensure, equals } from "@serenity-js/assertions" import { WalletSdk } from "../abilities/WalletSdk" @@ -9,6 +9,9 @@ import { assert } from "chai" export class EdgeAgentWorkflow { + static get instance() { + return WalletSdk.loadSDK() + } static async connect(edgeAgent: Actor) { const url = await edgeAgent.answer(Notepad.notes().get("invitation")) @@ -39,7 +42,7 @@ export class EdgeAgentWorkflow { } static async processIssuedCredential(edgeAgent: Actor, recordId: string) { - const { IssueCredential } = SDK; + const { IssueCredential } = await this.instance; await edgeAgent.attemptsTo( WalletSdk.execute(async (sdk, messages) => { const issuedCredential = messages.issuedCredentialStack.shift()! @@ -51,7 +54,7 @@ export class EdgeAgentWorkflow { } static async acceptCredential(edgeAgent: Actor) { - const { OfferCredential } = SDK + const { OfferCredential } = await this.instance; await edgeAgent.attemptsTo( WalletSdk.execute(async (sdk, messages) => { @@ -77,7 +80,7 @@ export class EdgeAgentWorkflow { } static async presentVerificationRequest(edgeAgent: Actor) { - const { RequestPresentation } = SDK + const { RequestPresentation } = await this.instance; await edgeAgent.attemptsTo( WalletSdk.execute(async (sdk, messages) => { @@ -100,7 +103,7 @@ export class EdgeAgentWorkflow { } static async verifyPresentation(edgeAgent: Actor, expected: boolean = true) { - const { Presentation } = SDK + const { Presentation } = await this.instance; await edgeAgent.attemptsTo( WalletSdk.execute(async (sdk, messages) => { @@ -123,7 +126,7 @@ export class EdgeAgentWorkflow { } static async tryToPresentVerificationRequestWithWrongAnoncred(edgeAgent: Actor) { - const { RequestPresentation } = SDK + const { RequestPresentation } = await this.instance; await edgeAgent.attemptsTo( WalletSdk.execute(async (sdk, messages) => { const credentials = await sdk.verifiableCredentials() @@ -142,7 +145,7 @@ export class EdgeAgentWorkflow { } static async presentProof(edgeAgent: Actor) { - const { RequestPresentation } = SDK + const { RequestPresentation } = await this.instance; await edgeAgent.attemptsTo( WalletSdk.execute(async (sdk, messages) => { @@ -301,6 +304,7 @@ export class EdgeAgentWorkflow { } static async createNewWalletFromBackupWithWrongSeed(edgeAgent: Actor) { + const SDK = await this.instance; const backup = await edgeAgent.answer(Notepad.notes().get("backup")) const walletSdk = new WalletSdk() const seed = new SDK.Apollo().createRandomSeed().seed diff --git a/src/mercury/didcomm/Wrapper.ts b/src/mercury/didcomm/Wrapper.ts index c133a8e11..58126f321 100644 --- a/src/mercury/didcomm/Wrapper.ts +++ b/src/mercury/didcomm/Wrapper.ts @@ -7,7 +7,7 @@ import type { Attachment, AttachmentData, } from "didcomm-wasm"; -import wasmBuffer from "../../../externals/generated/didcomm-wasm/didcomm_js_bg.wasm" +import wasmBuffer from "didcomm-wasm/didcomm_js_bg.wasm" import * as Domain from "../../domain"; import { DIDCommDIDResolver } from "./DIDResolver"; @@ -34,7 +34,7 @@ export class DIDCommWrapper implements DIDCommProtocol { public static async getDIDComm() { this.didcomm ??= await import("didcomm-wasm").then(async module => { - const wasmInstance = module.initSync(wasmBuffer); + const wasmInstance = module.initSync({ module: wasmBuffer }); await module.default(wasmInstance); return module; }); diff --git a/src/pollux/AnoncredsLoader.ts b/src/pollux/AnoncredsLoader.ts index 15c6277a2..6ba51e96b 100644 --- a/src/pollux/AnoncredsLoader.ts +++ b/src/pollux/AnoncredsLoader.ts @@ -1,6 +1,9 @@ // import { Anoncreds } from "../domain/models/Anoncreds"; import type * as Anoncreds from "anoncreds-wasm"; + + +import wasmBuffer from 'anoncreds-wasm/anoncreds_wasm_bg.wasm'; /** * @class AnoncredsLoader * handle loading and access of anoncreds library @@ -21,18 +24,8 @@ export class AnoncredsLoader { private async load() { this.pkg ??= await import("anoncreds-wasm").then(async module => { - - let wasmBuffer: Buffer; - if (typeof window !== 'undefined') { - const wasmModule = await import("../../externals/generated/anoncreds-wasm/anoncreds_wasm_bg.wasm") - wasmBuffer = wasmModule.default as any - module.initSync(wasmBuffer); - - } else { - const wasmModule = await import("../../externals/generated/anoncreds-wasm/anoncreds_wasm_bg.wasm") - wasmBuffer = wasmModule.default as any - module.initSync(wasmBuffer); - } + const wasmInstance = module.initSync({ module: wasmBuffer }); + await module.default(wasmInstance); return module }); } diff --git a/src/pollux/Pollux.ts b/src/pollux/Pollux.ts index 7518b2ab7..ef1d02de4 100644 --- a/src/pollux/Pollux.ts +++ b/src/pollux/Pollux.ts @@ -838,7 +838,7 @@ export default class Pollux implements IPollux { async start() { this._anoncreds = await AnoncredsLoader.getInstance(); this._jwe ??= await import("jwe-wasm").then(async module => { - const wasmInstance = module.initSync(wasmBuffer); + const wasmInstance = module.initSync({ module: wasmBuffer }); await module.default(wasmInstance); return module; });