fix: strip out the multibase prefix on encode, add it back if its not there on decode

This commit is contained in:
Derrick Hammer 2024-03-18 17:50:48 -04:00
parent 0c47ef6e53
commit 4830de48b4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 9 additions and 2 deletions

View File

@ -22,16 +22,23 @@ export class Multihash {
while (hash.length % 4 !== 0) { while (hash.length % 4 !== 0) {
hash += "="; hash += "=";
} }
if (hash[0] !== "u") {
hash = "u" + hash;
}
const bytes = base64url.decode(hash); const bytes = base64url.decode(hash);
return new Multihash(new Uint8Array(bytes)); return new Multihash(new Uint8Array(bytes));
} }
toBase64Url(): string { toBase64Url(): string {
return base64url.encode(this.fullBytes); return base64url.encode(this.fullBytes).substring(1);
} }
toBase32(): string { toBase32(): string {
return base32.encode(this.fullBytes).replace(/=/g, "").toLowerCase(); return base32
.encode(this.fullBytes)
.replace(/=/g, "")
.toLowerCase()
.substring(1);
} }
toString(): string { toString(): string {