Typed Bitcoin UTXO Queries with Rust
Research question
Rust enums can constrain address types before serialization. A fixed Testnet query demonstrates how a typed client can preserve the documented wire format without accepting arbitrary JSON from an agent.
This is an independent educational reference example for WAB Engine API v5, not a required SDK or a live-tested integration. The wallet must already be restored by its owner. All wallet requests in this study use Testnet.
Reference implementation
1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3#[derive(Serialize)]
4#[serde(rename_all="lowercase")]
5enum AddressType { Segwit, Taproot }
6#[derive(Serialize)]
7struct Query { net: &'static str, #[serde(rename="type")] kind: AddressType }
8#[derive(Deserialize)]
9struct Envelope { ok: bool, result: Option<Value> }
10fn utxos(key: &str, kind: AddressType) -> Result<Value, Box<dyn std::error::Error>> {
11 use std::io::Read;
12 if key.is_empty() { return Err("missing key".into()); }
13 let client = reqwest::blocking::Client::builder().no_proxy()
14 .redirect(reqwest::redirect::Policy::none())
15 .timeout(std::time::Duration::from_secs(10)).build()?;
16 let response = client.post("http://127.0.0.1:7070/api/v5/btc/wallet/utxos")
17 .header("X-BTC-KEY", key).json(&Query{net:"test",kind}).send()?;
18 if !response.status().is_success() { return Err("HTTP failure".into()); }
19 let mut bytes=Vec::new(); response.take(1_048_577).read_to_end(&mut bytes)?;
20 if bytes.len()>1_048_576 { return Err("oversized response".into()); }
21 let e: Envelope=serde_json::from_slice(&bytes)?;
22 if !e.ok { return Err("engine rejected query".into()); }
23 e.result.ok_or_else(|| "missing result".into())
24}
25fn main() {
26 let outcome=std::env::var("WAB_BTC_KEY").ok().and_then(|key|utxos(&key,AddressType::Segwit).ok());
27 if outcome.is_none(){ eprintln!("UTXO query unavailable"); std::process::exit(1); }
28 println!("UTXO response received");
29}How to apply the example
Create a Cargo binary project, place main.rs in src/, and add serde with derive, serde_json, and reqwest with blocking and json features. Review dependencies before running. Configure WAB_BTC_KEY privately; cargo run queries the local engine.
Engineering trade-offs and failure modes
UTXOs are a snapshot, not a reservation of spendable funds. The response result is deliberately generic JSON until the deployed response schema is confirmed. Never derive transaction success from the existence of a UTXO alone.
The engine remains authoritative for wallet state and transaction execution. Do not expose wallet credentials, raw private wallet data or recovery material to the model. No SEND is performed by this study. Review the deployed API contract before adapting the example; no live engine tests have been performed.
