diff --git a/nssa/core/src/account.rs b/nssa/core/src/account.rs index ddae797..889592d 100644 --- a/nssa/core/src/account.rs +++ b/nssa/core/src/account.rs @@ -1,4 +1,4 @@ -use crate::program::ProgramId; +use crate::{address::Address, program::ProgramId}; use serde::{Deserialize, Serialize}; pub type Nonce = u128; @@ -14,16 +14,17 @@ pub struct Account { pub nonce: Nonce, } -/// A fingerprint of the owner of an account. This can be, for example, an `Address` in case the account -/// is public, or a `NullifierPublicKey` in case the account is private. -#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] -#[cfg_attr(any(feature = "host", test), derive(Debug))] -pub struct AccountId(pub(super) [u8; 32]); -impl AccountId { - pub fn new(value: [u8; 32]) -> Self { - Self(value) - } -} +// /// A fingerprint of the owner of an account. This can be, for example, an `Address` in case the account +// /// is public, or a `NullifierPublicKey` in case the account is private. +// #[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] +// #[cfg_attr(any(feature = "host", test), derive(Debug))] +// pub struct AccountId(pub(super) [u8; 32]); +// impl AccountId { +// pub fn new(value: [u8; 32]) -> Self { +// Self(value) +// } +// } +pub type AccountId = Address; #[derive(Serialize, Deserialize, Clone)] #[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))] diff --git a/nssa/core/src/address.rs b/nssa/core/src/address.rs index 69a6979..2774211 100644 --- a/nssa/core/src/address.rs +++ b/nssa/core/src/address.rs @@ -5,7 +5,11 @@ use serde::{Deserialize, Serialize}; use crate::account::AccountId; -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] +#[cfg_attr( + any(feature = "host", test), + derive(Debug, Copy, PartialOrd, Ord, Hash, Default) +)] pub struct Address { value: [u8; 32], } @@ -26,6 +30,7 @@ impl AsRef<[u8]> for Address { } } +#[cfg(feature = "host")] #[derive(Debug, thiserror::Error)] pub enum AddressError { #[error("invalid hex")] @@ -34,6 +39,7 @@ pub enum AddressError { InvalidLength(usize), } +#[cfg(feature = "host")] impl FromStr for Address { type Err = AddressError; fn from_str(s: &str) -> Result { @@ -47,34 +53,14 @@ impl FromStr for Address { } } +#[cfg(feature = "host")] impl Display for Address { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", hex::encode(self.value)) } } -impl Serialize for Address { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - let hex_string = self.to_string(); - - hex_string.serialize(serializer) - } -} - -impl<'de> Deserialize<'de> for Address { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let hex_string = String::deserialize(deserializer)?; - - Address::from_str(&hex_string).map_err(serde::de::Error::custom) - } -} - +#[cfg(feature = "host")] impl From<&Address> for AccountId { fn from(address: &Address) -> Self { const PUBLIC_ACCOUNT_ID_PREFIX: &[u8; 32] = b"/NSSA/v0.1/AccountId/Public/\x00\x00\x00\x00"; @@ -87,6 +73,8 @@ impl From<&Address> for AccountId { #[cfg(test)] mod tests { + use crate::account::AccountId; + use super::{Address, AddressError}; #[test] @@ -117,16 +105,16 @@ mod tests { assert!(matches!(result, AddressError::InvalidLength(_))); } - // #[test] - // fn test_account_id_from_address() { - // let address: Address = "37".repeat(32).parse().unwrap(); - // let expected_account_id = AccountId::new([ - // 93, 223, 66, 245, 78, 230, 157, 188, 110, 161, 134, 255, 137, 177, 220, 88, 37, 44, - // 243, 91, 236, 4, 36, 147, 185, 112, 21, 49, 234, 4, 107, 185, - // ]); - // - // let account_id = AccountId::from(&address); - // - // assert_eq!(account_id, expected_account_id); - // } + #[test] + fn test_account_id_from_address() { + let address: Address = "37".repeat(32).parse().unwrap(); + let expected_account_id = AccountId::new([ + 93, 223, 66, 245, 78, 230, 157, 188, 110, 161, 134, 255, 137, 177, 220, 88, 37, 44, + 243, 91, 236, 4, 36, 147, 185, 112, 21, 49, 234, 4, 107, 185, + ]); + + let account_id = AccountId::from(&address); + + assert_eq!(account_id, expected_account_id); + } } diff --git a/nssa/core/src/encoding.rs b/nssa/core/src/encoding.rs index d7fc8b8..59844d3 100644 --- a/nssa/core/src/encoding.rs +++ b/nssa/core/src/encoding.rs @@ -140,7 +140,7 @@ impl Secp256k1Point { impl AccountId { pub fn to_bytes(&self) -> [u8; 32] { - self.0 + *self.value() } } diff --git a/nssa/core/src/lib.rs b/nssa/core/src/lib.rs index 5a8ca2b..1aa63b4 100644 --- a/nssa/core/src/lib.rs +++ b/nssa/core/src/lib.rs @@ -6,7 +6,6 @@ pub mod encryption; mod nullifier; pub mod program; -#[cfg(feature = "host")] pub mod address; pub use circuit_io::{PrivacyPreservingCircuitInput, PrivacyPreservingCircuitOutput}; diff --git a/wallet/src/chain_storage/mod.rs b/wallet/src/chain_storage/mod.rs index e2923fe..8dc85e1 100644 --- a/wallet/src/chain_storage/mod.rs +++ b/wallet/src/chain_storage/mod.rs @@ -18,10 +18,11 @@ impl WalletChainStore { for init_acc_data in config.initial_accounts.clone() { match init_acc_data { InitialAccountData::Public(data) => { - public_init_acc_map.insert(data.address, data.pub_sign_key); + public_init_acc_map.insert(data.address.parse()?, data.pub_sign_key); } InitialAccountData::Private(data) => { - private_init_acc_map.insert(data.address, (data.key_chain, data.account)); + private_init_acc_map + .insert(data.address.parse()?, (data.key_chain, data.account)); } } } diff --git a/wallet/src/config.rs b/wallet/src/config.rs index b5d861c..aa9b5ba 100644 --- a/wallet/src/config.rs +++ b/wallet/src/config.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct InitialAccountDataPublic { - pub address: nssa::Address, + pub address: String, pub pub_sign_key: nssa::PrivateKey, } @@ -16,7 +16,7 @@ pub struct PersistentAccountDataPublic { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct InitialAccountDataPrivate { - pub address: nssa::Address, + pub address: String, pub account: nssa_core::account::Account, pub key_chain: KeyChain, } @@ -49,8 +49,8 @@ pub enum PersistentAccountData { impl InitialAccountData { pub fn address(&self) -> nssa::Address { match &self { - Self::Public(acc) => acc.address, - Self::Private(acc) => acc.address, + Self::Public(acc) => acc.address.parse().unwrap(), + Self::Private(acc) => acc.address.parse().unwrap(), } } }