Skip to content

Commit

Permalink
Add derivations for the package of direct caller
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Oct 11, 2024
1 parent 5bab8f7 commit 09f449d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
28 changes: 24 additions & 4 deletions crates/radix-engine-toolkit-uniffi/src/common/non_fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ impl NonFungibleGlobalId {
)
}

#[uniffi::constructor]
pub fn global_caller_badge(
component_address: Arc<Address>,
network_id: u8,
) -> Result<Arc<Self>> {
derive_virtual_global_caller_non_fungible_global_id_from_component_address(
component_address, network_id,
)
}

#[uniffi::constructor]
pub fn package_of_direct_caller_badge(
package_address: Arc<Address>,
network_id: u8,
) -> Result<Arc<Self>> {
derive_virtual_package_of_direct_caller_non_fungible_global_id_from_component_address(
package_address, network_id,
)
}

pub fn resource_address(&self) -> Arc<Address> {
let address = self.0.resource_address();
Arc::new(Address::from_typed_node_id(address, self.1))
Expand Down Expand Up @@ -179,10 +199,10 @@ pub fn non_fungible_local_id_sbor_decode(
bytes: Vec<u8>,
) -> Result<NonFungibleLocalId> {
let native = match bytes.first().copied() {
Some(NATIVE_SCRYPTO_SBOR_V1_PAYLOAD_PREFIX) => native_scrypto_decode::<
NativeNonFungibleLocalId,
>(&bytes)
.map_err(Into::into),
Some(NATIVE_SCRYPTO_SBOR_V1_PAYLOAD_PREFIX) => {
native_scrypto_decode::<NativeNonFungibleLocalId>(&bytes)
.map_err(Into::into)
}
Some(NATIVE_MANIFEST_SBOR_V1_PAYLOAD_PREFIX) => {
native_manifest_decode::<NativeNonFungibleLocalId>(&bytes)
.map_err(Into::into)
Expand Down
42 changes: 39 additions & 3 deletions crates/radix-engine-toolkit-uniffi/src/derive/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,37 @@ pub fn derive_virtual_signature_non_fungible_global_id_from_public_key(
core_virtual_signature_non_fungible_global_id_from_public_key(
&public_key,
);
Ok(Arc::new(NonFungibleGlobalId(non_fungible_global_id, network_id)))
Ok(Arc::new(NonFungibleGlobalId(
non_fungible_global_id,
network_id,
)))
}

#[uniffi::export]
pub fn derive_virtual_global_caller_non_fungible_global_id_from_component_address(
component_address: Arc<Address>,
network_id: u8,
) -> Result<Arc<NonFungibleGlobalId>> {
let component_address =
NativeComponentAddress::try_from(*component_address)?;
let non_fungible_global_id = core_virtual_global_caller_non_fungible_global_id_from_component_address(component_address);
Ok(Arc::new(NonFungibleGlobalId(
non_fungible_global_id,
network_id,
)))
}

#[uniffi::export]
pub fn derive_virtual_package_of_direct_caller_non_fungible_global_id_from_component_address(
package_address: Arc<Address>,
network_id: u8,
) -> Result<Arc<NonFungibleGlobalId>> {
let package_address = NativePackageAddress::try_from(*package_address)?;
let non_fungible_global_id = core_virtual_package_of_direct_caller_non_fungible_global_id_from_component_address(package_address);
Ok(Arc::new(NonFungibleGlobalId(
non_fungible_global_id,
network_id,
)))
}

#[uniffi::export]
Expand All @@ -61,7 +91,10 @@ pub fn derive_virtual_account_address_from_olympia_account_address(
core_virtual_account_address_from_olympia_account_address(
&olympia_account_address.0,
)?;
Ok(Arc::new(Address::from_typed_node_id(component_address, network_id)))
Ok(Arc::new(Address::from_typed_node_id(
component_address,
network_id,
)))
}

#[uniffi::export]
Expand All @@ -72,7 +105,10 @@ pub fn derive_resource_address_from_olympia_resource_address(
let resource_address = core_resource_address_from_olympia_resource_address(
&olympia_resource_address.0,
)?;
Ok(Arc::new(Address::from_typed_node_id(resource_address, network_id)))
Ok(Arc::new(Address::from_typed_node_id(
resource_address,
network_id,
)))
}

#[uniffi::export]
Expand Down
2 changes: 2 additions & 0 deletions crates/radix-engine-toolkit-uniffi/src/internal_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ mod core {
virtual_account_address_from_public_key as core_virtual_account_address_from_public_key,
virtual_identity_address_from_public_key as core_virtual_identity_address_from_public_key,
virtual_signature_non_fungible_global_id_from_public_key as core_virtual_signature_non_fungible_global_id_from_public_key,
virtual_global_caller_non_fungible_global_id_from_component_address as core_virtual_global_caller_non_fungible_global_id_from_component_address,
virtual_package_of_direct_caller_non_fungible_global_id_from_component_address as core_virtual_package_of_direct_caller_non_fungible_global_id_from_component_address,
virtual_account_address_from_olympia_account_address as core_virtual_account_address_from_olympia_account_address,
resource_address_from_olympia_resource_address as core_resource_address_from_olympia_resource_address,
public_key_from_olympia_account_address as core_public_key_from_olympia_account_address,
Expand Down
36 changes: 22 additions & 14 deletions crates/radix-engine-toolkit/src/functions/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ where
NonFungibleGlobalId::from_public_key(public_key)
}

pub fn virtual_global_caller_non_fungible_global_id_from_component_address(
component_address: ComponentAddress,
) -> NonFungibleGlobalId {
NonFungibleGlobalId::global_caller_badge(component_address)
}

pub fn virtual_package_of_direct_caller_non_fungible_global_id_from_component_address(
package_address: PackageAddress,
) -> NonFungibleGlobalId {
NonFungibleGlobalId::package_of_direct_caller_badge(package_address)
}

pub fn virtual_account_address_from_olympia_account_address<S>(
olympia_account_address: S,
) -> Result<ComponentAddress, DerivationError>
Expand Down Expand Up @@ -117,14 +129,12 @@ where
olympia_account_address.chars().nth(2),
) {
(Some('d'), Some('x')) => Ok(()),
(Some(char1), Some(char2)) => {
Err(
DerivationError::InvalidCharsInOlympiaAddressEntitySpecifier {
expected: ('d', 'x'),
actual: (char1, char2),
},
)
}
(Some(char1), Some(char2)) => Err(
DerivationError::InvalidCharsInOlympiaAddressEntitySpecifier {
expected: ('d', 'x'),
actual: (char1, char2),
},
),
_ => Err(DerivationError::InvalidOlympiaAddressLength {
expected: 65,
actual: olympia_account_address.len(),
Expand Down Expand Up @@ -159,12 +169,10 @@ where
}
})
}
Some(prefix) => {
Err(DerivationError::InvalidOlympiaAddressPrefix {
expected: 0x04,
actual: *prefix,
})
}
Some(prefix) => Err(DerivationError::InvalidOlympiaAddressPrefix {
expected: 0x04,
actual: *prefix,
}),
None => Err(DerivationError::InvalidOlympiaAddressLength {
expected: EXPECTED_LENGTH,
actual: data.len(),
Expand Down

0 comments on commit 09f449d

Please sign in to comment.