pcli/command/query/
shielded_pool.rs

1use anyhow::Result;
2use colored_json::prelude::*;
3use penumbra_sdk_proto::DomainType;
4use penumbra_sdk_sct::{CommitmentSource, NullificationInfo, Nullifier};
5use penumbra_sdk_tct::StateCommitment;
6
7#[derive(Debug, clap::Subcommand)]
8pub enum ShieldedPool {
9    /// Queries the state commitment tree anchor for a given height.
10    Anchor {
11        /// The height to query.
12        height: u64,
13    },
14    /// Queries the source of a given commitment.
15    Commitment {
16        /// The commitment to query.
17        #[clap(parse(try_from_str = StateCommitment::parse_hex))]
18        commitment: StateCommitment,
19    },
20    /// Queries the note source of a given nullifier.
21    Nullifier {
22        /// The nullifier to query.
23        #[clap(parse(try_from_str = Nullifier::parse_hex))]
24        nullifier: Nullifier,
25    },
26    /// Queries the compact block at a given height.
27    CompactBlock { height: u64 },
28}
29
30impl ShieldedPool {
31    pub fn key(&self) -> String {
32        use penumbra_sdk_sct::state_key as sct_state_key;
33        match self {
34            ShieldedPool::Anchor { height } => sct_state_key::tree::anchor_by_height(*height),
35            ShieldedPool::CompactBlock { .. } => {
36                unreachable!("should be handled at outer level via rpc");
37            }
38            ShieldedPool::Commitment { commitment } => sct_state_key::tree::note_source(commitment),
39            ShieldedPool::Nullifier { nullifier } => {
40                sct_state_key::nullifier_set::spent_nullifier_lookup(nullifier)
41            }
42        }
43    }
44
45    pub fn display_value(&self, bytes: &[u8]) -> Result<()> {
46        let json = match self {
47            ShieldedPool::Anchor { .. } => {
48                let anchor = penumbra_sdk_tct::Root::decode(bytes)?;
49                serde_json::to_string_pretty(&anchor)?
50            }
51            ShieldedPool::CompactBlock { .. } => {
52                unreachable!("should be handled at outer level via rpc");
53            }
54            ShieldedPool::Commitment { .. } => {
55                let commitment_source = CommitmentSource::decode(bytes)?;
56                serde_json::to_string_pretty(&commitment_source)?
57            }
58            ShieldedPool::Nullifier { .. } => {
59                let note_source = NullificationInfo::decode(bytes)?;
60                serde_json::to_string_pretty(&note_source)?
61            }
62        };
63        println!("{}", json.to_colored_json_auto()?);
64        Ok(())
65    }
66}