Often, the average bitcoiner is not aware that multiple blockchains exist. Most people transparently connect to the main network, because wallet software by default operates in the realm of real money. In fact, you’re going to discover 3 different Bitcoin networks with their own peculiarities.
As an user, I expect you to know that Bitcoin is backed by a vast peer-to-peer network. To be more precise, there are 2 public subnetworks:
with the “3” suffix meaning the 3rd generation of Testnet. Under normal operation wallet clients connect to Mainnet, the only network where bitcoins have concrete value in real life. Testnet name is self-explanatory, it’s the best choice for testing and software development since Testnet coins are worthless.
The third network is sort a virtual network and is called Regtest. The name stands for “regression test” and it’s a specific mode of Bitcoin Core in which local blocks are mined immediately. Regtest is therefore substantially different from Mainnet and Testnet3, where miners must comply with a certain set of consensus rules. Quick blocks make Regtest particularly interesting: it makes possible creating custom blockchains with negligible processing power.
Mainnet and Testnet3 mainly differ in the default TCP port for p2p access and the prefix of transmitted packets (little-endian):
Additionally, Testnet3 validation rules are much more relaxed thus making room for experimental transactions.
Let alone the genesis block, blockchains are completely network agnostic. That is, blocks and transactions embed no information about the network they live in. There’s no way to tell the network that broadcast a transaction from the transaction data alone. Incidentally, a transaction can be published as-is to both Mainnet and Testnet3.
To avoid ambiguity, user shared entities (namely private keys and addresses) were made network-dependent by adding magic prefixes. Later we’ll learn what these prefixes are and how they affect the way keys and addresses are displayed.
The Base58 encoding
There are entities that even the average Bitcoin user is bound to deal with: keys and addresses. In an attempt to make them shorter and slightly more human-readable than hexes, Bitcoin introduces the Base58 encoding. Base58 is a stripped down version of Base64, a widely known scheme to display binary data in ASCII text. Here’s how Base58 differs:
- Base58 only has alphanumeric symbols, no
- Base58 excludes
lfor visual ambiguity.
- Base58 strings are fully selectable with a double-click.
- Base58 symbol values follow the ASCII ordering.
The complete Base58 alphabet:
With such a mapping, the Base58 encoding is just a number base conversion. The only tricky part comes from big numbers, as seen in base58.h. Beware of endianness, contrary to the majority of blockchain objects Base58 strings are encoded big-endian.
Since Base58 strings are long and unfriendly, a checksum suffix is typically appended to avoid typos. The checksum consists of the first 4 bytes of hash256(payload). The (payload + checksum) format is called Base58Check. Steps:
- Perform hash256 on the payload bytes.
- The first 4 bytes of the hash256 digest are the checksum.
- Append the checksum to the payload.
- Encode the result to Base58.
Base58Check examples will be treated in the second part.
Get the code!
Full source on GitHub.
Next block in chain?
You learned that there are 3 different Bitcoin networks. Real money flows on Mainnet, while test money resides on Testnet3. Regtest is for testing local, custom blockchains. All the long strings shared for bitcoin transfers are encoded Base58.
In the next article I’ll complete the big picture with keys serialization and addresses, the typical transaction endpoints. Please share this post if you enjoyed it and use the form below for questions and comments!