Skip to content

Commit

Permalink
Add address decode
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Sep 11, 2023
1 parent 23d7503 commit 891bb00
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
36 changes: 36 additions & 0 deletions generator/src/function_examples/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,39 @@ impl<'f> HasExamples<'f, EXAMPLE_SIZE> for AddressEntityType {
]
}
}

impl<'f> HasExamples<'f, EXAMPLE_SIZE> for AddressDecode {
fn example_inputs() -> [Self::Input; EXAMPLE_SIZE] {
[
SerializableNodeId::new(XRD.into_node_id(), 0xf2),
SerializableNodeId::new(SECP256K1_SIGNATURE_VIRTUAL_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(ED25519_SIGNATURE_VIRTUAL_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(PACKAGE_OF_DIRECT_CALLER_VIRTUAL_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(GLOBAL_CALLER_VIRTUAL_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(SYSTEM_TRANSACTION_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(PACKAGE_OWNER_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(VALIDATOR_OWNER_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(ACCOUNT_OWNER_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(IDENTITY_OWNER_BADGE.into_node_id(), 0xf2),
SerializableNodeId::new(PACKAGE_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(RESOURCE_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(ACCOUNT_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(IDENTITY_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(CONSENSUS_MANAGER_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(ACCESS_CONTROLLER_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(POOL_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(TRANSACTION_PROCESSOR_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(METADATA_MODULE_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(ROYALTY_MODULE_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(ROLE_ASSIGNMENT_MODULE_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(GENESIS_HELPER_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(FAUCET_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(TRANSACTION_TRACKER_PACKAGE.into_node_id(), 0xf2),
SerializableNodeId::new(CONSENSUS_MANAGER.into_node_id(), 0xf2),
SerializableNodeId::new(GENESIS_HELPER.into_node_id(), 0xf2),
SerializableNodeId::new(FAUCET.into_node_id(), 0xf2),
SerializableNodeId::new(TRANSACTION_TRACKER.into_node_id(), 0xf2),
]
.map(|serializable| serializable.0.to_string())
}
}
3 changes: 2 additions & 1 deletion generator/src/function_examples/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub fn generate_function_examples() -> IndexMap<
UtilsKnownAddress
],
"address" => function_examples![
AddressEntityType
AddressEntityType,
AddressDecode,
]
)
}
Expand Down
3 changes: 2 additions & 1 deletion generator/src/function_schema/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ pub fn generate_function_schema(
ManifestSborDecodeToString
],
"address" => function_schema![
AddressEntityType
AddressEntityType,
AddressDecode,
]
)
}
Expand Down
16 changes: 14 additions & 2 deletions radix-engine-toolkit-core/src/functions/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@
// specific language governing permissions and limitations
// under the License.

use radix_engine_common::types::EntityType;
use radix_engine_common::prelude::*;

use crate::models::node_id::TypedNodeId;
use crate::models::node_id::*;
use crate::utils::*;

pub fn entity_type(node_id: TypedNodeId) -> EntityType {
node_id.entity_type()
}

pub fn decode(node_id: &str) -> Option<(u8, EntityType, String, [u8; 30])> {
let network_id = network_id_from_address_string(node_id)?;
let network_definition = network_definition_from_network_id(network_id);
let decoder = AddressBech32Decoder::new(&network_definition);
let (hrp, _, _) = AddressBech32Decoder::validate_and_decode_ignore_hrp(&node_id).ok()?;
let (entity_type, data) = decoder.validate_and_decode(node_id).ok()?;
data.try_into()
.map(|data| (network_id, entity_type, hrp, data))
.ok()
}
39 changes: 39 additions & 0 deletions radix-engine-toolkit/src/functions/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use crate::prelude::*;
use scrypto::prelude::*;

use schemars::*;
use serde::*;

#[typeshare::typeshare]
pub type AddressEntityTypeInput = SerializableNodeId;

Expand All @@ -40,3 +43,39 @@ impl<'f> Function<'f> for AddressEntityType {

export_function!(AddressEntityType as address_entity_type);
export_jni_function!(AddressEntityType as addressEntityType);

#[typeshare::typeshare]
pub type AddressDecodeInput = String;

#[typeshare::typeshare]
#[derive(
Serialize, Deserialize, JsonSchema, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
)]
pub struct AddressDecodeOutput {
pub network_id: SerializableU8,
pub entity_type: SerializableEntityType,
pub hrp: String,
pub data: SerializableBytes,
}

pub struct AddressDecode;
impl<'f> Function<'f> for AddressDecode {
type Input = AddressDecodeInput;
type Output = AddressDecodeOutput;

fn handle(input: Self::Input) -> Result<Self::Output, crate::error::InvocationHandlingError> {
let (network_id, entity_type, hrp, data) =
radix_engine_toolkit_core::functions::address::decode(&input)
.ok_or(InvocationHandlingError::InvalidAddress(input))?;

Ok(Self::Output {
network_id: network_id.into(),
entity_type: entity_type.into(),
hrp,
data: data.to_vec().into(),
})
}
}

export_function!(AddressDecode as address_decode);
export_jni_function!(AddressDecode as addressDecode);

0 comments on commit 891bb00

Please sign in to comment.