Skip to content

feat(backend): add contact type and address type. #5815

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions src/shared/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub type Timestamp = u64;
pub mod account;
pub mod backend_config;
pub mod bitcoin;
pub mod contact;
pub mod custom_token;
pub mod dapp;
pub mod migration;
Expand Down
118 changes: 118 additions & 0 deletions src/shared/src/types/contact.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use std::fmt;

use candid::{CandidType, Deserialize};

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct Contact {
pub id: String,
pub name: String,
pub addresses: Vec<ContactAddress>,
pub avatar: Option<String>,
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub enum AddressType {
Copy link
Member

Choose a reason for hiding this comment

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

Note: You may want to sync with @AntonioVentilii on this list. We could have a meeting with the three of us. In particular:

  • We support testnets as well as mainnets for each network, so some clarity is needed: is this a list of protocols, or is it "where to send the message"? E.g. what is the addressType for ckBTC (which is BTC, but using the ICP protocol), what is the address type for a Solana Testnet address, what is the address type for a Solana Devnet Account?

  • There is a risk of having several lists of networks that are all slightly different. One for snapshots, one for rewards, one for rewards, and so on. It might be an idea to have a single list of networks and a single list of protocols.

Copy link
Member

Choose a reason for hiding this comment

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

As discussed, maybe move this to shared/types/?

ICP,
BTC,
ETH,
SOL,
ADA,
DOT,
AVAX,
BSC,
MATIC,
Other(String),
}

impl fmt::Display for AddressType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AddressType::ICP => write!(f, "ICP"),
AddressType::BTC => write!(f, "BTC"),
AddressType::ETH => write!(f, "ETH"),
AddressType::SOL => write!(f, "SOL"),
AddressType::ADA => write!(f, "ADA"),
AddressType::DOT => write!(f, "DOT"),
AddressType::AVAX => write!(f, "AVAX"),
AddressType::BSC => write!(f, "BSC"),
AddressType::MATIC => write!(f, "MATIC"),
AddressType::Other(name) => write!(f, "{name}"),
}
}
}

impl From<&str> for AddressType {
fn from(s: &str) -> Self {
match s.to_uppercase().as_str() {
"ICP" => AddressType::ICP,
"BTC" => AddressType::BTC,
"ETH" => AddressType::ETH,
"SOL" => AddressType::SOL,
"ADA" => AddressType::ADA,
"DOT" => AddressType::DOT,
"AVAX" => AddressType::AVAX,
"BSC" => AddressType::BSC,
"MATIC" => AddressType::MATIC,
_ => AddressType::Other(s.to_string()),
}
}
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct ContactAddress {
pub network_type: AddressType,
pub address: String,
pub label: Option<String>,
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq, Default)]
pub struct ContactSettings {
pub contacts: Vec<Contact>,
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct AddContact {
pub contact: Contact,
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct RemoveContact {
pub contact_id: String,
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct UpdateContact {
pub contact: Contact,
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct AddAddress {
pub contact_id: String,
pub address: ContactAddress,
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct RemoveAddress {
pub contact_id: String,
pub network_type: AddressType,
pub address: String,
}

#[derive(CandidType, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct UpdateAddress {
pub contact_id: String,
pub old_network_type: AddressType,
pub old_address: String,
pub new_address: ContactAddress,
}

#[derive(CandidType, Deserialize, Clone, Eq, PartialEq, Debug)]
pub enum ContactError {
UserNotFound,
ContactNotFound,
ContactIdAlreadyExists,
InvalidContactData,
AddressAlreadyExists,
AddressNotFound,
InvalidAddressFormat,
}