Remove old consensus code from examples
What changed, and why it matters
This commit only updates example code in the rust-bitcoin project. It replaces older 'consensus' encoding/decoding API calls with newer 'consensus_encoding' API calls in three example files. There is no change to the library's actual security-sensitive code, no bug fix, and no security patch.
No security action required. Treat as a routine documentation/examples maintenance commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff modifies three example files under bitcoin/examples/ (io.rs, script.rs, sighash.rs). It migrates calls from the legacy bitcoin::consensus::{Decodable, Encodable, encode::deserialize, serialize_hex, etc.} interfaces to the newer bitcoin::encoding / io::encode_to_writer / decode_from_read / ExactSizeEncoder APIs. The changes are purely demonstrative and documentation-oriented, intended to show users the modern encoding API. No library internals, no consensus rules, and no cryptographic code are changed.
Changed components
bitcoin/examples/io.rsbitcoin/examples/script.rsbitcoin/examples/sighash.rsInspect captured patch +22 / −19
diff --git a/bitcoin/examples/io.rs b/bitcoin/examples/io.rs
index 1cabb4f4..c60352a9 100644
--- a/bitcoin/examples/io.rs
+++ b/bitcoin/examples/io.rs
@@ -7,8 +7,8 @@
//! this we provide the `bitcoin_io` crate which provides `io::Read`, `io::BufRead`, and
//! `io::Write`. This module demonstrates its usage.
-use bitcoin::consensus::{Decodable, Encodable as _};
use bitcoin::{OutPoint, Txid};
+use encoding::{Encode, ExactSizeEncoder};
fn main() {
// Encode/Decode a `rust-bitcoin` type to/from a stdlib type.
@@ -33,13 +33,12 @@ fn encode_decode_from_stdlib_type() {
let mut v = Vec::new();
// Under the hood we implement our `io` traits for a bunch of stdlib types so this just works.
- let _bytes_written = data.consensus_encode(&mut v).expect("failed to encode to writer");
+ io::encode_to_writer(&data, &mut v).expect("failed to encode to writer");
// Slices implement `std::io::Read`.
- let mut reader = v.as_ref();
+ let reader = v.as_ref();
- let _: OutPoint =
- Decodable::consensus_decode(&mut reader).expect("failed to decode from reader");
+ let _: OutPoint = io::decode_from_read(reader).expect("failed to decode from reader");
}
/// Encodes to a custom type by implementing the `bitcoin_io::Write` trait.
@@ -68,7 +67,9 @@ fn encode_to_custom_type() {
let data = dummy_utxo();
let mut counter = WriteCounter { count: 0 };
- let bytes_written = data.consensus_encode(&mut counter).expect("failed to encode to writer");
+ let mut encoder = data.encoder();
+ let bytes_written = encoder.len();
+ io::drain_to_writer(&mut encoder, &mut counter).expect("failed to encode to writer");
assert_eq!(bytes_written, 36); // 32 bytes for txid + 4 bytes for vout.
}
@@ -87,7 +88,9 @@ fn encode_using_wrapper() {
// let bytes_written = data.consensus_encode(&mut counter)?;
let mut counter = io::FromStd::new(WriteCounter::new());
- let bytes_written = data.consensus_encode(&mut counter).expect("failed to encode to writer");
+ let mut encoder = data.encoder();
+ let bytes_written = encoder.len();
+ io::drain_to_writer(&mut encoder, &mut counter).expect("failed to encode to writer");
assert_eq!(bytes_written, 36); // 32 bytes for txid + 4 bytes for vout.
assert_eq!(bytes_written, counter.get_ref().written());
diff --git a/bitcoin/examples/script.rs b/bitcoin/examples/script.rs
index abcf824e..16b87a73 100644
--- a/bitcoin/examples/script.rs
+++ b/bitcoin/examples/script.rs
@@ -7,7 +7,7 @@
//!
//! [`CompactSize`]: <https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer>
-use bitcoin::consensus::encode;
+use bitcoin::encoding;
use bitcoin::key::WPubkeyHash;
use bitcoin::{script, WitnessScriptBuf};
@@ -37,7 +37,7 @@ fn main() {
println!("hex created using `LowerHex`: {hex_lower_hex_trait}");
// The `deserialize_hex` function requires the length prefix.
- assert!(encode::deserialize_hex::<WitnessScriptBuf>(&hex_lower_hex_trait).is_err());
+ assert!(encoding::decode_from_hex::<WitnessScriptBuf>(&hex_lower_hex_trait).is_err());
// And so does `from_hex_prefixed`.
assert!(WitnessScriptBuf::from_hex_prefixed(&hex_lower_hex_trait).is_err());
// But we provide an explicit constructor that does not.
@@ -54,25 +54,25 @@ fn main() {
let decoded = WitnessScriptBuf::from_hex_prefixed(&hex_inherent).unwrap(); // Defined in `ScriptBufExt`.
assert_eq!(decoded, script_code);
// We can also parse the output of `to_hex_string_prefixed` using `deserialize_hex`.
- let decoded = encode::deserialize_hex::<WitnessScriptBuf>(&hex_inherent).unwrap();
+ let decoded = encoding::decode_from_hex::<WitnessScriptBuf>(&hex_inherent).unwrap();
assert_eq!(decoded, script_code);
// We also support encode/decode using `consensus::encode` functions.
- let encoded = encode::serialize_hex(&script_code);
+ let encoded = encoding::encode_to_hex(script_code.as_script(), hex::Case::Lower);
println!("hex created using consensus::encode::serialize_hex: {encoded}");
- let decoded: WitnessScriptBuf = encode::deserialize_hex(&encoded).unwrap();
+ let decoded: WitnessScriptBuf = encoding::decode_from_hex(&encoded).unwrap();
assert_eq!(decoded, script_code);
// And we can mix these two calls because both include the length prefix.
- let encoded = encode::serialize_hex(&script_code);
+ let encoded = encoding::encode_to_hex(script_code.as_script(), hex::Case::Lower);
let decoded = WitnessScriptBuf::from_hex_prefixed(&encoded).unwrap();
assert_eq!(decoded, script_code);
// Encode/decode using a byte vector.
- let encoded = encode::serialize(&script_code);
+ let encoded = encoding::encode_to_vec(script_code.as_script());
assert_eq!(&encoded[1..], script_code.as_bytes()); // Shows that prefix is the first byte.
- let decoded: WitnessScriptBuf = encode::deserialize(&encoded).unwrap();
+ let decoded: WitnessScriptBuf = encoding::decode_from_slice(&encoded).unwrap();
assert_eq!(decoded, script_code);
// to/from bytes excludes the prefix, these are not encoding/decoding functions so this is sane.
diff --git a/bitcoin/examples/sighash.rs b/bitcoin/examples/sighash.rs
index 766ecbed..ebf6e05b 100644
--- a/bitcoin/examples/sighash.rs
+++ b/bitcoin/examples/sighash.rs
@@ -1,6 +1,6 @@
use bitcoin::ext::*;
use bitcoin::{
- consensus, ecdsa, sighash, Amount, FullPublicKey, ScriptPubKey, ScriptPubKeyBuf, Transaction,
+ ecdsa, encoding, sighash, Amount, FullPublicKey, ScriptPubKey, ScriptPubKeyBuf, Transaction,
WitnessScript,
};
use hex::hex;
@@ -21,7 +21,7 @@ use hex::hex;
/// * `inp_idx` - the spending tx input index
/// * `amount` - the ref tx output amount.
fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
- let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
+ let tx: Transaction = encoding::decode_from_slice(raw_tx).unwrap();
let inp = &tx.inputs[inp_idx];
let witness = &inp.witness;
println!("Witness: {witness:?}");
@@ -60,7 +60,7 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
/// * `inp_idx` - the spending tx input index
/// * `script_pubkey_bytes_opt` - the Option with scriptPubKey bytes. If None, it's p2sh case, i.e., reftx output's scriptPubKey.type is "scripthash". In this case scriptPubkey is extracted from the spending transaction's scriptSig. If Some(), it's p2ms case, i.e., reftx output's scriptPubKey.type is "multisig", and the scriptPubkey is supplied from the referenced output.
fn compute_sighash_legacy(raw_tx: &[u8], inp_idx: usize, script_pubkey_bytes_opt: Option<&[u8]>) {
- let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
+ let tx: Transaction = encoding::decode_from_slice(raw_tx).unwrap();
let inp = &tx.inputs[inp_idx];
let script_sig = &inp.script_sig;
println!("scriptSig is: {script_sig}");
@@ -105,7 +105,7 @@ fn compute_sighash_legacy(raw_tx: &[u8], inp_idx: usize, script_pubkey_bytes_opt
/// * `inp_idx` - the spending tx input index
/// * `amount` - the ref tx output amount.
fn compute_sighash_p2wsh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
- let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
+ let tx: Transaction = encoding::decode_from_slice(raw_tx).unwrap();
let inp = &tx.inputs[inp_idx];
let witness = &inp.witness;
println!("witness {witness:?}");
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.