Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty web3networks when linking identities #2352

Merged
merged 3 commits into from
Dec 22, 2023
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
17 changes: 10 additions & 7 deletions tee-worker/litentry/core/data-providers/src/geniidata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ use crate::sgx_reexport_prelude::*;
#[cfg(all(not(feature = "std"), feature = "sgx"))]
extern crate sgx_tstd as std;

use crate::{
build_client, Error as DataProviderError,
GLOBAL_DATA_PROVIDER_CONFIG,
};
use crate::{build_client, Error as DataProviderError, GLOBAL_DATA_PROVIDER_CONFIG};
use http::header::ACCEPT;
use http_req::response::Headers;
use itc_rest_client::{
error::Error as RestClientError,
http_client::{HttpClient, DefaultSend},
http_client::{DefaultSend, HttpClient},
rest_client::RestClient,
RestGet, RestPath,
};
Expand Down Expand Up @@ -82,9 +79,15 @@ impl GeniidataClient {
pub fn new() -> Self {
let mut headers = Headers::new();
headers.insert(ACCEPT.as_str(), "application/json");
headers.insert("api-key", GLOBAL_DATA_PROVIDER_CONFIG.read().unwrap().geniidata_api_key.as_str());
headers.insert(
"api-key",
GLOBAL_DATA_PROVIDER_CONFIG.read().unwrap().geniidata_api_key.as_str(),
);
Kailai-Wang marked this conversation as resolved.
Show resolved Hide resolved

let client = build_client(GLOBAL_DATA_PROVIDER_CONFIG.read().unwrap().geniidata_url.as_str(), headers);
let client = build_client(
GLOBAL_DATA_PROVIDER_CONFIG.read().unwrap().geniidata_url.as_str(),
headers,
);

GeniidataClient { client }
}
Expand Down
30 changes: 23 additions & 7 deletions tee-worker/litentry/pallets/identity-management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ use frame_support::{pallet_prelude::*, traits::StorageVersion};
use frame_system::pallet_prelude::*;

pub use litentry_primitives::{
all_substrate_web3networks, Identity, ParentchainBlockNumber, UserShieldingKeyType, Web3Network,
all_bitcoin_web3networks, all_evm_web3networks, all_substrate_web3networks, Identity,
ParentchainBlockNumber, UserShieldingKeyType, Web3Network,
};
use sp_std::vec::Vec;

Expand Down Expand Up @@ -170,13 +171,28 @@ pub mod pallet {
);
ensure!(identity != who, Error::<T>::LinkPrimeIdentityDisallowed);

ensure!(
identity.matches_web3networks(web3networks.as_ref()),
Error::<T>::WrongWeb3NetworkTypes
);
let mut adjusted_web3networks = web3networks;

// To reduce the encoded call size, the client has a workaround on `staging` branch to
// pass in empty `web3networks`, we'll need to restore it to full network types
if identity.is_web3() && adjusted_web3networks.is_empty() {
adjusted_web3networks = match identity {
Identity::Substrate(..) => all_substrate_web3networks(),
Identity::Evm(..) => all_evm_web3networks(),
Identity::Bitcoin(..) => all_bitcoin_web3networks(),
_ => Vec::new(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it throw here and panic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally we don't panic inside pallet, it's better to return an error here - but since it's on staging, I didn't even bother adding another error type (which requires refreshment of sidechain metadata) as I know it's unreachable.

};
} else {
ensure!(
identity.matches_web3networks(adjusted_web3networks.as_ref()),
Error::<T>::WrongWeb3NetworkTypes
);
}

let context =
<IdentityContext<T>>::new(<frame_system::Pallet<T>>::block_number(), web3networks);
let context = <IdentityContext<T>>::new(
<frame_system::Pallet<T>>::block_number(),
adjusted_web3networks,
);
Self::insert_identity_with_limit(&who, &identity, context)?;
Self::deposit_event(Event::IdentityLinked { who, identity });
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions tee-worker/litentry/pallets/identity-management/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use frame_support::{
};
use frame_system as system;
use frame_system::EnsureSignedBy;
use litentry_primitives::{Identity, IdentityString, USER_SHIELDING_KEY_LEN};
use litentry_primitives::{Identity, IdentityString, Web3Network, USER_SHIELDING_KEY_LEN};
use sp_core::H256;
use sp_runtime::{
testing::Header,
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn new_test_ext(set_shielding_key: bool) -> sp_io::TestExternalities {
RuntimeOrigin::signed(ALICE),
who,
shielding_key.clone(),
vec![],
vec![Web3Network::Litentry],
);
}
});
Expand Down
31 changes: 27 additions & 4 deletions tee-worker/litentry/pallets/identity-management/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn set_user_shielding_key_works() {
RuntimeOrigin::signed(ALICE),
who.clone(),
shielding_key,
vec![],
vec![Web3Network::Litentry],
));
assert_eq!(IMT::user_shielding_keys(who.clone()), Some(shielding_key));
System::assert_last_event(RuntimeEvent::IMT(crate::Event::UserShieldingKeySet {
Expand Down Expand Up @@ -121,6 +121,29 @@ fn link_identity_with_wrong_network_fails() {
});
}

#[test]
fn link_identity_with_empty_network_works() {
new_test_ext(true).execute_with(|| {
let web3networks: Vec<Web3Network> = vec![];
let who: Identity = BOB.into();
assert_ok!(IMT::link_identity(
RuntimeOrigin::signed(ALICE),
who.clone(),
alice_evm_identity(),
web3networks,
));
assert_eq!(
IMT::id_graphs(who.clone(), alice_evm_identity()).unwrap(),
IdentityContext {
link_block: 1,
web3networks: vec![Web3Network::Ethereum, Web3Network::Bsc].try_into().unwrap(),
Kailai-Wang marked this conversation as resolved.
Show resolved Hide resolved
status: IdentityStatus::Active
}
);
assert_eq!(crate::IDGraphLens::<Test>::get(&who), 2);
});
}

#[test]
fn cannot_link_identity_again() {
new_test_ext(true).execute_with(|| {
Expand Down Expand Up @@ -194,7 +217,7 @@ fn remove_identity_works() {
RuntimeOrigin::signed(ALICE),
who.clone(),
shielding_key,
vec![],
vec![Web3Network::Litentry],
));
assert_noop!(
IMT::remove_identity(
Expand Down Expand Up @@ -256,7 +279,7 @@ fn set_identity_networks_works() {
RuntimeOrigin::signed(ALICE),
who.clone(),
shielding_key,
vec![],
vec![Web3Network::Litentry],
));
assert_noop!(
IMT::remove_identity(
Expand Down Expand Up @@ -305,7 +328,7 @@ fn set_identity_networks_with_wrong_network_fails() {
RuntimeOrigin::signed(ALICE),
who.clone(),
shielding_key,
vec![],
vec![Web3Network::Litentry],
));
assert_noop!(
IMT::remove_identity(
Expand Down