How to get scriptPubKey from UTXO

Hello everyone! I’m working on a Bitcoin integration, and I have a question about UTXO handling with iccdk.

I understand that in a P2PKH (Pay-to-PubKey-Hash) script, the scriptPubKey in a UTXO typically contains a pubkeyhash specifying the address authorized to redeem the bitcoins. I’m fetching UTXOs for a specific address, and I expected to be able to retrieve the scriptPubKey as part of the UTXO data. However, I’m not seeing this information in the response.

Here’s the code I’m using to fetch UTXOs:

pub async fn _get_utxos(network: BitcoinNetwork, address: String) -> GetUtxosResponse {
    let filter = None;
    let utxos_res = bitcoin_get_utxos(GetUtxosRequest {
        address,
        network,
        filter,
    })
    .await;

    utxos_res.unwrap().0
}

The UTXO structure, according to the docs, looks like this:

pub struct Utxo {
    pub outpoint: Outpoint,
    pub value: Satoshi,
    pub height: u32,
}

As you can see, the Utxo struct doesn’t seem to contain the scriptPubKey. My goal is to query the UTXOs for a given address and check if any UTXO’s scriptPubKey contains the pubkeyhash corresponding to the address I’m watching. If it does, I’d like to emit an event.

How can I access the scriptPubKey in this context, or is there another way to achieve this? Any advice would be appreciated!

Unfortunately, the script public key is not part of the outputs. Instead, the script public keys are “revealed” in the transaction inputs. That’s just how Bitcoin works.

You can get the transaction ID from the outpoint in the UTXO and download the referenced transaction using an HTTPS outcall (for example, using this call).

Awesome. This is very helpful