Struct near_sdk::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

source

pub const MIN_LEN: usize = 2usize

Shortest valid length for a NEAR Account ID.

source

pub const MAX_LEN: usize = 64usize

Longest valid length for a NEAR Account ID.

source

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>§

source

pub const MIN_LEN: usize = 2usize

source

pub const MAX_LEN: usize = 64usize

source

pub fn as_bytes(&self) -> &[u8]

Returns a reference to the account ID bytes.

source

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());
source

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());
source

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));
source

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);
source

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());
source

pub fn len(&self) -> usize

Returns the length of the underlying account id string.

source

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

source§

fn as_ref(&self) -> &AccountIdRef

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<str> for AccountId

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<AccountIdRef> for AccountId

source§

fn borrow(&self) -> &AccountIdRef

Immutably borrows from an owned value. Read more
source§

impl BorshDeserialize for AccountId

source§

fn deserialize_reader<R>(rd: &mut R) -> Result<AccountId, Error>
where R: Read,

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

source§

impl BorshSerialize for AccountId

source§

fn serialize<W>(&self, writer: &mut W) -> Result<(), Error>
where W: Write,

source§

impl Clone for AccountId

source§

fn clone(&self) -> AccountId

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for AccountId

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Deref for AccountId

§

type Target = AccountIdRef

The resulting type after dereferencing.
source§

fn deref(&self) -> &<AccountId as Deref>::Target

Dereferences the value.
source§

impl<'de> Deserialize<'de> for AccountId

source§

fn deserialize<D>( deserializer: D ) -> Result<AccountId, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for AccountId

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<'a> From<&'a AccountIdRef> for AccountId

source§

fn from(id: &'a AccountIdRef) -> AccountId

Converts to this type from the input type.
source§

impl<'a> From<Cow<'a, AccountIdRef>> for AccountId

source§

fn from(value: Cow<'a, AccountIdRef>) -> AccountId

Converts to this type from the input type.
source§

impl FromStr for AccountId

§

type Err = ParseAccountError

The associated error which can be returned from parsing.
source§

fn from_str(account_id: &str) -> Result<AccountId, <AccountId as FromStr>::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for AccountId

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for AccountId

source§

fn cmp(&self, other: &AccountId) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<'a> PartialEq<&'a AccountIdRef> for AccountId

source§

fn eq(&self, other: &&'a AccountIdRef) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<&'a str> for AccountId

source§

fn eq(&self, other: &&'a str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<AccountId> for &'a AccountIdRef

source§

fn eq(&self, other: &AccountId) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<AccountId> for &'a str

source§

fn eq(&self, other: &AccountId) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<AccountId> for AccountIdRef

source§

fn eq(&self, other: &AccountId) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<AccountId> for str

source§

fn eq(&self, other: &AccountId) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<AccountIdRef> for AccountId

source§

fn eq(&self, other: &AccountIdRef) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<String> for AccountId

source§

fn eq(&self, other: &String) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<str> for AccountId

source§

fn eq(&self, other: &str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for AccountId

source§

fn eq(&self, other: &AccountId) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialOrd<&'a AccountIdRef> for AccountId

source§

fn partial_cmp(&self, other: &&'a AccountIdRef) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a> PartialOrd<&'a str> for AccountId

source§

fn partial_cmp(&self, other: &&'a str) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a> PartialOrd<AccountId> for &'a AccountIdRef

source§

fn partial_cmp(&self, other: &AccountId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a> PartialOrd<AccountId> for &'a str

source§

fn partial_cmp(&self, other: &AccountId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<AccountId> for AccountIdRef

source§

fn partial_cmp(&self, other: &AccountId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<AccountId> for str

source§

fn partial_cmp(&self, other: &AccountId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<AccountIdRef> for AccountId

source§

fn partial_cmp(&self, other: &AccountIdRef) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<String> for AccountId

source§

fn partial_cmp(&self, other: &String) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<str> for AccountId

source§

fn partial_cmp(&self, other: &str) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd for AccountId

source§

fn partial_cmp(&self, other: &AccountId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for AccountId

source§

fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<Box<str>> for AccountId

§

type Error = ParseAccountError

The type returned in the event of a conversion error.
source§

fn try_from( account_id: Box<str> ) -> Result<AccountId, <AccountId as TryFrom<Box<str>>>::Error>

Performs the conversion.
source§

impl TryFrom<String> for AccountId

§

type Error = ParseAccountError

The type returned in the event of a conversion error.
source§

fn try_from( account_id: String ) -> Result<AccountId, <AccountId as TryFrom<String>>::Error>

Performs the conversion.
source§

impl Eq for AccountId

source§

impl StructuralPartialEq for AccountId

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FunctionError for T
where T: AsRef<str>,

source§

fn panic(&self) -> !

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,