diff --git a/CHANGELOG.md b/CHANGELOG.md index 05e39c6..b5fbf35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,10 @@ ## 1.4.0 - 2023-11-dd ### Added -- Add `exportRawSecretKey()` and `exportRawPublicKey()` to key pair interface. +- Add `raw` option to key pair `export()`. Based on the requested public/secret + key, the output will include the raw bytes for the public/secret key using + the properties `publicKey` and/or `secretKey`, respectively. The public key + will be output using the compressed format. ## 1.3.0 - 2023-10-31 diff --git a/lib/index.js b/lib/index.js index ba89acc..51420d9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -98,8 +98,19 @@ async function _createKeyPairInterface({keyPair, keyAgreement = false}) { keyPair = await importKeyPair(keyPair); } const exportFn = async ({ - publicKey = true, secretKey = false, includeContext = true + publicKey = true, secretKey = false, includeContext = true, raw = false } = {}) => { + if(raw) { + const jwk = await toJwk({keyPair, secretKey}); + const result = {}; + if(publicKey) { + result.publicKey = toPublicKeyBytes({jwk}); + } + if(secretKey) { + result.secretKey = toSecretKeyBytes({jwk}); + } + return result; + } return exportKeyPair({keyPair, publicKey, secretKey, includeContext}); }; const {publicKeyMultibase, secretKeyMultibase} = await exportFn({ @@ -127,14 +138,6 @@ async function _createKeyPairInterface({keyPair, keyAgreement = false}) { } return _deriveSecret( {localKeyPair: this, remoteKeyPair: remotePublicKey}); - }, - async exportRawPublicKey() { - const jwk = await toJwk({keyPair: this}); - return toPublicKeyBytes({jwk}); - }, - async exportRawSecretKey() { - const jwk = await toJwk({keyPair: this, secretKey: true}); - return toSecretKeyBytes({jwk}); } }; diff --git a/test/EcdsaMultikey.spec.js b/test/EcdsaMultikey.spec.js index b7419ab..c056986 100644 --- a/test/EcdsaMultikey.spec.js +++ b/test/EcdsaMultikey.spec.js @@ -151,7 +151,7 @@ describe('EcdsaMultikey', () => { const keyPair = await EcdsaMultikey.generate({curve: 'P-256'}); const expectedPublicKey = base58.decode( keyPair.publicKeyMultibase.slice(1)).slice(2); - const publicKey = await keyPair.exportRawPublicKey(); + const {publicKey} = await keyPair.export({publicKey: true, raw: true}); expect(expectedPublicKey).to.deep.equal(publicKey); }); @@ -159,7 +159,7 @@ describe('EcdsaMultikey', () => { const keyPair = await EcdsaMultikey.generate({curve: 'P-256'}); const expectedSecretKey = base58.decode( keyPair.secretKeyMultibase.slice(1)).slice(2); - const secretKey = await keyPair.exportRawSecretKey(); + const {secretKey} = await keyPair.export({secretKey: true, raw: true}); expect(expectedSecretKey).to.deep.equal(secretKey); }); });