From a6b75b5d67999f81fb6825f679d1aa59beed9321 Mon Sep 17 00:00:00 2001 From: Neal Date: Tue, 6 Feb 2024 13:46:35 -0600 Subject: [PATCH] updates --- packages/credentials/README.md | 4 +- packages/credentials/tests/jwt.spec.ts | 4 +- .../tests/presentation-exchange.spec.ts | 12 ++-- .../tests/verifiable-credential.spec.ts | 59 ++++++++----------- 4 files changed, 35 insertions(+), 44 deletions(-) diff --git a/packages/credentials/README.md b/packages/credentials/README.md index eae8bb03f..4f7f65c32 100644 --- a/packages/credentials/README.md +++ b/packages/credentials/README.md @@ -69,8 +69,8 @@ Sign a `VerifiableCredential` with a DID: First create a `Did` object as follows: ```javascript -import { DidKeyMethod } from '@web5/dids'; -const issuer = await DidKeyMethod.create(); +import { DidKey } from '@web5/dids'; +const issuer: BearerDid = await DidKey.create(); ``` Then sign the VC using the `did` object diff --git a/packages/credentials/tests/jwt.spec.ts b/packages/credentials/tests/jwt.spec.ts index c9308d45c..ca3990734 100644 --- a/packages/credentials/tests/jwt.spec.ts +++ b/packages/credentials/tests/jwt.spec.ts @@ -2,11 +2,10 @@ import type { JwtHeaderParams, JwtPayload, PrivateKeyJwk } from '@web5/crypto'; import { expect } from 'chai'; import { Convert } from '@web5/common'; -import { Secp256k1 } from '@web5/crypto'; +import { Ed25519 } from '@web5/crypto'; import { DidKey, PortableDid } from '@web5/dids'; import { Jwt } from '../src/jwt.js'; -import { Ed25519 } from '@web5/crypto'; describe('Jwt', () => { describe('parse()', () => { @@ -138,7 +137,6 @@ describe('Jwt', () => { const did = await DidKey.fromKeys(portabldDid); - // const did = await DidKey.create({ options: { algorithm: 'secp256k1'} }); const header: JwtHeaderParams = { typ: 'JWT', alg: 'EdDSA', kid: did.didDocument.verificationMethod![0].id }; const base64UrlEncodedHeader = Convert.object(header).toBase64Url(); diff --git a/packages/credentials/tests/presentation-exchange.spec.ts b/packages/credentials/tests/presentation-exchange.spec.ts index 997888a41..8a6ee05fb 100644 --- a/packages/credentials/tests/presentation-exchange.spec.ts +++ b/packages/credentials/tests/presentation-exchange.spec.ts @@ -32,8 +32,8 @@ describe('PresentationExchange', () => { const vc = await VerifiableCredential.create({ type : 'StreetCred', - issuer : issuerDid.uri!, - subject : issuerDid.uri!, + issuer : issuerDid.uri, + subject : issuerDid.uri, data : new BitcoinCredential('btcAddress123'), }); @@ -60,8 +60,8 @@ describe('PresentationExchange', () => { it('should return the only one verifiable credential', async () => { const vc = await VerifiableCredential.create({ type : 'StreetCred', - issuer : issuerDid.uri!, - subject : issuerDid.uri!, + issuer : issuerDid.uri, + subject : issuerDid.uri, data : new OtherCredential('otherstuff'), }); @@ -146,8 +146,8 @@ describe('PresentationExchange', () => { it('should fail to create a presentation with vc that does not match presentation definition', async () => { const vc = await VerifiableCredential.create({ type : 'StreetCred', - issuer : issuerDid.uri!, - subject : issuerDid.uri!, + issuer : issuerDid.uri, + subject : issuerDid.uri, data : new OtherCredential('otherstuff'), }); diff --git a/packages/credentials/tests/verifiable-credential.spec.ts b/packages/credentials/tests/verifiable-credential.spec.ts index 4b7c5608b..7431a6fcb 100644 --- a/packages/credentials/tests/verifiable-credential.spec.ts +++ b/packages/credentials/tests/verifiable-credential.spec.ts @@ -2,14 +2,13 @@ import type { BearerDid, PortableDid } from '@web5/dids'; import sinon from 'sinon'; import { expect } from 'chai'; -import { DidDht, DidKey, DidIon, Did } from '@web5/dids'; +import { DidDht, DidKey, DidIon } from '@web5/dids'; import { Jwt } from '../src/jwt.js'; import { VerifiableCredential } from '../src/verifiable-credential.js'; import CredentialsVerifyTestVector from '../../../web5-spec/test-vectors/credentials/verify.json' assert { type: 'json' }; describe('Verifiable Credential Tests', async() => { - // const did = await DidKey.create(); let issuerDid: BearerDid; class StreetCredibility { @@ -25,11 +24,11 @@ describe('Verifiable Credential Tests', async() => { describe('Verifiable Credential (VC)', () => { it('create vc works', async () => { - const subjectDid = issuerDid.uri!; + const subjectDid = issuerDid.uri; const vc = await VerifiableCredential.create({ type : 'StreetCred', - issuer : issuerDid.uri!, + issuer : issuerDid.uri, subject : subjectDid, data : new StreetCredibility('high', true), }); @@ -46,8 +45,8 @@ describe('Verifiable Credential Tests', async() => { const vc = await VerifiableCredential.create({ type : 'TBDeveloperCredential', - subject : did.uri!, - issuer : did.uri!, + subject : did.uri, + issuer : did.uri, data : { username: 'nitro' } @@ -58,11 +57,11 @@ describe('Verifiable Credential Tests', async() => { await VerifiableCredential.verify({ vcJwt }); for( const currentVc of [vc, VerifiableCredential.parseJwt({ vcJwt })]){ - expect(currentVc.issuer).to.equal(did.uri!); - expect(currentVc.subject).to.equal(did.uri!); + expect(currentVc.issuer).to.equal(did.uri); + expect(currentVc.subject).to.equal(did.uri); expect(currentVc.type).to.equal('TBDeveloperCredential'); expect(currentVc.vcDataModel.issuanceDate).to.not.be.undefined; - expect(currentVc.vcDataModel.credentialSubject).to.deep.equal({ id: did.uri!, username: 'nitro'}); + expect(currentVc.vcDataModel.credentialSubject).to.deep.equal({ id: did.uri, username: 'nitro'}); } }); @@ -71,8 +70,8 @@ describe('Verifiable Credential Tests', async() => { const vc = await VerifiableCredential.create({ type : 'TBDeveloperCredential', - subject : did.uri!, - issuer : did.uri!, + subject : did.uri, + issuer : did.uri, data : { username: 'nitro' } @@ -83,11 +82,11 @@ describe('Verifiable Credential Tests', async() => { await VerifiableCredential.verify({ vcJwt }); for (const currentVc of [vc, VerifiableCredential.parseJwt({ vcJwt })]){ - expect(currentVc.issuer).to.equal(did.uri!); - expect(currentVc.subject).to.equal(did.uri!); + expect(currentVc.issuer).to.equal(did.uri); + expect(currentVc.subject).to.equal(did.uri); expect(currentVc.type).to.equal('TBDeveloperCredential'); expect(currentVc.vcDataModel.issuanceDate).to.not.be.undefined; - expect(currentVc.vcDataModel.credentialSubject).to.deep.equal({ id: did.uri!, username: 'nitro'}); + expect(currentVc.vcDataModel.credentialSubject).to.deep.equal({ id: did.uri, username: 'nitro'}); } }); @@ -141,11 +140,11 @@ describe('Verifiable Credential Tests', async() => { }); it('signing with Ed25519 key works', async () => { - const subjectDid = issuerDid.uri!; + const subjectDid = issuerDid.uri; const vc = await VerifiableCredential.create({ type : 'StreetCred', - issuer : issuerDid.uri!, + issuer : issuerDid.uri, subject : subjectDid, data : new StreetCredibility('high', true), }); @@ -163,8 +162,8 @@ describe('Verifiable Credential Tests', async() => { const vc = await VerifiableCredential.create({ type : 'StreetCred', - issuer : did.uri!, - subject : did.uri!, + issuer : did.uri, + subject : did.uri, data : new StreetCredibility('high', true), }); @@ -184,13 +183,12 @@ describe('Verifiable Credential Tests', async() => { it('parseJwt checks if missing vc property', async () => { const did = await DidKey.create(); - // const did = await DidKey.toKeys({ did: bearerDid }); const jwt = await Jwt.sign({ signerDid : did, payload : { - iss : did.uri!, - sub : did.uri! + iss : did.uri, + sub : did.uri } }); @@ -202,8 +200,8 @@ describe('Verifiable Credential Tests', async() => { it('parseJwt returns an instance of VerifiableCredential on success', async () => { const vc = await VerifiableCredential.create({ type : 'StreetCred', - issuer : issuerDid.uri!, - subject : issuerDid.uri!, + issuer : issuerDid.uri, + subject : issuerDid.uri, data : new StreetCredibility('high', true), }); @@ -241,16 +239,16 @@ describe('Verifiable Credential Tests', async() => { it('verify does not throw an exception with valid vc', async () => { const vc = await VerifiableCredential.create({ type : 'StreetCred', - issuer : issuerDid.uri!, - subject : issuerDid.uri!, + issuer : issuerDid.uri, + subject : issuerDid.uri, data : new StreetCredibility('high', true), }); const vcJwt = await vc.sign({did: issuerDid}); const { issuer, subject, vc: credential } = await VerifiableCredential.verify({ vcJwt }); - expect(issuer).to.equal(issuerDid.uri!); - expect(subject).to.equal(issuerDid.uri!); + expect(issuer).to.equal(issuerDid.uri); + expect(subject).to.equal(issuerDid.uri); expect(credential).to.not.be.null; }); @@ -387,12 +385,7 @@ describe('Verifiable Credential Tests', async() => { const vectors = CredentialsVerifyTestVector.vectors; for (const vector of vectors) { - const { input, errors, description } = vector; - - // TODO: DID:JWK is not supported yet - if (description === 'verify a jwt verifiable credential signed with a did:jwk') { - continue; - } + const { input, errors } = vector; if (errors) { let errorOccurred = false;