From b5c2f5bed70a14f557e51af3d2d1cedb6decf5dd Mon Sep 17 00:00:00 2001 From: Francisco Javier Ribo Labrador Date: Wed, 13 Nov 2024 14:29:40 +0100 Subject: [PATCH 1/3] fix: add listener async + wait time for presentation verified Signed-off-by: Francisco Javier Ribo Labrador --- demos/next-sdjwt-workshop/src/steps/Step3.tsx | 2 +- demos/next-sdjwt-workshop/src/steps/Step4.tsx | 4 +- demos/next-sdjwt-workshop/src/steps/Step6.tsx | 37 +++++++++++++++---- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/demos/next-sdjwt-workshop/src/steps/Step3.tsx b/demos/next-sdjwt-workshop/src/steps/Step3.tsx index 31dcfc123..5e42f1f6f 100644 --- a/demos/next-sdjwt-workshop/src/steps/Step3.tsx +++ b/demos/next-sdjwt-workshop/src/steps/Step3.tsx @@ -149,7 +149,7 @@ const agent = await SDK.Agent.initialize({ ), { language: 'typescript', - code: `agent.addListener(SDK.ListenerKey.MESSAGE, (messages) => { + code: `agent.addListener(SDK.ListenerKey.MESSAGE, async (messages) => { //Your custom logic here });`, }, diff --git a/demos/next-sdjwt-workshop/src/steps/Step4.tsx b/demos/next-sdjwt-workshop/src/steps/Step4.tsx index a13aa64ba..a7f890e54 100644 --- a/demos/next-sdjwt-workshop/src/steps/Step4.tsx +++ b/demos/next-sdjwt-workshop/src/steps/Step4.tsx @@ -134,7 +134,7 @@ await agent.acceptInvitation(parsed, 'SampleCredentialOfferOOB');

You can listen for new Credential Offers in your index.mjs by adding some code into the message event listener we added earlier:

{ +agent.addListener(SDK.ListenerKey.MESSAGE, async (messages) => { for (const message of messages) { if (message.piuri === SDK.ProtocolType.DidcommOfferCredential) { console.log('Credential Offer:', message); @@ -158,7 +158,7 @@ agent.addListener(SDK.ListenerKey.MESSAGE, (messages) => {

In the index.mjs workshop file, we are now going to extend our message listener to listen for the Issued Credential message:

{ +agent.addListener(SDK.ListenerKey.MESSAGE, async (messages) => { for (const message of messages) { if (message instanceof SDK.Domain.Message) { if (message.piuri === SDK.ProtocolType.DidcommOfferCredential) { diff --git a/demos/next-sdjwt-workshop/src/steps/Step6.tsx b/demos/next-sdjwt-workshop/src/steps/Step6.tsx index 0928c9e0f..dbcb4af3a 100644 --- a/demos/next-sdjwt-workshop/src/steps/Step6.tsx +++ b/demos/next-sdjwt-workshop/src/steps/Step6.tsx @@ -61,6 +61,7 @@ import { sha512 } from '@noble/hashes/sha512' import InMemory from '@pluto-encrypted/inmemory' + class ShortFormDIDResolverSample { method = "prism"; @@ -109,6 +110,21 @@ class ShortFormDIDResolverSample { } } +async function wait(callback) { + const start = Date.now() + return new Promise(async (resolve, reject) => { + const interval = setInterval(async () => { + let result = await callback() + if (result) { + clearInterval(interval) + resolve("") + } + if (Date.now() - start > 30 * 1000) { + reject("timeout") + } + }, 1000) + }) +} (async () => { const registerPrismDid = await fetch(\`http://localhost:3000/cloud-agent/did-registrar/dids\`, { @@ -135,7 +151,7 @@ class ShortFormDIDResolverSample { const prismDidResponse = await registerPrismDid.json(); console.log('Prism DID created:', { longFormDid: prismDidResponse.longFormDid }); - const publishPrismDid = await fetch(\`http://localhost:3000/cloud-agent/did-registrar/dids/\${prismDidResponse.longFormDid}/publications\`, { + const publishPrismDid = await fetch(\`http://localhost:3000/cloud-agent/did-registrar/dids/${prismDidResponse.longFormDid}/publications\`, { method: "POST", headers: { "Content-Type": "application/json" } }); @@ -286,12 +302,19 @@ class ShortFormDIDResolverSample { const requestPresentation = SDK.RequestPresentation.fromMessage(message); const presentation = await agent.createPresentationForRequestProof(requestPresentation, credential); await agent.sendMessage(presentation.makeMessage()); - const verifyPresentation = await fetch(\`http://localhost:3000/cloud-agent/present-proof/presentations/\${presentationId}\`, { - method: "GET", - headers: { "Content-Type": "application/json" } - }); - const verificationResponse = await verifyPresentation.json(); - console.log('Verification Result:', { isValid: verificationResponse.status === "PresentationVerified" }); + try { + await wait(async () => { + const verifyPresentation = await fetch(\`http://localhost:3000/cloud-agent/present-proof/presentations/${presentationId}\`, { + method: "GET", + headers: { "Content-Type": "application/json" } + }); + const verificationResponse = await verifyPresentation.json(); + return verificationResponse.status === "PresentationVerified" + }) + console.log('Verification Result:', { isValid: true }); + } catch (e) { + console.log('Verification Result:', { isValid: false }); + } } } } From c2302cd78c6a848e3a0ab3cf82fbdf52a3fde528 Mon Sep 17 00:00:00 2001 From: Francisco Javier Ribo Labrador Date: Wed, 13 Nov 2024 14:32:05 +0100 Subject: [PATCH 2/3] fix: last step Signed-off-by: Francisco Javier Ribo Labrador --- demos/next-sdjwt-workshop/src/steps/Step6.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/next-sdjwt-workshop/src/steps/Step6.tsx b/demos/next-sdjwt-workshop/src/steps/Step6.tsx index dbcb4af3a..56c90e0ef 100644 --- a/demos/next-sdjwt-workshop/src/steps/Step6.tsx +++ b/demos/next-sdjwt-workshop/src/steps/Step6.tsx @@ -151,7 +151,7 @@ async function wait(callback) { const prismDidResponse = await registerPrismDid.json(); console.log('Prism DID created:', { longFormDid: prismDidResponse.longFormDid }); - const publishPrismDid = await fetch(\`http://localhost:3000/cloud-agent/did-registrar/dids/${prismDidResponse.longFormDid}/publications\`, { + const publishPrismDid = await fetch(\`http://localhost:3000/cloud-agent/did-registrar/dids/\${prismDidResponse.longFormDid}/publications\`, { method: "POST", headers: { "Content-Type": "application/json" } }); @@ -304,7 +304,7 @@ async function wait(callback) { await agent.sendMessage(presentation.makeMessage()); try { await wait(async () => { - const verifyPresentation = await fetch(\`http://localhost:3000/cloud-agent/present-proof/presentations/${presentationId}\`, { + const verifyPresentation = await fetch(\`http://localhost:3000/cloud-agent/present-proof/presentations/\${presentationId}\`, { method: "GET", headers: { "Content-Type": "application/json" } }); From fb1d35bd07398d98088b4b109a315bf269d4f4a2 Mon Sep 17 00:00:00 2001 From: Francisco Javier Ribo Labrador Date: Wed, 13 Nov 2024 14:42:15 +0100 Subject: [PATCH 3/3] fix: last step fix Signed-off-by: Francisco Javier Ribo Labrador --- demos/next-sdjwt-workshop/src/steps/Step6.tsx | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/demos/next-sdjwt-workshop/src/steps/Step6.tsx b/demos/next-sdjwt-workshop/src/steps/Step6.tsx index 56c90e0ef..964a9296f 100644 --- a/demos/next-sdjwt-workshop/src/steps/Step6.tsx +++ b/demos/next-sdjwt-workshop/src/steps/Step6.tsx @@ -61,7 +61,6 @@ import { sha512 } from '@noble/hashes/sha512' import InMemory from '@pluto-encrypted/inmemory' - class ShortFormDIDResolverSample { method = "prism"; @@ -110,20 +109,25 @@ class ShortFormDIDResolverSample { } } -async function wait(callback) { - const start = Date.now() - return new Promise(async (resolve, reject) => { - const interval = setInterval(async () => { - let result = await callback() - if (result) { - clearInterval(interval) - resolve("") - } - if (Date.now() - start > 30 * 1000) { - reject("timeout") - } - }, 1000) - }) +async function verifyCondition(callback) { + try { + const start = Date.now() + await new Promise(async (resolve, reject) => { + const interval = setInterval(async () => { + let result = await callback() + if (result) { + clearInterval(interval) + resolve("") + } + if (Date.now() - start > 60 * 1000) { + reject("timeout") + } + }, 1000) + }) + return true + } catch (e) { + return false + } } (async () => { @@ -302,19 +306,16 @@ async function wait(callback) { const requestPresentation = SDK.RequestPresentation.fromMessage(message); const presentation = await agent.createPresentationForRequestProof(requestPresentation, credential); await agent.sendMessage(presentation.makeMessage()); - try { - await wait(async () => { - const verifyPresentation = await fetch(\`http://localhost:3000/cloud-agent/present-proof/presentations/\${presentationId}\`, { - method: "GET", - headers: { "Content-Type": "application/json" } - }); - const verificationResponse = await verifyPresentation.json(); - return verificationResponse.status === "PresentationVerified" - }) - console.log('Verification Result:', { isValid: true }); - } catch (e) { - console.log('Verification Result:', { isValid: false }); - } + const verificationResult = await verifyCondition(async () => { + const verifyPresentation = await fetch(\`http://localhost:3000/cloud-agent/present-proof/presentations/\${presentationId}\`, { + method: "GET", + headers: { "Content-Type": "application/json" } + }); + const verificationResponse = await verifyPresentation.json(); + console.log('Verification Check:', verificationResponse.status) + return verificationResponse.status === "PresentationVerified" + }) + console.log('Verification Result:', { isValid: verificationResult }); } } }