From 837467cfbd263a27d92cfe2256919380e3767221 Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Tue, 20 Aug 2024 15:14:46 -0400 Subject: [PATCH] Move `get_registered_details` fully into client --- crates/client/src/client.rs | 40 +------------------ crates/client/src/errors.rs | 4 +- crates/client/src/substrate.rs | 35 ++++++++++++++++ .../src/helpers/substrate.rs | 33 --------------- .../src/user/api.rs | 4 +- .../src/user/tests.rs | 17 +++----- 6 files changed, 47 insertions(+), 86 deletions(-) diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 1cd29af62..2609cfe1b 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -37,7 +37,7 @@ use crate::{ EntropyConfig, }, client::entropy::staking_extension::events::{EndpointChanged, ThresholdAccountChanged}, - substrate::{query_chain, submit_transaction_with_pair}, + substrate::{get_registered_details, query_chain, submit_transaction_with_pair}, user::{get_signers_from_chain, UserSignatureRequest}, Hasher, }; @@ -359,43 +359,6 @@ pub async fn put_old_register_request_on_chain( Err(ClientError::RegistrationTimeout) } -/// Returns a registered user's key visibility -/// -/// TODO (Nando): This was copied from `entropy-tss::helpers::substrate` -/// -/// What's the best place for this? Ideally here, and then we import this into the entropy-tss, but -/// we need the `full-client` feature enabled... -#[tracing::instrument(skip_all, fields(verifying_key))] -pub async fn get_registered_details( - api: &OnlineClient, - rpc: &LegacyRpcMethods, - verifying_key: Vec, -) -> Result { - tracing::info!("Querying chain for registration info."); - - let registered_info_query = - entropy::storage().registry().registered(BoundedVec(verifying_key.clone())); - let registered_result = query_chain(api, rpc, registered_info_query, None).await?; - - let registration_info = if let Some(old_registration_info) = registered_result { - tracing::debug!("Found user in old `Registered` struct."); - - old_registration_info - } else { - // We failed with the old registration path, let's try the new one - tracing::warn!("Didn't find user in old `Registered` struct, trying new one."); - - let registered_info_query = - entropy::storage().registry().registered_on_chain(BoundedVec(verifying_key)); - - query_chain(api, rpc, registered_info_query, None) - .await? - .ok_or_else(|| ClientError::NotRegistered)? - }; - - Ok(registration_info) -} - /// Check that the verfiying key from a new signature matches that in the from the /// on-chain registration info for a given account pub async fn check_verifying_key( @@ -410,6 +373,7 @@ pub async fn check_verifying_key( entropy::storage().registry().registered(BoundedVec(verifying_key_serialized)); let query_registered_status = query_chain(api, rpc, registered_query, None).await; query_registered_status?.ok_or(ClientError::NotRegistered)?; + Ok(()) } diff --git a/crates/client/src/errors.rs b/crates/client/src/errors.rs index 012bf2d87..54da8b69b 100644 --- a/crates/client/src/errors.rs +++ b/crates/client/src/errors.rs @@ -26,6 +26,8 @@ pub enum SubstrateError { GenericSubstrate(#[from] subxt::error::Error), #[error("Could not sumbit transaction {0}")] BadEvent(String), + #[error("User is not registered on-chain")] + NotRegistered, } /// An error on getting the current subgroup signers @@ -92,7 +94,7 @@ pub enum ClientError { BadRecoveryId, #[error("Cannot parse chain query response: {0}")] TryFromSlice(#[from] std::array::TryFromSliceError), - #[error("User not registered")] + #[error("User is not registered on-chain")] NotRegistered, #[error("No synced validators")] NoSyncedValidators, diff --git a/crates/client/src/substrate.rs b/crates/client/src/substrate.rs index b54022c13..519316f84 100644 --- a/crates/client/src/substrate.rs +++ b/crates/client/src/substrate.rs @@ -13,7 +13,10 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . //! For interacting with the substrate chain node +use crate::chain_api::entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec; +use crate::chain_api::entropy::runtime_types::pallet_registry::pallet::RegisteredInfo; use crate::chain_api::{entropy, EntropyConfig}; + use entropy_shared::MORTALITY_BLOCKS; use sp_core::{sr25519, Pair}; use subxt::{ @@ -107,6 +110,38 @@ where Ok(result) } +/// Returns a registered user's key visibility +#[tracing::instrument(skip_all, fields(verifying_key))] +pub async fn get_registered_details( + api: &OnlineClient, + rpc: &LegacyRpcMethods, + verifying_key: Vec, +) -> Result { + tracing::info!("Querying chain for registration info."); + + let registered_info_query = + entropy::storage().registry().registered(BoundedVec(verifying_key.clone())); + let registered_result = query_chain(api, rpc, registered_info_query, None).await?; + + let registration_info = if let Some(old_registration_info) = registered_result { + tracing::debug!("Found user in old `Registered` struct."); + + old_registration_info + } else { + // We failed with the old registration path, let's try the new one + tracing::warn!("Didn't find user in old `Registered` struct, trying new one."); + + let registered_info_query = + entropy::storage().registry().registered_on_chain(BoundedVec(verifying_key)); + + query_chain(api, rpc, registered_info_query, None) + .await? + .ok_or_else(|| SubstrateError::NotRegistered)? + }; + + Ok(registration_info) +} + /// A wrapper around [sr25519::Pair] which implements [Signer] /// This is needed because on wasm we cannot use the generic `subxt::tx::PairSigner` #[derive(Clone)] diff --git a/crates/threshold-signature-server/src/helpers/substrate.rs b/crates/threshold-signature-server/src/helpers/substrate.rs index 55fcb6772..8a937b249 100644 --- a/crates/threshold-signature-server/src/helpers/substrate.rs +++ b/crates/threshold-signature-server/src/helpers/substrate.rs @@ -20,7 +20,6 @@ use crate::{ self, runtime_types::{ bounded_collections::bounded_vec::BoundedVec, pallet_programs::pallet::ProgramInfo, - pallet_registry::pallet::RegisteredInfo, }, }, EntropyConfig, @@ -72,38 +71,6 @@ pub async fn get_oracle_data( Ok(oracle_info.0) } -/// Returns a registered user's key visibility -#[tracing::instrument(skip_all, fields(verifying_key))] -pub async fn get_registered_details( - api: &OnlineClient, - rpc: &LegacyRpcMethods, - verifying_key: Vec, -) -> Result { - tracing::info!("Querying chain for registration info."); - - let registered_info_query = - entropy::storage().registry().registered(BoundedVec(verifying_key.clone())); - let registered_result = query_chain(api, rpc, registered_info_query, None).await?; - - let registration_info = if let Some(old_registration_info) = registered_result { - tracing::debug!("Found user in old `Registered` struct."); - - old_registration_info - } else { - // We failed with the old registration path, let's try the new one - tracing::warn!("Didn't find user in old `Registered` struct, trying new one."); - - let registered_info_query = - entropy::storage().registry().registered_on_chain(BoundedVec(verifying_key)); - - query_chain(api, rpc, registered_info_query, None) - .await? - .ok_or_else(|| UserErr::ChainFetch("Not Registering error: Register Onchain first"))? - }; - - Ok(registration_info) -} - /// Takes Stash keys and returns validator info from chain pub async fn get_validators_info( api: &OnlineClient, diff --git a/crates/threshold-signature-server/src/user/api.rs b/crates/threshold-signature-server/src/user/api.rs index ef75bea2d..2879ce19a 100644 --- a/crates/threshold-signature-server/src/user/api.rs +++ b/crates/threshold-signature-server/src/user/api.rs @@ -26,6 +26,7 @@ use axum::{ use base64::prelude::{Engine, BASE64_STANDARD}; use bip39::{Language, Mnemonic}; use blake2::{Blake2s256, Digest}; +use entropy_client::substrate::get_registered_details; use entropy_kvdb::kv_manager::{ error::{InnerKvError, KvError}, helpers::serialize as key_serialize, @@ -68,8 +69,7 @@ use crate::{ launch::LATEST_BLOCK_NUMBER_NEW_USER, signing::{do_signing, Hasher}, substrate::{ - get_oracle_data, get_program, get_registered_details, get_stash_address, query_chain, - submit_transaction, + get_oracle_data, get_program, get_stash_address, query_chain, submit_transaction, }, user::{check_in_registration_group, compute_hash, do_dkg}, validator::{get_signer, get_signer_and_x25519_secret}, diff --git a/crates/threshold-signature-server/src/user/tests.rs b/crates/threshold-signature-server/src/user/tests.rs index 98c6e31c6..ed632d564 100644 --- a/crates/threshold-signature-server/src/user/tests.rs +++ b/crates/threshold-signature-server/src/user/tests.rs @@ -17,6 +17,7 @@ use axum::http::StatusCode; use base64::prelude::{Engine, BASE64_STANDARD}; use bip39::{Language, Mnemonic}; use blake3::hash; +use entropy_client::substrate::get_registered_details; use entropy_client::{ client::{sign, store_program, update_programs}, user::get_signers_from_chain, @@ -430,12 +431,8 @@ async fn signature_request_with_derived_account_works() { let actual_verifying_key = actual_verifying_key.0; // Next we want to check that the info that's on-chain is what we actually expect - let registered_info = crate::helpers::substrate::get_registered_details( - &entropy_api, - &rpc, - actual_verifying_key.to_vec(), - ) - .await; + let registered_info = + get_registered_details(&entropy_api, &rpc, actual_verifying_key.to_vec()).await; assert!( matches!(registered_info, Ok(_)), @@ -1526,12 +1523,8 @@ async fn test_new_registration_flow() { let actual_verifying_key = actual_verifying_key.0; // Next we want to check that the info that's on-chain is what we actually expect - let registered_info = crate::helpers::substrate::get_registered_details( - &entropy_api, - &rpc, - actual_verifying_key.to_vec(), - ) - .await; + let registered_info = + get_registered_details(&entropy_api, &rpc, actual_verifying_key.to_vec()).await; assert!( matches!(registered_info, Ok(_)),