Skip to content

Commit

Permalink
Add load from publicKeyJwk tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Mar 18, 2024
1 parent 6cd31a0 commit 2b73cfc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ export async function from(key, options = {}) {
const {keyAgreement} = options;

let multikey = {...key};
if(multikey.type && multikey.type !== 'Multikey') {
if(multikey.type !== 'Multikey') {
// attempt loading from JWK if `publicKeyJwk` is present
if(multikey.publicKeyJwk) {
return fromJwk({jwk: multikey.publicKeyJwk, secretKey: true});
return fromJwk({jwk: multikey.publicKeyJwk, secretKey: false});
}
if(multikey.type) {
multikey = await toMultikey({keyPair: multikey});
return _createKeyPairInterface({keyPair: multikey, keyAgreement});
}
multikey = await toMultikey({keyPair: multikey});
return _createKeyPairInterface({keyPair: multikey, keyAgreement});
}
if(!multikey.type) {
multikey.type = 'Multikey';
Expand Down
18 changes: 18 additions & 0 deletions test/EcdsaMultikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ describe('EcdsaMultikey', () => {
expect(await keyPairImported.export({publicKey: true, secretKey: true}))
.to.eql(keyPairExported);
});

it('should load `publicKeyJwk`', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve: 'P-256'
});
const jwk1 = await EcdsaMultikey.toJwk({keyPair});
should.not.exist(jwk1.d);
const keyPairImported1 = await EcdsaMultikey.from({publicKeyJwk: jwk1});
const keyPairImported2 = await EcdsaMultikey.from({
type: 'JsonWebKey',
publicKeyJwk: jwk1
});
const jwk2 = await EcdsaMultikey.toJwk({keyPair: keyPairImported1});
const jwk3 = await EcdsaMultikey.toJwk({keyPair: keyPairImported2});
expect(jwk1).to.eql(jwk2);
expect(jwk1).to.eql(jwk3);
});
});

describe('fromJwk/toJwk', () => {
Expand Down

0 comments on commit 2b73cfc

Please sign in to comment.