MultiversX Tracker is Live!

How to lookup a previous transactions script with rust-bitcoin

Bitcoin Stack Exchange

Bitcoin News / Bitcoin Stack Exchange 206 Views

I am using the rust-bitcoin and bitcoincore-rpc crates and want to be able to write some code which is able to lookup the script_pubkey of the first input to a transaction (from the previous transaction that provided the input).

To attempt to do this, I've written some code which:

  • Get's the Outpoint of the first input to a transaction
  • Looks up the transaction that outpoint is contained in
  • Get the script_pubkey of that outpoint

I'm having some trouble getting the last point to work, as i've been unable to figure out a way to dynamically reference the output vector based on the vout value from the first transaction's input.

See the code below for an example of what I am trying to do

I'm a newbie to both the Rust programming language, and the rust-bitcoin package, and using this an an exercise to try to teach myself, so apologies if I am missing something simple, or if there is a much better way to do this, and thank-you very much for any help provided!

extern crate bitcoincore_rpc;
use bitcoincore_rpc::bitcoin::Txid;
use bitcoincore_rpc::{Auth, Client, RpcApi};
use std::str::FromStr;
fn main() { let rpc = Client::new( "http://localhost:8332", Auth::UserPass("bitcoinrpc".to_string(), "PASSWORD".to_string()), ) .unwrap(); // Get the transaction we are using to test let txid = Txid::from_str("796941d13be9085d9d12ac97d5f9d9e6c9470cc400ea112aa4093f4c12f4c5cf").unwrap(); let tx = rpc.get_raw_transaction(&txid, None).unwrap(); println!("Transaction: {:?}\n--", tx); // Get the first input of the transaction let first_input_outpoint = &tx.input[0].previous_output; println!("First input outpoint: {:?}\n--", first_input_outpoint); // Get the first input's transaction let first_input_tx = rpc .get_raw_transaction(&first_input_outpoint.txid, None) .unwrap(); println!("First Input Transaction: {:?}\n--", first_input_tx); // Get the script // // This will not compile due to "the type `[TxOut]` cannot be indexed by `u32`" error // Statically referencing the index like first_input_tx.output[1].script_pubkey works fine, but I need to be able to // dynamically reference based on the vout index from the original transaction, and not sure how to do this. let first_input_script_pubkey = &first_input_tx.output[first_input_outpoint.vout].script_pubkey;
}

Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
πŸ’° Install these recommended apps:
πŸ’² SocialGood - 100% Crypto Back on Everyday Shopping
πŸ’² xPortal - The DeFi For The Next Billion
πŸ’² CryptoTab Browser - Lightweight, fast, and ready to mine!
πŸ’° Register on these recommended exchanges:
🟑 Binance🟑 Bitfinex🟑 Bitmart🟑 Bittrex🟑 Bitget
🟑 CoinEx🟑 Crypto.com🟑 Gate.io🟑 Huobi🟑 Kucoin.



Comments