Bitcoin Addresses & What is Bech32 (bc1)

in #bitcoin6 years ago

Address validation

Rather than just checking for string length, allowed characters (0-9, A-Z, a-z), or that the address starts with a 1, 3 or bc1, a bitcoin address can be validate with a checksum built into the address. For a 25-byte legacy address, the last 4 bytes of the address is actually a checksum which is part of the double SHA-256 hash of the previous 21 bytes in the address. Therefore, with a wallet software that implement such checksum validation function, a mistyped address by user would be recognised and rejected when the checksum was computed by the first 21 bytes and compare with the one existing in the address.

Bitcoin Address Formats

There are currently 3 address formats in use:
Legacy Address (P2PKH): begins with the number 1
Segwit & Multisig Address (P2SH): starting with the number 3 (better backward compatibility)
Segwit & Multisig Address (Bech32): starting with bc1 (newer native Segwit format)

Legacy P2PKH (Pay-to-PubkeyHash) Address simply locks the unspent fund with the redeemer hashed public key and unlock the fund by comparing Pubkey Hash
P2SH (Pay-to-Script-Hash) Address allows funds to be locked in conditions like multisig in a basic smart contract way.

Bech32 Development

Bech32 is implemented to bitcoin as BIP173 in late 2017 as an updated P2SH format that is less compatible with older softwares right now but it offers more optimal efficiency and security when more softwares support it.

One of the main differences of Bech32 format is that it is not case sensitive (only includes 0-9, a-z in the address), hence it avoids confusion when writing down and is easier to read out. As less characters are needed in the address, the address is encoded in Base 32 instead of the traditional Base 58 which is more convenient and efficient in computation, and also more widely adopted in other computer application. Data can be stored a lot more compactly to in QR code.

The double SHA256 checksum with the original Base 58 is slow and has no error-detection guarantees. Bech32 offers higher security with better optimise error detection code to the checksum to minimise the chance of accepting an invalid address. Most of the research on error-detecting codes only applies to Base 32 like formats making Bech32 more mature in security and technology.

Currently, only less than 1% of BTC is stored in Bech32 addresses in the whole network but the adoption rate has been growing since Dec 2017. Wallets such as Bitcoin Core, Electrum, Samourai Wallet, Coinomi are now fully supporting Bech32 addresses. Hardware wallets like Trezor and Ledger can use such addresses when pair with Electrum wallet. However, most exchanges and bitcoin ATM are not fully supporting the this format yet and among all the major exchanges only Coinbase supports withdrawal to Bech32 addresses. Although this address format is less popular right now, the adoption rate is rising just like the Segwit adoption by major wallets and exchanges few months before. This address format change is believed to be beneficial to the users to by protecting them from mistakes and confusion. Before we can really see easily readable words address being implemented, Bech32 could boost mass adoption by improving the user experience.

References

http://rosettacode.org/wiki/Bitcoin/address_validation#C
https://medium.com/@hrobeers/p2sh-bitcoin-script-puzzle-explained-26c8cb03ff90
https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki