Why my Bitcoin address starts with number 1?

Bitcoin in Action
4 min readJul 9, 2020

--

Hello,

today we answer to Marco. He asks:

Why my Bitcoin address starts with number 1? I have seen other addresses start with the number 3. What’s difference are there?

Nice question Marco. Your address starts with number 1 because it uses the script Pay to Public Key Hash (P2PKH). Bitcoin creates the address using cryptographic functions on script, in P2PKH it get prefix 1 after applying SHA256 and RIPEMD160 on compressed public key, coming before version prefix. In the video course and in our book Bitcoin from theory to practice, we are able to get an address P2PKH from scratch.

When you find an address starting with number 3, it means that it uses the script Pay to script hash (P2SH). It also means that if you find an address starting with bc1, it means that it uses SegWit.

In reality, there aren’t any address, it is the result of digest on script.

We use to call the result “address”.Now, let’s go! We are going to analyze P2PKH in practice!

In Action

In this example, we are using mainnet environment. If you don’t have a full node, check this article! (yes, for now it’s in italian language, do you want translate? Send me an email! :) )

Use the method getnewaddress:

$ bitcoin-cli getnewaddress “” “legacy”1ExUh5vqcUjuuocAgnSyAJ74iBiRHBwnAU

I get an address P2PKH. The second parameter is empty, it’s a label and is useful when you search a particular address in Bitcoin Core.

To retrieve more informations I use the method getaddressinfo.

$ bitcoin-cli getaddressinfo 1ExUh5vqcUjuuocAgnSyAJ74iBiRHBwnAU{"address": "1ExUh5vqcUjuuocAgnSyAJ74iBiRHBwnAU","scriptPubKey": "76a9149917a0e372343fde946253d1c60c7f479925b1e288ac","ismine": true,"solvable": true,"desc": "pkh([5975a740/0'/0'/2']02413df2a3977bb663c4fc7418e7e004129c5cfc2d3d2bb6c9210ea8ee13769610)#wsy7apvp","iswatchonly": false,"isscript": false,"iswitness": false,"pubkey": "02413df2a3977bb663c4fc7418e7e004129c5cfc2d3d2bb6c9210ea8ee13769610","iscompressed": true,"label": "","ischange": false,"timestamp": 1588801569,"hdkeypath": "m/0'/0'/2'","hdseedid": "27eae26f0861d81dfde79537fdbb0b8273f00211","hdmasterfingerprint": "5975a740","labels": [{"name": "","purpose": "receive"}]}

It returns a bunch of information, like compressed public key, derivation path and the scriptPubKey.

In scriptPubKey we can find the conditions to satisfy in order to unlock the UTXO (Unspent Transaction Output).

Using another method of Bitcoin core, we can go more deeply.

$ bitcoin-cli decodescript 76a9149917a0e372343fde946253d1c60c7f479925b1e288ac{“asm”: “OP_DUP OP_HASH160 9917a0e372343fde946253d1c60c7f479925b1e2 OP_EQUALVERIFY OP_CHECKSIG”,“reqSigs”: 1,“type”: “pubkeyhash”,“addresses”: [“1ExUh5vqcUjuuocAgnSyAJ74iBiRHBwnAU”],“p2sh”: “3Ae5soRJUqiqZgfvFxVaLTZs2PrdqvjDTL”,“segwit”: {“asm”: “0 9917a0e372343fde946253d1c60c7f479925b1e2”,“hex”: “00149917a0e372343fde946253d1c60c7f479925b1e2”,“reqSigs”: 1,“type”: “witness_v0_keyhash”,“addresses”: [“bc1qnyt6pcmjxslaa9rz20guvrrlg7vjtv0ztsmjag”],“p2sh-segwit”: “3Mi5vTHySS8iCRpK2TWnTXsxU8FTDHQ3SQ”}}

We get a very interesting information. We can find several kind of address, like P2SH, wrapped SegWit and native Segwit. On the first line we can see P2PKH, asm means assembly. The Operation codes are linked to Script language. Anytime we use P2PKH, a function HASH160 is applied on the public key, and the result must be equal to UTXO related to.

For that reason it’s called Public Key Hash. Obviously the script checks even the signature.

As we said at the beginning, the address is simply a digest of SHA256 and RIPEMD160 on compressed public key, that comes before version prefix. In Bitcoin Wiki we can that version prefix for mainnet environment is one byte of 0 (00).

I use env variable to store the values. I save the digest SHA256 of Compressed public key in $ADDR_SHA.

$ ADDR_SHA=`printf 02413df2a3977bb663c4fc7418e7e004129c5cfc2d3d2bb6c9210ea8ee13769610 | xxd -r -p | openssl sha256| sed ‘s/^.* //’`

I use the digest in order to give another digest from the cryptographic function RIPEMD160.

$ ADDR_RIPEMD160=`printf $ADDR_SHA |xxd -r -p | openssl ripemd160 | sed ‘s/^.* //’`

Please he last step: I get the address, using base58 with checksum.

Please note, base58 is not a cryptographic function, it’s an encoding function.

$ printf 00$ADDR_RIPEMD160 | xxd -p -r | base58 -c1ExUh5vqcUjuuocAgnSyAJ74iBiRHBwnAU

Thanks to this example, we understand how and why P2PKH starts with number 1. Remember, you will be able to generate an address P2PKH from scratch with our book Bitcoin from theory to practice. Are you excited?

See ya!

--

--

Bitcoin in Action
Bitcoin in Action

Written by Bitcoin in Action

Bitcoin. Money for nerds? Digital gold? Perhaps, but certainly it is a social revolution!

No responses yet