-
Notifications
You must be signed in to change notification settings - Fork 68
/
lib.rs
87 lines (73 loc) · 2.3 KB
/
lib.rs
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
//! HMAC-based Hierarchical Key Derivation: deterministically derive a
//! hierarchy of symmetric keys from initial keying material through
//! repeated applications of the Hash-based Message Authentication Code
//! (HMAC) construction.
//!
//! This library implements a fully symmetric construction inspired by
//! [BIP-0032: Hierarchical Deterministic Wallets][bip32].
//!
//! # Usage
//!
//! To derive a key using HKD32, you'll need the following:
//!
//! - [`KeyMaterial`]: a 32-byte (256-bit) uniformly random value
//! - [`Path`] or [`PathBuf`]: path to the child key
//!
//! Derivation paths can be raw bytestrings but also support a Unix path-like
//! syntax which can be parsed using the `String::parse` method:
//!
//! ```rust
//! let path = "/foo/bar/baz".parse::<hkd32::PathBuf>().unwrap();
//! ```
//!
//! # Example
//!
//! ```rust
//! use rand_core::OsRng;
//!
//! // Parent key
//! let input_key_material = hkd32::KeyMaterial::random(&mut OsRng);
//!
//! // Path to the child key
//! let derivation_path = "/foo/bar/baz".parse::<hkd32::PathBuf>().unwrap();
//!
//! // Derive subkey from the parent key. Call `as_bytes()` on this to obtain
//! // a byte slice containing the derived key.
//! let output_key_material = input_key_material.derive_subkey(derivation_path);
//! ```
//!
//! [bip32]: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
#[cfg(feature = "alloc")]
#[cfg_attr(any(feature = "bip39", test), macro_use)]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "mnemonic")]
pub mod mnemonic;
mod key_material;
mod path;
#[cfg(feature = "alloc")]
mod pathbuf;
pub use self::{key_material::*, path::*};
#[cfg(feature = "alloc")]
pub use self::pathbuf::PathBuf;
/// Delimiter used for strings containing paths
pub const DELIMITER: char = '/';
/// Size of input key material and derived keys.
///
/// Note: the name HKD32 is both a play on this size and "BIP32".
pub const KEY_SIZE: usize = 32;
/// Opaque error type
#[derive(Copy, Clone, Debug)]
pub struct Error;
#[cfg(feature = "std")]
impl std::error::Error for Error {}
use core::fmt;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Error")
}
}