<p>I’m assuming that you are actually just interested in getting information on every transaction your node sees, rather than the actual p2p traffic.</p>
<p>If so, here is how I would approach your problem:</p>
<ol>
<li>Run a Bitcoin Core full node. If you only care about unconfirmed transactions, consider making it a pruned node. If you want to collect data on all transactions, make it a non-pruning node and enable the transaction index with <code>txindex=1</code>.</li>
<li>Install <code>PyZMQ</code></li>
<li>Enable <a href="https://github.com/bitcoin/bitcoin/blob/master/doc/zmq.md" rel="nofollow noreferrer">ZeroMQ</a>.</li>
<li>Listen to the <code>hashtx</code> ZMQ notifications<br />
<code>hashtx</code> notifies about all transactions, either when they are added to mempool or when a new block arrives, so you may get multiple notifications for the same txid.</li>
<li>For each <code>hashtx</code> notification you receive, check if the transaction is seen for the first time, then call <a href="https://bitcoincore.org/en/doc/24.0.0/rpc/rawtransactions/getrawtransaction/" rel="nofollow noreferrer"><code>bitcoin-cli getrawtransaction &lt;txid&gt;</code></a>. In the response, read the <code>value</code> and <code>address</code> fields for each element of the <code>vout</code> vector. Optionally read <code>n</code> to get the output’s index. Note that some outputs don’t have addresses, e.g. P2PK and nulldata (OP_RETURN) outputs.</li>
<li>Optionally: Store the resulting outputs in a database using their &quot;outpoint&quot; (txid:n) as the key.</li>
</ol>
<p>Note that some txs you see in your mempool may get replaced by a later transaction. If you want to make sure you only keep transactions that end up being part of the best chain, you may want to track transactions in blocks rather than transactions in the mempool, and be sure to handle blockchain reorganizations.</p>
<p>If that’s too slow for your purposes, you can get a whole block explorer of your own by running an instance of mempool.space or Esplora. Alternatively, you can probably pay for an API key to get this (public!) data from a number of different block explorers.</p>
<p><em>Note that you might need to smooth out some corners on this approach if you want use it, I have not tested this. There might also be easier more efficient ways, I never tried to solve this problem myself.</em></p>

CC BY-SA 4.0
2023-03-08T17:24:23.027Z
2023-03-08T18:08:31.840Z
2023-03-08T18:08:31.840Z
Murch
<p>I'm trying to extract public keys and transaction amounts from tx commands using python. Everyone on this stack exchange has been extremely helpful, but I think it's time to try and leverage a library to do this heavy lifting for me. Does anyone know any libraries where I can simply pass in the tx data without any parsing, so including info like magic number, and output details of the transaction?</p>

CC BY-SA 4.0
2023-03-08T17:04:11.887Z
software recommendations are off-topic but search engines do a good job of finding candidates : https://www.google.com/search?q=python+bitcoin+parsing+library or search here https://bitcoin.stackexchange.com/search?q=python+parse
RedGrittyBrick
CC BY-SA 4.0
2023-03-08T17:08:37.947Z
I don’t think this is a software recommendation rather than a question on how to extract all outputs’ addresses and amounts on new transactions seen on the network.
Murch
CC BY-SA 4.0
2023-03-08T18:10:30.900Z
Is it actually a requirement to start with the network messages or do you just want to learn about each transaction?
Murch
<transactions><raw-transaction><output>
parse bitcoin addresses from transactions received through the P2P protocol using python
bitcoinluvr6969
# parse bitcoin addresses from transactions received through the P2P protocol using python

(Asked by: username_0 on 2023-03-08)

I'm trying to extract public keys and transaction amounts from tx commands using python. Everyone on this stack exchange has been extremely helpful, but I think it's time to try and leverage a library to do this heavy lifting for me. Does anyone know any libraries where I can simply pass in the tx data without any parsing, so including info like magic number, and output details of the transaction?

**Tags:** transactions, raw-transaction, output

> **Comments:**
> **username_1**: software recommendations are off-topic but search engines do a good job of finding candidates : https://www.google.com/search?q=python+bitcoin+parsing+library or search here https://bitcoin.stackexchange.com/search?q=python+parse  
> **username_2**: I don’t think this is a software recommendation rather than a question on how to extract all outputs’ addresses and amounts on new transactions seen on the network.  
> **username_2**: Is it actually a requirement to start with the network messages or do you just want to learn about each transaction?  

---

## 1 answers

### Answer by username_2 on 2023-03-08. Score: 1 | Accepted answer

I’m assuming that you are actually just interested in getting information on every transaction your node sees, rather than the actual p2p traffic.

If so, here is how I would approach your problem:

1. Run a Bitcoin Core full node. If you only care about unconfirmed transactions, consider making it a pruned node. If you want to collect data on all transactions, make it a non-pruning node and enable the transaction index with `txindex=1`.
2. Install `PyZMQ`
3. Enable [ZeroMQ](https://github.com/bitcoin/bitcoin/blob/master/doc/zmq.md).
4. Listen to the `hashtx` ZMQ notifications  
   `hashtx` notifies about all transactions, either when they are added to mempool or when a new block arrives, so you may get multiple notifications for the same txid.
5. For each `hashtx` notification you receive, check if the transaction is seen for the first time, then call [`bitcoin-cli getrawtransaction <txid>`](https://bitcoincore.org/en/doc/24.0.0/rpc/rawtransactions/getrawtransaction/). In the response, read the `value` and `address` fields for each element of the `vout` vector. Optionally read `n` to get the output’s index. Note that some outputs don’t have addresses, e.g. P2PK and nulldata (OP\_RETURN) outputs.
6. Optionally: Store the resulting outputs in a database using their "outpoint" (txid:n) as the key.

Note that some txs you see in your mempool may get replaced by a later transaction. If you want to make sure you only keep transactions that end up being part of the best chain, you may want to track transactions in blocks rather than transactions in the mempool, and be sure to handle blockchain reorganizations.

If that’s too slow for your purposes, you can get a whole block explorer of your own by running an instance of mempool.space or Esplora. Alternatively, you can probably pay for an API key to get this (public!) data from a number of different block explorers.

*Note that you might need to smooth out some corners on this approach if you want use it, I have not tested this. There might also be easier more efficient ways, I never tried to solve this problem myself.*


---

