From 9ca8a66a1dbbcda99eea2ff6967a63a0e80b0a38 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 5 Jul 2024 21:05:42 +0000 Subject: [PATCH] Move fromJWK and Raw tests to multikey. --- test/EcdsaMultikey.spec.js | 93 -------------------------------------- test/assertions.js | 90 ++++++++++++++++++++++++++++++++++++ test/multikey.spec.js | 8 ++++ 3 files changed, 98 insertions(+), 93 deletions(-) diff --git a/test/EcdsaMultikey.spec.js b/test/EcdsaMultikey.spec.js index 03068cb..852b5a9 100644 --- a/test/EcdsaMultikey.spec.js +++ b/test/EcdsaMultikey.spec.js @@ -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 { @@ -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); diff --git a/test/assertions.js b/test/assertions.js index 390cd33..6e1f555 100644 --- a/test/assertions.js +++ b/test/assertions.js @@ -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 + 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; diff --git a/test/multikey.spec.js b/test/multikey.spec.js index 88e7d46..b807299 100644 --- a/test/multikey.spec.js +++ b/test/multikey.spec.js @@ -8,6 +8,8 @@ import { testExport, testFrom, testGenerate, + testJWK, + testRaw, testSignVerify } from './assertions.js'; @@ -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}); + }); }); } });