From a51768af3f3d4c8e138e1ded250800810bedc903 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 31 Oct 2024 14:52:16 +1100 Subject: [PATCH] key: Deprecate to_bytes As we have been doing in other places deprecate `to_bytes` in favour of `to_vec`. Note this is only for functions that return a `Vec`, the `key` module has `CompressedKey::to_bytes` still as it returns an array. --- bitcoin/src/blockdata/script/tests.rs | 4 ++-- bitcoin/src/crypto/key.rs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs index 693f36e2b..5a9878a59 100644 --- a/bitcoin/src/blockdata/script/tests.rs +++ b/bitcoin/src/blockdata/script/tests.rs @@ -54,7 +54,7 @@ fn p2pk_pubkey_bytes_valid_key_and_valid_script_returns_expected_key() { let key = key_str.parse::().unwrap(); let p2pk = Script::builder().push_key(key).push_opcode(OP_CHECKSIG).into_script(); let actual = p2pk.p2pk_pubkey_bytes().unwrap(); - assert_eq!(actual.to_vec(), key.to_bytes()); + assert_eq!(actual.to_vec(), key.to_vec()); } #[test] @@ -109,7 +109,7 @@ fn p2pk_pubkey_bytes_compressed_key_returns_expected_key() { let key = compressed_key_str.parse::().unwrap(); let p2pk = Script::builder().push_key(key).push_opcode(OP_CHECKSIG).into_script(); let actual = p2pk.p2pk_pubkey_bytes().unwrap(); - assert_eq!(actual.to_vec(), key.to_bytes()); + assert_eq!(actual.to_vec(), key.to_vec()); } #[test] diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 931fc8808..0bac43000 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -110,7 +110,11 @@ impl PublicKey { } /// Serializes the public key to bytes. - pub fn to_bytes(self) -> Vec { + #[deprecated(since = "TBD", note = "use to_vec instead")] + pub fn to_bytes(self) -> Vec { self.to_vec() } + + /// Serializes the public key to bytes. + pub fn to_vec(self) -> Vec { let mut buf = Vec::new(); self.write_into(&mut buf).expect("vecs don't error"); buf @@ -442,8 +446,13 @@ impl PrivateKey { } } + /// Serializes the private key to bytes. - pub fn to_bytes(self) -> Vec { self.inner[..].to_vec() } + #[deprecated(since = "TBD", note = "use to_vec instead")] + pub fn to_bytes(self) -> Vec { self.to_vec() } + + /// Serializes the private key to bytes. + pub fn to_vec(self) -> Vec { self.inner[..].to_vec() } /// Deserializes a private key from a slice. pub fn from_slice(