drive/verify/address_funds/verify_address_info/mod.rs
1mod v0;
2
3use crate::drive::Drive;
4use crate::error::drive::DriveError;
5use crate::error::Error;
6use crate::verify::RootHash;
7use dpp::address_funds::PlatformAddress;
8use dpp::fee::Credits;
9use dpp::prelude::AddressNonce;
10use dpp::version::PlatformVersion;
11
12impl Drive {
13 /// Verifies the proof of a single address's balance and nonce information.
14 ///
15 /// This method validates and extracts a specific address's balance and nonce based on the provided proof.
16 /// It ensures the integrity and authenticity of the data associated with the specified address.
17 /// The method supports multiple versions for backward compatibility and forwards the verification logic
18 /// to the appropriate versioned implementation.
19 ///
20 /// # Arguments
21 /// - `proof`: A byte slice containing the cryptographic proof for the address information.
22 /// - `address`: The platform address to verify.
23 /// - `verify_subset_of_proof`: A boolean flag indicating whether to verify only a subset of the proof (useful for optimizations).
24 /// - `platform_version`: A reference to the platform version, used to determine the appropriate versioned implementation.
25 ///
26 /// # Returns
27 /// - `Ok((RootHash, Option<(AddressNonce, Credits)>))`: On success, returns a tuple containing:
28 /// - `RootHash`: The root hash of the Merkle tree, confirming the proof's validity.
29 /// - `Option<(AddressNonce, Credits)>`: The verified address balance and nonce if it exists, or `None` if the address is absent.
30 /// - `Err(Error)`: If verification fails, returns an [`Error`] indicating the cause of failure.
31 ///
32 /// # Errors
33 /// - [`Error::Proof`]: If the proof is invalid, corrupted, or contains unexpected data structures.
34 /// - [`Error::Drive(DriveError::UnknownVersionMismatch)`]: If the method is called with an unsupported platform version.
35 /// - [`Error::GroveDB`]: If the data deserialization or conversion fails during proof verification.
36 pub fn verify_address_info(
37 proof: &[u8],
38 address: &PlatformAddress,
39 verify_subset_of_proof: bool,
40 platform_version: &PlatformVersion,
41 ) -> Result<(RootHash, Option<(AddressNonce, Credits)>), Error> {
42 match platform_version
43 .drive
44 .methods
45 .verify
46 .address_funds
47 .verify_address_info
48 {
49 0 => Self::verify_address_info_v0(
50 proof,
51 address,
52 verify_subset_of_proof,
53 platform_version,
54 ),
55 version => Err(Error::Drive(DriveError::UnknownVersionMismatch {
56 method: "verify_address_info".to_string(),
57 known_versions: vec![0],
58 received: version,
59 })),
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67 use dpp::address_funds::PlatformAddress;
68 use dpp::version::PlatformVersion;
69
70 #[test]
71 fn test_verify_address_info_unknown_version_mismatch() {
72 let mut platform_version = PlatformVersion::latest().clone();
73 platform_version
74 .drive
75 .methods
76 .verify
77 .address_funds
78 .verify_address_info = 255;
79
80 let address = PlatformAddress::P2pkh([0u8; 20]);
81
82 let result = Drive::verify_address_info(&[], &address, false, &platform_version);
83
84 assert!(
85 matches!(
86 result,
87 Err(Error::Drive(DriveError::UnknownVersionMismatch { .. }))
88 ),
89 "expected UnknownVersionMismatch, got {:?}",
90 result,
91 );
92 }
93}