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 more