Skip to content

Commit

Permalink
Migrate zcash-inspect to zcash-devtool inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Dec 20, 2024
1 parent db8bccc commit e67eef8
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 110 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ zcash_client_sqlite = { version = "0.14", features = ["unstable", "orchard", "se
zcash_keys = { version = "0.6", features = ["unstable", "orchard"] }
zcash_primitives = "0.21"
zcash_proofs = "0.21"
zcash_protocol = "0.4"
zcash_protocol = { version = "0.4", features = ["local-consensus"] }
zip32 = "0.1"
zip321 = "0.2"

Expand All @@ -52,6 +52,21 @@ chrono = "0.4"
iso_currency = { version = "0.5", features = ["with-serde"] }
rust_decimal = "1"

# Inspect
bech32 = "0.11"
bellman = "0.14"
blake2b_simd = "1"
ed25519-zebra = "4"
equihash = "0.2"
group = "0.13"
lazy_static = "1"
secp256k1 = "0.27"
serde_json = "1"
sha2 = "0.10"
uint = "0.9"
zcash_encoding = "0.2"
zcash_note_encryption = "0.4"

# PCZT QR codes
image = { version = "0.25", optional = true }
minicbor = { version = "0.19", optional = true }
Expand Down Expand Up @@ -82,13 +97,15 @@ tui = [
]

[patch.crates-io]
equihash = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "4fa6d3b549f8803016a309281404eab095d04de8" }
pczt = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
sapling = { package = "sapling-crypto", git = "https://github.com/zcash/sapling-crypto.git", rev = "3c2235747553da642fb142d1eeb9b1afa8391987" }
transparent = { package = "zcash_transparent", git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
zcash_address = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
zcash_client_backend = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
zcash_client_sqlite = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
zcash_encoding = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
zcash_keys = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
zcash_proofs = { git = "https://github.com/zcash/librustzcash.git", rev = "9c6d1b958bd015f3fc3f8d5e5815b2bfc54e484b" }
Expand Down
1 change: 1 addition & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use uuid::Uuid;
use zcash_client_backend::data_api::WalletRead;
use zcash_client_sqlite::AccountUuid;

pub(crate) mod inspect;
pub(crate) mod pczt;
pub(crate) mod wallet;

Expand Down
135 changes: 60 additions & 75 deletions src/commands/inspect/main.rs → src/commands/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::env;
use std::io;
use std::io::Cursor;
use std::process;

use bech32::primitives::decode::CheckedHrpstring;
use bech32::Bech32;
use gumdrop::{Options, ParsingStyle};
use clap::Args;
use lazy_static::lazy_static;
use secrecy::Zeroize;
use tokio::runtime::Runtime;
use zcash_address::{
unified::{self, Encoding},
ZcashAddress,
Expand Down Expand Up @@ -39,81 +37,68 @@ lazy_static! {
static ref ORCHARD_VK: orchard::circuit::VerifyingKey = orchard::circuit::VerifyingKey::build();
}

#[derive(Debug, Options)]
struct CliOptions {
#[options(help = "Print this help output")]
help: bool,

#[options(help = "Query information from the chain to help determine what the data is")]
#[derive(Debug, Args)]
pub(crate) struct Command {
/// Query information from the chain to help determine what the data is
#[arg(short, long)]
lookup: bool,

#[options(free, required, help = "String or hex-encoded bytes to inspect")]
/// String or hex-encoded bytes to inspect
data: String,

#[options(
free,
help = "JSON object with keys corresponding to requested context information"
)]
/// JSON object with keys corresponding to requested context information
context: Option<Context>,
}

fn main() {
let args = env::args().collect::<Vec<_>>();
let mut opts =
CliOptions::parse_args(&args[1..], ParsingStyle::default()).unwrap_or_else(|e| {
eprintln!("{}: {}", args[0], e);
process::exit(2);
});

if opts.help_requested() {
println!("Usage: {} data [context]", args[0]);
println!();
println!("{}", CliOptions::usage());
return;
}

if let Ok(mnemonic) = bip0039::Mnemonic::from_phrase(&opts.data) {
opts.data.zeroize();
keys::inspect_mnemonic(mnemonic, opts.context);
} else if let Ok(bytes) = hex::decode(&opts.data) {
inspect_bytes(bytes, opts.context, opts.lookup);
} else if let Ok(addr) = ZcashAddress::try_from_encoded(&opts.data) {
address::inspect(addr);
} else if let Ok((network, uivk)) = unified::Uivk::decode(&opts.data) {
keys::view::inspect_uivk(uivk, network);
} else if let Ok((network, ufvk)) = unified::Ufvk::decode(&opts.data) {
keys::view::inspect_ufvk(ufvk, network);
} else if let Ok(parsed) = CheckedHrpstring::new::<Bech32>(&opts.data) {
let data = parsed.byte_iter().collect::<Vec<_>>();
match parsed.hrp().as_str() {
constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => {
keys::view::inspect_sapling_extfvk(data, NetworkType::Main);
}
constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => {
keys::view::inspect_sapling_extfvk(data, NetworkType::Test);
}
constants::regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => {
keys::view::inspect_sapling_extfvk(data, NetworkType::Regtest);
}
constants::mainnet::HRP_SAPLING_EXTENDED_SPENDING_KEY => {
keys::inspect_sapling_extsk(data, NetworkType::Main);
}
constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY => {
keys::inspect_sapling_extsk(data, NetworkType::Test);
}
constants::regtest::HRP_SAPLING_EXTENDED_SPENDING_KEY => {
keys::inspect_sapling_extsk(data, NetworkType::Regtest);
}
_ => {
// Unknown data format.
eprintln!("String does not match known Zcash data formats.");
process::exit(2);
impl Command {
pub(crate) async fn run(self) -> Result<(), anyhow::Error> {
let mut opts = self;

if let Ok(mnemonic) = bip0039::Mnemonic::from_phrase(&opts.data) {
opts.data.zeroize();
keys::inspect_mnemonic(mnemonic, opts.context);
} else if let Ok(bytes) = hex::decode(&opts.data) {
inspect_bytes(bytes, opts.context, opts.lookup).await;
} else if let Ok(addr) = ZcashAddress::try_from_encoded(&opts.data) {
address::inspect(addr);
} else if let Ok((network, uivk)) = unified::Uivk::decode(&opts.data) {
keys::view::inspect_uivk(uivk, network);
} else if let Ok((network, ufvk)) = unified::Ufvk::decode(&opts.data) {
keys::view::inspect_ufvk(ufvk, network);
} else if let Ok(parsed) = CheckedHrpstring::new::<Bech32>(&opts.data) {
let data = parsed.byte_iter().collect::<Vec<_>>();
match parsed.hrp().as_str() {
constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => {
keys::view::inspect_sapling_extfvk(data, NetworkType::Main);
}
constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => {
keys::view::inspect_sapling_extfvk(data, NetworkType::Test);
}
constants::regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => {
keys::view::inspect_sapling_extfvk(data, NetworkType::Regtest);
}
constants::mainnet::HRP_SAPLING_EXTENDED_SPENDING_KEY => {
keys::inspect_sapling_extsk(data, NetworkType::Main);
}
constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY => {
keys::inspect_sapling_extsk(data, NetworkType::Test);
}
constants::regtest::HRP_SAPLING_EXTENDED_SPENDING_KEY => {
keys::inspect_sapling_extsk(data, NetworkType::Regtest);
}
_ => {
// Unknown data format.
eprintln!("String does not match known Zcash data formats.");
process::exit(2);
}
}
} else {
// Unknown data format.
eprintln!("String does not match known Zcash data formats.");
process::exit(2);
}
} else {
// Unknown data format.
eprintln!("String does not match known Zcash data formats.");
process::exit(2);

Ok(())
}
}

Expand All @@ -133,7 +118,7 @@ where
})
}

fn inspect_bytes(bytes: Vec<u8>, context: Option<Context>, lookup: bool) {
async fn inspect_bytes(bytes: Vec<u8>, context: Option<Context>, lookup: bool) {
if let Some(block) = complete(&bytes, |r| block::Block::read(r)) {
block::inspect(&block, context);
} else if let Some(header) = complete(&bytes, |r| BlockHeader::read(r)) {
Expand All @@ -145,7 +130,7 @@ fn inspect_bytes(bytes: Vec<u8>, context: Option<Context>, lookup: bool) {
} else {
// It's not a known variable-length format. check fixed-length data formats.
match bytes.len() {
32 => inspect_possible_hash(bytes.try_into().unwrap(), context, lookup),
32 => inspect_possible_hash(bytes.try_into().unwrap(), context, lookup).await,
64 => {
// Could be a signature
eprintln!("This is most likely a signature.");
Expand All @@ -158,7 +143,7 @@ fn inspect_bytes(bytes: Vec<u8>, context: Option<Context>, lookup: bool) {
}
}

fn inspect_possible_hash(bytes: [u8; 32], context: Option<Context>, lookup: bool) {
async fn inspect_possible_hash(bytes: [u8; 32], context: Option<Context>, lookup: bool) {
let maybe_mainnet_block_hash = bytes.iter().take(4).all(|c| c == &0);

if lookup {
Expand All @@ -167,8 +152,7 @@ fn inspect_possible_hash(bytes: [u8; 32], context: Option<Context>, lookup: bool
let mut candidate = bytes;
candidate.reverse();

let rt = Runtime::new().unwrap();
let found = rt.block_on(async {
let found = async {
match lookup::Lightwalletd::mainnet().await {
Err(e) => eprintln!("Error: Failed to connect to mainnet lightwalletd: {:?}", e),
Ok(mut mainnet) => {
Expand All @@ -190,7 +174,8 @@ fn inspect_possible_hash(bytes: [u8; 32], context: Option<Context>, lookup: bool
};

false
});
}
.await;

if found {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/inspect/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use zcash_primitives::{
transaction::Transaction,
};

use crate::{
use super::{
transaction::{extract_height_from_coinbase, is_coinbase},
Context, ZUint256,
};
Expand Down
Loading

0 comments on commit e67eef8

Please sign in to comment.