Skip to content

Commit

Permalink
Add a feature-gated ledger field in WalletCapability
Browse files Browse the repository at this point in the history
  • Loading branch information
pacu committed Nov 14, 2024
1 parent f962aa6 commit ea30b69
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions zingolib/src/wallet/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use zcash_primitives::{

pub mod legacy;
pub mod unified;
#[cfg(feature = "ledger-support")]
pub mod ledger;

/// Sha256(Sha256(value))
pub fn double_sha256(payload: &[u8]) -> Vec<u8> {
Expand Down
71 changes: 71 additions & 0 deletions zingolib/src/wallet/keys/ledger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
///
/// Holds information related to the ledger


use std::io;
use secp256k1::PublicKey as SecpPublicKey;
use tracing_subscriber::field::debug;
use crate::wallet::traits::ReadableWriteable;

/// Holds ledger things
#[derive(Debug)]
pub struct LedgerKeys {
ledger_id: SecpPublicKey,
_app: ZcashApp
}

/// Placeholder for the real thing
#[derive(Debug)]
pub struct ZcashApp {}

impl ZcashApp {
fn new() -> ZcashApp { ZcashApp { }}
}

impl ReadableWriteable for LedgerKeys {
const VERSION: u8 = 0; //not applicable

fn read<R: std::io::Read>(mut reader: R, _input: ()) -> std::io::Result<Self> {
// let version = Self::read_version(&mut reader)?;

// if version > Self::VERSION {
// let e = format!(
// "Don't know how to read ledger wallet version {}. Do you have the latest version?",
// version
// );
// return Err(io::Error::new(io::ErrorKind::InvalidData, e));
// }

//retrieve the ledger id and verify it matches with the aocnnected device
let ledger_id = {
let mut buf = [0; secp256k1::constants::PUBLIC_KEY_SIZE];
reader.read_exact(&mut buf)?;

SecpPublicKey::from_slice(&buf).map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Bad public key stored for ledger id: {:?}", e),
)
})?
};

// let app = Self::connect_ledger()?;
// // lets used futures simpler executor
// if ledger_id != futures::executor::block_on(Self::get_id(&app))? {
// return Err(io::Error::new(
// io::ErrorKind::InvalidInput,
// "Detected different ledger than used previously".to_string(),
// ));
// }
Ok(LedgerKeys { ledger_id, _app: ZcashApp::new()})

}

fn write<W: std::io::Write>(&self, mut writer: W, _input: ()) -> std::io::Result<()> {
let id = self.ledger_id.serialize();
writer.write_all(&id)?;

Ok(())
}
}
17 changes: 16 additions & 1 deletion zingolib/src/wallet/keys/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::{
wallet::data::new_rejection_address,
};

use super::ledger::LedgerKeys;
use super::legacy::{generate_transparent_address_from_legacy_key, legacy_sks_to_usk, Capability};
use super::ToBase58Check;

Expand Down Expand Up @@ -221,6 +222,9 @@ impl TryFrom<&UnifiedKeyStore> for zcash_primitives::legacy::keys::AccountPubKey
/// In addition to fundamental spending and viewing keys, the type caches generated addresses.
#[derive(Debug, Getters, Setters)]
pub struct WalletCapability {

#[cfg(feature = "ledger-support")]
ledger: Option<LedgerKeys>,
/// Unified key store
#[getset(get = "pub", set = "pub(crate)")]
unified_key_store: UnifiedKeyStore,
Expand All @@ -239,6 +243,8 @@ pub struct WalletCapability {
impl Default for WalletCapability {
fn default() -> Self {
Self {
#[cfg(feature = "ledger-support")]
ledger: None,
unified_key_store: UnifiedKeyStore::Empty,
transparent_child_addresses: Arc::new(AppendOnlyVec::new()),
rejection_addresses: Arc::new(AppendOnlyVec::new()),
Expand Down Expand Up @@ -626,7 +632,7 @@ impl WalletCapability {
}

impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
const VERSION: u8 = 4;
const VERSION: u8 = 5;

fn read<R: Read>(mut reader: R, input: ChainType) -> io::Result<Self> {
let version = Self::get_version(&mut reader)?;
Expand Down Expand Up @@ -763,6 +769,15 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
..Default::default()
}
}
5 => {
legacy_key = false;
length_of_rejection_addresses = reader.read_u32::<LittleEndian>()?;

Self {
unified_key_store: UnifiedKeyStore::read(&mut reader, input)?,
..Default::default()
}
}
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
Expand Down

0 comments on commit ea30b69

Please sign in to comment.