Join 100,766 users already on

Electrum RPC with Python — Trust a stranger (or SPV verify)

0 310
Avatar for cashdev
Written by   40
2 years ago

Running your own full node is not always the best option, so a full nodes RPC interface is not exactly readily available.

You can still interact with the Bitcoin Cash blockchain using public Electrum servers.

Here is a code snippet using the bitcoincash Python library that fetches the block header of block 613880.

$ cat
from bitcoincash.electrum import Electrum
from bitcoincash.core import CBlockHeader, x
import asyncio

async def electrum_stuff():
    cli = Electrum()
    await cli.connect()

    header_hex = await cli.RPC('blockchain.block.header', 613880)

    header = CBlockHeader.deserialize(x(header_hex))
    print(f"Received header: {header}")

    await cli.close()

loop = asyncio.get_event_loop()

And when ran:

$ python3 
Received header: CBlockHeader(545259520, 
    1576668009, 0x18030793, 0x34e3adb0)

Full list of Electrum RPC methods are documented by ElectrumX.

In this code snippet, we're trusting the server to provide us with correct the information. That may be fine for your use case. Or perhaps you or a friend are running your own electrum server, which you trust.

Electrum servers still provide enough data to give you SPV security, so you don't have to trust the public server. You can verify. Should I write articles about SPV verifying blockchain data? Comment below.

Sponsors of cashdev

$ 6.06
$ 5.00 from @Read.Cash
$ 1.00 from @btcfork
$ 0.05 from @SatoshisToothbrush
+ 1
Avatar for cashdev
Written by   40
2 years ago
Enjoyed this article?  Earn Bitcoin Cash by sharing it! Explain
...and you will also help the author collect more tips.