Skip to content

Commit

Permalink
Fix Base58 padding for string representation of binary data (#4527).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jan 3, 2024
1 parent f6d155c commit ccac24a
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src.ts/utils/base58.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ const BN_58 = BigInt(58);
* Encode %%value%% as a Base58-encoded string.
*/
export function encodeBase58(_value: BytesLike): string {
let value = toBigInt(getBytes(_value));
const bytes = getBytes(_value);

let value = toBigInt(bytes);
let result = "";
while (value) {
result = Alphabet[Number(value % BN_58)] + result;
value /= BN_58;
}

// Account for leading padding zeros
for (let i = 0; i < bytes.length; i++) {
if (bytes[i]) { break; }
result = Alphabet[0] + result;
}

return result;
}

Expand Down

0 comments on commit ccac24a

Please sign in to comment.