From 02fb3a08a8c2da185555101760a6c95c70c96066 Mon Sep 17 00:00:00 2001 From: pauldelucia Date: Thu, 27 Jun 2024 16:49:34 +0900 Subject: [PATCH] extract copied code to function --- .../rs-sdk/src/platform/transition/vote.rs | 47 +++++++------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/packages/rs-sdk/src/platform/transition/vote.rs b/packages/rs-sdk/src/platform/transition/vote.rs index 8066e5cde6a..b21a16c281b 100644 --- a/packages/rs-sdk/src/platform/transition/vote.rs +++ b/packages/rs-sdk/src/platform/transition/vote.rs @@ -51,21 +51,7 @@ impl PutVote for Vote { signer: &S, settings: Option, ) -> Result<(), Error> { - let pub_key_hash = voting_public_key.public_key_hash()?; - - let mut hasher = Sha256::new(); - hasher.update(voter_pro_tx_hash.as_bytes()); - hasher.update(pub_key_hash); - let voting_identity_id_hashed = hasher.finalize(); - let voting_identity_id = match Identifier::from_bytes(&voting_identity_id_hashed) { - Ok(id) => id, - Err(e) => { - return Err(Error::Generic(format!( - "Couldn't convert id string to Identifier: {}", - e - ))) - } - }; + let voting_identity_id = get_voting_identity_id(voter_pro_tx_hash, voting_public_key)?; let new_masternode_voting_nonce = sdk .get_identity_nonce(voting_identity_id, true, settings) @@ -97,21 +83,7 @@ impl PutVote for Vote { signer: &S, settings: Option, ) -> Result { - let pub_key_hash = voting_public_key.public_key_hash()?; - - let mut hasher = Sha256::new(); - hasher.update(voter_pro_tx_hash.as_bytes()); - hasher.update(pub_key_hash); - let voting_identity_id_hashed = hasher.finalize(); - let voting_identity_id = match Identifier::from_bytes(&voting_identity_id_hashed) { - Ok(id) => id, - Err(e) => { - return Err(Error::Generic(format!( - "Couldn't convert id string to Identifier: {}", - e - ))) - } - }; + let voting_identity_id = get_voting_identity_id(voter_pro_tx_hash, voting_public_key)?; let new_masternode_voting_nonce = sdk .get_identity_nonce(voting_identity_id, true, settings) @@ -175,3 +147,18 @@ impl PutVote for Vote { } } } + +fn get_voting_identity_id( + voter_pro_tx_hash: Identifier, + voting_public_key: &IdentityPublicKey, +) -> Result { + let pub_key_hash = voting_public_key.public_key_hash()?; + + let mut hasher = Sha256::new(); + hasher.update(voter_pro_tx_hash.as_bytes()); + hasher.update(pub_key_hash); + let voting_identity_id_hashed = hasher.finalize(); + + Identifier::from_bytes(&voting_identity_id_hashed) + .map_err(|e| Error::Generic(format!("Couldn't convert id string to Identifier: {}", e))) +}