Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add listener async + wait time for presentation verified #336

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 29 additions & 6 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 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
Loading