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

Update explainer to include feature detection and new syntax #173

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all 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
67 changes: 46 additions & 21 deletions explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,58 @@ Here is an example of how the  the API might be used in practice:
The API needs to be initiated through a user gesture, such as a button click:

```html
<button onclick="requestLicense()">Request Driver's license<button>
<button onclick="requestCredential()">Request Driver's license<button>
```


```javascript
async function requestLicense() {
const oid4pv = {
// Protocol extensibility:
protocol: "oid4vp", // An example of an OpenID4VP request to wallets. // Based on https://github.com/openid/OpenID4VP/issues/125
data: {
nonce: "n-0S6_WzA2Mj",
presentation_definition: {
// Presentation Exchange request, omitted for brevity
},
},
};
const digitalCredential = await navigator.credentials.get({
digital: {
requests: [oid4pv],
},
});
// To be decrypted on the server...
const encryptedData = digitalCredential.data;
}
async function requestCredential() {

// Check for Digital Credentials API support
if (typeof window.DigitalCredential !== 'undefined') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe return early here? e.g.

Suggested change
if (typeof window.DigitalCredential !== 'undefined') {
if (typeof window.DigitalCredential !== 'undefined') {
// API not supported, return undefined.
return;
}


try {

// These parameters are typically fetched from the backend.
// Statically defined here for protocol extensibility illustration purposes.
const oid4vp = {
protocol: "oid4vp", // An example of an OpenID4VP request to wallets. // Based on https://github.com/openid/OpenID4VP/issues/125
data: {
nonce: "n-0S6_WzA2Mj",
presentation_definition: {
//Presentation Exchange request, omitted for brevity
},
},
};

// create an Abort Controller
const controller = new AbortController();

// Call the Digital Credentials API using the presentation request from the backend
let dcResponse = await navigator.credentials.get({
signal: controller.signal,
digital: {
requests: [ oid4vp ]
}
});

// Send the encrypted response to the backend for decryption and verification
// Ommitted for brevity

} catch (error) {
console.error('Error:', error);
}
} else {

// fallback scenario, illustrative only
alert("The Digital Credentials API is not supported in this browser.")
}
};
```

You can read a more detailed and technical description of the API in the [specification draft](https://wicg.github.io/digital-identities/).
> Example from: https://digitalcredentials.dev/docs/verifier-site/requesting-cred

You can read a more detailed and technical description of the API in the [specification draft](https://wicg.github.io/digital-credentials/).

### Using the API from another origin

Expand Down