Struct Subaccount

Source
pub struct Subaccount(pub [u8; 32]);

Tuple Fields§

§0: [u8; 32]

Implementations§

Source§

impl Subaccount

Source

pub fn new(environment: Environment, nonce: u64) -> Self

Creates a new Subaccount with a given environment and nonce.

§Example
use b3_helper_lib::{Environment, Subaccount};

let subaccount = Subaccount::new(Environment::Production, 123456789);
assert_eq!(subaccount.id(), "account_123456789");
assert_eq!(subaccount.nonce(), 123456789);
assert_eq!(subaccount.environment(), Environment::Production);
Source

pub fn from_principal(principal: Principal) -> Self

Creates a new Subaccount with a given environment and nonce. This method is used to create subaccounts for the principal. The nonce is set to 0. The environment is set to production.

§Example
use b3_helper_lib::{Environment, Subaccount};
use candid::Principal;

let principal = Principal::from_text("2chl6-4hpzw-vqaaa-aaaaa-c").unwrap();

let subaccount = Subaccount::from_principal(principal);

assert_eq!(subaccount.id(), "principal_2chl6-4hpzw-vqaaa-aaaaa-c");
assert_eq!(subaccount.nonce(), 0);
assert_eq!(subaccount.environment(), Environment::Production);

let principal = Principal::from_text("b7pqa-qqaaa-aaaap-abdva-cai").unwrap();

let subaccount = Subaccount::from_principal(principal);

assert_eq!(subaccount.id(), "principal_b7pqa-qqaaa-aaaap-abdva-cai");
assert_eq!(subaccount.nonce(), 0);
assert_eq!(subaccount.environment(), Environment::Production);
Source

pub fn environment(&self) -> Environment

Creates a new Subaccount with a given environment and nonce. This method is used to create subaccounts for the principal.

§Example
use b3_helper_lib::{Environment, Subaccount};
use candid::Principal;

let principal = Principal::from_text("b7pqa-qqaaa-aaaap-abdva-cai").unwrap();

let subaccount = Subaccount::from_principal(principal);
assert_eq!(subaccount.environment(), Environment::Production);

let subaccount = Subaccount::new(Environment::Development, 123456789);
assert_eq!(subaccount.environment(), Environment::Development);
Source

pub fn id(&self) -> String

Returns the id of the subaccount. The id is used to identify the subaccount in the backend.

§Example
use b3_helper_lib::{Environment, Subaccount};
use candid::Principal;

let principal = Principal::from_text("b7pqa-qqaaa-aaaap-abdva-cai").unwrap();

let subaccount = Subaccount::from_principal(principal);
assert_eq!(subaccount.id(), "principal_b7pqa-qqaaa-aaaap-abdva-cai");

let subaccount = Subaccount::new(Environment::Development, 123456789);
assert_eq!(subaccount.id(), "development_account_123456789");

let subaccount = Subaccount::new(Environment::Staging, 123456789);
assert_eq!(subaccount.id(), "staging_account_123456789");

let subaccount = Subaccount::new(Environment::Production, 123456789);
assert_eq!(subaccount.id(), "account_123456789");
Source

pub fn name(&self) -> String

returns the account name of the subaccount

§Example
use b3_helper_lib::{Environment, Subaccount};

let subaccount = Subaccount::new(Environment::Production, 123456789);
assert_eq!(subaccount.name(), "Account 123456790");

let subaccount = Subaccount::new(Environment::Production, 0);
assert_eq!(subaccount.name(), "Default");

let subaccount = Subaccount::new(Environment::Production, 1);
assert_eq!(subaccount.name(), "Account 2");

let subaccount = Subaccount::new(Environment::Production, 2);
assert_eq!(subaccount.name(), "Account 3");
Source

pub fn nonce(&self) -> u64

returns the nonce of the subaccount The nonce is the last 24 bytes of the subaccount if first byte of the subaccount id is 0 then its an Account otherwise its an Principal

§Example
use b3_helper_lib::{Environment, Subaccount};

let subaccount = Subaccount::from_principal("2chl6-4hpzw-vqaaa-aaaaa-c".parse().unwrap());
assert_eq!(subaccount.nonce(), 0);

let subaccount = Subaccount::new(Environment::Production, 123456789);
assert_eq!(subaccount.nonce(), 123456789);

let subaccount = Subaccount::new(Environment::Production, 1);
assert_eq!(subaccount.nonce(), 1);
Source

pub fn is_default(&self) -> bool

Checks if the subaccount is the default subaccount The default subaccount is the first Production subaccount of an account

§Example
use b3_helper_lib::{Environment, Subaccount};

let subaccount = Subaccount::from_principal("2chl6-4hpzw-vqaaa-aaaaa-c".parse().unwrap());
assert_eq!(subaccount.is_default(), false);

let subaccount = Subaccount::new(Environment::Production, 123456789);
assert_eq!(subaccount.is_default(), false);

let subaccount = Subaccount::new(Environment::Development, 0);
assert_eq!(subaccount.is_default(), false);

let subaccount = Subaccount::new(Environment::Staging, 0);
assert_eq!(subaccount.is_default(), false);

let subaccount = Subaccount::new(Environment::Production, 0);
assert_eq!(subaccount.is_default(), true);
Source

pub fn is_principal(&self) -> bool

Checks if the subaccount is a principal subaccount A principal subaccount is a subaccount that is not the default subaccount and has a principal id

§Example
use b3_helper_lib::{Environment, Subaccount};

let subaccount = Subaccount::from_principal("2chl6-4hpzw-vqaaa-aaaaa-c".parse().unwrap());
assert_eq!(subaccount.is_principal(), true);

