How to convert P2PKH to Hash160 (or something else acceptable by Script)? How to convert a bitcoin address (starts with 1) to the right format acceptable by script (I'm using this Ruby library)? I'm doing this:

tx.output do |o|
  o.value(50000)
  o.script { |s| s.recipient('1GzqkR2zNQUzHLpE7PLPjVNJ51FHC3bpDH') }
end


I'm getting:

RuntimeError: Script type must be hash160, pubkey, p2wpkh or multisig


I suspect there is something wrong with the format of the destination address. I cross-posted to GitHub too.
To convert a P2PKH address to a script, you first have to decode it using base58check.

Here is an example:

$ bs58 -dc 1GzqkR2zNQUzHLpE7PLPjVNJ51FHC3bpDH
00af7a4a30243f2301e6a14a4979ae099d52f560d4


Remove the prefix 00, see https://en.bitcoin.it/wiki/List_of_address_prefixes. You are then left with the hash160 of the pubkey, but this is not a full script. The P2PKH script looks like: OP_DUP OP_HASH160 &lt;pubKeyHash&gt; OP_EQUALVERIFY OP_CHECKSIG. So you need to add the missing ops:


OP_DUP = 0x76
OP_HASH160 = 0xa9
OP_EQUALVERIFY = 0x88
OP_CHECKSIG = 0xac


Resulting script: 76a9af7a4a30243f2301e6a14a4979ae099d52f560d488ac

Note wallet software does all this for you when creating transactions based on an address.

Links


Bitcoin Wiki | Script
Bitcoin Wiki | Transaction (P2PKH)
Python Tools (bs58):
https://github.com/jbaczuk/blockchain-dev-tools/