pub enum AccountId {
V0(AccountIdV0),
}Expand description
The identifier of an Account.
This enum is a wrapper around concrete versions of IDs. The following documents version 0.
§Layout
An AccountId consists of two field elements, where the first is called the prefix and the
second is called the suffix. It is laid out as follows:
prefix: [hash (56 bits) | storage mode (2 bits) | type (2 bits) | version (4 bits)]
suffix: [zero bit | hash (55 bits) | 8 zero bits]§Generation
An AccountId is a commitment to a user-generated seed and the code and storage of an account.
An id is generated by first creating the account’s initial storage and code. Then a random seed
is picked and the hash of (SEED, CODE_COMMITMENT, STORAGE_COMMITMENT, EMPTY_WORD) is computed.
This process is repeated until the hash’s first element has the desired storage mode, account
type and version and the suffix’ most significant bit is zero.
The prefix of the ID is exactly the first element of the hash. The suffix of the ID is the second element of the hash, but its lower 8 bits are zeroed. Thus, the prefix of the ID must derive exactly from the hash, while only the first 56 bits of the suffix are derived from the hash.
In total, due to requiring specific bits for storage mode, type, version and the most significant bit in the suffix, generating an ID requires 9 bits of Proof-of-Work.
§Constraints
Constructors will return an error if:
- The prefix contains account ID metadata (storage mode, type or version) that does not match any of the known values.
- The most significant bit of the suffix is not zero.
- The lower 8 bits of the suffix are not zero, although
AccountId::newensures this is the case rather than return an error.
§Design Rationale
The rationale behind the above layout is as follows.
- The prefix is the output of a hash function so it will be a valid field element without requiring additional constraints.
- The version is placed at a static offset such that future ID versions which may change the number of type or storage mode bits will not cause the version to be at a different offset. This is important so that a parser can always reliably read the version and then parse the remainder of the ID depending on the version. Having only 4 bits for the version is a trade off between future proofing to allow introducing more versions and the version requiring Proof of Work as part of the ID generation.
- The version, type and storage mode are part of the prefix which is included in the representation of a non-fungible asset. The prefix alone is enough to determine all of these properties about the ID.
- The most significant bit of the suffix must be zero to ensure the value of the suffix is
always a valid felt, even if the lower 8 bits are all set to
1. The lower 8 bits of the suffix may be overwritten when the ID is embedded in other layouts such as theNoteMetadata. In that case, it can happen that all lower bits of the encoded suffix are one, so having the zero bit constraint is important for validity.
Variants§
V0(AccountIdV0)
Implementations§
Source§impl AccountId
impl AccountId
Sourcepub const SERIALIZED_SIZE: usize = 15usize
pub const SERIALIZED_SIZE: usize = 15usize
The serialized size of an AccountId in bytes.
Sourcepub fn new(
seed: Word,
version: AccountIdVersion,
code_commitment: Word,
storage_commitment: Word,
) -> Result<Self, AccountIdError>
pub fn new( seed: Word, version: AccountIdVersion, code_commitment: Word, storage_commitment: Word, ) -> Result<Self, AccountIdError>
Creates an AccountId by hashing the given seed, code_commitment,
storage_commitment and using the resulting first and second element of the hash as the
prefix and suffix felts of the ID.
See the documentation of the AccountId for more details on the generation.
§Errors
Returns an error if any of the ID constraints are not met. See the constraints documentation for details.
Sourcepub fn new_unchecked(elements: [Felt; 2]) -> Self
pub fn new_unchecked(elements: [Felt; 2]) -> Self
Creates an AccountId from the given felts where the felt at index 0 is the prefix
and the felt at index 1 is the suffix.
§Warning
Validity of the ID must be ensured by the caller. An invalid ID may lead to panics.
§Panics
Panics if the prefix does not contain a known account ID version.
If debug_assertions are enabled (e.g. in debug mode), this function panics if any of the ID constraints are not met. See the constraints documentation for details.
Sourcepub fn compute_account_seed(
init_seed: [u8; 32],
account_type: AccountType,
storage_mode: AccountStorageMode,
version: AccountIdVersion,
code_commitment: Word,
storage_commitment: Word,
) -> Result<Word, AccountError>
pub fn compute_account_seed( init_seed: [u8; 32], account_type: AccountType, storage_mode: AccountStorageMode, version: AccountIdVersion, code_commitment: Word, storage_commitment: Word, ) -> Result<Word, AccountError>
Grinds an account seed until its hash matches the given account_type, storage_mode and
version and returns it as a Word. The input to the hash function next to the seed are
the code_commitment and storage_commitment.
The grinding process is started from the given init_seed which should be a random seed
generated from a cryptographically secure source.
Sourcepub const fn account_type(&self) -> AccountType
pub const fn account_type(&self) -> AccountType
Returns the type of this account ID.
Sourcepub fn is_faucet(&self) -> bool
pub fn is_faucet(&self) -> bool
Returns true if an account with this ID is a faucet which can issue assets.
Sourcepub fn is_regular_account(&self) -> bool
pub fn is_regular_account(&self) -> bool
Returns true if an account with this ID is a regular account.
Sourcepub fn storage_mode(&self) -> AccountStorageMode
pub fn storage_mode(&self) -> AccountStorageMode
Returns the storage mode of this account ID.
Sourcepub fn is_onchain(&self) -> bool
pub fn is_onchain(&self) -> bool
Returns true if the full state of the account is on chain, i.e. if the modes are
AccountStorageMode::Public or AccountStorageMode::Network, false otherwise.
Sourcepub fn is_public(&self) -> bool
pub fn is_public(&self) -> bool
Returns true if the storage mode is AccountStorageMode::Public, false otherwise.
Sourcepub fn is_network(&self) -> bool
pub fn is_network(&self) -> bool
Returns true if the storage mode is AccountStorageMode::Network, false otherwise.
Sourcepub fn is_private(&self) -> bool
pub fn is_private(&self) -> bool
Returns true if the storage mode is AccountStorageMode::Private, false otherwise.
Sourcepub fn version(&self) -> AccountIdVersion
pub fn version(&self) -> AccountIdVersion
Returns the version of this account ID.
Sourcepub fn from_hex(hex_str: &str) -> Result<Self, AccountIdError>
pub fn from_hex(hex_str: &str) -> Result<Self, AccountIdError>
Creates an AccountId from a hex string. Assumes the string starts with “0x” and
that the hexadecimal characters are big-endian encoded.
Sourcepub fn to_hex(self) -> String
pub fn to_hex(self) -> String
Returns a big-endian, hex-encoded string of length 32, including the 0x prefix. This means
it encodes 15 bytes.
Sourcepub fn prefix(&self) -> AccountIdPrefix
pub fn prefix(&self) -> AccountIdPrefix
Returns the AccountIdPrefix of this ID.
The prefix of an account ID is guaranteed to be unique.
Trait Implementations§
Source§impl Deserializable for AccountId
impl Deserializable for AccountId
Source§fn read_from<R: ByteReader>(
source: &mut R,
) -> Result<Self, DeserializationError>
fn read_from<R: ByteReader>( source: &mut R, ) -> Result<Self, DeserializationError>
source, attempts to deserialize these bytes
into Self, and returns the result. Read moreSource§fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
Source§impl From<AccountIdV0> for AccountId
impl From<AccountIdV0> for AccountId
Source§fn from(id: AccountIdV0) -> Self
fn from(id: AccountIdV0) -> Self
Source§impl Ord for AccountId
impl Ord for AccountId
Source§impl PartialOrd for AccountId
impl PartialOrd for AccountId
Source§impl Serializable for AccountId
impl Serializable for AccountId
Source§fn write_into<W: ByteWriter>(&self, target: &mut W)
fn write_into<W: ByteWriter>(&self, target: &mut W)
self into bytes and writes these bytes into the target.Source§fn get_size_hint(&self) -> usize
fn get_size_hint(&self) -> usize
Source§impl TryFrom<[BaseElement; 2]> for AccountId
impl TryFrom<[BaseElement; 2]> for AccountId
Source§fn try_from(elements: [Felt; 2]) -> Result<Self, Self::Error>
fn try_from(elements: [Felt; 2]) -> Result<Self, Self::Error>
Returns an AccountId instantiated with the provided field elements where elements[0]
is taken as the prefix and elements[1] is taken as the suffix.
§Errors
Returns an error if any of the ID constraints are not met. See the constraints documentation for details.
Source§type Error = AccountIdError
type Error = AccountIdError
Source§impl TryFrom<[u8; 15]> for AccountId
impl TryFrom<[u8; 15]> for AccountId
Source§fn try_from(bytes: [u8; 15]) -> Result<Self, Self::Error>
fn try_from(bytes: [u8; 15]) -> Result<Self, Self::Error>
Tries to convert a byte array in big-endian order to an AccountId.
§Errors
Returns an error if any of the ID constraints are not met. See the constraints documentation for details.
Source§type Error = AccountIdError
type Error = AccountIdError
Source§impl TryFrom<u128> for AccountId
impl TryFrom<u128> for AccountId
Source§fn try_from(int: u128) -> Result<Self, Self::Error>
fn try_from(int: u128) -> Result<Self, Self::Error>
Tries to convert a u128 into an AccountId.
§Errors
Returns an error if any of the ID constraints are not met. See the constraints documentation for details.
Source§type Error = AccountIdError
type Error = AccountIdError
impl Copy for AccountId
impl Eq for AccountId
impl StructuralPartialEq for AccountId
Auto Trait Implementations§
impl Freeze for AccountId
impl RefUnwindSafe for AccountId
impl Send for AccountId
impl Sync for AccountId
impl Unpin for AccountId
impl UnwindSafe for AccountId
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more