Merge rust-bitcoin/rust-bitcoin#2286: fix : adds the arrayvec dependency

8783d526bd fix : adds the arrayvec dependency (harshit933)

Pull request description:

  This commit adds the arrayvec dependency to the sortKey.

  Potential fix #2276

ACKs for top commit:
  Kixunil:
    ACK 8783d526bd
  apoelstra:
    ACK 8783d526bd

Tree-SHA512: 35f28ade02dd526ce5dfa2f42578b36cd5af29a5a9f409da70a775bc12046674737e9bce9fabcc87f1b4669080ad10465c75601342f280c11eab11f791f44c36
This commit is contained in:
Andrew Poelstra 2023-12-15 21:52:57 +00:00
commit 9f68e6a6b5
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 14 additions and 27 deletions

View File

@ -13,6 +13,7 @@ use hashes::{hash160, Hash};
use hex::FromHex; use hex::FromHex;
use internals::write_err; use internals::write_err;
use io::{Read, Write}; use io::{Read, Write};
use internals::array_vec::ArrayVec;
use crate::crypto::ecdsa; use crate::crypto::ecdsa;
use crate::internal_macros::impl_asref_push_bytes; use crate::internal_macros::impl_asref_push_bytes;
@ -161,17 +162,11 @@ impl PublicKey {
/// ``` /// ```
pub fn to_sort_key(self) -> SortKey { pub fn to_sort_key(self) -> SortKey {
if self.compressed { if self.compressed {
let bytes = self.inner.serialize(); let buf = ArrayVec::from_slice(&self.inner.serialize());
let mut res = [0; 32]; SortKey(buf)
res[..].copy_from_slice(&bytes[1..33]);
SortKey(bytes[0], res, [0; 32])
} else { } else {
let bytes = self.inner.serialize_uncompressed(); let buf = ArrayVec::from_slice(&self.inner.serialize_uncompressed());
let mut res_left = [0; 32]; SortKey(buf)
let mut res_right = [0; 32];
res_left[..].copy_from_slice(&bytes[1..33]);
res_right[..].copy_from_slice(&bytes[33..65]);
SortKey(bytes[0], res_left, res_right)
} }
} }
@ -221,7 +216,7 @@ impl From<PublicKey> for XOnlyPublicKey {
/// An opaque return type for PublicKey::to_sort_key /// An opaque return type for PublicKey::to_sort_key
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct SortKey(u8, [u8; 32], [u8; 32]); pub struct SortKey(ArrayVec<u8, 65>);
impl fmt::Display for PublicKey { impl fmt::Display for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -1130,25 +1125,17 @@ mod tests {
) )
.unwrap(); .unwrap();
let key2 = PublicKey { inner: key1.inner, compressed: false }; let key2 = PublicKey { inner: key1.inner, compressed: false };
let expected1 = SortKey( let arrayvec1 = ArrayVec::from_slice(
2, &<[u8; 33]>::from_hex(
<[u8; 32]>::from_hex( "02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8",
"ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8",
)
.unwrap(),
[0_u8; 32],
);
let expected2 = SortKey(
4,
<[u8; 32]>::from_hex(
"ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8",
)
.unwrap(),
<[u8; 32]>::from_hex(
"1794e7f3d5e420641a3bc690067df5541470c966cbca8c694bf39aa16d836918",
) )
.unwrap(), .unwrap(),
); );
let expected1 = SortKey(arrayvec1);
let arrayvec2 = ArrayVec::from_slice(&<[u8; 65]>::from_hex(
"04ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f81794e7f3d5e420641a3bc690067df5541470c966cbca8c694bf39aa16d836918",
).unwrap());
let expected2 = SortKey(arrayvec2);
assert_eq!(key1.to_sort_key(), expected1); assert_eq!(key1.to_sort_key(), expected1);
assert_eq!(key2.to_sort_key(), expected2); assert_eq!(key2.to_sort_key(), expected2);
} }