Struct near_account_id::AccountId
source · pub struct AccountId(/* private fields */);
Expand description
NEAR Account Identifier.
This is a unique, syntactically valid, human-readable account identifier on the NEAR network.
See the crate-level docs for information about validation.
Also see Error kind precedence.
Examples
use near_account_id::AccountId;
let alice: AccountId = "alice.near".parse().unwrap();
assert!("ƒelicia.near".parse::<AccountId>().is_err()); // (ƒ is not f)
Implementations§
source§impl AccountId
impl AccountId
sourcepub fn validate(account_id: &str) -> Result<(), ParseAccountError>
pub fn validate(account_id: &str) -> Result<(), ParseAccountError>
Validates a string as a well-structured NEAR Account ID.
Checks Account ID validity without constructing an AccountId
instance.
Examples
use near_account_id::{AccountId, ParseErrorKind};
assert!(AccountId::validate("alice.near").is_ok());
assert!(
matches!(
AccountId::validate("ƒelicia.near"), // fancy ƒ!
Err(err) if err.kind() == &ParseErrorKind::InvalidChar
)
);
Error kind precedence
If an Account ID has multiple format violations, the first one would be reported.
Examples
use near_account_id::{AccountId, ParseErrorKind};
assert!(
matches!(
AccountId::validate("A__ƒƒluent."),
Err(err) if err.kind() == &ParseErrorKind::InvalidChar
)
);
assert!(
matches!(
AccountId::validate("a__ƒƒluent."),
Err(err) if err.kind() == &ParseErrorKind::RedundantSeparator
)
);
assert!(
matches!(
AccountId::validate("aƒƒluent."),
Err(err) if err.kind() == &ParseErrorKind::InvalidChar
)
);
assert!(
matches!(
AccountId::validate("affluent."),
Err(err) if err.kind() == &ParseErrorKind::RedundantSeparator
)
);
Methods from Deref<Target = AccountIdRef>§
pub const MIN_LEN: usize = 2usize
pub const MAX_LEN: usize = 64usize
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns a string slice of the entire Account ID.
Examples
use near_account_id::AccountIdRef;
let carol = AccountIdRef::new("carol.near").unwrap();
assert_eq!("carol.near", carol.as_str());
sourcepub fn is_top_level(&self) -> bool
pub fn is_top_level(&self) -> bool
Returns true
if the account ID is a top-level NEAR Account ID.
See Top-level Accounts.
Examples
use near_account_id::AccountIdRef;
let near_tla = AccountIdRef::new("near").unwrap();
assert!(near_tla.is_top_level());
// "alice.near" is a sub account of "near" account
let alice = AccountIdRef::new("alice.near").unwrap();
assert!(!alice.is_top_level());
sourcepub fn is_sub_account_of(&self, parent: &AccountIdRef) -> bool
pub fn is_sub_account_of(&self, parent: &AccountIdRef) -> bool
Returns true
if the AccountId
is a direct sub-account of the provided parent account.
See Subaccounts.
Examples
use near_account_id::AccountId;
let near_tla: AccountId = "near".parse().unwrap();
assert!(near_tla.is_top_level());
let alice: AccountId = "alice.near".parse().unwrap();
assert!(alice.is_sub_account_of(&near_tla));
let alice_app: AccountId = "app.alice.near".parse().unwrap();
// While app.alice.near is a sub account of alice.near,
// app.alice.near is not a sub account of near
assert!(alice_app.is_sub_account_of(&alice));
assert!(!alice_app.is_sub_account_of(&near_tla));
sourcepub fn get_account_type(&self) -> AccountType
pub fn get_account_type(&self) -> AccountType
Returns AccountType::EthImplicitAccount
if the AccountId
is a 40 characters long hexadecimal prefixed with ‘0x’.
Returns AccountType::NearImplicitAccount
if the AccountId
is a 64 characters long hexadecimal.
Otherwise, returns AccountType::NamedAccount
.
See Implicit-Accounts.
Examples
use near_account_id::{AccountId, AccountType};
let alice: AccountId = "alice.near".parse().unwrap();
assert!(alice.get_account_type() == AccountType::NamedAccount);
let eth_rando = "0xb794f5ea0ba39494ce839613fffba74279579268"
.parse::<AccountId>()
.unwrap();
assert!(eth_rando.get_account_type() == AccountType::EthImplicitAccount);
let near_rando = "98793cd91a3f870fb126f66285808c7e094afcfc4eda8a970f6648cdf0dbd6de"
.parse::<AccountId>()
.unwrap();
assert!(near_rando.get_account_type() == AccountType::NearImplicitAccount);
sourcepub fn is_system(&self) -> bool
pub fn is_system(&self) -> bool
Returns true
if this AccountId
is the system account.
See System account.
Examples
use near_account_id::AccountId;
let alice: AccountId = "alice.near".parse().unwrap();
assert!(!alice.is_system());
let system: AccountId = "system".parse().unwrap();
assert!(system.is_system());
sourcepub fn get_parent_account_id(&self) -> Option<&AccountIdRef>
pub fn get_parent_account_id(&self) -> Option<&AccountIdRef>
Returns parent’s account id reference
Examples
use near_account_id::AccountIdRef;
let alice: &AccountIdRef = AccountIdRef::new_or_panic("alice.near");
let parent: &AccountIdRef = alice.get_parent_account_id().unwrap();
assert!(alice.is_sub_account_of(parent));
let near: &AccountIdRef = AccountIdRef::new_or_panic("near");
assert!(near.get_parent_account_id().is_none());
let implicit: &AccountIdRef = AccountIdRef::new_or_panic("248e104d1d4764d713c4211c13808c8fc887869c580f4178e60538ac5c2a0b26");
assert!(implicit.get_parent_account_id().is_none());
Trait Implementations§
source§impl AsRef<AccountIdRef> for AccountId
impl AsRef<AccountIdRef> for AccountId
source§fn as_ref(&self) -> &AccountIdRef
fn as_ref(&self) -> &AccountIdRef
source§impl Borrow<AccountIdRef> for AccountId
impl Borrow<AccountIdRef> for AccountId
source§fn borrow(&self) -> &AccountIdRef
fn borrow(&self) -> &AccountIdRef
source§impl<'a> From<&'a AccountIdRef> for AccountId
impl<'a> From<&'a AccountIdRef> for AccountId
source§fn from(id: &'a AccountIdRef) -> Self
fn from(id: &'a AccountIdRef) -> Self
source§impl<'a> From<Cow<'a, AccountIdRef>> for AccountId
impl<'a> From<Cow<'a, AccountIdRef>> for AccountId
source§fn from(value: Cow<'a, AccountIdRef>) -> Self
fn from(value: Cow<'a, AccountIdRef>) -> Self
source§impl Ord for AccountId
impl Ord for AccountId
source§impl<'a> PartialEq<&'a AccountIdRef> for AccountId
impl<'a> PartialEq<&'a AccountIdRef> for AccountId
source§fn eq(&self, other: &&'a AccountIdRef) -> bool
fn eq(&self, other: &&'a AccountIdRef) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialEq<&'a str> for AccountId
impl<'a> PartialEq<&'a str> for AccountId
source§impl<'a> PartialEq<AccountId> for &'a AccountIdRef
impl<'a> PartialEq<AccountId> for &'a AccountIdRef
source§impl<'a> PartialEq<AccountId> for &'a str
impl<'a> PartialEq<AccountId> for &'a str
source§impl PartialEq<AccountId> for AccountIdRef
impl PartialEq<AccountId> for AccountIdRef
source§impl PartialEq<AccountId> for String
impl PartialEq<AccountId> for String
source§impl PartialEq<AccountId> for str
impl PartialEq<AccountId> for str
source§impl PartialEq<AccountIdRef> for AccountId
impl PartialEq<AccountIdRef> for AccountId
source§fn eq(&self, other: &AccountIdRef) -> bool
fn eq(&self, other: &AccountIdRef) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<String> for AccountId
impl PartialEq<String> for AccountId
source§impl PartialEq for AccountId
impl PartialEq for AccountId
source§impl<'a> PartialOrd<&'a AccountIdRef> for AccountId
impl<'a> PartialOrd<&'a AccountIdRef> for AccountId
source§fn partial_cmp(&self, other: &&'a AccountIdRef) -> Option<Ordering>
fn partial_cmp(&self, other: &&'a AccountIdRef) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'a> PartialOrd<&'a str> for AccountId
impl<'a> PartialOrd<&'a str> for AccountId
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'a> PartialOrd<AccountId> for &'a AccountIdRef
impl<'a> PartialOrd<AccountId> for &'a AccountIdRef
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'a> PartialOrd<AccountId> for &'a str
impl<'a> PartialOrd<AccountId> for &'a str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<AccountId> for AccountIdRef
impl PartialOrd<AccountId> for AccountIdRef
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<AccountId> for String
impl PartialOrd<AccountId> for String
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<AccountId> for str
impl PartialOrd<AccountId> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<AccountIdRef> for AccountId
impl PartialOrd<AccountIdRef> for AccountId
source§fn partial_cmp(&self, other: &AccountIdRef) -> Option<Ordering>
fn partial_cmp(&self, other: &AccountIdRef) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<String> for AccountId
impl PartialOrd<String> for AccountId
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<str> for AccountId
impl PartialOrd<str> for AccountId
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd for AccountId
impl PartialOrd for AccountId
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more