Skip to content

Commit 697e6e8

Browse files
committed
feat(client): add get_scripthash_utxos
1 parent 41879dd commit 697e6e8

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/async.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,19 @@ impl<S: Sleeper> AsyncClient<S> {
475475
Ok(blocks)
476476
}
477477

478-
/// Get all UTXOs locked to an address.
478+
/// Get all [`TxOut`]s locked to an [`Address`].
479479
pub async fn get_address_utxos(&self, address: &Address) -> Result<Vec<Utxo>, Error> {
480-
self.get_response_json(&format!("/address/{address}/utxo"))
481-
.await
480+
let path = format!("/address/{address}/utxo");
481+
482+
self.get_response_json(&path).await
483+
}
484+
485+
/// Get all [`TxOut`]s locked to a [`Script`] hash.
486+
pub async fn get_scripthash_utxos(&self, script: &Script) -> Result<Vec<Utxo>, Error> {
487+
let script_hash = sha256::Hash::hash(script.as_bytes());
488+
let path = format!("/scripthash/{script_hash}/utxo");
489+
490+
self.get_response_json(&path).await
482491
}
483492

484493
/// Get the underlying base URL.

src/blocking.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,19 @@ impl BlockingClient {
403403
Ok(blocks)
404404
}
405405

406-
/// Get all UTXOs locked to an address.
406+
/// Get all [`TxOut`]s locked to an [`Address`].
407407
pub fn get_address_utxos(&self, address: &Address) -> Result<Vec<Utxo>, Error> {
408-
self.get_response_json(&format!("/address/{address}/utxo"))
408+
let path = format!("/address/{address}/utxo");
409+
410+
self.get_response_json(&path)
411+
}
412+
413+
/// Get all [`TxOut`]s locked to a [`Script`] hash.
414+
pub fn get_scripthash_utxos(&self, script: &Script) -> Result<Vec<Utxo>, Error> {
415+
let script_hash = sha256::Hash::hash(script.as_bytes());
416+
let path = format!("/scripthash/{script_hash}/utxo");
417+
418+
self.get_response_json(&path)
409419
}
410420

411421
/// Sends a GET request to the given `url`, retrying failed attempts

src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,35 @@ mod test {
12791279
assert_eq!(address_utxos_blocking, address_utxos_async);
12801280
}
12811281

1282+
#[cfg(all(feature = "blocking", feature = "async"))]
1283+
#[tokio::test]
1284+
async fn test_get_scripthash_utxos() {
1285+
let (blocking_client, async_client) = setup_clients().await;
1286+
1287+
let address = BITCOIND
1288+
.client
1289+
.new_address_with_type(AddressType::Legacy)
1290+
.unwrap();
1291+
let script = address.script_pubkey();
1292+
1293+
let _txid = BITCOIND
1294+
.client
1295+
.send_to_address(&address, Amount::from_sat(21000))
1296+
.unwrap()
1297+
.txid()
1298+
.unwrap();
1299+
1300+
let _miner = MINER.lock().await;
1301+
generate_blocks_and_wait(1);
1302+
1303+
let scripthash_utxos_blocking = blocking_client.get_scripthash_utxos(&script).unwrap();
1304+
let scripthash_utxos_async = async_client.get_scripthash_utxos(&script).await.unwrap();
1305+
1306+
assert_ne!(scripthash_utxos_blocking.len(), 0);
1307+
assert_ne!(scripthash_utxos_async.len(), 0);
1308+
assert_eq!(scripthash_utxos_blocking, scripthash_utxos_async);
1309+
}
1310+
12821311
#[cfg(all(feature = "blocking", feature = "async"))]
12831312
#[tokio::test]
12841313
async fn test_get_tx_outspends() {

0 commit comments

Comments
 (0)