Looking for a Python solution to connect to the Bitcoin network

8 replies 89 views
chris.altHero Member
Posts: 458 · Reputation: 2287
#1Jul 9, 2019, 07:11 AM
I'm trying to find a Python script or library that can hook up with the P2P Bitcoin network, like how actual Bitcoin nodes do. Anyone got any ideas? I want to directly request blocks from various full nodes. It's kinda like pretending to be a Bitcoin node. It would be great to have a full node implementation in Python, but from what I've seen, the networking part is missing in libraries like python-bitcoinlib and btcpy. What I'm really after is something similar to what Bitcoin Core's "net_processing.cpp" handles. Ken Shirriff has a simple example on his blog, "Bitcoin the Hard Way," but it only deals with one message type (sending a transaction). Edit: Turns out I was mistaken, python-bitcoinlib actually has the feature I needed.
8 Reply Quote Share
coin_sigmaLegendary
Posts: 1275 · Reputation: 5553
#2Jul 9, 2019, 08:07 AM
I don't know if this would help to what you looking for but this is related to full node Bitcoind in Python that includes all commands and guide. Here's the open-source link - https://github.com/BlockchainCommons/Learning-Bitcoin-from-the-Command-Line/blob/master/18_4_Accessing_Bitcoind_with_Python.md - https://github.com/karask/python-bitcoin-utils Remember to check out BlockchainCommons under Learning-Bitcoin-from-the-Command-Line as well. They have a ton of command line guides in Python and CLI.
1 Reply Quote Share
HumbleBullFull Member
Posts: 54 · Reputation: 378
#3Jul 9, 2019, 11:14 AM
When you talk about random full nodes i don't get it clear at all, there are not random nodes open for public, that would be kind of dangerous because people would be able to dump the private keys from those nodes. What you want is a way to connect python to the RPC calls on the Bitcoin node, that way you will be able to call any node command from Python, if you ask to chat GPT how to do that, you will find multiple ways, one of them is python-bitcoinlib, here is a basic example: I hope this information helps you. If I'm not wrong, some tools let you filter the RPC command, that way you can avoid some abuses on your service.
4 Reply Quote Share
chris.altHero Member
Posts: 458 · Reputation: 2287
#4Jul 9, 2019, 08:03 PM
I think you both misunderstood me I didn't mean to connect to a local Bitcoin node via JSON-RPC, I have already a library which is able to do this. What I'm looking for is a library or script connecting to the P2P network. If you run a local Bitcoin node, then it will connect to other nodes via a messaging protocol which is partly described in the link I posted in the OP or also here in the Bitcoin wiki (although I don't know if this info is still up to date). This protocol is very different to JSON-RPC (and @seoincorporation: of course it doesn't allow others to dump your private key ). For some reason it seems that this networking part isn't included in many Python Bitcoin libraries, or in none at all I found until now. Perhaps to discourage scriptkiddies from DDOSing nodes? Thanks. Unfortunately this library also seems not to include what I'm looking for. It has a NodeProxy module but this also only connects to a local (or "open") JSON-RPC interface. I suspect I have to do it "the hard way" and code my own using the two snippets of Shirrif's blog ...
6 Reply Quote Share
hash_bossLegendary
Posts: 1166 · Reputation: 5261
#5Jul 10, 2019, 01:21 AM
If i understand your requirement correctly, i think BitcoinJ[1] meets your requirement since it have SPV functionally. I recall it support BIP 37 (outdated way to get relevant TX on a wallet), but i couldn't information whether it support BIP 157 (which replace BIP 37). It's written in Java, but they claim you can use it from Python using Jython[2]. P.S. I never try using this library. [1] https://bitcoinj.org/ [2] https://bitcoinj.org/using-from-other-languages
4 Reply Quote Share
HumbleBullFull Member
Posts: 54 · Reputation: 378
#6Jul 10, 2019, 06:12 AM
It was a confusion on my side, but now i understand what you are trying to do, you don't want to use Python as a tool to manipulate the bitcoin node, what you want to do is connect directly to the P2P networks, and that's possible with the same tool i recommended before python-bitcoinlib, here you have an example: Another option is to create your own custom implementation like you can see in the next example: And another tool that you should look at is bitcoinnet, in think that tool was made with the P2P connection idea, so, with this information you should be one step closer to the goal.
0 Reply Quote Share
chris.altHero Member
Posts: 458 · Reputation: 2287
#7Jul 10, 2019, 10:54 AM
Thanks @seoincorporation, that's what I looked for I think! I'm surprised, I have an older python-bitcoinlib version installed which didn't provide this functionality, and while I have searched the docs about this library before opening this thread it was also an outdated version. I see the messaging part is currently "in flux" according to the Github site, and it is still largely undocumented (but that's not a problem), so we can perhaps expect lots of improvements in the near future. (The addition seems however not to be that new. Strange ...) @ABCbits: thanks, may also be helpful. I was looking however more for a lib/script I can read myself, and I'm not really a coder but relatively fluent in Python.
3 Reply Quote Share
LuckyCoinLegendary
Posts: 832 · Reputation: 4795
#8Jul 10, 2019, 02:37 PM
Those old python-bitcoin-utils/python-bitcoinlib libraries from long ago are at least partial full node implementations in that they can process some of the P2P messages. But nobody seems to maintain them anymore. Actually, I'm not sure if even BitcoinJ is maintained, even just barely, but given that the Bitcoin network has been largely backward-compatible over the years, most of the commands should still work. Segwit support is the only major thing you're missing here.
0 Reply Quote Share
hash_bossLegendary
Posts: 1166 · Reputation: 5261
#9Jul 12, 2019, 02:58 AM
It's not exactly what OP asked, but i just remembered NBitcoin[1] also offer SPV feature. But it's less popular than BitcoinJ and written in C#. I wouldn't bother mention BitcoinJ if it's not actively maintained[2] or have release in past year[3]. And according to their release note[4], it have some SegWit support. [1] https://github.com/MetacoSA/NBitcoin [2] https://github.com/bitcoinj/bitcoinj/commits/master/ [3] https://github.com/bitcoinj/bitcoinj/releases [4] https://bitcoinj.org/release-notes
1 Reply Quote Share
?Reply
Sign in to reply to this topic

Related topics