import { CID } from "multiformats/cid";
/**
 * Decode a possibly string-encoded CID.
 * Passing an already decoded CID instance works too.
 * @throws Throws an error if a CID cannot be decoded!
 */
export function decodeCID(val) {
    const cid = CID.asCID(val);
    if (cid)
        return cid;
    if (typeof val === "string")
        return CID.parse(val);
    if (typeof val === "object" && "version" in val && "code" in val && "multihash" in val) {
        return CID.create(val.version, val.code, val.multihash);
    }
    throw new Error(`Could not decode CID: ${val}`);
}
/**
 * Encode a CID as a string.
 */
export function encodeCID(cid) {
    return cid.toString();
}
//# sourceMappingURL=cid.js.map