diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b04582..7bee7ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @digitalbazaar/ed25519-multikey ChangeLog +## 1.3.0 - 2024-10-dd + +### Added +- Include `id` and `controller` properties when importing key types of + `JsonWebKey` or `JsonWebKey2020`. + ## 1.2.0 - 2024-08-20 ### Added diff --git a/lib/index.js b/lib/index.js index 847718a..5f00c86 100644 --- a/lib/index.js +++ b/lib/index.js @@ -43,7 +43,14 @@ export async function from(key) { if(multikey.type !== 'Multikey') { // attempt loading from JWK if `publicKeyJwk` is present if(multikey.publicKeyJwk) { - return fromJwk({jwk: multikey.publicKeyJwk, secretKey: false}); + let id; + let controller; + if(multikey.type === 'JsonWebKey' || multikey.type === 'JsonWebKey2020') { + ({id, controller} = multikey); + } + return fromJwk({ + jwk: multikey.publicKeyJwk, secretKey: false, id, controller + }); } if(multikey.type) { multikey = await toMultikey({keyPair: multikey}); @@ -62,12 +69,18 @@ export async function from(key) { } // imports key pair from JWK -export async function fromJwk({jwk, secretKey = false} = {}) { +export async function fromJwk({jwk, secretKey = false, id, controller} = {}) { const multikey = { '@context': MULTIKEY_CONTEXT_V1_URL, type: 'Multikey', publicKeyMultibase: jwkToPublicKeyMultibase({jwk}) }; + if(typeof id === 'string') { + multikey.id = id; + } + if(typeof controller === 'string') { + multikey.controller = controller; + } if(secretKey && jwk.d) { multikey.secretKeyMultibase = jwkToSecretKeyMultibase({jwk}); } diff --git a/test/Ed25519Multikey.spec.js b/test/Ed25519Multikey.spec.js index a902328..20a562b 100644 --- a/test/Ed25519Multikey.spec.js +++ b/test/Ed25519Multikey.spec.js @@ -186,6 +186,33 @@ describe('Ed25519Multikey', () => { expect(exported1).to.eql(jwk); expect(exported2).to.eql(jwk); }); + it('should load `publicKeyJwk` w/id and controller', async () => { + const jwk = { + crv: 'Ed25519', + kty: 'OKP', + x: '11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo' + }; + const imported1 = await Ed25519Multikey.from({ + type: 'JsonWebKey', + id: 'urn:id:1#0', + controller: 'urn:id:1', + publicKeyJwk: jwk + }); + const imported2 = await Ed25519Multikey.from({ + type: 'JsonWebKey', + id: 'urn:id:1#0', + controller: 'urn:id:1', + publicKeyJwk: jwk + }); + imported1.id.should.equal('urn:id:1#0'); + imported1.controller.should.equal('urn:id:1'); + imported2.id.should.equal('urn:id:1#0'); + imported2.controller.should.equal('urn:id:1'); + const exported1 = await Ed25519Multikey.toJwk({keyPair: imported1}); + const exported2 = await Ed25519Multikey.toJwk({keyPair: imported2}); + expect(exported1).to.eql(jwk); + expect(exported2).to.eql(jwk); + }); }); describe('Backwards compat with Ed25519VerificationKey2018', () => {