<p>You can use <a href="https://github.com/petertodd/python-bitcoinlib" rel="nofollow noreferrer">Peter Todd's python-bitcoinlib</a>, which contains builders for all the network messages.</p>

<p>Simple example, given a list of hashes:</p>

<pre><code>from bitcoin.net import *
from bitcoin.messages import *

m = msg_inv()
for h in hashes:
    assert len(h) == 32
    inv = CInv()
    inv.type = 1  # TX
    inv.hash = h
    m.inv.append(inv)

inv_msg = m.serialize()
</code></pre>

<p>This assumes that the list of hashes you have are little endian unhexlified strings.</p>

CC BY-SA 3.0
2017-10-21T23:14:00.247Z
Thank you for this, its a good starting point for what I aim to do. Do you have any suggestions for how to do this on a source code level in Bitcoin core?
Nabeel
CC BY-SA 3.0
2017-10-21T23:18:45.923Z
Not really, sorry. There's a few Core devs on this SE, they may be able to help you though.
sr_gi
CC BY-SA 3.0
2017-10-20T21:03:30.053Z
2017-10-20T21:03:30.053Z
sr_gi
<p>I am trying to create an inventory message of specific hashes that I select. Assuming I have all the hashes stored in a vector, how do I create the message 'manually'? (i.e stuff like getting total length, generating checksum and adding in all the inventory vector elements to the message and then sending it to all my peers.) </p>

<p>Any help is appreciated, if you can point me at any Bitcoin core (v0.14 or v0.15) source code that handles this kind of stuff that is also welcome. </p>

<bitcoin-core><network><mempool><inventory>
How to create an inventory message and send it?
Nabeel
# How to create an inventory message and send it?

(Asked by: username_0 on 2017-10-20)

I am trying to create an inventory message of specific hashes that I select. Assuming I have all the hashes stored in a vector, how do I create the message 'manually'? (i.e stuff like getting total length, generating checksum and adding in all the inventory vector elements to the message and then sending it to all my peers.)

Any help is appreciated, if you can point me at any Bitcoin core (v0.14 or v0.15) source code that handles this kind of stuff that is also welcome.

**Tags:** bitcoin-core, network, mempool, inventory


---

## 1 answers

### Answer by username_1 on 2017-10-20. Score: 1 | Accepted answer

You can use [Peter Todd's python-bitcoinlib](https://github.com/petertodd/python-bitcoinlib), which contains builders for all the network messages.

Simple example, given a list of hashes:

```
from bitcoin.net import *
from bitcoin.messages import *

m = msg_inv()
for h in hashes:
    assert len(h) == 32
    inv = CInv()
    inv.type = 1  # TX
    inv.hash = h
    m.inv.append(inv)

inv_msg = m.serialize()

```

This assumes that the list of hashes you have are little endian unhexlified strings.

> **Comments:**
> **username_0**: Thank you for this, its a good starting point for what I aim to do. Do you have any suggestions for how to do this on a source code level in Bitcoin core?  
> **username_1**: Not really, sorry. There's a few Core devs on this SE, they may be able to help you though.  

---

