From 4830de48b450244f959c7cf4f81f3e112610fc19 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 18 Mar 2024 17:50:48 -0400 Subject: [PATCH] fix: strip out the multibase prefix on encode, add it back if its not there on decode --- src/multihash.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/multihash.ts b/src/multihash.ts index 04a98f0..0f6a976 100644 --- a/src/multihash.ts +++ b/src/multihash.ts @@ -22,16 +22,23 @@ export class Multihash { while (hash.length % 4 !== 0) { hash += "="; } + if (hash[0] !== "u") { + hash = "u" + hash; + } const bytes = base64url.decode(hash); return new Multihash(new Uint8Array(bytes)); } toBase64Url(): string { - return base64url.encode(this.fullBytes); + return base64url.encode(this.fullBytes).substring(1); } toBase32(): string { - return base32.encode(this.fullBytes).replace(/=/g, "").toLowerCase(); + return base32 + .encode(this.fullBytes) + .replace(/=/g, "") + .toLowerCase() + .substring(1); } toString(): string {