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};
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}
55
56impl<N: Network, Private: Visibility> Record<N, Private> {
57    pub fn from_plaintext(
59        owner: Owner<N, Plaintext<N>>,
60        data: IndexMap<Identifier<N>, Entry<N, Plaintext<N>>>,
61        nonce: Group<N>,
62    ) -> Result<Record<N, Plaintext<N>>> {
63        let reserved = [Identifier::from_str("owner")?];
64        ensure!(!has_duplicates(data.keys().chain(reserved.iter())), "Found a duplicate entry name in a record");
66        ensure!(data.len() <= N::MAX_DATA_ENTRIES, "Found a record that exceeds size ({})", data.len());
68        Ok(Record { owner, data, nonce })
70    }
71
72    pub fn from_ciphertext(
74        owner: Owner<N, Ciphertext<N>>,
75        data: IndexMap<Identifier<N>, Entry<N, Ciphertext<N>>>,
76        nonce: Group<N>,
77    ) -> Result<Record<N, Ciphertext<N>>> {
78        let reserved = [Identifier::from_str("owner")?];
79        ensure!(!has_duplicates(data.keys().chain(reserved.iter())), "Found a duplicate entry name in a record");
81        ensure!(data.len() <= N::MAX_DATA_ENTRIES, "Found a record that exceeds size ({})", data.len());
83        Ok(Record { owner, data, nonce })
85    }
86}
87
88impl<N: Network, Private: Visibility> Record<N, Private> {
89    pub const fn owner(&self) -> &Owner<N, Private> {
91        &self.owner
92    }
93
94    pub const fn data(&self) -> &IndexMap<Identifier<N>, Entry<N, Private>> {
96        &self.data
97    }
98
99    pub const fn nonce(&self) -> &Group<N> {
101        &self.nonce
102    }
103}
104
105impl<N: Network, Private: Visibility> Record<N, Private> {
106    pub fn into_owner(self) -> Owner<N, Private> {
108        self.owner
109    }
110
111    pub fn into_data(self) -> IndexMap<Identifier<N>, Entry<N, Private>> {
113        self.data
114    }
115
116    pub fn into_nonce(self) -> Group<N> {
118        self.nonce
119    }
120}