refactor: add optional raw parameter

This commit is contained in:
Derrick Hammer 2023-09-03 13:07:59 -04:00
parent 01920ecf16
commit d2a20610ce
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 8 additions and 0 deletions

View File

@ -14,18 +14,21 @@ export function encodeCid(
size: bigint,
type?: number,
hashType?: number,
raw?: boolean,
);
export function encodeCid(
hash: string,
size: bigint,
type?: number,
hashType?: number,
raw?: boolean,
);
export function encodeCid(
hash: any,
size: bigint,
type = CID_TYPES.RAW,
hashType = CID_HASH_TYPES.BLAKE3,
raw: boolean = false,
) {
if (typeof hash === "string") {
hash = edUtils.hexToBytes(hash);
@ -46,6 +49,11 @@ export function encodeCid(
sizeView.setBigInt64(0, size, true);
const prefixedHash = Uint8Array.from([type, hashType, ...hash, ...sizeBytes]);
if (raw) {
return prefixedHash;
}
return base58btc.encode(prefixedHash).toString();
}