2023-03-06 02:52:00 -08:00
|
|
|
use crate::key2::split_key;
|
2022-10-30 03:03:38 -07:00
|
|
|
use bip39::{Language, Mnemonic, Seed};
|
|
|
|
use orchard::keys::{FullViewingKey, Scope, SpendingKey};
|
2022-11-06 04:50:51 -08:00
|
|
|
use orchard::Address;
|
2022-10-28 23:40:05 -07:00
|
|
|
|
|
|
|
pub struct OrchardKeyBytes {
|
2022-11-17 01:13:51 -08:00
|
|
|
pub sk: Option<[u8; 32]>,
|
2022-10-28 23:40:05 -07:00
|
|
|
pub fvk: [u8; 96],
|
|
|
|
}
|
|
|
|
|
2022-10-30 03:03:38 -07:00
|
|
|
impl OrchardKeyBytes {
|
|
|
|
pub fn get_address(&self, index: usize) -> Address {
|
|
|
|
let fvk = FullViewingKey::from_bytes(&self.fvk).unwrap();
|
|
|
|
let address = fvk.address_at(index, Scope::External);
|
|
|
|
address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 23:40:05 -07:00
|
|
|
pub fn derive_orchard_keys(coin_type: u32, seed: &str, account_index: u32) -> OrchardKeyBytes {
|
2023-03-06 02:52:00 -08:00
|
|
|
let (phrase, password) = split_key(seed);
|
|
|
|
let mnemonic = Mnemonic::from_phrase(&phrase, Language::English).unwrap();
|
|
|
|
let seed = Seed::new(&mnemonic, &password);
|
2022-11-06 04:50:51 -08:00
|
|
|
let sk = SpendingKey::from_zip32_seed(seed.as_bytes(), coin_type, account_index).unwrap();
|
2022-10-28 23:40:05 -07:00
|
|
|
let fvk = FullViewingKey::from(&sk);
|
|
|
|
OrchardKeyBytes {
|
2022-11-17 01:13:51 -08:00
|
|
|
sk: Some(sk.to_bytes().clone()),
|
2022-11-06 04:50:51 -08:00
|
|
|
fvk: fvk.to_bytes(),
|
2022-10-28 23:40:05 -07:00
|
|
|
}
|
|
|
|
}
|