1

Bitcoin address is 25 bytes long, address = [Version Byte (1)][Hash 160 (20)][Checksum (4)] whereas number of characters in this string are 34.

Same with private key: 32-byte have almost 52 characters.

I want to know how can I map number of characters to the size(Bytes) of the bitcoin address(P2PKH)?

3
  • As its name implies the ripemd160 function will always output 20 bytes (160 bits). The P2PKH address will always be 25 bytes long. – darosior Jun 18 '20 at 13:53
  • That’s not correct. Leading null bytes are sequestered, encoding to addresses down to 26 characters long. – Anonymous Jun 18 '20 at 14:35
  • when you start to code then only you know. Number of characters depends on how the variable is declared, is it byte, hex or character. For example, if you may think you declare privKey="1223a34fdcb...." in hex format but the program may interpret it as ascii character 1,2,2 etc. So, you have to double checked. – Cisco Mmu Jun 18 '20 at 16:43
2

There are many types of Bitcoin addresses. Your example specifies (legacy) pay-to-pubkey-hash (P2PKH) address defined by Base58 check encoding and Wallet Import Format (WIF) which is also specified by base58 check

Base58 encoding treats the entire input payload as a big-endian big integer. Consider these alternate examples:

Input value: bc in ASCII: 0x62 0x63 (2 bytes) interpreted as a 2-byte int and encoded without leading zeros...

base 2: 110001001100011 (15 characters)

base 10: 25187 (5 characters)

base 16: 6263 (4 characters)

base 58: 8VG (3 characters)

So going forwards we know that for this example it took 3 base58 characters to encode two bytes. Lets run it backwards and try minimum and maximum values:

base 58, maximum value with 3 characters zzz encodes three byte value: 0x02fa27

base 58, minimum value with 3 characters 211 encodes two byte value: 0x0d24

Conclusion:

From this example alone we discover that we can not determine the length in bytes of the original value based just on the length in characters of the encoded string.

Bonus:

Modern addresses are now encoded with bech32 and represent Segregated Witness programs. The bech32 spec is defined in BIP173 and look like this: bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4

bech32 encoding does not use interpretation as big integers and instead maps input bytes more directly to output characters:

The data-part values:

  • 1 byte: the witness version
  • A conversion of the 2-to-40-byte witness program (as defined by BIP141) to base32:
    • Start with the bits of the witness program, most significant bit per byte first.
    • Re-arrange those bits into groups of 5, and pad with zeroes at the end if needed.
    • Translate those bits to characters using the table above.

As a result of the previous rules, addresses are always between 14 and 74 characters long, and their length modulo 8 cannot be 0, 3, or 5. Version 0 witness addresses are always 42 or 62 characters, but implementations MUST allow the use of any version.

So in summary, with a bech32 address you can determine the length in bytes of the input value based on the length in characters of the encoded string:

In bech32, each character represents 5 bits

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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