Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bitcoin/examples/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ fn main() {

let version_message = build_version_message(address);

let first_message =
RawNetworkMessage::new(bitcoin::Network::Bitcoin.magic(), version_message);
let first_message = RawNetworkMessage::new(bitcoin::Network::Bitcoin.magic(), version_message);

if let Ok(mut stream) = TcpStream::connect(address) {
// Send the message
Expand Down
13 changes: 8 additions & 5 deletions bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub enum AddressType {
/// Pay to taproot.
P2tr,
/// Pay to anchor.
P2a
P2a,
}

impl fmt::Display for AddressType {
Expand Down Expand Up @@ -249,17 +249,17 @@ pub enum AddressData {
/// Data encoded by a P2PKH address.
P2pkh {
/// The pubkey hash used to encumber outputs to this address.
pubkey_hash: PubkeyHash
pubkey_hash: PubkeyHash,
},
/// Data encoded by a P2SH address.
P2sh {
/// The script hash used to encumber outputs to this address.
script_hash: ScriptHash
script_hash: ScriptHash,
},
/// Data encoded by a Segwit address.
Segwit {
/// The witness program used to encumber outputs to this address.
witness_program: WitnessProgram
witness_program: WitnessProgram,
},
}

Expand Down Expand Up @@ -565,7 +565,10 @@ impl Address {
pub fn is_spend_standard(&self) -> bool { self.address_type().is_some() }

/// Constructs an [`Address`] from an output script (`scriptPubkey`).
pub fn from_script(script: &Script, params: impl AsRef<Params>) -> Result<Address, FromScriptError> {
pub fn from_script(
script: &Script,
params: impl AsRef<Params>,
) -> Result<Address, FromScriptError> {
let network = params.as_ref().network;
if script.is_p2pkh() {
let bytes = script.as_bytes()[3..23].try_into().expect("statically 20B long");
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ mod test {
use hex::test_hex_unwrap as hex;

use super::*;
use crate::consensus::params;
use crate::consensus::encode::serialize;
use crate::consensus::params;

#[test]
fn bitcoin_genesis_first_transaction() {
Expand Down
12 changes: 3 additions & 9 deletions bitcoin/src/blockdata/locktime/absolute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,25 +297,19 @@ impl FromStr for LockTime {
impl TryFrom<&str> for LockTime {
type Error = ParseIntError;

fn try_from(s: &str) -> Result<Self, Self::Error> {
LockTime::from_str(s)
}
fn try_from(s: &str) -> Result<Self, Self::Error> { LockTime::from_str(s) }
}

impl TryFrom<String> for LockTime {
type Error = ParseIntError;

fn try_from(s: String) -> Result<Self, Self::Error> {
LockTime::from_str(&s)
}
fn try_from(s: String) -> Result<Self, Self::Error> { LockTime::from_str(&s) }
}

impl TryFrom<Box<str>> for LockTime {
type Error = ParseIntError;

fn try_from(s: Box<str>) -> Result<Self, Self::Error> {
LockTime::from_str(&s)
}
fn try_from(s: Box<str>) -> Result<Self, Self::Error> { LockTime::from_str(&s) }
}

impl From<Height> for LockTime {
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/script/witness_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const MIN_SIZE: usize = 2;
pub const MAX_SIZE: usize = 40;

/// The P2A program which is given by 0x4e73.
pub(crate) const P2A_PROGRAM: [u8;2] = [78, 115];
pub(crate) const P2A_PROGRAM: [u8; 2] = [78, 115];

/// The segregated witness program.
///
Expand Down
14 changes: 4 additions & 10 deletions bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//! This module provides the structures and functions needed to support transactions.
//!

use core::{cmp, fmt};
use core::str::FromStr;
use core::{cmp, fmt};

use hashes::{sha256d, Hash};
use internals::write_err;
Expand Down Expand Up @@ -539,25 +539,19 @@ impl FromStr for Sequence {
impl TryFrom<&str> for Sequence {
type Error = ParseIntError;

fn try_from(s: &str) -> Result<Self, Self::Error> {
Sequence::from_str(s)
}
fn try_from(s: &str) -> Result<Self, Self::Error> { Sequence::from_str(s) }
}

impl TryFrom<String> for Sequence {
type Error = ParseIntError;

fn try_from(s: String) -> Result<Self, Self::Error> {
Sequence::from_str(&s)
}
fn try_from(s: String) -> Result<Self, Self::Error> { Sequence::from_str(&s) }
}

impl TryFrom<Box<str>> for Sequence {
type Error = ParseIntError;

fn try_from(s: Box<str>) -> Result<Self, Self::Error> {
Sequence::from_str(&s)
}
fn try_from(s: Box<str>) -> Result<Self, Self::Error> { Sequence::from_str(&s) }
}

/// Bitcoin transaction output.
Expand Down
46 changes: 25 additions & 21 deletions bitcoin/src/blockdata/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use crate::consensus::encode::{Error, MAX_VEC_SIZE};
use crate::consensus::{Decodable, Encodable, WriteExt};
use crate::crypto::ecdsa;
use crate::prelude::*;
use crate::taproot::{self, LeafScript, LeafVersion, TAPROOT_ANNEX_PREFIX, TAPROOT_CONTROL_BASE_SIZE, TAPROOT_LEAF_MASK};
use crate::taproot::{
self, LeafScript, LeafVersion, TAPROOT_ANNEX_PREFIX, TAPROOT_CONTROL_BASE_SIZE,
TAPROOT_LEAF_MASK,
};
use crate::{Script, VarInt};

/// The Witness is the data used to unlock bitcoin since the [segwit upgrade].
Expand Down Expand Up @@ -429,10 +432,13 @@ impl Witness {
/// version.
pub fn taproot_leaf_script(&self) -> Option<LeafScript<&Script>> {
match P2TrSpend::from_witness(self) {
Some(P2TrSpend::Script { leaf_script, control_block, .. }) if control_block.len() >= TAPROOT_CONTROL_BASE_SIZE => {
let version = LeafVersion::from_consensus(control_block[0] & TAPROOT_LEAF_MASK).ok()?;
Some(LeafScript { version, script: leaf_script, })
},
Some(P2TrSpend::Script { leaf_script, control_block, .. })
if control_block.len() >= TAPROOT_CONTROL_BASE_SIZE =>
{
let version =
LeafVersion::from_consensus(control_block[0] & TAPROOT_LEAF_MASK).ok()?;
Some(LeafScript { version, script: leaf_script })
}
_ => None,
}
}
Expand All @@ -456,18 +462,14 @@ impl Witness {
/// This does not guarantee that this represents a P2TR [`Witness`].
///
/// See [`Script::is_p2tr`] to check whether this is actually a Taproot witness.
pub fn taproot_annex(&self) -> Option<&[u8]> {
P2TrSpend::from_witness(self)?.annex()
}
pub fn taproot_annex(&self) -> Option<&[u8]> { P2TrSpend::from_witness(self)?.annex() }

/// Get the p2wsh witness script following BIP141 rules.
///
/// This does not guarantee that this represents a P2WS [`Witness`]. See
/// [Script::is_p2wsh](crate::blockdata::script::Script::is_p2wsh) to
/// check whether this is actually a P2WSH witness.
pub fn witness_script(&self) -> Option<&Script> {
self.last().map(Script::from_bytes)
}
pub fn witness_script(&self) -> Option<&Script> { self.last().map(Script::from_bytes) }
}

impl Index<usize> for Witness {
Expand Down Expand Up @@ -520,14 +522,16 @@ impl<'a> P2TrSpend<'a> {
// for the fact that annex is still there.
match witness.len() {
0 => None,
1 => Some(P2TrSpend::Key { /* signature: witness.last().expect("len > 0") ,*/ annex: None }),
1 => Some(P2TrSpend::Key {
/* signature: witness.last().expect("len > 0") ,*/ annex: None,
}),
2 if witness.last().expect("len > 0").starts_with(&[TAPROOT_ANNEX_PREFIX]) => {
let spend = P2TrSpend::Key {
// signature: witness.second_to_last().expect("len > 1"),
annex: witness.last(),
};
Some(spend)
},
}
// 2 => this is script spend without annex - same as when there are 3+ elements and the
// last one does NOT start with TAPROOT_ANNEX_PREFIX. This is handled in the catchall
// arm.
Expand All @@ -538,15 +542,15 @@ impl<'a> P2TrSpend<'a> {
annex: witness.last(),
};
Some(spend)
},
}
_ => {
let spend = P2TrSpend::Script {
leaf_script: Script::from_bytes(witness.second_to_last().expect("len > 1")),
control_block: witness.last().expect("len > 0"),
annex: None,
};
Some(spend)
},
}
}
}

Expand Down Expand Up @@ -870,7 +874,8 @@ mod test {
#[test]
fn get_taproot_leaf_script() {
let tapscript = hex!("deadbeef");
let control_block = hex!("c0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
let control_block =
hex!("c0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
// annex starting with 0x50 causes the branching logic.
let annex = hex!("50");

Expand All @@ -883,10 +888,8 @@ mod test {
let witness = deserialize::<Witness>(&witness_serialized[..]).unwrap();
let witness_annex = deserialize::<Witness>(&witness_serialized_annex[..]).unwrap();

let expected_leaf_script = LeafScript {
version: LeafVersion::TapScript,
script: Script::from_bytes(&tapscript),
};
let expected_leaf_script =
LeafScript { version: LeafVersion::TapScript, script: Script::from_bytes(&tapscript) };

// With or without annex, the tapscript should be returned.
assert_eq!(witness.taproot_leaf_script().unwrap(), expected_leaf_script);
Expand All @@ -911,7 +914,8 @@ mod test {

let witness = deserialize::<Witness>(&witness_serialized[..]).unwrap();
let witness_annex = deserialize::<Witness>(&witness_serialized_annex[..]).unwrap();
let witness_key_spend_annex = deserialize::<Witness>(&witness_serialized_key_spend_annex[..]).unwrap();
let witness_key_spend_annex =
deserialize::<Witness>(&witness_serialized_key_spend_annex[..]).unwrap();

// With or without annex, the tapscript should be returned.
assert_eq!(witness.taproot_control_block(), Some(&control_block[..]));
Expand Down
8 changes: 2 additions & 6 deletions bitcoin/src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,7 @@ macro_rules! impl_int_encodable {
($ty:ident, $meth_dec:ident, $meth_enc:ident) => {
impl Decodable for $ty {
#[inline]
fn consensus_decode<R: Read + ?Sized>(
r: &mut R,
) -> core::result::Result<Self, Error> {
fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> core::result::Result<Self, Error> {
ReadExt::$meth_dec(r)
}
}
Expand Down Expand Up @@ -593,9 +591,7 @@ macro_rules! impl_array {

impl Decodable for [u8; $size] {
#[inline]
fn consensus_decode<R: Read + ?Sized>(
r: &mut R,
) -> core::result::Result<Self, Error> {
fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> core::result::Result<Self, Error> {
let mut ret = [0; $size];
r.read_slice(&mut ret)?;
Ok(ret)
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/consensus/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub static SIGNET: Params = Params::SIGNET;
/// The regtest parameters.
pub static REGTEST: Params = Params::REGTEST;

#[allow(deprecated)] // For `pow_limit`.
#[allow(deprecated)] // For `pow_limit`.
impl Params {
/// The mainnet parameters (alias for `Params::MAINNET`).
pub const BITCOIN: Params = Params::MAINNET;
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ impl TweakedPublicKey {
}

#[doc(hidden)]
#[deprecated(since="0.32.6", note="use to_x_only_public_key() instead")]
#[deprecated(since = "0.32.6", note = "use to_x_only_public_key() instead")]
pub fn to_inner(self) -> XOnlyPublicKey { self.0 }

/// Returns the underlying x-only public key.
Expand Down Expand Up @@ -884,7 +884,7 @@ impl TweakedKeypair {
pub fn dangerous_assume_tweaked(pair: Keypair) -> TweakedKeypair { TweakedKeypair(pair) }

#[doc(hidden)]
#[deprecated(since="0.32.6", note="use to_keypair() instead")]
#[deprecated(since = "0.32.6", note = "use to_keypair() instead")]
pub fn to_inner(self) -> Keypair { self.0 }

/// Returns the underlying key pair.
Expand Down
4 changes: 1 addition & 3 deletions bitcoin/src/crypto/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,7 @@ impl EcdsaSighashType {
/// type (after masking with 0x1f), regardless of the ANYONECANPAY flag.
///
/// See: <https://github.com/bitcoin/bitcoin/blob/e486597/src/script/interpreter.cpp#L1618-L1619>
pub fn is_single(&self) -> bool {
matches!(self, Self::Single | Self::SinglePlusAnyoneCanPay)
}
pub fn is_single(&self) -> bool { matches!(self, Self::Single | Self::SinglePlusAnyoneCanPay) }

/// Creates a [`EcdsaSighashType`] from a raw `u32`.
///
Expand Down
25 changes: 7 additions & 18 deletions bitcoin/src/dogecoin/address/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use core::fmt;

use internals::write_err;

use crate::dogecoin::address::{Address, NetworkUnchecked};
use crate::dogecoin::Network;

pub use crate::address::error::{
FromScriptError, InvalidBase58PayloadLengthError, InvalidLegacyPrefixError,
LegacyAddressTooLongError, P2shError,
};
use crate::dogecoin::address::{Address, NetworkUnchecked};
use crate::dogecoin::Network;

/// Address parsing error.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -62,33 +61,23 @@ impl std::error::Error for ParseError {
}

impl From<base58::Error> for ParseError {
fn from(e: base58::Error) -> Self {
Self::Base58(e)
}
fn from(e: base58::Error) -> Self { Self::Base58(e) }
}

impl From<LegacyAddressTooLongError> for ParseError {
fn from(e: LegacyAddressTooLongError) -> Self {
Self::LegacyAddressTooLong(e)
}
fn from(e: LegacyAddressTooLongError) -> Self { Self::LegacyAddressTooLong(e) }
}

impl From<InvalidBase58PayloadLengthError> for ParseError {
fn from(e: InvalidBase58PayloadLengthError) -> Self {
Self::InvalidBase58PayloadLength(e)
}
fn from(e: InvalidBase58PayloadLengthError) -> Self { Self::InvalidBase58PayloadLength(e) }
}

impl From<InvalidLegacyPrefixError> for ParseError {
fn from(e: InvalidLegacyPrefixError) -> Self {
Self::InvalidLegacyPrefix(e)
}
fn from(e: InvalidLegacyPrefixError) -> Self { Self::InvalidLegacyPrefix(e) }
}

impl From<NetworkValidationError> for ParseError {
fn from(e: NetworkValidationError) -> Self {
Self::NetworkValidation(e)
}
fn from(e: NetworkValidationError) -> Self { Self::NetworkValidation(e) }
}

/// Address's network differs from required one.
Expand Down
Loading