進階
RPC 接口指南
Bitcoin Core RPC 接口入門,包括配置、認證和基本使用方法。
15 分鐘
什麼是 RPC?
RPC(Remote Procedure Call)是 Bitcoin Core 提供的程式化接口, 允許外部程式查詢節點狀態、發送交易和管理錢包。 它使用 JSON-RPC 2.0 協議,支持 HTTP 和命令行兩種訪問方式。
bitcoin-cli
命令行工具,最簡單的 RPC 調用方式。適合手動操作和腳本。
HTTP JSON-RPC
標準 HTTP 接口,可用任何程式語言調用。適合應用程式整合。
配置 RPC
在 bitcoin.conf 中配置 RPC 訪問:
# bitcoin.conf 位置 # Linux: ~/.bitcoin/bitcoin.conf # macOS: ~/Library/Application Support/Bitcoin/bitcoin.conf # Windows: %APPDATA%\Bitcoin\bitcoin.conf # 基本配置 server=1 # 啟用 RPC 服務 rpcuser=myuser # RPC 用戶名 rpcpassword=mypassword # RPC 密碼 # 網路配置 rpcbind=127.0.0.1 # 監聽地址(預設只本地) rpcport=8332 # RPC 端口(mainnet 預設) rpcallowip=127.0.0.1 # 允許連接的 IP
安全警告: 永遠不要在公開網路上暴露 RPC 接口。如果必須遠程訪問,請使用 SSH 隧道或 VPN。
Cookie 認證(推薦)
Bitcoin Core 支持更安全的 Cookie 認證,無需在配置文件中存儲密碼:
# Cookie 文件位置 ~/.bitcoin/.cookie # Cookie 格式 __cookie__:隨機生成的密碼 # bitcoin-cli 自動使用 Cookie 認證 # 無需額外配置
使用 bitcoin-cli
# 基本語法 bitcoin-cli [選項] <命令> [參數...] # 常用選項 -rpcuser=<user> # 指定用戶名 -rpcpassword=<pass> # 指定密碼 -rpcconnect=<ip> # 指定節點 IP -rpcport=<port> # 指定端口 -testnet / -signet # 連接測試網路 # 範例 bitcoin-cli getblockchaininfo bitcoin-cli -testnet getblockcount bitcoin-cli -rpcconnect=192.168.1.100 getnetworkinfo
使用 HTTP 調用
cURL 範例
# JSON-RPC 請求格式
curl --user myuser:mypassword \
--data-binary '{"jsonrpc":"1.0","id":"test","method":"getblockchaininfo","params":[]}' \
-H 'content-type: text/plain;' \
http://127.0.0.1:8332/
# 帶參數的請求
curl --user myuser:mypassword \
--data-binary '{"jsonrpc":"1.0","id":"test","method":"getblock","params":["000000..."]}' \
-H 'content-type: text/plain;' \
http://127.0.0.1:8332/ Python 範例
import requests
import json
def call_rpc(method, params=[]):
url = "http://127.0.0.1:8332/"
headers = {"content-type": "text/plain;"}
payload = json.dumps({
"jsonrpc": "1.0",
"id": "python",
"method": method,
"params": params
})
response = requests.post(
url,
headers=headers,
data=payload,
auth=("myuser", "mypassword")
)
return response.json()["result"]
# 使用範例
info = call_rpc("getblockchaininfo")
print(f"區塊高度: {info['blocks']}") JavaScript 範例
async function callRPC(method, params = []) {
const response = await fetch('http://127.0.0.1:8332/', {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Authorization': 'Basic ' + btoa('myuser:mypassword')
},
body: JSON.stringify({
jsonrpc: '1.0',
id: 'js',
method: method,
params: params
})
});
const data = await response.json();
return data.result;
}
// 使用範例
const info = await callRPC('getblockchaininfo');
console.log(`區塊高度: ${info.blocks}`); RPC 命令分類
| 分類 | 常用命令 | 說明 |
|---|---|---|
| 區塊鏈 | getblock, getblockchaininfo | 查詢區塊和鏈狀態 |
| 錢包 | listunspent, sendtoaddress | 管理錢包和發送交易 |
| 網路 | getnetworkinfo, getpeerinfo | P2P 網路狀態 |
| 交易 | getrawtransaction, sendrawtransaction | 原始交易操作 |
| 挖礦 | getblocktemplate, submitblock | 礦池接口 |
| 控制 | stop, uptime, help | 節點控制 |
獲取幫助
# 列出所有命令 bitcoin-cli help # 查看特定命令的詳細說明 bitcoin-cli help getblock # 查看命令分類 bitcoin-cli help | grep -E "^== "
錯誤處理
RPC 錯誤會返回標準化的錯誤碼:
| 錯誤碼 | 含義 |
|---|---|
| -1 | 一般錯誤 |
| -3 | 無效參數類型 |
| -5 | 無效地址或密鑰 |
| -8 | 無效參數 |
| -28 | 節點正在初始化 |
已複製連結