Skip to content

Commit 1cbbdf2

Browse files
authored
Merge pull request #347 from docknetwork/fix/jwt-with-resolver
Fix verifying JWT credentials using Dock DID resolver
2 parents cf685ef + 2b3aceb commit 1cbbdf2

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@docknetwork/sdk",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"main": "index.js",
55
"license": "MIT",
66
"repository": {

src/utils/vc/credentials.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getAndValidateSchemaIfPresent } from './schema';
88
import { isRevocationCheckNeeded, checkRevocationStatus } from '../revocation';
99
import DIDResolver from '../../did-resolver'; // eslint-disable-line
1010

11-
import { getSuiteFromKeyDoc, expandJSONLD } from './helpers';
11+
import { getSuiteFromKeyDoc, expandJSONLD, getKeyFromDIDDocument } from './helpers';
1212
import { DEFAULT_CONTEXT_V1_URL, credentialContextField } from './constants';
1313
import { ensureValidDatetime } from '../type-helpers';
1414

@@ -275,7 +275,8 @@ export async function verifyCredential(vcJSONorString, {
275275
throw new Error('No kid in JWT header');
276276
}
277277

278-
const { document: keyDocument } = await docLoader(header.kid);
278+
const { document: didDocument } = await docLoader(header.kid);
279+
const keyDocument = getKeyFromDIDDocument(didDocument, header.kid);
279280
const keyDocSuite = await getSuiteFromKeyDoc(keyDocument, false, { detached: false, header });
280281
const verified = await keyDocSuite.verifySignature({
281282
verifyData: new Uint8Array(Buffer.from(jwtSplit[1], 'utf8')),

src/utils/vc/helpers.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,22 @@ export async function expandJSONLD(credential, options = {}) {
9595
});
9696
return expanded[0];
9797
}
98+
99+
export function potentialToArray(a) {
100+
/* eslint-disable no-nested-ternary */
101+
return a ? (Array.isArray(a) ? a : [a]) : [];
102+
}
103+
104+
export function getKeyFromDIDDocument(didDocument, didUrl) {
105+
// Ensure not already a key doc
106+
if (didDocument.publicKeyBase58 || didDocument.publicKeyMultibase || didDocument.publicKeyJwk || (didDocument.publicKey && !Array.isArray(didDocument.publicKey))) {
107+
return didDocument;
108+
}
109+
110+
const possibleKeys = [
111+
...potentialToArray(didDocument.verificationMethod),
112+
...potentialToArray(didDocument.keyAgreement),
113+
...potentialToArray(didDocument.publicKey),
114+
];
115+
return possibleKeys.filter((key) => key.id === didUrl)[0];
116+
}

tests/integration/issuing.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,25 @@ describe('Verifiable Credential issuance where issuer has a Dock DID', () => {
119119
),
120120
);
121121
}, 40000);
122+
123+
test('(JWT) Issue a verifiable credential with ed25519 key and verify it', async () => {
124+
const issuerKey = getKeyDoc(issuer1DID, dock.keyring.addFromUri(issuer1KeySeed, null, 'ed25519'), 'Ed25519VerificationKey2018');
125+
const credential = await issueCredential(issuerKey, unsignedCred, true, null, null, null, null, false, 'jwt');
126+
const result = await verifyCredential(credential, { resolver });
127+
expect(result.verified).toBeTruthy();
128+
}, 40000);
129+
130+
test('(JWT) Issue a verifiable credential with secp256k1 key and verify it', async () => {
131+
const issuerKey = getKeyDoc(issuer2DID, generateEcdsaSecp256k1Keypair(issuer2KeyEntropy), 'EcdsaSecp256k1VerificationKey2019');
132+
const credential = await issueCredential(issuerKey, unsignedCred, true, null, null, null, null, false, 'jwt');
133+
const result = await verifyCredential(credential, { resolver });
134+
expect(result.verified).toBeTruthy();
135+
}, 40000);
136+
137+
test('(JWT) Issue a verifiable credential with sr25519 key and verify it', async () => {
138+
const issuerKey = getKeyDoc(issuer3DID, dock.keyring.addFromUri(issuer3KeySeed, null, 'sr25519'), 'Sr25519VerificationKey2020');
139+
const credential = await issueCredential(issuerKey, unsignedCred, true, null, null, null, null, false, 'jwt');
140+
const result = await verifyCredential(credential, { resolver });
141+
expect(result.verified).toBeTruthy();
142+
}, 40000);
122143
});

0 commit comments

Comments
 (0)