From af66adf145dda6681d14aa64a7f8e65d9ba0cc72 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Sun, 17 Mar 2024 22:12:03 -0400 Subject: [PATCH] Add load from `publicKeyJwk` tests. --- lib/index.js | 10 ++++++---- test/EcdsaMultikey.spec.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index e7bcc23..ce5368b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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'; diff --git a/test/EcdsaMultikey.spec.js b/test/EcdsaMultikey.spec.js index 1e450f3..031bd83 100644 --- a/test/EcdsaMultikey.spec.js +++ b/test/EcdsaMultikey.spec.js @@ -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', () => {