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', () => {