Addresses in Bitcoin

Addresses in Bitcoin

One of the ways of proving ownership of bitcoin is through a bitcoin address. Similar to having an account number to which cash can be sent, Bitcoin has addresses where the cryptocurrency can be sent. A bitcoin address is not permanent due to privacy, hence diverse addresses are created for transactions.

The addresses consist of 26-35 alphanumeric characters, and can be generated from a public key or can also represent scripts. Conventionally, bitcoin addresses are derived from a public key using a one-way function, making it impossible to derive the private key from the public key.

Address Formats in Bitcoin

As there are different types of bank accounts, there are also different bitcoin address formats. Some wallets support only one format, while some are compatible with almost all the formats. These address formats depend on what the locking script(scriptPubKey) contains, on the off chance that it contains the hash of a public key, at that point the address format will be P2PKH, in case it is the hash of a custom script, the address format will be P2SH

P2PK (Pay-to-Public Key)

P2PK is used to lock bitcoin to a public key, it basically means that the bitcoin can only be spent by the user that has the private key that corresponds to the public key used to lock the bitcoin.Unlike the conventional way of generating bitcoin address, the public key is directly used as the address.

In spite of it being the best script for locking bitcoin, it is hardly used. P2PK can be found in coinbase transactions in earlier blocks in the blockchain, it was used by miners for the block reward when constructing a candidate block. P2PK was also used for the first ever bitcoin transaction from Satoshi to Hal Finney.

Presently, P2PK is not used because it exposes the public key of the recipient, which makes the address vulnerable, public key has a length of 130 characters, which requires more care since it is prone to error due to the length, and any mistake implies the fund is lost. In addition, it requires a larger transaction fee, since the transaction size will be largely due to the length of the key.

P2PKH (Pay-to-Public Key Hash)

P2PKH is the traditional format bitcoin transaction started with, the script is present by default in wallets that implement the Bitcoin client. It is more secure than P2PK, since applying a hash to the public key implementing the SHA-256 algorithm makes it impossible to deduce the private key from the public key.

The P2PKH addresses always start with 1.

How P2PKH Works

If Alice wants to send bitcoins to Bob, Bob will start the process by generating the P2PKH address format from the public key using the SHA-256 and RIPEMD-160 functions, Bob then sends the address to Alice, which then starts the process of generating the transaction and the P2PKH script, when Alice enters the transaction details, her wallet will translate data into a P2PKH, that way they can send the bitcoin. The P2PKH script will be executed, passing the ownership of the coins to Bob since they have the public key corresponding to the hash.

P2PKH is more secure than P2PK since you do not have to send your public address to anyone, which makes it less prone to attack. However, P2PK is outdated and is not compatible with most wallets, probably because the average fee when sending from a P2PKH address is usually higher due to the transaction size. The scriptPubKey will look like this:

OP_DUP OP_HASH160 {the public key hash} OP_EQUALVERIFY OP_CHECKSIG

P2SH (Pay-to-Script Hash)

P2SH was introduced as part of BIP16 in early 2012. Unlike P2PKH, P2SH does not use the hash of the public key, instead, it uses the hash of a script that involves certain spending conditions, which is not revealed to the sender. It is an easy way to represent a scriptPubKey as a bitcoin address, it allows you to lock the bitcoins in the script hash and then provide the original script when the bitcoins are unlocked for a transaction. The P2SH addresses always start with 3

How P2SH Works

P2SH can be best used by multi-signature address, Bitcoin allows to share ownership of coins with a multi-signature transaction or multi-sig, a scriptPubKey that represents an m-of-n multi-sig is created, this means that in order to spend the coins, m number of private keys will be needed to sign the spending transaction out of the n number of different public keys provided.

Before P2SH, the process of sending bitcoin to a multi-sig address was complicated, you need to send the newly created, unsigned multi-sig transaction to every signer and after they signed it, you also need to collect the partially signed transactions from them and combine them into one, after which it can be published on the network.

With P2SH, paying into a multi-sig scriptPubKey became as simple as just providing a bitcoin address. A redeem script is created, which can have the condition that 2 out 3 of the keys need to sign before the coins can be spent, the hash of this script is then sent to the sender, who has no idea that it is a multi-sig, once the coins have been sent the script hash has those coins locked to it, and the only way to spend them is to enter the original script (the redeem script) used to create the hash, which can include digital signatures and pubkey verifications.

