mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-07-30 20:11:23 +00:00
Merge pull request #128 from str4d/125-zip-32-seed-length
Panic if spending_key is given a seed shorter than 32 bytes
This commit is contained in:
@@ -5,6 +5,10 @@ use zcash_primitives::zip32::{ChildIndex, ExtendedSpendingKey};
|
|||||||
/// Derives the ZIP 32 [`ExtendedSpendingKey`] for a given coin type and account from the
|
/// Derives the ZIP 32 [`ExtendedSpendingKey`] for a given coin type and account from the
|
||||||
/// given seed.
|
/// given seed.
|
||||||
///
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if `seed` is shorter than 32 bytes.
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
@@ -13,6 +17,10 @@ use zcash_primitives::zip32::{ChildIndex, ExtendedSpendingKey};
|
|||||||
/// let extsk = spending_key(&[0; 32][..], COIN_TYPE, 0);
|
/// let extsk = spending_key(&[0; 32][..], COIN_TYPE, 0);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn spending_key(seed: &[u8], coin_type: u32, account: u32) -> ExtendedSpendingKey {
|
pub fn spending_key(seed: &[u8], coin_type: u32, account: u32) -> ExtendedSpendingKey {
|
||||||
|
if seed.len() < 32 {
|
||||||
|
panic!("ZIP 32 seeds MUST be at least 32 bytes");
|
||||||
|
}
|
||||||
|
|
||||||
ExtendedSpendingKey::from_path(
|
ExtendedSpendingKey::from_path(
|
||||||
&ExtendedSpendingKey::master(&seed),
|
&ExtendedSpendingKey::master(&seed),
|
||||||
&[
|
&[
|
||||||
@@ -22,3 +30,14 @@ pub fn spending_key(seed: &[u8], coin_type: u32, account: u32) -> ExtendedSpendi
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::spending_key;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn spending_key_panics_on_short_seed() {
|
||||||
|
let _ = spending_key(&[0; 31][..], 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user