From a1af67c3a7faf36da236ba45eac19a95ac7f73f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kr=C3=BCger?= Date: Thu, 21 Jun 2018 19:42:05 +0200 Subject: [PATCH] fix: #79 check if pubKey equals id and de-dup some code --- src/index.js | 92 ++++++++++++++++---------------------------- test/peer-id.spec.js | 2 +- 2 files changed, 35 insertions(+), 59 deletions(-) diff --git a/src/index.js b/src/index.js index 1c92cd7..58e87a9 100644 --- a/src/index.js +++ b/src/index.js @@ -231,28 +231,10 @@ exports.createFromPrivKey = function (key, callback) { }) } -exports.createFromProtobuf = function (buf, callback) { - if (typeof callback !== 'function') { - throw new Error('callback is required') - } - - let obj - let id - let rawPrivKey - let rawPubKey - let pub +function createFromRawData(id, rawPubKey, rawPrivKey, callback) { + const pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey) - try { - obj = PeerIdProto.decode(buf) - id = obj.id - rawPrivKey = obj.privKey - rawPubKey = obj.pubKey - pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey) - } catch (err) { - return callback(err) - } - - if (rawPrivKey) { + if (rawPrivKey) { // private [public] [id] waterfall([ (cb) => crypto.keys.unmarshalPrivateKey(rawPrivKey, cb), (priv, cb) => priv.public.hash((err, digest) => { @@ -282,11 +264,39 @@ exports.createFromProtobuf = function (buf, callback) { callback(null, new PeerId(id, priv, pub)) }) - } else { - callback(null, new PeerId(id, null, pub)) + } else if (pub) { // public [id] + pub.hash((err, digest) => { + if (err) { + return callback(err) + } + + if (id && !digest.equals(id)) { + return callback(new Error('Id and public key do not match')) + } + + callback(null, new PeerId(id, null, pub)) + }) + } else { // id + callback(null, new PeerId(id)) } } +exports.createFromProtobuf = function (buf, callback) { + if (typeof callback !== 'function') { + throw new Error('callback is required') + } + + let obj + + try { + obj = PeerIdProto.decode(buf) + } catch (err) { + return callback(err) + } + + createFromRawData(obj.id, obj.pubKey, obj.privKey, callback) +} + exports.createFromJSON = function (obj, callback) { if (typeof callback !== 'function') { throw new Error('callback is required') @@ -295,50 +305,16 @@ exports.createFromJSON = function (obj, callback) { let id let rawPrivKey let rawPubKey - let pub try { id = mh.fromB58String(obj.id) rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64') rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64') - pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey) } catch (err) { return callback(err) } - if (rawPrivKey) { - waterfall([ - (cb) => crypto.keys.unmarshalPrivateKey(rawPrivKey, cb), - (priv, cb) => priv.public.hash((err, digest) => { - cb(err, digest, priv) - }), - (privDigest, priv, cb) => { - if (pub) { - pub.hash((err, pubDigest) => { - cb(err, privDigest, priv, pubDigest) - }) - } else { - cb(null, privDigest, priv) - } - } - ], (err, privDigest, priv, pubDigest) => { - if (err) { - return callback(err) - } - - if (pub && !privDigest.equals(pubDigest)) { - return callback(new Error('Public and private key do not match')) - } - - if (id && !privDigest.equals(id)) { - return callback(new Error('Id and private key do not match')) - } - - callback(null, new PeerId(id, priv, pub)) - }) - } else { - callback(null, new PeerId(id, null, pub)) - } + createFromRawData(id, rawPubKey, rawPrivKey, callback) } exports.isPeerId = function (peerId) { diff --git a/test/peer-id.spec.js b/test/peer-id.spec.js index 5511238..200fd87 100644 --- a/test/peer-id.spec.js +++ b/test/peer-id.spec.js @@ -299,7 +299,7 @@ describe('PeerId', () => { describe('returns error via cb instead of crashing', () => { const garbage = [Buffer.from('00010203040506070809', 'hex'), {}, null, false, undefined, true, 1, 0, Buffer.from(''), 'aGVsbG93b3JsZA==', 'helloworld', ''] - const fncs = ['createFromPubKey', 'createFromPrivKey', 'createFromJSON'] + const fncs = ['createFromPubKey', 'createFromPrivKey', 'createFromJSON', 'createFromProtobuf'] garbage.forEach(garbage => { fncs.forEach(fnc => {