diff --git a/packages/dapi-grpc/build.rs b/packages/dapi-grpc/build.rs index 95c6a3682b6..4423d8f394c 100644 --- a/packages/dapi-grpc/build.rs +++ b/packages/dapi-grpc/build.rs @@ -63,7 +63,7 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig { // Derive features for versioned messages // // "GetConsensusParamsRequest" is excluded as this message does not support proofs - const VERSIONED_REQUESTS: [&str; 40] = [ + const VERSIONED_REQUESTS: [&str; 41] = [ "GetDataContractHistoryRequest", "GetDataContractRequest", "GetDataContractsRequest", @@ -75,6 +75,7 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig { "GetIdentityContractNonceRequest", "GetIdentityBalanceAndRevisionRequest", "GetIdentityBalanceRequest", + "GetIdentityByNonUniquePublicKeyHashRequest", "GetIdentityByPublicKeyHashRequest", "GetIdentityKeysRequest", "GetIdentityRequest", @@ -110,6 +111,9 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig { // - "GetConsensusParamsResponse" // - "GetStatusResponse" // + // The following responses are excluded as they need custom proof handling: + // - "GetIdentityByNonUniquePublicKeyHashResponse" + // // "GetEvonodesProposedEpochBlocksResponse" is used for 2 Requests const VERSIONED_RESPONSES: [&str; 39] = [ "GetDataContractHistoryResponse", diff --git a/packages/dapi-grpc/protos/platform/v0/platform.proto b/packages/dapi-grpc/protos/platform/v0/platform.proto index 905ef6c1d23..4d8ddd09363 100644 --- a/packages/dapi-grpc/protos/platform/v0/platform.proto +++ b/packages/dapi-grpc/protos/platform/v0/platform.proto @@ -33,6 +33,8 @@ service Platform { rpc getDocuments(GetDocumentsRequest) returns (GetDocumentsResponse); rpc getIdentityByPublicKeyHash(GetIdentityByPublicKeyHashRequest) returns (GetIdentityByPublicKeyHashResponse); + rpc getIdentityByNonUniquePublicKeyHash(GetIdentityByNonUniquePublicKeyHashRequest) + returns (GetIdentityByNonUniquePublicKeyHashResponse); rpc waitForStateTransitionResult(WaitForStateTransitionResultRequest) returns (WaitForStateTransitionResultResponse); rpc getConsensusParams(GetConsensusParamsRequest) @@ -620,6 +622,35 @@ message GetIdentityByPublicKeyHashResponse { oneof version { GetIdentityByPublicKeyHashResponseV0 v0 = 1; } } +message GetIdentityByNonUniquePublicKeyHashRequest { + message GetIdentityByNonUniquePublicKeyHashRequestV0 { + bytes public_key_hash = 1; + optional bytes start_after = 2; // Give one result after a previous result + bool prove = 3; + } + oneof version { GetIdentityByNonUniquePublicKeyHashRequestV0 v0 = 1; } +} + +message GetIdentityByNonUniquePublicKeyHashResponse { + message GetIdentityByNonUniquePublicKeyHashResponseV0 { + message IdentityResponse { + optional bytes identity = 1; + } + + message IdentityProvedResponse { + Proof grovedb_identity_public_key_hash_proof = 1; + optional bytes identity_proof_bytes = 2; // A hack, we return 2 proofs + } + oneof result { + IdentityResponse identity = 1; + IdentityProvedResponse proof = 2; + } + + ResponseMetadata metadata = 3; // Metadata about the blockchain state + } + oneof version { GetIdentityByNonUniquePublicKeyHashResponseV0 v0 = 1; } +} + message WaitForStateTransitionResultRequest { message WaitForStateTransitionResultRequestV0 { bytes state_transition_hash = 1; // The hash of the state transition to wait for diff --git a/packages/rs-dapi-client/src/transport/grpc.rs b/packages/rs-dapi-client/src/transport/grpc.rs index d1337142bdc..57d8c28866c 100644 --- a/packages/rs-dapi-client/src/transport/grpc.rs +++ b/packages/rs-dapi-client/src/transport/grpc.rs @@ -490,6 +490,15 @@ impl_transport_request_grpc!( get_status ); +// rpc getIdentityByNonUniquePublicKeyHash(GetIdentityByNonUniquePublicKeyHashRequest) returns (GetIdentityByNonUniquePublicKeyHashResponse); +impl_transport_request_grpc!( + platform_proto::GetIdentityByNonUniquePublicKeyHashRequest, + platform_proto::GetIdentityByNonUniquePublicKeyHashResponse, + PlatformGrpcClient, + RequestSettings::default(), + get_identity_by_non_unique_public_key_hash +); + // rpc getIdentityTokenBalances(GetIdentityTokenBalancesRequest) returns (GetIdentityTokenBalancesResponse); impl_transport_request_grpc!( platform_proto::GetIdentityTokenBalancesRequest, diff --git a/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/test/tokens.rs b/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/test/tokens.rs index 28147c64bea..89b46e2da16 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/test/tokens.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/test/tokens.rs @@ -1,8 +1,6 @@ -use crate::error::execution::ExecutionError; use crate::error::Error; use crate::platform_types::platform::Platform; use dpp::block::block_info::BlockInfo; -use dpp::dashcore::Network; use dpp::data_contract::associated_token::token_configuration::v0::TokenConfigurationV0; use dpp::data_contract::associated_token::token_configuration_convention::v0::TokenConfigurationConventionV0; use dpp::data_contract::associated_token::token_distribution_rules::v0::TokenDistributionRulesV0; @@ -110,6 +108,10 @@ impl Platform { transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result<(), Error> { + let mut rng = StdRng::seed_from_u64(0u64); + let non_unique_key = + IdentityPublicKey::random_voting_key_with_rng(11, &mut rng, platform_version)?; + for id in [IDENTITY_ID_1, IDENTITY_ID_2, IDENTITY_ID_3] { // Create identity without keys let mut identity = Identity::create_basic_identity(id, platform_version)?; @@ -117,7 +119,9 @@ impl Platform { // Generate keys let seed = id.to_buffer()[0]; let mut rng = StdRng::seed_from_u64(seed as u64); - let keys = IdentityPublicKey::main_keys_with_random_authentication_keys_with_private_keys_with_rng(3, &mut rng, platform_version)?; + let mut keys = IdentityPublicKey::main_keys_with_random_authentication_keys_with_private_keys_with_rng(3, &mut rng, platform_version)?; + // every identity has the same non-unique key + keys.push(non_unique_key.clone()); for (key, private_key) in keys.iter() { let private_key = hex::encode(private_key); diff --git a/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_non_unique_public_key_hash/mod.rs b/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_non_unique_public_key_hash/mod.rs new file mode 100644 index 00000000000..9a1c7c104d5 --- /dev/null +++ b/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_non_unique_public_key_hash/mod.rs @@ -0,0 +1,70 @@ +use crate::error::query::QueryError; +use crate::error::Error; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use crate::query::QueryValidationResult; +use dapi_grpc::platform::v0::get_identity_by_non_unique_public_key_hash_request::Version as RequestVersion; +use dapi_grpc::platform::v0::get_identity_by_non_unique_public_key_hash_response::Version as ResponseVersion; +use dapi_grpc::platform::v0::{ + GetIdentityByNonUniquePublicKeyHashRequest, GetIdentityByNonUniquePublicKeyHashResponse, +}; +use dpp::version::PlatformVersion; + +mod v0; + +impl Platform { + /// Querying of an identity by a public key hash + pub fn query_identity_by_non_unique_public_key_hash( + &self, + GetIdentityByNonUniquePublicKeyHashRequest { version }: GetIdentityByNonUniquePublicKeyHashRequest, + platform_state: &PlatformState, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let Some(version) = version else { + return Ok(QueryValidationResult::new_with_error( + QueryError::DecodingError( + "could not decode identity by public key non unique hash query".to_string(), + ), + )); + }; + // TODO why `identity_by_unique_public_key_hash`? + // Shouldn't we rename or add new field like `identity_by_non_unique_public_key_hash`? + let feature_version_bounds = &platform_version + .drive_abci + .query + .identity_based_queries + .identity_by_unique_public_key_hash; + + let feature_version = match &version { + RequestVersion::V0(_) => 0, + }; + + if !feature_version_bounds.check_version(feature_version) { + return Ok(QueryValidationResult::new_with_error( + QueryError::UnsupportedQueryVersion( + "identity_by_non_unique_public_key_hash".to_string(), + feature_version_bounds.min_version, + feature_version_bounds.max_version, + platform_version.protocol_version, + feature_version, + ), + )); + } + + match version { + RequestVersion::V0(request_v0) => { + let request = self.query_identity_by_non_unique_public_key_hash_v0( + request_v0, + platform_state, + platform_version, + )?; + + Ok( + request.map(|response_v0| GetIdentityByNonUniquePublicKeyHashResponse { + version: Some(ResponseVersion::V0(response_v0)), + }), + ) + } + } + } +} diff --git a/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_non_unique_public_key_hash/v0/mod.rs b/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_non_unique_public_key_hash/v0/mod.rs new file mode 100644 index 00000000000..f31423a2aea --- /dev/null +++ b/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_non_unique_public_key_hash/v0/mod.rs @@ -0,0 +1,219 @@ +use crate::error::query::QueryError; +use crate::error::Error; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use crate::query::QueryValidationResult; +use dapi_grpc::platform::v0::get_identity_by_non_unique_public_key_hash_request::GetIdentityByNonUniquePublicKeyHashRequestV0; +use dapi_grpc::platform::v0::get_identity_by_non_unique_public_key_hash_response::{ + get_identity_by_non_unique_public_key_hash_response_v0, GetIdentityByNonUniquePublicKeyHashResponseV0, +}; +use dapi_grpc::platform::v0::get_identity_by_non_unique_public_key_hash_response::get_identity_by_non_unique_public_key_hash_response_v0::{IdentityProvedResponse, IdentityResponse}; +use dpp::check_validation_result_with_data; +use dpp::platform_value::{Bytes20, Bytes32}; +use dpp::serialization::PlatformSerializable; +use dpp::validation::ValidationResult; +use dpp::version::PlatformVersion; + +impl Platform { + pub(super) fn query_identity_by_non_unique_public_key_hash_v0( + &self, + GetIdentityByNonUniquePublicKeyHashRequestV0 { + public_key_hash, + start_after, + prove, + }: GetIdentityByNonUniquePublicKeyHashRequestV0, + platform_state: &PlatformState, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let public_key_hash = + check_validation_result_with_data!(Bytes20::from_vec(public_key_hash) + .map(|bytes| bytes.0) + .map_err(|_| QueryError::InvalidArgument( + "public key hash must be 20 bytes long".to_string() + ))); + + let start_after = if let Some(start_after) = start_after { + Some(check_validation_result_with_data!(Bytes32::from_vec( + start_after + ) + .map(|bytes| bytes.0) + .map_err(|_| QueryError::InvalidArgument( + "start_after must be 32 bytes long identity ID".to_string() + )))) + } else { + None + }; + + let response = if prove { + let proof = self + .drive + .prove_full_identity_by_non_unique_public_key_hash( + public_key_hash, + start_after, + None, + platform_version, + )?; + + GetIdentityByNonUniquePublicKeyHashResponseV0 { + result: Some( + get_identity_by_non_unique_public_key_hash_response_v0::Result::Proof( + IdentityProvedResponse { + grovedb_identity_public_key_hash_proof: Some(self.response_proof_v0( + platform_state, + proof.identity_id_public_key_hash_proof, + )), + identity_proof_bytes: proof.identity_proof, + }, + ), + ), + metadata: Some(self.response_metadata_v0(platform_state)), + } + } else { + let maybe_identity = self + .drive + .fetch_full_identity_by_non_unique_public_key_hash( + public_key_hash, + start_after, + None, + platform_version, + )?; + + let serialized_identity = maybe_identity + .map(|identity| { + identity + .serialize_consume_to_bytes() + .map_err(Error::Protocol) + }) + .transpose()?; + + GetIdentityByNonUniquePublicKeyHashResponseV0 { + metadata: Some(self.response_metadata_v0(platform_state)), + result: Some( + get_identity_by_non_unique_public_key_hash_response_v0::Result::Identity( + IdentityResponse { + identity: serialized_identity, + }, + ), + ), + } + }; + + Ok(QueryValidationResult::new_with_data(response)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::query::tests::setup_platform; + use dapi_grpc::platform::v0::ResponseMetadata; + use dpp::dashcore::Network; + + #[test] + fn test_invalid_public_key_hash() { + let (platform, state, version) = setup_platform(None, Network::Testnet, None); + + let request = GetIdentityByNonUniquePublicKeyHashRequestV0 { + public_key_hash: vec![0; 8], + start_after: None, + prove: false, + }; + + let result = platform + .query_identity_by_non_unique_public_key_hash_v0(request, &state, version) + .expect("expected query to succeed"); + + assert!(matches!( + result.errors.as_slice(), + [QueryError::InvalidArgument(msg)] if msg == &"public key hash must be 20 bytes long".to_string() + )); + } + + #[test] + fn test_invalid_start_after() { + let (platform, state, version) = setup_platform(None, Network::Testnet, None); + + let negative_tests: Vec<&[u8]> = vec![&[0u8; 4], &[0u8; 20], &[0u8; 64]]; + + for test in negative_tests { + let request = GetIdentityByNonUniquePublicKeyHashRequestV0 { + public_key_hash: vec![0; 20], + start_after: Some(test.to_vec()), + prove: false, + }; + + let result = platform + .query_identity_by_non_unique_public_key_hash_v0(request, &state, version) + .expect("expected query to succeed"); + + assert!( + matches!( + result.errors.as_slice(), + [QueryError::InvalidArgument(msg)] if msg == &"start_after must be 32 bytes long identity ID".to_string()), + "errors: {:?}", + result.errors, + ); + } + } + + #[test] + fn test_identity_not_found() { + let (platform, state, version) = setup_platform(None, Network::Testnet, None); + + let public_key_hash = vec![0; 20]; + let request = GetIdentityByNonUniquePublicKeyHashRequestV0 { + public_key_hash: public_key_hash.clone(), + start_after: None, + prove: false, + }; + + let result = platform + .query_identity_by_non_unique_public_key_hash_v0(request, &state, version) + .expect("expected query to succeed"); + + assert_eq!( + result.data, + Some(GetIdentityByNonUniquePublicKeyHashResponseV0 { + metadata: Some(ResponseMetadata { + height: 0, + core_chain_locked_height: 0, + epoch: 0, + time_ms: 0, + protocol_version: 9, + chain_id: "chain_id".to_string() + }), + result: Some( + get_identity_by_non_unique_public_key_hash_response_v0::Result::Identity( + IdentityResponse { identity: None } + ) + ), + }) + ); + } + + #[test] + fn test_identity_absence_proof() { + let (platform, state, version) = setup_platform(None, Network::Testnet, None); + + let public_key_hash = vec![0; 20]; + let request = GetIdentityByNonUniquePublicKeyHashRequestV0 { + public_key_hash: public_key_hash.clone(), + start_after: None, + prove: true, + }; + + let result = platform + .query_identity_by_non_unique_public_key_hash_v0(request, &state, version) + .expect("expected query to succeed"); + + assert!(matches!( + result.data, + Some(GetIdentityByNonUniquePublicKeyHashResponseV0 { + result: Some( + get_identity_by_non_unique_public_key_hash_response_v0::Result::Proof(_) + ), + metadata: Some(_), + }) + )); + } +} diff --git a/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_public_key_hash/mod.rs b/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_unique_public_key_hash/mod.rs similarity index 92% rename from packages/rs-drive-abci/src/query/identity_based_queries/identity_by_public_key_hash/mod.rs rename to packages/rs-drive-abci/src/query/identity_based_queries/identity_by_unique_public_key_hash/mod.rs index db20b658b6f..2585d26afb8 100644 --- a/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_public_key_hash/mod.rs +++ b/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_unique_public_key_hash/mod.rs @@ -32,7 +32,7 @@ impl Platform { .drive_abci .query .identity_based_queries - .identity_by_public_key_hash; + .identity_by_unique_public_key_hash; let feature_version = match &version { RequestVersion::V0(_) => 0, @@ -41,7 +41,7 @@ impl Platform { if !feature_version_bounds.check_version(feature_version) { return Ok(QueryValidationResult::new_with_error( QueryError::UnsupportedQueryVersion( - "identity_by_public_key_hash".to_string(), + "identity_by_unique_public_key_hash".to_string(), feature_version_bounds.min_version, feature_version_bounds.max_version, platform_version.protocol_version, @@ -52,7 +52,7 @@ impl Platform { match version { RequestVersion::V0(request_v0) => { - let request = self.query_identity_by_public_key_hash_v0( + let request = self.query_identity_by_unique_public_key_hash_v0( request_v0, platform_state, platform_version, diff --git a/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_public_key_hash/v0/mod.rs b/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_unique_public_key_hash/v0/mod.rs similarity index 94% rename from packages/rs-drive-abci/src/query/identity_based_queries/identity_by_public_key_hash/v0/mod.rs rename to packages/rs-drive-abci/src/query/identity_based_queries/identity_by_unique_public_key_hash/v0/mod.rs index 0d3db5c8023..012f289e027 100644 --- a/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_public_key_hash/v0/mod.rs +++ b/packages/rs-drive-abci/src/query/identity_based_queries/identity_by_unique_public_key_hash/v0/mod.rs @@ -14,7 +14,7 @@ use dpp::validation::ValidationResult; use dpp::version::PlatformVersion; impl Platform { - pub(super) fn query_identity_by_public_key_hash_v0( + pub(super) fn query_identity_by_unique_public_key_hash_v0( &self, GetIdentityByPublicKeyHashRequestV0 { public_key_hash, @@ -91,7 +91,7 @@ mod tests { }; let result = platform - .query_identity_by_public_key_hash_v0(request, &state, version) + .query_identity_by_unique_public_key_hash_v0(request, &state, version) .expect("expected query to succeed"); assert!(matches!( @@ -111,7 +111,7 @@ mod tests { }; let result = platform - .query_identity_by_public_key_hash_v0(request, &state, version) + .query_identity_by_unique_public_key_hash_v0(request, &state, version) .expect("expected query to succeed"); assert!(matches!( @@ -131,7 +131,7 @@ mod tests { }; let result = platform - .query_identity_by_public_key_hash_v0(request, &state, version) + .query_identity_by_unique_public_key_hash_v0(request, &state, version) .expect("expected query to succeed"); assert!(matches!( diff --git a/packages/rs-drive-abci/src/query/identity_based_queries/mod.rs b/packages/rs-drive-abci/src/query/identity_based_queries/mod.rs index 29fea765215..98b92b25128 100644 --- a/packages/rs-drive-abci/src/query/identity_based_queries/mod.rs +++ b/packages/rs-drive-abci/src/query/identity_based_queries/mod.rs @@ -3,7 +3,8 @@ mod balance_and_revision; mod balances; mod identities_contract_keys; mod identity; -mod identity_by_public_key_hash; +mod identity_by_non_unique_public_key_hash; +mod identity_by_unique_public_key_hash; mod identity_contract_nonce; mod identity_nonce; mod keys; diff --git a/packages/rs-drive-abci/src/query/service.rs b/packages/rs-drive-abci/src/query/service.rs index 156ae629983..9d77f180745 100644 --- a/packages/rs-drive-abci/src/query/service.rs +++ b/packages/rs-drive-abci/src/query/service.rs @@ -28,6 +28,7 @@ use dapi_grpc::platform::v0::{ GetIdentitiesTokenBalancesResponse, GetIdentitiesTokenInfosRequest, GetIdentitiesTokenInfosResponse, GetIdentityBalanceAndRevisionRequest, GetIdentityBalanceAndRevisionResponse, GetIdentityBalanceRequest, GetIdentityBalanceResponse, + GetIdentityByNonUniquePublicKeyHashRequest, GetIdentityByNonUniquePublicKeyHashResponse, GetIdentityByPublicKeyHashRequest, GetIdentityByPublicKeyHashResponse, GetIdentityContractNonceRequest, GetIdentityContractNonceResponse, GetIdentityKeysRequest, GetIdentityKeysResponse, GetIdentityNonceRequest, GetIdentityNonceResponse, GetIdentityRequest, @@ -411,6 +412,18 @@ impl PlatformService for QueryService { .await } + async fn get_identity_by_non_unique_public_key_hash( + &self, + request: Request, + ) -> Result, Status> { + self.handle_blocking_query( + request, + Platform::::query_identity_by_non_unique_public_key_hash, + "get_identity_by_non_unique_public_key_hash", + ) + .await + } + async fn wait_for_state_transition_result( &self, _request: Request, diff --git a/packages/rs-drive-abci/tests/strategy_tests/query.rs b/packages/rs-drive-abci/tests/strategy_tests/query.rs index 4a4103c4dbc..5e0aaba3d85 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/query.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/query.rs @@ -266,7 +266,7 @@ impl QueryStrategy { let (proof_root_hash, identity): ( RootHash, Option, - ) = Drive::verify_full_identity_by_public_key_hash( + ) = Drive::verify_full_identity_by_unique_public_key_hash( &proof.grovedb_proof, key_hash, platform_version, diff --git a/packages/rs-drive-proof-verifier/src/proof.rs b/packages/rs-drive-proof-verifier/src/proof.rs index ec15e3df0c9..42fafd98472 100644 --- a/packages/rs-drive-proof-verifier/src/proof.rs +++ b/packages/rs-drive-proof-verifier/src/proof.rs @@ -15,7 +15,10 @@ use dapi_grpc::platform::v0::get_protocol_version_upgrade_vote_status_request::{ self, GetProtocolVersionUpgradeVoteStatusRequestV0, }; use dapi_grpc::platform::v0::security_level_map::KeyKindRequestType as GrpcKeyKind; -use dapi_grpc::platform::v0::{get_contested_resource_identity_votes_request, get_data_contract_history_request, get_data_contract_request, get_data_contracts_request, get_epochs_info_request, get_evonodes_proposed_epoch_blocks_by_ids_request, get_evonodes_proposed_epoch_blocks_by_range_request, get_group_actions_request, get_group_info_request, get_group_infos_request, get_identities_balances_request, get_identities_contract_keys_request, get_identity_balance_and_revision_request, get_identity_balance_request, get_identity_by_public_key_hash_request, get_identity_contract_nonce_request, get_identity_keys_request, get_identity_nonce_request, get_identity_request, get_path_elements_request, get_prefunded_specialized_balance_request, GetContestedResourceVotersForIdentityRequest, GetContestedResourceVotersForIdentityResponse, GetGroupActionSignersRequest, GetGroupActionSignersResponse, GetGroupActionsRequest, GetGroupActionsResponse, GetGroupInfoRequest, GetGroupInfoResponse, GetGroupInfosRequest, GetGroupInfosResponse, GetPathElementsRequest, GetPathElementsResponse, GetProtocolVersionUpgradeStateRequest, GetProtocolVersionUpgradeStateResponse, GetProtocolVersionUpgradeVoteStatusRequest, GetProtocolVersionUpgradeVoteStatusResponse, Proof, ResponseMetadata}; +use dapi_grpc::platform::v0::{ + get_contested_resource_identity_votes_request, get_data_contract_history_request, get_data_contract_request, get_data_contracts_request, get_epochs_info_request, get_evonodes_proposed_epoch_blocks_by_ids_request, get_evonodes_proposed_epoch_blocks_by_range_request, get_identities_balances_request, get_identities_contract_keys_request, get_identity_balance_and_revision_request, get_identity_balance_request, get_identity_by_non_unique_public_key_hash_request, + get_identity_by_public_key_hash_request, get_identity_contract_nonce_request, get_identity_keys_request, get_identity_nonce_request, get_identity_request, get_path_elements_request, get_prefunded_specialized_balance_request, GetContestedResourceVotersForIdentityRequest, GetContestedResourceVotersForIdentityResponse, GetPathElementsRequest, GetPathElementsResponse, GetProtocolVersionUpgradeStateRequest, GetProtocolVersionUpgradeStateResponse, GetProtocolVersionUpgradeVoteStatusRequest, GetProtocolVersionUpgradeVoteStatusResponse, Proof, ResponseMetadata +}; use dapi_grpc::platform::{ v0::{self as platform, key_request_type, KeyRequestType as GrpcKeyType}, VersionedGrpcResponse, @@ -36,6 +39,7 @@ use dpp::state_transition::proof_result::StateTransitionProofResult; use dpp::state_transition::StateTransition; use dpp::version::PlatformVersion; use dpp::voting::votes::Vote; +use drive::drive::identity::identity_and_non_unique_public_key_hash_double_proof::IdentityAndNonUniquePublicKeyHashDoubleProof; use drive::drive::identity::key::fetch::{ IdentityKeysRequest, KeyKindRequestType, KeyRequestType, PurposeU8, SecurityLevelU8, }; @@ -51,10 +55,6 @@ use indexmap::IndexMap; use std::array::TryFromSliceError; use std::collections::BTreeMap; use std::num::TryFromIntError; -use dpp::data_contract::group::{Group, GroupMemberPower}; -use dpp::data_contract::GroupContractPosition; -use dpp::group::group_action::GroupAction; -use dpp::group::group_action_status::GroupActionStatus; use crate::error::MapGroveDbError; /// Parse and verify the received proof and retrieve the requested object, if any. @@ -283,7 +283,7 @@ impl FromProof for Identity { id.into_buffer(), platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -326,12 +326,12 @@ impl FromProof for Identity { }; // Extract content from proof and verify Drive/GroveDB proofs - let (root_hash, maybe_identity) = Drive::verify_full_identity_by_public_key_hash( + let (root_hash, maybe_identity) = Drive::verify_full_identity_by_unique_public_key_hash( &proof.grovedb_proof, public_key_hash, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -339,6 +339,98 @@ impl FromProof for Identity { } } +impl FromProof for Identity { + type Request = platform::GetIdentityByNonUniquePublicKeyHashRequest; + type Response = platform::GetIdentityByNonUniquePublicKeyHashResponse; + fn maybe_from_proof_with_metadata<'a, I: Into, O: Into>( + request: I, + response: O, + _network: Network, + platform_version: &PlatformVersion, + provider: &'a dyn ContextProvider, + ) -> Result<(Option, ResponseMetadata, Proof), Error> + where + Self: Sized + 'a, + { + let request = request.into(); + let response = response.into(); + // Parse response to read proof and metadata + // note that proof in this case is different + // let proof = response.proof().or(Err(Error::NoProofInResult))?; + use platform::get_identity_by_non_unique_public_key_hash_response::{ + get_identity_by_non_unique_public_key_hash_response_v0::Result as V0Result, Version::V0, + }; + + let (proved_response, mtd) = match response.version { + Some(V0(v0)) => { + let proof = if let V0Result::Proof(p) = v0.result.ok_or(Error::NoProofInResult)? { + p + } else { + return Err(Error::NoProofInResult); + }; + + (proof, v0.metadata.ok_or(Error::EmptyResponseMetadata)?) + } + _ => return Err(Error::EmptyResponseMetadata), + }; + + // let mtd = response.metadata().or(Err(Error::EmptyResponseMetadata))?; + + let (public_key_hash, after_identity) = match request.version.ok_or(Error::EmptyVersion)? { + get_identity_by_non_unique_public_key_hash_request::Version::V0(v0) => { + let public_key_hash = + v0.public_key_hash + .try_into() + .map_err(|_| Error::RequestError { + error: "Invalid public key hash length".to_string(), + })?; + + let after = v0 + .start_after + .map(|a| { + a.try_into().map_err(|_| Error::RequestError { + error: "Invalid start_after length".to_string(), + }) + }) + .transpose()?; + (public_key_hash, after) + } + }; + + // we need to convert some data to handle non-default proof structure for this response + let proof = proved_response + .grovedb_identity_public_key_hash_proof + .ok_or(Error::NoProofInResult)?; + + let proof_tuple = IdentityAndNonUniquePublicKeyHashDoubleProof { + identity_proof: proved_response.identity_proof_bytes, + identity_id_public_key_hash_proof: proof.grovedb_proof.clone(), + }; + + // Extract content from proof and verify Drive/GroveDB proofs + let (root_hash, maybe_identity) = + Drive::verify_full_identity_by_non_unique_public_key_hash( + &proof_tuple, + public_key_hash, + after_identity, + platform_version, + ) + .map_err(|e| match e { + drive::error::Error::GroveDB(e) => Error::GroveDBError { + proof_bytes: proof.grovedb_proof.clone(), + height: mtd.height, + time_ms: mtd.time_ms, + error: e.to_string(), + }, + _ => e.into(), + })?; + + verify_tenderdash_proof(&proof, &mtd, &root_hash, provider)?; + + Ok((maybe_identity, mtd.clone(), proof)) + } +} + impl FromProof for IdentityPublicKeys { type Request = platform::GetIdentityKeysRequest; type Response = platform::GetIdentityKeysResponse; @@ -396,7 +488,7 @@ impl FromProof for IdentityPublicKeys { false, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; let maybe_keys: Option = if let Some(identity) = maybe_identity { if identity.loaded_public_keys.is_empty() { @@ -522,7 +614,7 @@ impl FromProof for IdentityNonceFetcher { false, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -577,7 +669,7 @@ impl FromProof for IdentityContractNo false, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -625,7 +717,7 @@ impl FromProof for IdentityBalance { false, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -674,7 +766,7 @@ impl FromProof for IdentityBalances { &identity_ids, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -720,7 +812,7 @@ impl FromProof for IdentityBalan false, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -767,7 +859,7 @@ impl FromProof for DataContract { id.into_buffer(), platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -817,7 +909,7 @@ impl FromProof for DataContracts { ids.as_slice(), platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; let contracts = contracts @@ -884,7 +976,7 @@ impl FromProof for DataContractHistory offset, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -943,7 +1035,7 @@ impl FromProof for StateTransitionPro &contracts_provider_fn, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1028,7 +1120,7 @@ impl FromProof for ExtendedEpochInfos { ascending, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; let epoch_info = epoch_info .into_iter() @@ -1076,7 +1168,7 @@ impl FromProof for ProtocolVersionUpgrade let (root_hash, objects) = Drive::verify_upgrade_state(&proof.grovedb_proof, platform_version) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1129,7 +1221,7 @@ impl FromProof for MasternodeProtoco try_u32_to_u16(request_v0.count)?, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1233,7 +1325,7 @@ where let (root_hash, documents) = request .verify_proof(&proof.grovedb_proof, platform_version) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; let documents = documents .into_iter() @@ -1315,7 +1407,7 @@ impl FromProof for IdentitiesContrac false, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1356,7 +1448,7 @@ impl FromProof for ContestedResources { let (root_hash, items) = resolved_request .verify_contests_proof(&proof.grovedb_proof, platform_version) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1398,7 +1490,7 @@ impl FromProof for Contenders { let (root_hash, contested_resource_vote_state) = resolved_request .verify_vote_poll_vote_state_proof(&proof.grovedb_proof, platform_version) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1450,7 +1542,7 @@ impl FromProof for Voters { let (root_hash, voters) = resolved_request .verify_vote_poll_votes_proof(&proof.grovedb_proof, platform_version) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1494,7 +1586,7 @@ impl FromProof for ResourceV &contract_provider_fn, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1536,7 +1628,7 @@ impl FromProof for VotePollsGroupedByTim &proof.grovedb_proof, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1581,7 +1673,7 @@ impl FromProof for PrefundedSpe false, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1679,7 +1771,7 @@ impl FromProof for TotalCreditsInPla mtd.core_chain_locked_height, platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1724,7 +1816,7 @@ impl FromProof for Propose ProposerQueryType::ByIds(ids), platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; @@ -1786,7 +1878,7 @@ impl FromProof for Propo ProposerQueryType::ByRange(limit.map(|l| l as u16), formatted_start), platform_version, ) - .map_drive_error(&proof, &mtd)?; + .map_drive_error(proof, mtd)?; verify_tenderdash_proof(proof, mtd, &root_hash, provider)?; diff --git a/packages/rs-drive-verify-c-binding/src/lib.rs b/packages/rs-drive-verify-c-binding/src/lib.rs index 220bb70cc13..cb32ff6624b 100644 --- a/packages/rs-drive-verify-c-binding/src/lib.rs +++ b/packages/rs-drive-verify-c-binding/src/lib.rs @@ -100,7 +100,7 @@ pub unsafe extern "C" fn verify_full_identity_by_identity_id( } #[no_mangle] -pub unsafe extern "C" fn verify_identity_id_by_public_key_hash( +pub unsafe extern "C" fn verify_identity_id_by_unique_public_key_hash( proof_array: *const u8, proof_len: usize, is_proof_subset: bool, @@ -109,8 +109,11 @@ pub unsafe extern "C" fn verify_identity_id_by_public_key_hash( let proof = unsafe { slice::from_raw_parts(proof_array, proof_len) }; let public_key_hash = unsafe { std::ptr::read(public_key_hash) }; - let verification_result = - Drive::verify_identity_id_by_public_key_hash(proof, is_proof_subset, public_key_hash); + let verification_result = Drive::verify_identity_id_by_unique_public_key_hash( + proof, + is_proof_subset, + public_key_hash, + ); match verification_result { Ok((root_hash, maybe_identity_id)) => { @@ -680,13 +683,13 @@ mod tests { } #[test] - fn verify_identity_id_by_public_key_hash() { + fn verify_identity_id_by_unique_public_key_hash() { let proof = multiple_identity_proof(); let public_key_hash: PublicKeyHash = [ 31, 8, 21, 38, 154, 252, 1, 45, 228, 66, 96, 206, 178, 138, 68, 150, 211, 24, 65, 132, ]; let (_root_hash, maybe_identity_id) = - Drive::verify_identity_id_by_public_key_hash(proof, true, public_key_hash) + Drive::verify_identity_id_by_unique_public_key_hash(proof, true, public_key_hash) .expect("should verify"); let expected_identity_id: [u8; 32] = [ 15, 126, 159, 152, 150, 254, 206, 186, 180, 193, 157, 65, 233, 215, 241, 108, 23, 39, diff --git a/packages/rs-drive/Cargo.toml b/packages/rs-drive/Cargo.toml index ff2d86357d4..8b88084f021 100644 --- a/packages/rs-drive/Cargo.toml +++ b/packages/rs-drive/Cargo.toml @@ -52,12 +52,12 @@ enum-map = { version = "2.0.3", optional = true } intmap = { version = "3.0.1", features = ["serde"], optional = true } chrono = { version = "0.4.35", optional = true } itertools = { version = "0.13", optional = true } -grovedb = { git = "https://github.com/dashpay/grovedb", rev= "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true, default-features = false } -grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev= "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true } -grovedb-path = { git = "https://github.com/dashpay/grovedb", rev= "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" } -grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev= "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true } -grovedb-version = { git = "https://github.com/dashpay/grovedb", rev= "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" } -grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev= "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" } +grovedb = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true, default-features = false } +grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true } +grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" } +grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true } +grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" } +grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" } [dev-dependencies] criterion = "0.5" @@ -117,4 +117,9 @@ server = [ full = ["server", "ciborium", "serde", "bs58", "tempfile", "base64", "chrono"] cbor_query = ["ciborium", "dpp/platform-value-cbor", "dpp/cbor"] grovedb_operations_logging = [] -verify = ["grovedb/verify", "grovedb-costs", "dpp/state-transitions", "dpp/system_contracts"] +verify = [ + "grovedb/verify", + "grovedb-costs", + "dpp/state-transitions", + "dpp/system_contracts", +] diff --git a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_full_identity_by_non_unique_public_key_hash/mod.rs b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_full_identity_by_non_unique_public_key_hash/mod.rs new file mode 100644 index 00000000000..7945cb0799c --- /dev/null +++ b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_full_identity_by_non_unique_public_key_hash/mod.rs @@ -0,0 +1,53 @@ +mod v0; + +use crate::drive::Drive; +use crate::error::{drive::DriveError, Error}; +use dpp::identity::Identity; + +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Fetches an identity with all its related information from storage based on a non unique public key hash. + /// + /// This function leverages the versioning system to direct the fetch operation to the appropriate handler based on the `DriveVersion` provided. + /// + /// # Arguments + /// + /// * `public_key_hash` - A non-unique public key hash corresponding to the identity to be fetched. + /// * `after` - An identity ID after which we want to get back our identity. Basically "don't get back this identity, get the next one" + /// * `transaction` - Transaction arguments. + /// * `drive_version` - A reference to the drive version. + /// + /// # Returns + /// + /// Returns a `Result` containing an `Option` of the `Identity` if it exists, otherwise an `Error` if the fetch operation fails or the version is not supported. + pub fn fetch_full_identity_by_non_unique_public_key_hash( + &self, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive + .methods + .identity + .fetch + .public_key_hashes + .fetch_full_identity_by_non_unique_public_key_hash + { + 0 => self.fetch_full_identity_by_non_unique_public_key_hash_v0( + public_key_hash, + after, + transaction, + platform_version, + ), + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "fetch_full_identity_by_non_unique_public_key_hash".to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_full_identity_by_non_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_full_identity_by_non_unique_public_key_hash/v0/mod.rs new file mode 100644 index 00000000000..fa6dd8b1618 --- /dev/null +++ b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_full_identity_by_non_unique_public_key_hash/v0/mod.rs @@ -0,0 +1,53 @@ +use crate::drive::Drive; + +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; + +use dpp::identity::Identity; +use dpp::version::PlatformVersion; + +use grovedb::TransactionArg; + +impl Drive { + /// Fetches an identity with all its information from storage. + pub(super) fn fetch_full_identity_by_non_unique_public_key_hash_v0( + &self, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let mut drive_operations: Vec = vec![]; + self.fetch_full_identity_by_non_unique_public_key_hash_operations_v0( + public_key_hash, + after, + transaction, + &mut drive_operations, + platform_version, + ) + } + + /// Given an identity, fetches the identity with its flags from storage. + pub(super) fn fetch_full_identity_by_non_unique_public_key_hash_operations_v0( + &self, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let identity_ids = self.fetch_identity_ids_by_non_unique_public_key_hash_operations( + public_key_hash, + Some(1), + after, + transaction, + drive_operations, + platform_version, + )?; + if let Some(identity_id) = identity_ids.first() { + self.fetch_full_identity(*identity_id, transaction, platform_version) + } else { + Ok(None) + } + } +} diff --git a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_identity_ids_by_non_unique_public_key_hash/mod.rs b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_identity_ids_by_non_unique_public_key_hash/mod.rs index 9c444e59731..13c3e8111f8 100644 --- a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_identity_ids_by_non_unique_public_key_hash/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_identity_ids_by_non_unique_public_key_hash/mod.rs @@ -2,8 +2,9 @@ mod v0; use crate::drive::Drive; use crate::error::{drive::DriveError, Error}; -use dpp::version::drive_versions::DriveVersion; +use crate::fees::op::LowLevelDriveOperation; use grovedb::TransactionArg; +use platform_version::version::PlatformVersion; impl Drive { /// Fetches identity ids from storage based on a non-unique public key hash. @@ -13,8 +14,9 @@ impl Drive { /// # Arguments /// /// * `public_key_hash` - A non-unique public key hash corresponding to the identity ids to be fetched. + /// * `limit` - An optional limit. /// * `transaction` - Transaction arguments. - /// * `drive_version` - A reference to the drive version. + /// * `platform_version` - A reference to the platform version. /// /// # Returns /// @@ -22,10 +24,13 @@ impl Drive { pub fn fetch_identity_ids_by_non_unique_public_key_hash( &self, public_key_hash: [u8; 20], + limit: Option, + after: Option<[u8; 32]>, transaction: TransactionArg, - drive_version: &DriveVersion, + platform_version: &PlatformVersion, ) -> Result, Error> { - match drive_version + match platform_version + .drive .methods .identity .fetch @@ -34,8 +39,10 @@ impl Drive { { 0 => self.fetch_identity_ids_by_non_unique_public_key_hash_v0( public_key_hash, + limit, + after, transaction, - drive_version, + platform_version, ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "fetch_identity_ids_by_non_unique_public_key_hash".to_string(), @@ -44,4 +51,37 @@ impl Drive { })), } } + + pub(crate) fn fetch_identity_ids_by_non_unique_public_key_hash_operations( + &self, + public_key_hash: [u8; 20], + limit: Option, + after: Option<[u8; 32]>, + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive + .methods + .identity + .fetch + .public_key_hashes + .fetch_identity_ids_by_non_unique_public_key_hash + { + 0 => self.fetch_identity_ids_by_non_unique_public_key_hash_operations_v0( + public_key_hash, + limit, + after, + transaction, + drive_operations, + platform_version, + ), + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "fetch_identity_ids_by_non_unique_public_key_hash_operations".to_string(), + known_versions: vec![0], + received: version, + })), + } + } } diff --git a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_identity_ids_by_non_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_identity_ids_by_non_unique_public_key_hash/v0/mod.rs index ee462d63281..0eeacb54b23 100644 --- a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_identity_ids_by_non_unique_public_key_hash/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/fetch_identity_ids_by_non_unique_public_key_hash/v0/mod.rs @@ -6,10 +6,9 @@ use crate::query::QueryItem; use grovedb::query_result_type::QueryResultType; -use dpp::version::drive_versions::DriveVersion; - use grovedb::{PathQuery, TransactionArg}; +use platform_version::version::PlatformVersion; use std::ops::RangeFull; impl Drive { @@ -17,37 +16,49 @@ impl Drive { pub(super) fn fetch_identity_ids_by_non_unique_public_key_hash_v0( &self, public_key_hash: [u8; 20], + limit: Option, + after: Option<[u8; 32]>, transaction: TransactionArg, - drive_version: &DriveVersion, + platform_version: &PlatformVersion, ) -> Result, Error> { let mut drive_operations: Vec = vec![]; self.fetch_identity_ids_by_non_unique_public_key_hash_operations_v0( public_key_hash, + limit, + after, transaction, &mut drive_operations, - drive_version, + platform_version, ) } - /// Gets identity ids from non unique public key hashes. + /// Gets identity ids from non-unique public key hashes. pub(super) fn fetch_identity_ids_by_non_unique_public_key_hash_operations_v0( &self, public_key_hash: [u8; 20], + limit: Option, + after: Option<[u8; 32]>, transaction: TransactionArg, drive_operations: &mut Vec, - drive_version: &DriveVersion, + platform_version: &PlatformVersion, ) -> Result, Error> { let non_unique_key_hashes = non_unique_key_hashes_sub_tree_path_vec(public_key_hash); - let path_query = PathQuery::new_single_query_item( - non_unique_key_hashes, - QueryItem::RangeFull(RangeFull), - ); + let mut path_query = if let Some(after) = after { + PathQuery::new_single_query_item( + non_unique_key_hashes, + QueryItem::RangeAfter(after.to_vec()..), + ) + } else { + PathQuery::new_single_query_item(non_unique_key_hashes, QueryItem::RangeFull(RangeFull)) + }; + path_query.query.limit = limit; + let (results, _) = self.grove_get_path_query( &path_query, transaction, QueryResultType::QueryKeyElementPairResultType, drive_operations, - drive_version, + &platform_version.drive, )?; results .to_keys() diff --git a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/mod.rs b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/mod.rs index 859982396ab..cbd75b76d42 100644 --- a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/mod.rs @@ -1,4 +1,5 @@ mod fetch_full_identities_by_unique_public_key_hashes; +mod fetch_full_identity_by_non_unique_public_key_hash; mod fetch_full_identity_by_unique_public_key_hash; mod fetch_identity_id_by_unique_public_key_hash; mod fetch_identity_ids_by_non_unique_public_key_hash; @@ -23,7 +24,6 @@ mod tests { fn test_fetch_all_keys_on_identity() { let drive = setup_drive(None, None); let platform_version = PlatformVersion::latest(); - let drive_version = &platform_version.drive; let transaction = drive.grove.start_transaction(); @@ -71,8 +71,10 @@ mod tests { let identity_ids = drive .fetch_identity_ids_by_non_unique_public_key_hash( hash, + None, + None, Some(&transaction), - &drive_version, + platform_version, ) .expect("expected to get identity ids"); assert!(identity_ids.contains(&identity.id().to_buffer())); diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/mod.rs index b095b6bf427..7ca607c9f0a 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/mod.rs @@ -1,6 +1,7 @@ mod prove_full_identities; mod prove_full_identities_by_unique_public_key_hashes; mod prove_full_identity; +mod prove_full_identity_by_non_unique_public_key_hash; mod prove_full_identity_by_unique_public_key_hash; mod prove_identities_contract_keys; mod prove_identity_id_by_unique_public_key_hash; diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_non_unique_public_key_hash/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_non_unique_public_key_hash/mod.rs new file mode 100644 index 00000000000..5712e0df0d4 --- /dev/null +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_non_unique_public_key_hash/mod.rs @@ -0,0 +1,75 @@ +mod v0; + +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; + +use crate::drive::identity::identity_and_non_unique_public_key_hash_double_proof::IdentityAndNonUniquePublicKeyHashDoubleProof; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Generates a proof for an identity associated with a given non-unique public key hash. + /// + /// This function retrieves an identity along with its associated proofs from storage. + /// It utilizes versioning to call the appropriate handler based on the provided + /// `PlatformVersion`. + /// + /// # Arguments + /// + /// - `public_key_hash` - A 20-byte array representing the hash of the public key + /// for which the identity should be fetched. + /// - `after` - An optional identity ID specifying the starting point for retrieval. + /// If provided, the function will return the identity that appears after the given ID, + /// ensuring that the specified identity itself is not included. + /// - `transaction` - A transaction argument used for database operations. + /// - `platform_version` - A reference to the platform version, ensuring that the + /// correct version-specific function is used. + /// + /// # Returns + /// + /// Returns a `Result` containing an [`IdentityAndNonUniquePublicKeyHashDoubleProof`], which + /// includes both the proof of the identity and the proof linking the public key hash to + /// an identity ID. If the operation fails or the platform version is unsupported, an `Error` + /// is returned. + /// + /// # Errors + /// + /// This function will return an `Error` if: + /// - The identity retrieval operation fails. + /// - The provided public key hash does not correspond to a known identity. + /// - The requested platform version is unknown or not supported. + /// + /// # Versioning + /// + /// - Currently, only version `0` of `prove_full_identity_by_non_unique_public_key_hash` + /// is implemented. If an unsupported version is provided, an `UnknownVersionMismatch` + /// error is returned. + pub fn prove_full_identity_by_non_unique_public_key_hash( + &self, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + match platform_version + .drive + .methods + .identity + .prove + .prove_full_identity_by_non_unique_public_key_hash + { + 0 => self.prove_full_identity_by_non_unique_public_key_hash_v0( + public_key_hash, + after, + transaction, + platform_version, + ), + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "prove_full_identity_by_non_unique_public_key_hash".to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_non_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_non_unique_public_key_hash/v0/mod.rs new file mode 100644 index 00000000000..93c8d26c519 --- /dev/null +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_non_unique_public_key_hash/v0/mod.rs @@ -0,0 +1,219 @@ +use crate::drive::Drive; + +use crate::error::Error; + +use crate::drive::identity::identity_and_non_unique_public_key_hash_double_proof::IdentityAndNonUniquePublicKeyHashDoubleProof; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Fetches an identity with all its information from storage. + pub(super) fn prove_full_identity_by_non_unique_public_key_hash_v0( + &self, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let identity_ids = self.fetch_identity_ids_by_non_unique_public_key_hash_operations( + public_key_hash, + Some(1), + after, + transaction, + &mut vec![], + platform_version, + )?; + // We only prove the absence of the public key hash + let mut path_query = + Self::identity_id_by_non_unique_public_key_hash_query(public_key_hash, after); + path_query.query.limit = Some(1); + let identity_id_public_key_hash_proof = self.grove_get_proved_path_query( + &path_query, + transaction, + &mut vec![], + &platform_version.drive, + )?; + let identity_proof = if let Some(identity_id) = identity_ids.first() { + let full_identity_query = + Self::full_identity_query(&identity_id, &platform_version.drive.grove_version)?; + Some(self.grove_get_proved_path_query( + &full_identity_query, + transaction, + &mut vec![], + &platform_version.drive, + )?) + } else { + None + }; + + Ok(IdentityAndNonUniquePublicKeyHashDoubleProof { + identity_proof, + identity_id_public_key_hash_proof, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::block::block_info::BlockInfo; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::identity_public_key::methods::hash::IdentityPublicKeyHashMethodsV0; + use dpp::identity::{Identity, IdentityPublicKey, KeyType}; + use dpp::version::PlatformVersion; + use rand::prelude::StdRng; + use rand::SeedableRng; + + #[test] + fn should_prove_a_single_identity_with_non_unique_key() { + let drive = setup_drive_with_initial_state_structure(None); + + let platform_version = PlatformVersion::latest(); + + let identity = Identity::random_identity(3, Some(14), platform_version) + .expect("expected a random identity"); + + drive + .add_new_identity( + identity.clone(), + false, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to add an identity"); + + let first_key_hash = identity + .public_keys() + .values() + .find(|public_key| !public_key.key_type().is_unique_key_type()) + .expect("expected a unique key") + .public_key_hash() + .expect("expected to hash data"); + + let proof = drive + .prove_full_identity_by_non_unique_public_key_hash( + first_key_hash, + None, + None, + platform_version, + ) + .expect("should not error when proving an identity"); + + let (_, proved_identity) = Drive::verify_full_identity_by_non_unique_public_key_hash( + &proof, + first_key_hash, + None, + platform_version, + ) + .expect("expect that this be verified"); + + assert_eq!(proved_identity, Some(identity)); + } + + #[test] + fn should_prove_a_single_identity_with_non_unique_key_when_two_have_same_key() { + let drive = setup_drive_with_initial_state_structure(None); + + let platform_version = PlatformVersion::latest(); + + let mut identity_1 = Identity::random_identity(3, Some(14), platform_version) + .expect("expected a random identity"); + + let mut identity_2 = Identity::random_identity(3, Some(15), platform_version) + .expect("expected a random identity"); + + let mut rng = StdRng::seed_from_u64(506); + + let key = IdentityPublicKey::random_voting_key_with_rng(3, &mut rng, platform_version) + .expect("expected key") + .0; + + identity_1.add_public_key(key.clone()); + identity_2.add_public_key(key.clone()); + + drive + .add_new_identity( + identity_1.clone(), + false, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to add an identity"); + + drive + .add_new_identity( + identity_2.clone(), + false, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to add an identity"); + + let key_hash = key.public_key_hash().expect("expected key hash"); + + let proof = drive + .prove_full_identity_by_non_unique_public_key_hash( + key_hash, + None, + None, + platform_version, + ) + .expect("should not error when proving an identity"); + + let (_, proved_identity) = Drive::verify_full_identity_by_non_unique_public_key_hash( + &proof, + key_hash, + None, + platform_version, + ) + .expect("expect that this be verified"); + + assert_eq!(proved_identity, Some(identity_1.clone())); + + let proof = drive + .prove_full_identity_by_non_unique_public_key_hash( + key_hash, + Some(identity_1.id().to_buffer()), + None, + platform_version, + ) + .expect("should not error when proving an identity"); + + let (_, proved_identity) = Drive::verify_full_identity_by_non_unique_public_key_hash( + &proof, + key_hash, + Some(identity_1.id().to_buffer()), + platform_version, + ) + .expect("expect that this be verified"); + + assert_eq!(proved_identity, Some(identity_2.clone())); + + let proof = drive + .prove_full_identity_by_non_unique_public_key_hash( + key_hash, + Some(identity_2.id().to_buffer()), + None, + platform_version, + ) + .expect("should not error when proving an identity"); + + let (_, proved_identity) = Drive::verify_full_identity_by_non_unique_public_key_hash( + &proof, + key_hash, + Some(identity_2.id().to_buffer()), + platform_version, + ) + .expect("expect that this be verified"); + + assert_eq!(proved_identity, None); + } +} diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_unique_public_key_hash/v0/mod.rs index f474fa52315..085920640ba 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_unique_public_key_hash/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_unique_public_key_hash/v0/mod.rs @@ -87,7 +87,7 @@ mod tests { .prove_full_identity_by_unique_public_key_hash(first_key_hash, None, platform_version) .expect("should not error when proving an identity"); - let (_, proved_identity) = Drive::verify_full_identity_by_public_key_hash( + let (_, proved_identity) = Drive::verify_full_identity_by_unique_public_key_hash( proof.as_slice(), first_key_hash, platform_version, diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_id_by_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_id_by_unique_public_key_hash/v0/mod.rs index 8900c2e2dcc..83af15538f1 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_id_by_unique_public_key_hash/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_id_by_unique_public_key_hash/v0/mod.rs @@ -65,7 +65,7 @@ mod tests { ) .expect("should not error when proving an identity"); - let (_, proved_identity_id) = Drive::verify_identity_id_by_public_key_hash( + let (_, proved_identity_id) = Drive::verify_identity_id_by_unique_public_key_hash( proof.as_slice(), false, first_key_hash, diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_ids_by_unique_public_key_hashes/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_ids_by_unique_public_key_hashes/v0/mod.rs index 1b4636e6b5a..d9c54243db5 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_ids_by_unique_public_key_hashes/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_ids_by_unique_public_key_hashes/v0/mod.rs @@ -89,7 +89,7 @@ mod tests { .expect("should not error when proving an identity"); let (_, proved_identity_id): ([u8; 32], BTreeMap<[u8; 20], Option<[u8; 32]>>) = - Drive::verify_identity_ids_by_public_key_hashes( + Drive::verify_identity_ids_by_unique_public_key_hashes( proof.as_slice(), false, &key_hashes, diff --git a/packages/rs-drive/src/drive/identity/fetch/queries/mod.rs b/packages/rs-drive/src/drive/identity/fetch/queries/mod.rs index f7832d2274f..846a260eee1 100644 --- a/packages/rs-drive/src/drive/identity/fetch/queries/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/queries/mod.rs @@ -1,5 +1,6 @@ use crate::drive::balances::balance_path_vec; use crate::drive::identity::key::fetch::IdentityKeysRequest; +use crate::drive::non_unique_key_hashes_tree_path_vec; use crate::drive::{identity_tree_path_vec, unique_key_hashes_tree_path_vec, Drive}; use std::ops::RangeFull; @@ -90,6 +91,24 @@ impl Drive { PathQuery::new_single_key(unique_key_hashes, public_key_hash.to_vec()) } + /// The query for proving an identity id from a non-unique public key hash. + /// This should be used for absence proofs + pub fn identity_id_by_non_unique_public_key_hash_query( + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + ) -> PathQuery { + let non_unique_key_hashes = non_unique_key_hashes_tree_path_vec(); + let mut query = Query::new_single_key(public_key_hash.to_vec()); + let sub_query = if let Some(after) = after { + Query::new_single_query_item(QueryItem::RangeAfter(after.to_vec()..)) + } else { + // TODO: why not limit 1? + Query::new_range_full() + }; + query.set_subquery(sub_query); + PathQuery::new(non_unique_key_hashes, SizedQuery::new(query, None, None)) + } + /// The query for proving identity ids from a vector of public key hashes. pub fn identity_ids_by_unique_public_key_hash_query( public_key_hashes: &[[u8; 20]], @@ -213,6 +232,23 @@ impl Drive { .map_err(Error::GroveDB) } + /// This query gets the full identity and the public key hash + pub fn full_identity_with_non_unique_public_key_hash_query( + public_key_hash: [u8; 20], + identity_id: [u8; 32], + after: Option<[u8; 32]>, + grove_version: &GroveVersion, + ) -> Result { + let full_identity_query = Self::full_identity_query(&identity_id, grove_version)?; + let identity_id_by_public_key_hash_query = + Self::identity_id_by_non_unique_public_key_hash_query(public_key_hash, after); + PathQuery::merge( + vec![&full_identity_query, &identity_id_by_public_key_hash_query], + grove_version, + ) + .map_err(Error::GroveDB) + } + /// The query full identities with key hashes too pub fn full_identities_with_keys_hashes_query( identity_ids: &[[u8; 32]], diff --git a/packages/rs-drive/src/drive/identity/identity_and_non_unique_public_key_hash_double_proof.rs b/packages/rs-drive/src/drive/identity/identity_and_non_unique_public_key_hash_double_proof.rs new file mode 100644 index 00000000000..ccd439ca080 --- /dev/null +++ b/packages/rs-drive/src/drive/identity/identity_and_non_unique_public_key_hash_double_proof.rs @@ -0,0 +1,21 @@ +/// Represents a proof containing an optional identity proof and a required +/// proof for the identity ID and non-unique public key hash. +/// +/// This struct is used to verify the authenticity and validity of an identity +/// and its associated non-unique public key hash. +/// +/// # Fields +/// +/// * `identity_proof` - An optional proof for the identity, represented as a +/// serialized byte vector. This may be `None` if no additional proof is required. +/// * `identity_id_public_key_hash_proof` - A required proof verifying the +/// association between an identity ID and its non-unique public key hash, +/// stored as a serialized byte vector. +pub struct IdentityAndNonUniquePublicKeyHashDoubleProof { + /// Optional proof of identity, stored as a serialized byte vector. + pub identity_proof: Option>, + + /// Proof linking an identity ID to a non-unique public key hash, + /// stored as a serialized byte vector. + pub identity_id_public_key_hash_proof: Vec, +} diff --git a/packages/rs-drive/src/drive/identity/mod.rs b/packages/rs-drive/src/drive/identity/mod.rs index 1922e74b884..a75be0ea563 100644 --- a/packages/rs-drive/src/drive/identity/mod.rs +++ b/packages/rs-drive/src/drive/identity/mod.rs @@ -40,6 +40,10 @@ pub mod key; #[cfg(feature = "server")] pub mod update; +/// A module for a struct encapsulating an identity and a non-unique public key hash to identity id proof +#[cfg(any(feature = "server", feature = "verify"))] +pub mod identity_and_non_unique_public_key_hash_double_proof; + use crate::drive::identity::contract_info::ContractInfoStructure; use crate::error::drive::DriveError; use crate::error::Error; diff --git a/packages/rs-drive/src/drive/mod.rs b/packages/rs-drive/src/drive/mod.rs index ce1fb9a74e5..9e11bddcd25 100644 --- a/packages/rs-drive/src/drive/mod.rs +++ b/packages/rs-drive/src/drive/mod.rs @@ -261,7 +261,7 @@ pub(crate) fn non_unique_key_hashes_tree_path() -> [&'static [u8]; 1] { } /// Returns the path to the masternode key hashes. -#[cfg(feature = "server")] +#[cfg(any(feature = "server", feature = "verify"))] pub(crate) fn non_unique_key_hashes_tree_path_vec() -> Vec> { vec![vec![ RootTree::NonUniquePublicKeyKeyHashesToIdentities as u8, @@ -278,7 +278,7 @@ pub(crate) fn non_unique_key_hashes_sub_tree_path(public_key_hash: &[u8]) -> [&[ } /// Returns the path to the masternode key hashes sub tree. -#[cfg(feature = "server")] +#[cfg(any(feature = "server", feature = "verify"))] pub(crate) fn non_unique_key_hashes_sub_tree_path_vec(public_key_hash: [u8; 20]) -> Vec> { vec![ vec![RootTree::NonUniquePublicKeyKeyHashesToIdentities as u8], diff --git a/packages/rs-drive/src/verify/identity/mod.rs b/packages/rs-drive/src/verify/identity/mod.rs index dd24b00f678..a9308182abb 100644 --- a/packages/rs-drive/src/verify/identity/mod.rs +++ b/packages/rs-drive/src/verify/identity/mod.rs @@ -1,13 +1,15 @@ mod verify_full_identities_by_public_key_hashes; mod verify_full_identity_by_identity_id; -mod verify_full_identity_by_public_key_hash; +mod verify_full_identity_by_non_unique_public_key_hash; +mod verify_full_identity_by_unique_public_key_hash; mod verify_identities_contract_keys; mod verify_identity_balance_and_revision_for_identity_id; mod verify_identity_balance_for_identity_id; mod verify_identity_balances_for_identity_ids; mod verify_identity_contract_nonce; -mod verify_identity_id_by_public_key_hash; -mod verify_identity_ids_by_public_key_hashes; +mod verify_identity_id_by_non_unique_public_key_hash; +mod verify_identity_id_by_unique_public_key_hash; +mod verify_identity_ids_by_unique_public_key_hashes; mod verify_identity_keys_by_identity_id; mod verify_identity_nonce; mod verify_identity_revision_for_identity_id; diff --git a/packages/rs-drive/src/verify/identity/verify_full_identities_by_public_key_hashes/v0/mod.rs b/packages/rs-drive/src/verify/identity/verify_full_identities_by_public_key_hashes/v0/mod.rs index 7c1f0545d05..43ce89e6d00 100644 --- a/packages/rs-drive/src/verify/identity/verify_full_identities_by_public_key_hashes/v0/mod.rs +++ b/packages/rs-drive/src/verify/identity/verify_full_identities_by_public_key_hashes/v0/mod.rs @@ -48,7 +48,7 @@ impl Drive { platform_version: &PlatformVersion, ) -> Result<(RootHash, T), Error> { let (root_hash, identity_ids_by_key_hashes) = - Self::verify_identity_ids_by_public_key_hashes::>( + Self::verify_identity_ids_by_unique_public_key_hashes::>( proof, true, public_key_hashes, diff --git a/packages/rs-drive/src/verify/identity/verify_full_identity_by_non_unique_public_key_hash/mod.rs b/packages/rs-drive/src/verify/identity/verify_full_identity_by_non_unique_public_key_hash/mod.rs new file mode 100644 index 00000000000..4e593caeb67 --- /dev/null +++ b/packages/rs-drive/src/verify/identity/verify_full_identity_by_non_unique_public_key_hash/mod.rs @@ -0,0 +1,79 @@ +mod v0; + +use crate::drive::Drive; + +use crate::error::drive::DriveError; + +use crate::error::Error; + +use crate::verify::RootHash; + +pub use dpp::prelude::Identity; + +use crate::drive::identity::identity_and_non_unique_public_key_hash_double_proof::IdentityAndNonUniquePublicKeyHashDoubleProof; +use dpp::version::PlatformVersion; + +impl Drive { + /// Verifies the full identity of a user using their non-unique public key hash. + /// + /// This function acts as a dispatcher that selects the appropriate version-specific + /// verification method based on the provided platform version. + /// + /// # Parameters + /// + /// - `proof`: A proof containing both the identity proof (if applicable) and the + /// proof linking the public key hash to an identity ID. + /// - `public_key_hash`: A 20-byte array representing the hash of the user's public key. + /// - `after`: An optional 32-byte array specifying an identity after which + /// the search should begin when retrieving the identity. + /// - `platform_version`: A reference to the platform version, ensuring that + /// the correct verification method is used. + /// + /// # Returns + /// + /// Returns a `Result` containing: + /// - `RootHash`: The root hash of GroveDB after verification. + /// - `Option`: The full identity of the user, if it exists. + /// + /// If no identity is found, the returned `Option` will be `None`. + /// + /// # Errors + /// + /// This function returns an `Error` if: + /// - The provided proof is invalid. + /// - The public key hash does not correspond to a valid identity ID. + /// - The identity ID exists but does not correspond to a valid full identity. + /// - The provided platform version is unknown or unsupported. + /// + /// # Versioning + /// + /// - Currently, only version `0` of `verify_full_identity_by_non_unique_public_key_hash` + /// is implemented. If an unsupported version is provided, an `UnknownVersionMismatch` + /// error is returned. + pub fn verify_full_identity_by_non_unique_public_key_hash( + proof: &IdentityAndNonUniquePublicKeyHashDoubleProof, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + platform_version: &PlatformVersion, + ) -> Result<(RootHash, Option), Error> { + match platform_version + .drive + .methods + .verify + .identity + .verify_full_identity_by_non_unique_public_key_hash + { + 0 => Self::verify_full_identity_by_non_unique_public_key_hash_v0( + proof, + public_key_hash, + after, + platform_version, + ), + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "verify_full_identity_by_non_unique_public_key_hash".to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive/src/verify/identity/verify_full_identity_by_non_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/verify/identity/verify_full_identity_by_non_unique_public_key_hash/v0/mod.rs new file mode 100644 index 00000000000..7f500fc7c8d --- /dev/null +++ b/packages/rs-drive/src/verify/identity/verify_full_identity_by_non_unique_public_key_hash/v0/mod.rs @@ -0,0 +1,82 @@ +use crate::drive::Drive; + +use crate::error::Error; + +use crate::verify::RootHash; + +pub use dpp::prelude::Identity; + +use crate::drive::identity::identity_and_non_unique_public_key_hash_double_proof::IdentityAndNonUniquePublicKeyHashDoubleProof; +use crate::error::proof::ProofError; +use dpp::version::PlatformVersion; + +impl Drive { + /// Verifies the full identity of a user using their non-unique public key hash. + /// + /// This function performs a two-step verification process: + /// 1. It verifies the identity ID associated with the given public key hash + /// by calling [`verify_identity_id_by_non_unique_public_key_hash()`]. + /// 2. If an identity ID is found, it then verifies the full identity by calling + /// [`verify_full_identity_by_identity_id()`]. + /// + /// # Arguments + /// + /// * `proof` - A proof containing both the identity proof (if applicable) and + /// the proof linking the public key hash to an identity ID. + /// * `public_key_hash` - A 20-byte array representing the hash of the user's public key. + /// * `after` - An optional 32-byte array used to specify a search point in the proof verification process. + /// * `platform_version` - A reference to the platform version, ensuring compatibility. + /// + /// # Returns + /// + /// If verification is successful, returns a `Result` containing: + /// - `RootHash` - The root hash of GroveDB after verification. + /// - `Option` - The full identity of the user, if it exists. + /// + /// If no identity is found, the returned `Option` will be `None`. + /// + /// # Errors + /// + /// This function will return an `Error` if: + /// * The provided proof is invalid. + /// * The public key hash does not correspond to a valid identity ID. + /// * The identity ID exists but the associated identity proof is missing. + /// * The identity verification process fails. + /// + /// # Inline Optimization + /// + /// This function is marked with `#[inline(always)]` to hint the compiler to + /// aggressively inline it for performance optimization. + #[inline(always)] + pub(super) fn verify_full_identity_by_non_unique_public_key_hash_v0( + proof: &IdentityAndNonUniquePublicKeyHashDoubleProof, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + platform_version: &PlatformVersion, + ) -> Result<(RootHash, Option), Error> { + let (root_hash, identity_id) = Self::verify_identity_id_by_non_unique_public_key_hash( + &proof.identity_id_public_key_hash_proof, + false, + public_key_hash, + after, + platform_version, + )?; + let maybe_identity = identity_id + .map(|identity_id| { + let Some(identity_proof) = &proof.identity_proof else { + return Err(Error::Proof(ProofError::IncompleteProof("identity is not in proof even though identity id is set from non unique public key hash"))); + }; + + Self::verify_full_identity_by_identity_id( + identity_proof.as_slice(), + false, + identity_id, + platform_version, + ) + .map(|(_, maybe_identity)| maybe_identity) + }) + .transpose()? + .flatten(); + Ok((root_hash, maybe_identity)) + } +} diff --git a/packages/rs-drive/src/verify/identity/verify_full_identity_by_public_key_hash/mod.rs b/packages/rs-drive/src/verify/identity/verify_full_identity_by_unique_public_key_hash/mod.rs similarity index 89% rename from packages/rs-drive/src/verify/identity/verify_full_identity_by_public_key_hash/mod.rs rename to packages/rs-drive/src/verify/identity/verify_full_identity_by_unique_public_key_hash/mod.rs index 6ce7cc6f263..17cd33c1995 100644 --- a/packages/rs-drive/src/verify/identity/verify_full_identity_by_public_key_hash/mod.rs +++ b/packages/rs-drive/src/verify/identity/verify_full_identity_by_unique_public_key_hash/mod.rs @@ -36,7 +36,7 @@ impl Drive { /// - The identity ID does not correspond to a valid full identity. /// - An unknown or unsupported platform version is provided. /// - pub fn verify_full_identity_by_public_key_hash( + pub fn verify_full_identity_by_unique_public_key_hash( proof: &[u8], public_key_hash: [u8; 20], platform_version: &PlatformVersion, @@ -48,13 +48,13 @@ impl Drive { .identity .verify_full_identity_by_public_key_hash { - 0 => Self::verify_full_identity_by_public_key_hash_v0( + 0 => Self::verify_full_identity_by_unique_public_key_hash_v0( proof, public_key_hash, platform_version, ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { - method: "verify_full_identity_by_public_key_hash".to_string(), + method: "verify_full_identity_by_unique_public_key_hash".to_string(), known_versions: vec![0], received: version, })), diff --git a/packages/rs-drive/src/verify/identity/verify_full_identity_by_public_key_hash/v0/mod.rs b/packages/rs-drive/src/verify/identity/verify_full_identity_by_unique_public_key_hash/v0/mod.rs similarity index 91% rename from packages/rs-drive/src/verify/identity/verify_full_identity_by_public_key_hash/v0/mod.rs rename to packages/rs-drive/src/verify/identity/verify_full_identity_by_unique_public_key_hash/v0/mod.rs index e481343470a..342df383e39 100644 --- a/packages/rs-drive/src/verify/identity/verify_full_identity_by_public_key_hash/v0/mod.rs +++ b/packages/rs-drive/src/verify/identity/verify_full_identity_by_unique_public_key_hash/v0/mod.rs @@ -18,7 +18,7 @@ impl Drive { /// The `public_key_hash` should contain the hash of the public key of the user. /// /// The function first verifies the identity ID associated with the given public key hash - /// by calling `verify_identity_id_by_public_key_hash()`. It then uses this identity ID to verify + /// by calling `verify_identity_id_by_unique_public_key_hash()`. It then uses this identity ID to verify /// the full identity by calling `verify_full_identity_by_identity_id()`. /// /// # Returns @@ -38,12 +38,12 @@ impl Drive { /// * The identity ID does not correspond to a valid full identity. /// #[inline(always)] - pub(super) fn verify_full_identity_by_public_key_hash_v0( + pub(super) fn verify_full_identity_by_unique_public_key_hash_v0( proof: &[u8], public_key_hash: [u8; 20], platform_version: &PlatformVersion, ) -> Result<(RootHash, Option), Error> { - let (root_hash, identity_id) = Self::verify_identity_id_by_public_key_hash( + let (root_hash, identity_id) = Self::verify_identity_id_by_unique_public_key_hash( proof, true, public_key_hash, diff --git a/packages/rs-drive/src/verify/identity/verify_identity_id_by_non_unique_public_key_hash/mod.rs b/packages/rs-drive/src/verify/identity/verify_identity_id_by_non_unique_public_key_hash/mod.rs new file mode 100644 index 00000000000..0d1ccae9f99 --- /dev/null +++ b/packages/rs-drive/src/verify/identity/verify_identity_id_by_non_unique_public_key_hash/mod.rs @@ -0,0 +1,65 @@ +mod v0; + +use crate::drive::Drive; + +use crate::error::drive::DriveError; + +use crate::error::Error; + +use crate::verify::RootHash; + +use dpp::version::PlatformVersion; + +impl Drive { + /// Verifies the identity ID of a user by their public key hash. + /// + /// # Parameters + /// + /// - `proof`: A byte slice representing the proof of authentication from the user. + /// - `is_proof_subset`: A boolean indicating whether the proof is a subset. + /// - `public_key_hash`: A 20-byte array representing the hash of the public key of the user. + /// - `after`: A 32 byte array representing an identity after which we want to get the identity id. + /// - `platform_version`: The platform version against which to verify the identity ID. + /// + /// # Returns + /// + /// If the verification is successful, it returns a `Result` with a tuple of `RootHash` and + /// an `Option` of a 32-byte array. The `RootHash` represents the root hash of GroveDB, + /// and the `Option<[u8; 32]>` represents the identity ID of the user if it exists. + /// + /// # Errors + /// + /// Returns an `Error` if: + /// + /// - An unknown or unsupported platform version is provided. + /// - Any other error as documented in the specific versioned function. + /// + pub fn verify_identity_id_by_non_unique_public_key_hash( + proof: &[u8], + is_proof_subset: bool, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + platform_version: &PlatformVersion, + ) -> Result<(RootHash, Option<[u8; 32]>), Error> { + match platform_version + .drive + .methods + .verify + .identity + .verify_identity_id_by_non_unique_public_key_hash + { + 0 => Self::verify_identity_id_by_non_unique_public_key_hash_v0( + proof, + is_proof_subset, + public_key_hash, + after, + platform_version, + ), + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "verify_identity_id_by_non_unique_public_key_hash".to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive/src/verify/identity/verify_identity_id_by_non_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/verify/identity/verify_identity_id_by_non_unique_public_key_hash/v0/mod.rs new file mode 100644 index 00000000000..1d3730b5562 --- /dev/null +++ b/packages/rs-drive/src/verify/identity/verify_identity_id_by_non_unique_public_key_hash/v0/mod.rs @@ -0,0 +1,69 @@ +use crate::drive::{non_unique_key_hashes_sub_tree_path_vec, Drive}; + +use crate::error::proof::ProofError; +use crate::error::Error; + +use crate::verify::RootHash; + +use grovedb::GroveDb; +use platform_version::version::PlatformVersion; + +impl Drive { + /// Verifies the identity ID of a user by their public key hash. + /// + /// # Parameters + /// + /// - `proof`: A byte slice representing the proof of authentication from the user. + /// - `is_proof_subset`: A boolean indicating whether the proof is a subset. + /// - `public_key_hash`: A 20-byte array representing the hash of the public key of the user. + /// - `after`: A 32 byte array representing an identity after which we want to get the identity id. + /// + /// # Returns + /// + /// If the verification is successful, it returns a `Result` with a tuple of `RootHash` and + /// an `Option` of a 32-byte array. The `RootHash` represents the root hash of GroveDB, + /// and the `Option<[u8; 32]>` represents the identity ID of the user if it exists. + /// + /// # Errors + /// + /// Returns an `Error` if: + /// + /// - The proof of authentication is not valid. + /// - The public key hash does not correspond to a valid identity ID. + /// - The proved key value is not for the correct path or key in unique key hashes. + /// - More than one identity ID is found. + /// + #[inline(always)] + pub(super) fn verify_identity_id_by_non_unique_public_key_hash_v0( + proof: &[u8], + is_proof_subset: bool, + public_key_hash: [u8; 20], + after: Option<[u8; 32]>, + platform_version: &PlatformVersion, + ) -> Result<(RootHash, Option<[u8; 32]>), Error> { + let mut path_query = + Self::identity_id_by_non_unique_public_key_hash_query(public_key_hash, after); + path_query.query.limit = Some(1); + let (root_hash, mut proved_key_values) = if is_proof_subset { + GroveDb::verify_subset_query(proof, &path_query, &platform_version.drive.grove_version)? + } else { + GroveDb::verify_query(proof, &path_query, &platform_version.drive.grove_version)? + }; + + if proved_key_values.len() == 1 { + let (path, key, _) = proved_key_values.remove(0); + if path != non_unique_key_hashes_sub_tree_path_vec(public_key_hash) { + return Err(Error::Proof(ProofError::CorruptedProof( + "we did not get back an element for the correct path in non unique key hashes" + .to_string(), + ))); + } + let identity_id = key.try_into().map_err(|_| { + Error::Proof(ProofError::IncorrectValueSize("value size is incorrect")) + })?; + Ok((root_hash, Some(identity_id))) + } else { + Ok((root_hash, None)) + } + } +} diff --git a/packages/rs-drive/src/verify/identity/verify_identity_id_by_public_key_hash/mod.rs b/packages/rs-drive/src/verify/identity/verify_identity_id_by_unique_public_key_hash/mod.rs similarity index 86% rename from packages/rs-drive/src/verify/identity/verify_identity_id_by_public_key_hash/mod.rs rename to packages/rs-drive/src/verify/identity/verify_identity_id_by_unique_public_key_hash/mod.rs index 75182ffeefc..4422e600e01 100644 --- a/packages/rs-drive/src/verify/identity/verify_identity_id_by_public_key_hash/mod.rs +++ b/packages/rs-drive/src/verify/identity/verify_identity_id_by_unique_public_key_hash/mod.rs @@ -33,7 +33,7 @@ impl Drive { /// - An unknown or unsupported platform version is provided. /// - Any other error as documented in the specific versioned function. /// - pub fn verify_identity_id_by_public_key_hash( + pub fn verify_identity_id_by_unique_public_key_hash( proof: &[u8], is_proof_subset: bool, public_key_hash: [u8; 20], @@ -44,16 +44,16 @@ impl Drive { .methods .verify .identity - .verify_identity_id_by_public_key_hash + .verify_identity_id_by_unique_public_key_hash { - 0 => Self::verify_identity_id_by_public_key_hash_v0( + 0 => Self::verify_identity_id_by_unique_public_key_hash_v0( proof, is_proof_subset, public_key_hash, platform_version, ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { - method: "verify_identity_id_by_public_key_hash".to_string(), + method: "verify_identity_id_by_unique_public_key_hash".to_string(), known_versions: vec![0], received: version, })), diff --git a/packages/rs-drive/src/verify/identity/verify_identity_id_by_public_key_hash/v0/mod.rs b/packages/rs-drive/src/verify/identity/verify_identity_id_by_unique_public_key_hash/v0/mod.rs similarity index 98% rename from packages/rs-drive/src/verify/identity/verify_identity_id_by_public_key_hash/v0/mod.rs rename to packages/rs-drive/src/verify/identity/verify_identity_id_by_unique_public_key_hash/v0/mod.rs index ceb8ae05b07..cccc1aa0f15 100644 --- a/packages/rs-drive/src/verify/identity/verify_identity_id_by_public_key_hash/v0/mod.rs +++ b/packages/rs-drive/src/verify/identity/verify_identity_id_by_unique_public_key_hash/v0/mod.rs @@ -33,7 +33,7 @@ impl Drive { /// - More than one identity ID is found. /// #[inline(always)] - pub(super) fn verify_identity_id_by_public_key_hash_v0( + pub(super) fn verify_identity_id_by_unique_public_key_hash_v0( proof: &[u8], is_proof_subset: bool, public_key_hash: [u8; 20], diff --git a/packages/rs-drive/src/verify/identity/verify_identity_ids_by_public_key_hashes/mod.rs b/packages/rs-drive/src/verify/identity/verify_identity_ids_by_unique_public_key_hashes/mod.rs similarity index 87% rename from packages/rs-drive/src/verify/identity/verify_identity_ids_by_public_key_hashes/mod.rs rename to packages/rs-drive/src/verify/identity/verify_identity_ids_by_unique_public_key_hashes/mod.rs index 20a9d98c3d8..6d1f70f8a04 100644 --- a/packages/rs-drive/src/verify/identity/verify_identity_ids_by_public_key_hashes/mod.rs +++ b/packages/rs-drive/src/verify/identity/verify_identity_ids_by_unique_public_key_hashes/mod.rs @@ -36,7 +36,7 @@ impl Drive { /// - An unknown or unsupported platform version is provided. /// - Any other error as documented in the specific versioned function. /// - pub fn verify_identity_ids_by_public_key_hashes< + pub fn verify_identity_ids_by_unique_public_key_hashes< T: FromIterator<([u8; 20], Option<[u8; 32]>)>, >( proof: &[u8], @@ -49,16 +49,16 @@ impl Drive { .methods .verify .identity - .verify_identity_ids_by_public_key_hashes + .verify_identity_ids_by_unique_public_key_hashes { - 0 => Self::verify_identity_ids_by_public_key_hashes_v0( + 0 => Self::verify_identity_ids_by_unique_public_key_hashes_v0( proof, is_proof_subset, public_key_hashes, platform_version, ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { - method: "verify_identity_ids_by_public_key_hashes".to_string(), + method: "verify_identity_ids_by_unique_public_key_hashes".to_string(), known_versions: vec![0], received: version, })), diff --git a/packages/rs-drive/src/verify/identity/verify_identity_ids_by_public_key_hashes/v0/mod.rs b/packages/rs-drive/src/verify/identity/verify_identity_ids_by_unique_public_key_hashes/v0/mod.rs similarity index 98% rename from packages/rs-drive/src/verify/identity/verify_identity_ids_by_public_key_hashes/v0/mod.rs rename to packages/rs-drive/src/verify/identity/verify_identity_ids_by_unique_public_key_hashes/v0/mod.rs index 6d1c6dc7d85..90beab8eaf3 100644 --- a/packages/rs-drive/src/verify/identity/verify_identity_ids_by_public_key_hashes/v0/mod.rs +++ b/packages/rs-drive/src/verify/identity/verify_identity_ids_by_unique_public_key_hashes/v0/mod.rs @@ -37,7 +37,7 @@ impl Drive { /// - The number of proved key values does not match the number of public key hashes provided. /// - The value size of the identity ID is incorrect. /// - pub(crate) fn verify_identity_ids_by_public_key_hashes_v0< + pub(crate) fn verify_identity_ids_by_unique_public_key_hashes_v0< T: FromIterator<([u8; 20], Option<[u8; 32]>)>, >( proof: &[u8], diff --git a/packages/rs-platform-version/Cargo.toml b/packages/rs-platform-version/Cargo.toml index a00c9f92057..70e92452683 100644 --- a/packages/rs-platform-version/Cargo.toml +++ b/packages/rs-platform-version/Cargo.toml @@ -11,7 +11,7 @@ license = "MIT" thiserror = { version = "1.0.63" } bincode = { version = "2.0.0-rc.3" } versioned-feature-core = { git = "https://github.com/dashpay/versioned-feature-core", version = "1.0.0" } -grovedb-version = { git = "https://github.com/dashpay/grovedb", rev= "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" } +grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" } once_cell = "1.19.0" [features] diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/mod.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/mod.rs index 7a5d65bd3ea..ac3412fd85e 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/mod.rs @@ -52,7 +52,7 @@ pub struct DriveAbciQueryIdentityVersions { pub balance: FeatureVersionBounds, pub identities_balances: FeatureVersionBounds, pub balance_and_revision: FeatureVersionBounds, - pub identity_by_public_key_hash: FeatureVersionBounds, + pub identity_by_unique_public_key_hash: FeatureVersionBounds, } #[derive(Clone, Debug, Default)] diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/v1.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/v1.rs index 494dfa6f097..829c9a218d2 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/v1.rs @@ -67,7 +67,7 @@ pub const DRIVE_ABCI_QUERY_VERSIONS_V1: DriveAbciQueryVersions = DriveAbciQueryV max_version: 0, default_current_version: 0, }, - identity_by_public_key_hash: FeatureVersionBounds { + identity_by_unique_public_key_hash: FeatureVersionBounds { min_version: 0, max_version: 0, default_current_version: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/mod.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/mod.rs index 1a2526f4fe7..ae6d64f233f 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/mod.rs @@ -95,6 +95,7 @@ pub struct DriveIdentityFetchPublicKeyHashesMethodVersions { pub has_non_unique_public_key_hash: FeatureVersion, pub has_non_unique_public_key_hash_already_for_identity: FeatureVersion, pub has_unique_public_key_hash: FeatureVersion, + pub fetch_full_identity_by_non_unique_public_key_hash: FeatureVersion, } #[derive(Clone, Debug, Default)] @@ -133,6 +134,7 @@ pub struct DriveIdentityProveMethodVersions { pub prove_full_identity_by_unique_public_key_hash: FeatureVersion, pub prove_identity_id_by_unique_public_key_hash: FeatureVersion, pub prove_identity_ids_by_unique_public_key_hashes: FeatureVersion, + pub prove_full_identity_by_non_unique_public_key_hash: FeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs index beb79c65c18..fab61b69d37 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs @@ -27,6 +27,7 @@ pub const DRIVE_IDENTITY_METHOD_VERSIONS_V1: DriveIdentityMethodVersions = has_non_unique_public_key_hash: 0, has_non_unique_public_key_hash_already_for_identity: 0, has_unique_public_key_hash: 0, + fetch_full_identity_by_non_unique_public_key_hash: 0, }, attributes: DriveIdentityFetchAttributesMethodVersions { revision: 0, @@ -58,6 +59,7 @@ pub const DRIVE_IDENTITY_METHOD_VERSIONS_V1: DriveIdentityMethodVersions = prove_full_identity_by_unique_public_key_hash: 0, prove_identity_id_by_unique_public_key_hash: 0, prove_identity_ids_by_unique_public_key_hashes: 0, + prove_full_identity_by_non_unique_public_key_hash: 0, }, keys: DriveIdentityKeysMethodVersions { fetch: DriveIdentityKeysFetchMethodVersions { diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/mod.rs b/packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/mod.rs index f8082f48198..90cd83d5291 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/mod.rs @@ -35,13 +35,15 @@ pub struct DriveVerifyIdentityMethodVersions { pub verify_full_identity_by_public_key_hash: FeatureVersion, pub verify_identity_balance_for_identity_id: FeatureVersion, pub verify_identity_balances_for_identity_ids: FeatureVersion, - pub verify_identity_id_by_public_key_hash: FeatureVersion, - pub verify_identity_ids_by_public_key_hashes: FeatureVersion, + pub verify_identity_id_by_unique_public_key_hash: FeatureVersion, + pub verify_identity_ids_by_unique_public_key_hashes: FeatureVersion, pub verify_identity_keys_by_identity_id: FeatureVersion, pub verify_identity_nonce: FeatureVersion, pub verify_identity_contract_nonce: FeatureVersion, pub verify_identities_contract_keys: FeatureVersion, pub verify_identity_revision_for_identity_id: FeatureVersion, + pub verify_full_identity_by_non_unique_public_key_hash: FeatureVersion, + pub verify_identity_id_by_non_unique_public_key_hash: FeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/v1.rs b/packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/v1.rs index 666ab416ca7..a848e92c7dd 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/v1.rs @@ -21,13 +21,15 @@ pub const DRIVE_VERIFY_METHOD_VERSIONS_V1: DriveVerifyMethodVersions = DriveVeri verify_full_identity_by_public_key_hash: 0, verify_identity_balance_for_identity_id: 0, verify_identity_balances_for_identity_ids: 0, - verify_identity_id_by_public_key_hash: 0, - verify_identity_ids_by_public_key_hashes: 0, + verify_identity_id_by_unique_public_key_hash: 0, + verify_identity_ids_by_unique_public_key_hashes: 0, verify_identity_keys_by_identity_id: 0, verify_identity_nonce: 0, verify_identity_contract_nonce: 0, verify_identities_contract_keys: 0, verify_identity_revision_for_identity_id: 0, + verify_full_identity_by_non_unique_public_key_hash: 0, + verify_identity_id_by_non_unique_public_key_hash: 0, }, group: DriveVerifyGroupMethodVersions { verify_group_info: 0, diff --git a/packages/rs-platform-version/src/version/mocks/v2_test.rs b/packages/rs-platform-version/src/version/mocks/v2_test.rs index 3b8d73f2735..f3e5c2c6624 100644 --- a/packages/rs-platform-version/src/version/mocks/v2_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v2_test.rs @@ -200,7 +200,7 @@ pub const TEST_PLATFORM_V2: PlatformVersion = PlatformVersion { max_version: 0, default_current_version: 0, }, - identity_by_public_key_hash: FeatureVersionBounds { + identity_by_unique_public_key_hash: FeatureVersionBounds { min_version: 0, max_version: 0, default_current_version: 0, diff --git a/packages/rs-sdk/src/error.rs b/packages/rs-sdk/src/error.rs index 276595958e4..e95b85775c4 100644 --- a/packages/rs-sdk/src/error.rs +++ b/packages/rs-sdk/src/error.rs @@ -100,7 +100,7 @@ impl TryFrom for StateTransitionBroadcastErr type Error = Error; fn try_from(value: StateTransitionBroadcastErrorProto) -> Result { - let cause = if value.data.len() > 0 { + let cause = if !value.data.is_empty() { let consensus_error = ConsensusError::deserialize_from_bytes(&value.data).map_err(|e| { tracing::debug!("Failed to deserialize consensus error: {}", e); diff --git a/packages/rs-sdk/src/platform/query.rs b/packages/rs-sdk/src/platform/query.rs index a145d89058b..ed8b1797675 100644 --- a/packages/rs-sdk/src/platform/query.rs +++ b/packages/rs-sdk/src/platform/query.rs @@ -180,7 +180,7 @@ impl Query for Identifier { } } -impl<'a> Query for DriveDocumentQuery<'a> { +impl Query for DriveDocumentQuery<'_> { fn query(self, prove: bool) -> Result { if !prove { unimplemented!("queries without proofs are not supported yet"); diff --git a/packages/rs-sdk/src/platform/tokens/token_status.rs b/packages/rs-sdk/src/platform/tokens/token_status.rs index 1cf5240b188..045e0b4f28e 100644 --- a/packages/rs-sdk/src/platform/tokens/token_status.rs +++ b/packages/rs-sdk/src/platform/tokens/token_status.rs @@ -1,4 +1,4 @@ -use crate::platform::{Fetch, FetchMany, Identifier, Query}; +use crate::platform::{FetchMany, Identifier, Query}; use crate::Error; use dapi_grpc::platform::v0::get_token_statuses_request::GetTokenStatusesRequestV0; use dapi_grpc::platform::v0::{get_token_statuses_request, GetTokenStatusesRequest}; diff --git a/packages/rs-sdk/src/platform/types/identity.rs b/packages/rs-sdk/src/platform/types/identity.rs index 4b7b7754d0f..963dcaa45f9 100644 --- a/packages/rs-sdk/src/platform/types/identity.rs +++ b/packages/rs-sdk/src/platform/types/identity.rs @@ -9,6 +9,7 @@ use crate::{ use dapi_grpc::platform::v0::get_identities_balances_request::GetIdentitiesBalancesRequestV0; use dapi_grpc::platform::v0::get_identity_balance_and_revision_request::GetIdentityBalanceAndRevisionRequestV0; use dapi_grpc::platform::v0::get_identity_balance_request::GetIdentityBalanceRequestV0; +use dapi_grpc::platform::v0::get_identity_by_non_unique_public_key_hash_request::GetIdentityByNonUniquePublicKeyHashRequestV0; use dapi_grpc::platform::v0::get_identity_by_public_key_hash_request::GetIdentityByPublicKeyHashRequestV0; use dapi_grpc::platform::v0::get_identity_contract_nonce_request::GetIdentityContractNonceRequestV0; use dapi_grpc::platform::v0::get_identity_nonce_request::GetIdentityNonceRequestV0; @@ -31,7 +32,8 @@ delegate_enum! { IdentityResponse, Identity, (GetIdentity,proto::GetIdentityRequest,proto::GetIdentityResponse), - (GetIdentityByPublicKeyHash, proto::GetIdentityByPublicKeyHashRequest, proto::GetIdentityByPublicKeyHashResponse) + (GetIdentityByPublicKeyHash, proto::GetIdentityByPublicKeyHashRequest, proto::GetIdentityByPublicKeyHashResponse), + (GetIdentityByNonUniquePublicKeyHash, proto::GetIdentityByNonUniquePublicKeyHashRequest, proto::GetIdentityByNonUniquePublicKeyHashResponse) } impl Query for dpp::prelude::Identifier { @@ -74,6 +76,36 @@ impl Query for PublicKeyHash { } } +/// Non-unique public key hash that can be used as a [Query] to find an identity. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NonUniquePublicKeyHashQuery { + pub key_hash: [u8; 20], + pub after: Option<[u8; 32]>, +} + +impl Query for NonUniquePublicKeyHashQuery { + fn query(self, prove: bool) -> Result { + if !prove { + unimplemented!("queries without proofs are not supported yet"); + } + + let request = proto::GetIdentityByNonUniquePublicKeyHashRequest { + version: Some( + proto::get_identity_by_non_unique_public_key_hash_request::Version::V0( + GetIdentityByNonUniquePublicKeyHashRequestV0 { + public_key_hash: self.key_hash.to_vec(), + start_after: self.after.map(|a| a.to_vec()), + prove, + }, + ), + ), + } + .into(); + + Ok(request) + } +} + impl Query for dpp::prelude::Identifier { fn query(self, prove: bool) -> Result { if !prove { diff --git a/packages/rs-sdk/tests/fetch/identity.rs b/packages/rs-sdk/tests/fetch/identity.rs index ad5a458960f..dd564623f4a 100644 --- a/packages/rs-sdk/tests/fetch/identity.rs +++ b/packages/rs-sdk/tests/fetch/identity.rs @@ -1,4 +1,4 @@ -use dash_sdk::platform::types::identity::PublicKeyHash; +use dash_sdk::platform::types::identity::{NonUniquePublicKeyHashQuery, PublicKeyHash}; use dash_sdk::platform::{Fetch, FetchMany}; use dpp::identity::accessors::IdentityGettersV0; use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; @@ -116,3 +116,66 @@ async fn test_identity_public_keys_all_read() { assert_eq!(id, pubkey.id()); } } + +/// Given some non-unique public key, when I fetch identity that uses this key, I get associated identities containing this key. +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +async fn test_fetch_identity_by_non_unique_public_keys() { + setup_logs(); + + let cfg = Config::new(); + let id: dpp::prelude::Identifier = cfg.existing_identity_id; + + let sdk = cfg + .setup_api("test_fetch_identity_by_non_unique_public_keys") + .await; + + // First, fetch an identity to get a non-unique public key + let identity = Identity::fetch(&sdk, id) + .await + .expect("fetch identity") + .expect("found identity"); + + let pubkeys: Vec<_> = identity + .public_keys() + .iter() + .filter(|public_key| !public_key.1.key_type().is_unique_key_type()) + .collect(); + + assert_ne!( + pubkeys.len(), + 0, + "identity must have at least one non-unique public key" + ); + + for non_unique_key in pubkeys.iter() { + let key_hash = non_unique_key.1.public_key_hash().expect("public key hash"); + let mut query = NonUniquePublicKeyHashQuery { + key_hash, + after: None, + }; + + // Now fetch identities by this non-unique public key hash + let mut count = 0; + while let Some(found) = Identity::fetch(&sdk, query) + .await + .expect("fetch identities by non-unique key hash") + { + count += 1; + tracing::debug!( + ?found, + ?key_hash, + ?count, + "fetched identities by non-unique public key hash" + ); + + query = NonUniquePublicKeyHashQuery { + key_hash, + after: Some(*found.id().as_bytes()), + }; + } + assert_eq!( + count, 3, + "expected exactly 3 identities with this non-unique public key" + ); + } +} diff --git a/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/.gitkeep b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_28e9fda4d74589e1756f49026696e167d0327209295b4a98e14c6eb0d2b7e631.json b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_28e9fda4d74589e1756f49026696e167d0327209295b4a98e14c6eb0d2b7e631.json new file mode 100644 index 00000000000..fee35a717a9 Binary files /dev/null and b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_28e9fda4d74589e1756f49026696e167d0327209295b4a98e14c6eb0d2b7e631.json differ diff --git a/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_3606368b19c21647e14e80f609e5f90edeec5e32ae9d35ca2f654e5a8b47ef3d.json b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_3606368b19c21647e14e80f609e5f90edeec5e32ae9d35ca2f654e5a8b47ef3d.json new file mode 100644 index 00000000000..a35f4e85b14 Binary files /dev/null and b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_3606368b19c21647e14e80f609e5f90edeec5e32ae9d35ca2f654e5a8b47ef3d.json differ diff --git a/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_3796e6d3ed7346055d82e39618ad441005468aa9ae346220fcc77830f30f06c7.json b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_3796e6d3ed7346055d82e39618ad441005468aa9ae346220fcc77830f30f06c7.json new file mode 100644 index 00000000000..ac0b4a41822 Binary files /dev/null and b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_3796e6d3ed7346055d82e39618ad441005468aa9ae346220fcc77830f30f06c7.json differ diff --git a/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_5f9f951aa5d5af07c588813f31e9355939311eca86a05923cfb53d11b746428a.json b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_5f9f951aa5d5af07c588813f31e9355939311eca86a05923cfb53d11b746428a.json new file mode 100644 index 00000000000..03a98a10115 Binary files /dev/null and b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_5f9f951aa5d5af07c588813f31e9355939311eca86a05923cfb53d11b746428a.json differ diff --git a/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_a6d61bfcc12549cc29bf4e9abe2839f231c080d4ffacee80457ecc1db6a4bbbb.json b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_a6d61bfcc12549cc29bf4e9abe2839f231c080d4ffacee80457ecc1db6a4bbbb.json new file mode 100644 index 00000000000..4af3de76387 Binary files /dev/null and b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/msg_IdentityRequest_a6d61bfcc12549cc29bf4e9abe2839f231c080d4ffacee80457ecc1db6a4bbbb.json differ diff --git a/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/quorum_pubkey-106-15957be6ce59202ce3df8dbd0604f02b4d71142b0d84600aa6fd73436efb869b.json b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/quorum_pubkey-106-15957be6ce59202ce3df8dbd0604f02b4d71142b0d84600aa6fd73436efb869b.json new file mode 100644 index 00000000000..20fe61d31dc --- /dev/null +++ b/packages/rs-sdk/tests/vectors/test_fetch_identity_by_non_unique_public_keys/quorum_pubkey-106-15957be6ce59202ce3df8dbd0604f02b4d71142b0d84600aa6fd73436efb869b.json @@ -0,0 +1 @@ +8b93a14d29fb6eb73d71ebe354b8582949aaddb6e9bce7871d597b30be6696b8482c43ea8ee6bf0dc054409ea35b6212 \ No newline at end of file