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

BREAKING - NetworkInfo new field: rpc_url #66

Merged
merged 5 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
run: rustup target list --installed

- name: Cargo check
run: cargo +nightly check
run: cargo check

- name: Check Clippy
run: cargo +nightly clippy
run: cargo clippy

unittest:
needs: install
Expand Down
22 changes: 14 additions & 8 deletions common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ pub type NetworkAddress = Vec<u8>;
/// Used to represent any blockchain in the Polkadot, Kusama or Rococo network.
pub type NetworkId = u32;

/// Used to represent the Ss58 Prefix of a Substrate chain.
pub type Ss58Prefix = u16;
/// We currently support these two address types since XCM is also supporting
/// only these ones.
#[derive(scale::Encode, scale::Decode, Debug, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo, StorageLayout))]
pub enum AccountType {
AccountId32,
AccountKey20,
}

#[derive(scale::Encode, scale::Decode, Debug, Default, PartialEq, Clone)]
#[derive(scale::Encode, scale::Decode, Debug, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo, StorageLayout))]
pub struct NetworkInfo {
/// Each address is associated with a specific blockchain.
pub name: String,
/// This is used on the frontend to ensure the user does not add an address
/// that is not valid on the network he specified.
pub ss58_prefix: Ss58Prefix,
/// We need to know the rpc url of each network otherwise we won't know how
/// to communicate with it.
pub rpc_url: String,
/// We need to know the address type when making XCM transfers.
pub account_type: AccountType,
}
64 changes: 34 additions & 30 deletions contracts/identity/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub use self::identity::{Identity, IdentityRef};
/// Encrypted addresses should never exceed this size limit.
const ADDRESS_SIZE_LIMIT: usize = 128;

/// Limit the name length of a network
const NETWORK_NAME_LIMIT: usize = 128;
/// Limit the rpc url length of a network.
const NETWORK_RPC_URL_LIMIT: usize = 64;

/// All the possible errors that may occur when interacting with the identity
/// contract.
Expand All @@ -29,14 +29,15 @@ pub enum Error {
InvalidNetwork,
AddressSizeExceeded,
NetworkNameTooLong,
NetworkRpcUrlTooLong,
AlreadyIdentityOwner,
}

#[ink::contract]
mod identity {
use super::*;
use crate::types::*;
use common::types::{NetworkInfo, Ss58Prefix, *};
use common::types::{NetworkInfo, *};
use ink::storage::Mapping;

/// Storage
Expand Down Expand Up @@ -132,21 +133,21 @@ mod identity {
/// The `NetworkId` that is associated with the newly added network.
#[ink(topic)]
pub(crate) network_id: NetworkId,
/// The name of the network name that got added.
pub(crate) name: String,
/// The `Ss58Prefix` of the network that got added.
pub(crate) ss58_prefix: Ss58Prefix,
/// The rpc url of the network that got added.
pub(crate) rpc_url: String,
/// The address type used on the network.
pub(crate) account_type: AccountType,
}

#[ink(event)]
pub struct NetworkUpdated {
/// The `NetworkId` that is associated with the updated network.
#[ink(topic)]
pub(crate) network_id: NetworkId,
/// The name of the updated network.
pub(crate) name: String,
/// The `Ss58Prefix` of the updated network.
pub(crate) ss58_prefix: Ss58Prefix,
/// The rpc url of the updated network.
pub(crate) rpc_url: String,
/// The address type used on the updated network.
pub(crate) account_type: AccountType,
}

#[ink(event)]
Expand Down Expand Up @@ -192,10 +193,13 @@ mod identity {
pub fn init_with_networks(networks: Vec<NetworkInfo>) -> Self {
let mut network_info_of = Mapping::default();

// Iterate over all the networks provided and make sure their name
// does not exceed the `NETWORK_NAME_LIMIT`.
// Iterate over all the networks provided and make sure that no
// fields are exceeding the length limits.
networks.clone().into_iter().enumerate().for_each(|(network_id, network)| {
assert!(network.name.len() <= NETWORK_NAME_LIMIT, "Network name is too long");
assert!(
network.rpc_url.len() <= NETWORK_RPC_URL_LIMIT,
"Network rpc url is too long"
);
let network_id = network_id as NetworkId;
network_info_of.insert(network_id, &network);
});
Expand Down Expand Up @@ -376,17 +380,17 @@ mod identity {
// Only the contract owner can add a network
ensure!(caller == self.admin, Error::NotAllowed);

// Ensure that the name of the network doesn't exceed length limit
ensure!(info.name.len() <= NETWORK_NAME_LIMIT, Error::NetworkNameTooLong);
// Ensure that the rpc url is not exceeding the length limit.
ensure!(info.rpc_url.len() <= 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 { name, ss58_prefix } = info;
let NetworkInfo { rpc_url, account_type } = info;

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

Ok(network_id)
}
Expand All @@ -395,8 +399,8 @@ mod identity {
pub fn update_network(
&mut self,
network_id: NetworkId,
new_prefix: Option<Ss58Prefix>,
new_name: Option<String>,
new_rpc_url: Option<String>,
new_address_type: Option<AccountType>,
) -> Result<(), Error> {
let caller = self.env().caller();

Expand All @@ -407,23 +411,23 @@ mod identity {
let mut info =
self.network_info_of.get(network_id).map_or(Err(Error::InvalidNetwork), Ok)?;

// Ensure that the name of the network doesn't exceed length limit
if let Some(name) = new_name {
ensure!(name.len() <= NETWORK_NAME_LIMIT, Error::NetworkNameTooLong);
info.name = name;
// 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;
}

if let Some(prefix) = new_prefix {
info.ss58_prefix = prefix;
if let Some(account_type) = new_address_type {
info.account_type = account_type;
}

// Update storage items
self.network_info_of.insert(network_id, &info);

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

Ok(())
Expand All @@ -437,8 +441,8 @@ mod identity {
ensure!(caller == self.admin, Error::NotAllowed);

// Ensure that the given `network_id` exists
let name = self.network_info_of.get(network_id);
ensure!(name.is_some(), Error::InvalidNetwork);
let network = self.network_info_of.get(network_id);
ensure!(network.is_some(), Error::InvalidNetwork);

self.network_info_of.remove(network_id);

Expand Down
Loading