diff --git a/nssa/core/src/account.rs b/nssa/core/src/account.rs index c3d5580..54d0196 100644 --- a/nssa/core/src/account.rs +++ b/nssa/core/src/account.rs @@ -14,6 +14,8 @@ 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 FingerPrint([u8; 32]); @@ -31,6 +33,17 @@ pub struct AccountWithMetadata { pub fingerprint: FingerPrint, } +#[cfg(feature = "host")] +impl AccountWithMetadata { + pub fn new(account: Account, is_authorized: bool, fingerprint: impl Into) -> Self { + Self { + account, + is_authorized, + fingerprint: fingerprint.into(), + } + } +} + #[cfg(test)] mod tests { use crate::program::DEFAULT_PROGRAM_ID; @@ -64,4 +77,20 @@ mod tests { assert_eq!(new_acc.program_owner, DEFAULT_PROGRAM_ID); } + + #[test] + fn test_account_with_metadata_constructor() { + let account = Account { + program_owner: [1, 2, 3, 4, 5, 6, 7, 8], + balance: 1337, + data: b"testing_account_with_metadata_constructor".to_vec(), + nonce: 0xdeadbeef, + }; + let fingerprint = FingerPrint::new([8; 32]); + let new_acc_with_metadata = + AccountWithMetadata::new(account.clone(), true, fingerprint.clone()); + assert_eq!(new_acc_with_metadata.account, account); + assert!(new_acc_with_metadata.is_authorized); + assert_eq!(new_acc_with_metadata.fingerprint, fingerprint); + } } diff --git a/nssa/core/src/circuit_io.rs b/nssa/core/src/circuit_io.rs index ecc25fc..beae76a 100644 --- a/nssa/core/src/circuit_io.rs +++ b/nssa/core/src/circuit_io.rs @@ -49,26 +49,26 @@ mod tests { fn test_privacy_preserving_circuit_output_to_bytes_is_compatible_with_from_slice() { let output = PrivacyPreservingCircuitOutput { public_pre_states: vec![ - AccountWithMetadata { - account: Account { + AccountWithMetadata::new( + Account { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], balance: 12345678901234567890, data: b"test data".to_vec(), nonce: 18446744073709551614, }, - is_authorized: true, - fingerprint: FingerPrint::new([0; 32]), - }, - AccountWithMetadata { - account: Account { + true, + FingerPrint::new([0; 32]), + ), + AccountWithMetadata::new( + Account { program_owner: [9, 9, 9, 8, 8, 8, 7, 7], balance: 123123123456456567112, data: b"test data".to_vec(), nonce: 9999999999999999999999, }, - is_authorized: false, - fingerprint: FingerPrint::new([1; 32]), - }, + false, + FingerPrint::new([1; 32]), + ), ], public_post_states: vec![Account { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], diff --git a/nssa/core/src/nullifier.rs b/nssa/core/src/nullifier.rs index c783091..e852af4 100644 --- a/nssa/core/src/nullifier.rs +++ b/nssa/core/src/nullifier.rs @@ -13,12 +13,6 @@ impl From<&NullifierPublicKey> for FingerPrint { } } -impl From for FingerPrint { - fn from(value: NullifierPublicKey) -> Self { - FingerPrint::new(value.0) - } -} - impl From<&NullifierSecretKey> for NullifierPublicKey { fn from(value: &NullifierSecretKey) -> Self { let mut bytes = Vec::new(); diff --git a/nssa/src/address.rs b/nssa/src/address.rs index 04bd99d..f9d085e 100644 --- a/nssa/src/address.rs +++ b/nssa/src/address.rs @@ -88,12 +88,6 @@ impl From<&Address> for FingerPrint { } } -impl From
for FingerPrint { - fn from(address: Address) -> Self { - FingerPrint::new(address.value) - } -} - #[cfg(test)] mod tests { use crate::{Address, address::AddressError}; diff --git a/nssa/src/privacy_preserving_transaction/circuit.rs b/nssa/src/privacy_preserving_transaction/circuit.rs index 82ba860..281d59b 100644 --- a/nssa/src/privacy_preserving_transaction/circuit.rs +++ b/nssa/src/privacy_preserving_transaction/circuit.rs @@ -108,20 +108,20 @@ mod tests { fn prove_privacy_preserving_execution_circuit_public_and_private_pre_accounts() { let recipient_keys = test_private_account_keys_1(); let program = Program::authenticated_transfer_program(); - let sender = AccountWithMetadata { - account: Account { + let sender = AccountWithMetadata::new( + Account { balance: 100, ..Account::default() }, - is_authorized: true, - fingerprint: FingerPrint::new([0; 32]), - }; + true, + FingerPrint::new([0; 32]), + ); - let recipient = AccountWithMetadata { - account: Account::default(), - is_authorized: false, - fingerprint: recipient_keys.npk().into(), - }; + let recipient = AccountWithMetadata::new( + Account::default(), + false, + FingerPrint::from(&recipient_keys.npk()), + ); let balance_to_move: u128 = 37; @@ -180,22 +180,22 @@ mod tests { let sender_keys = test_private_account_keys_1(); let recipient_keys = test_private_account_keys_2(); - let sender_pre = AccountWithMetadata { - account: Account { + let sender_pre = AccountWithMetadata::new( + Account { balance: 100, nonce: 0xdeadbeef, ..Account::default() }, - is_authorized: true, - fingerprint: sender_keys.npk().into(), - }; + true, + FingerPrint::from(&sender_keys.npk()), + ); let commitment_sender = Commitment::new(&sender_keys.npk(), &sender_pre.account); - let recipient = AccountWithMetadata { - account: Account::default(), - is_authorized: false, - fingerprint: recipient_keys.npk().into(), - }; + let recipient = AccountWithMetadata::new( + Account::default(), + false, + FingerPrint::from(&recipient_keys.npk()), + ); let balance_to_move: u128 = 37; let mut commitment_set = CommitmentSet::with_capacity(2); diff --git a/nssa/src/privacy_preserving_transaction/transaction.rs b/nssa/src/privacy_preserving_transaction/transaction.rs index a683f85..6992b2f 100644 --- a/nssa/src/privacy_preserving_transaction/transaction.rs +++ b/nssa/src/privacy_preserving_transaction/transaction.rs @@ -90,10 +90,12 @@ impl PrivacyPreservingTransaction { let public_pre_states: Vec<_> = message .public_addresses .iter() - .map(|address| AccountWithMetadata { - account: state.get_account_by_address(address), - is_authorized: signer_addresses.contains(address), - fingerprint: address.into(), + .map(|address| { + AccountWithMetadata::new( + state.get_account_by_address(address), + signer_addresses.contains(address), + address, + ) }) .collect(); diff --git a/nssa/src/program.rs b/nssa/src/program.rs index d105f51..552c436 100644 --- a/nssa/src/program.rs +++ b/nssa/src/program.rs @@ -168,19 +168,16 @@ mod tests { let program = Program::simple_balance_transfer(); let balance_to_move: u128 = 11223344556677; let instruction_data = Program::serialize_instruction(balance_to_move).unwrap(); - let sender = AccountWithMetadata { - account: Account { + let sender = AccountWithMetadata::new( + Account { balance: 77665544332211, ..Account::default() }, - is_authorized: true, - fingerprint: FingerPrint::new([0; 32]), - }; - let recipient = AccountWithMetadata { - account: Account::default(), - is_authorized: false, - fingerprint: FingerPrint::new([1; 32]), - }; + true, + FingerPrint::new([0; 32]), + ); + let recipient = + AccountWithMetadata::new(Account::default(), false, FingerPrint::new([1; 32])); let expected_sender_post = Account { balance: 77665544332211 - balance_to_move, diff --git a/nssa/src/public_transaction/transaction.rs b/nssa/src/public_transaction/transaction.rs index 14dcc6f..e5c8b5a 100644 --- a/nssa/src/public_transaction/transaction.rs +++ b/nssa/src/public_transaction/transaction.rs @@ -91,10 +91,12 @@ impl PublicTransaction { let pre_states: Vec<_> = message .addresses .iter() - .map(|address| AccountWithMetadata { - account: state.get_account_by_address(address), - is_authorized: signer_addresses.contains(address), - fingerprint: address.into(), + .map(|address| { + AccountWithMetadata::new( + state.get_account_by_address(address), + signer_addresses.contains(address), + address, + ) }) .collect(); diff --git a/nssa/src/state.rs b/nssa/src/state.rs index 347102a..4b8b25b 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -776,19 +776,15 @@ pub mod tests { balance_to_move: u128, state: &V01State, ) -> PrivacyPreservingTransaction { - let sender = AccountWithMetadata { - account: state.get_account_by_address(&sender_keys.address()), - is_authorized: true, - fingerprint: sender_keys.address().into(), - }; + let sender = AccountWithMetadata::new( + state.get_account_by_address(&sender_keys.address()), + true, + &sender_keys.address(), + ); let sender_nonce = sender.account.nonce; - let recipient = AccountWithMetadata { - account: Account::default(), - is_authorized: false, - fingerprint: recipient_keys.npk().into(), - }; + let recipient = AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk()); let esk = [3; 32]; let shared_secret = SharedSecretKey::new(&esk, &recipient_keys.ivk()); @@ -827,16 +823,10 @@ pub mod tests { ) -> PrivacyPreservingTransaction { let program = Program::authenticated_transfer_program(); let sender_commitment = Commitment::new(&sender_keys.npk(), sender_private_account); - let sender_pre = AccountWithMetadata { - account: sender_private_account.clone(), - is_authorized: true, - fingerprint: sender_keys.npk().into(), - }; - let recipient_pre = AccountWithMetadata { - account: Account::default(), - is_authorized: false, - fingerprint: recipient_keys.npk().into(), - }; + let sender_pre = + AccountWithMetadata::new(sender_private_account.clone(), true, &sender_keys.npk()); + let recipient_pre = + AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk()); let esk_1 = [3; 32]; let shared_secret_1 = SharedSecretKey::new(&esk_1, &sender_keys.ivk()); @@ -889,16 +879,13 @@ pub mod tests { ) -> PrivacyPreservingTransaction { let program = Program::authenticated_transfer_program(); let sender_commitment = Commitment::new(&sender_keys.npk(), sender_private_account); - let sender_pre = AccountWithMetadata { - account: sender_private_account.clone(), - is_authorized: true, - fingerprint: sender_keys.npk().into(), - }; - let recipient_pre = AccountWithMetadata { - account: state.get_account_by_address(recipient_address), - is_authorized: false, - fingerprint: recipient_address.into(), - }; + let sender_pre = + AccountWithMetadata::new(sender_private_account.clone(), true, &sender_keys.npk()); + let recipient_pre = AccountWithMetadata::new( + state.get_account_by_address(recipient_address), + false, + recipient_address, + ); let esk = [3; 32]; let shared_secret = SharedSecretKey::new(&esk, &sender_keys.ivk());