Fix Base58 padding for string representation of binary data (#4527).

This commit is contained in:
Richard Moore 2024-01-02 19:11:11 -05:00
parent f6d155c820
commit ccac24a5b0

View File

@ -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;
}