Skip to content

Commit 9cb1184

Browse files
committed
docs: fixed example
1 parent 51faf39 commit 9cb1184

File tree

1 file changed

+103
-34
lines changed

1 file changed

+103
-34
lines changed

dash/src/crypto/key.rs

Lines changed: 103 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ use core::ops;
2525
use core::str::FromStr;
2626

2727
use hashes::hex::FromHex;
28-
use hashes::{Hash, hash160, hex};
28+
use hashes::{hash160, hex, Hash};
2929
use internals::write_err;
30-
pub use secp256k1::{self, Keypair, Parity, Secp256k1, Verification, XOnlyPublicKey, constants};
30+
pub use secp256k1::{self, constants, Keypair, Parity, Secp256k1, Verification, XOnlyPublicKey};
3131

3232
use crate::hash_types::{PubkeyHash, WPubkeyHash};
3333
use crate::network::constants::Network;
@@ -101,17 +101,23 @@ impl std::error::Error for Error {
101101

102102
#[doc(hidden)]
103103
impl From<base58::Error> for Error {
104-
fn from(e: base58::Error) -> Error { Error::Base58(e) }
104+
fn from(e: base58::Error) -> Error {
105+
Error::Base58(e)
106+
}
105107
}
106108

107109
#[doc(hidden)]
108110
impl From<secp256k1::Error> for Error {
109-
fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) }
111+
fn from(e: secp256k1::Error) -> Error {
112+
Error::Secp256k1(e)
113+
}
110114
}
111115

112116
#[doc(hidden)]
113117
impl From<hex::Error> for Error {
114-
fn from(e: hex::Error) -> Self { Error::Hex(e) }
118+
fn from(e: hex::Error) -> Self {
119+
Error::Hex(e)
120+
}
115121
}
116122

