Skip to content

Commit

Permalink
Add from() test for publicKeyJwk.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Mar 18, 2024
1 parent e6642e7 commit 8542c20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ export async function generate({id, controller, seed} = {}) {
// import key pair from JSON Multikey
export async function from(key) {
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});
}
multikey = await toMultikey({keyPair: multikey});
return _createKeyPairInterface({keyPair: multikey});
if(multikey.type) {
multikey = await toMultikey({keyPair: multikey});
return _createKeyPairInterface({keyPair: multikey});
}
}
if(!multikey.type) {
multikey.type = 'Multikey';
Expand Down
18 changes: 18 additions & 0 deletions test/Ed25519Multikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ describe('Ed25519Multikey', () => {
expect(await imported.export({publicKey: true, secretKey: true}))
.to.eql(exported);
});
it('should load `publicKeyJwk`', async () => {
const jwk = {
crv: 'Ed25519',
kty: 'OKP',
x: '11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo'
};
const imported1 = await Ed25519Multikey.from({
publicKeyJwk: jwk
});
const imported2 = await Ed25519Multikey.from({
type: 'JsonWebKey',
publicKeyJwk: jwk
});
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 8542c20

Please sign in to comment.