diff --git a/zcash_primitives/src/zip32.rs b/zcash_primitives/src/zip32.rs index 9d3bb836c..66f6ceb99 100644 --- a/zcash_primitives/src/zip32.rs +++ b/zcash_primitives/src/zip32.rs @@ -134,6 +134,20 @@ impl Default for DiversifierIndex { } } +impl From for DiversifierIndex { + fn from(i: u32) -> Self { + u64::from(i).into() + } +} + +impl From for DiversifierIndex { + fn from(i: u64) -> Self { + let mut result = DiversifierIndex([0; 11]); + (&mut result.0[..8]).copy_from_slice(&i.to_le_bytes()); + result + } +} + impl DiversifierIndex { pub fn new() -> Self { DiversifierIndex([0; 11]) @@ -734,6 +748,20 @@ mod tests { assert_eq!(dk.diversifier_index(&Diversifier(d_3)), j_3); } + #[test] + fn diversifier_index_from() { + let di32: u32 = 0xa0b0c0d0; + assert_eq!( + DiversifierIndex::from(di32), + DiversifierIndex([0xd0, 0xc0, 0xb0, 0xa0, 0, 0, 0, 0, 0, 0, 0]) + ); + let di64: u64 = 0x0102030405060708; + assert_eq!( + DiversifierIndex::from(di64), + DiversifierIndex([8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0]) + ); + } + #[test] fn find_diversifier() { let dk = DiversifierKey([0; 32]);