pub struct ChainCode(/* private fields */);Expand description
A 32-byte chain code used in BIP32 hierarchical deterministic key derivation.
Chain codes provide additional entropy for deriving child keys. Every extended key (both private and public) has an associated chain code that is used in the HMAC-SHA512 operation during child key derivation.
§Security
Chain codes must be kept secret, similar to private keys. Exposing a chain code along with an extended public key can compromise the privacy of all child keys.
Memory Safety: This type implements ZeroizeOnDrop, which automatically
overwrites the chain code bytes with zeros when the value is dropped, preventing
sensitive data from lingering in memory.
§Size
Chain codes are always exactly 32 bytes (256 bits) in length.
§Examples
use khodpay_bip32::ChainCode;
// Create from a 32-byte array
let bytes = [0u8; 32];
let chain_code = ChainCode::from_bytes(&bytes)?;
// Access the underlying bytes
let bytes_ref: &[u8; 32] = chain_code.as_bytes();
assert_eq!(bytes_ref.len(), 32);Implementations§
Source§impl ChainCode
impl ChainCode
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Creates a ChainCode from a byte slice.
§Arguments
bytes- A byte slice that must be exactly 32 bytes long
§Errors
Returns Error::InvalidPrivateKey if the slice is not exactly 32 bytes.
§Examples
use khodpay_bip32::ChainCode;
// Valid 32-byte slice
let bytes = vec![0u8; 32];
let chain_code = ChainCode::from_bytes(&bytes)?;
// Invalid length
let invalid = vec![0u8; 16];
assert!(ChainCode::from_bytes(&invalid).is_err());Sourcepub fn as_bytes(&self) -> &[u8; 32]
pub fn as_bytes(&self) -> &[u8; 32]
Returns a reference to the chain code bytes as a 32-byte array.
§Examples
use khodpay_bip32::ChainCode;
let bytes = [42u8; 32];
let chain_code = ChainCode::new(bytes);
let bytes_ref: &[u8; 32] = chain_code.as_bytes();
assert_eq!(bytes_ref, &bytes);Sourcepub fn to_vec(&self) -> Vec<u8> ⓘ
pub fn to_vec(&self) -> Vec<u8> ⓘ
Converts the chain code to a Vec<u8>.
§Examples
use khodpay_bip32::ChainCode;
let bytes = [1u8; 32];
let chain_code = ChainCode::new(bytes);
let vec = chain_code.to_vec();
assert_eq!(vec.len(), 32);
assert_eq!(vec, bytes.to_vec());Trait Implementations§
impl Eq for ChainCode
impl StructuralPartialEq for ChainCode
Auto Trait Implementations§
impl Freeze for ChainCode
impl RefUnwindSafe for ChainCode
impl Send for ChainCode
impl Sync for ChainCode
impl Unpin for ChainCode
impl UnwindSafe for ChainCode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Upper case
letters are used (e.g. F9B4CA)