Skip to content

Commit

Permalink
fix: add listener async + wait time for presentation verified
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Javier Ribo Labrador <[email protected]>
  • Loading branch information
elribonazo committed Nov 13, 2024
1 parent eff3cf3 commit b5c2f5b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion demos/next-sdjwt-workshop/src/steps/Step3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const agent = await SDK.Agent.initialize({
</div>),
{
language: 'typescript',
code: `agent.addListener(SDK.ListenerKey.MESSAGE, (messages) => {
code: `agent.addListener(SDK.ListenerKey.MESSAGE, async (messages) => {
//Your custom logic here
});`,
},
Expand Down
4 changes: 2 additions & 2 deletions demos/next-sdjwt-workshop/src/steps/Step4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ await agent.acceptInvitation(parsed, 'SampleCredentialOfferOOB');
<p className="text-gray-600 my-4 leading-relaxed">You can listen for new Credential Offers in your <i>index.mjs</i> by adding some code into the message event listener we added earlier:</p>
<CodeComponent content={{
code: `let credential;
agent.addListener(SDK.ListenerKey.MESSAGE, (messages) => {
agent.addListener(SDK.ListenerKey.MESSAGE, async (messages) => {
for (const message of messages) {
if (message.piuri === SDK.ProtocolType.DidcommOfferCredential) {
console.log('Credential Offer:', message);
Expand All @@ -158,7 +158,7 @@ agent.addListener(SDK.ListenerKey.MESSAGE, (messages) => {
<p className="text-gray-600 my-4 leading-relaxed">In the <i>index.mjs</i> workshop file, we are now going to extend our message listener to listen for the Issued Credential message:</p>
<CodeComponent content={{
code: `let credential;
agent.addListener(SDK.ListenerKey.MESSAGE, (messages) => {
agent.addListener(SDK.ListenerKey.MESSAGE, async (messages) => {
for (const message of messages) {
if (message instanceof SDK.Domain.Message) {
if (message.piuri === SDK.ProtocolType.DidcommOfferCredential) {
Expand Down
37 changes: 30 additions & 7 deletions demos/next-sdjwt-workshop/src/steps/Step6.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
sha512
} from '@noble/hashes/sha512'
import InMemory from '@pluto-encrypted/inmemory'
class ShortFormDIDResolverSample {
method = "prism";
Expand Down Expand Up @@ -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\`, {
Expand All @@ -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" }
});
Expand Down Expand Up @@ -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 });
}
}
}
}
Expand Down

0 comments on commit b5c2f5b

Please sign in to comment.