117123
/// A Dash ECDSA public key
@@ -126,13 +132,19 @@ pub struct PublicKey {
126132
impl PublicKey {
127133
/// Constructs compressed ECDSA public key from the provided generic Secp256k1 public key
128134
pub fn new(key: impl Into<secp256k1::PublicKey>) -> PublicKey {
129-
PublicKey { compressed: true, inner: key.into() }
135+
PublicKey {
136+
compressed: true,
137+
inner: key.into(),
138+
}
130139
}
131140

132141
/// Constructs uncompressed (legacy) ECDSA public key from the provided generic Secp256k1
133142
/// public key
134143
pub fn new_uncompressed(key: impl Into<secp256k1::PublicKey>) -> PublicKey {
135-
PublicKey { compressed: false, inner: key.into() }
144+
PublicKey {
145+
compressed: false,
146+
inner: key.into(),
147+
}
136148
}
137149

138150
fn with_serialized<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
@@ -144,7 +156,9 @@ impl PublicKey {
144156
}
145157

146158
/// Returns dash 160-bit hash of the public key
147-
pub fn pubkey_hash(&self) -> PubkeyHash { self.with_serialized(PubkeyHash::hash) }
159+
pub fn pubkey_hash(&self) -> PubkeyHash {
160+
self.with_serialized(PubkeyHash::hash)
161+
}
148162

149163
/// Returns dash 160-bit hash of the public key for witness program
150164
pub fn wpubkey_hash(&self) -> Option<WPubkeyHash> {
@@ -172,7 +186,11 @@ impl PublicKey {
172186
let mut bytes = [0; 65];
173187

174188
reader.read_exact(&mut bytes[0..1])?;
175-
let bytes = if bytes[0] < 4 { &mut bytes[..33] } else { &mut bytes[..65] };
189+
let bytes = if bytes[0] < 4 {
190+
&mut bytes[..33]
191+
} else {
192+
&mut bytes[..65]
193+
};
176194

177195
reader.read_exact(&mut bytes[1..])?;
178196
Self::from_slice(bytes).map_err(|e| {
@@ -278,7 +296,10 @@ impl PublicKey {
278296
return Err(Error::InvalidKeyPrefix(data[0]));
279297
}
280298

281-
Ok(PublicKey { compressed, inner: secp256k1::PublicKey::from_slice(data)? })
299+
Ok(PublicKey {
300+
compressed,
301+
inner: secp256k1::PublicKey::from_slice(data)?,
302+
})
282303
}
283304

284305
/// Computes the public key as supposed to be used with this secret
@@ -318,7 +339,9 @@ impl FromStr for PublicKey {
318339
}
319340

320341
impl From<PublicKey> for PubkeyHash {
321-
fn from(key: PublicKey) -> PubkeyHash { key.pubkey_hash() }
342+
fn from(key: PublicKey) -> PubkeyHash {
343+
key.pubkey_hash()
344+
}
322345
}
323346

324347
/// A Dash ECDSA private key
@@ -337,13 +360,21 @@ impl PrivateKey {
337360
/// Constructs compressed ECDSA private key from the provided generic Secp256k1 private key
338361
/// and the specified network
339362
pub fn new(key: secp256k1::SecretKey, network: Network) -> PrivateKey {
340-
PrivateKey { compressed: true, network, inner: key }
363+
PrivateKey {
364+
compressed: true,
365+
network,
366+
inner: key,
367+
}
341368
}
342369

343370
/// Constructs uncompressed (legacy) ECDSA private key from the provided generic Secp256k1
344371
/// private key and the specified network
345372
pub fn new_uncompressed(key: secp256k1::SecretKey, network: Network) -> PrivateKey {
346-
PrivateKey { compressed: false, network, inner: key }
373+
PrivateKey {
374+
compressed: false,
375+
network,
376+
inner: key,
377+
}
347378
}
348379

349380
/// Creates a public key from this private key
@@ -355,7 +386,9 @@ impl PrivateKey {
355386
}
356387

357388
/// Serialize the private key to bytes
358-
pub fn to_bytes(self) -> Vec<u8> { self.inner[..].to_vec() }
389+
pub fn to_bytes(self) -> Vec<u8> {
390+
self.inner[..].to_vec()
391+
}
359392

360393
/// Deserialize a private key from a slice
361394
pub fn from_slice(data: &[u8], network: Network) -> Result<PrivateKey, Error> {
@@ -416,22 +449,30 @@ impl PrivateKey {
416449
}
417450

418451
impl fmt::Display for PrivateKey {
419-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt_wif(f) }
452+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
453+
self.fmt_wif(f)
454+
}
420455
}
421456

422457
#[cfg(not(feature = "std"))]
423458
impl fmt::Debug for PrivateKey {
424-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[private key data]") }
459+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
460+
write!(f, "[private key data]")
461+
}
425462
}
426463

427464
impl FromStr for PrivateKey {
428465
type Err = Error;
429-
fn from_str(s: &str) -> Result<PrivateKey, Error> { PrivateKey::from_wif(s) }
466+
fn from_str(s: &str) -> Result<PrivateKey, Error> {
467+
PrivateKey::from_wif(s)
468+
}
430469
}
431470

432471
impl ops::Index<ops::RangeFull> for PrivateKey {
433472
type Output = [u8];
434-
fn index(&self, _: ops::RangeFull) -> &[u8] { &self.inner[..] }
473+
fn index(&self, _: ops::RangeFull) -> &[u8] {
474+
&self.inner[..]
475+
}
435476
}
436477

437478
#[cfg(feature = "serde")]
@@ -554,11 +595,15 @@ pub type UntweakedPublicKey = XOnlyPublicKey;
554595
pub struct TweakedPublicKey(XOnlyPublicKey);
555596

556597
impl fmt::LowerHex for TweakedPublicKey {
557-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
598+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
599+
fmt::LowerHex::fmt(&self.0, f)
600+
}
558601
}
559602

560603
impl fmt::Display for TweakedPublicKey {
561-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
604+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
605+
fmt::Display::fmt(&self.0, f)
606+
}
562607
}
563608

564609
/// Untweaked BIP-340 key pair
@@ -569,10 +614,10 @@ pub type UntweakedKeyPair = Keypair;
569614
/// # Examples
570615
/// ```
571616
/// # #[cfg(feature = "rand-std")] {
572-
/// # use dashcore::key::{KeyPair, TweakedKeyPair, TweakedPublicKey};
617+
/// # use dashcore::key::{Keypair, TweakedKeyPair, TweakedPublicKey};
573618
/// # use dashcore::secp256k1::{rand, Secp256k1};
574619
/// # let secp = Secp256k1::new();
575-
/// # let keypair = TweakedKeyPair::dangerous_assume_tweaked(KeyPair::new(&secp, &mut rand::thread_rng()));
620+
/// # let keypair = TweakedKeyPair::dangerous_assume_tweaked(Keypair::new(&secp, &mut rand::thread_rng()));
576621
/// // There are various conversion methods available to get a tweaked pubkey from a tweaked keypair.
577622
/// let (_pk, _parity) = keypair.public_parts();
578623
/// let _pk = TweakedPublicKey::from_keypair(keypair);
@@ -645,7 +690,9 @@ impl TapTweak for UntweakedPublicKey {
645690
(TweakedPublicKey(output_key), parity)
646691
}
647692

648-
fn dangerous_assume_tweaked(self) -> TweakedPublicKey { TweakedPublicKey(self) }
693+
fn dangerous_assume_tweaked(self) -> TweakedPublicKey {
694+
TweakedPublicKey(self)
695+
}
649696
}
650697

651698
impl TapTweak for UntweakedKeyPair {
@@ -675,7 +722,9 @@ impl TapTweak for UntweakedKeyPair {
675722
TweakedKeyPair(tweaked)
676723
}
677724

678-
fn dangerous_assume_tweaked(self) -> TweakedKeyPair { TweakedKeyPair(self) }
725+
fn dangerous_assume_tweaked(self) -> TweakedKeyPair {
726+
TweakedKeyPair(self)
727+
}
679728
}
680729

681730
impl TweakedPublicKey {
@@ -697,13 +746,17 @@ impl TweakedPublicKey {
697746
}
698747

699748
/// Returns the underlying public key.
700-
pub fn to_inner(self) -> XOnlyPublicKey { self.0 }
749+
pub fn to_inner(self) -> XOnlyPublicKey {
750+
self.0
751+
}
701752

702753
/// Serialize the key as a byte-encoded pair of values. In compressed form
703754
/// the y-coordinate is represented by only a single bit, as x determines
704755
/// it up to one bit.
705756
#[inline]
706-
pub fn serialize(&self) -> [u8; constants::SCHNORR_PUBLIC_KEY_SIZE] { self.0.serialize() }
757+
pub fn serialize(&self) -> [u8; constants::SCHNORR_PUBLIC_KEY_SIZE] {
758+
self.0.serialize()
759+
}
707760
}
708761

709762
impl TweakedKeyPair {
@@ -713,11 +766,15 @@ impl TweakedKeyPair {
713766
/// This method is dangerous and can lead to loss of funds if used incorrectly.
714767
/// Specifically, in multi-party protocols a peer can provide a value that allows them to steal.
715768
#[inline]
716-
pub fn dangerous_assume_tweaked(pair: Keypair) -> TweakedKeyPair { TweakedKeyPair(pair) }
769+
pub fn dangerous_assume_tweaked(pair: Keypair) -> TweakedKeyPair {
770+
TweakedKeyPair(pair)
771+
}
717772

718773
/// Returns the underlying key pair.
719774
#[inline]
720-
pub fn to_inner(self) -> Keypair { self.0 }
775+
pub fn to_inner(self) -> Keypair {
776+
self.0
777+
}
721778

722779
/// Returns the [`TweakedPublicKey`] and its [`Parity`] for this [`TweakedKeyPair`].
723780
#[inline]
@@ -729,17 +786,23 @@ impl TweakedKeyPair {
729786

730787
impl From<TweakedPublicKey> for XOnlyPublicKey {
731788
#[inline]
732-
fn from(pair: TweakedPublicKey) -> Self { pair.0 }
789+
fn from(pair: TweakedPublicKey) -> Self {
790+
pair.0
791+
}
733792
}
734793

735794
impl From<TweakedKeyPair> for Keypair {
736795
#[inline]
737-
fn from(pair: TweakedKeyPair) -> Self { pair.0 }
796+
fn from(pair: TweakedKeyPair) -> Self {
797+
pair.0
798+
}
738799
}
739800

740801
impl From<TweakedKeyPair> for TweakedPublicKey {
741802
#[inline]
742-
fn from(pair: TweakedKeyPair) -> Self { TweakedPublicKey::from_keypair(pair) }
803+
fn from(pair: TweakedKeyPair) -> Self {
804+
TweakedPublicKey::from_keypair(pair)
805+
}
743806
}
744807

745808
#[cfg(test)]
@@ -832,7 +895,7 @@ mod tests {
832895
#[cfg(feature = "serde")]
833896
#[test]
834897
fn test_key_serde() {
835-
use serde_test::{Configure, Token, assert_tokens};
898+
use serde_test::{assert_tokens, Configure, Token};
836899

837900
static KEY_WIF: &str = "cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy";
838901
static PK_STR: &str = "039b6347398505f5ec93826dc61c19f47c66c0283ee9be980e29ce325a0f4679ef";
@@ -865,7 +928,10 @@ mod tests {
865928
let s = Secp256k1::new();
866929
let sk = PrivateKey::from_str(KEY_WIF).unwrap();
867930
let pk = PublicKey::from_private_key(&s, &sk);
868-
let pk_u = PublicKey { inner: pk.inner, compressed: false };
931+
let pk_u = PublicKey {
932+
inner: pk.inner,
933+
compressed: false,
934+
};
869935

870936
assert_tokens(&sk, &[Token::BorrowedStr(KEY_WIF)]);
871937
assert_tokens(&pk.compact(), &[Token::BorrowedBytes(&PK_BYTES[..])]);
@@ -929,7 +995,10 @@ mod tests {
929995
"02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8",
930996
)
931997
.unwrap();
932-
let key2 = PublicKey { inner: key1.inner, compressed: false };
998+
let key2 = PublicKey {
999+
inner: key1.inner,
1000+
compressed: false,
1001+
};
9331002
let expected1 = SortKey(
9341003
2,
9351004
<[u8; 32]>::from_hex(

0 commit comments

Comments
 (0)