From 42886f8eef5e5e450a6951270dd60d414eed8fb2 Mon Sep 17 00:00:00 2001 From: Alberto Elias Date: Tue, 19 Feb 2019 12:41:54 +0100 Subject: [PATCH] fix: fixes importing and exporting eliptic curve keys and adds tests --- package.json | 2 +- src/keychain.js | 11 +++++------ test/keychain.spec.js | 44 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index aa48e66..fadf42d 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "async": "^2.6.1", "interface-datastore": "~0.6.0", "libp2p-crypto": "~0.16.0", - "libp2p-crypto-secp256k1": "~0.2.3", + "libp2p-crypto-secp256k1": "AlbertoElias/js-libp2p-crypto-secp256k1#patch-1", "merge-options": "^1.0.1", "node-forge": "~0.7.6", "pull-stream": "^3.6.8", diff --git a/src/keychain.js b/src/keychain.js index 7cc6932..db07c14 100644 --- a/src/keychain.js +++ b/src/keychain.js @@ -214,7 +214,7 @@ class Keychain { if (err) return _error(callback, err) if (type === 'ed25519' || type === 'secp256k1') { - const keypairMarshal = keypair.marshal() + const keypairMarshal = keypair.bytes self._storeKey(name, kid, keypairMarshal, dsname, callback) } else { keypair.export(this._(), (err, pem) => { @@ -379,14 +379,14 @@ class Keychain { if (err) { return _error(callback, `Key '${name}' does not exist. ${err.message}`) } - const encKey = res.toString() if (password) { + const encKey = res.toString() crypto.keys.import(encKey, this._(), (err, privateKey) => { if (err) return _error(callback, err) privateKey.export(password, callback) }) } else { - crypto.keys.unmarshalPrivateKey(encKey, callback) + crypto.keys.unmarshalPrivateKey(res, callback) } }) } @@ -426,10 +426,9 @@ class Keychain { }) }) } else { - const privateKey = crypto.keys.marshalPrivateKey(encKey) - privateKey.id((err, kid) => { + encKey.id((err, kid) => { if (err) return _error(callback, err) - self._storeKey(name, kid, encKey, dsname, callback) + self._storeKey(name, kid, encKey.bytes, dsname, callback) }) } }) diff --git a/test/keychain.spec.js b/test/keychain.spec.js index 86d549f..e5643e4 100644 --- a/test/keychain.spec.js +++ b/test/keychain.spec.js @@ -16,6 +16,8 @@ module.exports = (datastore1, datastore2) => { const keyName = 'tajné jméno' const renamedKeyName = 'ชื่อลับ' let keyInfo + let ecKeyInfo + let secpKeyInfo let emptyKeystore let ks @@ -85,15 +87,17 @@ module.exports = (datastore1, datastore2) => { ks.createKey(keyName + 'ed25519', 'ed25519', 2048, (err, info) => { expect(err).to.not.exist() expect(info).exist() + ecKeyInfo = info done() }) }) - xit('can be an secp256k1 key', function (done) { + it('can be an secp256k1 key', function (done) { this.timeout(50 * 1000) ks.createKey(keyName + 'secp256k1', 'secp256k1', 2048, (err, info) => { expect(err).to.not.exist() expect(info).exist() + secpKeyInfo = info done() }) }) @@ -280,6 +284,8 @@ module.exports = (datastore1, datastore2) => { describe('exported key', () => { let pemKey + let ed25519Key + let secp256k1Key it('is a PKCS #8 encrypted pem', (done) => { ks.exportKey(keyName, 'password', (err, pem) => { @@ -299,6 +305,42 @@ module.exports = (datastore1, datastore2) => { }) }) + it('can export ed25519 key', (done) => { + ks.exportKey(keyName + 'ed25519', null, (err, key) => { + expect(err).to.not.exist() + ed25519Key = key + expect(key).to.exist() + done() + }) + }) + + it('ed25519 key can be imported', (done) => { + ks.importKey('imported-key-ed25199', ed25519Key, null, (err, key) => { + expect(err).to.not.exist() + expect(key.name).to.equal('imported-key-ed25199') + expect(key.id).to.equal(ecKeyInfo.id) + done() + }) + }) + + it('can export secp256k1 key', (done) => { + ks.exportKey(keyName + 'secp256k1', null, (err, key) => { + expect(err).to.not.exist() + secp256k1Key = key + expect(key).to.exist() + done() + }) + }) + + it('secp256k1 key can be imported', (done) => { + ks.importKey('imported-key-secp256k1', secp256k1Key, null, (err, key) => { + expect(err).to.not.exist() + expect(key.name).to.equal('imported-key-secp256k1') + expect(key.id).to.equal(secpKeyInfo.id) + done() + }) + }) + it('cannot be imported as an existing key name', (done) => { ks.importKey(keyName, pemKey, 'password', (err, key) => { expect(err).to.exist()