Skip to content

Commit

Permalink
Move fromJWK and Raw tests to multikey.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Jul 5, 2024
1 parent 7443bc2 commit 9ca8a66
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 93 deletions.
93 changes: 0 additions & 93 deletions test/EcdsaMultikey.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
*/
import * as base58 from 'base58-universal';
import * as EcdsaMultikey from '../lib/index.js';
import chai from 'chai';
import {
Expand Down Expand Up @@ -64,98 +63,6 @@ describe('EcdsaMultikey', () => {
});
});

describe('fromJwk/toJwk', () => {
it('should round-trip secret JWKs', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve: 'P-256'
});
const jwk1 = await EcdsaMultikey.toJwk({keyPair, secretKey: true});
should.exist(jwk1.d);
const keyPairImported = await EcdsaMultikey.fromJwk(
{jwk: jwk1, secretKey: true});
const jwk2 = await EcdsaMultikey.toJwk(
{keyPair: keyPairImported, secretKey: true});
expect(jwk1).to.eql(jwk2);
});

it('should round-trip public JWKs', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve: 'P-256'
});
const jwk1 = await EcdsaMultikey.toJwk({keyPair});
should.not.exist(jwk1.d);
const keyPairImported = await EcdsaMultikey.fromJwk({jwk: jwk1});
const jwk2 = await EcdsaMultikey.toJwk({keyPair: keyPairImported});
expect(jwk1).to.eql(jwk2);
});
});

describe('fromRaw', () => {
it('should import raw public key', async () => {
const curve = 'P-256';
const keyPair = await EcdsaMultikey.generate({curve});

// first export
const expectedPublicKey = base58.decode(
keyPair.publicKeyMultibase.slice(1)).slice(2);
const {publicKey} = await keyPair.export({publicKey: true, raw: true});
expect(expectedPublicKey).to.deep.equal(publicKey);

// then import
const imported = await EcdsaMultikey.fromRaw({curve, publicKey});

// then re-export to confirm
const {publicKey: publicKey2} = await imported.export(
{publicKey: true, raw: true});
expect(expectedPublicKey).to.deep.equal(publicKey2);
});

it('should import raw secret key', async () => {
const curve = 'P-256';
const keyPair = await EcdsaMultikey.generate({curve});

// first export
const expectedSecretKey = base58.decode(
keyPair.secretKeyMultibase.slice(1)).slice(2);
const {secretKey, publicKey} = await keyPair.export(
{secretKey: true, raw: true});
expect(expectedSecretKey).to.deep.equal(secretKey);

// then import
const imported = await EcdsaMultikey.fromRaw(
{curve, secretKey, publicKey});

// then re-export to confirm
const {secretKey: secretKey2} = await imported.export(
{secretKey: true, raw: true});
expect(expectedSecretKey).to.deep.equal(secretKey2);
});

it('should import raw secret key for key agreement', async () => {
const curve = 'P-256';
const keyPair = await EcdsaMultikey.generate({curve, keyAgreement: true});

// first export
const expectedSecretKey = base58.decode(
keyPair.secretKeyMultibase.slice(1)).slice(2);
const {secretKey, publicKey} = await keyPair.export(
{secretKey: true, raw: true});
expect(expectedSecretKey).to.deep.equal(secretKey);

// then import
const imported = await EcdsaMultikey.fromRaw(
{curve, secretKey, publicKey, keyAgreement: true});
expect(imported.keyAgreement).to.equal(true);

// then re-export to confirm
const {secretKey: secretKey2} = await imported.export(
{secretKey: true, raw: true});
expect(expectedSecretKey).to.deep.equal(secretKey2);
});
});

