Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Feb 13, 2024
1 parent de79006 commit af6f9f0
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ mod core;
mod hierarchical_deterministic;
mod profile;
mod wallet;
mod wret;

pub mod prelude {

pub use crate::wret::*;
pub use crate::core::*;
pub use crate::hierarchical_deterministic::*;
pub use crate::profile::*;
Expand Down
3 changes: 2 additions & 1 deletion src/profile/v100/address/account_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ impl EntityAddress for AccountAddress {
/// to validate the HRP (`"account_"`) and is also used when forming HD derivation
/// paths as per CAP26.
fn entity_type() -> AbstractEntityType {
AbstractEntityType::Account
// AbstractEntityType::Account
todo!()
}

// Underscored to decrease visibility. You SHOULD NOT call this function directly,
Expand Down
180 changes: 142 additions & 38 deletions src/profile/v100/entity/abstract_entity_type.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,159 @@
use crate::prelude::*;
use radix_engine_common::types::EntityType as ScryptoEntityType;

/// Type of a wallet Radix Entity - Account or Identity (used by Personas).
///
/// CAP26 uses this type to create separate key spaces for Accounts and Identities
#[derive(
Serialize,
Deserialize,
FromRepr,
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
)]
#[repr(u32)] // it is u32 since used in Derivation Paths (CAP26) where each component is a u32.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum AccountOrIdentityEntityTypePayload {
Virtual {
/// `is_legacy` will be `true` if the address was created using a `Secp256k1PublicKey` instead of `Ed25519PublicKey`
is_legacy: bool,
},
NonVirtual,
}
type AccountEntityTypePayload = AccountOrIdentityEntityTypePayload;
type IdentityEntityTypePayload = AccountOrIdentityEntityTypePayload;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum FungibleOrNonFungibleEntityTypePayload {
Fungible,
NonFungible,
}
type ResourceEntityTypePayload = FungibleOrNonFungibleEntityTypePayload;
type VaultEntityTypePayload = FungibleOrNonFungibleEntityTypePayload;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum ResourcePoolEntityTypePayload {
SinglePool,
TwoPools,
MultiPool,
}

/// Used to allow easy implementations of different Address types, e.g. AccountAddress
/// and ResourceAddress.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum AbstractEntityType {
/// The entity type used by Accounts.
Account,
/// The entity type used by Personas.
Identity,
/// Resource address
Resource,
/// `AccountAddress` uses this entity type
Account { payload: AccountEntityTypePayload },

/// `OlympiaAccountAddress` uses this entity type.
///
/// N.B. there is no corresponding `ScryptoEntityType` (radix_engine_common::types::EntityType) for this,
/// since is outside of the Babylon NodeId and address space system.
///
/// Also Olympia Account addresses uses bech32, not bech32m as Babylon uses.
OlympiaAccount,

/// `IdentityAddress` uses this entity type
Identity { payload: IdentityEntityTypePayload },

/// `ResourceAddress` uses this entity type
Resource { payload: ResourceEntityTypePayload },

/// `PackageAddress` uses this entity type
Package,

/// `ComponentAddress` uses this entity type
Component,

/// `AccessControllerAddress` uses this entity type
AccessController,

/// `ValidatorAddress` uses this entity type
Validator,

/// `VaultAddress` uses this entity type
Vault { payload: VaultEntityTypePayload },

/// `ResourcePoolAddress` uses this entity type
ResourcePool {
payload: ResourcePoolEntityTypePayload,
},
}
impl AbstractEntityType {
/// Conversion of the Radix Engines type for EntityType to Self.
pub fn try_from(value: ScryptoEntityType) -> Result<Self> {
pub fn try_from(
value: ScryptoEntityType,
) -> Result<AbstractEntityType, CommonError> {
match value {
ScryptoEntityType::GlobalVirtualEd25519Account => Ok(Self::Account),
ScryptoEntityType::GlobalVirtualEd25519Account => {
Ok(Self::Account {
payload: AccountEntityTypePayload::Virtual {
is_legacy: false,
},
})
}
ScryptoEntityType::GlobalAccount => Ok(Self::Account {
payload: AccountEntityTypePayload::NonVirtual,
}),
ScryptoEntityType::GlobalVirtualSecp256k1Account => {
Ok(Self::Account)
Ok(Self::Account {
payload: AccountEntityTypePayload::Virtual {
is_legacy: true,
},
})
}
ScryptoEntityType::GlobalVirtualEd25519Identity => {
Ok(Self::Identity)
Ok(Self::Identity {
payload: IdentityEntityTypePayload::Virtual {
is_legacy: false,
},
})
}
ScryptoEntityType::GlobalIdentity => Ok(Self::Identity {
payload: IdentityEntityTypePayload::NonVirtual,
}),
ScryptoEntityType::GlobalVirtualSecp256k1Identity => {
error!("Identity created by Secp256k1 PublicKey found, this is a serious error.");
Ok(Self::Identity {
payload: IdentityEntityTypePayload::Virtual {
is_legacy: true,
},
})
}
ScryptoEntityType::GlobalFungibleResourceManager => {
Ok(Self::Resource)
Ok(Self::Resource {
payload: FungibleOrNonFungibleEntityTypePayload::Fungible,
})
}
ScryptoEntityType::GlobalNonFungibleResourceManager => {
Ok(Self::Resource {
payload:
FungibleOrNonFungibleEntityTypePayload::NonFungible,
})
}
ScryptoEntityType::GlobalPackage => Ok(Self::Package),
ScryptoEntityType::GlobalValidator => Ok(Self::Validator),
ScryptoEntityType::GlobalOneResourcePool => {
Ok(Self::ResourcePool {
payload: ResourcePoolEntityTypePayload::SinglePool,
})
}
ScryptoEntityType::GlobalTwoResourcePool => {
Ok(Self::ResourcePool {
payload: ResourcePoolEntityTypePayload::TwoPools,
})
}
ScryptoEntityType::GlobalMultiResourcePool => {
Ok(Self::ResourcePool {
payload: ResourcePoolEntityTypePayload::MultiPool,
})
}
ScryptoEntityType::GlobalAccessController => {
Ok(Self::AccessController)
}
ScryptoEntityType::InternalNonFungibleVault => Ok(Self::Vault {
payload: VaultEntityTypePayload::NonFungible,
}),
ScryptoEntityType::InternalFungibleVault => Ok(Self::Vault {
payload: VaultEntityTypePayload::Fungible,
}),
ScryptoEntityType::GlobalConsensusManager
| ScryptoEntityType::GlobalTransactionTracker
| ScryptoEntityType::GlobalGenericComponent
| ScryptoEntityType::InternalFungibleVault
| ScryptoEntityType::InternalGenericComponent
| ScryptoEntityType::InternalKeyValueStore => {
Err(CommonError::UnsupportedEntityType)
}
_ => Err(CommonError::UnsupportedEntityType),
}
}

/// Human Readable Part (HRP) used to create account and identity addresses.
pub fn hrp(&self) -> String {
match self {
Self::Account => "account".to_string(),
Self::Identity => "identity".to_string(),
Self::Resource => "resource".to_string(),
}
}
}
Empty file added src/wret/mod.rs
Empty file.

0 comments on commit af6f9f0

Please sign in to comment.