Skip to content

Commit

Permalink
fix: add listener async + wait time for presentation verified (#336)
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 Nov 13, 2024
1 parent eff3cf3 commit 80f3370
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 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
36 changes: 30 additions & 6 deletions demos/next-sdjwt-workshop/src/steps/Step6.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ class ShortFormDIDResolverSample {
}
}
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 () => {
const registerPrismDid = await fetch(\`http://localhost:3000/cloud-agent/did-registrar/dids\`, {
Expand Down Expand Up @@ -286,12 +306,16 @@ 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" });
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 });
}
}
}
Expand Down

1 comment on commit 80f3370

@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: 75%
76.14% (3636/4775) 66.53% (1666/2504) 80.29% (864/1076)

Please sign in to comment.