Skip to content

Commit e89d43a

Browse files
committed
add testing helpers functions
1 parent 1ae51ac commit e89d43a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

clarity-serialization/src/tests/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
#![cfg(any(test, feature = "testing"))]
1818

19+
use stacks_common::address::{AddressHashMode, C32_ADDRESS_VERSION_TESTNET_SINGLESIG};
20+
use stacks_common::types::chainstate::{StacksAddress, StacksPrivateKey, StacksPublicKey};
21+
1922
use crate::errors::CodecError;
20-
use crate::types::Value;
23+
use crate::types::{PrincipalData, StandardPrincipalData, Value};
2124

2225
impl Value {
2326
pub fn list_from(list_data: Vec<Value>) -> Result<Value, CodecError> {
@@ -33,3 +36,28 @@ impl PartialEq for CodecError {
3336
self.to_string() == other.to_string()
3437
}
3538
}
39+
40+
impl From<&StacksPrivateKey> for StandardPrincipalData {
41+
fn from(o: &StacksPrivateKey) -> StandardPrincipalData {
42+
let stacks_addr = StacksAddress::from_public_keys(
43+
C32_ADDRESS_VERSION_TESTNET_SINGLESIG,
44+
&AddressHashMode::SerializeP2PKH,
45+
1,
46+
&vec![StacksPublicKey::from_private(o)],
47+
)
48+
.unwrap();
49+
StandardPrincipalData::from(stacks_addr)
50+
}
51+
}
52+
53+
impl From<&StacksPrivateKey> for PrincipalData {
54+
fn from(o: &StacksPrivateKey) -> PrincipalData {
55+
PrincipalData::Standard(StandardPrincipalData::from(o))
56+
}
57+
}
58+
59+
impl From<&StacksPrivateKey> for Value {
60+
fn from(o: &StacksPrivateKey) -> Value {
61+
Value::from(StandardPrincipalData::from(o))
62+
}
63+
}

clarity-serialization/src/types/serialization.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,14 @@ impl Value {
10991099

11001100
/// Try to deserialize a value without type information. This *does not* perform sanitization
11011101
/// so it should not be used when decoding clarity database values.
1102+
#[cfg(any(test, feature = "testing"))]
1103+
pub fn try_deserialize_bytes_untyped(bytes: &Vec<u8>) -> Result<Value, CodecError> {
1104+
Value::deserialize_read(&mut bytes.as_slice(), None, false)
1105+
}
1106+
1107+
/// Try to deserialize a value without type information. This *does not* perform sanitization
1108+
/// so it should not be used when decoding clarity database values.
1109+
#[cfg(not(any(test, feature = "testing")))]
11021110
fn try_deserialize_bytes_untyped(bytes: &Vec<u8>) -> Result<Value, CodecError> {
11031111
Value::deserialize_read(&mut bytes.as_slice(), None, false)
11041112
}

0 commit comments

Comments
 (0)