Skip to content

Commit

Permalink
feat: test coverage increase in ed25519.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ANIRUDH-333 committed Oct 4, 2024
1 parent 8b6eb13 commit c5bb4df
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/jose/jws/general.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { PrivateKeySigner } from '../../../src/index.js';
import { signatureAlgorithms } from '../../../src/jose/algorithms/signing/signature-algorithms.js';
import sinon from 'sinon';
import { UniversalResolver } from '@web5/dids';
import { Encoder } from '../../../src/utils/encoder.js';

Check failure on line 11 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (18.17.0)

Imports should be sorted alphabetically

Check failure on line 11 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (20.3.0)

Imports should be sorted alphabetically
import { ed25519 } from '../../../src/jose/algorithms/signing/ed25519.js';

Check failure on line 12 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (18.17.0)

Imports should be sorted alphabetically

Check failure on line 12 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (20.3.0)

Imports should be sorted alphabetically
import { DwnError, DwnErrorCode } from '../../../src/core/dwn-error.js';

const { Ed25519, secp256k1 } = signatureAlgorithms;
const secp256r1 = signatureAlgorithms['P-256'];
Expand Down Expand Up @@ -124,6 +127,34 @@ describe('General JWS Sign/Verify', () => {
expect(verificationResult.signers).to.include('did:jank:alice');
});

it('should throw an error for invalid Ed25519 JWK', async () => {
const invalidJwk = {
kty: 'RSA', // Invalid key type

Check failure on line 132 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (18.17.0)

Missing space after key 'kty'

Check failure on line 132 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (20.3.0)

Missing space after key 'kty'
crv: 'Ed25519',

Check failure on line 133 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (18.17.0)

Missing space after key 'crv'

Check failure on line 133 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (20.3.0)

Missing space after key 'crv'
d: 'invalid-private-key'

Check failure on line 134 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (18.17.0)

Missing space after key 'd'

Check failure on line 134 in tests/jose/jws/general.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node (20.3.0)

Missing space after key 'd'
};

const content = new TextEncoder().encode('anyPayloadValue');

try {
await ed25519.sign(content, invalidJwk as any);
expect.fail('Expected an error to be thrown');
} catch (error) {
expect(error).to.be.instanceOf(DwnError);
expect((error as DwnError).code).to.equal(DwnErrorCode.Ed25519InvalidJwk);
expect((error as DwnError).message).to.include('invalid jwk. kty MUST be OKP. crv MUST be Ed25519');
}
});

it('should convert public key bytes to JWK', async () => {
const { publicJwk } = await ed25519.generateKeyPair();
const publicKeyBytes = Encoder.base64UrlToBytes(publicJwk.x);

const convertedJwk = await ed25519.publicKeyToJwk(publicKeyBytes);

expect(convertedJwk).to.deep.equal(publicJwk);
});

it('should support multiple signatures using different key types', async () => {
const secp256k1Keys = await secp256k1.generateKeyPair();
const ed25519Keys = await Ed25519.generateKeyPair();
Expand Down

0 comments on commit c5bb4df

Please sign in to comment.