use borsh::BorshSerialize;
use pyde_crypto::poseidon2::poseidon2_hash;
use crate::error::SdkError;
use crate::types::{Address, Event};
pub const CHILD_ADDRESS_PREFIX: &[u8; 11] = b"pyde-child:";
pub const CHILD_PREIMAGE_LEN: usize = 107;
#[must_use]
pub fn child_preimage(
parent: &Address,
template: &Address,
salt: &[u8; 32],
) -> [u8; CHILD_PREIMAGE_LEN] {
let mut preimage = [0u8; CHILD_PREIMAGE_LEN];
preimage[..11].copy_from_slice(CHILD_ADDRESS_PREFIX);
preimage[11..43].copy_from_slice(parent.as_bytes());
preimage[43..75].copy_from_slice(template.as_bytes());
preimage[75..107].copy_from_slice(salt);
preimage
}
#[must_use]
pub fn child_address(parent: &Address, template: &Address, salt: &[u8; 32]) -> Address {
let bytes: [u8; 32] = poseidon2_hash(&child_preimage(parent, template, salt)).into();
Address::new(bytes)
}
pub struct Salt;
impl Salt {
#[must_use]
#[allow(clippy::expect_used)]
pub fn of<T: BorshSerialize>(value: &T) -> [u8; 32] {
let bytes = borsh::to_vec(value).expect("borsh encode into Vec cannot fail");
poseidon2_hash(&bytes).into()
}
#[must_use]
pub fn of_unordered_pair(a: &Address, b: &Address) -> [u8; 32] {
let (lo, hi) = if a.as_bytes() <= b.as_bytes() {
(a, b)
} else {
(b, a)
};
let mut buf = [0u8; 64];
buf[..32].copy_from_slice(lo.as_bytes());
buf[32..].copy_from_slice(hi.as_bytes());
poseidon2_hash(&buf).into()
}
}
pub const INSTANTIATED_TOPIC: [u8; 32] = [
0x62, 0x2a, 0x0a, 0x9e, 0x1e, 0x2b, 0x48, 0x72, 0x88, 0x90, 0x4a, 0x22, 0xb1, 0x81, 0x74, 0xd6,
0xe4, 0x5b, 0x37, 0x49, 0xc7, 0x56, 0xa9, 0x42, 0x09, 0xef, 0x9a, 0x9c, 0xf7, 0x68, 0x84, 0x7a,
];
const INSTANTIATED_DATA_LEN: usize = 80;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Instantiated {
pub parent: Address,
pub template: Address,
pub salt: [u8; 32],
pub child: Address,
pub value: u128,
}
impl Instantiated {
pub fn decode(event: &Event) -> Result<Self, SdkError> {
if event.topics.len() != 3 {
return Err(SdkError::InvalidResponse(format!(
"Instantiated expects 3 topics; got {}",
event.topics.len()
)));
}
let topic0 = decode_topic(&event.topics[0])?;
if topic0 != INSTANTIATED_TOPIC {
return Err(SdkError::InvalidResponse(format!(
"topic 0 is not Blake3(\"pyde.Instantiated\"): 0x{}",
hex::encode(topic0)
)));
}
let child = Address::new(decode_topic(&event.topics[1])?);
let template = Address::new(decode_topic(&event.topics[2])?);
let data = hex::decode(event.data.trim_start_matches("0x"))
.map_err(|e| SdkError::InvalidResponse(format!("Instantiated data hex: {e}")))?;
if data.len() != INSTANTIATED_DATA_LEN {
return Err(SdkError::InvalidResponse(format!(
"Instantiated data must be {INSTANTIATED_DATA_LEN} bytes; got {}",
data.len()
)));
}
let mut parent = [0u8; 32];
parent.copy_from_slice(&data[..32]);
let parent = Address::new(parent);
let mut salt = [0u8; 32];
salt.copy_from_slice(&data[32..64]);
let mut value_le = [0u8; 16];
value_le.copy_from_slice(&data[64..80]);
let value = u128::from_le_bytes(value_le);
let emitter = Address::from_hex(&event.contract_addr)?;
if emitter != parent {
return Err(SdkError::InvalidResponse(format!(
"Instantiated emitter {emitter} does not match parent {parent}"
)));
}
Ok(Self {
parent,
template,
salt,
child,
value,
})
}
}
impl TryFrom<&Event> for Instantiated {
type Error = SdkError;
fn try_from(event: &Event) -> Result<Self, Self::Error> {
Self::decode(event)
}
}
fn decode_topic(topic: &str) -> Result<[u8; 32], SdkError> {
let bytes = hex::decode(topic.trim_start_matches("0x"))
.map_err(|e| SdkError::InvalidResponse(format!("topic hex: {e}")))?;
let arr: [u8; 32] = bytes.try_into().map_err(|b: Vec<u8>| {
SdkError::InvalidResponse(format!("topic must be 32 bytes; got {}", b.len()))
})?;
Ok(arr)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn child_address_kat_pins_derivation() {
let parent = Address::new([0x11; 32]);
let template = Address::new([0x22; 32]);
let salt = [0x33; 32];
assert_eq!(
child_address(&parent, &template, &salt).to_hex(),
"0x354ab9a58e3fb76b484390a2ef277594042e12fd0b74343e5bf34dba492f3dfe"
);
}
#[test]
fn preimage_layout_is_fixed_width() {
let parent = Address::new([0xAA; 32]);
let template = Address::new([0xBB; 32]);
let salt = [0xCC; 32];
let preimage = child_preimage(&parent, &template, &salt);
assert_eq!(
&preimage[..11],
&[0x70, 0x79, 0x64, 0x65, 0x2D, 0x63, 0x68, 0x69, 0x6C, 0x64, 0x3A]
);
assert_eq!(&preimage[11..43], &[0xAA; 32]);
assert_eq!(&preimage[43..75], &[0xBB; 32]);
assert_eq!(&preimage[75..107], &[0xCC; 32]);
}
#[test]
fn child_address_hashes_the_preimage() {
let parent = Address::new([0x01; 32]);
let template = Address::new([0x02; 32]);
let salt = [0x03; 32];
let via_preimage: [u8; 32] =
poseidon2_hash(&child_preimage(&parent, &template, &salt)).into();
assert_eq!(child_address(&parent, &template, &salt).0, via_preimage);
}
#[test]
fn child_address_distinguishes_each_input() {
let p = Address::new([0x01; 32]);
let t = Address::new([0x02; 32]);
let s = [0x03; 32];
let base = child_address(&p, &t, &s);
assert_ne!(base, child_address(&Address::new([0x04; 32]), &t, &s));
assert_ne!(base, child_address(&p, &Address::new([0x04; 32]), &s));
assert_ne!(base, child_address(&p, &t, &[0x04; 32]));
assert_ne!(base, child_address(&t, &p, &s));
}
#[test]
fn salt_of_is_deterministic_and_typed() {
assert_eq!(Salt::of(&7u64), Salt::of(&7u64));
assert_ne!(Salt::of(&7u64), Salt::of(&8u64));
assert_ne!(Salt::of(&7u64), Salt::of(&7u32));
}
#[test]
fn salt_of_unordered_pair_is_order_independent() {
let a = Address::new([0x0A; 32]);
let b = Address::new([0x0B; 32]);
assert_eq!(
Salt::of_unordered_pair(&a, &b),
Salt::of_unordered_pair(&b, &a)
);
assert_eq!(
Salt::of_unordered_pair(&a, &a),
Salt::of_unordered_pair(&a, &a)
);
assert_ne!(
Salt::of_unordered_pair(&a, &b),
Salt::of_unordered_pair(&a, &a)
);
}
#[test]
fn salt_of_unordered_pair_sorts_unsigned_at_sign_boundary() {
let lo = Address::new([0x7F; 32]);
let hi = Address::new([0x80; 32]);
let mut sorted = [0u8; 64];
sorted[..32].copy_from_slice(&[0x7F; 32]);
sorted[32..].copy_from_slice(&[0x80; 32]);
let expected: [u8; 32] = poseidon2_hash(&sorted).into();
assert_eq!(Salt::of_unordered_pair(&hi, &lo), expected);
assert_eq!(Salt::of_unordered_pair(&lo, &hi), expected);
}
#[test]
fn instantiated_topic_is_pinned() {
assert_eq!(
INSTANTIATED_TOPIC,
*blake3::hash(b"pyde.Instantiated").as_bytes()
);
}
fn sample_event() -> (Event, Instantiated) {
let parent = Address::new([0xA1; 32]);
let template = Address::new([0xB2; 32]);
let salt = [0xC3; 32];
let child = child_address(&parent, &template, &salt);
let value: u128 = 1_500_000_000;
let mut data = Vec::with_capacity(80);
data.extend_from_slice(parent.as_bytes());
data.extend_from_slice(&salt);
data.extend_from_slice(&value.to_le_bytes());
let event = Event {
wave_id: "0x2a".into(),
tx_index: "0x0".into(),
event_index: "0x0".into(),
contract_addr: parent.to_hex(),
topics: vec![
format!("0x{}", hex::encode(INSTANTIATED_TOPIC)),
child.to_hex(),
template.to_hex(),
],
data: format!("0x{}", hex::encode(&data)),
};
let expected = Instantiated {
parent,
template,
salt,
child,
value,
};
(event, expected)
}
#[test]
fn decode_round_trips_every_field() {
let (event, expected) = sample_event();
let decoded = Instantiated::decode(&event).unwrap();
assert_eq!(decoded, expected);
assert_eq!(
child_address(&decoded.parent, &decoded.template, &decoded.salt),
decoded.child
);
let via_try: Instantiated = (&event).try_into().unwrap();
assert_eq!(via_try, expected);
}
#[test]
fn decode_rejects_wrong_topic0() {
let (mut event, _) = sample_event();
event.topics[0] = format!("0x{}", hex::encode([0x99u8; 32]));
let err = Instantiated::decode(&event).unwrap_err();
assert!(matches!(err, SdkError::InvalidResponse(_)));
assert!(err.to_string().contains("topic 0"));
}
#[test]
fn decode_rejects_wrong_topic_count() {
let (mut event, _) = sample_event();
event.topics.pop();
assert!(matches!(
Instantiated::decode(&event),
Err(SdkError::InvalidResponse(_))
));
let (mut event, _) = sample_event();
event.topics.push(format!("0x{}", hex::encode([0u8; 32])));
assert!(matches!(
Instantiated::decode(&event),
Err(SdkError::InvalidResponse(_))
));
let (mut event, _) = sample_event();
event.topics.clear();
assert!(matches!(
Instantiated::decode(&event),
Err(SdkError::InvalidResponse(_))
));
}
#[test]
fn decode_rejects_short_data() {
let (mut event, _) = sample_event();
let bytes = hex::decode(event.data.trim_start_matches("0x")).unwrap();
event.data = format!("0x{}", hex::encode(&bytes[..79]));
let err = Instantiated::decode(&event).unwrap_err();
assert!(err.to_string().contains("80 bytes"));
}
#[test]
fn decode_rejects_oversized_data() {
let (mut event, _) = sample_event();
event.data.push_str("00");
assert!(matches!(
Instantiated::decode(&event),
Err(SdkError::InvalidResponse(_))
));
}
#[test]
fn decode_rejects_non_32_byte_topic() {
let (mut event, _) = sample_event();
event.topics[1] = "0xabcd".into();
let err = Instantiated::decode(&event).unwrap_err();
assert!(err.to_string().contains("32 bytes"));
}
#[test]
fn decode_rejects_emitter_parent_mismatch() {
let (mut event, _) = sample_event();
event.contract_addr = Address::new([0xEE; 32]).to_hex();
let err = Instantiated::decode(&event).unwrap_err();
assert!(err.to_string().contains("does not match parent"));
}
}