describe('Backwards compat with EcdsaSecp256r1VerificationKey2019', () => {
it('Multikey should import properly', async () => {
const keyPair = await EcdsaMultikey.from(mockKeyEcdsaSecp256);
Expand Down
90 changes: 90 additions & 0 deletions test/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,96 @@ export function testFrom({serializedKeyPair, id, keyType}) {
});
}

export function testJWK({curve}) {
it('should round-trip secret JWKs', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve
});
const jwk1 = await EcdsaMultikey.toJwk({keyPair, secretKey: true});
expect(jwk1.d).to.exist;
const keyPairImported = await EcdsaMultikey.fromJwk(
{jwk: jwk1, secretKey: true});
const jwk2 = await EcdsaMultikey.toJwk(
{keyPair: keyPairImported, secretKey: true});
expect(jwk1).to.eql(jwk2);
});

it('should round-trip public JWKs', async () => {
const keyPair = await EcdsaMultikey.generate({
id: '4e0db4260c87cc200df3',
curve
});
const jwk1 = await EcdsaMultikey.toJwk({keyPair});
expect(jwk1.d).to.not.exist

Check failure on line 249 in test/assertions.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Missing semicolon
const keyPairImported = await EcdsaMultikey.fromJwk({jwk: jwk1});
const jwk2 = await EcdsaMultikey.toJwk({keyPair: keyPairImported});
expect(jwk1).to.eql(jwk2);
});
}

export function testRaw({curve}) {
it('should import raw public key', async () => {
const keyPair = await EcdsaMultikey.generate({curve});

// first export
const expectedPublicKey = base58.decode(
keyPair.publicKeyMultibase.slice(1)).slice(2);
const {publicKey} = await keyPair.export({publicKey: true, raw: true});
expect(expectedPublicKey).to.deep.equal(publicKey);

// then import
const imported = await EcdsaMultikey.fromRaw({curve, publicKey});

// then re-export to confirm
const {publicKey: publicKey2} = await imported.export(
{publicKey: true, raw: true});
expect(expectedPublicKey).to.deep.equal(publicKey2);
});

it('should import raw secret key', async () => {
const keyPair = await EcdsaMultikey.generate({curve});

// first export
const expectedSecretKey = base58.decode(
keyPair.secretKeyMultibase.slice(1)).slice(2);
const {secretKey, publicKey} = await keyPair.export(
{secretKey: true, raw: true});
expect(expectedSecretKey).to.deep.equal(secretKey);

// then import
const imported = await EcdsaMultikey.fromRaw(
{curve, secretKey, publicKey});

// then re-export to confirm
const {secretKey: secretKey2} = await imported.export(
{secretKey: true, raw: true});
expect(expectedSecretKey).to.deep.equal(secretKey2);
});

it('should import raw secret key for key agreement', async () => {
const keyPair = await EcdsaMultikey.generate({curve, keyAgreement: true});

// first export
const expectedSecretKey = base58.decode(
keyPair.secretKeyMultibase.slice(1)).slice(2);
const {secretKey, publicKey} = await keyPair.export(
{secretKey: true, raw: true});
expect(expectedSecretKey).to.deep.equal(secretKey);

// then import
const imported = await EcdsaMultikey.fromRaw(
{curve, secretKey, publicKey, keyAgreement: true});
expect(imported.keyAgreement).to.equal(true);

// then re-export to confirm
const {secretKey: secretKey2} = await imported.export(
{secretKey: true, raw: true});
expect(expectedSecretKey).to.deep.equal(secretKey2);
});

}

function _ensurePublicKeyEncoding({keyPair, publicKeyMultibase, keyType}) {
keyPair.publicKeyMultibase.startsWith('z').should.be.true;
publicKeyMultibase.startsWith('z').should.be.true;
Expand Down
8 changes: 8 additions & 0 deletions test/multikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
testExport,
testFrom,
testGenerate,
testJWK,
testRaw,
testSignVerify
} from './assertions.js';

Expand All @@ -34,6 +36,12 @@ describe('ecdsa-multikey', function() {
describe('from', function() {
testFrom({keyType, id, serializedKeyPair});
});
describe('fromJwk/toJwk', () => {
testJWK({curve: keyType});
});
describe('fromRaw', () => {
testRaw({curve: keyType});
});
});
}
});

0 comments on commit 9ca8a66

Please sign in to comment.