penumbra_keys/address/
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
use std::cmp::Ordering;

use serde::{Deserialize, Serialize};

use penumbra_proto::{penumbra::core::keys::v1 as pb, DomainType};

use crate::keys::{AddressIndex, WalletId};

use super::Address;

/// A view of a Penumbra address, either an opaque payment address or an address
/// with known structure.
///
/// This type allows working with addresses and address indexes without knowing
/// the corresponding FVK.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "pb::AddressView", into = "pb::AddressView")]
pub enum AddressView {
    Opaque {
        address: Address,
    },
    Decoded {
        address: Address,
        index: AddressIndex,
        wallet_id: WalletId,
    },
}

impl AddressView {
    pub fn address(&self) -> Address {
        match self {
            AddressView::Opaque { address } => address.clone(),
            AddressView::Decoded { address, .. } => address.clone(),
        }
    }
}

impl DomainType for AddressView {
    type Proto = pb::AddressView;
}

impl From<AddressView> for pb::AddressView {
    fn from(view: AddressView) -> Self {
        match view {
            AddressView::Opaque { address } => Self {
                address_view: Some(pb::address_view::AddressView::Opaque(
                    pb::address_view::Opaque {
                        address: Some(address.into()),
                    },
                )),
            },
            AddressView::Decoded {
                address,
                index,
                wallet_id,
            } => Self {
                address_view: Some(pb::address_view::AddressView::Decoded(
                    pb::address_view::Decoded {
                        address: Some(address.into()),
                        index: Some(index.into()),
                        wallet_id: Some(wallet_id.into()),
                    },
                )),
            },
        }
    }
}

impl TryFrom<pb::AddressView> for AddressView {
    type Error = anyhow::Error;
    fn try_from(value: pb::AddressView) -> Result<Self, Self::Error> {
        match value.address_view {
            Some(pb::address_view::AddressView::Opaque(opaque)) => {
                let address = opaque
                    .address
                    .ok_or_else(|| anyhow::anyhow!("AddressView::Opaque missing address field"))?
                    .try_into()?;
                Ok(AddressView::Opaque { address })
            }
            Some(pb::address_view::AddressView::Decoded(visible)) => {
                let address = visible
                    .address
                    .ok_or_else(|| anyhow::anyhow!("AddressView::Visible missing address field"))?
                    .try_into()?;
                let index = visible
                    .index
                    .ok_or_else(|| anyhow::anyhow!("AddressView::Visible missing index field"))?
                    .try_into()?;
                let wallet_id = visible
                    .wallet_id
                    .ok_or_else(|| anyhow::anyhow!("AddressView::Visible missing wallet_id field"))?
                    .try_into()?;
                Ok(AddressView::Decoded {
                    address,
                    index,
                    wallet_id,
                })
            }
            None => Err(anyhow::anyhow!("AddressView missing address_view field")),
        }
    }
}

// Canonical ordering for serialization
impl PartialOrd for AddressView {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        use AddressView::*;
        // Opaque < Decoded
        match (self, other) {
            (Opaque { .. }, Decoded { .. }) => Some(Ordering::Less),
            (Decoded { .. }, Opaque { .. }) => Some(Ordering::Greater),
            (Opaque { address: a1 }, Opaque { address: a2 }) => a1.partial_cmp(a2),
            (
                Decoded {
                    address: a1,
                    index: i1,
                    wallet_id: w1,
                },
                Decoded {
                    address: a2,
                    index: i2,
                    wallet_id: w2,
                },
            ) => (a1, i1, w1).partial_cmp(&(a2, i2, w2)),
        }
    }
}

impl Ord for AddressView {
    fn cmp(&self, other: &Self) -> Ordering {
        // Opaque < Decoded
        match (self, other) {
            (AddressView::Opaque { address: a1 }, AddressView::Opaque { address: a2 }) => {
                a1.cmp(a2)
            }
            (
                AddressView::Decoded {
                    address: a1,
                    index: i1,
                    wallet_id: w1,
                },
                AddressView::Decoded {
                    address: a2,
                    index: i2,
                    wallet_id: w2,
                },
            ) => match a1.cmp(a2) {
                Ordering::Equal => match i1.cmp(i2) {
                    Ordering::Equal => w1.cmp(w2),
                    ord => ord,
                },
                ord => ord,
            },
            (
                AddressView::Opaque { address: _ },
                AddressView::Decoded {
                    address: _,
                    index: _,
                    wallet_id: _,
                },
            ) => Ordering::Less,
            (
                AddressView::Decoded {
                    address: _,
                    index: _,
                    wallet_id: _,
                },
                AddressView::Opaque { address: _ },
            ) => Ordering::Greater,
        }
    }
}

#[cfg(test)]
mod tests {
    use rand_core::OsRng;

    use crate::keys::{Bip44Path, SeedPhrase, SpendKey};

    use super::*;

    #[test]
    fn address_view_basic() {
        let sk1 = SpendKey::from_seed_phrase_bip44(SeedPhrase::generate(OsRng), &Bip44Path::new(0));
        let sk2 = SpendKey::from_seed_phrase_bip44(SeedPhrase::generate(OsRng), &Bip44Path::new(0));

        let fvk1 = sk1.full_viewing_key();
        let fvk2 = sk2.full_viewing_key();

        let addr1_0 = fvk1.payment_address(0.into()).0;
        let addr1_1 = fvk1.payment_address(1.into()).0;
        let addr2_0 = fvk2.payment_address(0.into()).0;
        let addr2_1 = fvk2.payment_address(1.into()).0;

        assert_eq!(
            fvk1.view_address(addr1_0.clone()),
            AddressView::Decoded {
                address: addr1_0.clone(),
                index: 0.into(),
                wallet_id: fvk1.wallet_id(),
            }
        );
        assert_eq!(
            fvk2.view_address(addr1_0.clone()),
            AddressView::Opaque {
                address: addr1_0.clone()
            }
        );
        assert_eq!(
            fvk1.view_address(addr1_1.clone()),
            AddressView::Decoded {
                address: addr1_1.clone(),
                index: 1.into(),
                wallet_id: fvk1.wallet_id(),
            }
        );
        assert_eq!(
            fvk2.view_address(addr1_1.clone()),
            AddressView::Opaque {
                address: addr1_1.clone()
            }
        );
        assert_eq!(
            fvk1.view_address(addr2_0.clone()),
            AddressView::Opaque {
                address: addr2_0.clone()
            }
        );
        assert_eq!(
            fvk2.view_address(addr2_0.clone()),
            AddressView::Decoded {
                address: addr2_0.clone(),
                index: 0.into(),
                wallet_id: fvk2.wallet_id(),
            }
        );
        assert_eq!(
            fvk1.view_address(addr2_1.clone()),
            AddressView::Opaque {
                address: addr2_1.clone()
            }
        );
        assert_eq!(
            fvk2.view_address(addr2_1.clone()),
            AddressView::Decoded {
                address: addr2_1.clone(),
                index: 1.into(),
                wallet_id: fvk2.wallet_id(),
            }
        );
    }
}