https://github.com/calcuis/bitcoin-rpc-server
https://github.com/calcuis/bitcoin-rpc-server
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/calcuis/bitcoin-rpc-server
- Owner: calcuis
- Created: 2024-07-20T23:53:52.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-07-23T01:14:32.000Z (11 months ago)
- Last Synced: 2025-01-21T13:11:36.228Z (5 months ago)
- Language: Python
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
To run a Bitcoin RPC (Remote Procedure Call) server, you need to follow these general steps:
### Install Bitcoin Core
First, you need to install `Bitcoin Core`, the official Bitcoin client. You can download it from the [Bitcoin Core website](https://bitcoin.org/en/download).### Initial Setup
Once installed, you'll need to set up the `bitcoin.conf` file with the necessary configurations. This file is typically located in the Bitcoin data directory:- Windows: C:\Users\YourUsername\AppData\Roaming\Bitcoin\bitcoin.conf
- macOS: ~/Library/Application Support/Bitcoin/bitcoin.conf
- Linux: ~/.bitcoin/bitcoin.confIf the file doesn't exist, create it.
### Configure bitcoin.conf
Add the following lines to `bitcoin.conf` to enable the RPC server:
```
server=1
rpcuser=your_rpc_username
rpcpassword=your_rpc_password
rpcallowip=127.0.0.1
rpcport=8332
```
- `server=1`: Enables the server mode.
- `rpcuser` and `rpcpassword`: Set the username and password for RPC authentication.
- `rpcallowip`: Specifies which IP addresses are allowed to connect. Use `127.0.0.1` to allow only local connections. For remote connections, specify the appropriate IP address.
- `rpcport`: The port on which the RPC server will listen. The default port is `8332`.### Start Bitcoin Core
Start Bitcoin Core with the following command:
- Windows: Run `bitcoin-qt.exe` from the installation directory.
- macOS: Run `./Applications/Bitcoin-Qt.app/Contents/MacOS/Bitcoin-Qt` from the terminal.
- Linux: Run `bitcoind` from the terminal.
### Use the RPC Interface
Once the Bitcoin Core is running, you can use the RPC interface. You can use tools like `curl`, `bitcoin-cli`, or write scripts in your preferred programming language to interact with the Bitcoin RPC.
### Using curl
Here is an example of how to use curl to make an RPC call:
```
curl --user your_rpc_username:your_rpc_password --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockchaininfo", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
```
### Using bitcoin-cli
Bitcoin Core includes `bitcoin-cli`, a `command-line tool` to interact with the RPC server:
```
bitcoin-cli -rpcuser=your_rpc_username -rpcpassword=your_rpc_password getblockchaininfo
```
### Using a Programming Language
Here's a Python example (connect.py) using the requests library:
```
import requests
import jsonrpc_user = 'your_rpc_username'
rpc_password = 'your_rpc_password'
url = 'http://127.0.0.1:8332/'headers = {'content-type': 'application/json'}
payload = json.dumps({
"method": "getblockchaininfo",
"params": [],
"jsonrpc": "1.0",
"id": "python"
})response = requests.post(url, headers=headers, data=payload, auth=(rpc_user, rpc_password))
print(response.json())
```
### Tips
- Do not expose your RPC server to the internet without proper security measures like SSL/TLS and firewall rules.
- Ensure that the RPC port is properly firewalled to prevent unauthorized access.
- Refer to the [Bitcoin Core documentation](https://developer.bitcoin.org/reference/rpc/index.html) for a list of available RPC commands and additional configuration options.