A lightweight and easy-to-use C++ client for interacting with a Bitcoin node via JSON-RPC. This library provides a comprehensive interface to communicate with a Bitcoin node, enabling developers to perform various blockchain operations, manage wallets, and interact with the Bitcoin network.
- Full JSON-RPC Support: Implements all Bitcoin Core RPC methods.
- Easy Integration: Simple and intuitive API for interacting with a Bitcoin node.
- Cross-Platform: Works on Linux, macOS, and Windows.
- Modular Design: Extensible and customizable for advanced use cases.
- Comprehensive Documentation: Well-documented code with examples.
- Bitcoin Core: Ensure you have a running Bitcoin node with JSON-RPC enabled.
- C++ Compiler: Supports C++23 or later (e.g., GCC, Clang, or MSVC).
- Dependencies:
-
Clone the repository:
git clone https://github.com/genyleap/bitcoin-rpc.git cd bitcoin-rpc
-
Build the project:
mkdir build cd build cmake .. make
-
Install the library (optional):
sudo make install
Here’s how to use the BitcoinClient
to interact with a Bitcoin node:
#include "bitcoinclient.hpp"
#include <iostream>
int main() {
// Initialize the Bitcoin client
BitcoinClient client("rpcuser", "rpcpassword", "http://127.0.0.1:8332/");
// Get the best block hash
Json::Value bestBlockHash = client.getBestBlockHash();
std::cout << "Best Block Hash: " << bestBlockHash.asString() << std::endl;
// Get blockchain info
Json::Value blockchainInfo = client.getBlockchainInfo();
std::cout << "Blockchain Height: " << blockchainInfo["blocks"].asInt() << std::endl;
return 0;
}
The BitcoinClient
class supports all Bitcoin Core RPC methods, including:
- Blockchain RPCs:
getBestBlockHash
,getBlock
,getBlockchainInfo
, etc. - Wallet RPCs:
getBalance
,sendToAddress
,createWallet
, etc. - Network RPCs:
getNetworkInfo
,addNode
,ping
, etc. - Mining RPCs:
getMiningInfo
,getBlockTemplate
,submitBlock
, etc. - Raw Transactions RPCs:
createRawTransaction
,signRawTransaction
,sendRawTransaction
, etc.
For a full list of methods, refer to the documentation.
To connect to your Bitcoin node, update the following parameters in your code:
- RPC Username: Set in
bitcoin.conf
asrpcuser
. - RPC Password: Set in
bitcoin.conf
asrpcpassword
. - RPC URL: Default is
http://127.0.0.1:8332/
.
Example bitcoin.conf
:
rpcuser=yourusername
rpcpassword=yourpassword
rpcallowip=127.0.0.1
server=1
For detailed documentation, refer to the header file. Each method is documented with Doxygen-style comments, including descriptions, parameters, return values, and examples.
To generate the documentation locally:
doxygen Doxyfile
Contributions are welcome! If you’d like to contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Commit your changes and push to your fork.
- Submit a pull request with a detailed description of your changes.
This project is licensed under the MIT License. See the LICENSE file for details.
If you find this project useful, please consider giving it a ⭐️ on GitHub. For questions or issues, open an issue.
- Bitcoin Core for the JSON-RPC API.
- JSONCPP for JSON parsing.
- cURL for HTTP requests.