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 media types and add examples for presentation rendering #32

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
53 changes: 43 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,40 @@ <h1>Basic Example</h1>
}
}
}
</pre>
</pre>
<pre class="example nohighlight vc" data-vc-tabs="jose sd-jwt cose" title="A Verifiable Presentation example">
{
<span class='comment'>// set the context, which establishes the special terms we will be using.</span>
"@context": [
"https://www.w3.org/ns/credentials/v2",
"https://www.w3.org/ns/credentials/examples/v2"
],
<span class='comment'>// specify the identifier for the presentation</span>
"id": "http://example.edu/presentations/1872",
<span class='comment'>// the presentation type, which declare what data to expect in the presentation</span>
"type": "VerifiablePresentation",
<span class='comment'>// when the presentation was issued</span>
"validFrom": "2010-01-01T19:23:24Z",
<span class='comment'>// credentials in the presentation</span>
"verifiableCredential": [
{
"@context": "https://www.w3.org/ns/credentials/v2",
"id": "data:application/vc-ld+cose;base64url,UXpWalYuLi5STWpV",
"type": "EnvelopedVerifiableCredential"
},
{
"@context": "https://www.w3.org/ns/credentials/v2",
"id": "data:application/vc-ld+jwt;eyVjV...RMjU",
"type": "EnvelopedVerifiableCredential"
},
{
"@context": "https://www.w3.org/ns/credentials/v2",
"id": "data:application/vc-ld+sd-jwt;eyVjV...RMjU",
"type": "EnvelopedVerifiableCredential"
}
]
}
</pre>
</section>

