ChainCode

Struct ChainCode 

Source
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

Source

pub const LENGTH: usize = 32usize

The length of a chain code in bytes.

Source

pub fn new(bytes: [u8; 32]) -> Self

Creates a new ChainCode from a 32-byte array.

§Arguments
  • bytes - A 32-byte array containing the chain code data
§Examples
use khodpay_bip32::ChainCode;

let bytes = [0u8; 32];
let chain_code = ChainCode::new(bytes);
assert_eq!(chain_code.as_bytes(), &bytes);
Source

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());
Source

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);
Source

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());
Source

pub fn len(&self) -> usize

Returns the length of the chain code (always 32).

§Examples
use khodpay_bip32::ChainCode;

let chain_code = ChainCode::new([0u8; 32]);
assert_eq!(chain_code.len(), 32);
Source

pub fn is_empty(&self) -> bool

Always returns false since chain codes have a fixed non-zero length.

This method exists for consistency with collection-like types.

§Examples
use khodpay_bip32::ChainCode;

let chain_code = ChainCode::new([0u8; 32]);
assert!(!chain_code.is_empty());

Trait Implementations§

Source§

impl AsRef<[u8]> for ChainCode

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for ChainCode

Source§

fn clone(&self) -> ChainCode

Returns a duplicate 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 ChainCode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for ChainCode

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<[u8; 32]> for ChainCode

Source§

fn from(bytes: [u8; 32]) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for ChainCode

Source§

fn eq(&self, other: &ChainCode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<&[u8]> for ChainCode

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(bytes: &[u8]) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<Vec<u8>> for ChainCode

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(bytes: Vec<u8>) -> Result<Self>

Performs the conversion.
Source§

impl Eq for ChainCode

Source§

impl StructuralPartialEq for ChainCode

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToHex for T
where T: AsRef<[u8]>,

Source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca)
Source§

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA)
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V