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
use crate::{account_format, AccountPrivateKey};
use snarkos_errors::objects::AccountError;
use snarkos_models::{algorithms::EncryptionScheme, dpc::DPCComponents};
use snarkos_utilities::{FromBytes, ToBytes};
use base58::{FromBase58, ToBase58};
use std::{
fmt,
io::{Read, Result as IoResult, Write},
str::FromStr,
};
#[derive(Derivative)]
#[derivative(
Default(bound = "C: DPCComponents"),
Clone(bound = "C: DPCComponents"),
PartialEq(bound = "C: DPCComponents"),
Eq(bound = "C: DPCComponents")
)]
pub struct AccountViewKey<C: DPCComponents> {
pub decryption_key: <C::AccountEncryption as EncryptionScheme>::PrivateKey,
}
impl<C: DPCComponents> AccountViewKey<C> {
pub fn from_private_key(
signature_parameters: &C::AccountSignature,
commitment_parameters: &C::AccountCommitment,
private_key: &AccountPrivateKey<C>,
) -> Result<Self, AccountError> {
let decryption_key = private_key.to_decryption_key(signature_parameters, commitment_parameters)?;
Ok(Self { decryption_key })
}
}
impl<C: DPCComponents> ToBytes for AccountViewKey<C> {
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.decryption_key.write(&mut writer)
}
}
impl<C: DPCComponents> FromBytes for AccountViewKey<C> {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let decryption_key = <C::AccountEncryption as EncryptionScheme>::PrivateKey::read(&mut reader)?;
Ok(Self { decryption_key })
}
}
impl<C: DPCComponents> fmt::Debug for AccountViewKey<C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "AccountViewKey {{ decryption_key: {:?} }}", self.decryption_key)
}
}
impl<C: DPCComponents> FromStr for AccountViewKey<C> {
type Err = AccountError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let data = s.from_base58()?;
if data.len() != 39 {
return Err(AccountError::InvalidByteLength(data.len()));
}
if &data[0..7] != account_format::VIEW_KEY_PREFIX {
return Err(AccountError::InvalidPrefixBytes(data[0..7].to_vec()));
}
let mut reader = &data[7..];
let decryption_key: <C::AccountEncryption as EncryptionScheme>::PrivateKey = FromBytes::read(&mut reader)?;
Ok(Self { decryption_key })
}
}
impl<C: DPCComponents> fmt::Display for AccountViewKey<C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut view_key = [0u8; 39];
let prefix = account_format::VIEW_KEY_PREFIX;
view_key[0..7].copy_from_slice(&prefix);
self.decryption_key
.write(&mut view_key[7..39])
.expect("decryption_key formatting failed");
write!(f, "{}", view_key.to_base58())
}
}