drive/verify/single_document/verify_proof/
mod.rs

1mod v0;
2
3use crate::verify::RootHash;
4
5use crate::error::Error;
6use crate::query::SingleDocumentDriveQuery;
7use dpp::data_contract::document_type::DocumentTypeRef;
8use dpp::document::Document;
9
10use crate::error::drive::DriveError;
11
12use dpp::version::PlatformVersion;
13
14impl SingleDocumentDriveQuery {
15    /// Verifies the proof of a single document query.
16    ///
17    /// `is_subset` indicates if the function should verify a subset of a larger proof.
18    ///
19    /// # Parameters
20    ///
21    /// - `is_subset`: A boolean indicating whether to verify a subset of a larger proof.
22    /// - `proof`: A byte slice representing the proof to be verified.
23    /// - `document_type`: The type of the document being verified.
24    /// - `platform_version`: The platform version against which to verify the proof.
25    ///
26    /// # Returns
27    ///
28    /// Returns a `Result` with a tuple of `RootHash` and `Option<Document>`. The `Option<Document>`
29    /// represents the deserialized document if it exists.
30    ///
31    /// # Errors
32    ///
33    /// Returns an `Error` if:
34    ///
35    /// - An unknown or unsupported platform version is provided.
36    /// - Any other error as documented in the specific versioned function.
37    pub fn verify_proof(
38        &self,
39        is_subset: bool,
40        proof: &[u8],
41        document_type: DocumentTypeRef,
42        platform_version: &PlatformVersion,
43    ) -> Result<(RootHash, Option<Document>), Error> {
44        match platform_version
45            .drive
46            .methods
47            .verify
48            .single_document
49            .verify_proof
50        {
51            0 => self.verify_proof_v0(is_subset, proof, document_type, platform_version),
52            version => Err(Error::Drive(DriveError::UnknownVersionMismatch {
53                method: "SingleDocumentDriveQuery::verify_proof".to_string(),
54                known_versions: vec![0],
55                received: version,
56            })),
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use crate::error::drive::DriveError;
65    use crate::query::SingleDocumentDriveQueryContestedStatus;
66    use dpp::data_contract::accessors::v0::DataContractV0Getters;
67    use dpp::data_contracts::SystemDataContract;
68    use dpp::system_data_contracts::load_system_data_contract;
69
70    #[test]
71    fn test_single_document_verify_proof_unknown_version() {
72        let platform_version = PlatformVersion::latest();
73        let contract = load_system_data_contract(SystemDataContract::DPNS, platform_version)
74            .expect("expected to load DPNS contract");
75        let document_type = contract
76            .document_type_for_name("domain")
77            .expect("expected domain document type");
78
79        let mut platform_version = platform_version.clone();
80        platform_version
81            .drive
82            .methods
83            .verify
84            .single_document
85            .verify_proof = 255;
86
87        let query = SingleDocumentDriveQuery {
88            contract_id: [0u8; 32],
89            document_type_name: "domain".to_string(),
90            document_type_keeps_history: false,
91            document_id: [0u8; 32],
92            block_time_ms: None,
93            contested_status: SingleDocumentDriveQueryContestedStatus::NotContested,
94        };
95
96        let result = query.verify_proof(false, &[], document_type, &platform_version);
97
98        assert!(
99            matches!(result, Err(Error::Drive(DriveError::UnknownVersionMismatch { method, known_versions, received }))
100                if method == "SingleDocumentDriveQuery::verify_proof" && known_versions == vec![0] && received == 255
101            )
102        );
103    }
104}