Skip to content

Commit

Permalink
Include id and controller when importing JWK types.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Oct 2, 2024
1 parent 0d83135 commit 633768c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 15 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand All @@ -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});
}
Expand Down
27 changes: 27 additions & 0 deletions test/Ed25519Multikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 633768c

Please sign in to comment.