Skip to content

Commit

Permalink
refactor: make all generation key fields private
Browse files Browse the repository at this point in the history
closes #193

makes all fields of GenerationSpendingKey and
GenerationReceivingAddress private and adds accessor methods for each.

updates code + tests to use the accessor methods.
  • Loading branch information
dan-da committed Dec 24, 2024
1 parent 5d4de11 commit 9033bfb
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ pub(crate) async fn make_coinbase_transaction(
.await
.wallet_state
.wallet_secret
.generate_sender_randomness(next_block_height, receiving_address.privacy_digest);
.generate_sender_randomness(next_block_height, receiving_address.privacy_digest());

let owned = true;
let liquid_coinbase_output = TxOutput::offchain_native_currency(
Expand Down Expand Up @@ -470,13 +470,13 @@ pub(crate) async fn make_coinbase_transaction(
let composer_utxo_not_timelocked = ExpectedUtxo::new(
liquid_coinbase_output.utxo(),
liquid_coinbase_output.sender_randomness(),
coinbase_recipient_spending_key.privacy_preimage,
coinbase_recipient_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
);
let composer_utxo_timelocked = ExpectedUtxo::new(
timelocked_coinbase_output.utxo(),
timelocked_coinbase_output.sender_randomness(),
coinbase_recipient_spending_key.privacy_preimage,
coinbase_recipient_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
);

Expand Down
2 changes: 1 addition & 1 deletion src/models/state/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ mod tests {
.add_expected_utxo(ExpectedUtxo::new(
coinbase_utxo_1,
cb_sender_randomness_1,
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
))
.await;
Expand Down
14 changes: 7 additions & 7 deletions src/models/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ mod global_state_tests {
.add_expected_utxo(ExpectedUtxo::new(
cb_utxo,
cb_sender_randomness,
own_key.privacy_preimage,
own_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
))
.await;
Expand Down Expand Up @@ -1899,7 +1899,7 @@ mod global_state_tests {
vec![ExpectedUtxo::new(
coinbase_utxo,
coinbase_output_randomness,
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
)],
)
Expand Down Expand Up @@ -2033,7 +2033,7 @@ mod global_state_tests {
vec![ExpectedUtxo::new(
coinbase_utxo_1,
cb_utxo_output_randomness_1,
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
)],
)
Expand Down Expand Up @@ -2353,7 +2353,7 @@ mod global_state_tests {
ExpectedUtxo::new(
expected_utxo.utxo,
expected_utxo.sender_randomness,
genesis_spending_key.privacy_preimage,
genesis_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
)
})
Expand Down Expand Up @@ -2792,7 +2792,7 @@ mod global_state_tests {
ExpectedUtxo::new(
cb_utxo,
cb_output_randomness,
spending_key.privacy_preimage,
spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
),
)
Expand Down Expand Up @@ -2874,7 +2874,7 @@ mod global_state_tests {
ExpectedUtxo::new(
cb_utxo,
cb_output_randomness,
spending_key.privacy_preimage,
spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
),
)
Expand Down Expand Up @@ -2982,7 +2982,7 @@ mod global_state_tests {
let cb = ExpectedUtxo::new(
cb_utxo1,
cb_sender_randomness1,
spend_key.privacy_preimage,
spend_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
);

Expand Down
12 changes: 6 additions & 6 deletions src/models/state/wallet/address/address_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl ReceivingAddress {
/// returns `receiver_identifer`
pub fn receiver_identifier(&self) -> BFieldElement {
match self {
Self::Generation(a) => a.receiver_identifier,
Self::Generation(a) => a.receiver_identifier(),
Self::Symmetric(a) => a.receiver_identifier(),
}
}
Expand Down Expand Up @@ -194,16 +194,16 @@ impl ReceivingAddress {
/// returns the `spending_lock`
pub fn spending_lock(&self) -> Digest {
match self {
Self::Generation(a) => a.spending_lock,
Self::Generation(a) => a.spending_lock(),
Self::Symmetric(k) => k.spending_lock(),
}
}

/// returns a privacy digest which corresponds to the privacy_preimage
/// returns a privacy digest which corresponds to the.privacy_preimage(),
/// of the matching [SpendingKey]
pub fn privacy_digest(&self) -> Digest {
match self {
Self::Generation(a) => a.privacy_digest,
Self::Generation(a) => a.privacy_digest(),
Self::Symmetric(k) => k.privacy_digest(),
}
}
Expand Down Expand Up @@ -400,15 +400,15 @@ impl SpendingKey {
/// as the privacy_digest
pub fn privacy_preimage(&self) -> Digest {
match self {
Self::Generation(k) => k.privacy_preimage,
Self::Generation(k) => k.privacy_preimage(),
Self::Symmetric(k) => k.privacy_preimage(),
}
}

/// returns the receiver_identifier, a public fingerprint
pub fn receiver_identifier(&self) -> BFieldElement {
match self {
Self::Generation(k) => k.receiver_identifier,
Self::Generation(k) => k.receiver_identifier(),
Self::Symmetric(k) => k.receiver_identifier(),
}
}
Expand Down
56 changes: 40 additions & 16 deletions src/models/state/wallet/address/generation_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ pub const GENERATION_FLAG: BFieldElement = BFieldElement::new(GENERATION_FLAG_U8
// keep the serialized (including bech32m) representation small.
#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize)]
pub struct GenerationSpendingKey {
pub seed: Digest,
seed: Digest,

#[serde(skip)]
pub receiver_identifier: BFieldElement,
receiver_identifier: BFieldElement,

#[serde(skip)]
pub decryption_key: lattice::kem::SecretKey,
decryption_key: lattice::kem::SecretKey,

#[serde(skip)]
pub privacy_preimage: Digest,
privacy_preimage: Digest,

#[serde(skip)]
unlock_key: Digest,
Expand All @@ -77,11 +77,11 @@ impl<'de> serde::de::Deserialize<'de> for GenerationSpendingKey {

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub struct GenerationReceivingAddress {
pub receiver_identifier: BFieldElement,
pub encryption_key: lattice::kem::PublicKey,
receiver_identifier: BFieldElement,
encryption_key: lattice::kem::PublicKey,

pub privacy_digest: Digest,
pub spending_lock: Digest,
privacy_digest: Digest,
spending_lock: Digest,
}

impl<'a> Arbitrary<'a> for GenerationReceivingAddress {
Expand All @@ -97,7 +97,7 @@ impl GenerationSpendingKey {
let (_sk, pk) = lattice::kem::keygen(randomness);
let privacy_digest = self.privacy_preimage.hash();
GenerationReceivingAddress {
receiver_identifier: self.receiver_identifier,
receiver_identifier: self.receiver_identifier(),
encryption_key: pk,
privacy_digest,
spending_lock: self.generate_spending_lock(),
Expand Down Expand Up @@ -173,6 +173,19 @@ impl GenerationSpendingKey {
fn generate_spending_lock(&self) -> Digest {
self.unlock_key.hash()
}

/// returns the privacy preimage.
///
/// note: The hash of the preimage is available in the receiving address
/// as the privacy_digest
pub fn privacy_preimage(&self) -> Digest {
self.privacy_preimage
}

/// returns the receiver_identifier, a public fingerprint
pub fn receiver_identifier(&self) -> BFieldElement {
self.receiver_identifier
}
}

impl GenerationReceivingAddress {
Expand Down Expand Up @@ -300,18 +313,13 @@ impl GenerationReceivingAddress {
LockScript::hash_lock(self.spending_lock)
}

/// returns the privacy digest
pub fn privacy_digest(&self) -> Digest {
self.privacy_digest
}

pub(crate) fn generate_public_announcement(
&self,
utxo_notification_payload: &UtxoNotificationPayload,
) -> PublicAnnouncement {
let encrypted_utxo_notification = EncryptedUtxoNotification {
flag: GENERATION_FLAG_U8.into(),
receiver_identifier: self.receiver_identifier,
receiver_identifier: self.receiver_identifier(),
ciphertext: self.encrypt(utxo_notification_payload),
};

Expand All @@ -325,10 +333,26 @@ impl GenerationReceivingAddress {
) -> String {
let encrypted_utxo_notification = EncryptedUtxoNotification {
flag: GENERATION_FLAG_U8.into(),
receiver_identifier: self.receiver_identifier,
receiver_identifier: self.receiver_identifier(),
ciphertext: self.encrypt(utxo_notification_payload),
};

encrypted_utxo_notification.into_bech32m(network)
}

/// returns the receiver_identifier, a public fingerprint
pub fn receiver_identifier(&self) -> BFieldElement {
self.receiver_identifier
}

/// returns a privacy digest which corresponds to the.privacy_preimage(),
/// of the matching [GenerationSpendingKey]
pub fn privacy_digest(&self) -> Digest {
self.privacy_digest
}

/// returns the `spending_lock`
pub fn spending_lock(&self) -> Digest {
self.spending_lock
}
}
19 changes: 11 additions & 8 deletions src/models/state/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ mod wallet_tests {
.add_expected_utxo(ExpectedUtxo::new(
block_1_coinbase_utxo.clone(),
block_1_coinbase_sender_randomness,
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
))
.await;
Expand Down Expand Up @@ -706,7 +706,7 @@ mod wallet_tests {
.add_expected_utxo(ExpectedUtxo::new(
cb_utxo,
cb_output_randomness,
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
))
.await;
Expand Down Expand Up @@ -758,7 +758,7 @@ mod wallet_tests {
.add_expected_utxo(ExpectedUtxo::new(
cb_utxo_prime,
cb_output_randomness_prime,
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
))
.await;
Expand Down Expand Up @@ -931,7 +931,7 @@ mod wallet_tests {

let bob_sender_randomness = bob.wallet_state.wallet_secret.generate_sender_randomness(
genesis_block.kernel.header.height,
alice_address.privacy_digest,
alice_address.privacy_digest(),
);
let receiver_data_12_to_alice = TxOutput::offchain_native_currency(
NeptuneCoins::new(12),
Expand Down Expand Up @@ -1037,7 +1037,7 @@ mod wallet_tests {
let expected_utxo = ExpectedUtxo::new(
cb_utxo,
cb_sender_randomness,
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
);
alice
Expand Down Expand Up @@ -1261,7 +1261,7 @@ mod wallet_tests {
ExpectedUtxo::new(
expected_utxo.utxo,
expected_utxo.sender_randomness,
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
)
})
Expand All @@ -1276,7 +1276,7 @@ mod wallet_tests {
let expected_utxo_for_alice = ExpectedUtxo::new(
receiver_data_1_to_alice_new.utxo(),
receiver_data_1_to_alice_new.sender_randomness(),
alice_spending_key.privacy_preimage,
alice_spending_key.privacy_preimage(),
UtxoNotifier::Cli,
);
alice
Expand Down Expand Up @@ -1497,7 +1497,10 @@ mod wallet_tests {
"_authority_wallet address: {}",
address.to_bech32m(Network::Alpha).unwrap()
);
println!("_authority_wallet spending_lock: {}", address.spending_lock);
println!(
"_authority_wallet spending_lock: {}",
address.spending_lock()
);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/models/state/wallet/transaction_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ mod tests {
let sender_randomness = state
.wallet_state
.wallet_secret
.generate_sender_randomness(block_height, address.privacy_digest);
.generate_sender_randomness(block_height, address.privacy_digest());

for owned_utxo_notification_medium in [
UtxoNotificationMedium::OffChain,
Expand All @@ -418,7 +418,7 @@ mod tests {
"Not owned UTXOs are, currently, always transmitted on-chain"
);
assert_eq!(tx_output.sender_randomness(), sender_randomness);
assert_eq!(tx_output.receiver_digest(), address.privacy_digest);
assert_eq!(tx_output.receiver_digest(), address.privacy_digest());
assert_eq!(tx_output.utxo(), utxo);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/models/state/wallet/wallet_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ mod tests {
vec![ExpectedUtxo::new(
cb_utxo,
cb_sender_randomness,
alice_key.privacy_preimage,
alice_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
)],
)
Expand Down Expand Up @@ -1570,7 +1570,7 @@ mod tests {
.add_expected_utxo(ExpectedUtxo::new(
cb_utxo,
cb_sender_randomness,
bob_spending_key.privacy_preimage,
bob_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
))
.await;
Expand Down Expand Up @@ -1715,7 +1715,7 @@ mod tests {
vec![ExpectedUtxo::new(
block_3a_coinbase_utxo,
block_3a_coinbase_sender_randomness,
bob_spending_key.privacy_preimage,
bob_spending_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
)],
)
Expand Down
4 changes: 2 additions & 2 deletions src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2432,7 +2432,7 @@ mod rpc_server_tests {
let cb = ExpectedUtxo::new(
cb_utxo,
cb_sender_randomness,
cb_key.privacy_preimage,
cb_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
);
rpc_server
Expand Down Expand Up @@ -2564,7 +2564,7 @@ mod rpc_server_tests {
let cb = ExpectedUtxo::new(
cb_utxo,
cb_sender_randomness,
bob_key.privacy_preimage,
bob_key.privacy_preimage(),
UtxoNotifier::OwnMinerComposeBlock,
);
bob_rpc_server
Expand Down
Loading

0 comments on commit 9033bfb

Please sign in to comment.