Skip to content

Commit 10efefe

Browse files
committed
Corrections to tutorials/Analyzing_privacy_of_transaction.md
1 parent 6afb303 commit 10efefe

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

Diff for: blockchain_analysis/src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ fn main() {
5353
// track_utxo_set_10(&rpc_client, 831200).unwrap();
5454
let res = txs_sort_unique_input_addrs(&rpc_client, 144008).unwrap();
5555

56-
for r in res {
56+
// Displaying the top 10 transactions with greatest input addresses
57+
for i in 0..10 {
58+
let r = &res[i];
5759
println!("Transaction = {}, uses {} addresses", r.0.txid(), r.1);
5860
}
5961
}

Diff for: blockchain_analysis/src/privacy.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,28 @@ pub fn txs_sort_unique_input_addrs(
99
client: &Client,
1010
block_height: u64,
1111
) -> Result<Vec<(Transaction, u32)>, bitcoincore_rpc::Error> {
12-
let block_hash = client.get_block_hash(block_height)?;
13-
println!("block hash {}", block_hash);
1412
let block = get_block(block_height, client)?;
1513
let txs = block.txdata;
1614

1715
let mut result: Vec<(Transaction, u32)> = vec![];
1816
for tx in txs.iter() {
17+
if tx.is_coinbase() {
18+
continue;
19+
}
1920
let mut input_addrs: HashSet<Address> = HashSet::new();
21+
2022
for input in tx.input.iter() {
21-
let prev_tx =
22-
client.get_raw_transaction(&input.previous_output.txid, Some(&block_hash))?;
23-
let prev_tx_output = &prev_tx.output[input.previous_output.vout as usize];
24-
let prev_tx_script = &prev_tx_output.script_pubkey;
25-
let input_address = Address::from_script(prev_tx_script, Network::Bitcoin)
26-
.expect("Failed to parse address from scriptPubKey");
27-
input_addrs.insert(input_address);
23+
if let Ok(prev_tx) = client.get_raw_transaction(&input.previous_output.txid, None) {
24+
let prev_tx_output = &prev_tx.output[input.previous_output.vout as usize];
25+
let prev_tx_script = &prev_tx_output.script_pubkey;
26+
if let Ok(input_address) = Address::from_script(prev_tx_script, Network::Bitcoin) {
27+
input_addrs.insert(input_address);
28+
} else {
29+
println!("`Address::from_script` only recognizes p2sh, p2pkh and their respective segwit variants");
30+
}
31+
} else {
32+
println!("Unknown transaction id of an input {}", &input.previous_output.txid);
33+
}
2834
}
2935
result.push((tx.clone(), input_addrs.len() as u32));
3036
}

Diff for: tutorials/Analyzing_privacy_of_transaction.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn extract_from_TxIn(input: TxIn) -> bitcoin::Address {
7575
// In bitcoin crate TxIn is defined like the following
7676
struct TxIn {
7777
pub previous_output: OutPoint,
78-
pub script_sig: ScriptBuf, // It is not possible to extract address directly from here using current implementations
78+
pub script_sig: ScriptBuf, // contains signature and public key data
7979
pub sequence: Sequence,
8080
pub witness: Witness,
8181
}
@@ -89,6 +89,7 @@ struct OutPoint {
8989
}
9090
```
9191

92+
9293
So in order to extract address from the transaction input we have to get information
9394
about the transaction where the input was the output (TxOut).
9495
The following code demonstrates how to do it,

Diff for: tutorials/p2sh.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)