<p>Yes, to get addresses from scriptPubKeys, you will need to do a pattern match. There are only a small number of address types each with their own fixed scriptPubKey pattern. Keep in mind that not all scriptPubKeys map to an address so you will find scriptPubKeys that have no address and you will need to handle those.</p>

<p>Bitcoin Core has a pattern matcher for going from scriptPubKeys to address types in <a href="https://github.com/bitcoin/bitcoin/blob/master/src/script/standard.cpp#L91" rel="nofollow noreferrer">the <code>solver</code> function</a>. The <code>MatchPayToPubkey</code>, <code>MatchPayToPubkeyHash</code>, and <code>MatchMultisig</code> can be found higher up in that file. The <code>IsPayToScriptHash</code> and <code>IsWitnessProgram</code> functions can be found <a href="https://github.com/bitcoin/bitcoin/blob/master/src/script/script.cpp#L196" rel="nofollow noreferrer">here</a>.</p>

<p>One thing to note about the <code>solver</code> is that it does not return just address types. Some of the types it returns do not map to addresses. These are TX_NULLDATA, TX_MULTISIG, TX_PUBKEY, and TX_NONSTANDARD.</p>

<p>It may also be useful for you to see how you get the scriptPubKey from an address. Bitcoin Core does it <a href="https://github.com/bitcoin/bitcoin/blob/master/src/script/standard.cpp#L242" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2019-07-13T05:56:05.133Z
Thanks a lot Andrew. I'm not great at C++ (only been programming for a year) but I think I can use it as a guide. One more question: why would a scriptPubKey not map to an address? For the purposes of my block parser, would I just discard it?
fon
CC BY-SA 4.0
2019-07-13T06:02:14.917Z
A scriptPubKey can be literally any sequence of bytes; it doesn't have to be a valid script, the output will just be unspendable. Those standard scriptPubKeys that don't have addresses occur because there was/is no reason for them to have addresses. A very common one that you will see are Pay to Pubkey outputs. These are in basically all of the early blocks because the original Bitcoin client created coinbase transactions with P2PK outputs. Some block explorers will convert these to addresses, but that is incorrect.
Ava Chow
CC BY-SA 4.0
2019-07-13T06:03:21.163Z
I recommend that you do not discard them because you will be losing information. It is useful to know exactly what the scriptPubKey for an output is. So you should still just store them. Maybe decode them into opcode notation instead of storing the bytes themselves, but you should still keep the scripts around in some form.
Ava Chow
CC BY-SA 4.0
2019-07-15T06:56:54.307Z
Regarding the handling of segwit txns, is my understanding correct?



1) Check if it's non-native or native.


2) If the former, check if it's p2sh-p2wpkh or p2sh-p2wsh and then handle it like any other p2sh.


3) If the latter, determine if it's p2wpkh or p2wsh.


4) After extracting the script hash, perform the appropriate hashing and base58check encoding to extract the addresses?
fon
CC BY-SA 4.0
2019-07-15T13:38:06.137Z
You won't be able to tell if an output is non-native segwit. The scriptPubKey will not contain the segwit script at all, you will just see a p2sh output. So you should treat all p2sh outputs the same, because, as outputs, they are.
Ava Chow
CC BY-SA 4.0
2019-07-13T05:48:39.607Z
2019-07-13T05:48:39.607Z
Ava Chow
<p>With library <a href="https://github.com/petertodd/python-bitcoinlib" rel="nofollow noreferrer">https://github.com/petertodd/python-bitcoinlib</a></p>
<p>a P2WPKH address derivation example:</p>
<pre><code>from bitcoin import SelectParams
from bitcoin.core import x
from bitcoin.core.script import CScript
from bitcoin.wallet import P2WPKHBitcoinAddress

SelectParams('mainnet')
# Create an address from that private key.
scriptPubKey = CScript(x('Paste_scriptPubKey_Here'))
address = P2WPKHBitcoinAddress.from_scriptPubKey(scriptPubKey)
print(address)
</code></pre>

CC BY-SA 4.0
2022-06-10T07:08:08.517Z
2022-06-10T07:08:08.517Z
Ilya Evdokimov
<p>I am creating a block parser and just learned about Scripts in Bitcoin yesterday which is kind of confusing so far. I want to extract the addresses involved in each transaction. Right now, I am able to read the script for each output. To extract the address, am I correct in thinking that my code will need to handle all of the different formats of scriptPubKeys?  Is there any sample code out there to guide me? If I am completely wrong, how should I do it?</p>

