Skip to content

Commit

Permalink
fix: libp2p#79 check if pubkey equals id and de-dup some code
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Jul 28, 2018
1 parent be1c3e6 commit 2448e21
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 59 deletions.
92 changes: 34 additions & 58 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,28 +241,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) => {
Expand Down Expand Up @@ -292,11 +274,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')
Expand All @@ -305,50 +315,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) {
Expand Down
2 changes: 1 addition & 1 deletion test/peer-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,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 => {
Expand Down

0 comments on commit 2448e21

Please sign in to comment.