From 15154b638be15305b2be6952d0155049a568e50d Mon Sep 17 00:00:00 2001 From: Neal Date: Thu, 7 Sep 2023 10:28:47 -0700 Subject: [PATCH] more tests --- .../tests/presentation-exchange.spec.ts | 55 ++++++++++--------- packages/credentials/tests/ssi.spec.ts | 26 +++++++++ packages/crypto/src/jose.ts | 25 +++------ 3 files changed, 63 insertions(+), 43 deletions(-) diff --git a/packages/credentials/tests/presentation-exchange.spec.ts b/packages/credentials/tests/presentation-exchange.spec.ts index cc5276814..ff078569c 100644 --- a/packages/credentials/tests/presentation-exchange.spec.ts +++ b/packages/credentials/tests/presentation-exchange.spec.ts @@ -147,17 +147,44 @@ describe('PresentationExchange', () => { }); }); -async function createBtcCredentialJwt(aliceDid: string, header: JwtHeaderParams, signer: Signer) { - const btcCredential: VerifiableCredentialTypeV1 = { +function createPresentationDefinition(): PresentationDefinition { + return { + 'id' : 'test-pd-id', + 'name' : 'simple PD', + 'purpose' : 'pd for testing', + 'input_descriptors' : [ + { + 'id' : 'whatever', + 'purpose' : 'id for testing', + 'constraints' : { + 'fields': [ + { + 'path': [ + '$.credentialSubject.btcAddress', + ] + } + ] + } + } + ] + }; +} + +function createVerifiableCredential(did:string): VerifiableCredentialTypeV1 { + return { '@context' : ['https://www.w3.org/2018/credentials/v1'], 'id' : 'btc-credential', 'type' : ['VerifiableCredential'], - 'issuer' : aliceDid, + 'issuer' : did, 'issuanceDate' : new Date().toISOString(), 'credentialSubject' : { 'btcAddress': 'btcAddress123' } }; +} + +async function createBtcCredentialJwt(aliceDid: string, header: JwtHeaderParams, signer: Signer) { + const btcCredential: VerifiableCredentialTypeV1 = createVerifiableCredential(aliceDid); return await createJwt({ header, @@ -190,28 +217,6 @@ async function createJwt(options: CreateJwtOpts) { return jwt; } -function createPresentationDefinition() { - return { - 'id' : 'test-pd-id', - 'name' : 'simple PD', - 'purpose' : 'pd for testing', - 'input_descriptors' : [ - { - 'id' : 'whatever', - 'purpose' : 'id for testing', - 'constraints' : { - 'fields': [ - { - 'path': [ - '$.credentialSubject.btcAddress', - ] - } - ] - } - } - ] - }; -} function decodeJwt(jwt: string) { const [encodedHeader, encodedPayload, encodedSignature] = jwt.split('.'); diff --git a/packages/credentials/tests/ssi.spec.ts b/packages/credentials/tests/ssi.spec.ts index cdb0404b5..b7bac623c 100644 --- a/packages/credentials/tests/ssi.spec.ts +++ b/packages/credentials/tests/ssi.spec.ts @@ -56,6 +56,28 @@ describe('SSI Tests', () => { expect(async () => await VerifiableCredential.verify(vcJwt)).to.not.throw(); }); + it('fails to create a VC JWT with CreateVCOptions and VC', async () => { + const vc:VerifiableCredentialTypeV1 = { + id : 'id123', + '@context' : ['https://www.w3.org/2018/credentials/v1'], + credentialSubject : { id: subjectIssuerDid, btcAddress: 'abc123' }, + type : ['VerifiableCredential'], + issuer : { id: subjectIssuerDid }, + issuanceDate : getCurrentXmlSchema112Timestamp(), + }; + + const vcCreateOptions: CreateVcOptions = { + credentialSubject : { id: subjectIssuerDid, btcAddress: 'abc123' }, + issuer : { id: subjectIssuerDid } + }; + + await expectThrowsAsync(() => VerifiableCredential.create(signOptions, vcCreateOptions, vc), 'options and verifiableCredentials are mutually exclusive, either include the full verifiableCredential or the options to create one'); + }); + + it('fails to create a VC JWT with no CreateVCOptions and no VC', async () => { + await expectThrowsAsync(() => VerifiableCredential.create(signOptions, undefined, undefined), 'options or verifiableCredential must be provided'); + }); + it('creates a VC JWT with a VC', async () => { const btcCredential: VerifiableCredentialTypeV1 = { '@context' : ['https://www.w3.org/2018/credentials/v1'], @@ -72,6 +94,10 @@ describe('SSI Tests', () => { expect(async () => await VerifiableCredential.verify(vcJwt)).to.not.throw(); }); + it('fails to verify an invalid VC JWT', async () => { + await expectThrowsAsync(() => VerifiableCredential.verify('invalid-jwt'), 'Incorrect format JWT'); + }); + it('decodes a VC JWT', async () => { const vcCreateOptions: CreateVcOptions = { credentialSubject : { id: subjectIssuerDid, btcAddress: 'abc123' }, diff --git a/packages/crypto/src/jose.ts b/packages/crypto/src/jose.ts index 87349f712..e80a949f5 100644 --- a/packages/crypto/src/jose.ts +++ b/packages/crypto/src/jose.ts @@ -289,28 +289,24 @@ export type JwkKeyPair = { export type JsonWebKey = PrivateKeyJwk | PublicKeyJwk; export interface JoseHeaderParams { - // Content Type cty?: string; - // JWK Set URL jku?: string; - // JSON Web Key jwk?: PublicKeyJwk; - // Key ID kid?: string; - // Type typ?: string; - // X.509 Certificate Chain x5c?: string[]; - // X.509 Certificate SHA-1 Thumbprint x5t?: string; - // X.509 URL x5u?: string; } export interface JwsHeaderParams extends JoseHeaderParams { alg: - // Edwards curve digital signature algorithm (e.g., Ed25519) - | 'EdDSA' + // HMAC using SHA-256 + | 'HS256' + // HMAC using SHA-384 + | 'HS384' + // HMAC using SHA-512 + | 'HS512' // ECDSA using P-256 and SHA-256 | 'ES256' // ECDSA using secp256k1 curve and SHA-256 @@ -318,14 +314,7 @@ export interface JwsHeaderParams extends JoseHeaderParams { // ECDSA using P-384 and SHA-384 | 'ES384' // ECDSA using P-521 and SHA-512 - | 'ES512' - // HMAC using SHA-256 - | 'HS256' - // HMAC using SHA-384 - | 'HS384' - // HMAC using SHA-512 - | 'HS512'; - + | 'ES512'; // Indicates that extensions to JOSE RFCs are being used // that MUST be understood and processed.