<p>An address is just a standardized way to encode an output script. They aren't part of the Bitcoin protocol, and only a few standard scripts have an associated address. As of now these are P2PKH, P2SH, P2WPKH, P2WSH and P2TR.</p>
<p>I won't go into exact details on how to convert each of these scripts into an address, but I'll give helpful links.</p>
<h2>P2PKH (Pay to Public Key Hash)</h2>
<p>A P2PKH output scripts looks like this:</p>
<pre><code>OP_DUP OP_HASH160 OP_PUSHBYTES_20 &lt;key_hash&gt; OP_EQUALVERIFY OP_CHECKSIG
</code></pre>
<p>The <code>&lt;key_hash&gt;</code> part is a 160-bit hash (SHA-256 then RIPEMD-160) of a public key. The address always starts with a <code>1</code>. More details <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses" rel="nofollow noreferrer">here</a>.</p>
<h2>P2SH (Pay to Script Hash)</h2>
<p>A P2SH output script looks like this:</p>
<pre><code>OP_HASH160 OP_PUSHBYTES_20 &lt;script_hash&gt; OP_EQUAL
</code></pre>
<p>The <code>&lt;script_hash&gt;</code> part is a 160-bit hash of a redeem script. The address always starts with a <code>3</code> and is constructed similarly to P2PKH. More details in <a href="https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki" rel="nofollow noreferrer">BIP13</a>.</p>
<h2>P2WPKH (Pay to Witness Public Key Hash)</h2>
<p>A P2WPKH output script looks like this:</p>
<pre><code>OP_0 OP_PUSHBYTES_20 &lt;key_hash&gt;
</code></pre>
<p>As in P2PKH, <code>&lt;key_hash&gt;</code> is a 160-bit hash of a public key. The address always starts with <code>bc1q</code>. More details in <a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki" rel="nofollow noreferrer">BIP173</a>.</p>
<h2>P2WSH (Pay to Witness Script Hash)</h2>
<p>A P2WSH output script looks like this:</p>
<pre><code>OP_0 OP_PUSHBYTES_32 &lt;script_hash&gt;
</code></pre>
<p>The <code>&lt;script_hash&gt;</code> part is a 256-bit hash (double SHA-256) of a redeem script. As with P2WPKH the address always starts with <code>bc1q</code> and is also specified in <a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki" rel="nofollow noreferrer">BIP173</a>.</p>
<h2>P2TR (Pay to Taproot)</h2>
<p>A P2TR output script looks like this:</p>
<pre><code>OP_1 OP_PUSHBYTES_32 &lt;key&gt;
</code></pre>
<p>The <code>&lt;key&gt;</code> part is a <a href="https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki" rel="nofollow noreferrer">BIP340</a> public key. The address always starts with <code>bc1p</code>. More details in <a href="https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki" rel="nofollow noreferrer">BIP350</a>.</p>

CC BY-SA 4.0
2022-07-05T13:33:27.163Z
2022-09-25T13:00:07.720Z
2022-09-25T13:00:07.720Z
Vojtěch Strnad
<p>Is there a way for me to translate the input and output scripts in a bitcoin transaction?</p>
<p>I am building an application that lets you query different stats from the Bitcoin-network, down the line I want it to be able to &quot;trace addresses&quot; as well.</p>
<p>I have tried to read up on Script and how a transaction works but I am a bit confused.</p>
<p>Since the built-in functions in bitcoin-cli (and most all block explorers) are able to find the addresses, I'm sure I should be able to parse and translate the scripts… or?</p>

CC BY-SA 4.0
2022-07-05T10:12:05.790Z
Welcome to Bitcoin.stackexchange.com. Do you simply want to know how, in principle, to convert any Bitcoin script (in Hex say) to a standard address for each of the transaction output types for which an address is well-defined?  (If so, all the discussion of BigQuery may be a hindrance to getting an answer)
RedGrittyBrick
CC BY-SA 4.0
2022-07-05T11:18:02.470Z
Ah thanks for the input, I agree. Just edited
JVH
<transactions><address><script>
Translating inputs (and outputs) to find addresses involved in transaction?
JVH
# Translating inputs (and outputs) to find addresses involved in transaction?

(Asked by: username_0 on 2022-07-05)

Is there a way for me to translate the input and output scripts in a bitcoin transaction?

I am building an application that lets you query different stats from the Bitcoin-network, down the line I want it to be able to "trace addresses" as well.

I have tried to read up on Script and how a transaction works but I am a bit confused.

Since the built-in functions in bitcoin-cli (and most all block explorers) are able to find the addresses, I'm sure I should be able to parse and translate the scripts… or?

**Tags:** transactions, address, script

> **Comments:**
> **username_1**: Welcome to Bitcoin.stackexchange.com. Do you simply want to know how, in principle, to convert any Bitcoin script (in Hex say) to a standard address for each of the transaction output types for which an address is well-defined?  (If so, all the discussion of BigQuery may be a hindrance to getting an answer)  
> **username_0**: Ah thanks for the input, I agree. Just edited  

---

## 1 answers

### Answer by username_2 on 2022-07-05. Score: 2

An address is just a standardized way to encode an output script. They aren't part of the Bitcoin protocol, and only a few standard scripts have an associated address. As of now these are P2PKH, P2SH, P2WPKH, P2WSH and P2TR.

I won't go into exact details on how to convert each of these scripts into an address, but I'll give helpful links.

## P2PKH (Pay to Public Key Hash)

A P2PKH output scripts looks like this:

```
OP_DUP OP_HASH160 OP_PUSHBYTES_20 <key_hash> OP_EQUALVERIFY OP_CHECKSIG

```

The `<key_hash>` part is a 160-bit hash (SHA-256 then RIPEMD-160) of a public key. The address always starts with a `1`. More details [here](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses).

## P2SH (Pay to Script Hash)

A P2SH output script looks like this:

```
OP_HASH160 OP_PUSHBYTES_20 <script_hash> OP_EQUAL

```

The `<script_hash>` part is a 160-bit hash of a redeem script. The address always starts with a `3` and is constructed similarly to P2PKH. More details in [BIP13](https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki).

## P2WPKH (Pay to Witness Public Key Hash)

A P2WPKH output script looks like this:

```
OP_0 OP_PUSHBYTES_20 <key_hash>

```

As in P2PKH, `<key_hash>` is a 160-bit hash of a public key. The address always starts with `bc1q`. More details in [BIP173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki).

## P2WSH (Pay to Witness Script Hash)

A P2WSH output script looks like this:

```
OP_0 OP_PUSHBYTES_32 <script_hash>

```

The `<script_hash>` part is a 256-bit hash (double SHA-256) of a redeem script. As with P2WPKH the address always starts with `bc1q` and is also specified in [BIP173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki).

## P2TR (Pay to Taproot)

A P2TR output script looks like this:

```
OP_1 OP_PUSHBYTES_32 <key>

```

The `<key>` part is a [BIP340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki) public key. The address always starts with `bc1p`. More details in [BIP350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki).


---

