Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.createFromPubKey = function (key, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
}
let pubKey
try {
let buf = key
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}
if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer')
pubKey = cryptoKeys.unmarshalPublicKey(buf)
} catch (err) {
return callback(err)
}
pubKey.hash((err, digest) => {
if (err) {
return callback(err)
}
callback(null, new PeerIdWithIs(digest, null, pubKey))
})
}
exports.createFromJSON = function (obj, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
}
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 && cryptoKeys.unmarshalPublicKey(rawPubKey)
} catch (err) {
return callback(err)
}
if (rawPrivKey) {
waterfall([
(cb) => cryptoKeys.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 {