<transactions><address><script><input-script><p2pkh>
How do I extract the address from a scriptPubKey?
fon
# How do I extract the address from a scriptPubKey?

(Asked by: username_0 on 2019-07-13)

I am creating a block parser and just learned about Scripts in Bitcoin yesterday which is kind of confusing so far. I want to extract the addresses involved in each transaction. Right now, I am able to read the script for each output. To extract the address, am I correct in thinking that my code will need to handle all of the different formats of scriptPubKeys? Is there any sample code out there to guide me? If I am completely wrong, how should I do it?

**Tags:** transactions, address, script, input-script, p2pkh


---

## 2 answers

### Answer by username_1 on 2019-07-13. Score: 3 | Accepted answer

Yes, to get addresses from scriptPubKeys, you will need to do a pattern match. There are only a small number of address types each with their own fixed scriptPubKey pattern. Keep in mind that not all scriptPubKeys map to an address so you will find scriptPubKeys that have no address and you will need to handle those.

Bitcoin Core has a pattern matcher for going from scriptPubKeys to address types in [the `solver` function](https://github.com/bitcoin/bitcoin/blob/master/src/script/standard.cpp#L91). The `MatchPayToPubkey`, `MatchPayToPubkeyHash`, and `MatchMultisig` can be found higher up in that file. The `IsPayToScriptHash` and `IsWitnessProgram` functions can be found [here](https://github.com/bitcoin/bitcoin/blob/master/src/script/script.cpp#L196).

One thing to note about the `solver` is that it does not return just address types. Some of the types it returns do not map to addresses. These are TX\_NULLDATA, TX\_MULTISIG, TX\_PUBKEY, and TX\_NONSTANDARD.

It may also be useful for you to see how you get the scriptPubKey from an address. Bitcoin Core does it [here](https://github.com/bitcoin/bitcoin/blob/master/src/script/standard.cpp#L242).

> **Comments:**
> **username_0**: Thanks a lot Andrew. I'm not great at C++ (only been programming for a year) but I think I can use it as a guide. One more question: why would a scriptPubKey not map to an address? For the purposes of my block parser, would I just discard it?  
> **username_1**: A scriptPubKey can be literally any sequence of bytes; it doesn't have to be a valid script, the output will just be unspendable. Those standard scriptPubKeys that don't have addresses occur because there was/is no reason for them to have addresses. A very common one that you will see are Pay to Pubkey outputs. These are in basically all of the early blocks because the original Bitcoin client created coinbase transactions with P2PK outputs. Some block explorers will convert these to addresses, but that is incorrect.  
> **username_1**: I recommend that you do not discard them because you will be losing information. It is useful to know exactly what the scriptPubKey for an output is. So you should still just store them. Maybe decode them into opcode notation instead of storing the bytes themselves, but you should still keep the scripts around in some form.  
> **username_0**: Regarding the handling of segwit txns, is my understanding correct?    1) Check if it's non-native or native.   2) If the former, check if it's p2sh-p2wpkh or p2sh-p2wsh and then handle it like any other p2sh.   3) If the latter, determine if it's p2wpkh or p2wsh.   4) After extracting the script hash, perform the appropriate hashing and base58check encoding to extract the addresses?  
> **username_1**: You won't be able to tell if an output is non-native segwit. The scriptPubKey will not contain the segwit script at all, you will just see a p2sh output. So you should treat all p2sh outputs the same, because, as outputs, they are.  

---

### Answer by username_2 on 2022-06-10. Score: 0

With library <https://github.com/petertodd/python-bitcoinlib>

a P2WPKH address derivation example:

```
from bitcoin import SelectParams
from bitcoin.core import x
from bitcoin.core.script import CScript
from bitcoin.wallet import P2WPKHBitcoinAddress

SelectParams('mainnet')
# Create an address from that private key.
scriptPubKey = CScript(x('Paste_scriptPubKey_Here'))
address = P2WPKHBitcoinAddress.from_scriptPubKey(scriptPubKey)
print(address)

```


---

