Skip to content

Commit faf27f3

Browse files
committed
feat: add get_scripthash_utxos
1 parent a390b6d commit faf27f3

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/async.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,17 @@ impl<S: Sleeper> AsyncClient<S> {
483483

484484
/// Get all UTXOs locked to an address.
485485
pub async fn get_address_utxos(&self, address: &Address) -> Result<Vec<Utxo>, Error> {
486-
self.get_response_json(&format!("/address/{address}/utxo"))
487-
.await
486+
let path = format!("/address/{address}/utxo");
487+
488+
self.get_response_json(&path).await
489+
}
490+
491+
/// Get all UTXOs locked to a scripthash.
492+
pub async fn get_scripthash_utxos(&self, script: &Script) -> Result<Vec<Utxo>, Error> {
493+
let script_hash = sha256::Hash::hash(script.as_bytes());
494+
let path = format!("/scripthash/{script_hash}/utxo");
495+
496+
self.get_response_json(&path).await
488497
}
489498

490499
/// Get the underlying base URL.

src/blocking.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,17 @@ impl BlockingClient {
408408

409409
/// Get all UTXOs locked to an address.
410410
pub fn get_address_utxos(&self, address: &Address) -> Result<Vec<Utxo>, Error> {
411-
self.get_response_json(&format!("/address/{address}/utxo"))
411+
let path = format!("/address/{address}/utxo");
412+
413+
self.get_response_json(&path)
414+
}
415+
416+
/// Get all UTXOs locked to a scripthash.
417+
pub fn get_scripthash_utxos(&self, script: &Script) -> Result<Vec<Utxo>, Error> {
418+
let script_hash = sha256::Hash::hash(script.as_bytes());
419+
let path = format!("/scripthash/{script_hash}/utxo");
420+
421+
self.get_response_json(&path)
412422
}
413423

414424
/// 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
@@ -1280,6 +1280,35 @@ mod test {
12801280
assert_eq!(address_utxos_blocking, address_utxos_async);
12811281
}
12821282

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

0 commit comments

Comments
 (0)