penumbra_transaction/
view.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
use anyhow::Context;
use decaf377_rdsa::{Binding, Signature};
use penumbra_asset::{Balance, Value};
use penumbra_dex::{swap::SwapView, swap_claim::SwapClaimView};
use penumbra_keys::AddressView;
use penumbra_proto::{core::transaction::v1 as pbt, DomainType};
use penumbra_shielded_pool::{OutputView, SpendView};
use serde::{Deserialize, Serialize};

pub mod action_view;
mod transaction_perspective;

pub use action_view::ActionView;
use penumbra_tct as tct;
pub use transaction_perspective::TransactionPerspective;

use crate::{
    memo::MemoCiphertext,
    transaction::{TransactionEffect, TransactionSummary},
    Action, DetectionData, Transaction, TransactionBody, TransactionParameters,
};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(try_from = "pbt::TransactionView", into = "pbt::TransactionView")]
pub struct TransactionView {
    pub body_view: TransactionBodyView,
    pub binding_sig: Signature<Binding>,
    pub anchor: tct::Root,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(
    try_from = "pbt::TransactionBodyView",
    into = "pbt::TransactionBodyView"
)]
pub struct TransactionBodyView {
    pub action_views: Vec<ActionView>,
    pub transaction_parameters: TransactionParameters,
    pub detection_data: Option<DetectionData>,
    pub memo_view: Option<MemoView>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(try_from = "pbt::MemoView", into = "pbt::MemoView")]
#[allow(clippy::large_enum_variant)]
pub enum MemoView {
    Visible {
        plaintext: MemoPlaintextView,
        ciphertext: MemoCiphertext,
    },
    Opaque {
        ciphertext: MemoCiphertext,
    },
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(try_from = "pbt::MemoPlaintextView", into = "pbt::MemoPlaintextView")]
pub struct MemoPlaintextView {
    pub return_address: AddressView,
    pub text: String,
}

impl TransactionView {
    pub fn transaction(&self) -> Transaction {
        let mut actions = Vec::new();

        for action_view in &self.body_view.action_views {
            actions.push(Action::from(action_view.clone()));
        }

        let memo_ciphertext = match &self.body_view.memo_view {
            Some(memo_view) => match memo_view {
                MemoView::Visible {
                    plaintext: _,
                    ciphertext,
                } => Some(ciphertext),
                MemoView::Opaque { ciphertext } => Some(ciphertext),
            },
            None => None,
        };

        let transaction_parameters = self.body_view.transaction_parameters.clone();
        let detection_data = self.body_view.detection_data.clone();

        Transaction {
            transaction_body: TransactionBody {
                actions,
                transaction_parameters,
                detection_data,
                memo: memo_ciphertext.cloned(),
            },
            binding_sig: self.binding_sig,
            anchor: self.anchor,
        }
    }

    pub fn action_views(&self) -> impl Iterator<Item = &ActionView> {
        self.body_view.action_views.iter()
    }

    /// Acts as a higher-order translator that summarizes a TransactionSummary by consolidating
    /// effects for each unique address.
    fn accumulate_effects(summary: TransactionSummary) -> TransactionSummary {
        use std::collections::BTreeMap;
        let mut keyed_effects: BTreeMap<AddressView, Balance> = BTreeMap::new();
        for effect in summary.effects {
            *keyed_effects.entry(effect.address).or_default() += effect.balance;
        }
        TransactionSummary {
            effects: keyed_effects
                .into_iter()
                .map(|(address, balance)| TransactionEffect { address, balance })
                .collect(),
        }
    }

