Skip to content

Commit

Permalink
Merge pull request #86 from zcash/zconvi_demo
Browse files Browse the repository at this point in the history
Add QR code display to `list-addresses` and a `generate-address` command.
  • Loading branch information
nuttycom authored Mar 5, 2025
2 parents 8212aba + f9642fa commit 4d136c1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ tui-logger = { version = "0.14", optional = true, features = ["tracing-support"]

[features]
default = ["transparent-inputs"]
qr = ["dep:qrcode"]
pczt-qr = ["dep:image", "dep:minicbor", "dep:nokhwa", "dep:qrcode", "dep:rqrr", "dep:ur"]
transparent-inputs = [
"zcash_client_sqlite/transparent-inputs",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ on Youtube [here](https://www.youtube.com/watch?v=5gvQF5oFT8E)

[![Youtube preview of the ZconVI presentation Zcash-devtool: the Zcash development multitool](https://img.youtube.com/vi/5gvQF5oFT8E/0.jpg)](https://www.youtube.com/watch?v=5gvQF5oFT8E)

The code developed in this demo resulted in [this](https://github.com/zcash/zcash-devtool/pull/86) pull request.

## License

All code in this workspace is licensed under either of
Expand Down
4 changes: 4 additions & 0 deletions src/commands/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Subcommand;

pub(crate) mod balance;
pub(crate) mod enhance;
pub(crate) mod gen_addr;
pub(crate) mod import_ufvk;
pub(crate) mod init;
pub(crate) mod init_fvk;
Expand Down Expand Up @@ -45,6 +46,9 @@ pub(crate) enum Command {
/// List the accounts in the wallet
ListAccounts(list_accounts::Command),

/// Generate a new address for an account in the wallet
GenerateAddress(gen_addr::Command),

/// List the addresses for an account in the wallet
ListAddresses(list_addresses::Command),

Expand Down
53 changes: 53 additions & 0 deletions src/commands/wallet/gen_addr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use clap::Args;
use uuid::Uuid;
use zcash_client_backend::data_api::{Account, WalletWrite};
use zcash_client_sqlite::{util::SystemClock, WalletDb};
use zcash_keys::keys::UnifiedAddressRequest;

use crate::{commands::select_account, config::get_wallet_network, data::get_db_paths};

#[cfg(feature = "qr")]
use qrcode::{render::unicode, QrCode};

// Options accepted for the `list-addresses` command
#[derive(Debug, Args)]
pub(crate) struct Command {
/// The UUID of the account to list addresses for
account_id: Option<Uuid>,

/// A flag indicating whether a QR code should be displayed for the address.
#[cfg(feature = "qr")]
#[arg(long, default_value = "true")]
display_qr: bool,
}

impl Command {
pub(crate) fn run(self, wallet_dir: Option<String>) -> anyhow::Result<()> {
let params = get_wallet_network(wallet_dir.as_ref())?;
let (_, db_data) = get_db_paths(wallet_dir.as_ref());
let mut db_data = WalletDb::for_path(db_data, params, SystemClock)?;

let account = select_account(&db_data, self.account_id)?;

println!("Account {:?}", account.id());
let (ua, _) = db_data
.get_next_available_address(account.id(), UnifiedAddressRequest::AllAvailableKeys)?
.unwrap();
let ua_str = ua.encode(&params);
println!(" Address: {}", ua_str);

#[cfg(feature = "qr")]
if self.display_qr {
let code = QrCode::new(ua_str)?;
let ua_qr = code
.render::<unicode::Dense1x2>()
.dark_color(unicode::Dense1x2::Light)
.light_color(unicode::Dense1x2::Dark)
.quiet_zone(true)
.build();
println!("{}", ua_qr);
}

Ok(())
}
}
24 changes: 23 additions & 1 deletion src/commands/wallet/list_addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ use zcash_keys::keys::UnifiedAddressRequest;

use crate::{commands::select_account, config::get_wallet_network, data::get_db_paths};

#[cfg(feature = "qr")]
use qrcode::{render::unicode, QrCode};

// Options accepted for the `list-addresses` command
#[derive(Debug, Args)]
pub(crate) struct Command {
/// The UUID of the account to list addresses for
account_id: Option<Uuid>,

/// A flag indicating whether a QR code should be displayed for the address.
#[cfg(feature = "qr")]
#[arg(long, default_value = "true")]
display_qr: bool,
}

impl Command {
Expand All @@ -25,7 +33,21 @@ impl Command {
let (ua, _) = account
.uivk()
.default_address(UnifiedAddressRequest::AllAvailableKeys)?;
println!(" Default Address: {}", ua.encode(&params));
let ua_str = ua.encode(&params);
println!(" Default Address: {}", ua_str);

#[cfg(feature = "qr")]
if self.display_qr {
let code = QrCode::new(ua_str)?;
let ua_qr = code
.render::<unicode::Dense1x2>()
.dark_color(unicode::Dense1x2::Light)
.light_color(unicode::Dense1x2::Dark)
.quiet_zone(true)
.build();
println!("{}", ua_qr);
}

Ok(())
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ fn main() -> Result<(), anyhow::Error> {
commands::wallet::Command::Enhance(command) => command.run(wallet_dir).await,
commands::wallet::Command::Balance(command) => command.run(wallet_dir).await,
commands::wallet::Command::ListAccounts(command) => command.run(wallet_dir),
commands::wallet::Command::GenerateAddress(command) => command.run(wallet_dir),
commands::wallet::Command::ListAddresses(command) => command.run(wallet_dir),
commands::wallet::Command::ListTx(command) => command.run(wallet_dir),
commands::wallet::Command::ListUnspent(command) => command.run(wallet_dir),
Expand Down

0 comments on commit 4d136c1

Please sign in to comment.