Skip to content

Commit 003b1ae

Browse files
chore: clean x11 into feature (#40)
1 parent 648b166 commit 003b1ae

File tree

13 files changed

+97
-44
lines changed

13 files changed

+97
-44
lines changed

Cargo.lock

Lines changed: 29 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dash/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ edition = "2021"
1919

2020
# Please don't forget to add relevant features to docs.rs below
2121
[features]
22-
default = [ "std", "secp-recovery" ]
22+
default = [ "std", "secp-recovery", "core-block-hash-use-x11" ]
2323
base64 = [ "base64-compat" ]
24-
rand-std = ["secp256k1/rand-std"]
24+
rand-std = ["secp256k1/rand"]
2525
rand = ["secp256k1/rand"]
2626
serde = ["actual-serde", "dashcore_hashes/serde", "secp256k1/serde"]
2727
secp-lowmemory = ["secp256k1/lowmemory"]
2828
secp-recovery = ["secp256k1/recovery"]
2929
signer = ["secp-recovery", "rand"]
30+
core-block-hash-use-x11 = ["dashcore_hashes/x11"]
3031

3132
# At least one of std, no-std must be enabled.
3233
#
@@ -44,7 +45,7 @@ rustdoc-args = ["--cfg", "docsrs"]
4445
internals = { package = "dashcore-private", version = "0.1.0", path = "../internals" }
4546
bech32 = { version = "0.9.0", default-features = false }
4647
dashcore_hashes = { path = "../hashes", package = "dashcore_hashes", default-features = false }
47-
secp256k1 = { default-features = false, features = ["bitcoin_hashes"], version= "0.27.0" }
48+
secp256k1 = { default-features = false, features = ["hashes"], version= "0.30.0" }
4849
core2 = { version = "0.3.0", optional = true, features = ["alloc"], default-features = false }
4950
rustversion = { version="1.0.9"}
5051
# Do NOT use this as a feature! Use the `serde` feature instead.
@@ -62,7 +63,7 @@ bincode = { version= "2.0.0-rc.3", optional = true }
6263
serde_json = "1.0.96"
6364
serde_test = "1.0.19"
6465
serde_derive = "1.0.103"
65-
secp256k1 = { features = [ "recovery", "rand-std", "bitcoin_hashes" ], version="0.27.0" }
66+
secp256k1 = { features = [ "recovery", "rand", "hashes" ], version="0.30.0" }
6667
bip39 = "2.0.0"
6768
bincode = "1.3.1"
6869

dash/src/bip32.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use secp256k1::{self, Secp256k1, XOnlyPublicKey};
3535
use serde;
3636

3737
use crate::base58;
38-
use crate::crypto::key::{self, KeyPair, PrivateKey, PublicKey};
38+
use crate::crypto::key::{self, Keypair, PrivateKey, PublicKey};
3939
use crate::hash_types::XpubIdentifier;
4040
use crate::internal_macros::impl_bytes_newtype;
4141
use crate::io::Write;
@@ -547,8 +547,8 @@ impl ExtendedPrivKey {
547547

548548
/// Constructs BIP340 keypair for Schnorr signatures and Taproot use matching the internal
549549
/// secret key representation.
550-
pub fn to_keypair<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> KeyPair {
551-
KeyPair::from_seckey_slice(secp, &self.private_key[..])
550+
pub fn to_keypair<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> Keypair {
551+
Keypair::from_seckey_slice(secp, &self.private_key[..])
552552
.expect("BIP32 internal private key representation is broken")
553553
}
554554

dash/src/consensus/encode.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
use core::convert::From;
3434
use core::{fmt, mem, u32};
3535

36-
use hashes::{Hash, hash_x11, hash160, sha256, sha256d};
36+
#[cfg(feature = "core-block-hash-use-x11")]
37+
use hashes::hash_x11;
38+
use hashes::{Hash, hash160, sha256, sha256d};
3739
use internals::write_err;
3840

3941
use crate::bip152::{PrefilledTransaction, ShortId};
@@ -841,12 +843,14 @@ tuple_encode!(T0, T1, T2, T3, T4, T5);
841843
tuple_encode!(T0, T1, T2, T3, T4, T5, T6);
842844
tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7);
843845

846+
#[cfg(feature = "core-block-hash-use-x11")]
844847
impl Decodable for hash_x11::Hash {
845848
fn consensus_decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
846849
Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
847850
}
848851
}
849852

853+
#[cfg(feature = "core-block-hash-use-x11")]
850854
impl Encodable for hash_x11::Hash {
851855
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
852856
self.as_byte_array().consensus_encode(w)

dash/src/crypto/key.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use core::str::FromStr;
2727
use hashes::hex::FromHex;
2828
use hashes::{Hash, hash160, hex};
2929
use internals::write_err;
30-
pub use secp256k1::{self, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey, constants};
30+
pub use secp256k1::{self, Keypair, Parity, Secp256k1, Verification, XOnlyPublicKey, constants};
3131

3232
use crate::hash_types::{PubkeyHash, WPubkeyHash};
3333
use crate::network::constants::Network;
@@ -542,7 +542,7 @@ impl fmt::Display for TweakedPublicKey {
542542
}
543543

544544
/// Untweaked BIP-340 key pair
545-
pub type UntweakedKeyPair = KeyPair;
545+
pub type UntweakedKeyPair = Keypair;
546546

547547
/// Tweaked BIP-340 key pair
548548
///
@@ -563,7 +563,7 @@ pub type UntweakedKeyPair = KeyPair;
563563
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
564564
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
565565
#[cfg_attr(feature = "serde", serde(transparent))]
566-
pub struct TweakedKeyPair(KeyPair);
566+
pub struct TweakedKeyPair(Keypair);
567567

568568
/// A trait for tweaking BIP340 key types (x-only public keys and key pairs).
569569
pub trait TapTweak {
@@ -693,11 +693,11 @@ impl TweakedKeyPair {
693693
/// This method is dangerous and can lead to loss of funds if used incorrectly.
694694
/// Specifically, in multi-party protocols a peer can provide a value that allows them to steal.
695695
#[inline]
696-
pub fn dangerous_assume_tweaked(pair: KeyPair) -> TweakedKeyPair { TweakedKeyPair(pair) }
696+
pub fn dangerous_assume_tweaked(pair: Keypair) -> TweakedKeyPair { TweakedKeyPair(pair) }
697697

698698
/// Returns the underlying key pair.
699699
#[inline]
700-
pub fn to_inner(self) -> KeyPair { self.0 }
700+
pub fn to_inner(self) -> Keypair { self.0 }
701701

702702
/// Returns the [`TweakedPublicKey`] and its [`Parity`] for this [`TweakedKeyPair`].
703703
#[inline]
@@ -712,7 +712,7 @@ impl From<TweakedPublicKey> for XOnlyPublicKey {
712712
fn from(pair: TweakedPublicKey) -> Self { pair.0 }
713713
}
714714

715-
impl From<TweakedKeyPair> for KeyPair {
715+
impl From<TweakedKeyPair> for Keypair {
716716
#[inline]
717717
fn from(pair: TweakedKeyPair) -> Self { pair.0 }
718718
}
@@ -1048,7 +1048,7 @@ mod tests {
10481048
use secp256k1::rand;
10491049

10501050
let secp = Secp256k1::new();
1051-
let kp = KeyPair::new(&secp, &mut rand::thread_rng());
1051+
let kp = Keypair::new(&secp, &mut rand::thread_rng());
10521052

10531053
let _ = PublicKey::new(kp);
10541054
let _ = PublicKey::new_uncompressed(kp);

dash/src/hash_types.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,36 @@ mod newtypes {
7171
use crate::alloc::string::ToString;
7272

7373
use core::str::FromStr;
74-
use hashes::{sha256, sha256d, hash160, hash_x11, hash_newtype};
74+
use hashes::{sha256, sha256d, hash160, hash_newtype};
7575
use hashes::hex::Error;
7676
use crate::prelude::String;
77+
#[cfg(feature = "core-block-hash-use-x11")]
78+
use hashes::hash_x11;
79+
80+
#[cfg(feature = "core-block-hash-use-x11")]
81+
hash_newtype! {
82+
/// A dash block hash.
83+
pub struct BlockHash(hash_x11::Hash);
84+
/// CycleHash is a cycle hash
85+
pub struct CycleHash(hash_x11::Hash);
86+
}
87+
#[cfg(not(feature = "core-block-hash-use-x11"))]
88+
hash_newtype! {
89+
/// A dash block hash.
90+
pub struct BlockHash(sha256d::Hash);
91+
/// CycleHash is a cycle hash
92+
pub struct CycleHash(sha256d::Hash);
93+
}
7794

7895
hash_newtype! {
7996
/// A dash transaction hash/transaction ID.
8097
pub struct Txid(sha256d::Hash);
8198

8299
/// A dash witness transaction ID.
83100
pub struct Wtxid(sha256d::Hash);
84-
/// A dash block hash.
85-
pub struct BlockHash(hash_x11::Hash);
101+
102+
103+
86104

87105
/// A hash of a public key.
88106
pub struct PubkeyHash(hash160::Hash);
@@ -127,8 +145,6 @@ mod newtypes {
127145
/// ProTxHash is a pro-tx hash
128146
#[hash_newtype(forward)]
129147
pub struct ProTxHash(sha256d::Hash);
130-
/// CycleHash is a cycle hash
131-
pub struct CycleHash(hash_x11::Hash);
132148
}
133149

134150
impl_hashencode!(Txid);

dash/src/network/message.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ mod test {
514514
use std::net::Ipv4Addr;
515515

516516
use hashes::Hash as HashTrait;
517+
#[cfg(feature = "core-block-hash-use-x11")]
517518
use hashes::hash_x11::Hash as X11Hash;
518519
use hashes::sha256d::Hash;
519520

@@ -535,9 +536,15 @@ mod test {
535536
};
536537

537538
fn hash(slice: [u8; 32]) -> Hash { Hash::from_slice(&slice).unwrap() }
539+
540+
#[cfg(feature = "core-block-hash-use-x11")]
538541
fn hash_x11(slice: [u8; 32]) -> X11Hash { X11Hash::from_slice(&slice).unwrap() }
539542

543+
#[cfg(not(feature = "core-block-hash-use-x11"))]
544+
fn hash_x11(slice: [u8; 32]) -> Hash { Hash::from_slice(&slice).unwrap() }
545+
540546
#[test]
547+
#[cfg(feature = "core-block-hash-use-x11")]
541548
fn full_round_ser_der_raw_network_message_test() {
542549
// TODO: Impl Rand traits here to easily generate random values.
543550
let version_msg: VersionMessage = deserialize(&hex!("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001")).unwrap();

dash/src/network/message_network.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
//! capabilities.
2222
//!
2323
24-
use hashes::hash_x11;
24+
#[cfg(feature = "core-block-hash-use-x11")]
25+
use hashes::hash_x11 as hashType;
26+
#[cfg(not(feature = "core-block-hash-use-x11"))]
27+
use hashes::sha256d as hashType;
2528

2629
use crate::consensus::{Decodable, Encodable, ReadExt, encode};
2730
use crate::internal_macros::impl_consensus_encoding;
@@ -149,14 +152,17 @@ pub struct Reject {
149152
/// reason of rejectection
150153
pub reason: Cow<'static, str>,
151154
/// reference to rejected item
152-
pub hash: hash_x11::Hash,
155+
pub hash: hashType::Hash,
153156
}
154157

155158
impl_consensus_encoding!(Reject, message, ccode, reason, hash);
156159

157160
#[cfg(test)]
158161
mod tests {
159-
use hashes::hash_x11;
162+
#[cfg(feature = "core-block-hash-use-x11")]
163+
use hashes::hash_x11 as hashType;
164+
#[cfg(not(feature = "core-block-hash-use-x11"))]
165+
use hashes::sha256d as hashType;
160166

161167
use super::{Reject, RejectReason, VersionMessage};
162168
use crate::consensus::encode::{deserialize, serialize};
@@ -206,7 +212,7 @@ mod tests {
206212
assert_eq!("txn-mempool-conflict", conflict.reason);
207213
assert_eq!(
208214
"0470f4f2dc4191221b59884bcffaaf00932748ab46356a80413c0b86d354df05"
209-
.parse::<hash_x11::Hash>()
215+
.parse::<hashType::Hash>()
210216
.unwrap(),
211217
conflict.hash
212218
);
@@ -217,7 +223,7 @@ mod tests {
217223
assert_eq!("non-final", nonfinal.reason);
218224
assert_eq!(
219225
"0b46a539138b5fde4e341b37f2d945c23d41193b30caa7fcbd8bdb836cbe9b25"
220-
.parse::<hash_x11::Hash>()
226+
.parse::<hashType::Hash>()
221227
.unwrap(),
222228
nonfinal.hash
223229
);

dash/src/sign_message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ mod message_signing {
116116
let (recid, raw) = self.signature.serialize_compact();
117117
let mut serialized = [0u8; 65];
118118
serialized[0] = 27;
119-
serialized[0] += recid.to_i32() as u8;
119+
serialized[0] += <RecoveryId as Into<i32>>::into(recid) as u8;
120120
if self.compressed {
121121
serialized[0] += 4;
122122
}
@@ -135,7 +135,7 @@ mod message_signing {
135135
secp256k1::Error::InvalidRecoveryId,
136136
));
137137
};
138-
let recid = RecoveryId::from_i32(((bytes[0] - 27) & 0x03) as i32)?;
138+
let recid = RecoveryId::try_from(((bytes[0] - 27) & 0x03) as i32)?;
139139
Ok(MessageSignature {
140140
signature: RecoverableSignature::from_compact(&bytes[1..], recid)?,
141141
compressed: ((bytes[0] - 27) & 0x04) != 0,

dash/src/signer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ impl CompactSignature for RecoverableSignature {
103103

104104
RecoverableSignature::from_compact(
105105
&signature.as_ref()[1..],
106-
RecoveryId::from_i32(i).unwrap(),
106+
RecoveryId::try_from(i).unwrap(),
107107
)
108108
.map_err(anyhow::Error::msg)
109109
}
110110

111111
fn to_compact_signature(&self, is_compressed: bool) -> [u8; 65] {
112112
let (recovery_byte, signature) = self.serialize_compact();
113-
let mut val = recovery_byte.to_i32() + 27 + 4;
113+
let mut val = <RecoveryId as Into<i32>>::into(recovery_byte) + 27 + 4;
114114
if !is_compressed {
115115
val -= 4;
116116
}

0 commit comments

Comments
 (0)