I'm trying to understand bitcoin better by trying things out in Ruby. I know some aspects of Bitcoin have already been
implemented in Ruby, but I'm trying to avoid "cheating" by studying those.
I read the wiki regarding
scripts as well as
addresses and I'm confused as to how to convert between the public key and the bitcoin address.
The scriptPubKey of a transaction output usually looks like this: OP_DUP OP_HASH160 <pubKeyHash>
OP_EQUALVERIFY OP_CHECKSIG. If I understand correctly, this <pubKeyHash> is the result of Base58 decoding the bitcoin address.
In ruby that can be done like this ("step 9" from the address page results in "step 8"):
require 'base58_gmp'
Base58GMP.decode("16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", "bitcoin").to_s(16)
So far so good (although it took a long time to realize bitcoin uses a non-standard form of Base58).
The scriptSig of a transaction input looks this this: <sig> <pubKey>. If I understand correctly <pubKey> here is the thing shown in step 1: "04508 ... 82BA6".
Now according to the scripts page, OP_HASH(<pubkey>) should result in <pubKeyHash>, but I'm having a hard time achieving that. OP_HASH is
supposed to only perform a SHA256 hash followed by an RMB160 hash. That looks like step 2 and step 3, but does not include step 4 through 8 (adding network info and a checksum). My guess is that I also need to follow those steps when I see OP_HASH.
Unfortunately I'm already stuck at step 2. If I try to reproduce the example in ruby, I get different results for the SHA256 hash.
require 'digest/sha2'
pubkey = "0450863A....582BA6"
step_2 = (Digest::SHA2.new << pubKey).to_s
That results in "32511e82d56...." in stead of "600FFE422..." as it says on the wiki. Both are 64 characters.
What am I doing wrong?
By the way, I tried searching the forum for similar questions, but search is blocked for several minutes after one or two attempts...