Support import of Sapling full viewing keys.

This commit is contained in:
Kris Nuttycombe 2024-09-20 16:43:57 -06:00
parent e0140a8d22
commit 0baa279695
1 changed files with 19 additions and 9 deletions

View File

@ -6,7 +6,7 @@ use zcash_client_backend::{
data_api::{AccountPurpose, WalletWrite},
proto::service,
};
use zcash_keys::keys::UnifiedFullViewingKey;
use zcash_keys::{encoding::decode_extfvk_with_network, keys::UnifiedFullViewingKey};
use zcash_primitives::consensus::NetworkType;
use zcash_protocol::consensus;
@ -45,9 +45,9 @@ impl From<Purpose> for AccountPurpose {
#[derive(Debug, Options)]
pub(crate) struct Command {
#[options(
help = "serialized ufvk to initialize the wallet with (ignored if --phrase is provided)"
help = "serialized full viewing key (Unified or Sapling) to initialize the wallet with"
)]
ufvk: Option<String>,
fvk: String,
#[options(help = "the wallet's birthday (default is current chain height)")]
birthday: Option<u32>,
@ -73,12 +73,22 @@ impl Command {
pub(crate) async fn run(self, wallet_dir: Option<String>) -> Result<(), anyhow::Error> {
let opts = self;
let (network_type, ufvk) = Ufvk::decode(
&opts
.ufvk
.expect("wallet init uses a new seed if no ufvk is provided"),
)?;
let ufvk = UnifiedFullViewingKey::parse(&ufvk).map_err(|e| anyhow!("{e}"))?;
let (network_type, ufvk) = Ufvk::decode(&opts.fvk)
.map_err(anyhow::Error::new)
.and_then(
|(network, ufvk)| -> Result<(NetworkType, UnifiedFullViewingKey), anyhow::Error> {
let ufvk = UnifiedFullViewingKey::parse(&ufvk)?;
Ok((network, ufvk))
},
)
.or_else(
|_| -> Result<(NetworkType, UnifiedFullViewingKey), anyhow::Error> {
let (network, sfvk) = decode_extfvk_with_network(&opts.fvk)?;
let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(sfvk)?;
Ok((network, ufvk))
},
)?;
let network = match network_type {
NetworkType::Main => consensus::Network::MainNetwork,
NetworkType::Test => consensus::Network::TestNetwork,