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

§

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) -> Selfwhere Self: Sized,

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

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

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

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

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

impl PartialEq<Subaccount> for Subaccount

source§

fn eq(&self, other: &Subaccount) -> 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 PartialOrd<Subaccount> 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

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 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, Global>> for Subaccount

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere T: for<'de> Deserialize<'de>,