penumbra_dex/swap/
ciphertext.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use anyhow::Result;

use penumbra_keys::{keys::OutgoingViewingKey, PayloadKey};
use penumbra_shielded_pool::note;

use super::{SwapPlaintext, SWAP_CIPHERTEXT_BYTES, SWAP_LEN_BYTES};

#[derive(Debug, Clone)]
pub struct SwapCiphertext(pub [u8; SWAP_CIPHERTEXT_BYTES]);

impl SwapCiphertext {
    pub fn decrypt(
        &self,
        ovk: &OutgoingViewingKey,
        commitment: note::StateCommitment,
    ) -> Result<SwapPlaintext> {
        let payload_key = PayloadKey::derive_swap(ovk, commitment);
        self.decrypt_with_payload_key(&payload_key)
    }

    pub fn decrypt_with_payload_key(&self, payload_key: &PayloadKey) -> Result<SwapPlaintext> {
        let swap_ciphertext = self.0;
        let decryption_result = payload_key
            .decrypt_swap(swap_ciphertext.to_vec())
            .map_err(|_| anyhow::anyhow!("unable to decrypt swap ciphertext"))?;

        // TODO: encapsulate plaintext encoding by making this a
        // pub(super) parse_decryption method on SwapPlaintext
        // and removing the TryFrom impls
        let plaintext: [u8; SWAP_LEN_BYTES] = decryption_result
            .try_into()
            .map_err(|_| anyhow::anyhow!("swap decryption result did not fit in plaintext len"))?;

        plaintext.try_into().map_err(|_| {
            anyhow::anyhow!("unable to convert swap plaintext bytes into SwapPlaintext")
        })
    }
}

impl TryFrom<[u8; SWAP_CIPHERTEXT_BYTES]> for SwapCiphertext {
    type Error = anyhow::Error;

    fn try_from(bytes: [u8; SWAP_CIPHERTEXT_BYTES]) -> Result<SwapCiphertext, Self::Error> {
        Ok(SwapCiphertext(bytes))
    }
}

impl TryFrom<&[u8]> for SwapCiphertext {
    type Error = anyhow::Error;

    fn try_from(slice: &[u8]) -> Result<SwapCiphertext, Self::Error> {
        Ok(SwapCiphertext(slice[..].try_into()?))
    }
}