Skip to content

Commit

Permalink
Add identity file to althea types lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ch-iara committed Nov 5, 2024
1 parent 4440067 commit de15f57
Show file tree
Hide file tree
Showing 41 changed files with 53 additions and 139 deletions.
3 changes: 2 additions & 1 deletion althea_types/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Identity {
};
AbiToken::Struct(vec![
AbiToken::Uint(u128::from_be_bytes(ip_bytes).into()),
AbiToken::Uint(self.wg_public_key.into()),
AbiToken::Uint(self.wg_public_key.as_bytes().into()),
AbiToken::Address(self.eth_address),
])
}
Expand Down Expand Up @@ -253,6 +253,7 @@ impl Identity {
)))
}
})
.to_be_bytes()
.into();

// 3rd entry is the eth address
Expand Down
94 changes: 1 addition & 93 deletions althea_types/src/interop.rs
Original file line number Diff line number Diff line change
@@ -1,114 +1,22 @@
use crate::identity::Identity;
use crate::wg_key::WgKey;
use crate::{ClientExtender, WifiDevice};
use arrayvec::ArrayString;
use babel_monitor::structs::Neighbor;
use babel_monitor::structs::Route;
use clarity::Address;
use deep_space::Address as AltheaAddress;
use ipnetwork::IpNetwork;
use num256::Uint256;
use serde::Serialize;
use serde::{Deserialize, Deserializer, Serializer};
use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::fmt::Display;
use std::hash::{Hash, Hasher};
use std::net::IpAddr;
use std::str::FromStr;
use std::time::{Duration, SystemTime};

/// This is how nodes are identified.
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct Identity {
pub mesh_ip: IpAddr,
pub eth_address: Address,
pub wg_public_key: WgKey,
pub nickname: Option<ArrayString<32>>,
}

impl Display for Identity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.nickname {
Some(nick) => {
write!(
f,
"nickname: {}, mesh_ip: {}, eth_address: {}, althea_address: {:?}, wg_pubkey {}",
nick, self.mesh_ip, self.eth_address, self.get_althea_address(), self.wg_public_key
)
}
None => write!(
f,
"mesh_ip: {}, eth_address: {}, althea_address: {:?}, wg_pubkey {}",
self.mesh_ip,
self.eth_address,
self.get_althea_address(),
self.wg_public_key
),
}
}
}

pub const ALTHEA_PREFIX: &str = "althea";

impl Identity {
pub fn new(
mesh_ip: IpAddr,
eth_address: Address,
wg_public_key: WgKey,
nickname: Option<ArrayString<32>>,
) -> Identity {
Identity {
mesh_ip,
eth_address,
wg_public_key,
nickname,
}
}

/// Returns true if this identity is converged, meaning the Althea address is
/// derived from and is interchangeable with the ETH address. If false we have
/// to avoid assumptions avoid these being the same private key
pub fn get_althea_address(&self) -> AltheaAddress {
AltheaAddress::from_slice(self.eth_address.as_bytes(), ALTHEA_PREFIX).unwrap()
}

pub fn get_hash(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
hasher.finish()
}

pub fn get_hash_array(&self) -> [u8; 8] {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
let bits = hasher.finish();
bits.to_be_bytes()
}
}

// Comparison ignoring nicknames to allow changing
// nicknames without breaking everything
impl PartialEq for Identity {
fn eq(&self, other: &Identity) -> bool {
self.mesh_ip == other.mesh_ip
&& self.eth_address == other.eth_address
&& self.wg_public_key == other.wg_public_key
}
}

// I don't understand why we need this
// docs insist on it though https://doc.rust-lang.org/std/cmp/trait.Eq.html
impl Eq for Identity {}

// Custom hash implementation that also ignores nickname
impl Hash for Identity {
fn hash<H: Hasher>(&self, state: &mut H) {
self.mesh_ip.hash(state);
self.eth_address.hash(state);
self.wg_public_key.hash(state);
}
}

