From 6b7b440cff1b48c1dbcd5d9acd70eccef51d631b Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 25 May 2022 16:58:07 +1000 Subject: [PATCH] Implement TryFrom for ProprietaryKey TryFrom` became available in Rust 1.34 so we can use it now we have bumped our MSRV. Implement `TryFrom` for `ProprietaryKey` and deprecate the `from_key` method. --- src/util/psbt/map/global.rs | 4 +++- src/util/psbt/map/input.rs | 3 ++- src/util/psbt/map/output.rs | 2 +- src/util/psbt/raw.rs | 26 +++++++++++++++++++++----- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/util/psbt/map/global.rs b/src/util/psbt/map/global.rs index 77288ce1..3c6b178e 100644 --- a/src/util/psbt/map/global.rs +++ b/src/util/psbt/map/global.rs @@ -12,6 +12,8 @@ // If not, see . // +use core::convert::TryFrom; + use crate::prelude::*; use crate::io::{self, Cursor, Read}; @@ -191,7 +193,7 @@ impl PartiallySignedTransaction { return Err(Error::InvalidKey(pair.key).into()) } } - PSBT_GLOBAL_PROPRIETARY => match proprietary.entry(raw::ProprietaryKey::from_key(pair.key.clone())?) { + PSBT_GLOBAL_PROPRIETARY => match proprietary.entry(raw::ProprietaryKey::try_from(pair.key.clone())?) { btree_map::Entry::Vacant(empty_key) => { empty_key.insert(pair.value); }, diff --git a/src/util/psbt/map/input.rs b/src/util/psbt/map/input.rs index 9f8b38f9..28ee2114 100644 --- a/src/util/psbt/map/input.rs +++ b/src/util/psbt/map/input.rs @@ -16,6 +16,7 @@ use crate::prelude::*; use crate::io; use core::fmt; use core::str::FromStr; +use core::convert::TryFrom; use secp256k1; use crate::blockdata::script::Script; @@ -356,7 +357,7 @@ impl Input { } } PSBT_IN_PROPRIETARY => { - let key = raw::ProprietaryKey::from_key(raw_key.clone())?; + let key = raw::ProprietaryKey::try_from(raw_key.clone())?; match self.proprietary.entry(key) { btree_map::Entry::Vacant(empty_key) => { empty_key.insert(raw_value); diff --git a/src/util/psbt/map/output.rs b/src/util/psbt/map/output.rs index 33a6824a..3c8510e9 100644 --- a/src/util/psbt/map/output.rs +++ b/src/util/psbt/map/output.rs @@ -249,7 +249,7 @@ impl Output { } } PSBT_OUT_PROPRIETARY => { - let key = raw::ProprietaryKey::from_key(raw_key.clone())?; + let key = raw::ProprietaryKey::try_from(raw_key.clone())?; match self.proprietary.entry(key) { btree_map::Entry::Vacant(empty_key) => { empty_key.insert(raw_value); diff --git a/src/util/psbt/raw.rs b/src/util/psbt/raw.rs index 64522b1e..d5fd663d 100644 --- a/src/util/psbt/raw.rs +++ b/src/util/psbt/raw.rs @@ -20,6 +20,7 @@ use crate::prelude::*; use core::fmt; +use core::convert::TryFrom; use crate::io; use crate::consensus::encode::{self, ReadExt, WriteExt, Decodable, Encodable, VarInt, serialize, deserialize, MAX_VEC_SIZE}; @@ -160,12 +161,9 @@ impl Decodable for ProprietaryKey where Subtype: Copy + From ProprietaryKey where Subtype: Copy + From + Into { /// Constructs [ProprietaryKey] from [Key]; returns /// [Error::InvalidProprietaryKey] if `key` do not starts with 0xFC byte + #[deprecated(since = "0.29.0", note = "use try_from instead")] pub fn from_key(key: Key) -> Result { - if key.type_value != 0xFC { - return Err(Error::InvalidProprietaryKey) - } - - Ok(deserialize(&key.key)?) + Self::try_from(key) } /// Constructs full [Key] corresponding to this proprietary key type @@ -176,3 +174,21 @@ impl ProprietaryKey where Subtype: Copy + From + Into } } } + +impl TryFrom for ProprietaryKey +where + Subtype:Copy + From + Into { + type Error = Error; + + /// Constructs a [`ProprietaryKey`] from a [`Key`]. + /// + /// # Errors + /// Returns [`Error::InvalidProprietaryKey`] if `key` does not start with `0xFC` byte. + fn try_from(key: Key) -> Result { + if key.type_value != 0xFC { + return Err(Error::InvalidProprietaryKey) + } + + Ok(deserialize(&key.key)?) + } +}