Use variable directly in format! string
There is a new lint error on nightly-2025-04-25 "variables can be used directly in the `format!` string". Use the variables in the `format!` string for all cases in `bitcoin/examples/`.
This commit is contained in:
parent
2fbbc825c9
commit
282434d4bd
|
@ -22,7 +22,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let seed_hex = &args[1];
|
let seed_hex = &args[1];
|
||||||
println!("Seed: {}", seed_hex);
|
println!("Seed: {seed_hex}");
|
||||||
println!("Using mainnet network");
|
println!("Using mainnet network");
|
||||||
|
|
||||||
let seed = Vec::from_hex(seed_hex).unwrap();
|
let seed = Vec::from_hex(seed_hex).unwrap();
|
||||||
|
@ -34,19 +34,19 @@ fn main() {
|
||||||
|
|
||||||
// calculate root key from seed
|
// calculate root key from seed
|
||||||
let root = Xpriv::new_master(NetworkKind::Main, &seed);
|
let root = Xpriv::new_master(NetworkKind::Main, &seed);
|
||||||
println!("Root key: {}", root);
|
println!("Root key: {root}");
|
||||||
|
|
||||||
// derive child xpub
|
// derive child xpub
|
||||||
let path = "84h/0h/0h".parse::<DerivationPath>().unwrap();
|
let path = "84h/0h/0h".parse::<DerivationPath>().unwrap();
|
||||||
let child = root.derive_xpriv(&secp, &path).expect("only deriving three steps");
|
let child = root.derive_xpriv(&secp, &path).expect("only deriving three steps");
|
||||||
println!("Child at {}: {}", path, child);
|
println!("Child at {path}: {child}");
|
||||||
let xpub = Xpub::from_xpriv(&secp, &child);
|
let xpub = Xpub::from_xpriv(&secp, &child);
|
||||||
println!("Public key at {}: {}", path, xpub);
|
println!("Public key at {path}: {xpub}");
|
||||||
|
|
||||||
// generate first receiving address at m/0/0
|
// generate first receiving address at m/0/0
|
||||||
// manually creating indexes this time
|
// manually creating indexes this time
|
||||||
let zero = ChildNumber::ZERO_NORMAL;
|
let zero = ChildNumber::ZERO_NORMAL;
|
||||||
let public_key = xpub.derive_xpub(&secp, &[zero, zero]).unwrap().public_key;
|
let public_key = xpub.derive_xpub(&secp, &[zero, zero]).unwrap().public_key;
|
||||||
let address = Address::p2wpkh(CompressedPublicKey(public_key), KnownHrp::Mainnet);
|
let address = Address::p2wpkh(CompressedPublicKey(public_key), KnownHrp::Mainnet);
|
||||||
println!("First receiving address: {}", address);
|
println!("First receiving address: {address}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,6 @@ fn main() {
|
||||||
// Create a Bitcoin P2WPKH address.
|
// Create a Bitcoin P2WPKH address.
|
||||||
let address = Address::p2wpkh(public_key, Network::Bitcoin);
|
let address = Address::p2wpkh(public_key, Network::Bitcoin);
|
||||||
|
|
||||||
println!("Private Key: {}", private_key);
|
println!("Private Key: {private_key}");
|
||||||
println!("Address: {}", address);
|
println!("Address: {address}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,8 +244,8 @@ fn main() {
|
||||||
// BOOM! Transaction signed and ready to broadcast.
|
// BOOM! Transaction signed and ready to broadcast.
|
||||||
let signed_tx = psbt.extract_tx().expect("valid transaction");
|
let signed_tx = psbt.extract_tx().expect("valid transaction");
|
||||||
let serialized_signed_tx = consensus::encode::serialize_hex(&signed_tx);
|
let serialized_signed_tx = consensus::encode::serialize_hex(&signed_tx);
|
||||||
println!("Transaction Details: {:#?}", signed_tx);
|
println!("Transaction Details: {signed_tx:#?}");
|
||||||
// check with:
|
// check with:
|
||||||
// bitcoin-cli decoderawtransaction <RAW_TX> true
|
// bitcoin-cli decoderawtransaction <RAW_TX> true
|
||||||
println!("Raw Transaction: {}", serialized_signed_tx);
|
println!("Raw Transaction: {serialized_signed_tx}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,7 +87,7 @@ fn main() -> Result<()> {
|
||||||
tx.verify(|_| Some(previous_output())).expect("failed to verify transaction");
|
tx.verify(|_| Some(previous_output())).expect("failed to verify transaction");
|
||||||
|
|
||||||
let hex = encode::serialize_hex(&tx);
|
let hex = encode::serialize_hex(&tx);
|
||||||
println!("You should now be able to broadcast the following transaction: \n\n{}", hex);
|
println!("You should now be able to broadcast the following transaction: \n\n{hex}");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ fn main() {
|
||||||
let str_address = &args[1];
|
let str_address = &args[1];
|
||||||
|
|
||||||
let address: SocketAddr = str_address.parse().unwrap_or_else(|error| {
|
let address: SocketAddr = str_address.parse().unwrap_or_else(|error| {
|
||||||
eprintln!("error parsing address: {:?}", error);
|
eprintln!("error parsing address: {error:?}");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -22,14 +22,14 @@ fn main() {
|
||||||
assert_eq!(decoded, script_code);
|
assert_eq!(decoded, script_code);
|
||||||
|
|
||||||
// Writes the script as human-readable eg, OP_DUP OP_HASH160 OP_PUSHBYTES_20 ...
|
// Writes the script as human-readable eg, OP_DUP OP_HASH160 OP_PUSHBYTES_20 ...
|
||||||
println!("human-readable script: {}", script_code);
|
println!("human-readable script: {script_code}");
|
||||||
|
|
||||||
// We do not implement parsing scripts from human-readable format.
|
// We do not implement parsing scripts from human-readable format.
|
||||||
// let decoded = s.parse::<ScriptBuf>().unwrap();
|
// let decoded = s.parse::<ScriptBuf>().unwrap();
|
||||||
|
|
||||||
// This is equivalent to consensus encoding i.e., includes the length prefix.
|
// This is equivalent to consensus encoding i.e., includes the length prefix.
|
||||||
let hex_lower_hex_trait = format!("{:x}", script_code);
|
let hex_lower_hex_trait = format!("{script_code:x}");
|
||||||
println!("hex created using `LowerHex`: {}", hex_lower_hex_trait);
|
println!("hex created using `LowerHex`: {hex_lower_hex_trait}");
|
||||||
|
|
||||||
// The `deserialize_hex` function requires the length prefix.
|
// The `deserialize_hex` function requires the length prefix.
|
||||||
assert_eq!(encode::deserialize_hex::<ScriptBuf>(&hex_lower_hex_trait).unwrap(), script_code);
|
assert_eq!(encode::deserialize_hex::<ScriptBuf>(&hex_lower_hex_trait).unwrap(), script_code);
|
||||||
|
@ -43,7 +43,7 @@ fn main() {
|
||||||
|
|
||||||
// This is consensus encoding i.e., includes the length prefix.
|
// This is consensus encoding i.e., includes the length prefix.
|
||||||
let hex_inherent = script_code.to_hex_string(); // Defined in `ScriptExt`.
|
let hex_inherent = script_code.to_hex_string(); // Defined in `ScriptExt`.
|
||||||
println!("hex created using inherent `to_hex_string`: {}", hex_inherent);
|
println!("hex created using inherent `to_hex_string`: {hex_inherent}");
|
||||||
|
|
||||||
// The inverse of `to_hex_string` is `from_hex`.
|
// The inverse of `to_hex_string` is `from_hex`.
|
||||||
let decoded = ScriptBuf::from_hex(&hex_inherent).unwrap(); // Defined in `ScriptBufExt`.
|
let decoded = ScriptBuf::from_hex(&hex_inherent).unwrap(); // Defined in `ScriptBufExt`.
|
||||||
|
@ -54,7 +54,7 @@ fn main() {
|
||||||
|
|
||||||
// We also support encode/decode using `consensus::encode` functions.
|
// We also support encode/decode using `consensus::encode` functions.
|
||||||
let encoded = encode::serialize_hex(&script_code);
|
let encoded = encode::serialize_hex(&script_code);
|
||||||
println!("hex created using consensus::encode::serialize_hex: {}", encoded);
|
println!("hex created using consensus::encode::serialize_hex: {encoded}");
|
||||||
|
|
||||||
let decoded: ScriptBuf = encode::deserialize_hex(&encoded).unwrap();
|
let decoded: ScriptBuf = encode::deserialize_hex(&encoded).unwrap();
|
||||||
assert_eq!(decoded, script_code);
|
assert_eq!(decoded, script_code);
|
||||||
|
|
|
@ -24,7 +24,7 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
|
||||||
let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
|
let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
|
||||||
let inp = &tx.input[inp_idx];
|
let inp = &tx.input[inp_idx];
|
||||||
let witness = &inp.witness;
|
let witness = &inp.witness;
|
||||||
println!("Witness: {:?}", witness);
|
println!("Witness: {witness:?}");
|
||||||
|
|
||||||
// BIP-141: The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a
|
// BIP-141: The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a
|
||||||
// signature, and the second one a public key.
|
// signature, and the second one a public key.
|
||||||
|
@ -38,16 +38,16 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
|
||||||
//this is nothing but a standard P2PKH script OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG:
|
//this is nothing but a standard P2PKH script OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG:
|
||||||
let pk = CompressedPublicKey::from_slice(pk_bytes).expect("failed to parse pubkey");
|
let pk = CompressedPublicKey::from_slice(pk_bytes).expect("failed to parse pubkey");
|
||||||
let wpkh = pk.wpubkey_hash();
|
let wpkh = pk.wpubkey_hash();
|
||||||
println!("Script pubkey hash: {:x}", wpkh);
|
println!("Script pubkey hash: {wpkh:x}");
|
||||||
let spk = ScriptBuf::new_p2wpkh(wpkh);
|
let spk = ScriptBuf::new_p2wpkh(wpkh);
|
||||||
|
|
||||||
let mut cache = sighash::SighashCache::new(&tx);
|
let mut cache = sighash::SighashCache::new(&tx);
|
||||||
let sighash = cache
|
let sighash = cache
|
||||||
.p2wpkh_signature_hash(inp_idx, &spk, amount, sig.sighash_type)
|
.p2wpkh_signature_hash(inp_idx, &spk, amount, sig.sighash_type)
|
||||||
.expect("failed to compute sighash");
|
.expect("failed to compute sighash");
|
||||||
println!("SegWit p2wpkh sighash: {:x}", sighash);
|
println!("SegWit p2wpkh sighash: {sighash:x}");
|
||||||
let msg = secp256k1::Message::from(sighash);
|
let msg = secp256k1::Message::from(sighash);
|
||||||
println!("Message is {:x}", msg);
|
println!("Message is {msg:x}");
|
||||||
let secp = secp256k1::Secp256k1::verification_only();
|
let secp = secp256k1::Secp256k1::verification_only();
|
||||||
pk.verify(&secp, msg, sig).unwrap()
|
pk.verify(&secp, msg, sig).unwrap()
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ fn compute_sighash_legacy(raw_tx: &[u8], inp_idx: usize, script_pubkey_bytes_opt
|
||||||
let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
|
let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
|
||||||
let inp = &tx.input[inp_idx];
|
let inp = &tx.input[inp_idx];
|
||||||
let script_sig = &inp.script_sig;
|
let script_sig = &inp.script_sig;
|
||||||
println!("scriptSig is: {}", script_sig);
|
println!("scriptSig is: {script_sig}");
|
||||||
let cache = sighash::SighashCache::new(&tx);
|
let cache = sighash::SighashCache::new(&tx);
|
||||||
//In the P2SH case we get scriptPubKey from scriptSig of the spending input.
|
//In the P2SH case we get scriptPubKey from scriptSig of the spending input.
|
||||||
//The scriptSig that corresponds to an M of N multisig should be: PUSHBYTES_0 PUSHBYTES_K0 <sig0><sighashflag0> ... PUSHBYTES_Km <sigM><sighashflagM> PUSHBYTES_X <scriptPubKey>
|
//The scriptSig that corresponds to an M of N multisig should be: PUSHBYTES_0 PUSHBYTES_K0 <sig0><sighashflag0> ... PUSHBYTES_Km <sigM><sighashflagM> PUSHBYTES_X <scriptPubKey>
|
||||||
|
@ -83,8 +83,7 @@ fn compute_sighash_legacy(raw_tx: &[u8], inp_idx: usize, script_pubkey_bytes_opt
|
||||||
let pushbytes_0 = instructions.remove(0).unwrap();
|
let pushbytes_0 = instructions.remove(0).unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
pushbytes_0.push_bytes().unwrap().as_bytes().is_empty(),
|
pushbytes_0.push_bytes().unwrap().as_bytes().is_empty(),
|
||||||
"first in ScriptSig must be PUSHBYTES_0 got {:?}",
|
"first in ScriptSig must be PUSHBYTES_0 got {pushbytes_0:?}"
|
||||||
pushbytes_0
|
|
||||||
);
|
);
|
||||||
|
|
||||||
//All other scriptSig instructions must be signatures
|
//All other scriptSig instructions must be signatures
|
||||||
|
@ -109,7 +108,7 @@ fn compute_sighash_p2wsh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
|
||||||
let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
|
let tx: Transaction = consensus::deserialize(raw_tx).unwrap();
|
||||||
let inp = &tx.input[inp_idx];
|
let inp = &tx.input[inp_idx];
|
||||||
let witness = &inp.witness;
|
let witness = &inp.witness;
|
||||||
println!("witness {:?}", witness);
|
println!("witness {witness:?}");
|
||||||
|
|
||||||
//last element is called witnessScript according to BIP141. It supersedes scriptPubKey.
|
//last element is called witnessScript according to BIP141. It supersedes scriptPubKey.
|
||||||
let witness_script_bytes: &[u8] = witness.last().expect("out of bounds");
|
let witness_script_bytes: &[u8] = witness.last().expect("out of bounds");
|
||||||
|
@ -122,7 +121,7 @@ fn compute_sighash_p2wsh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
|
||||||
let sig = ecdsa::Signature::from_slice(sig_bytes).expect("failed to parse sig");
|
let sig = ecdsa::Signature::from_slice(sig_bytes).expect("failed to parse sig");
|
||||||
let sig_len = sig_bytes.len() - 1; //last byte is EcdsaSighashType sighash flag
|
let sig_len = sig_bytes.len() - 1; //last byte is EcdsaSighashType sighash flag
|
||||||
//ECDSA signature in DER format lengths are between 70 and 72 bytes
|
//ECDSA signature in DER format lengths are between 70 and 72 bytes
|
||||||
assert!((70..=72).contains(&sig_len), "signature length {} out of bounds", sig_len);
|
assert!((70..=72).contains(&sig_len), "signature length {sig_len} out of bounds");
|
||||||
//here we assume that all sighash_flags are the same. Can they be different?
|
//here we assume that all sighash_flags are the same. Can they be different?
|
||||||
let sighash = cache
|
let sighash = cache
|
||||||
.p2wsh_signature_hash(inp_idx, witness_script, amount, sig.sighash_type)
|
.p2wsh_signature_hash(inp_idx, witness_script, amount, sig.sighash_type)
|
||||||
|
|
|
@ -82,7 +82,7 @@ fn main() {
|
||||||
let tx = sighasher.into_transaction();
|
let tx = sighasher.into_transaction();
|
||||||
|
|
||||||
// BOOM! Transaction signed and ready to broadcast.
|
// BOOM! Transaction signed and ready to broadcast.
|
||||||
println!("{:#?}", tx);
|
println!("{tx:#?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An example of keys controlled by the transaction sender.
|
/// An example of keys controlled by the transaction sender.
|
||||||
|
|
|
@ -81,7 +81,7 @@ fn main() {
|
||||||
let tx = sighasher.into_transaction();
|
let tx = sighasher.into_transaction();
|
||||||
|
|
||||||
// BOOM! Transaction signed and ready to broadcast.
|
// BOOM! Transaction signed and ready to broadcast.
|
||||||
println!("{:#?}", tx);
|
println!("{tx:#?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An example of keys controlled by the transaction sender.
|
/// An example of keys controlled by the transaction sender.
|
||||||
|
|
|
@ -244,8 +244,8 @@ fn main() {
|
||||||
// BOOM! Transaction signed and ready to broadcast.
|
// BOOM! Transaction signed and ready to broadcast.
|
||||||
let signed_tx = psbt.extract_tx().expect("valid transaction");
|
let signed_tx = psbt.extract_tx().expect("valid transaction");
|
||||||
let serialized_signed_tx = consensus::encode::serialize_hex(&signed_tx);
|
let serialized_signed_tx = consensus::encode::serialize_hex(&signed_tx);
|
||||||
println!("Transaction Details: {:#?}", signed_tx);
|
println!("Transaction Details: {signed_tx:#?}");
|
||||||
// check with:
|
// check with:
|
||||||
// bitcoin-cli decoderawtransaction <RAW_TX> true
|
// bitcoin-cli decoderawtransaction <RAW_TX> true
|
||||||
println!("Raw Transaction: {}", serialized_signed_tx);
|
println!("Raw Transaction: {serialized_signed_tx}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,8 +125,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
],
|
],
|
||||||
)?);
|
)?);
|
||||||
println!(
|
println!(
|
||||||
"\nYou should now be able to broadcast the following transaction: \n\n{}",
|
"\nYou should now be able to broadcast the following transaction: \n\n{tx_hex_string}"
|
||||||
tx_hex_string
|
|
||||||
);
|
);
|
||||||
|
|
||||||
println!("\nEND EXAMPLE 1\n");
|
println!("\nEND EXAMPLE 1\n");
|
||||||
|
@ -147,7 +146,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
)?;
|
)?;
|
||||||
let tx_hex = encode::serialize_hex(&tx);
|
let tx_hex = encode::serialize_hex(&tx);
|
||||||
|
|
||||||
println!("Inheritance funding tx hex:\n\n{}", tx_hex);
|
println!("Inheritance funding tx hex:\n\n{tx_hex}");
|
||||||
// You can now broadcast the transaction hex:
|
// You can now broadcast the transaction hex:
|
||||||
// bt sendrawtransaction ...
|
// bt sendrawtransaction ...
|
||||||
//
|
//
|
||||||
|
@ -160,7 +159,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
to_address,
|
to_address,
|
||||||
)?;
|
)?;
|
||||||
let spending_tx_hex = encode::serialize_hex(&spending_tx);
|
let spending_tx_hex = encode::serialize_hex(&spending_tx);
|
||||||
println!("\nInheritance spending tx hex:\n\n{}", spending_tx_hex);
|
println!("\nInheritance spending tx hex:\n\n{spending_tx_hex}");
|
||||||
// If you try to broadcast now, the transaction will be rejected as it is timelocked.
|
// If you try to broadcast now, the transaction will be rejected as it is timelocked.
|
||||||
// First mine 900 blocks so we're sure we are over the 1000 block locktime:
|
// First mine 900 blocks so we're sure we are over the 1000 block locktime:
|
||||||
// bt generatetoaddress 900 $(bt-benefactor getnewaddress '' 'bech32m')
|
// bt generatetoaddress 900 $(bt-benefactor getnewaddress '' 'bech32m')
|
||||||
|
@ -185,7 +184,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
)?;
|
)?;
|
||||||
let tx_hex = encode::serialize_hex(&tx);
|
let tx_hex = encode::serialize_hex(&tx);
|
||||||
|
|
||||||
println!("Inheritance funding tx hex:\n\n{}", tx_hex);
|
println!("Inheritance funding tx hex:\n\n{tx_hex}");
|
||||||
// You can now broadcast the transaction hex:
|
// You can now broadcast the transaction hex:
|
||||||
// bt sendrawtransaction ...
|
// bt sendrawtransaction ...
|
||||||
//
|
//
|
||||||
|
@ -200,7 +199,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let (tx, _) = benefactor.refresh_tx(1000)?;
|
let (tx, _) = benefactor.refresh_tx(1000)?;
|
||||||
let tx_hex = encode::serialize_hex(&tx);
|
let tx_hex = encode::serialize_hex(&tx);
|
||||||
|
|
||||||
println!("\nRefreshed inheritance tx hex:\n\n{}\n", tx_hex);
|
println!("\nRefreshed inheritance tx hex:\n\n{tx_hex}\n");
|
||||||
|
|
||||||
println!("\nEND EXAMPLE 3\n");
|
println!("\nEND EXAMPLE 3\n");
|
||||||
println!("----------------\n");
|
println!("----------------\n");
|
||||||
|
|
Loading…
Reference in New Issue