diff --git a/integration-tests/e2e-tests/tests/sdk/EnvironmentVariables.ts b/integration-tests/e2e-tests/tests/sdk/EnvironmentVariables.ts index 389b6ae64..d65a8a7d4 100644 --- a/integration-tests/e2e-tests/tests/sdk/EnvironmentVariables.ts +++ b/integration-tests/e2e-tests/tests/sdk/EnvironmentVariables.ts @@ -3,7 +3,7 @@ import {configDotenv} from "dotenv" configDotenv() export class EnvironmentVariables { - public static oobUrl: string = process.env.MEDIATOR_OOB_URL! + public static mediatorOobUrl: string = process.env.MEDIATOR_OOB_URL! public static agentUrl: string = process.env.PRISM_AGENT_URL! public static publishedDid: string = process.env.PUBLISHED_DID! public static schemaId: string = process.env.SCHEMA_ID! diff --git a/integration-tests/e2e-tests/tests/sdk/WalletSdk.ts b/integration-tests/e2e-tests/tests/sdk/WalletSdk.ts index d7c1946f0..511737bc8 100644 --- a/integration-tests/e2e-tests/tests/sdk/WalletSdk.ts +++ b/integration-tests/e2e-tests/tests/sdk/WalletSdk.ts @@ -18,10 +18,7 @@ import {PlutoInMemory} from "./src/PlutoInMemory" export class WalletSdk extends Ability implements Initialisable, Discardable { sdk!: Agent - credentialOfferStack: Message[] = [] - proofRequestStack: Message[] = [] - issuedCredentialStack: Message[] = [] - receivedMessages: string[] = [] + messages: MessageQueue = new MessageQueue() static async withANewInstance(): Promise { return new WalletSdk(await WalletSdkBuilder.createInstance()) @@ -34,19 +31,19 @@ export class WalletSdk extends Ability implements Initialisable, Discardable { static credentialOfferStackSize(): QuestionAdapter { return Question.about("credential offer stack", actor => { - return WalletSdk.as(actor).credentialOfferStack.length + return WalletSdk.as(actor).messages.credentialOfferStack.length }) } static issuedCredentialStackSize(): QuestionAdapter { return Question.about("issued credential stack", actor => { - return WalletSdk.as(actor).issuedCredentialStack.length + return WalletSdk.as(actor).messages.issuedCredentialStack.length }) } static proofOfRequestStackSize(): QuestionAdapter { return Question.about("proof of request stack", actor => { - return WalletSdk.as(actor).proofRequestStack.length + return WalletSdk.as(actor).messages.proofRequestStack.length }) } @@ -57,9 +54,9 @@ export class WalletSdk extends Ability implements Initialisable, Discardable { }) => Promise): Interaction { return Interaction.where("#actor uses wallet sdk", async actor => { await callback(WalletSdk.as(actor).sdk, { - credentialOfferStack: WalletSdk.as(actor).credentialOfferStack, - issuedCredentialStack: WalletSdk.as(actor).issuedCredentialStack, - proofRequestStack: WalletSdk.as(actor).proofRequestStack + credentialOfferStack: WalletSdk.as(actor).messages.credentialOfferStack, + issuedCredentialStack: WalletSdk.as(actor).messages.issuedCredentialStack, + proofRequestStack: WalletSdk.as(actor).messages.proofRequestStack }) }) } @@ -69,28 +66,15 @@ export class WalletSdk extends Ability implements Initialisable, Discardable { } async initialise(): Promise { - await this.sdk.start() - this.sdk.addListener( - ListenerKey.MESSAGE, - async (messages: Domain.Message[]) => { + ListenerKey.MESSAGE, (messages: Domain.Message[]) => { for (const message of messages) { - // checks if sdk already received message - if (this.receivedMessages.includes(message.id)) { - return - } - - this.receivedMessages.push(message.id) - if (message.piuri.includes("/offer-credential")) { - this.credentialOfferStack.push(message) - } else if (message.piuri.includes("/present-proof")) { - this.proofRequestStack.push(message) - } else if (message.piuri.includes("/issue-credential")) { - this.issuedCredentialStack.push(message) - } + this.messages.enqueue(message) } } ) + + await this.sdk.start() } isInitialised(): boolean { @@ -100,7 +84,7 @@ export class WalletSdk extends Ability implements Initialisable, Discardable { class WalletSdkBuilder { private static async getMediatorDidThroughOob(): Promise { - const response = await axios.get(EnvironmentVariables.oobUrl) + const response = await axios.get(EnvironmentVariables.mediatorOobUrl) const encodedData = response.data.split("?_oob=")[1] const oobData = JSON.parse(Buffer.from(encodedData, "base64").toString()) return oobData.from @@ -143,3 +127,63 @@ class WalletSdkBuilder { ) } } + +/** + * Helper class for message queueing processor + */ +class MessageQueue { + private processingId: NodeJS.Timeout | null = null + private queue: Message[] = [] + + credentialOfferStack: Message[] = [] + proofRequestStack: Message[] = [] + issuedCredentialStack: Message[] = [] + receivedMessages: string[] = [] + + enqueue(message: Message) { + this.queue.push(message) + + // auto start processing messages + if (!this.processingId) { + this.processMessages() + } + } + + dequeue(): Message { + return this.queue.shift()! + } + + // Check if the queue is empty + isEmpty(): boolean { + return this.queue.length === 0 + } + + // Get the number of messages in the queue + size(): number { + return this.queue.length + } + + processMessages() { + this.processingId = setInterval(() => { + if (!this.isEmpty()) { + const message: Message = this.dequeue() + // checks if sdk already received message + if (this.receivedMessages.includes(message.id)) { + return + } + + this.receivedMessages.push(message.id) + if (message.piuri.includes("/offer-credential")) { + this.credentialOfferStack.push(message) + } else if (message.piuri.includes("/present-proof")) { + this.proofRequestStack.push(message) + } else if (message.piuri.includes("/issue-credential")) { + this.issuedCredentialStack.push(message) + } + } else { + clearInterval(this.processingId!) + this.processingId = null + } + }, 50) + } +} diff --git a/integration-tests/e2e-tests/tests/sdk/configuration/CloudAgentConfiguration.ts b/integration-tests/e2e-tests/tests/sdk/configuration/CloudAgentConfiguration.ts index d9fca6f40..2d364cd2c 100644 --- a/integration-tests/e2e-tests/tests/sdk/configuration/CloudAgentConfiguration.ts +++ b/integration-tests/e2e-tests/tests/sdk/configuration/CloudAgentConfiguration.ts @@ -8,6 +8,7 @@ import { import {Utils} from "../../Utils" import {randomUUID} from "crypto" import {axiosInstance} from "../steps/LifecycleSteps" +import * as fs from "fs" export class CloudAgentConfiguration { static publishedDid: string @@ -24,6 +25,18 @@ export class CloudAgentConfiguration { await this.prepareSchema() this.isInitialized = true + + Utils.appendToNotes(`Mediator: ${EnvironmentVariables.mediatorOobUrl}`) + Utils.appendToNotes(`Agent: ${EnvironmentVariables.agentUrl}`) + Utils.appendToNotes(`DID: ${this.publishedDid}`) + Utils.appendToNotes(`Schema: ${this.schemaId}`) + Utils.appendToNotes(`SDK Version: ${this.getSdkVersion()}`) + } + + private static getSdkVersion(): string { + const file = "node_modules/@input-output-hk/atala-prism-wallet-sdk/package.json" + const json = JSON.parse(fs.readFileSync(file).toString()) + return json.version } /** @@ -37,7 +50,7 @@ export class CloudAgentConfiguration { this.publishedDid = EnvironmentVariables.publishedDid return } catch (err) { - console.warn("DID not found. Creating a new one and publishing it.") + Utils.appendToNotes("DID not found. Creating a new one and publishing it.") } const creationData = new CreateManagedDidRequest() @@ -83,8 +96,6 @@ export class CloudAgentConfiguration { } }, 1000) }) - - Utils.appendToNotes(`Created new DID: ${this.publishedDid}`) } /** @@ -98,7 +109,7 @@ export class CloudAgentConfiguration { this.schemaId = EnvironmentVariables.schemaId return } catch (err) { - console.warn("Schema not found. Creating a new one.") + Utils.appendToNotes("Schema not found. Creating a new one.") } const credentialSchemaInput = new CredentialSchemaInput() @@ -133,7 +144,6 @@ export class CloudAgentConfiguration { ) this.schemaId = schemaResponse.data.guid - - Utils.appendToNotes(`Created new schema: ${this.schemaId}`) } } + diff --git a/integration-tests/e2e-tests/tests/sdk/src/CloudAgentWorkflow.ts b/integration-tests/e2e-tests/tests/sdk/src/CloudAgentWorkflow.ts index 244c76a3a..bec323310 100644 --- a/integration-tests/e2e-tests/tests/sdk/src/CloudAgentWorkflow.ts +++ b/integration-tests/e2e-tests/tests/sdk/src/CloudAgentWorkflow.ts @@ -42,7 +42,7 @@ export class CloudAgentWorkflow { Notepad.notes().get("connectionId") ) await cloudAgent.attemptsTo( - Wait.upTo(Duration.ofSeconds(60)).until( + Wait.upTo(Duration.ofMinutes(2)).until( Questions.httpGet(`/connections/${connectionId}`), Expectations.propertyValueToBe("state", state) ) @@ -51,7 +51,7 @@ export class CloudAgentWorkflow { static async verifyCredentialState(cloudAgent: Actor, recordId: string, state: string) { await cloudAgent.attemptsTo( - Wait.upTo(Duration.ofSeconds(60)).until( + Wait.upTo(Duration.ofMinutes(2)).until( Questions.httpGet(`/issue-credentials/records/${recordId}`), Expectations.propertyValueToBe("protocolState", state) ) @@ -63,7 +63,7 @@ export class CloudAgentWorkflow { Notepad.notes().get("presentationId") ) await cloudAgent.attemptsTo( - Wait.upTo(Duration.ofSeconds(60)).until( + Wait.upTo(Duration.ofMinutes(2)).until( Questions.httpGet(`/present-proof/presentations/${presentationId}`), Expectations.propertyValueToBe("status", state) ) diff --git a/integration-tests/e2e-tests/tests/sdk/src/EdgeAgentWorkflow.ts b/integration-tests/e2e-tests/tests/sdk/src/EdgeAgentWorkflow.ts index a6b3de71a..3481c4736 100644 --- a/integration-tests/e2e-tests/tests/sdk/src/EdgeAgentWorkflow.ts +++ b/integration-tests/e2e-tests/tests/sdk/src/EdgeAgentWorkflow.ts @@ -17,7 +17,7 @@ export class EdgeAgentWorkflow { static async waitForCredentialOffer(edgeAgent: Actor, numberOfCredentialOffer: number = 1) { await edgeAgent.attemptsTo( - Wait.upTo(Duration.ofSeconds(60)).until( + Wait.upTo(Duration.ofMinutes(2)).until( WalletSdk.credentialOfferStackSize(), equals(numberOfCredentialOffer) ) @@ -26,7 +26,7 @@ export class EdgeAgentWorkflow { static async waitToReceiveCredentialIssuance(edgeAgent: Actor, expectedNumberOfCredentials: number) { await edgeAgent.attemptsTo( - Wait.upTo(Duration.ofSeconds(60)).until( + Wait.upTo(Duration.ofMinutes(2)).until( WalletSdk.issuedCredentialStackSize(), equals(expectedNumberOfCredentials) ) @@ -37,7 +37,7 @@ export class EdgeAgentWorkflow { await edgeAgent.attemptsTo( WalletSdk.execute(async (sdk, messages) => { await Utils.repeat(numberOfCredentials, async () => { - const issuedCredential = messages.issuedCredentialStack.pop()! + const issuedCredential = messages.issuedCredentialStack.shift()! const issueCredential = IssueCredential.fromMessage(issuedCredential) await sdk.processIssuedCredentialMessage(issueCredential) }) @@ -48,16 +48,13 @@ export class EdgeAgentWorkflow { static async acceptCredential(edgeAgent: Actor) { await edgeAgent.attemptsTo( WalletSdk.execute(async (sdk, messages) => { - const message = OfferCredential.fromMessage(messages.credentialOfferStack.pop()!) - const requestCredential = - await sdk.prepareRequestCredentialWithIssuer(message) + const message = OfferCredential.fromMessage(messages.credentialOfferStack.shift()!) + const requestCredential = await sdk.prepareRequestCredentialWithIssuer(message) + const requestCredentialMessage = requestCredential.makeMessage() try { - await sdk.sendMessage(requestCredential.makeMessage()) + await sdk.sendMessage(requestCredentialMessage) } catch (e) { - console.error( - "Accepting credential shouldn't throw exception", - new Error().stack?.split("\n")[1].trim(), - ) + // } }) ) @@ -65,7 +62,7 @@ export class EdgeAgentWorkflow { static async waitForProofRequest(edgeAgent: Actor) { await edgeAgent.attemptsTo( - Wait.upTo(Duration.ofSeconds(60)).until( + Wait.upTo(Duration.ofMinutes(2)).until( WalletSdk.proofOfRequestStackSize(), equals(1), ), @@ -78,7 +75,7 @@ export class EdgeAgentWorkflow { const credentials = await sdk.verifiableCredentials() const credential = credentials[0] const requestPresentationMessage = RequestPresentation.fromMessage( - messages.proofRequestStack.pop()!, + messages.proofRequestStack.shift()!, ) const presentation = await sdk.createPresentationForRequestProof( requestPresentationMessage, @@ -87,10 +84,7 @@ export class EdgeAgentWorkflow { try { await sdk.sendMessage(presentation.makeMessage()) } catch (e) { - console.error( - "Send present-proof shouldn't throw exception", - new Error().stack?.split("\n")[1].trim(), - ) + // } } ) diff --git a/integration-tests/e2e-tests/tests/sdk/steps/EdgeAgentSteps.ts b/integration-tests/e2e-tests/tests/sdk/steps/EdgeAgentSteps.ts index de3c02b70..b65c01ff2 100644 --- a/integration-tests/e2e-tests/tests/sdk/steps/EdgeAgentSteps.ts +++ b/integration-tests/e2e-tests/tests/sdk/steps/EdgeAgentSteps.ts @@ -41,7 +41,7 @@ When("{actor} accepts {int} credentials offer at once from {actor}", }) await cloudAgent.attemptsTo(Notepad.notes().set("recordIdList", recordIdList)) - await EdgeAgentWorkflow.waitForCredentialOffer(edgeAgent, 3) + await EdgeAgentWorkflow.waitForCredentialOffer(edgeAgent, numberOfCredentials) await Utils.repeat(numberOfCredentials, async () => { await EdgeAgentWorkflow.acceptCredential(edgeAgent)