I'm trying to more fully understand the base58check encoding algorithm.

Unfortunately, I keep running into a problem with how to handle a situation where the base58 value of an input would have leading 0's (1's in base58). Everywhere I find any information about this online, it simply states that "the data is encoded to avoid having leading 0s, except to represent full '0' bytes in then input."

What I need to understand is: What is changed in the data (input or output) to avoid/correct this situation in the output string?

1 Answer 1


Iterate from the beginning until you reach a non-zero byte. Base58 encode the non-zero byte and the following, and add 1s before the base58 string you made for each initial one zero byte (0x00) you counted.

Zero bytes after the first non-zero byte don't need special treatment.

For example, [0, 13, 36], is encoded as 1211 since the value is 1*58^2 and 1 in base58 alphabet is 2. From here, you see that if it's a multiple of 58, no special treatment is needed since the encoding is big endian and the 1s caused by 58's powers are automatically appended to the back

  • That's the part that is explained...and I explained that in my question. What do I do when the data becomes evenly divisible by 58 "too soon"? Just trim the leading 0s? Jan 24, 2021 at 14:58
  • "the data is encoded to avoid having leading 0s, except to represent full '0' bytes in then input." Jan 24, 2021 at 15:00
  • @RobhercKV5ROB OK, updated
    – MCCCS
    Jan 24, 2021 at 15:22
  • Ok, if there's no special treatment given, just divide modulo 58 until you reach 0, then my base58 conversion code must not be the culprit....**scratches head** I wrote my own offline cold-wallet generator using 4 passwords to generate a repeatable private key (i.e. same 4 passwords will always generate same private key) and it has worked perfectly 99% of the time. Unfortunately, 1 key I generated (WIF format) threw a "hash check failed" type error when I checked it against 3 known reliable wallet address (from private key) generators. Jan 24, 2021 at 15:30
  • @RobhercKV5ROB You can verify your Base58 implementation by entering the byte sequence to be encoded to box 8 here: gobittest.appspot.com/Address This website is useful because you can also enter the private key (assumming it has no money) and it'll show you all the steps so you can find the culprit
    – MCCCS
    Jan 24, 2021 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.