This commit is contained in:
Sergio Chouhy 2025-09-30 16:45:25 -03:00
parent 4bd5494370
commit 6070213232
6 changed files with 43 additions and 54 deletions

View File

@ -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))]

View File

@ -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<Self, Self::Err> {
@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let hex_string = self.to_string();
hex_string.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for Address {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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);
}
}

View File

@ -140,7 +140,7 @@ impl Secp256k1Point {
impl AccountId {
pub fn to_bytes(&self) -> [u8; 32] {
self.0
*self.value()
}
}

View File

@ -6,7 +6,6 @@ pub mod encryption;
mod nullifier;
pub mod program;
#[cfg(feature = "host")]
pub mod address;
pub use circuit_io::{PrivacyPreservingCircuitInput, PrivacyPreservingCircuitOutput};

View File

@ -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));
}
}
}

View File

@ -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(),
}
}
}