<p>To convert a P2PKH address to a script, you first have to decode it using <a href="https://en.bitcoin.it/wiki/Base58Check_encoding" rel="nofollow noreferrer">base58check</a>.</p>

<p>Here is an example:</p>

<pre><code>$ bs58 -dc 1GzqkR2zNQUzHLpE7PLPjVNJ51FHC3bpDH
00af7a4a30243f2301e6a14a4979ae099d52f560d4
</code></pre>

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

<ul>
<li><code>OP_DUP = 0x76</code></li>
<li><code>OP_HASH160 = 0xa9</code></li>
<li><code>OP_EQUALVERIFY = 0x88</code></li>
<li><code>OP_CHECKSIG = 0xac</code></li>
</ul>

<p>Resulting script: <code>76a9af7a4a30243f2301e6a14a4979ae099d52f560d488ac</code></p>

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

<h3>Links</h3>

<ul>
<li><a href="https://en.bitcoin.it/wiki/Script" rel="nofollow noreferrer">Bitcoin Wiki | Script</a></li>
<li><a href="https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash" rel="nofollow noreferrer">Bitcoin Wiki | Transaction (P2PKH)</a></li>
<li>Python Tools (bs58):
<a href="https://github.com/jbaczuk/blockchain-dev-tools/" rel="nofollow noreferrer">https://github.com/jbaczuk/blockchain-dev-tools/</a></li>
</ul>

CC BY-SA 4.0
2019-04-09T13:10:20.997Z
2019-04-09T13:10:20.997Z
JBaczuk
<p>How to convert a bitcoin address (starts with <code>1</code>) to the right format acceptable by <code>script</code> (I'm using <a href="https://github.com/lian/bitcoin-ruby" rel="nofollow noreferrer">this</a> Ruby library)? I'm doing this:</p>

<pre><code>tx.output do |o|
  o.value(50000)
  o.script { |s| s.recipient('1GzqkR2zNQUzHLpE7PLPjVNJ51FHC3bpDH') }
end
</code></pre>

<p>I'm getting:</p>

<pre><code>RuntimeError: Script type must be hash160, pubkey, p2wpkh or multisig
</code></pre>

<p>I suspect there is something wrong with the format of the destination address. I <a href="https://github.com/lian/bitcoin-ruby/issues/287" rel="nofollow noreferrer">cross-posted</a> to GitHub too.</p>

<script><ruby>
How to convert P2PKH to Hash160 (or something else acceptable by Script)?
yegor256
# How to convert P2PKH to Hash160 (or something else acceptable by Script)?

(Asked by: username_0 on 2019-04-09)

How to convert a bitcoin address (starts with `1`) to the right format acceptable by `script` (I'm using [this](https://github.com/lian/bitcoin-ruby) 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](https://github.com/lian/bitcoin-ruby/issues/287) to GitHub too.

**Tags:** script, ruby


---

## 1 answers

### Answer by username_1 on 2019-04-09. Score: 1 | Accepted answer

To convert a P2PKH address to a script, you first have to decode it using [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding).

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 <pubKeyHash> 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](https://en.bitcoin.it/wiki/Script)
* [Bitcoin Wiki | Transaction (P2PKH)](https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash)
* Python Tools (bs58):
  <https://github.com/username_1/blockchain-dev-tools/>


---