    /// Produces a TransactionSummary, iterating through each visible action and collecting the effects of the transaction.
    pub fn summary(&self) -> TransactionSummary {
        let mut effects = Vec::new();

        for action_view in &self.body_view.action_views {
            match action_view {
                ActionView::Spend(spend_view) => match spend_view {
                    SpendView::Visible { spend: _, note } => {
                        // Provided imbalance (+)
                        let balance = Balance::from(note.value.value());

                        let address = AddressView::Opaque {
                            address: note.address(),
                        };

                        effects.push(TransactionEffect { address, balance });
                    }
                    SpendView::Opaque { spend: _ } => continue,
                },
                ActionView::Output(output_view) => match output_view {
                    OutputView::Visible {
                        output: _,
                        note,
                        payload_key: _,
                    } => {
                        // Required imbalance (-)
                        let balance = -Balance::from(note.value.value());

                        let address = AddressView::Opaque {
                            address: note.address(),
                        };

                        effects.push(TransactionEffect { address, balance });
                    }
                    OutputView::Opaque { output: _ } => continue,
                },
                ActionView::Swap(swap_view) => match swap_view {
                    SwapView::Visible {
                        swap: _,
                        swap_plaintext,
                        output_1,
                        output_2: _,
                        claim_tx: _,
                        asset_1_metadata: _,
                        asset_2_metadata: _,
                        batch_swap_output_data: _,
                    } => {
                        let address = AddressView::Opaque {
                            address: output_1.clone().expect("sender address").address(),
                        };

                        let value_fee = Value {
                            amount: swap_plaintext.claim_fee.amount(),
                            asset_id: swap_plaintext.claim_fee.asset_id(),
                        };
                        let value_1 = Value {
                            amount: swap_plaintext.delta_1_i,
                            asset_id: swap_plaintext.trading_pair.asset_1(),
                        };
                        let value_2 = Value {
                            amount: swap_plaintext.delta_2_i,
                            asset_id: swap_plaintext.trading_pair.asset_2(),
                        };

                        // Required imbalance (-)
                        let mut balance = Balance::default();
                        balance -= value_1;
                        balance -= value_2;
                        balance -= value_fee;

                        effects.push(TransactionEffect { address, balance });
                    }
                    SwapView::Opaque {
                        swap: _,
                        batch_swap_output_data: _,
                        output_1: _,
                        output_2: _,
                        asset_1_metadata: _,
                        asset_2_metadata: _,
                    } => continue,
                },
                ActionView::SwapClaim(swap_claim_view) => match swap_claim_view {
                    SwapClaimView::Visible {
                        swap_claim,
                        output_1,
                        output_2: _,
                        swap_tx: _,
                    } => {
                        let address = AddressView::Opaque {
                            address: output_1.address(),
                        };

                        let value_fee = Value {
                            amount: swap_claim.body.fee.amount(),
                            asset_id: swap_claim.body.fee.asset_id(),
                        };

                        // Provided imbalance (+)
                        let mut balance = Balance::default();
                        balance += value_fee;

                        effects.push(TransactionEffect { address, balance });
                    }
                    SwapClaimView::Opaque { swap_claim: _ } => continue,
                },
                _ => {} // Fill in other action views as neccessary
            }
        }

        let summary = TransactionSummary { effects };

        Self::accumulate_effects(summary)
    }
}

impl DomainType for TransactionView {
    type Proto = pbt::TransactionView;
}

impl TryFrom<pbt::TransactionView> for TransactionView {
    type Error = anyhow::Error;

    fn try_from(v: pbt::TransactionView) -> Result<Self, Self::Error> {
        let binding_sig = v
            .binding_sig
            .ok_or_else(|| anyhow::anyhow!("transaction view missing binding signature"))?
            .try_into()
            .context("transaction binding signature malformed")?;

        let anchor = v
            .anchor
            .ok_or_else(|| anyhow::anyhow!("transaction view missing anchor"))?
            .try_into()
            .context("transaction anchor malformed")?;

        let body_view = v
            .body_view
            .ok_or_else(|| anyhow::anyhow!("transaction view missing body"))?
            .try_into()
            .context("transaction body malformed")?;

        Ok(Self {
            body_view,
            binding_sig,
            anchor,
        })
    }
}

impl TryFrom<pbt::TransactionBodyView> for TransactionBodyView {
    type Error = anyhow::Error;

