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 and is layed out as follows:
1st felt: [random (56 bits) | storage mode (2 bits) | type (2 bits) | version (4 bits)]
2nd felt: [anchor_epoch (16 bits) | random (40 bits) | 8 zero bits]§Generation
An AccountId is a commitment to a user-generated seed, the code and storage of an account and
to a certain hash of an epoch block of the blockchain. An id is generated by picking an epoch
block as an anchor - which is why it is also referred to as the anchor block - and creating the
account’s initial storage and code. Then a random seed is picked and the hash of (SEED, CODE_COMMITMENT, STORAGE_COMMITMENT, ANCHOR_BLOCK_COMMITMENT) is computed. If the hash’s first
element has the desired storage mode, account type and version, the computation part of the ID
generation is done. If not, another random seed is picked and the process is repeated. The first
felt of the ID is then the first element of the hash.
The suffix of the ID is the second element of the hash. Its upper 16 bits are overwritten with the epoch in which the ID is anchored and the lower 8 bits are zeroed. Thus, the prefix of the ID must derive exactly from the hash, while only part of the suffix is derived from the hash.
§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 anchor epoch in the suffix is equal to
u16::MAX. - 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 be able to introduce 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 anchor epoch is not important beyond the creation process, so placing it in the second felt is fine. Moreover, all properties of the prefix must be derived from the seed, so they add to the proof of work difficulty. Adding 16 bits of PoW for the epoch would be significant.
- The anchor epoch is placed at the most significant end of the suffix. Its value must be less
than
u16::MAXso that at least one of the upper 16 bits is always zero. This ensures that the entire suffix is valid even if the remaining bits of the felt are one. - The lower 8 bits of the suffix may be overwritten when the ID is encoded in other layouts such
as the
NoteMetadata. In such cases, it can happen that all bits of the encoded suffix would be one, so having the epoch constraint is important. - The ID is dependent on the hash of an epoch block. This is a block whose number is a multiple
of 2^
BlockNumber::EPOCH_LENGTH_EXPONENT, e.g.0,65536,131072, … These are the first blocks of epoch 0, 1, 2, … We call this dependence anchoring because the ID is anchored to that epoch block’s commitment. Anchoring makes it practically impossible for an attacker to construct a rainbow table of account IDs whose epoch is X, if the block for epoch X has not been constructed yet because its hash is then unknown. Therefore, picking a recent anchor block when generating a new ID makes it extremely unlikely that an attacker can highjack this ID because the hash of that block has only been known for a short period of time.- An ID highjack refers to an attack where a user generates an ID and lets someone else send assets to it. At this point the user has not registered the ID on-chain yet, likely because they need the funds in the asset to pay for their first transaction where the account would be registered. Until the ID is registered on chain, an attacker with a rainbow table who happens to have a seed, code and storage commitment combination that hashes to the user’s ID can claim the assets sent to the user’s ID. Adding the anchor block commitment to the ID generation process makes this attack practically impossible.
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,
anchor: AccountIdAnchor,
version: AccountIdVersion,
code_commitment: Digest,
storage_commitment: Digest,
) -> Result<Self, AccountIdError>
pub fn new( seed: Word, anchor: AccountIdAnchor, version: AccountIdVersion, code_commitment: Digest, storage_commitment: Digest, ) -> Result<Self, AccountIdError>
Creates an AccountId by hashing the given seed, code_commitment,
storage_commitment and AccountIdAnchor::block_commitment from the anchor and using
the resulting first and second element of the hash as the prefix and suffix felts of the
ID.
The AccountIdAnchor::epoch from the anchor overwrites part of the suffix.
Note that the anchor must correspond to a valid block in the chain for the ID to be deemed
valid during creation.
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 2 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: Digest,
storage_commitment: Digest,
anchor_block_commitment: Digest,
) -> Result<Word, AccountError>
pub fn compute_account_seed( init_seed: [u8; 32], account_type: AccountType, storage_mode: AccountStorageMode, version: AccountIdVersion, code_commitment: Digest, storage_commitment: Digest, anchor_block_commitment: Digest, ) -> 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, storage_commitment and anchor_block_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 version(&self) -> AccountIdVersion
pub fn version(&self) -> AccountIdVersion
Returns the version of this account ID.
Sourcepub fn anchor_epoch(&self) -> u16
pub fn anchor_epoch(&self) -> u16
Returns the anchor epoch of this account ID.
This is the epoch to which this ID is anchored. The hash of this epoch block is used in the generation of the 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 to_bech32(&self, network_id: NetworkId) -> String
pub fn to_bech32(&self, network_id: NetworkId) -> String
Encodes the AccountId into a bech32 string.
§Encoding
The encoding of an account ID into bech32 is done as follows:
- Convert the account ID into its
[u8; 15]data format. - Insert the address type
AddressType::AccountIdbyte at index 0, shifting all other elements to the right. - Choose an HRP, defined as a
NetworkId, for exampleNetworkId::Mainnetwhose string representation ismm. - Encode the resulting HRP together with the data into a bech32 string using the
bech32::Bech32mchecksum algorithm.
This is an example of an account ID in hex and bech32 representations:
hex: 0x140fa04a1e61fc100000126ef8f1d6
bech32: mm1qq2qlgz2reslcyqqqqfxa7836chrjcvk§Rationale
Having the address type at the very beginning is so that it can be decoded to detect the
type of the address without having to decode the entire data. Moreover, choosing the
address type as a multiple of 8 means the first character of the bech32 string after the
1 separator will be different for every address type. This makes the type of the address
conveniently human-readable.
The only allowed checksum algorithm is Bech32m due to being the best
available checksum algorithm with no known weaknesses (unlike Bech32).
No checksum is also not allowed since the intended use of bech32 is to have error
detection capabilities.
Sourcepub fn from_bech32(
bech32_string: &str,
) -> Result<(NetworkId, Self), AccountIdError>
pub fn from_bech32( bech32_string: &str, ) -> Result<(NetworkId, Self), AccountIdError>
Decodes a bech32 string into an AccountId.
See AccountId::to_bech32 for details on the format. The procedure for decoding the
bech32 data into the ID consists of the inverse operations of encoding.
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<AccountId> for LeafIndex<ACCOUNT_TREE_DEPTH>
Account IDs are used as indexes in the account database, which is a tree of depth 64.
impl From<AccountId> for LeafIndex<ACCOUNT_TREE_DEPTH>
Account IDs are used as indexes in the account database, which is a tree of depth 64.
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