How many characters Bitcoin addresses can be?
Disclaimer
I don’t have any bitcoins, so I don’t lose anything if the following is incorrect. However let me know if you find any mistake.
The Story
You can find different pieces of information on the internet about the possible length of Bitcoin addresses, but not all of them are true.
Bitcoin addresses look different on the blockchain and when they are presented to users. Users don’t like binary encoding and they also want to avoid misreading and mistyping the addresses. The Bitcoin addresses we see are thus encoded for user convenience. There were two such encodings invented so far: Base58Check and Bech32.
Base58Check
The classic Bitcoin addresses use the Base58Check encoding.
The algorithm first builds a sequence of bytes the following way:
1 byte: version
20 byte: payload (the public key hash or the script hash)
4 byte: checksum
In the next step the sequence of bytes is interpreted as a base256 (radix 256) number and it’s converted to a base58 number. To not lose any information, the inventor of the algorithm decided to also copy the leading zero digits from the base256 number to the base58 number.
So here we are: we now encoded our fixed length sequence of bytes into a variable length sequence of characters, where the length depends on the values of the original bytes. But what are the limits of the length of a valid Bitcoin address?
When we convert a number to a numeral system with a smaller set of digits, we can expect that the resulting number will be longer. (One digit can encode less, so we need more of them.) But when we copy the leading zero digits, then we use the same number of digits in the output as in the input.
Intuitively the shortest Bitcoin address can be constructed by using as many leading zero bytes as possible. Except this is not true, as it was pointed out by user “morsecoder” at https://bitcoin.stackexchange.com/questions/36944/what-are-the-minimum-and-maximum-lengths-of-a-mainnet-bitcoin-address/36948#36948
The full zero version and payload fields (together with the proper checksum) give the 1111111111111111111114oLvT2 valid Bitcoin address, which consists of 27 characters. But it’s not the shortest possible valid Bitcoin address, because if we increment the payload, then the 5 non-zero bytes easily fit in the 6 base58 digits, and as we have one less 1-character (zero digit) in the output, our new Bitcoin address will be only 26 characters: 11111111111111111111BZbvjr. This illustrates an ugly property of the Base58Check encoding: the output length does not monotonically increase with the input value.
For analysis, it’s best to separate the Bitcoin address to 2 parts: the leading zero digits and the significant digits. The length of the second part now does monotonically increase with the input value, and we can easily calculate a length range based on the input range.
Here is a table (generated with minlength.py) with all the combinations of the leading zeros and the significant digits, as you can see we can’t go below the 26 character length:
┌───────────────┬───────────────────────┬──────────────────────┐
│ Leading zeros │ Minimum base58 digits │ Minimum total length │
├───────────────┼───────────────────────┼──────────────────────┤
│ 21 │ 6 │ 27 │
│ 20 │ 6 │ 26 │
│ 19 │ 7 │ 26 │
│ 18 │ 9 │ 27 │
│ 17 │ 10 │ 27 │
│ 16 │ 11 │ 27 │
│ 15 │ 13 │ 28 │
│ 14 │ 14 │ 28 │
│ 13 │ 16 │ 29 │
│ 12 │ 17 │ 29 │
│ 11 │ 18 │ 29 │
│ 10 │ 20 │ 30 │
│ 9 │ 21 │ 30 │
│ 8 │ 22 │ 30 │
│ 7 │ 24 │ 31 │
│ 6 │ 25 │ 31 │
│ 5 │ 26 │ 31 │
│ 4 │ 28 │ 32 │
│ 3 │ 29 │ 32 │
│ 2 │ 31 │ 33 │
│ 1 │ 32 │ 33 │
└───────────────┴───────────────────────┴──────────────────────┘
Regarding the maximum length, it very much depends on the version byte. If the version byte is 0 (address starts with “1”), then we can make a similar table (maxlength.py) and we can see that the maximum total length is 34 characters:
┌───────────────┬───────────────────────┬──────────────────────┐
│ Leading zeros │ Maximum base58 digits │ Maximum total length │
├───────────────┼───────────────────────┼──────────────────────┤
│ 21 │ 6 │ 27 │
│ 20 │ 7 │ 27 │
│ 19 │ 9 │ 28 │
│ 18 │ 10 │ 28 │
│ 17 │ 11 │ 28 │
│ 16 │ 13 │ 29 │
│ 15 │ 14 │ 29 │
│ 14 │ 16 │ 30 │
│ 13 │ 17 │ 30 │
│ 12 │ 18 │ 30 │
│ 11 │ 20 │ 31 │
│ 10 │ 21 │ 31 │
│ 9 │ 22 │ 31 │
│ 8 │ 24 │ 32 │
│ 7 │ 25 │ 32 │
│ 6 │ 26 │ 32 │
│ 5 │ 28 │ 33 │
│ 4 │ 29 │ 33 │
│ 3 │ 31 │ 34 │
│ 2 │ 32 │ 34 │
│ 1 │ 33 │ 34 │
└───────────────┴───────────────────────┴──────────────────────┘
If the version byte is not 0, then fortunately we can forget the leading zeros and we can simply convert the number to base58. Here I recomputed the table (prefix.py) from https://en.bitcoin.it/wiki/List_of_address_prefixes:
┌─────────────────┬────────────────┬────────────────┐
│ Decimal version │ Leading symbol │ Address length │
├─────────────────┼────────────────┼────────────────┤
│ 1 │ Q to o │ 33 │
│ 2 │ o to 2 │ 33 to 34 │
│ 3 │ 2 │ 34 │
│ 4 │ 2 to 3 │ 34 │
│ 5-6 │ 3 │ 34 │
│ 7 │ 3 to 4 │ 34 │
│ 8 │ 4 │ 34 │
│ 9 │ 4 to 5 │ 34 │
│ 10-11 │ 5 │ 34 │
│ 12 │ 5 to 6 │ 34 │
│ 13 │ 6 │ 34 │
│ 14 │ 6 to 7 │ 34 │
│ 15-16 │ 7 │ 34 │
│ 17 │ 7 to 8 │ 34 │
│ 18 │ 8 │ 34 │
│ 19 │ 8 to 9 │ 34 │
│ 20-21 │ 9 │ 34 │
│ 22 │ 9 to A │ 34 │
│ 23 │ A │ 34 │
│ 24 │ A to B │ 34 │
│ 25-26 │ B │ 34 │
│ 27 │ B to C │ 34 │
│ 28 │ C │ 34 │
│ 29 │ C to D │ 34 │
│ 30-31 │ D │ 34 │
│ 32 │ D to E │ 34 │
│ 33 │ E │ 34 │
│ 34 │ E to F │ 34 │
│ 35-36 │ F │ 34 │
│ 37 │ F to G │ 34 │
│ 38 │ G │ 34 │
│ 39 │ G to H │ 34 │
│ 40-41 │ H │ 34 │
│ 42 │ H to J │ 34 │
│ 43 │ J │ 34 │
│ 44 │ J to K │ 34 │
│ 45-46 │ K │ 34 │
│ 47 │ K to L │ 34 │
│ 48 │ L │ 34 │
│ 49 │ L to M │ 34 │
│ 50-51 │ M │ 34 │
│ 52 │ M to N │ 34 │
│ 53 │ N │ 34 │
│ 54 │ N to P │ 34 │
│ 55-56 │ P │ 34 │
│ 57 │ P to Q │ 34 │
│ 58 │ Q │ 34 │
│ 59 │ Q to R │ 34 │
│ 60-61 │ R │ 34 │
│ 62 │ R to S │ 34 │
│ 63 │ S │ 34 │
│ 64 │ S to T │ 34 │
│ 65-66 │ T │ 34 │
│ 67 │ T to U │ 34 │
│ 68 │ U │ 34 │
│ 69 │ U to V │ 34 │
│ 70-71 │ V │ 34 │
│ 72 │ V to W │ 34 │
│ 73 │ W │ 34 │
│ 74 │ W to X │ 34 │
│ 75-76 │ X │ 34 │
│ 77 │ X to Y │ 34 │
│ 78 │ Y │ 34 │
│ 79 │ Y to Z │ 34 │
│ 80-81 │ Z │ 34 │
│ 82 │ Z to a │ 34 │
│ 83 │ a │ 34 │
│ 84 │ a to b │ 34 │
│ 85 │ b │ 34 │
│ 86 │ b to c │ 34 │
│ 87-88 │ c │ 34 │
│ 89 │ c to d │ 34 │
│ 90 │ d │ 34 │
│ 91 │ d to e │ 34 │
│ 92-93 │ e │ 34 │
│ 94 │ e to f │ 34 │
│ 95 │ f │ 34 │
│ 96 │ f to g │ 34 │
│ 97-98 │ g │ 34 │
│ 99 │ g to h │ 34 │
│ 100 │ h │ 34 │
│ 101 │ h to i │ 34 │
│ 102-103 │ i │ 34 │
│ 104 │ i to j │ 34 │
│ 105 │ j │ 34 │
│ 106 │ j to k │ 34 │
│ 107-108 │ k │ 34 │
│ 109 │ k to m │ 34 │
│ 110 │ m │ 34 │
│ 111 │ m to n │ 34 │
│ 112-113 │ n │ 34 │
│ 114 │ n to o │ 34 │
│ 115 │ o │ 34 │
│ 116 │ o to p │ 34 │
│ 117-118 │ p │ 34 │
│ 119 │ p to q │ 34 │
│ 120 │ q │ 34 │
│ 121 │ q to r │ 34 │
│ 122-123 │ r │ 34 │
│ 124 │ r to s │ 34 │
│ 125 │ s │ 34 │
│ 126 │ s to t │ 34 │
│ 127-128 │ t │ 34 │
│ 129 │ t to u │ 34 │
│ 130 │ u │ 34 │
│ 131 │ u to v │ 34 │
│ 132-133 │ v │ 34 │
│ 134 │ v to w │ 34 │
│ 135 │ w │ 34 │
│ 136 │ w to x │ 34 │
│ 137-138 │ x │ 34 │
│ 139 │ x to y │ 34 │
│ 140 │ y │ 34 │
│ 141 │ y to z │ 34 │
│ 142-143 │ z │ 34 │
│ 144 │ z to 2 │ 34 to 35 │
│ 145-255 │ 2 │ 35 │
└─────────────────┴────────────────┴────────────────┘
Bech32
For completeness let’s now see the Bech32 addresses!
The Bech32 encoding (defined in BIP173) uses base32, and simply converts every 5 input bit to 1 output character. This has the advantage that if the input has fixed length, then the output will have fixed length, too.
There are 2 kinds of Bech32 addresses in use currently, and luckily both of them have fixed length. They are segwit addresses with the following general format:
2 character: human-readable part (“bc” for mainnet, “tb” for testnet)
1 character: separator (“1”)
1 character: witness version (“q”, meaning version 0)
n character: witness program encoded to base32
6 character: checksum
P2WPKH Bech32 addresses (having a 20 byte witness program) are 42 (20*8/5+10) character long. An example from BIP173 is bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4.
P2WSH Bech32 addresses (having a 32 byte witness program) are 62 (32*8/5+10 rounded up) character long. An example from BIP173 is bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3.
That’s it, Bech32 was easy.
Conclusion
- Bitcoin addresses starting with “1” are 26 to 34 characters.
- Bitcoin addresses starting with “3” are 34 characters.
- Bitcoin addresses starting with “bc1q” are 42 or 62 characters.