#[derive(PartialEq, Eq, Hash, Clone, Debug, Serialize, Deserialize)]
pub struct Denom {
/// String representation of token, ex, ualthea, wei, from athea chain will be some unpredictable ibc/<hash>
Expand Down
5 changes: 3 additions & 2 deletions althea_types/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use std::time::Duration;
use babel_monitor::structs::BabeldConfig;
use clarity::Address;

use crate::identity::Identity;
use crate::{
websockets::OperatorAction, BillingDetails, ContactType, ExitConnection, HardwareInfo,
Identity, InstallationDetails, NeighborStatus, ShaperSettings, SystemChain, UpdateType,
UpdateTypeLegacy, UsageTrackerFlat, UsageTrackerTransfer,
InstallationDetails, NeighborStatus, ShaperSettings, SystemChain, UpdateType, UpdateTypeLegacy,
UsageTrackerFlat, UsageTrackerTransfer,
};

fn default_ops_last_seen_usage_hour() -> u64 {
Expand Down
1 change: 1 addition & 0 deletions althea_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate serde_derive;

pub mod contact_info;
pub mod error;
pub mod identity;
pub mod interop;
pub mod legacy;
pub mod monitoring;
Expand Down
3 changes: 2 additions & 1 deletion althea_types/src/websockets/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ mod tests {
use std::net::IpAddr;

use crate::{
identity::Identity,
websockets::{OperatorWebsocketMessage, RouterWebsocketMessage, WsOperatorAddressStruct},
Identity, WgKey,
WgKey,
};

// test encryption and decryption of router messages
Expand Down
4 changes: 2 additions & 2 deletions althea_types/src/websockets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use num256::Uint256;
pub mod encryption;

use crate::{
BillingDetails, ContactType, ExitConnection, HardwareInfo, Identity, InstallationDetails,
NeighborStatus, ShaperSettings, SystemChain, UpdateType, UpdateTypeLegacy,
identity::Identity, BillingDetails, ContactType, ExitConnection, HardwareInfo,
InstallationDetails, NeighborStatus, ShaperSettings, SystemChain, UpdateType, UpdateTypeLegacy,
UsageTrackerTransfer, WgKey, WifiToken,
};

Expand Down
2 changes: 1 addition & 1 deletion antenna_forwarding_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate lazy_static;

use althea_kernel_interface::KernelInterface;
use althea_kernel_interface::LinuxCommandRunner;
use althea_types::Identity;
use althea_types::identity::Identity;
use althea_types::WgKey;
use antenna_forwarding_protocol::process_streams;
use antenna_forwarding_protocol::write_all_spinlock;
Expand Down
2 changes: 1 addition & 1 deletion antenna_forwarding_protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern crate log;
#[macro_use]
extern crate lazy_static;

use althea_types::Identity;
use althea_types::identity::Identity;
use althea_types::WgKey;
use sodiumoxide::crypto::box_;
use sodiumoxide::crypto::box_::Nonce;
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/src/setup_utils/rita.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::namespaces::get_nsfd;
use super::namespaces::NamespaceInfo;
use super::namespaces::NodeType;
use althea_kernel_interface::KernelInterfaceError;
use althea_types::Identity;
use althea_types::identity::Identity;
use clarity::Address;
use ipnetwork::IpNetwork;
use ipnetwork::Ipv6Network;
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use althea_proto::{
query_client::QueryClient, Metadata, QueryDenomMetadataRequest,
},
};
use althea_types::{ContactType, Denom, Identity, SystemChain, WgKey};
use althea_types::{identity::Identity, ContactType, Denom, SystemChain, WgKey};
use awc::http::StatusCode;
use babel_monitor::{open_babel_stream, parse_routes, structs::Route};
use clarity::{Address, Transaction, Uint256};
Expand Down
2 changes: 1 addition & 1 deletion rita_client/src/dashboard/neighbors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use actix_web_async::http::StatusCode;
use actix_web_async::{HttpRequest, HttpResponse};
use althea_types::Identity;
use althea_types::identity::Identity;
use arrayvec::ArrayString;
use babel_monitor::parsing::get_installed_route;
use babel_monitor::parsing::get_route_via_neigh;
Expand Down
2 changes: 1 addition & 1 deletion rita_client/src/exit_manager/exit_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::exit_manager::{
};
use crate::traffic_watcher::{query_exit_debts, QueryExitDebts};
use actix_async::System as AsyncSystem;
use althea_types::identity::Identity;
use althea_types::ExitList;
use althea_types::ExitState;
use althea_types::Identity;
use futures::future::join_all;
use futures::join;
use rita_common::blockchain_oracle::low_balance;
Expand Down
2 changes: 1 addition & 1 deletion rita_client/src/exit_manager/exit_switcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ pub fn get_babel_routes(babel_port: u16) -> Result<Vec<Route>, RitaClientError>
#[cfg(test)]
mod tests {

use althea_types::{FromStr, Identity, WgKey};
use althea_types::{identity::Identity, FromStr, WgKey};
use clarity::Address;
use ipnetwork::IpNetwork;

Expand Down
2 changes: 1 addition & 1 deletion rita_client/src/heartbeat/dummy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This module contains a bunch of dummy functions that are called to populate a dummy exit so that heart beats
//! send data to op tools without actually being connected to an exit
use althea_types::identity::Identity;
use althea_types::ExitDetails;
use althea_types::FromStr;
use althea_types::Identity;
use althea_types::LocalIdentity;
use althea_types::WgKey;
use babel_monitor::structs::Neighbor;
Expand Down
2 changes: 1 addition & 1 deletion rita_client/src/heartbeat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use rita_common::tunnel_manager::Neighbor as RitaNeighbor;

use crate::exit_manager::get_selected_exit_ip as get_selected_exit_em;

use althea_types::identity::Identity;
use althea_types::HeartbeatMessage;
use althea_types::Identity;
use althea_types::WgKey;
use babel_monitor::structs::Neighbor;
use babel_monitor::structs::Route;
Expand Down
2 changes: 1 addition & 1 deletion rita_client/src/operator_fee_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! will need to be re-written to better reflect a normal billing system at some point, perhaps
//! querying an API for an individual bill. As this is not designed to be a trustless payment
use althea_types::Identity;
use althea_types::identity::Identity;
use althea_types::PaymentTx;
use num256::Uint256;
use rita_common::blockchain_oracle::get_oracle_balance;
Expand Down
2 changes: 1 addition & 1 deletion rita_client/src/operator_update/ops_websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use crate::operator_update::{
use actix_async::System;
use actix_web_actors::ws;
use althea_types::{
identity::Identity,
websockets::{
OperatorWebsocketResponse, RouterWebsocketMessage, WsConnectionDetailsStruct,
WsCustomerDetailsStruct, WsOperatorAddressStruct, WsTimeseriesDataStruct,
},
Identity,
};
use awc::ws::Frame;
use crypto_box::{PublicKey, SecretKey};
Expand Down
2 changes: 1 addition & 1 deletion rita_client/src/traffic_watcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use crate::rita_loop::is_gateway_client;
use crate::RitaClientError;
use althea_types::Identity;
use althea_types::identity::Identity;
use babel_monitor::parsing::get_installed_route;
use babel_monitor::structs::BabelMonitorError;
use babel_monitor::structs::Route;
Expand Down
2 changes: 1 addition & 1 deletion rita_client_registration/src/client_db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{net::IpAddr, time::Duration};

use althea_types::{Identity, WgKey};
use althea_types::{identity::Identity, WgKey};
use clarity::{
abi::{encode_call, AbiToken},
utils::bytes_to_hex_str,
Expand Down
2 changes: 1 addition & 1 deletion rita_client_registration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
time::Duration,
};

use althea_types::{ExitClientIdentity, Identity, WgKey};
use althea_types::{identity::Identity, ExitClientIdentity, WgKey};
use awc::error::{JsonPayloadError, SendRequestError};
use clarity::Address;
use phonenumber::PhoneNumber;
Expand Down
2 changes: 1 addition & 1 deletion rita_common/src/dashboard/debts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::debt_keeper::get_debts_list;
use crate::debt_keeper::traffic_replace;
use crate::debt_keeper::Traffic;
use actix_web_async::{web::Json, HttpRequest, HttpResponse};
use althea_types::Identity;
use althea_types::identity::Identity;

pub async fn get_debts(_req: HttpRequest) -> HttpResponse {
trace!("get_debts: Hit");
Expand Down
2 changes: 1 addition & 1 deletion rita_common/src/debt_keeper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::tunnel_manager::TunnelAction;
use crate::tunnel_manager::TunnelChange;
use crate::RitaCommonError;
use crate::KI;
use althea_types::identity::Identity;
use althea_types::Denom;
use althea_types::Identity;
use althea_types::UnpublishedPaymentTx;
use num256::{Int256, Uint256};
use num_traits::identities::Zero;
Expand Down
2 changes: 1 addition & 1 deletion rita_common/src/payment_controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::payment_validator::ToValidate;
use crate::payment_validator::{ALTHEA_CHAIN_PREFIX, ALTHEA_CONTACT_TIMEOUT};
use crate::rita_loop::get_web3_server;
use althea_types::interop::UnpublishedPaymentTx;
use althea_types::{identity::Identity, SystemChain};
use althea_types::{Denom, PaymentTx};
use althea_types::{Identity, SystemChain};
use awc;
use deep_space::client::ChainStatus;
use deep_space::{Coin, Contact, EthermintPrivateKey};
Expand Down
2 changes: 1 addition & 1 deletion rita_common/src/payment_validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::usage_tracker::update_payments;
use crate::RitaCommonError;
use crate::KI;
use althea_proto::althea::microtx::v1::MsgMicrotx;
use althea_types::identity::Identity;
use althea_types::Denom;
use althea_types::Identity;
use althea_types::PaymentTx;
use althea_types::SystemChain;
use clarity::Address;
Expand Down
8 changes: 4 additions & 4 deletions rita_common/src/peer_listener/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ fn test_decode_imhere_with_multicast_interface() {
fn test_hello_serde() {
use crate::peer_listener::Hello;
use crate::peer_listener::Peer;
use althea_types::Identity;
use althea_types::identity::Identity;
use althea_types::LocalIdentity;
use althea_types::WgKey;
use bincode;
Expand Down Expand Up @@ -293,7 +293,7 @@ fn test_hello_serde() {
fn test_encoded_hello_size() {
use crate::peer_listener::Hello;
use crate::peer_listener::Peer;
use althea_types::Identity;
use althea_types::identity::Identity;
use althea_types::LocalIdentity;
use althea_types::WgKey;
use clarity::Address;
Expand Down Expand Up @@ -348,7 +348,7 @@ fn test_encoded_hello_size() {
fn test_hello_encode_decode() {
use crate::peer_listener::Hello;
use crate::peer_listener::Peer;
use althea_types::Identity;
use althea_types::identity::Identity;
use althea_types::LocalIdentity;
use althea_types::WgKey;
use clarity::Address;
Expand Down Expand Up @@ -412,7 +412,7 @@ fn test_hello_encode_decode() {
fn test_deserialize_with_wrong_serialization() {
use crate::peer_listener::Hello;
use crate::peer_listener::Peer;
use althea_types::Identity;
use althea_types::identity::Identity;
use althea_types::LocalIdentity;
use althea_types::WgKey;
use clarity::Address;
Expand Down
2 changes: 1 addition & 1 deletion rita_common/src/simulated_txfee_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::payment_controller::TRANSACTION_SUBMISSION_TIMEOUT;
use crate::rita_loop::get_web3_server;
use crate::usage_tracker::update_payments;
use crate::KI;
use althea_types::Identity;
use althea_types::identity::Identity;
use althea_types::PaymentTx;
use num256::Uint256;
use num_traits::{Signed, Zero};
Expand Down
Loading

0 comments on commit de15f57

Please sign in to comment.