Skip to content

Commit

Permalink
Merge pull request #68 from TheDotflow/multiple-rpcs
Browse files Browse the repository at this point in the history
Support multiple RPCs
  • Loading branch information
cuteolaf authored Jul 31, 2023
2 parents 27b8fb8 + a7923a7 commit 74dcd95
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 69 deletions.
9 changes: 8 additions & 1 deletion common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ pub enum AccountType {
pub struct NetworkInfo {
/// We need to know the rpc url of each network otherwise we won't know how
/// to communicate with it.
pub rpc_url: String,
pub rpc_urls: Vec<String>,
/// We need to know the address type when making XCM transfers.
pub account_type: AccountType,
}

impl NetworkInfo {
// Makes sure none of the network's urls exceed the size limit.
pub fn ensure_rpc_url_size_limit(&self, limit: usize) -> bool {
self.rpc_urls.iter().all(|url| url.len() <= limit)
}
}
23 changes: 14 additions & 9 deletions contracts/identity/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mod identity {
#[ink(topic)]
pub(crate) network_id: NetworkId,
/// The rpc url of the network that got added.
pub(crate) rpc_url: String,
pub(crate) rpc_urls: Vec<String>,
/// The address type used on the network.
pub(crate) account_type: AccountType,
}
Expand All @@ -145,7 +145,7 @@ mod identity {
#[ink(topic)]
pub(crate) network_id: NetworkId,
/// The rpc url of the updated network.
pub(crate) rpc_url: String,
pub(crate) rpc_urls: Vec<String>,
/// The address type used on the updated network.
pub(crate) account_type: AccountType,
}
Expand Down Expand Up @@ -197,7 +197,7 @@ mod identity {
// fields are exceeding the length limits.
networks.clone().into_iter().enumerate().for_each(|(network_id, network)| {
assert!(
network.rpc_url.len() <= NETWORK_RPC_URL_LIMIT,
network.ensure_rpc_url_size_limit(NETWORK_RPC_URL_LIMIT),
"Network rpc url is too long"
);
let network_id = network_id as NetworkId;
Expand Down Expand Up @@ -381,16 +381,19 @@ mod identity {
ensure!(caller == self.admin, Error::NotAllowed);

// Ensure that the rpc url is not exceeding the length limit.
ensure!(info.rpc_url.len() <= NETWORK_RPC_URL_LIMIT, Error::NetworkRpcUrlTooLong);
ensure!(
info.ensure_rpc_url_size_limit(NETWORK_RPC_URL_LIMIT),
Error::NetworkRpcUrlTooLong
);

let network_id = self.network_id_count;
self.network_info_of.insert(network_id, &info);

self.network_id_count = self.network_id_count.saturating_add(1);

let NetworkInfo { rpc_url, account_type } = info;
let NetworkInfo { rpc_urls, account_type } = info;

self.env().emit_event(NetworkAdded { network_id, rpc_url, account_type });
self.env().emit_event(NetworkAdded { network_id, rpc_urls, account_type });

Ok(network_id)
}
Expand All @@ -414,7 +417,7 @@ mod identity {
// Ensure that the rpc url of the network doesn't exceed length limit.
if let Some(rpc_url) = new_rpc_url {
ensure!(rpc_url.len() <= NETWORK_RPC_URL_LIMIT, Error::NetworkRpcUrlTooLong);
info.rpc_url = rpc_url;
info.rpc_urls.push(rpc_url);
}

if let Some(account_type) = new_address_type {
Expand All @@ -426,7 +429,7 @@ mod identity {

self.env().emit_event(NetworkUpdated {
network_id,
rpc_url: info.rpc_url,
rpc_urls: info.rpc_urls,
account_type: info.account_type,
});

Expand Down Expand Up @@ -480,7 +483,9 @@ mod identity {
let caller = self.env().caller();

let is_recovery_account = self.recovery_account_of.get(identity_no) == Some(caller);
let Some(identity_owner) = self.owner_of(identity_no) else { return Err(Error::NotAllowed) };
let Some(identity_owner) = self.owner_of(identity_no) else {
return Err(Error::NotAllowed)
};

ensure!(identity_owner == caller || is_recovery_account, Error::NotAllowed);
// The new owner cannot already have an identity since we allow only
Expand Down
Loading

0 comments on commit 74dcd95

Please sign in to comment.