|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use anyhow::{anyhow, Context, Result}; |
| 5 | +use corepc_types::v26::{WalletCreateFundedPsbt, WalletProcessPsbt}; |
| 6 | +use payjoin::bitcoin::{Address, Amount, Network, Txid}; |
| 7 | +use reqwest::Client; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | +use serde_json::{json, Value}; |
| 10 | + |
| 11 | +/// Authentication method for Bitcoin Core RPC |
| 12 | +#[derive(Clone, Debug)] |
| 13 | +pub enum Auth { |
| 14 | + UserPass(String, String), |
| 15 | + CookieFile(PathBuf), |
| 16 | +} |
| 17 | + |
| 18 | +/// Internal async Bitcoin RPC client using reqwest |
| 19 | +pub struct AsyncBitcoinRpc { |
| 20 | + client: Client, |
| 21 | + url: String, |
| 22 | + username: String, |
| 23 | + password: String, |
| 24 | +} |
| 25 | + |
| 26 | +impl AsyncBitcoinRpc { |
| 27 | + pub async fn new(url: String, auth: Auth) -> Result<Self> { |
| 28 | + let client = |
| 29 | + Client::builder().use_rustls_tls().build().context("Failed to create HTTP client")?; |
| 30 | + |
| 31 | + // Load credentials once at initialization - no repeated file I/O |
| 32 | + let (username, password) = match auth { |
| 33 | + Auth::UserPass(user, pass) => (user, pass), |
| 34 | + Auth::CookieFile(path) => { |
| 35 | + let cookie = tokio::fs::read_to_string(&path) |
| 36 | + .await |
| 37 | + .with_context(|| format!("Failed to read cookie file: {path:?}"))?; |
| 38 | + let parts: Vec<&str> = cookie.trim().split(':').collect(); |
| 39 | + if parts.len() != 2 { |
| 40 | + return Err(anyhow!("Invalid cookie format in file: {path:?}")); |
| 41 | + } |
| 42 | + (parts[0].to_string(), parts[1].to_string()) |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + Ok(Self { client, url, username, password }) |
| 47 | + } |
| 48 | + |
| 49 | + /// Get base URL without wallet path for blockchain-level calls |
| 50 | + fn get_base_url(&self) -> String { |
| 51 | + if let Some(pos) = self.url.find("/wallet/") { |
| 52 | + self.url[..pos].to_string() |
| 53 | + } else { |
| 54 | + self.url.clone() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// Make a JSON-RPC call to Bitcoin Core |
| 59 | + async fn call_rpc<T>(&self, method: &str, params: serde_json::Value) -> Result<T> |
| 60 | + where |
| 61 | + T: for<'de> Deserialize<'de>, |
| 62 | + { |
| 63 | + // Determine which URL to use based on the method |
| 64 | + // Blockchain/network calls go to base URL, wallet calls go to wallet URL |
| 65 | + let url = match method { |
| 66 | + "getblockchaininfo" | "getnetworkinfo" | "getmininginfo" | "getblockcount" |
| 67 | + | "getbestblockhash" | "getblock" | "getblockhash" | "gettxout" => self.get_base_url(), |
| 68 | + _ => self.url.clone(), |
| 69 | + }; |
| 70 | + |
| 71 | + let request_body = json!({ |
| 72 | + "jsonrpc": "2.0", |
| 73 | + "method": method, |
| 74 | + "params": params, |
| 75 | + "id": 1 |
| 76 | + }); |
| 77 | + |
| 78 | + let request = self |
| 79 | + .client |
| 80 | + .post(&url) |
| 81 | + .json(&request_body) |
| 82 | + .basic_auth(&self.username, Some(&self.password)); |
| 83 | + |
| 84 | + let response = request.send().await.context("Failed to send RPC request")?; |
| 85 | + |
| 86 | + if !response.status().is_success() { |
| 87 | + return Err(anyhow!("RPC request failed with status: {}", response.status())); |
| 88 | + } |
| 89 | + |
| 90 | + let json: RpcResponse<T> = response.json().await.context("Failed to parse RPC response")?; |
| 91 | + |
| 92 | + match json { |
| 93 | + RpcResponse::Success { result, .. } => Ok(result), |
| 94 | + RpcResponse::Error { error, .. } => Err(anyhow!("RPC error: {:?}", error)), |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + pub async fn wallet_create_funded_psbt( |
| 99 | + &self, |
| 100 | + inputs: &[Value], |
| 101 | + outputs: &HashMap<String, Amount>, |
| 102 | + locktime: Option<u32>, |
| 103 | + options: Option<Value>, |
| 104 | + bip32derivs: Option<bool>, |
| 105 | + ) -> Result<WalletCreateFundedPsbt> { |
| 106 | + let outputs_btc: HashMap<String, f64> = |
| 107 | + outputs.iter().map(|(addr, amount)| (addr.clone(), amount.to_btc())).collect(); |
| 108 | + |
| 109 | + let locktime = locktime.unwrap_or(0); |
| 110 | + let options = options.unwrap_or_else(|| json!({})); |
| 111 | + let bip32derivs = bip32derivs.unwrap_or(true); |
| 112 | + |
| 113 | + let params = json!([inputs, outputs_btc, locktime, options, bip32derivs]); |
| 114 | + self.call_rpc("walletcreatefundedpsbt", params).await |
| 115 | + } |
| 116 | + |
| 117 | + pub async fn wallet_process_psbt( |
| 118 | + &self, |
| 119 | + psbt: &str, |
| 120 | + sign: Option<bool>, |
| 121 | + sighash_type: Option<String>, |
| 122 | + bip32derivs: Option<bool>, |
| 123 | + ) -> Result<WalletProcessPsbt> { |
| 124 | + let sign = sign.unwrap_or(true); |
| 125 | + let sighash_type = sighash_type.unwrap_or_else(|| "ALL".to_string()); |
| 126 | + let bip32derivs = bip32derivs.unwrap_or(true); |
| 127 | + |
| 128 | + let params = json!([psbt, sign, sighash_type, bip32derivs]); |
| 129 | + self.call_rpc("walletprocesspsbt", params).await |
| 130 | + } |
| 131 | + |
| 132 | + pub async fn finalize_psbt( |
| 133 | + &self, |
| 134 | + psbt: &str, |
| 135 | + extract: Option<bool>, |
| 136 | + ) -> Result<FinalizePsbtResult> { |
| 137 | + let extract = extract.unwrap_or(true); |
| 138 | + let params = json!([psbt, extract]); |
| 139 | + self.call_rpc("finalizepsbt", params).await |
| 140 | + } |
| 141 | + |
| 142 | + pub async fn test_mempool_accept( |
| 143 | + &self, |
| 144 | + rawtxs: &[String], |
| 145 | + ) -> Result<Vec<TestMempoolAcceptResult>> { |
| 146 | + let params = json!([rawtxs]); |
| 147 | + self.call_rpc("testmempoolaccept", params).await |
| 148 | + } |
| 149 | + |
| 150 | + pub async fn send_raw_transaction(&self, hex: &[u8]) -> Result<Txid> { |
| 151 | + use payjoin::bitcoin::hex::DisplayHex; |
| 152 | + let hex_string = hex.to_lower_hex_string(); |
| 153 | + let params = json!([hex_string]); |
| 154 | + let txid_string: String = self.call_rpc("sendrawtransaction", params).await?; |
| 155 | + Ok(txid_string.parse()?) |
| 156 | + } |
| 157 | + |
| 158 | + pub async fn get_address_info(&self, address: &Address) -> Result<GetAddressInfoResult> { |
| 159 | + let params = json!([address.to_string()]); |
| 160 | + self.call_rpc("getaddressinfo", params).await |
| 161 | + } |
| 162 | + |
| 163 | + pub async fn get_new_address( |
| 164 | + &self, |
| 165 | + label: Option<&str>, |
| 166 | + address_type: Option<&str>, |
| 167 | + ) -> Result<Address<payjoin::bitcoin::address::NetworkUnchecked>> { |
| 168 | + let params = if label.is_none() && address_type.is_none() { |
| 169 | + json!([]) |
| 170 | + } else { |
| 171 | + json!([label, address_type]) |
| 172 | + }; |
| 173 | + |
| 174 | + let address_string: String = self.call_rpc("getnewaddress", params).await?; |
| 175 | + let addr: payjoin::bitcoin::Address<payjoin::bitcoin::address::NetworkUnchecked> = |
| 176 | + address_string.parse().context("Failed to parse address")?; |
| 177 | + Ok(addr) |
| 178 | + } |
| 179 | + |
| 180 | + pub async fn list_unspent( |
| 181 | + &self, |
| 182 | + minconf: Option<u32>, |
| 183 | + maxconf: Option<u32>, |
| 184 | + addresses: Option<&[Address]>, |
| 185 | + include_unsafe: Option<bool>, |
| 186 | + query_options: Option<Value>, |
| 187 | + ) -> Result<Vec<ListUnspentResult>> { |
| 188 | + let addresses_str: Option<Vec<String>> = |
| 189 | + addresses.map(|addrs| addrs.iter().map(|a| a.to_string()).collect()); |
| 190 | + let params = json!([minconf, maxconf, addresses_str, include_unsafe, query_options]); |
| 191 | + self.call_rpc("listunspent", params).await |
| 192 | + } |
| 193 | + |
| 194 | + pub async fn get_blockchain_info(&self) -> Result<serde_json::Value> { |
| 195 | + let params = json!([]); |
| 196 | + self.call_rpc("getblockchaininfo", params).await |
| 197 | + } |
| 198 | + |
| 199 | + pub async fn network(&self) -> Result<Network> { |
| 200 | + let info = self.get_blockchain_info().await?; |
| 201 | + let chain = info["chain"].as_str().ok_or_else(|| anyhow!("Missing chain field"))?; |
| 202 | + match chain { |
| 203 | + "main" => Ok(Network::Bitcoin), |
| 204 | + "test" => Ok(Network::Testnet), |
| 205 | + "regtest" => Ok(Network::Regtest), |
| 206 | + "signet" => Ok(Network::Signet), |
| 207 | + other => Err(anyhow!("Unknown network: {}", other)), |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +/// JSON-RPC response envelope |
| 213 | +#[derive(Serialize, Deserialize, Debug)] |
| 214 | +#[serde(untagged)] |
| 215 | +enum RpcResponse<T> { |
| 216 | + Success { result: T, error: Option<Value>, id: Value }, |
| 217 | + Error { result: Option<Value>, error: RpcError, id: Value }, |
| 218 | +} |
| 219 | + |
| 220 | +#[derive(Serialize, Deserialize, Debug)] |
| 221 | +struct RpcError { |
| 222 | + code: i32, |
| 223 | + message: String, |
| 224 | +} |
| 225 | + |
| 226 | +/// Result type for testmempoolaccept RPC call - minimal struct for our use case |
| 227 | +#[derive(Debug, Deserialize)] |
| 228 | +pub struct TestMempoolAcceptResult { |
| 229 | + pub allowed: bool, |
| 230 | + // Ignore additional fields that Bitcoin Core v29 may include |
| 231 | +} |
| 232 | + |
| 233 | +/// Result type for getaddressinfo RPC call - minimal struct for our use case |
| 234 | +#[derive(Debug, Deserialize)] |
| 235 | +pub struct GetAddressInfoResult { |
| 236 | + #[serde(rename = "ismine")] |
| 237 | + pub is_mine: bool, |
| 238 | +} |
| 239 | + |
| 240 | +/// Result type for listunspent RPC call - compatible with both v26 and v29+ |
| 241 | +#[derive(Debug, Deserialize)] |
| 242 | +pub struct ListUnspentResult { |
| 243 | + pub txid: String, |
| 244 | + pub vout: u32, |
| 245 | + #[serde(rename = "scriptPubKey")] |
| 246 | + pub script_pubkey: String, |
| 247 | + pub amount: f64, |
| 248 | + // Optional fields for compatibility with newer Bitcoin Core versions |
| 249 | + #[serde(rename = "redeemScript")] |
| 250 | + pub redeem_script: Option<String>, |
| 251 | + // Ignore additional fields that Bitcoin Core v29+ may include |
| 252 | +} |
| 253 | + |
| 254 | +/// Result type for finalizepsbt RPC call - compatible with both v26 and v29+ |
| 255 | +#[derive(Debug, Deserialize)] |
| 256 | +pub struct FinalizePsbtResult { |
| 257 | + pub hex: Option<String>, |
| 258 | +} |
0 commit comments