From 459059622f1ced6c0efb1384d6ac65bea68f275f Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Mon, 4 Mar 2019 20:45:44 +0000 Subject: [PATCH] util::key: Provide to_bytes() methods for key types These are mainly utility methods around the existing way to serialize the key types. --- src/util/key.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/util/key.rs b/src/util/key.rs index a3adc91..9c4b9d4 100644 --- a/src/util/key.rs +++ b/src/util/key.rs @@ -44,6 +44,13 @@ impl PublicKey { debug_assert!(write_res.is_ok()); } + /// Serialize the public key to bytes + pub fn to_bytes(&self) -> Vec { + let mut buf = Vec::new(); + self.write_into(&mut buf); + buf + } + /// Deserialize a public key from a slice pub fn from_slice(data: &[u8]) -> Result { let compressed: bool = match data.len() { @@ -110,6 +117,11 @@ impl PrivateKey { } } + /// Serialize the private key to bytes + pub fn to_bytes(&self) -> Vec { + self.key[..].to_vec() + } + /// Format the private key to WIF format. pub fn fmt_wif(&self, fmt: &mut fmt::Write) -> fmt::Result { let mut ret = [0; 34];