diff --git a/rusk-abi/src/abi.rs b/rusk-abi/src/abi.rs index 91bfa0f3e4..1d60a35095 100644 --- a/rusk-abi/src/abi.rs +++ b/rusk-abi/src/abi.rs @@ -8,8 +8,6 @@ use dusk_bytes::Serializable; #[cfg(feature = "abi")] use phoenix_core::PublicKey; -#[cfg(feature = "abi")] -use rkyv::{archived_root, Deserialize, Infallible}; pub use piecrust_uplink::*; @@ -79,20 +77,21 @@ pub fn payment_info( } /// Query owner of a given contract. +/// Returns none if contract is not found. +/// Panics if owner is not a valid public key (should never happen). #[cfg(feature = "abi")] pub fn owner(contract: ContractId) -> Option { owner_raw(contract).map(|buf| { - let ret = unsafe { archived_root::(buf.as_slice()) }; - ret.deserialize(&mut Infallible).expect("Infallible") + PublicKey::from_bytes(&buf).expect("Owner should deserialize correctly") }) } -/// Query owner of a given contract. +/// Query self owner of a given contract. +/// Panics if owner is not a valid public key (should never happen). #[cfg(feature = "abi")] pub fn self_owner() -> PublicKey { let buf = self_owner_raw(); - let ret = unsafe { archived_root::(buf.as_slice()) }; - ret.deserialize(&mut Infallible).expect("Infallible") + PublicKey::from_bytes(&buf).expect("Owner should deserialize correctly") } /// Query raw owner of a given contract. diff --git a/rusk-abi/tests/contracts/host_fn/src/lib.rs b/rusk-abi/tests/contracts/host_fn/src/lib.rs index f6447c0a78..f4e1254f59 100644 --- a/rusk-abi/tests/contracts/host_fn/src/lib.rs +++ b/rusk-abi/tests/contracts/host_fn/src/lib.rs @@ -68,7 +68,11 @@ impl HostFnTest { rusk_abi::block_height() } - pub fn owner(&self) -> [u8; PublicKey::SIZE] { + pub fn owner(&self) -> PublicKey { + rusk_abi::self_owner() + } + + pub fn owner_raw(&self) -> [u8; PublicKey::SIZE] { rusk_abi::self_owner_raw() } } @@ -114,6 +118,11 @@ unsafe fn contract_owner(arg_len: u32) -> u32 { rusk_abi::wrap_call(arg_len, |_: ()| STATE.owner()) } +#[no_mangle] +unsafe fn contract_owner_raw(arg_len: u32) -> u32 { + rusk_abi::wrap_call(arg_len, |_: ()| STATE.owner_raw()) +} + const PAYMENT_INFO: PaymentInfo = PaymentInfo::Transparent(None); #[no_mangle] diff --git a/rusk-abi/tests/lib.rs b/rusk-abi/tests/lib.rs index f47b3bf377..0c37b7a8cc 100644 --- a/rusk-abi/tests/lib.rs +++ b/rusk-abi/tests/lib.rs @@ -357,15 +357,29 @@ fn get_owner() -> &'static PublicKey { } #[test] -fn owner() { +fn owner_raw() { let vm = rusk_abi::new_ephemeral_vm().expect("Instantiating VM should succeed"); let (mut session, contract_id) = instantiate(&vm, 0); let owner: [u8; 64] = session - .call(contract_id, "contract_owner", get_owner(), POINT_LIMIT) + .call(contract_id, "contract_owner_raw", get_owner(), POINT_LIMIT) .expect("Query should succeed") .data; assert_eq!(owner, get_owner().to_bytes()); } + +#[test] +fn owner() { + let vm = + rusk_abi::new_ephemeral_vm().expect("Instantiating VM should succeed"); + let (mut session, contract_id) = instantiate(&vm, 0); + + let owner: PublicKey = session + .call(contract_id, "contract_owner", get_owner(), POINT_LIMIT) + .expect("Query should succeed") + .data; + + assert_eq!(owner, get_owner().to_owned()); +}