Skip to content

Commit cd4749b

Browse files
committed
feat: add get_scripthash_utxos
1 parent 78677f8 commit cd4749b

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
@@ -1277,6 +1277,35 @@ mod test {
12771277
assert_eq!(address_utxos_blocking, address_utxos_async);
12781278
}
12791279

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

0 commit comments

Comments
 (0)