Skip to content

Instantly share code, notes, and snippets.

@rayspock
Last active December 27, 2023 03:02
Show Gist options
  • Save rayspock/bc03bd20c554714518e22c5ba4aab6d6 to your computer and use it in GitHub Desktop.
Save rayspock/bc03bd20c554714518e22c5ba4aab6d6 to your computer and use it in GitHub Desktop.
ElectrumX with RESTful API

Spark API

(Optional) Setup Node (Using Bitcoin Node 0.16.3 as Example)

server=1
listen=1
daemon=1
txindex=1
rpcuser=<random username>
rpcpassword=<strong password>
  1. Clone Bitcoin Source
git clone https://github.com/bitcoin/bitcoin.git
  1. Prerequisites Libraries
sudo apt-get install build-essential libtool autotools-dev automake pkg-config libssl-dev libevent-dev bsdmainutils python3
sudo apt-get install libboost-all-dev
  1. This will download and verify the Berkeley Database is legitimate
wget http://download.oracle.com/berkeley-db/db-4.8.30.NC.tar.gz
echo '12edc0df75bf9abd7f82f821795bcee50f42cb2e5f76a6a281b85732798364ef  db-4.8.30.NC.tar.gz' | sha256sum -c
  1. Install Berkeley Database
tar -xvf db-4.8.30.NC.tar.gz
cd db-4.8.30.NC/build_unix
mkdir -p build
BDB_PREFIX=$(pwd)/build
../dist/configure --disable-shared --enable-cxx --with-pic --prefix=$BDB_PREFIX
sudo make install
cd ../..
  1. Compiling Bitcoin Core
cd bitcoin
git checkout tags/v0.16.3
./autogen.sh
./configure CPPFLAGS="-I${BDB_PREFIX}/include/ -O2" LDFLAGS="-L${BDB_PREFIX}/lib/"
make
sudo make install
  • (Optional) If you already have bitcoin node installed, you need to reindex the blockchain:
bitcoin-cli stop
bitcoind -reindex

Setup ElectrumX

  1. Install LevelDB
sudo apt-get install python3-leveldb libleveldb-dev
  1. Install Python 3.7
sudo apt-get update && sudo apt-get install python3.7 python3.7-dev python3-pip
  • Install required Python packages
python3.7 -m pip install --upgrade pip setuptools wheel
python3.7 -m pip install --upgrade aiohttp pylru leveldb plyvel aiorpcx ecdsa
  1. Install and set up ElectrumX
  • Clone the ElectrumX code from a GitHub repository
git clone https://github.com/rayspock/electrumx.git -b spark-1.1.7
cd electrumx
  • Run the installation script
python3 setup.py install
  • Create a data folder where the blockchain data will be stored
mkdir ~/db
  • This repository contains a sample systemd unit file that you can use to setup ElectrumX with systemd. Simply copy it to /etc/systemd/system
cp contrib/systemd/electrumx.service /etc/systemd/system/
# default /etc/electrumx.conf for systemd

# REQUIRED
COIN=BitcoinSegwit
NET=mainnet
DB_DIRECTORY=/home/ubuntu/data/db
# Bitcoin Node RPC Credentials
DAEMON_URL=http://rpcuser:rpcpassword@hostname:port/
DB_ENGINE=leveldb
CACHE_MB=1800
SERVICES=http://:3001
# Set to an empty string to listen on all available interfaces (likely both IPv4 and IPv6).
PEER_DISCOVERY=off
PEER_ANNOUNCE=
  • You need to edit User variables in electrumx.service
vi /etc/systemd/system/electrumx.service
  • Now you can start ElectrumX using systemctl:
systemctl start electrumx
  • You can use journalctl to check the log output:
journalctl -u electrumx -f
  • Once configured you may want to start ElectrumX at boot:
systemctl enable electrumx
  • If you have updated repository want to deploy again
python3 setup.py install --force
systemctl restart electrumx

HTTP REST API

Unspent

request method: GET
request url: /addrs/[:addrs]/utxo

response header: 200 OK
response body:
[
    {
        "address": "3BMEXqoawayL82Jng7TzcdviFajqxTrZBv",
        "txid": "2e6e4d8b3011a95394a85a37d0a9686cb8f24091f4c0ada31d0d804f78d0534d",
        "vout": 1,
        "scriptPubKey": "a91469f376b6cde2f10855c82cab72369d5272b66aaa87",
        "amount": 0.0743694,
        "satoshis": 7436940,
        "height": 558085,
        "confirmations": 2795
    },
    {
        "address": "3BMEXqoawayL82Jng7TzcdviFajqxTrZBv",
        "txid": "57f29894c2bd6271260f68afc9815ddf2cc2a5312d8ad381f97a3d3ace94413a",
        "vout": 0,
        "scriptPubKey": "a91469f376b6cde2f10855c82cab72369d5272b66aaa87",
        "amount": 0.05,
        "satoshis": 5000000,
        "height": 560877,
        "confirmations": 3
    }
]
/addrs/3BMEXqoawayL82Jng7TzcdviFajqxTrZBv,31w3iWUN5EMJMW2YRCc5m4RFqm3zN61xK2/utxo

