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.script { |s| s.recipient('1GzqkR2zNQUzHLpE7PLPjVNJ51FHC3bpDH') }

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

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 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG. So you need to add the missing ops:

  • OP_DUP = 0x76
  • OP_HASH160 = 0xa9
  • OP_CHECKSIG = 0xac

Resulting script: 76a9af7a4a30243f2301e6a14a4979ae099d52f560d488ac

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.