let subaccount = Subaccount::new(Environment::Production, 123456789);
assert_eq!(subaccount.is_principal(), false);

let subaccount = Subaccount::new(Environment::Development, 0);
assert_eq!(subaccount.is_principal(), false);

let subaccount = Subaccount::new(Environment::Staging, 0);
assert_eq!(subaccount.is_principal(), false);

let subaccount = Subaccount::new(Environment::Production, 0);
assert_eq!(subaccount.is_principal(), false);
Source

pub fn from_slice(slice: &[u8]) -> Result<Self, SubaccountError>

Returns the subaccount from slice. Error if the slice is not 32 bytes long.

§Example
use b3_helper_lib::Subaccount;

let subaccount = Subaccount::from_slice(&[0u8; 32]).unwrap();
assert!(subaccount.is_default());

let subaccount = Subaccount::from_slice(&[1u8; 32]).unwrap();
assert_eq!(subaccount.to_string(), "0101010101010101010101010101010101010101010101010101010101010101".to_string());

let subaccount = Subaccount::from_slice(&[2u8; 32]).unwrap();
assert_eq!(subaccount.to_string(), "0202020202020202020202020202020202020202020202020202020202020202".to_string());

let subaccount = Subaccount::from_slice(&[0u8; 33]);
assert!(subaccount.is_err());
Source

pub fn as_ref(&self) -> &[u8; 32]

Source

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

Source

pub fn to_vec(&self) -> Vec<u8>

Source

pub fn to_principal(&self) -> Result<Principal, SubaccountError>

Returns the subaccount as a Principal. Panics if the slice is longer than 29 bytes.

§Example
use b3_helper_lib::{Environment, Subaccount};
use candid::Principal;

let principal = Principal::from_text("b7pqa-qqaaa-aaaap-abdva-cai").unwrap();

let subaccount = Subaccount::from_principal(principal);

assert_eq!(subaccount.to_principal().unwrap().to_text(), "b7pqa-qqaaa-aaaap-abdva-cai");
Source

pub fn to_hex(&self) -> String

Returns the hex representation of the subaccount with leading zeros removed e.g. 0000000 will be returned as 0 0000001 will be returned as 1

§Example
use b3_helper_lib::{Environment, Subaccount};
use candid::Principal;

let subaccount = Subaccount::new(Environment::Production, 0);
assert_eq!(subaccount.to_hex(), "".to_string());

let subaccount = Subaccount::from_principal("2chl6-4hpzw-vqaaa-aaaaa-c".parse().unwrap());
assert_eq!(subaccount.to_hex(), "9efcdab00000000000100000000000000000000000000000000000000000000".to_string());
Source

pub fn from_hex(hex: &str) -> Result<Self, SubaccountError>

Returns the hex representation of the subaccount with add leading zeros if necessary

§Example
use b3_helper_lib::{Environment, Subaccount};
use candid::Principal;

let subaccount = Subaccount::from_hex("").unwrap();
assert!(subaccount.is_default());

let subaccount = Subaccount::from_hex("test");
assert!(subaccount.is_err());

let subaccount = Subaccount::from_hex("1").unwrap();
assert_eq!(subaccount.id(), "account_1");

let subaccount = Subaccount::from_hex("ff00000000000004d2").unwrap();
assert_eq!(subaccount.id(), "development_account_1234");
assert_eq!(subaccount.nonce(), 1234);
assert_eq!(subaccount.environment(), Environment::Development);
assert_eq!(subaccount.id(), "development_account_1234");

let subaccount = Subaccount::from_hex("aa00000000075bcd15").unwrap();
assert_eq!(subaccount.id(), "staging_account_123456789");
assert_eq!(subaccount.nonce(), 123456789);
assert_eq!(subaccount.environment(), Environment::Staging);

let subaccount = Subaccount::from_hex("9efcdab00000000000100000000000000000000000000000000000000000000").unwrap();
assert!(subaccount.is_principal());
assert_eq!(subaccount.to_principal().unwrap().to_text(), "2chl6-4hpzw-vqaaa-aaaaa-c");
Source§

impl Subaccount

Trait Implementations§

Source§

impl CandidType for Subaccount

Source§

fn _ty() -> Type

Source§

fn id() -> TypeId

Source§

fn idl_serialize<__S>(&self, __serializer: __S) -> Result<(), __S::Error>
where __S: Serializer,

Source§

fn ty() -> Type

Source§

impl Clone for Subaccount

Source§

fn clone(&self) -> Subaccount

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 Subaccount

Source§

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

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

impl Default for Subaccount

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Subaccount

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

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

impl Display for Subaccount

Source§

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

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

impl From<[u8; 32]> for Subaccount

Source§

fn from(bytes: [u8; 32]) -> Self

Converts to this type from the input type.
Source§

impl From<Principal> for Subaccount

Source§

fn from(principal: Principal) -> Self

Converts to this type from the input type.
Source§

impl From<Subaccount> for ICRCAccount

Source§

fn from(subaccount: Subaccount) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Subaccount

Source§

type Err = SubaccountError

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

fn from_str(s: &str) -> Result<Self, Self::Err>

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

impl Hash for Subaccount

Source§

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

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 Subaccount

Source§

fn cmp(&self, other: &Self) -> 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,

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

impl PartialEq for Subaccount

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Subaccount

Source§

fn partial_cmp(&self, other: &Self) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Subaccount

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

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

impl TryFrom<Vec<u8>> for Subaccount

Source§

type Error = SubaccountError

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

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for Subaccount

Source§

impl StructuralPartialEq for Subaccount

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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§

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

Source§

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

Source§

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