Transaction Fee

request method: GET
request url: /utils/estimatefee[?nbBlocks=2]

response header: 200 OK
response body:
{
    "fee": "0.00012245"
}

AddressTransaction

request method: GET
request url: /addrs/[:addrs]/txs[?from=&to=]
from (optional): 0
to (optional): 50(Default)
(maximum transactions query is 50 per request)

response header: 200 OK
response body:
{
    "totalItems": 2,
    "from":0,
    "to":50,
    "items": [
        {
            "txid": "c6ddd00f94e323f7fd547f86aa77b95bd7685220bdc92a807a8cdf300e667419",
            "blockheight": 551075,
            "vin": [
                {
                    "txid": "cb82c31f14d3546ad2f49ad41b33abf3ecd4a44af2f0b8f98a0be57833ab0916",
                    "addr": "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej",
                    "valueSat": 5950508,
                    "value": 0.05950508
                }
            ],
            "vout": [
                {
                    "value": 0.04,
                    "n": 0,
                    "scriptPubKey": {
                        "asm": "OP_HASH160 69f376b6cde2f10855c82cab72369d5272b66aaa OP_EQUAL",
                        "hex": "a91469f376b6cde2f10855c82cab72369d5272b66aaa87",
                        "reqSigs": 1,
                        "type": "scripthash",
                        "addresses": [
                            "3BMEXqoawayL82Jng7TzcdviFajqxTrZBv"
                        ]
                    }
                },
                {
                    "value": 0.01860508,
                    "n": 1,
                    "scriptPubKey": {
                        "asm": "0 701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d",
                        "hex": "0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d",
                        "reqSigs": 1,
                        "type": "witness_v0_scripthash",
                        "addresses": [
                            "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"
                        ]
                    }
                }
            ],
            "valueOut": 0.058605080000000004,
            "valueIn": 0.05950508,
            "fees": 0.000899999999999996,
            "confirmations": 9806,
            "time": 1542891782
        },
        {
            "txid": "b23b4f304e9eb7e796c950913cd5c241d73044bf7e24017f8246e06b5077bc0f",
            "blockheight": 556707,
            "vin": [
                {
                    "txid": "f4a7d67bc8ab924856716aefb1784ca12dc95ee67500bef31af57657979f044a",
                    "addr": "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej",
                    "valueSat": 25720000,
                    "value": 0.2572
                }
            ],
            "vout": [
                {
                    "value": 0.038,
                    "n": 0,
                    "scriptPubKey": {
                        "asm": "OP_HASH160 69f376b6cde2f10855c82cab72369d5272b66aaa OP_EQUAL",
                        "hex": "a91469f376b6cde2f10855c82cab72369d5272b66aaa87",
                        "reqSigs": 1,
                        "type": "scripthash",
                        "addresses": [
                            "3BMEXqoawayL82Jng7TzcdviFajqxTrZBv"
                        ]
                    }
                },
                {
                    "value": 0.2183,
                    "n": 1,
                    "scriptPubKey": {
                        "asm": "0 701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d",
                        "hex": "0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d",
                        "reqSigs": 1,
                        "type": "witness_v0_scripthash",
                        "addresses": [
                            "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"
                        ]
                    }
                }
            ],
            "valueOut": 0.2563,
            "valueIn": 0.2572,
            "fees": 0.0009,
            "confirmations": 4174,
            "time": 1546446084
        }
    ]
}
/addrs/3BMEXqoawayL82Jng7TzcdviFajqxTrZBv,31w3iWUN5EMJMW2YRCc5m4RFqm3zN61xK2/txs

AddressesBalance

request method: GET
request url: /addr/[:addr]

response header: 200 OK
response body:
{
    "addrStr": "3BMEXqoawayL82Jng7TzcdviFajqxTrZBv",
    "balance": 5709.93351287,
    "balanceSat": 570993351287,
    "unconfirmedBalance": 0,
    "unconfirmedBalanceSat": 0
}
/addr/3BMEXqoawayL82Jng7TzcdviFajqxTrZBv

SendTransaction

request method: POST
request url: /tx/send
request body:
{
    rawtx: "signed transaction as hex string"
}

response header: 200 OK
response body:
{
    "txid": "transaction id"
}
rawtx: 01000000017b1eabe0209b1fe794124575ef807057c77ada2138ae4fa8d6c4de0398a14f3f00000000494830450221008949f0cb400094ad2b5eb399d59d01c14d73d8fe6e96df1a7150deb388ab8935022079656090d7f6bac4c9a94e0aad311a4268e082a725f8aeae0573fb12ff866a5f01ffffffff01f0ca052a010000001976a914cbc20a7664f2f69e5355aa427045bc15e7c6c77288ac00000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment