Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Oct 11, 2024
1 parent 57b1594 commit 7c33a05
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion packages/agent/tests/local-key-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Jwk } from '@web5/crypto';
import type { BearerDid } from '@web5/dids';

import sinon from 'sinon';
import { expect } from 'chai';
import { Convert } from '@web5/common';
import { CryptoUtils } from '@web5/crypto';
import { CryptoUtils, Ed25519 } from '@web5/crypto';

import type { Web5PlatformAgent } from '../src/types/agent.js';

Expand Down Expand Up @@ -106,6 +107,47 @@ describe('LocalKeyManager', () => {
});
});

describe('importKey()', () => {
it('imports a key and returns a key URI', async () => {
// generate a key and import it
const key = await Ed25519.generateKey();
const keyUri = await testHarness.agent.keyManager.importKey({ key });

// fetch the key using the keyUri
const importedKey = await testHarness.agent.keyManager.exportKey({ keyUri });

// validate the key
expect(importedKey).to.exist;
expect(importedKey).to.deep.equal(key);
});

it('does not attempt to import a key that is already in the key store', async () => {
// scenario: a key already exists within your key store, you attempt to import it again
// the method should not throw an error, but should also not attempt to store the key again

const storeSetSpy = sinon.spy(testHarness.agent.keyManager['_keyStore'], 'set');

// generate a key within the key manager
const keyUri = await testHarness.agent.keyManager.generateKey({ algorithm: 'secp256k1' });
expect(storeSetSpy.callCount).to.equal(1);

// export the key
const exportedKey = await testHarness.agent.keyManager.exportKey({ keyUri });

// reset the spy count
storeSetSpy.resetHistory();

// attempt to import the same key, will not fail
await testHarness.agent.keyManager.importKey({ key: exportedKey });
expect(storeSetSpy.callCount).to.equal(0); //

// sanity import a key generated outside the key manager
const key = await Ed25519.generateKey();
await testHarness.agent.keyManager.importKey({ key });
expect(storeSetSpy.callCount).to.equal(1);
});
});

describe('exportKey()', () => {
it('exports a private key as a JWK', async () => {
const keyUri = await testHarness.agent.keyManager.generateKey({ algorithm: 'secp256k1' });
Expand Down

0 comments on commit 7c33a05

Please sign in to comment.