Skip to content

Commit

Permalink
fix: incompatibility with cucumberjs loading imported package
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 committed Sep 17, 2024
1 parent 263e422 commit 8357e88
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 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

0 comments on commit 8357e88

Please sign in to comment.