16
16
17
17
use :: core:: fmt;
18
18
use :: { SecretKey , KeyPair , to_hex} ;
19
+ use ecdh:: SharedSecret ;
19
20
use constants:: SECRET_KEY_SIZE ;
20
21
21
22
macro_rules! impl_display_secret {
@@ -113,28 +114,26 @@ impl SecretKey {
113
114
/// little-endian hexadecimal string using the provided formatter.
114
115
///
115
116
/// This is the only method that outputs the actual secret key value, and, thus,
116
- /// should be used with extreme precaution .
117
+ /// should be used with extreme caution .
117
118
///
118
- /// # Example
119
+ /// # Examples
119
120
///
120
121
/// ```
121
122
/// # #[cfg(all(feature = "std", not(feature = "bitcoin_hashes")))] {
122
- /// use secp256k1::ONE_KEY;
123
- /// let key = ONE_KEY;
124
- /// // Normal display hides value
125
- /// assert_eq!(
126
- /// "SecretKey(#2518682f7819fb2d)",
127
- /// format!("{:?}", key)
128
- /// );
123
+ /// let key = secp256k1::ONE_KEY;
124
+ ///
125
+ /// // Normal display hides value.
126
+ /// assert_eq!("SecretKey(#2518682f7819fb2d)", format!("{:?}", key));
127
+ ///
129
128
/// // Here we explicitly display the secret value:
130
129
/// assert_eq!(
131
130
/// "0000000000000000000000000000000000000000000000000000000000000001",
132
131
/// format!("{}", key.display_secret())
133
132
/// );
133
+ /// // Also, we can explicitly display with `Debug`:
134
134
/// assert_eq!(
135
- /// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")",
136
- /// format!("{:?}", key.display_secret())
137
- /// );
135
+ /// format!("{:?}", key.display_secret()),
136
+ /// format!("DisplaySecret(\"{}\")", key.display_secret()));
138
137
/// # }
139
138
/// ```
140
139
#[ inline]
@@ -172,6 +171,7 @@ impl KeyPair {
172
171
/// "0000000000000000000000000000000000000000000000000000000000000001",
173
172
/// format!("{}", key.display_secret())
174
173
/// );
174
+ /// // Also, we can explicitly display with `Debug`:
175
175
/// assert_eq!(
176
176
/// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")",
177
177
/// format!("{:?}", key.display_secret())
@@ -183,3 +183,42 @@ impl KeyPair {
183
183
DisplaySecret { secret : self . serialize_secret ( ) }
184
184
}
185
185
}
186
+
187
+ impl SharedSecret {
188
+ /// Formats the explicit byte value of the shared secret kept inside the type as a
189
+ /// little-endian hexadecimal string using the provided formatter.
190
+ ///
191
+ /// This is the only method that outputs the actual shared secret value, and, thus,
192
+ /// should be used with extreme caution.
193
+ ///
194
+ /// # Examples
195
+ ///
196
+ /// ```
197
+ /// # #[cfg(all(feature = "std", not(feature = "bitcoin_hashes")))] {
198
+ /// # use std::str::FromStr;
199
+ /// # use secp256k1::{SecretKey, PublicKey};
200
+ /// use secp256k1::ecdh::SharedSecret;
201
+ ///
202
+ /// # let pk = PublicKey::from_slice(&[3, 23, 183, 225, 206, 31, 159, 148, 195, 42, 67, 115, 146, 41, 248, 140, 11, 3, 51, 41, 111, 180, 110, 143, 114, 134, 88, 73, 198, 174, 52, 184, 78]).expect("hard coded slice should parse correctly");
203
+ /// # let sk = SecretKey::from_str("57f0148f94d13095cfda539d0da0d1541304b678d8b36e243980aab4e1b7cead").unwrap();
204
+ ///
205
+ /// let secret = SharedSecret::new(&pk, &sk);
206
+ /// // Normal display hides the value.
207
+ /// assert_eq!(format!("{:?}", secret), "SharedSecret(#2f7b793c9a1de4bf)");
208
+ ///
209
+ /// // Here we explicitly display the secret value:
210
+ /// assert_eq!(
211
+ /// format!("{}", secret.display_secret()),
212
+ /// "cf05ae7da039ddce6d56dd57d3000c6dd91c6f1695eae47e05389f11e2467043"
213
+ /// );
214
+ /// // Also, we can explicitly display with `Debug`:
215
+ /// assert_eq!(
216
+ /// format!("{:?}", secret.display_secret()),
217
+ /// format!("DisplaySecret(\"{}\")", secret.display_secret()));
218
+ /// # }
219
+ /// ```
220
+ #[ inline]
221
+ pub fn display_secret ( & self ) -> DisplaySecret {
222
+ DisplaySecret { secret : self . serialize_secret ( ) }
223
+ }
224
+ }
0 commit comments