refactor: add optional raw parameter
This commit is contained in:
parent
01920ecf16
commit
d2a20610ce
|
@ -14,18 +14,21 @@ export function encodeCid(
|
||||||
size: bigint,
|
size: bigint,
|
||||||
type?: number,
|
type?: number,
|
||||||
hashType?: number,
|
hashType?: number,
|
||||||
|
raw?: boolean,
|
||||||
);
|
);
|
||||||
export function encodeCid(
|
export function encodeCid(
|
||||||
hash: string,
|
hash: string,
|
||||||
size: bigint,
|
size: bigint,
|
||||||
type?: number,
|
type?: number,
|
||||||
hashType?: number,
|
hashType?: number,
|
||||||
|
raw?: boolean,
|
||||||
);
|
);
|
||||||
export function encodeCid(
|
export function encodeCid(
|
||||||
hash: any,
|
hash: any,
|
||||||
size: bigint,
|
size: bigint,
|
||||||
type = CID_TYPES.RAW,
|
type = CID_TYPES.RAW,
|
||||||
hashType = CID_HASH_TYPES.BLAKE3,
|
hashType = CID_HASH_TYPES.BLAKE3,
|
||||||
|
raw: boolean = false,
|
||||||
) {
|
) {
|
||||||
if (typeof hash === "string") {
|
if (typeof hash === "string") {
|
||||||
hash = edUtils.hexToBytes(hash);
|
hash = edUtils.hexToBytes(hash);
|
||||||
|
@ -46,6 +49,11 @@ export function encodeCid(
|
||||||
sizeView.setBigInt64(0, size, true);
|
sizeView.setBigInt64(0, size, true);
|
||||||
|
|
||||||
const prefixedHash = Uint8Array.from([type, hashType, ...hash, ...sizeBytes]);
|
const prefixedHash = Uint8Array.from([type, hashType, ...hash, ...sizeBytes]);
|
||||||
|
|
||||||
|
if (raw) {
|
||||||
|
return prefixedHash;
|
||||||
|
}
|
||||||
|
|
||||||
return base58btc.encode(prefixedHash).toString();
|
return base58btc.encode(prefixedHash).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue