Skip to content

Commit 1004ba2

Browse files
committed
Implement TryFrom<Key> for ProprietaryKey
TryFrom` became available in Rust 1.34 so we can use it now we have bumped our MSRV. Implement `TryFrom<Key>` for `ProprietaryKey` and deprecate the `from_key` method.
1 parent 7903b19 commit 1004ba2

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

src/util/psbt/map/global.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15+
use core::convert::TryFrom;
16+
1517
use crate::prelude::*;
1618

1719
use crate::io::{self, Cursor, Read};
@@ -191,7 +193,7 @@ impl PartiallySignedTransaction {
191193
return Err(Error::InvalidKey(pair.key).into())
192194
}
193195
}
194-
PSBT_GLOBAL_PROPRIETARY => match proprietary.entry(raw::ProprietaryKey::from_key(pair.key.clone())?) {
196+
PSBT_GLOBAL_PROPRIETARY => match proprietary.entry(raw::ProprietaryKey::try_from(pair.key.clone())?) {
195197
btree_map::Entry::Vacant(empty_key) => {
196198
empty_key.insert(pair.value);
197199
},

src/util/psbt/map/input.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::prelude::*;
1616
use crate::io;
1717
use core::fmt;
1818
use core::str::FromStr;
19+
use core::convert::TryFrom;
1920

2021
use secp256k1;
2122
use crate::blockdata::script::Script;
@@ -356,7 +357,7 @@ impl Input {
356357
}
357358
}
358359
PSBT_IN_PROPRIETARY => {
359-
let key = raw::ProprietaryKey::from_key(raw_key.clone())?;
360+
let key = raw::ProprietaryKey::try_from(raw_key.clone())?;
360361
match self.proprietary.entry(key) {
361362
btree_map::Entry::Vacant(empty_key) => {
362363
empty_key.insert(raw_value);

src/util/psbt/map/output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl Output {
249249
}
250250
}
251251
PSBT_OUT_PROPRIETARY => {
252-
let key = raw::ProprietaryKey::from_key(raw_key.clone())?;
252+
let key = raw::ProprietaryKey::try_from(raw_key.clone())?;
253253
match self.proprietary.entry(key) {
254254
btree_map::Entry::Vacant(empty_key) => {
255255
empty_key.insert(raw_value);

src/util/psbt/raw.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
use crate::prelude::*;
2222
use core::fmt;
23+
use core::convert::TryFrom;
2324

2425
use crate::io;
2526
use crate::consensus::encode::{self, ReadExt, WriteExt, Decodable, Encodable, VarInt, serialize, deserialize, MAX_VEC_SIZE};
@@ -160,12 +161,9 @@ impl<Subtype> Decodable for ProprietaryKey<Subtype> where Subtype: Copy + From<u
160161
impl<Subtype> ProprietaryKey<Subtype> where Subtype: Copy + From<u8> + Into<u8> {
161162
/// Constructs [ProprietaryKey] from [Key]; returns
162163
/// [Error::InvalidProprietaryKey] if `key` do not starts with 0xFC byte
164+
#[deprecated(since = "0.29.0", note = "use try_from instead")]
163165
pub fn from_key(key: Key) -> Result<Self, Error> {
164-
if key.type_value != 0xFC {
165-
return Err(Error::InvalidProprietaryKey)
166-
}
167-
168-
Ok(deserialize(&key.key)?)
166+
Self::try_from(key)
169167
}
170168

171169
/// Constructs full [Key] corresponding to this proprietary key type
@@ -176,3 +174,21 @@ impl<Subtype> ProprietaryKey<Subtype> where Subtype: Copy + From<u8> + Into<u8>
176174
}
177175
}
178176
}
177+
178+
impl<Subtype> TryFrom<Key> for ProprietaryKey<Subtype>
179+
where
180+
Subtype:Copy + From<u8> + Into<u8> {
181+
type Error = Error;
182+
183+
/// Constructs a [`ProprietaryKey`] from a [`Key`].
184+
///
185+
/// # Errors
186+
/// Returns [`Error::InvalidProprietaryKey`] if `key` does not start with `0xFC` byte.
187+
fn try_from(key: Key) -> Result<Self, Self::Error> {
188+
if key.type_value != 0xFC {
189+
return Err(Error::InvalidProprietaryKey)
190+
}
191+
192+
Ok(deserialize(&key.key)?)
193+
}
194+
}

0 commit comments

Comments
 (0)