Struct b3_helper_lib::Subaccount
source · pub struct Subaccount(pub [u8; 32]);
Tuple Fields§
§0: [u8; 32]
Implementations§
source§impl Subaccount
impl Subaccount
sourcepub fn new(environment: Environment, nonce: u64) -> Self
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);
sourcepub fn from_principal(principal: Principal) -> Self
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);
sourcepub fn environment(&self) -> Environment
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);
sourcepub fn id(&self) -> String
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");
sourcepub fn name(&self) -> String
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");
sourcepub fn nonce(&self) -> u64
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);
sourcepub fn is_default(&self) -> bool
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);
sourcepub fn is_principal(&self) -> bool
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);
sourcepub fn from_slice(slice: &[u8]) -> Result<Self, SubaccountError>
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());
pub fn as_ref(&self) -> &[u8; 32]
pub fn as_slice(&self) -> &[u8] ⓘ
pub fn to_vec(&self) -> Vec<u8> ⓘ
sourcepub fn to_principal(&self) -> Result<Principal, SubaccountError>
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");
sourcepub fn to_hex(&self) -> String
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());
sourcepub fn from_hex(hex: &str) -> Result<Self, SubaccountError>
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
impl Subaccount
pub fn account_identifier(&self, owner: SignerId) -> AccountIdentifier
pub fn icrc_account(&self, owner: SignerId) -> ICRCAccount
Trait Implementations§
source§impl CandidType for Subaccount
impl CandidType for Subaccount
source§impl Clone for Subaccount
impl Clone for Subaccount
source§fn clone(&self) -> Subaccount
fn clone(&self) -> Subaccount
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Subaccount
impl Debug for Subaccount
source§impl Default for Subaccount
impl Default for Subaccount
source§impl<'de> Deserialize<'de> for Subaccount
impl<'de> Deserialize<'de> for Subaccount
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl Display for Subaccount
impl Display for Subaccount
source§impl From<Principal> for Subaccount
impl From<Principal> for Subaccount
source§impl From<Subaccount> for ICRCAccount
impl From<Subaccount> for ICRCAccount
source§fn from(subaccount: Subaccount) -> Self
fn from(subaccount: Subaccount) -> Self
source§impl FromStr for Subaccount
impl FromStr for Subaccount
source§impl Hash for Subaccount
impl Hash for Subaccount
source§impl Ord for Subaccount
impl Ord for Subaccount
source§impl PartialEq<Subaccount> for Subaccount
impl PartialEq<Subaccount> for Subaccount
source§fn eq(&self, other: &Subaccount) -> bool
fn eq(&self, other: &Subaccount) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<Subaccount> for Subaccount
impl PartialOrd<Subaccount> for Subaccount
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