fix: catch error when unmarshaling instead of crashing (#72)

This commit is contained in:
Maciej Krüger
2017-12-01 09:49:50 +01:00
committed by David Dias
parent f91f2b6506
commit 156911e162
4 changed files with 64 additions and 19 deletions

View File

@ -165,12 +165,20 @@ exports.createFromPubKey = function (key, callback) {
throw new Error('callback is required')
}
let buf = key
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}
let pubKey
const pubKey = crypto.keys.unmarshalPublicKey(buf)
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 = crypto.keys.unmarshalPublicKey(buf)
} catch (err) {
return callback(err)
}
pubKey.hash((err, digest) => {
if (err) {
@ -183,15 +191,22 @@ exports.createFromPubKey = function (key, callback) {
// Private key input will be a string
exports.createFromPrivKey = function (key, callback) {
let buf = key
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}
if (typeof callback !== 'function') {
throw new Error('callback is required')
}
let buf = key
try {
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')
} catch (err) {
return callback(err)
}
waterfall([
(cb) => crypto.keys.unmarshalPrivateKey(buf, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
@ -211,10 +226,19 @@ exports.createFromJSON = function (obj, callback) {
throw new Error('callback is required')
}
const id = mh.fromB58String(obj.id)
const rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
const rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
const pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey)
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([