Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-neal committed Sep 7, 2023
1 parent 18782c7 commit 15154b6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 43 deletions.
55 changes: 30 additions & 25 deletions packages/credentials/tests/presentation-exchange.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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('.');
Expand Down
26 changes: 26 additions & 0 deletions packages/credentials/tests/ssi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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' },
Expand Down
25 changes: 7 additions & 18 deletions packages/crypto/src/jose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,43 +289,32 @@ 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
| 'ES256K'
// 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.
Expand Down

0 comments on commit 15154b6

Please sign in to comment.