Skip to content

Commit ca9cbf0

Browse files
authored
Merge pull request #128 from sgeisler/2020-08-scantxoutset
Add scantxoutset
2 parents a9d182d + 36a0676 commit ca9cbf0

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

client/src/client.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use log::Level::{Debug, Trace, Warn};
2929
use error::*;
3030
use json;
3131
use queryable;
32+
use serde_json::Value;
3233

3334
/// Crate-specific Result type, shorthand for `std::result::Result` with our
3435
/// crate-specific Error type;
@@ -1016,6 +1017,13 @@ pub trait RpcApi: Sized {
10161017
fn uptime(&self) -> Result<u64> {
10171018
self.call("uptime", &[])
10181019
}
1020+
1021+
fn scan_tx_out_set_blocking(
1022+
&self,
1023+
descriptors: &[json::ScanTxOutRequest],
1024+
) -> Result<json::ScanTxOutResult> {
1025+
self.call("scantxoutset", &["start".into(), into_json(descriptors)?])
1026+
}
10191027
}
10201028

10211029
/// Client implements a JSON-RPC client for the Bitcoin Core daemon or compatible APIs.

integration_test/src/main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use bitcoin::{
3030
Address, Amount, Network, OutPoint, PrivateKey, Script, SigHashType, SignedAmount, Transaction,
3131
TxIn, TxOut, Txid,
3232
};
33+
use bitcoincore_rpc::bitcoincore_rpc_json::ScanTxOutRequest;
3334

3435
lazy_static! {
3536
static ref SECP: secp256k1::Secp256k1<secp256k1::All> = secp256k1::Secp256k1::new();
@@ -138,6 +139,7 @@ fn main() {
138139
test_combine_psbt(&cl);
139140
test_finalize_psbt(&cl);
140141
test_list_received_by_address(&cl);
142+
test_scantxoutset(&cl);
141143
test_import_public_key(&cl);
142144
test_import_priv_key(&cl);
143145
test_import_address(&cl);
@@ -917,6 +919,20 @@ fn test_uptime(cl: &Client) {
917919
cl.uptime().unwrap();
918920
}
919921

922+
fn test_scantxoutset(cl: &Client) {
923+
let addr = cl.get_new_address(None, None).unwrap();
924+
925+
cl.generate_to_address(2, &addr).unwrap();
926+
cl.generate_to_address(7, &cl.get_new_address(None, None).unwrap()).unwrap();
927+
928+
let utxos = cl
929+
.scan_tx_out_set_blocking(&[ScanTxOutRequest::Single(format!("addr({})", addr))])
930+
.unwrap();
931+
932+
assert_eq!(utxos.unspents.len(), 2);
933+
assert_eq!(utxos.success, Some(true));
934+
}
935+
920936
fn test_stop(cl: Client) {
921937
println!("Stopping: '{}'", cl.stop().unwrap());
922938
}

json/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,47 @@ pub enum PubKeyOrAddress<'a> {
12531253
PubKey(&'a PublicKey),
12541254
}
12551255

1256+
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
1257+
#[serde(untagged)]
1258+
/// Start a scan of the UTXO set for an [output descriptor](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md).
1259+
pub enum ScanTxOutRequest {
1260+
/// Scan for a single descriptor
1261+
Single(String),
1262+
/// Scan for a descriptor with xpubs
1263+
Extended {
1264+
/// Descriptor
1265+
desc: String,
1266+
/// Range of the xpub derivations to scan
1267+
range: (u64, u64),
1268+
},
1269+
}
1270+
1271+
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
1272+
pub struct ScanTxOutResult {
1273+
pub success: Option<bool>,
1274+
#[serde(rename = "txouts")]
1275+
pub tx_outs: Option<u64>,
1276+
pub height: Option<u64>,
1277+
#[serde(rename = "bestblock")]
1278+
pub best_block_hash: Option<bitcoin::BlockHash>,
1279+
pub unspents: Vec<Utxo>,
1280+
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
1281+
pub total_amount: bitcoin::Amount,
1282+
}
1283+
1284+
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
1285+
#[serde(rename_all = "camelCase")]
1286+
pub struct Utxo {
1287+
pub txid: bitcoin::Txid,
1288+
pub vout: u32,
1289+
pub script_pub_key: bitcoin::Script,
1290+
#[serde(rename = "desc")]
1291+
pub descriptor: String,
1292+
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
1293+
pub amount: bitcoin::Amount,
1294+
pub height: u64,
1295+
}
1296+
12561297
impl<'a> serde::Serialize for PubKeyOrAddress<'a> {
12571298
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
12581299
where

0 commit comments

Comments
 (0)