Skip to content

Commit

Permalink
Move round trip-loaded exports to multikey spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Jul 5, 2024
1 parent 8882a67 commit 7443bc2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
50 changes: 0 additions & 50 deletions test/EcdsaMultikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('EcdsaMultikey', () => {
});

describe('from', () => {

it('should error if publicKeyMultibase property is missing', async () => {
let error;
try {
Expand All @@ -63,55 +62,6 @@ describe('EcdsaMultikey', () => {
expect(error.message)
.to.equal('The "publicKeyMultibase" property is required.');
});

it('should round-trip load exported keys', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve: 'P-256'
});
const keyPairExported = await keyPair.export({
publicKey: true, secretKey: true
});
const keyPairImported = await EcdsaMultikey.from(keyPairExported);

expect(await keyPairImported.export({publicKey: true, secretKey: true}))
.to.eql(keyPairExported);
});

it('should import with `@context` array', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve: 'P-256'
});
const keyPairExported = await keyPair.export({
publicKey: true, secretKey: true
});
const keyPairImported = await EcdsaMultikey.from({
...keyPairExported,
'@context': [{}, keyPairExported['@context']]
});

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', () => {
Expand Down
48 changes: 47 additions & 1 deletion test/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {webcrypto} from '../lib/crypto.js';
import {exportKeyPair} from '../lib/serialize.js';
import {getNamedCurveFromPublicMultikey} from '../lib/helpers.js';

chai.should();
const {expect} = chai;

export function testSignVerify({id, serializedKeyPair}) {
Expand Down Expand Up @@ -177,6 +176,53 @@ export function testFrom({serializedKeyPair, id, keyType}) {
_ensurePublicKeyEncoding({keyPair, keyType, publicKeyMultibase});
expect(keyPair.id).to.equal(id);
});
it('should round-trip load exported keys', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve: keyType
});
const keyPairExported = await keyPair.export({
publicKey: true, secretKey: true
});
const keyPairImported = await EcdsaMultikey.from(keyPairExported);

expect(await keyPairImported.export({publicKey: true, secretKey: true}))
.to.eql(keyPairExported);
});

it('should import with `@context` array', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve: keyType
});
const keyPairExported = await keyPair.export({
publicKey: true, secretKey: true
});
const keyPairImported = await EcdsaMultikey.from({
...keyPairExported,
'@context': [{}, keyPairExported['@context']]
});

expect(await keyPairImported.export({publicKey: true, secretKey: true}))
.to.eql(keyPairExported);
});
it('should load `publicKeyJwk`', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve: keyType
});
const jwk1 = await EcdsaMultikey.toJwk({keyPair});
expect(jwk1.d).to.not.exist;
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);
});
}

function _ensurePublicKeyEncoding({keyPair, publicKeyMultibase, keyType}) {
Expand Down

0 comments on commit 7443bc2

Please sign in to comment.