Skip to content

Commit

Permalink
Initialize wallet with unified spending key
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Sep 1, 2023
1 parent a180fe5 commit f0108a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion zingolib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ orchard = { workspace = true }
subtle = "2.4.1"
incrementalmerkletree = { workspace = true, features = ["test-dependencies"] }
zcash_address = { workspace = true }
zcash_client_backend = { workspace = true }
zcash_client_backend = { workspace = true, features = ["unstable", "transparent-inputs"] }
zcash_encoding = { workspace = true }
zcash_note_encryption = { workspace = true }
zcash_primitives = { workspace = true }
Expand Down
14 changes: 14 additions & 0 deletions zingolib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ pub enum WalletBase {
SeedBytes([u8; 32]),
MnemonicPhrase(String),
Mnemonic(Mnemonic),
/// Unified full viewing key
Ufvk(String),
/// Unified spending key
Usk(Vec<u8>),
}
impl WalletBase {
pub fn from_string(base: String) -> WalletBase {
Expand Down Expand Up @@ -616,6 +619,17 @@ impl LightWallet {
})?;
(wc, None)
}
WalletBase::Usk(unified_spending_key) => {
let wc = WalletCapability::new_from_usk(unified_spending_key.as_slice()).map_err(
|e| {
Error::new(
ErrorKind::InvalidData,
format!("Error parsing unified spending key: {}", e),
)
},
)?;
(wc, None)
}
};

if let Err(e) = wc.new_address(wc.can_view()) {
Expand Down
14 changes: 14 additions & 0 deletions zingolib/src/wallet/keys/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use orchard::keys::Scope;

use zcash_address::unified::{Container, Encoding, Fvk, Ufvk};
use zcash_client_backend::address::UnifiedAddress;
use zcash_client_backend::keys::{Era, UnifiedSpendingKey};
use zcash_encoding::Vector;
use zcash_primitives::{
legacy::TransparentAddress, sapling::note_encryption::PreparedIncomingViewingKey,
Expand Down Expand Up @@ -352,6 +353,19 @@ impl WalletCapability {
Ok(Self::new_from_seed(config, &bip39_seed, position))
}

/// Creates a new `WalletCapability` from a unified spending key.
pub fn new_from_usk(usk: &[u8]) -> Result<Self, String> {
// Decode unified spending key
let usk = UnifiedSpendingKey::from_bytes(Era::Orchard, usk)
.map_err(|_| "Error decoding unified spending key.")?;
Ok(Self {
orchard: Capability::Spend(usk.orchard().to_owned()),
sapling: Capability::Spend(usk.sapling().to_owned()),
// transparent: Capability::Spend(usk.transparent()),
..Default::default()
})
}

pub fn new_from_ufvk(config: &ZingoConfig, ufvk_encoded: String) -> Result<Self, String> {
// Decode UFVK
if ufvk_encoded.starts_with(config.hrp_sapling_viewing_key()) {
Expand Down

0 comments on commit f0108a7

Please sign in to comment.