P2SH moves the responsibility of providing the full redeem script from the sender to the recipient, which reduces the transaction fee of the sender as a fixed-length hash lets the sender send funds to any arbitrary redeem script without worrying about paying higher fees. It is the responsibility of the recipient who creates the redeem script to determine how large their spending transaction will be and how much it will cost. It is also more secure since it’s impossible to know what kind of locking script that hash came from by just looking at it. P2SH however, takes up more space in the blockchain which can influence the network's operating capacity. The scriptPubKey of P2SH will look like this:

OP_HASH160 {the hash of the redeem script} OP_EQUAL

P2WPKH (Pay-to-Witness-Pulic Key-Hash)

P2WPKH is an advanced type of address which helps to reduce blockchain block size thereby speeding up transaction response time. In 2015, a new feature was added to bitcoin which is called Segregrated Witness, this moves the proof of ownership from the scriptsig part to the witness part.

P2WPKH is relatively similar to P2PKH, the main difference is that the public key hash is not contained in the scriptsig but the witness. This helps to drastically reduce the transaction sending fee and allows for high processing speed, however, most wallets do not support SegWit as there are still lots of contorversy surrounding it. P2WPKH addresses start with "bc1q", as they are Bech32-encoded.

P2WSH (Pay-to-Witness-Script-Hash)

Just like P2PKH and P2WPKH, P2WSH and P2SH is similar, the difference is just the location of the script hash which will now be in the witness instead of the scriptsig. It has the same benefits as P2WPKH, since they are both SegWit addresses. The scriptPubKey will look like this:

OP_0 {the 32 byte Witness Script hash}

P2TR (Pay-to-Tap Root)

P2TR, also known as TapRoot address is the most recent bitcoin address format, it is like a privacy upgrade which makes it possible to make more complex transactions exactly like normal transactions. It is a type of ScriptPubKey which locks bitcoin to a script that can be unlocked by a public key or a Merkelized Alternative Script Tree (MAST), enabling the coin to be spent in various ways.

The taproot upgrade consist of 3 distinct BIPs, which are:

  • BIP 340 : this proposes the introduction of Schnoor signature for the bitcoin blockchain, schnnor signature is a cryptographic scheme that creates short and efficient digital signature,while retaining a high level of security. With schnoor signature, a single aggregated public key and signature are recorded on the blockchain for a multisignature transaction,rather than all the public keys and signatures involved
  • BIP 341(Taproot): this is concerned about how schnoor signature is to be integrated into the Bitcoin network. It explains how bitcoin script can be updated to evaluate and integrate schnoor signatures using the Merkelized alternative script trees, which ensures that only the executed conditions of the smart contract transaction is committed to the blockchain, rather than the full details of all other possible outcomes.
  • BIP 342(Tapscript): this is concerned with updating and adding opcodes, which help to verify Taproot spends and Schnorr signatures.

In my opinion, P2TR is really a great upgrade to the Bitcoin network. Using a multi-sig transaction as an example, most multi-sig transaction has conditions that have to be met in order to spend the coins, the conditions can be written as smart contract, in this smart contract, there can be various ways which the coins can be spent. Regardless of what condition is executed, a multi-sig transaction still need a m of n signature and in most cases the signature is more than one, while SegWit helped to solve the hassle of having to send transaction to each signatory, the signatures will be recorded in the blockchain. Taproot, however takes the signatures or keys involved and create a short and efficient signature using the schnoor signature scheme, then when one of the conditions to spend the coins has been met and it is now spendable, taproot ensures that only the condition executed is recorded on the blockchain.

P2TR is more secured than any other address format, it does not distinguish between a single sig and multi-sig transaction, this way, no one knows what type of wallet was used for the transaction. It also helps to save up space on the blockchain, thereby increasing the efficiency of transaction verification. P2TR was implemented into the network in 2021 after many back and forth, it is a Bech32 encoded address just like SegWit, and it starts with "bc1p".

Understanding the different formats is imperative so as to know when to use it and what type of wallets to have, this article helps to give users a basic understanding about how bitcoin address formats work and benefit of using them. All the address formats ensures privacy, as that is one of the principles of Bitcoin,the recent formats has however helped to improve privacy and scale the network.