53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
'use strict'
|
|
|
|
const mh = require('multihashes')
|
|
const { Buffer } = require('buffer')
|
|
var CIDUtil = {
|
|
/**
|
|
* Test if the given input is a valid CID object.
|
|
* Returns an error message if it is not.
|
|
* Returns undefined if it is a valid CID.
|
|
*
|
|
* @param {any} other
|
|
* @returns {string}
|
|
*/
|
|
checkCIDComponents: function (other) {
|
|
if (other == null) {
|
|
return 'null values are not valid CIDs'
|
|
}
|
|
|
|
if (!(other.version === 0 || other.version === 1)) {
|
|
return 'Invalid version, must be a number equal to 1 or 0'
|
|
}
|
|
|
|
if (typeof other.codec !== 'string') {
|
|
return 'codec must be string'
|
|
}
|
|
|
|
if (other.version === 0) {
|
|
if (other.codec !== 'dag-pb') {
|
|
return "codec must be 'dag-pb' for CIDv0"
|
|
}
|
|
if (other.multibaseName !== 'base58btc') {
|
|
return "multibaseName must be 'base58btc' for CIDv0"
|
|
}
|
|
}
|
|
|
|
if (!Buffer.isBuffer(other.multihash)) {
|
|
return 'multihash must be a Buffer'
|
|
}
|
|
|
|
try {
|
|
mh.validate(other.multihash)
|
|
} catch (err) {
|
|
let errorMsg = err.message
|
|
if (!errorMsg) { // Just in case mh.validate() throws an error with empty error message
|
|
errorMsg = 'Multihash validation failed'
|
|
}
|
|
return errorMsg
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = CIDUtil
|