Skip to content

Commit

Permalink
Move raw export feature to export() using raw param.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Nov 4, 2023
1 parent 2e67527 commit 71b4031
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 12 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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});
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/EcdsaMultikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ 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);
});

it('should export raw secret key', async () => {
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);
});
});
Expand Down

0 comments on commit 71b4031

Please sign in to comment.