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

Add network property to ChainInfo #79

Merged
merged 5 commits into from
Sep 23, 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
11 changes: 10 additions & 1 deletion common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub type IdentityNo = u32;
pub type EncryptedAddress = Vec<u8>;

/// Used to represent any blockchain in the Polkadot, Kusama or Rococo chain.
pub type ChainId = u32;
///
/// We also need the network to differentiate two parachains with the same paraId.
pub type ChainId = (u32, Network);

/// We currently support these two address types since XCM is also supporting
/// only these ones.
Expand All @@ -25,6 +27,13 @@ pub enum AccountType {
AccountKey20,
}

#[derive(scale::Encode, scale::Decode, Debug, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo, StorageLayout))]
pub enum Network {
Polkadot,
Kusama,
}

#[derive(scale::Encode, scale::Decode, Debug, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo, StorageLayout))]
pub struct ChainInfo {
Expand Down
35 changes: 19 additions & 16 deletions contracts/identity/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ mod identity {
/// so this storage value keeps track of that.
pub(crate) latest_identity_no: IdentityNo,

/// The chain information associated with a specific `ChainId`.
/// The chain information associated with a specific `ChainId` on the
/// specific network.
///
/// NOTE: This mapping is only modifiable by the admin.
pub(crate) chain_info_of: Mapping<ChainId, ChainInfo>,
Expand Down Expand Up @@ -201,7 +202,6 @@ mod identity {
.into_iter()
.zip(chains.into_iter())
.for_each(|(chain_id, chain)| {
let chain_id = chain_id as ChainId;
chain_info_of.insert(chain_id, &chain);
});

Expand Down Expand Up @@ -262,14 +262,16 @@ mod identity {
}
}

/// A list of all the available chains each associated with a `ChainId`.
/// A list of all the available chains each associated with the associated
/// `ChainId`.
#[ink(message)]
pub fn available_chains(&self) -> Vec<(ChainId, ChainInfo)> {
pub fn available_chains(&self, network: Network) -> Vec<(u32, ChainInfo)> {
self.chain_ids
.clone()
.into_iter()
.map(|id| (id, self.chain_info_of(id)))
.filter_map(|(id, maybe_chain)| maybe_chain.map(|info| (id, info)))
.map(|id| (id.clone(), self.chain_info_of(id)))
.filter(|(id, _)| id.1 == network)
.filter_map(|(id, maybe_chain)| maybe_chain.map(|info| (id.0, info)))
.collect()
}

Expand Down Expand Up @@ -310,7 +312,7 @@ mod identity {

let mut identity_info = self.get_identity_info_of_caller(caller)?;

identity_info.add_address(chain, address.clone())?;
identity_info.add_address(chain.clone(), address.clone())?;
self.number_to_identity.insert(identity_no, &identity_info);

self.env().emit_event(AddressAdded { identity_no, chain, address });
Expand All @@ -331,7 +333,7 @@ mod identity {

let mut identity_info = self.get_identity_info_of_caller(caller)?;

identity_info.update_address(chain, address.clone())?;
identity_info.update_address(chain.clone(), address.clone())?;
self.number_to_identity.insert(identity_no, &identity_info);

self.env()
Expand All @@ -349,7 +351,7 @@ mod identity {

let mut identity_info = self.get_identity_info_of_caller(caller)?;

identity_info.remove_address(chain)?;
identity_info.remove_address(chain.clone())?;
self.number_to_identity.insert(identity_no, &identity_info);

self.env().emit_event(AddressRemoved { identity_no, chain });
Expand Down Expand Up @@ -380,8 +382,8 @@ mod identity {
// Only the contract owner can add a chain
ensure!(caller == self.admin, Error::NotAllowed);

self.chain_info_of.insert(chain_id, &info);
self.chain_ids.push(chain_id);
self.chain_info_of.insert(chain_id.clone(), &info);
self.chain_ids.push(chain_id.clone());

let ChainInfo { account_type } = info;

Expand All @@ -402,14 +404,15 @@ mod identity {
ensure!(caller == self.admin, Error::NotAllowed);

// Ensure that the given chain id exists
let mut info = self.chain_info_of.get(chain_id).map_or(Err(Error::InvalidChain), Ok)?;
let mut info =
self.chain_info_of.get(chain_id.clone()).map_or(Err(Error::InvalidChain), Ok)?;

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

// Update storage items
self.chain_info_of.insert(chain_id, &info);
self.chain_info_of.insert(chain_id.clone(), &info);

self.env()
.emit_event(ChainUpdated { chain_id, account_type: info.account_type });
Expand All @@ -425,11 +428,11 @@ mod identity {
ensure!(caller == self.admin, Error::NotAllowed);

// Ensure that the given `chain_id` exists
let chain = self.chain_info_of.get(chain_id);
let chain = self.chain_info_of.get(chain_id.clone());
ensure!(chain.is_some(), Error::InvalidChain);

self.chain_info_of.remove(chain_id);
self.chain_ids.retain(|&c_id| c_id != chain_id);
self.chain_info_of.remove(chain_id.clone());
self.chain_ids.retain(|c_id| *c_id != chain_id.clone());

self.env().emit_event(ChainRemoved { chain_id });

Expand Down
Loading
Loading