    fn try_from(body_view: pbt::TransactionBodyView) -> Result<Self, Self::Error> {
        let mut action_views = Vec::<ActionView>::new();
        for av in body_view.action_views.clone() {
            action_views.push(av.try_into()?);
        }

        let memo_view: Option<MemoView> = match body_view.memo_view {
            Some(mv) => match mv.memo_view {
                Some(x) => match x {
                    pbt::memo_view::MemoView::Visible(v) => Some(MemoView::Visible {
                        plaintext: v
                            .plaintext
                            .ok_or_else(|| {
                                anyhow::anyhow!("transaction view memo missing memo plaintext")
                            })?
                            .try_into()?,
                        ciphertext: v
                            .ciphertext
                            .ok_or_else(|| {
                                anyhow::anyhow!("transaction view memo missing memo ciphertext")
                            })?
                            .try_into()?,
                    }),
                    pbt::memo_view::MemoView::Opaque(v) => Some(MemoView::Opaque {
                        ciphertext: v
                            .ciphertext
                            .ok_or_else(|| {
                                anyhow::anyhow!("transaction view memo missing memo ciphertext")
                            })?
                            .try_into()?,
                    }),
                },
                None => None,
            },
            None => None,
        };

        let transaction_parameters = body_view
            .transaction_parameters
            .ok_or_else(|| anyhow::anyhow!("transaction view missing transaction parameters view"))?
            .try_into()?;

        // Iterate through the detection_data vec, and convert each FMD clue.
        let fmd_clues = body_view
            .detection_data
            .map(|dd| {
                dd.fmd_clues
                    .into_iter()
                    .map(|fmd| fmd.try_into())
                    .collect::<Result<Vec<_>, _>>()
            })
            .transpose()?;

        let detection_data = fmd_clues.map(|fmd_clues| DetectionData { fmd_clues });

        Ok(TransactionBodyView {
            action_views,
            transaction_parameters,
            detection_data,
            memo_view,
        })
    }
}

impl From<TransactionView> for pbt::TransactionView {
    fn from(v: TransactionView) -> Self {
        Self {
            body_view: Some(v.body_view.into()),
            anchor: Some(v.anchor.into()),
            binding_sig: Some(v.binding_sig.into()),
        }
    }
}

impl From<TransactionBodyView> for pbt::TransactionBodyView {
    fn from(v: TransactionBodyView) -> Self {
        Self {
            action_views: v.action_views.into_iter().map(Into::into).collect(),
            transaction_parameters: Some(v.transaction_parameters.into()),
            detection_data: v.detection_data.map(Into::into),
            memo_view: v.memo_view.map(|m| m.into()),
        }
    }
}

impl From<MemoView> for pbt::MemoView {
    fn from(v: MemoView) -> Self {
        Self {
            memo_view: match v {
                MemoView::Visible {
                    plaintext,
                    ciphertext,
                } => Some(pbt::memo_view::MemoView::Visible(pbt::memo_view::Visible {
                    plaintext: Some(plaintext.into()),
                    ciphertext: Some(ciphertext.into()),
                })),
                MemoView::Opaque { ciphertext } => {
                    Some(pbt::memo_view::MemoView::Opaque(pbt::memo_view::Opaque {
                        ciphertext: Some(ciphertext.into()),
                    }))
                }
            },
        }
    }
}

impl TryFrom<pbt::MemoView> for MemoView {
    type Error = anyhow::Error;

    fn try_from(v: pbt::MemoView) -> Result<Self, Self::Error> {
        match v
            .memo_view
            .ok_or_else(|| anyhow::anyhow!("missing memo field"))?
        {
            pbt::memo_view::MemoView::Visible(x) => Ok(MemoView::Visible {
                plaintext: x
                    .plaintext
                    .ok_or_else(|| anyhow::anyhow!("missing plaintext field"))?
                    .try_into()?,
                ciphertext: x
                    .ciphertext
                    .ok_or_else(|| anyhow::anyhow!("missing ciphertext field"))?
                    .try_into()?,
            }),
            pbt::memo_view::MemoView::Opaque(x) => Ok(MemoView::Opaque {
                ciphertext: x
                    .ciphertext
                    .ok_or_else(|| anyhow::anyhow!("missing ciphertext field"))?
                    .try_into()?,
            }),
        }
    }
}

impl From<MemoPlaintextView> for pbt::MemoPlaintextView {
    fn from(v: MemoPlaintextView) -> Self {
        Self {
            return_address: Some(v.return_address.into()),
            text: v.text,
        }
    }
}

impl TryFrom<pbt::MemoPlaintextView> for MemoPlaintextView {
    type Error = anyhow::Error;

