refactor: add support for decodeCid to accept a Uint8Array

This commit is contained in:
Derrick Hammer 2023-09-03 12:55:09 -04:00
parent b6722cf98d
commit 55e28b806c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 5 additions and 2 deletions

View File

@ -49,9 +49,12 @@ export function encodeCid(
return base58btc.encode(prefixedHash).toString(); return base58btc.encode(prefixedHash).toString();
} }
export function decodeCid(cid: string): CID { export function decodeCid(cid: string | Uint8Array): CID {
let bytes = base58btc.decode(cid); let bytes = cid;
if (typeof bytes === "string") {
bytes = base58btc.decode(bytes);
}
if (!Object.values(CID_TYPES).includes(bytes[0])) { if (!Object.values(CID_TYPES).includes(bytes[0])) {
throw new Error("Invalid cid type"); throw new Error("Invalid cid type");
} }