Skip to content

Commit 0a87280

Browse files
committed
feat(client): add get_scripthash_utxos method
1 parent 17fcf9a commit 0a87280

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
@@ -1275,6 +1275,36 @@ mod test {
12751275
assert_ne!(address_utxos_async.len(), 0);
12761276
assert_eq!(address_utxos_blocking, address_utxos_async);
12771277
}
1278+
1279+
#[cfg(all(feature = "blocking", feature = "async"))]
1280+
#[tokio::test]
1281+
async fn test_get_scripthash_utxos() {
1282+
let (blocking_client, async_client) = setup_clients().await;
1283+
1284+
let address = BITCOIND
1285+
.client
1286+
.new_address_with_type(AddressType::Legacy)
1287+
.unwrap();
1288+
let script = address.script_pubkey();
1289+
1290+
let _txid = BITCOIND
1291+
.client
1292+
.send_to_address(&address, Amount::from_sat(21000))
1293+
.unwrap()
1294+
.txid()
1295+
.unwrap();
1296+
1297+
let _miner = MINER.lock().await;
1298+
generate_blocks_and_wait(1);
1299+
1300+
let scripthash_utxos_blocking = blocking_client.get_scripthash_utxos(&script).unwrap();
1301+
let scripthash_utxos_async = async_client.get_scripthash_utxos(&script).await.unwrap();
1302+
1303+
assert_ne!(scripthash_utxos_blocking.len(), 0);
1304+
assert_ne!(scripthash_utxos_async.len(), 0);
1305+
assert_eq!(scripthash_utxos_blocking, scripthash_utxos_async);
1306+
}
1307+
12781308
#[cfg(all(feature = "blocking", feature = "async"))]
12791309
#[tokio::test]
12801310
async fn test_get_tx_outspends() {

0 commit comments

Comments
 (0)