    fn try_from(v: pbt::MemoPlaintextView) -> Result<Self, Self::Error> {
        let sender: AddressView = v
            .return_address
            .ok_or_else(|| anyhow::anyhow!("memo plan missing memo plaintext"))?
            .try_into()
            .context("return address malformed")?;

        let text: String = v.text;

        Ok(Self {
            return_address: sender,
            text,
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use decaf377::Fr;
    use decaf377::{Element, Fq};
    use decaf377_rdsa::{Domain, VerificationKey};
    use penumbra_asset::{
        asset::{self, Cache, Id},
        balance::Commitment,
        STAKING_TOKEN_ASSET_ID,
    };
    use penumbra_dex::swap::proof::SwapProof;
    use penumbra_dex::swap::{SwapCiphertext, SwapPayload};
    use penumbra_dex::Swap;
    use penumbra_dex::{
        swap::{SwapPlaintext, SwapPlan},
        TradingPair,
    };
    use penumbra_fee::Fee;
    use penumbra_keys::keys::Bip44Path;
    use penumbra_keys::keys::{SeedPhrase, SpendKey};
    use penumbra_keys::{
        symmetric::{OvkWrappedKey, WrappedMemoKey},
        test_keys, Address, FullViewingKey, PayloadKey,
    };
    use penumbra_num::Amount;
    use penumbra_proof_params::GROTH16_PROOF_LENGTH_BYTES;
    use penumbra_sct::Nullifier;
    use penumbra_shielded_pool::Rseed;
    use penumbra_shielded_pool::{output, spend, Note, NoteView, OutputPlan, SpendPlan};
    use penumbra_tct::structure::Hash;
    use penumbra_tct::StateCommitment;
    use rand_core::OsRng;
    use std::ops::Deref;

    use crate::{
        plan::{CluePlan, DetectionDataPlan},
        view, ActionPlan, TransactionPlan,
    };

    #[cfg(test)]
    fn dummy_sig<D: Domain>() -> Signature<D> {
        Signature::from([0u8; 64])
    }

    #[cfg(test)]
    fn dummy_pk<D: Domain>() -> VerificationKey<D> {
        VerificationKey::try_from(Element::default().vartime_compress().0)
            .expect("creating a dummy verification key should work")
    }

    #[cfg(test)]
    fn dummy_commitment() -> Commitment {
        Commitment(Element::default())
    }

    #[cfg(test)]
    fn dummy_proof_spend() -> spend::SpendProof {
        spend::SpendProof::try_from(
            penumbra_proto::penumbra::core::component::shielded_pool::v1::ZkSpendProof {
                inner: vec![0u8; GROTH16_PROOF_LENGTH_BYTES],
            },
        )
        .expect("creating a dummy proof should work")
    }

    #[cfg(test)]
    fn dummy_proof_output() -> output::OutputProof {
        output::OutputProof::try_from(
            penumbra_proto::penumbra::core::component::shielded_pool::v1::ZkOutputProof {
                inner: vec![0u8; GROTH16_PROOF_LENGTH_BYTES],
            },
        )
        .expect("creating a dummy proof should work")
    }

    #[cfg(test)]
    fn dummy_proof_swap() -> SwapProof {
        SwapProof::try_from(
            penumbra_proto::penumbra::core::component::dex::v1::ZkSwapProof {
                inner: vec![0u8; GROTH16_PROOF_LENGTH_BYTES],
            },
        )
        .expect("creating a dummy proof should work")
    }

    #[cfg(test)]
    fn dummy_spend() -> spend::Spend {
        use penumbra_shielded_pool::EncryptedBackref;

        spend::Spend {
            body: spend::Body {
                balance_commitment: dummy_commitment(),
                nullifier: Nullifier(Fq::default()),
                rk: dummy_pk(),
                encrypted_backref: EncryptedBackref::dummy(),
            },
            auth_sig: dummy_sig(),
            proof: dummy_proof_spend(),
        }
    }

    #[cfg(test)]
    fn dummy_output() -> output::Output {
        output::Output {
            body: output::Body {
                note_payload: penumbra_shielded_pool::NotePayload {
                    note_commitment: penumbra_shielded_pool::note::StateCommitment(Fq::default()),
                    ephemeral_key: [0u8; 32]
                        .as_slice()
                        .try_into()
                        .expect("can create dummy ephemeral key"),
                    encrypted_note: penumbra_shielded_pool::NoteCiphertext([0u8; 176]),
                },
                balance_commitment: dummy_commitment(),
                ovk_wrapped_key: OvkWrappedKey([0u8; 48]),
                wrapped_memo_key: WrappedMemoKey([0u8; 48]),
            },
            proof: dummy_proof_output(),
        }
    }

    #[cfg(test)]
    fn dummy_swap_plaintext() -> SwapPlaintext {
        let seed_phrase = SeedPhrase::generate(OsRng);
        let sk_recipient = SpendKey::from_seed_phrase_bip44(seed_phrase, &Bip44Path::new(0));
        let fvk_recipient = sk_recipient.full_viewing_key();
        let ivk_recipient = fvk_recipient.incoming();
        let (claim_address, _dtk_d) = ivk_recipient.payment_address(0u32.into());

        let gm = asset::Cache::with_known_assets().get_unit("gm").unwrap();
        let gn = asset::Cache::with_known_assets().get_unit("gn").unwrap();
        let trading_pair = TradingPair::new(gm.id(), gn.id());

        let delta_1 = Amount::from(1u64);
        let delta_2 = Amount::from(0u64);
        let fee = Fee::default();

        let swap_plaintext = SwapPlaintext::new(
            &mut OsRng,
            trading_pair,
            delta_1,
            delta_2,
            fee,
            claim_address,
        );

        swap_plaintext
    }

    #[cfg(test)]
    fn dummy_swap() -> Swap {
        use penumbra_dex::swap::Body;

        let seed_phrase = SeedPhrase::generate(OsRng);
        let sk_recipient = SpendKey::from_seed_phrase_bip44(seed_phrase, &Bip44Path::new(0));
        let fvk_recipient = sk_recipient.full_viewing_key();
        let ivk_recipient = fvk_recipient.incoming();
        let (claim_address, _dtk_d) = ivk_recipient.payment_address(0u32.into());

        let gm = asset::Cache::with_known_assets().get_unit("gm").unwrap();
        let gn = asset::Cache::with_known_assets().get_unit("gn").unwrap();
        let trading_pair = TradingPair::new(gm.id(), gn.id());

        let delta_1 = Amount::from(1u64);
        let delta_2 = Amount::from(0u64);
        let fee = Fee::default();

        let swap_plaintext = SwapPlaintext::new(
            &mut OsRng,
            trading_pair,
            delta_1,
            delta_2,
            fee,
            claim_address,
        );

        let fee_blinding = Fr::from(0u64);
        let fee_commitment = swap_plaintext.claim_fee.commit(fee_blinding);

        let swap_payload = SwapPayload {
            encrypted_swap: SwapCiphertext([0u8; 272]),
            commitment: StateCommitment::try_from([0; 32]).expect("state commitment"),
        };

        Swap {
            body: Body {
                trading_pair: trading_pair,
                delta_1_i: delta_1,
                delta_2_i: delta_2,
                fee_commitment: fee_commitment,
                payload: swap_payload,
            },
            proof: dummy_proof_swap(),
        }
    }

    #[cfg(test)]
    fn dummy_note_view(
        address: Address,
        value: Value,
        cache: &Cache,
        fvk: &FullViewingKey,
    ) -> NoteView {
        let note = Note::from_parts(address, value, Rseed::generate(&mut OsRng))
            .expect("generate dummy note");

        NoteView {
            value: note.value().view_with_cache(cache),
            rseed: note.rseed(),
            address: fvk.view_address(note.address()),
        }
    }

    #[cfg(test)]
    fn convert_note(cache: &Cache, fvk: &FullViewingKey, note: &Note) -> NoteView {
        NoteView {
            value: note.value().view_with_cache(cache),
            rseed: note.rseed(),
            address: fvk.view_address(note.address()),
        }
    }

    #[cfg(test)]
    fn convert_action(
        cache: &Cache,
        fvk: &FullViewingKey,
        action: &ActionPlan,
    ) -> Option<ActionView> {
        use view::action_view::SpendView;

        match action {
            ActionPlan::Output(x) => Some(ActionView::Output(
                penumbra_shielded_pool::OutputView::Visible {
                    output: dummy_output(),
                    note: convert_note(cache, fvk, &x.output_note()),
                    payload_key: PayloadKey::from([0u8; 32]),
                },
            )),
            ActionPlan::Spend(x) => Some(ActionView::Spend(SpendView::Visible {
                spend: dummy_spend(),
                note: convert_note(cache, fvk, &x.note),
            })),
            ActionPlan::ValidatorDefinition(_) => None,
            ActionPlan::Swap(x) => Some(ActionView::Swap(SwapView::Visible {
                swap: dummy_swap(),
                swap_plaintext: dummy_swap_plaintext(),
                output_1: Some(dummy_note_view(
                    x.swap_plaintext.claim_address.clone(),
                    x.swap_plaintext.claim_fee.0,
                    cache,
                    fvk,
                )),
                output_2: None,
                claim_tx: None,
                asset_1_metadata: None,
                asset_2_metadata: None,
                batch_swap_output_data: None,
            })),
            ActionPlan::SwapClaim(_) => None,
            ActionPlan::ProposalSubmit(_) => None,
            ActionPlan::ProposalWithdraw(_) => None,
            ActionPlan::DelegatorVote(_) => None,
            ActionPlan::ValidatorVote(_) => None,
            ActionPlan::ProposalDepositClaim(_) => None,
            ActionPlan::PositionOpen(_) => None,
            ActionPlan::PositionClose(_) => None,
            ActionPlan::PositionWithdraw(_) => None,
            ActionPlan::Delegate(_) => None,
            ActionPlan::Undelegate(_) => None,
            ActionPlan::UndelegateClaim(_) => None,
            ActionPlan::Ics20Withdrawal(_) => None,
            ActionPlan::CommunityPoolSpend(_) => None,
            ActionPlan::CommunityPoolOutput(_) => None,
            ActionPlan::CommunityPoolDeposit(_) => None,
            ActionPlan::ActionDutchAuctionSchedule(_) => None,
            ActionPlan::ActionDutchAuctionEnd(_) => None,
            ActionPlan::ActionDutchAuctionWithdraw(_) => None,
            ActionPlan::IbcAction(_) => todo!(),
        }
    }

    #[test]
    fn test_internal_transfer_transaction_summary() {
        // Generate two notes controlled by the test address.
        let value = Value {
            amount: 100u64.into(),
            asset_id: *STAKING_TOKEN_ASSET_ID,
        };
        let note = Note::generate(&mut OsRng, &test_keys::ADDRESS_0, value);

        let value2 = Value {
            amount: 50u64.into(),
            asset_id: Id(Fq::rand(&mut OsRng)),
        };
        let note2 = Note::generate(&mut OsRng, &test_keys::ADDRESS_0, value2);

        let value3 = Value {
            amount: 75u64.into(),
            asset_id: *STAKING_TOKEN_ASSET_ID,
        };

        // Record that note in an SCT, where we can generate an auth path.
        let mut sct = tct::Tree::new();
        for _ in 0..5 {
            let random_note = Note::generate(&mut OsRng, &test_keys::ADDRESS_0, value);
            sct.insert(tct::Witness::Keep, random_note.commit())
                .unwrap();
        }
        sct.insert(tct::Witness::Keep, note.commit()).unwrap();
        sct.insert(tct::Witness::Keep, note2.commit()).unwrap();

        let auth_path = sct.witness(note.commit()).unwrap();
        let auth_path2 = sct.witness(note2.commit()).unwrap();

        // Add a single spend and output to the transaction plan such that the
        // transaction balances.
        let plan = TransactionPlan {
            transaction_parameters: TransactionParameters {
                expiry_height: 0,
                fee: Fee::default(),
                chain_id: "".into(),
            },
            actions: vec![
                SpendPlan::new(&mut OsRng, note, auth_path.position()).into(),
                SpendPlan::new(&mut OsRng, note2, auth_path2.position()).into(),
                OutputPlan::new(&mut OsRng, value3, test_keys::ADDRESS_1.deref().clone()).into(),
            ],
            detection_data: Some(DetectionDataPlan {
                clue_plans: vec![CluePlan::new(
                    &mut OsRng,
                    test_keys::ADDRESS_1.deref().clone(),
                    1.try_into().unwrap(),
                )],
            }),
            memo: None,
        };

        let transaction_view = TransactionView {
            anchor: penumbra_tct::Root(Hash::zero()),
            binding_sig: Signature::from([0u8; 64]),
            body_view: TransactionBodyView {
                action_views: plan
                    .actions
                    .iter()
                    .filter_map(|x| {
                        convert_action(&Cache::with_known_assets(), &test_keys::FULL_VIEWING_KEY, x)
                    })
                    .collect(),
                transaction_parameters: plan.transaction_parameters.clone(),
                detection_data: None,
                memo_view: None,
            },
        };

        let transaction_summary = TransactionView::summary(&transaction_view);

        assert_eq!(transaction_summary.effects.len(), 2);
    }

    #[test]
    fn test_swap_transaction_summary() {
        // Generate two notes controlled by the test address.
        let value = Value {
            amount: 100u64.into(),
            asset_id: *STAKING_TOKEN_ASSET_ID,
        };
        let note = Note::generate(&mut OsRng, &test_keys::ADDRESS_0, value);

        let value2 = Value {
            amount: 50u64.into(),
            asset_id: Id(Fq::rand(&mut OsRng)),
        };
        let note2 = Note::generate(&mut OsRng, &test_keys::ADDRESS_0, value2);

        let value3 = Value {
            amount: 75u64.into(),
            asset_id: *STAKING_TOKEN_ASSET_ID,
        };

        // Record that note in an SCT, where we can generate an auth path.
        let mut sct = tct::Tree::new();
        for _ in 0..5 {
            let random_note = Note::generate(&mut OsRng, &test_keys::ADDRESS_0, value);
            sct.insert(tct::Witness::Keep, random_note.commit())
                .unwrap();
        }
        sct.insert(tct::Witness::Keep, note.commit()).unwrap();
        sct.insert(tct::Witness::Keep, note2.commit()).unwrap();

        let auth_path = sct.witness(note.commit()).unwrap();
        let auth_path2 = sct.witness(note2.commit()).unwrap();

        let gm = asset::Cache::with_known_assets().get_unit("gm").unwrap();
        let gn = asset::Cache::with_known_assets().get_unit("gn").unwrap();
        let trading_pair = TradingPair::new(gm.id(), gn.id());

        let delta_1 = Amount::from(100_000u64);
        let delta_2 = Amount::from(0u64);
        let fee = Fee::default();
        let claim_address: Address = test_keys::ADDRESS_0.deref().clone();
        let plaintext = SwapPlaintext::new(
            &mut OsRng,
            trading_pair,
            delta_1,
            delta_2,
            fee,
            claim_address,
        );

        // Add a single spend and output to the transaction plan such that the
        // transaction balances.
        let plan = TransactionPlan {
            transaction_parameters: TransactionParameters {
                expiry_height: 0,
                fee: Fee::default(),
                chain_id: "".into(),
            },
            actions: vec![
                SpendPlan::new(&mut OsRng, note, auth_path.position()).into(),
                SpendPlan::new(&mut OsRng, note2, auth_path2.position()).into(),
                OutputPlan::new(&mut OsRng, value3, test_keys::ADDRESS_1.deref().clone()).into(),
                SwapPlan::new(&mut OsRng, plaintext.clone()).into(),
            ],
            detection_data: Some(DetectionDataPlan {
                clue_plans: vec![CluePlan::new(
                    &mut OsRng,
                    test_keys::ADDRESS_1.deref().clone(),
                    1.try_into().unwrap(),
                )],
            }),
            memo: None,
        };

        let transaction_view = TransactionView {
            anchor: penumbra_tct::Root(Hash::zero()),
            binding_sig: Signature::from([0u8; 64]),
            body_view: TransactionBodyView {
                action_views: plan
                    .actions
                    .iter()
                    .filter_map(|x| {
                        convert_action(&Cache::with_known_assets(), &test_keys::FULL_VIEWING_KEY, x)
                    })
                    .collect(),
                transaction_parameters: plan.transaction_parameters.clone(),
                detection_data: None,
                memo_view: None,
            },
        };

        let transaction_summary = TransactionView::summary(&transaction_view);

        assert_eq!(transaction_summary.effects.len(), 2);
    }
}