I'm trying to sign a coinbase transaction. I know I need to use 76 A9 14 (HASH160) 88 AC
but I don't understand how to create the HASH160 (20 byte) from a bitcoin address.
4 Answers
Check the Wiki. I think what you are looking for is point 3:
How to create Bitcoin Address
0 - Having a private ECDSA key
18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725
1 - Take the corresponding public key generated with it (65 bytes, 1 byte 0x04, 32 bytes corresponding to X coordinate, 32 bytes corresponding to Y coordinate)
0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
2 - Perform SHA-256 hashing on the public key
600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408
3 - Perform RIPEMD-160 hashing on the result of SHA-256
010966776006953D5567439E5E39F86A0D273BEE
4 - Add version byte in front of RIPEMD-160 hash (0x00 for Main Network)
00010966776006953D5567439E5E39F86A0D273BEE
5 - Perform SHA-256 hash on the extended RIPEMD-160 result
445C7A8007A93D8733188288BB320A8FE2DEBD2AE1B47F0F50BC10BAE845C094
6 - Perform SHA-256 hash on the result of the previous SHA-256 hash
D61967F63C7DD183914A4AE452C9F6AD5D462CE3D277798075B107615C1A8A30
7 - Take the first 4 bytes of the second SHA-256 hash. This is the address checksum
D61967F6
8 - Add the 4 checksum bytes from point 7 at the end of extended RIPEMD-160 hash from point 4. This is the 25-byte binary Bitcoin Address.
00010966776006953D5567439E5E39F86A0D273BEED61967F6
9 - Convert the result from a byte string into a base58 string using Base58Check encoding. This is the most commonly used Bitcoin Address format
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
You can check your calculations against my testing suite.
-
1Thanks! Unfortunately, point 0 is where I get lost... Does calculating the hash require access to the private key? Can I sign a coinbase transaction using a bitcoin address bypassing the need to calculate the HASH160? Oct 9, 2012 at 16:44
-
@user789235 If you want to SPEND the transaction, you need a private key. If you want to specify the recipient of the transaction, you only need to put the address in and some ops. Read here for more info en.bitcoin.it/wiki/Script#Scripts Oct 9, 2012 at 18:19
-
Thank you. I'm pretty sure I get it. So, I now want to create a signature of a testnet coinbase transaction. Is there an easier way to get the hash than starting with generating a public/private key pair from scratch? Oct 9, 2012 at 23:38
-
@user789235 If you just want to create a coinbase transaction that credits a given address their coins, you just specify the public key or address in the output script. If you want to create a transaction that spends the coinbase input, I have no idea how to do that. Oct 10, 2012 at 10:31
-
Could you incorporate part of the info from the wiki in your answer so that the answer is not dependant on "point 3" on the wiki staying the same. Thanks!– D.H.Oct 10, 2012 at 20:07
This wiki page is probably more helpful. It describes how the bitcoin address is created from the 160 bit hash. You need to run those steps backwards to get back to the 160 bit hash.
For example, if your address is 1HgiEYL6fsKrfh8wuMhAGfvSc6PY5ZXJdv, you first count and remove the 1's from the start. There's only one '1' in most addresses. Remember this for later.
With the rest of the address, use this page to convert each character into its base58 value: 'H' -> 16, 'g' -> 39, 'i' -> 41, ..., 'v' -> 53 etc. So you have a list of values: [16, 39, 41, ..., 53].
Then combine all these values into a single large number:
(([...](((16) * 58 + 39) * 58 + 41) ...) * 58 + 53)
Convert the single large number into a sequence of bytes, prepend the same number of 00 bytes as the number of '1's you initially removed from the address, remove the last 4 bytes (that's the checksum), remove the first zero byte (that's the version byte), and there's your 160 bit hash.
-
Perfect! I'll start the calculation. As a lazy mathematician's way of getting what I'm looking for, I came up with this approach. I'm not sure it's valid, but I send 20btc on testnet to the address for which I wanted a HASH160. That transaction is here I think I can use the hash from this transaction. But I'll do the math to confirm.... Oct 10, 2012 at 17:14
-
I don't think the testnet client will accept a regular bitcoin address as being valid. Maybe I've misunderstood what you did. Oct 11, 2012 at 3:27
For regular addresses you can use the addresstohash tool in blockexplorer.
For testnet addresses you can open https://blockexplorer.com/testnet/address/the address you want
and read the Hash160 field. This works for addresses which aren't in the block chain too.
You can use Casascius address utility to do various manipulations with keys, hashes and addresses.