Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion packages/rs-dpp/src/identity/identity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::address_funds::PlatformAddress;
use crate::identity::v0::IdentityV0;
use crate::identity::{IdentityPublicKey, KeyID};
use crate::prelude::Revision;
use crate::prelude::{AddressNonce, Revision};
use crate::state_transition::identity_id_from_input_addresses;

#[cfg(feature = "identity-hashing")]
use crate::serialization::PlatformSerializable;
Expand Down Expand Up @@ -118,6 +120,29 @@ impl Identity {
}
}

/// Create a new identity using input [PlatformAddress]es.
///
/// This function derives the identity ID from the provided input addresses.
///
/// ## Arguments
///
/// * `inputs` - A map of PlatformAddress to AddressNonce tuples used to derive the identity id; the nonces
/// should represent state after creation of the identity (eg. be incremented by 1).
/// * `public_keys` - A map of KeyID to IdentityPublicKey tuples representing the public keys for the identity.
/// * `platform_version` - The platform version to use for identity creation.
///
/// ## Returns
///
/// * `Result<Identity, ProtocolError>` - Returns the newly created Identity or a ProtocolError if the operation fails.
pub fn new_with_input_addresses_and_keys(
inputs: &BTreeMap<PlatformAddress, (AddressNonce, Credits)>,
public_keys: BTreeMap<KeyID, IdentityPublicKey>,
platform_version: &PlatformVersion,
) -> Result<Identity, ProtocolError> {
let identity_id = identity_id_from_input_addresses(inputs)?;
Self::new_with_id_and_keys(identity_id, public_keys, platform_version)
}
Comment thread
lklimek marked this conversation as resolved.

/// Convenience method to get Partial Identity Info
pub fn into_partial_identity_info(self) -> PartialIdentity {
match self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
use std::collections::BTreeMap;

use crate::address_funds::PlatformAddress;
use crate::fee::Credits;
use crate::prelude::AddressNonce;
use crate::state_transition::StateTransitionWitnessSigned;
use crate::util::hash::hash_double;
use crate::ProtocolError;
use platform_value::Identifier;

pub trait StateTransitionIdentityIdFromInputs: StateTransitionWitnessSigned {
/// Get the identity id from inputs
/// Get the identity id from inputs.
///
/// Inputs should represent state after creation of the identity (eg. be incremented by 1).
fn identity_id_from_inputs(&self) -> Result<Identifier, ProtocolError> {
if self.inputs().is_empty() {
return Err(ProtocolError::ParsingError(
"Identity creation requires at least one input".to_string(),
));
}

// Build a map containing only (PlatformAddress, KeyOfTypeNonce) pairs,
// ignoring the Credits in the input values.
let address_nonce_map: BTreeMap<&PlatformAddress, &AddressNonce> = self
.inputs()
.iter()
.map(|(address, (nonce, _credits))| (address, nonce))
.collect();

use crate::util::hash::hash_double;

let input_bytes = bincode::encode_to_vec(&address_nonce_map, bincode::config::standard())
.map_err(|e| {
ProtocolError::EncodingError(format!("Failed to encode inputs: {}", e))
})?;
let inputs = self.inputs();
identity_id_from_input_addresses(inputs)
}
}

let hash = hash_double(input_bytes);
Ok(Identifier::new(hash))
/// Helper that computes the identity ID from input addresses and nonces.
/// Nonces should represent state after creation of the identity (eg. be incremented by 1).
///
/// Internal use only; see `StateTransitionIdentityIdFromInputs` trait.
pub(crate) fn identity_id_from_input_addresses(
input_addresses: &BTreeMap<PlatformAddress, (AddressNonce, Credits)>,
) -> Result<Identifier, ProtocolError> {
if input_addresses.is_empty() {
return Err(ProtocolError::ParsingError(
"Identity creation requires at least one input".to_string(),
));
}
// Build a map containing only (PlatformAddress, KeyOfTypeNonce) pairs,
// ignoring the Credits in the input values.
let address_nonce_map: BTreeMap<&PlatformAddress, &AddressNonce> = input_addresses
.iter()
.map(|(address, (nonce, _credits))| (address, nonce))
.collect();
let input_bytes = bincode::encode_to_vec(&address_nonce_map, bincode::config::standard())
.map_err(|e| ProtocolError::EncodingError(format!("Failed to encode inputs: {}", e)))?;

let hash = hash_double(input_bytes);
Ok(Identifier::new(hash))
}
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ impl Drive {
})?;
let (root_hash_identity, identity) = Drive::verify_full_identity_by_identity_id(
proof,
false,
true,
identity_id.into_buffer(),
platform_version,
)?;
Expand All @@ -1006,7 +1006,7 @@ impl Drive {
) = Drive::verify_addresses_infos(
proof,
addresses_to_check,
false,
true,
platform_version,
)?;

Expand Down
40 changes: 6 additions & 34 deletions packages/rs-sdk/src/platform/transition/put_identity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::platform::transition::broadcast_identity::BroadcastRequestForNewIdentity;
use crate::platform::transition::{
address_inputs::{collect_address_infos_from_proof, fetch_inputs_with_nonce, nonce_inc},
broadcast::BroadcastStateTransition,
address_inputs::collect_address_infos_from_proof, broadcast::BroadcastStateTransition,
};
use crate::{Error, Sdk};

Expand Down Expand Up @@ -47,18 +46,12 @@ pub trait PutIdentity<IS: Signer<IdentityPublicKey>>: Waitable {
where
Self: Sized;

/// Creates an identity funded by Platform addresses (nonces fetched automatically).
async fn put_with_address_funding<AS: Signer<PlatformAddress> + Send + Sync>(
&self,
sdk: &Sdk,
inputs: BTreeMap<PlatformAddress, Credits>,
output: Option<(PlatformAddress, Credits)>,
identity_signer: &IS,
input_address_signer: &AS,
settings: Option<PutSettings>,
) -> Result<(Identity, AddressInfos), Error>;

/// Creates an identity funded by Platform addresses using explicit nonces.
///
/// Use [Identity::new_with_input_addresses_and_keys](dpp::identity::Identity::new_with_input_addresses_and_keys)
/// to create an identity. Then use this method to put it to the platform.
///
/// This is a preferred method, as you need to use the same nonces when creating the identity.
async fn put_with_address_funding_with_nonce<AS: Signer<PlatformAddress> + Send + Sync>(
&self,
sdk: &Sdk,
Expand Down Expand Up @@ -112,27 +105,6 @@ impl<IS: Signer<IdentityPublicKey>> PutIdentity<IS> for Identity {
Self::wait_for_response(sdk, state_transition, settings).await
}

async fn put_with_address_funding<AS: Signer<PlatformAddress> + Send + Sync>(
&self,
sdk: &Sdk,
inputs: BTreeMap<PlatformAddress, Credits>,
output: Option<(PlatformAddress, Credits)>,
identity_signer: &IS,
input_address_signer: &AS,
settings: Option<PutSettings>,
) -> Result<(Identity, AddressInfos), Error> {
let inputs_with_nonce = nonce_inc(fetch_inputs_with_nonce(sdk, &inputs).await?);
self.put_with_address_funding_with_nonce(
sdk,
inputs_with_nonce,
output,
identity_signer,
input_address_signer,
settings,
)
.await
}

async fn put_with_address_funding_with_nonce<AS: Signer<PlatformAddress> + Send + Sync>(
&self,
sdk: &Sdk,
Expand Down
Loading