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.
This commit is contained in:
Tobin C. Harding 2022-05-25 16:58:07 +10:00
parent 5c49fe775f
commit 6b7b440cff
4 changed files with 27 additions and 8 deletions

View File

@ -12,6 +12,8 @@
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. // If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// //
use core::convert::TryFrom;
use crate::prelude::*; use crate::prelude::*;
use crate::io::{self, Cursor, Read}; use crate::io::{self, Cursor, Read};
@ -191,7 +193,7 @@ impl PartiallySignedTransaction {
return Err(Error::InvalidKey(pair.key).into()) 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) => { btree_map::Entry::Vacant(empty_key) => {
empty_key.insert(pair.value); empty_key.insert(pair.value);
}, },

View File

@ -16,6 +16,7 @@ use crate::prelude::*;
use crate::io; use crate::io;
use core::fmt; use core::fmt;
use core::str::FromStr; use core::str::FromStr;
use core::convert::TryFrom;
use secp256k1; use secp256k1;
use crate::blockdata::script::Script; use crate::blockdata::script::Script;
@ -356,7 +357,7 @@ impl Input {
} }
} }
PSBT_IN_PROPRIETARY => { 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) { match self.proprietary.entry(key) {
btree_map::Entry::Vacant(empty_key) => { btree_map::Entry::Vacant(empty_key) => {
empty_key.insert(raw_value); empty_key.insert(raw_value);

View File

@ -249,7 +249,7 @@ impl Output {
} }
} }
PSBT_OUT_PROPRIETARY => { 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) { match self.proprietary.entry(key) {
btree_map::Entry::Vacant(empty_key) => { btree_map::Entry::Vacant(empty_key) => {
empty_key.insert(raw_value); empty_key.insert(raw_value);

View File

@ -20,6 +20,7 @@
use crate::prelude::*; use crate::prelude::*;
use core::fmt; use core::fmt;
use core::convert::TryFrom;
use crate::io; use crate::io;
use crate::consensus::encode::{self, ReadExt, WriteExt, Decodable, Encodable, VarInt, serialize, deserialize, MAX_VEC_SIZE}; 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
impl<Subtype> ProprietaryKey<Subtype> where Subtype: Copy + From<u8> + Into<u8> { impl<Subtype> ProprietaryKey<Subtype> where Subtype: Copy + From<u8> + Into<u8> {
/// Constructs [ProprietaryKey] from [Key]; returns /// Constructs [ProprietaryKey] from [Key]; returns
/// [Error::InvalidProprietaryKey] if `key` do not starts with 0xFC byte /// [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<Self, Error> { pub fn from_key(key: Key) -> Result<Self, Error> {
if key.type_value != 0xFC { Self::try_from(key)
return Err(Error::InvalidProprietaryKey)
}
Ok(deserialize(&key.key)?)
} }
/// Constructs full [Key] corresponding to this proprietary key type /// Constructs full [Key] corresponding to this proprietary key type
@ -176,3 +174,21 @@ impl<Subtype> ProprietaryKey<Subtype> where Subtype: Copy + From<u8> + Into<u8>
} }
} }
} }
impl<Subtype> TryFrom<Key> for ProprietaryKey<Subtype>
where
Subtype:Copy + From<u8> + Into<u8> {
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<Self, Self::Error> {
if key.type_value != 0xFC {
return Err(Error::InvalidProprietaryKey)
}
Ok(deserialize(&key.key)?)
}
}