Skip to content

Commit 9ea894d

Browse files
committed
inspect: Add support for transparent secret key encoding
This enables us to obtain the pubkey encoding from a `zcashd` wallet export, which is useful for testing `pczt create-manual`.
1 parent 0fa1e9c commit 9ea894d

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ rust_decimal = "1"
6161
bech32 = "0.11"
6262
bellman = "0.14"
6363
blake2b_simd = "1"
64+
bs58 = "0.5"
6465
ed25519-zebra = "4"
6566
equihash = "0.2"
6667
group = "0.13"

src/commands/inspect.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ impl Command {
9595
process::exit(2);
9696
}
9797
}
98+
} else if let Ok(decoded) = bs58::decode(&opts.data).with_check(None).into_vec() {
99+
match decoded.as_slice() {
100+
// `constants::mainnet::B58_TRANSPARENT_SECRET_KEY_PREFIX`
101+
[0x80, data @ ..] => keys::inspect_transparent_sk(data, NetworkType::Main),
102+
// `constants::testnet::B58_TRANSPARENT_SECRET_KEY_PREFIX`
103+
// (also regtest but we can't distinguish the two)
104+
[0xEF, data @ ..] => keys::inspect_transparent_sk(data, NetworkType::Test),
105+
_ => {
106+
// Unknown data format.
107+
eprintln!("String does not match known Zcash data formats.");
108+
process::exit(2);
109+
}
110+
}
98111
} else {
99112
// Unknown data format.
100113
eprintln!("String does not match known Zcash data formats.");

src/commands/inspect/keys.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ use zcash_address::{
1212
unified::{self, Encoding},
1313
ToAddress, ZcashAddress,
1414
};
15-
use zcash_keys::keys::{UnifiedAddressRequest, UnifiedFullViewingKey};
15+
use zcash_keys::{
16+
encoding::AddressCodec,
17+
keys::{UnifiedAddressRequest, UnifiedFullViewingKey},
18+
};
1619
use zcash_protocol::{
17-
consensus::{Network, NetworkConstants, NetworkType},
20+
consensus::{self, Network, NetworkConstants, NetworkType},
1821
local_consensus::LocalNetwork,
1922
};
2023

@@ -220,3 +223,46 @@ pub(crate) fn inspect_sapling_extsk(data: Vec<u8>, network: NetworkType) {
220223
eprintln!();
221224
eprintln!("WARNING: This spending key is now likely cached in your terminal's history buffer.");
222225
}
226+
227+
pub(crate) fn inspect_transparent_sk(data: &[u8], network: NetworkType) {
228+
let (data, compressed) = match data.split_last() {
229+
Some((b, rest)) if *b == 1 => (rest, true),
230+
_ => (data, false),
231+
};
232+
233+
match secp256k1::SecretKey::from_slice(data) {
234+
Err(_) => {
235+
eprintln!("Invalid encoding that claims to be a transparent secret key");
236+
}
237+
Ok(sk) => {
238+
eprintln!("Transparent secret key");
239+
240+
let pubkey = sk.public_key(&secp256k1::Secp256k1::signing_only());
241+
eprintln!(
242+
"- Public key: {}",
243+
if compressed {
244+
hex::encode(pubkey.serialize())
245+
} else {
246+
hex::encode(pubkey.serialize_uncompressed())
247+
}
248+
);
249+
250+
if compressed {
251+
let params = match network {
252+
NetworkType::Main => consensus::Network::MainNetwork,
253+
NetworkType::Test => consensus::Network::TestNetwork,
254+
NetworkType::Regtest => unreachable!(),
255+
};
256+
257+
// `TransparentAddress::from_pubkey` only supports compressed encodings.
258+
eprintln!(
259+
"- P2PKH address: {}",
260+
TransparentAddress::from_pubkey(&pubkey).encode(&params),
261+
);
262+
}
263+
}
264+
}
265+
266+
eprintln!();
267+
eprintln!("WARNING: This secret key is now likely cached in your terminal's history buffer.");
268+
}

0 commit comments

Comments
 (0)