snarkvm_console_program/data/record/
mod.rs1mod entry;
17pub use entry::Entry;
18
19mod helpers;
20pub use helpers::Owner;
21
22mod bytes;
23mod decrypt;
24mod encrypt;
25mod equal;
26mod find;
27mod is_owner;
28mod num_randomizers;
29mod parse_ciphertext;
30mod parse_plaintext;
31mod serial_number;
32mod serialize;
33mod tag;
34mod to_bits;
35mod to_commitment;
36mod to_fields;
37
38use crate::{Access, Ciphertext, Identifier, Literal, Plaintext, ProgramID};
39use snarkvm_console_account::{Address, PrivateKey, ViewKey};
40use snarkvm_console_network::prelude::*;
41use snarkvm_console_types::{Boolean, Field, Group, Scalar, U8};
42
43use indexmap::IndexMap;
44
45#[derive(Clone)]
47pub struct Record<N: Network, Private: Visibility> {
48 owner: Owner<N, Private>,
50 data: IndexMap<Identifier<N>, Entry<N, Private>>,
52 nonce: Group<N>,
54 version: U8<N>,
58}
59
60impl<N: Network, Private: Visibility> Record<N, Private> {
61 pub fn from_plaintext(
63 owner: Owner<N, Plaintext<N>>,
64 data: IndexMap<Identifier<N>, Entry<N, Plaintext<N>>>,
65 nonce: Group<N>,
66 version: U8<N>,
67 ) -> Result<Record<N, Plaintext<N>>> {
68 let reserved = [Identifier::from_str("owner")?];
69 ensure!(!has_duplicates(data.keys().chain(reserved.iter())), "Found a duplicate entry name in a record");
71 ensure!(data.len() <= N::MAX_DATA_ENTRIES, "Found a record that exceeds size ({})", data.len());
73 Ok(Record { owner, data, nonce, version })
75 }
76
77 pub fn from_ciphertext(
79 owner: Owner<N, Ciphertext<N>>,
80 data: IndexMap<Identifier<N>, Entry<N, Ciphertext<N>>>,
81 nonce: Group<N>,
82 version: U8<N>,
83 ) -> Result<Record<N, Ciphertext<N>>> {
84 let reserved = [Identifier::from_str("owner")?];
85 ensure!(!has_duplicates(data.keys().chain(reserved.iter())), "Found a duplicate entry name in a record");
87 ensure!(data.len() <= N::MAX_DATA_ENTRIES, "Found a record that exceeds size ({})", data.len());
89 Ok(Record { owner, data, nonce, version })
91 }
92}
93
94impl<N: Network, Private: Visibility> Record<N, Private> {
95 pub const fn owner(&self) -> &Owner<N, Private> {
97 &self.owner
98 }
99
100 pub const fn data(&self) -> &IndexMap<Identifier<N>, Entry<N, Private>> {
102 &self.data
103 }
104
105 pub const fn nonce(&self) -> &Group<N> {
107 &self.nonce
108 }
109
110 pub const fn version(&self) -> &U8<N> {
112 &self.version
113 }
114
115 pub fn is_hiding(&self) -> bool {
117 !self.version.is_zero()
118 }
119}
120
121impl<N: Network, Private: Visibility> Record<N, Private> {
122 pub fn into_owner(self) -> Owner<N, Private> {
124 self.owner
125 }
126
127 pub fn into_data(self) -> IndexMap<Identifier<N>, Entry<N, Private>> {
129 self.data
130 }
131
132 pub fn into_nonce(self) -> Group<N> {
134 self.nonce
135 }
136}