1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Canonical fixed-width subaccount atom.
use crate::Principal;
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
//
// Subaccount
//
type SubaccountBytes = [u8; 32];
#[derive(
CandidType,
Clone,
Copy,
Debug,
Default,
Eq,
PartialEq,
Hash,
Ord,
PartialOrd,
Serialize,
Deserialize,
)]
/// A canonical 32-byte ICRC account subaccount.
pub struct Subaccount(SubaccountBytes);
impl Subaccount {
/// The lexicographically smallest subaccount.
pub const MIN: Self = Self::from_array([0x00; 32]);
/// The lexicographically largest subaccount.
pub const MAX: Self = Self::from_array([0xFF; 32]);
/// Return the fixed-width byte array.
#[must_use]
pub const fn to_array(&self) -> [u8; 32] {
self.0
}
/// Construct from the exact fixed-width byte array.
#[must_use]
pub const fn from_array(array: [u8; 32]) -> Self {
Self(array)
}
/// Borrow the fixed-width bytes.
#[must_use]
pub const fn as_slice(&self) -> &[u8] {
&self.0
}
/// Consume the value and return its fixed-width bytes.
#[must_use]
pub const fn to_bytes(self) -> [u8; 32] {
self.0
}
}
impl Display for Subaccount {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in &self.0 {
write!(f, "{byte:02x}")?;
}
Ok(())
}
}
// code taken from
// <https://docs.rs/ic-ledger-types/latest/src/ic_ledger_types/lib.rs.html#140-148>
#[expect(clippy::cast_possible_truncation)]
impl From<Principal> for Subaccount {
fn from(principal: Principal) -> Self {
let mut bytes = [0u8; 32];
let p = principal.as_slice();
// Defensive check: Principals are currently <= 29 bytes
let len = p.len().min(31); // reserve 1 byte for the length prefix
bytes[0] = len as u8;
// Copy safely without panic risk
bytes[1..=len].copy_from_slice(&p[..len]);
Self(bytes)
}
}