Seed

Struct Seed 

Source
pub struct Seed { /* private fields */ }
Expand description

A cryptographic seed for deterministic key generation.

A Seed is a source of entropy used to generate cryptographic keys in a deterministic manner. Unlike randomly generated keys, seed-derived keys can be recreated if you have the original seed, making them useful for backup and recovery scenarios.

This implementation of Seed includes the random seed data as well as optional metadata:

  • A name (for identifying the seed)
  • A note (for storing additional information)
  • A creation date

The minimum seed length is 16 bytes to ensure sufficient security and entropy.

§CBOR Serialization

Seed implements the CBORTaggedCodable trait, which means it can be serialized to and deserialized from CBOR with specific tags. The tags used are TAG_SEED and the older TAG_SEED_V1 for backward compatibility.

When serialized to CBOR, a Seed is represented as a map with the following keys:

  • 1: The seed data (required)
  • 2: The creation date (optional)
  • 3: The name (optional, omitted if empty)
  • 4: The note (optional, omitted if empty)

§UR Serialization

When serialized as a Uniform Resource (UR), a Seed is represented with the type “seed”.

§Key Derivation

A Seed implements the PrivateKeyDataProvider trait, which means it can be used as a source of entropy for deriving private keys in various cryptographic schemes.

§Examples

Creating a new random seed:

use bc_components::Seed;

// Create a new random seed with default length (16 bytes)
let seed = Seed::new();

Creating a seed with a specific length:

use bc_components::Seed;

// Create a seed with 32 bytes of entropy
let seed = Seed::new_with_len(32).unwrap();

Creating a seed with metadata:

use bc_components::Seed;
use dcbor::prelude::*;

// Create seed data
let data = vec![0u8; 16];

// Create a seed with name, note, and creation date
let mut seed = Seed::new_opt(
    data,
    Some("Wallet Backup".to_string()),
    Some("Cold storage backup for main wallet".to_string()),
    Some(Date::now()),
)
.unwrap();

// Modify metadata
seed.set_name("Updated Wallet Backup");

Implementations§

Source§

impl Seed

Source

pub const MIN_SEED_LENGTH: usize = 16usize

Source

pub fn new() -> Self

Create a new random seed.

The length of the seed will be 16 bytes.

Source

pub fn new_with_len(count: usize) -> Result<Self>

Create a new random seed with a specified length.

If the number of bytes is less than 16, this will return None.

Source

pub fn new_with_len_using( count: usize, rng: &mut impl RandomNumberGenerator, ) -> Result<Self>

Create a new random seed with a specified length.

If the number of bytes is less than 16, this will return None.

Source

pub fn new_opt( data: impl AsRef<[u8]>, name: Option<String>, note: Option<String>, creation_date: Option<Date>, ) -> Result<Self>

Create a new seed from the data and options.

If the data is less than 16 bytes, this will return None.

Source

pub fn as_bytes(&self) -> &[u8]

Return the data of the seed.

Source

pub fn name(&self) -> &str

Return the name of the seed.

Source

pub fn set_name(&mut self, name: &str)

Set the name of the seed.

Source

pub fn note(&self) -> &str

Return the note of the seed.

Source

pub fn set_note(&mut self, note: &str)

Set the note of the seed.

Source

pub fn creation_date(&self) -> &Option<Date>

Return the creation date of the seed.

Source

pub fn set_creation_date(&mut self, creation_date: Option<Date>)

Set the creation date of the seed.

Trait Implementations§

Source§

impl AsRef<[u8]> for Seed

Allows using a Seed as a reference to a byte slice.

Source§

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

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

impl AsRef<Seed> for Seed

Provides a self-reference, enabling API consistency with other types.

Source§

fn as_ref(&self) -> &Seed

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

impl CBORTagged for Seed

Identifies the CBOR tags used for Seed serialization, including the legacy tag.

Source§

fn cbor_tags() -> Vec<Tag>

Returns the CBOR tags associated with this type. Read more
Source§

impl CBORTaggedDecodable for Seed

Defines how a Seed is decoded from CBOR.

Source§

fn from_untagged_cbor(cbor: CBOR) -> Result<Self>

Creates an instance of this type by decoding it from untagged CBOR. Read more
Source§

fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>
where Self: Sized,

Creates an instance of this type by decoding it from tagged CBOR. Read more
Source§

fn from_tagged_cbor_data(data: impl AsRef<[u8]>) -> Result<Self, Error>
where Self: Sized,

Creates an instance of this type by decoding it from binary encoded tagged CBOR. Read more
Source§

fn from_untagged_cbor_data(data: impl AsRef<[u8]>) -> Result<Self, Error>
where Self: Sized,

Creates an instance of this type by decoding it from binary encoded untagged CBOR. Read more
Source§

impl CBORTaggedEncodable for Seed

Defines how a Seed is encoded as CBOR (as a map with data and metadata).

Source§

fn untagged_cbor(&self) -> CBOR

Returns the untagged CBOR encoding of this instance. Read more
Source§

fn tagged_cbor(&self) -> CBOR

Returns the tagged CBOR encoding of this instance. Read more
Source§

fn tagged_cbor_data(&self) -> Vec<u8>

Returns the tagged value in CBOR binary representation. Read more
Source§

impl Clone for Seed

Source§

fn clone(&self) -> Seed

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 Seed

Source§

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

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

impl Default for Seed

Provides a default implementation that creates a new random seed with the minimum length.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Seed> for CBOR

Enables conversion of a Seed into a tagged CBOR value.

Source§

fn from(value: Seed) -> Self

Converts to this type from the input type.
Source§

impl Hash for Seed

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Seed

Source§

fn eq(&self, other: &Seed) -> 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 PrivateKeyDataProvider for Seed

Implements PrivateKeyDataProvider to use seed data for key derivation.

Source§

fn private_key_data(&self) -> Vec<u8>

Returns unique data from which cryptographic keys can be derived. Read more
Source§

impl TryFrom<CBOR> for Seed

Enables conversion from CBOR to Seed, with proper error handling.

Source§

type Error = Error

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

fn try_from(cbor: CBOR) -> Result<Self>

Performs the conversion.
Source§

impl Eq for Seed

Source§

impl StructuralPartialEq for Seed

Auto Trait Implementations§

§

impl Freeze for Seed

§

impl RefUnwindSafe for Seed

§

impl Send for Seed

§

impl Sync for Seed

§

impl Unpin for Seed

§

impl UnwindSafe for Seed

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> CBORDecodable for T
where T: TryFrom<CBOR, Error = Error>,

Source§

fn try_from_cbor(cbor: &CBOR) -> Result<Self, Error>

Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. 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<T> URDecodable for T

Source§

fn from_ur(ur: impl AsRef<UR>) -> Result<Self, Error>
where Self: Sized,

Source§

fn from_ur_string(ur_string: impl Into<String>) -> Result<Self, Error>
where Self: Sized,

Source§

impl<T> UREncodable for T

Source§

fn ur(&self) -> UR

Returns the UR representation of the object.
Source§

fn ur_string(&self) -> String

Returns the UR string representation of the object.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> CBORCodable for T

Source§

impl<T> CBORTaggedCodable for T

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> URCodable for T