Skip to content

Commit 9c76e43

Browse files
committed
feat(client): add get_scripthash_utxos method
1 parent 01136dd commit 9c76e43

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/async.rs

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

528528
/// Get all UTXOs locked to an address.
529529
pub async fn get_address_utxos(&self, address: &Address) -> Result<Vec<Utxo>, Error> {
530-
self.get_response_json(&format!("/address/{address}/utxo"))
531-
.await
530+
let path = format!("/address/{address}/utxo");
531+
532+
self.get_response_json(&path).await
533+
}
534+
535+
/// Get all [`TxOut`]s locked to a [`Script`] hash.
536+
pub async fn get_scripthash_utxos(&self, script: &Script) -> Result<Vec<Utxo>, Error> {
537+
let script_hash = sha256::Hash::hash(script.as_bytes());
538+
let path = format!("/scripthash/{script_hash}/utxo");
539+
540+
self.get_response_json(&path).await
532541
}
533542

534543
/// Get the underlying base URL.

src/blocking.rs

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

456456
/// Get all UTXOs locked to an address.
457457
pub fn get_address_utxos(&self, address: &Address) -> Result<Vec<Utxo>, Error> {
458-
self.get_response_json(&format!("/address/{address}/utxo"))
458+
let path = format!("/address/{address}/utxo");
459+
460+
self.get_response_json(&path)
461+
}
462+
463+
/// Get all [`TxOut`]s locked to a [`Script`] hash.
464+
pub fn get_scripthash_utxos(&self, script: &Script) -> Result<Vec<Utxo>, Error> {
465+
let script_hash = sha256::Hash::hash(script.as_bytes());
466+
let path = format!("/scripthash/{script_hash}/utxo");
467+
468+
self.get_response_json(&path)
459469
}
460470

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

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,36 @@ mod test {
12781278
assert_ne!(address_utxos_async.len(), 0);
12791279
assert_eq!(address_utxos_blocking, address_utxos_async);
12801280
}
1281+
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+
12811311
#[cfg(all(feature = "blocking", feature = "async"))]
12821312
#[tokio::test]
12831313
async fn test_get_tx_outspends() {

0 commit comments

Comments
 (0)