Address

Address

An address is what you give to people so that they can "send" you bitcoins.

When someone receives it, they can create a specific locking script based on the type of address you have given them.

How do you create an address?

Well, that depends on how you'd like your bitcoins to be locked up.

But in general, an address contains:

  1. Some specific data that you would like included in the lock. For example, your hash160(publickey).
  2. A prefix to indicate what kind of lock to create.
  3. And a checksum to help with catching any typos.

Finally, all of that gets converted to Base58, which makes it a little more user-friendly.

Pay To PubKey Hash (P2PKH)

This is a typical address that locks bitcoins to a public key (or to be more precise: the hash of a public key).

As mentioned, we append a prefix and prepend a checksum to our hashed public key, then encode it all in base58.

Now we have an address to give to people.

Decoded:

When someone creates a locking script from this address, they just decode the base58 to retrieve the hash160 inside it, then create a P2PKH lock around it, as follows:

So the prefix indicates what kind of lock to create, and the hash160 tells them what to put inside it.

Pay To Script Hash (P2SH)

This lock includes the hash of a locking script that someone else has constructed. This means that they will provide the actual locking script later on (when they come to unlock it), which means they can construct a more complex locking script without you having to worry about the details of it.

Same as before, except this time we're including the hash of a script, and using the prefix 05 to indicate a P2SH.

Decoded:

And this is what a P2SH looks like:

Prefixes

As mentioned, the prefix you use will indicate the type of locking script to create.

Here are a list of common address prefixes:

Prefix Locking Script Leading Character Example Address
00 P2PKH 1 1AKDDsfTh8uY4X3ppy1m7jw1fVMBSMkzjP
05 P2SH 3 34nSkinWC9rDDJiUY438qQN1JHmGqBHGW7
6F P2PKH (testnet) m / n ms2qxPw1Q2nTkm4eMHqe6mM7JAFqAwDhpB
C4 P2SH (testnet) 2 2MwSNRexxm3uhAKF696xq3ztdiqgMj36rJo

https://en.bitcoin.it/wiki/List_of_address_prefixes

The prefix will also alter the leading character of an address, so you can tell what kind of locking script has been used by just looking at the address itself.

Why do we use addresses?

An address is a short-hand way of writing locking scripts in a human-readable way. - echeveria (on IRC)

If we didn't use addresses, we would have to send other people complete locking scripts, like this:

76a914662ad25db00e7bb38bc04831ae48b4b446d1269888ac # P2PKH script

But by using addresses, we can just send something like this instead:

1AKDDsfTh8uY4X3ppy1m7jw1fVMBSMkzjP

They both achieve the same result, but addresses give us a more user-friendly format to pass around. Not to mention the fact that they contain a checksum, which means that errors can be detected if someone writes an address incorrectly.

Code

Note: This code requires the checksum.rb and base58_encode.rb functions.

def hash160_to_address(hash160, type=:p2pkh)
  prefixes = {
    p2pkh: '00',         # 1address - For standard bitcoin addresses
    p2sh:  '05',         # 3address - For sending to an address that requires multiple signatures (multisig)
    p2pkh_testnet: '6F', # (m/n)address
    p2sh_testnet:  'C4'  # 2address
  }

  prefix = prefixes[type]
  checksum = checksum(prefix + hash160)
  address = base58_encode(prefix + hash160 + checksum)

  return address
end

hash160 = '662ad25db00e7bb38bc04831ae48b4b446d12698'
puts hash160_to_address(hash160) # 1AKDDsfTh8uY4X3ppy1m7jw1fVMBSMkzjP

By Greg Walker,

Last Updated: Mar 14 2018
  • Mar 14 2018: glossary/address - Publishing (added links to it across glossary)
  • Mar 14 2018: More router stuff, and added address.md files