<section>
Expand Down Expand Up @@ -129,47 +162,47 @@ <h1>File Hash Examples</h1>
<li>
raw (`openssl dgst -sha256`):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="openssl dgst -sha256" /></code>
data-hash-format="openssl dgst -sha256"></span></code>
</li>
<li>
digestSRI (sha2-256 base64pad):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="sri sha2-256" /></code>
data-hash-format="sri sha2-256"></span></code>
</li>
<li>
digestSRI (sha2-384 base64pad):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="sri sha2-384" /></code>
data-hash-format="sri sha2-384"></span></code>
</li>
<li>
digestMultibase (sha2-256 base16):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="multihash sha2-256 base16" /></code>
data-hash-format="multihash sha2-256 base16"></span></code>
</li>
<li>
digestMultibase (sha2-256 base58btc):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="multihash sha2-256 base58btc" /></code>
data-hash-format="multihash sha2-256 base58btc"></span></code>
</li>
<li>
digestMultibase (sha2-256 base64-url-nopad):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="multihash sha2-256" /></code>
data-hash-format="multihash sha2-256"></span></code>
</li>
<li>
digestMultibase (sha2-384 base64-url-nopad):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="multihash sha2-384" /></code>
data-hash-format="multihash sha2-384"></span></code>
</li>
<li>
digestMultibase (sha3-256 base64-url-nopad):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="multihash sha3-256" /></code>
data-hash-format="multihash sha3-256"></span></code>
</li>
<li>
digestMultibase (sha3-384 base64-url-nopad):
<code><span class="vc-hash" data-hash-url="https://www.w3.org/ns/credentials/v2"
data-hash-format="multihash sha3-384" /></code>
data-hash-format="multihash sha3-384"></span></code>
</li>
</ul>

Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,12 @@ async function createVcExamples() {
try {
verifiableCredentialProof = await attachProof({credential, suite});
const mediaType =
(verifiableCredentialProof.type.includes('VerifiablePresentation'))
? 'application/vp' : 'application/vc';
(verifiableCredentialProof.type
.includes('VerifiablePresentation')) ?
'application/vp' : 'application/vc';
return `<h1>${mediaType}</h1>
<pre>${JSON.stringify(verifiableCredentialProof, null, 2)
.match(/.{1,75}/g).join('\n')}</pre>`;
.match(/.{1,75}/g).join('\n')}</pre>`;
} catch(e) {
console.error(
'respec-vc error: Failed to attach proof to Verifiable Credential.',
Expand Down
21 changes: 7 additions & 14 deletions src/cose.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ function buf2hex(buffer) {
const getCredential = async (
privateKey,
byteSigner,
messageType,
messageJson,
) => {
let oldMessageType = (messageType === 'application/vc+cose')
? 'application/vc+ld+json+cose' : 'application/vp+ld+json+cose';

return issuer({
alg: privateKey.alg,
type: oldMessageType,
type: 'application/vc+ld+json+cose',
signer: byteSigner,
}).issue({
claimset: new TextEncoder().encode(JSON.stringify(messageJson, null, 2)),
Expand All @@ -29,7 +25,6 @@ const getCredential = async (
const getPresentation = async (
privateKey,
byteSigner,
messageType,
message,
) => {
const disclosures = (message.verifiableCredential || []).map(enveloped => {
Expand All @@ -45,11 +40,9 @@ const getPresentation = async (
credential: content,
};
});
let oldMessageType = (messageType === 'application/vc+cose')
? 'application/vc+ld+json+cose' : 'application/vp+ld+json+cose';
return holder({
alg: privateKey.alg,
type: oldMessageType,
type: 'application/vp+ld+json+cose',
}).issue({
signer: byteSigner,
presentation: message,
Expand All @@ -73,11 +66,11 @@ const getBinaryMessage = async (privateKey, messageType, messageJson) => {
},
};
switch(messageType) {
case 'application/vc+cose': {
return getCredential(privateKey, byteSigner, messageType, messageJson);
case 'application/vc-ld+cose': {
return getCredential(privateKey, byteSigner, messageJson);
}
case 'application/vp+cose': {
return getPresentation(privateKey, byteSigner, messageType, messageJson);
case 'application/vp-ld+cose': {
return getPresentation(privateKey, byteSigner, messageJson);
}
default: {
throw new Error('Unknown message type');
Expand All @@ -89,7 +82,7 @@ export const getCoseExample = async (privateKey, messageJson) => {
const type = Array.isArray(messageJson.type) ?
messageJson.type : [messageJson.type];
const messageType = type.includes('VerifiableCredential') ?
'application/vc+cose' : 'application/vp+cose';
'application/vc-ld+cose' : 'application/vp-ld+cose';
const message = await getBinaryMessage(privateKey, messageType, messageJson);
const messageHex = buf2hex(message);
const messageBuffer = Buffer.from(messageHex, 'hex');
Expand Down
18 changes: 7 additions & 11 deletions src/jose.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ import * as jose from 'jose';
const getCredential = async (
privateKey,
byteSigner,
messageType,
messageJson
) => {
let oldMessageType = (messageType === 'application/vc+jwt')
? 'application/vc+ld+json+jwt' : 'application/vp+ld+json+jwt';
return issuer({
alg: privateKey.alg,
type: oldMessageType,
type: 'application/vc+ld+json+jwt',
signer: byteSigner,
}).issue({
claimset: new TextEncoder().encode(JSON.stringify(messageJson, null, 2)),
Expand All @@ -27,7 +24,6 @@ const getCredential = async (
const getPresentation = async (
privateKey,
byteSigner,
messageType,
message
) => {
const disclosures = (message.verifiableCredential || []).map(enveloped => {
Expand All @@ -44,7 +40,7 @@ const getPresentation = async (
});
return holder({
alg: privateKey.alg,
type: messageType,
type: 'application/vp+ld+json+jwt',
}).issue({
signer: byteSigner,
presentation: message,
Expand Down Expand Up @@ -75,11 +71,11 @@ const getBinaryMessage = async (privateKey, messageType, messageJson) => {
},
};
switch(messageType) {
case 'application/vc+jwt': {
return getCredential(privateKey, byteSigner, messageType, messageJson);
case 'application/vc-ld+jwt': {
return getCredential(privateKey, byteSigner, messageJson);
}
case 'application/vp+jwt': {
return getPresentation(privateKey, byteSigner, messageType, messageJson);
case 'application/vp-ld+jwt': {
return getPresentation(privateKey, byteSigner, messageJson);
}
default: {
throw new Error('Unknown message type');
Expand All @@ -91,7 +87,7 @@ export const getJoseExample = async (privateKey, messageJson) => {
const type = Array.isArray(messageJson.type) ?
messageJson.type : [messageJson.type];
const messageType = type.includes('VerifiableCredential') ?
'application/vc+jwt' : 'application/vp+jwt';
'application/vc-ld+jwt' : 'application/vp-ld+jwt';
const message = await getBinaryMessage(privateKey, messageType, messageJson);
const messageEncoded = new TextDecoder().decode(message);
const decodedHeader = jose.decodeProtectedHeader(messageEncoded);
Expand Down
47 changes: 15 additions & 32 deletions src/sd-jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,16 @@ export const generateIssuerClaims = example => {
.replace(/type:/g, '!sd type:');
};

const getCredential = async (
privateKey,
byteSigner,
messageType,
messageJson,
) => {
let oldMessageType = (messageType === 'application/vc+sd-jwt')
? 'application/vc+ld+json+sd-jwt' : 'application/vp+ld+json+sd-jwt';

return issuer({
alg: privateKey.alg,
type: oldMessageType,
signer: byteSigner,
}).issue({
claimset: new TextEncoder().encode(generateIssuerClaims(messageJson)),
});
};

const getPresentation = async (
privateKey,
byteSigner,
messageType,
messageJson,
) => {
const mediaType = 'application/vc+sd-jwt';
return getCredential(privateKey, byteSigner, mediaType, messageJson);
};
const getCredential =
async (privateKey, byteSigner, messageJson) => {
return issuer({
alg: privateKey.alg,
type: 'application/vc+ld+json+sd-jwt',
signer: byteSigner,
}).issue({
claimset: new TextEncoder().encode(generateIssuerClaims(messageJson)),
});
};

export const getBinaryMessage = async (
privateKey,
Expand All @@ -119,11 +102,11 @@ export const getBinaryMessage = async (
},
};
switch(messageType) {
case 'application/vc+sd-jwt': {
return getCredential(privateKey, byteSigner, messageType, messageJson);
case 'application/vc-ld+sd-jwt': {
return getCredential(privateKey, byteSigner, messageJson);
}
case 'application/vp+sd-jwt': {
return getPresentation(privateKey, byteSigner, messageType, messageJson);
case 'application/vp-ld+sd-jwt': {
return getCredential(privateKey, byteSigner, messageJson);
}
default: {
throw new Error('Unknown message type');
Expand All @@ -140,7 +123,7 @@ export const getSdJwtExample = async (
const type = Array.isArray(messageJson.type) ?
messageJson.type : [messageJson.type];
const messageType = type.includes('VerifiableCredential') ?
'application/vc+sd-jwt' : 'application/vp+sd-jwt';
'application/vc-ld+sd-jwt' : 'application/vp-ld+sd-jwt';
const binaryMessage =
await getBinaryMessage(privateKey, messageType, messageJson);
const message = new TextDecoder().decode(binaryMessage);
Expand Down