fix : adds the arrayvec dependency

This commit adds the arrayvec dependency to the sortKey.
This commit is contained in:
harshit933 2023-12-14 17:04:22 +05:30
parent 37a6b48933
commit 8783d526bd
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 {
@ -1128,25 +1123,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);
} }