Skip to content

Commit

Permalink
fix: e2e cucumber import (#291)
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Javier Ribo Labrador <elribonazo@gmail.com>
  • Loading branch information
elribonazo authored Sep 17, 2024
1 parent bcbd355 commit e569d79
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 34 deletions.
36 changes: 25 additions & 11 deletions integration-tests/e2e-tests/src/abilities/WalletSdk.ts
Original file line number Diff line number Diff line change
@@ -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<string, WalletSdk> = new Map()

Expand All @@ -15,6 +15,8 @@ export class WalletSdk extends Ability implements Initialisable, Discardable {
messages: MessageQueue = new MessageQueue()
id: UUID = randomUUID()



static async withANewInstance(): Promise<Ability> {
return new WalletSdk()
}
Expand Down Expand Up @@ -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<void> {
agentList.delete(this.id)
if (this.isInitialised()) {
Expand All @@ -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)
}
}
)
Expand Down Expand Up @@ -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()
}
}

Expand All @@ -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()
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/e2e-tests/src/steps/EdgeAgentSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
18 changes: 11 additions & 7 deletions integration-tests/e2e-tests/src/workflow/EdgeAgentWorkflow.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<string>(Notepad.notes().get("invitation"))
Expand Down Expand Up @@ -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()!
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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()
Expand All @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/mercury/didcomm/Wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
});
Expand Down
17 changes: 5 additions & 12 deletions src/pollux/AnoncredsLoader.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/pollux/Pollux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down

1 comment on commit e569d79

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 77%
78.15% (3428/4386) 67.21% (1507/2242) 81.73% (828/1013)

Please sign in to comment.