mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-07 15:53:14 +00:00
refactor address to avoid code repetition in computing addresses from public keys
This commit is contained in:
parent
76a023c33a
commit
4300e87485
34
accounts/src/account_core/address.rs
Normal file
34
accounts/src/account_core/address.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
use common::transaction::SignaturePublicKey;
|
||||||
|
use tiny_keccak::{Hasher, Keccak};
|
||||||
|
|
||||||
|
// TODO: Consider wrapping `AccountAddress` in a struct.
|
||||||
|
|
||||||
|
pub type AccountAddress = [u8; 32];
|
||||||
|
pub fn from_public_key(public_key: &SignaturePublicKey) -> AccountAddress {
|
||||||
|
let mut address = [0; 32];
|
||||||
|
let mut keccak_hasher = Keccak::v256();
|
||||||
|
keccak_hasher.update(&public_key.to_sec1_bytes());
|
||||||
|
keccak_hasher.finalize(&mut address);
|
||||||
|
address
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use common::transaction::SignaturePrivateKey;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::account_core::address;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_address_key_equal_keccak_pub_sign_key() {
|
||||||
|
let signing_key = SignaturePrivateKey::from_slice(&[1; 32]).unwrap();
|
||||||
|
let public_key = signing_key.verifying_key();
|
||||||
|
|
||||||
|
let mut expected_address = [0; 32];
|
||||||
|
let mut keccak_hasher = Keccak::v256();
|
||||||
|
keccak_hasher.update(&public_key.to_sec1_bytes());
|
||||||
|
keccak_hasher.finalize(&mut expected_address);
|
||||||
|
|
||||||
|
assert_eq!(expected_address, address::from_public_key(public_key));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,14 +7,18 @@ use log::info;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utxo::utxo_core::UTXO;
|
use utxo::utxo_core::UTXO;
|
||||||
|
|
||||||
use crate::key_management::{
|
pub mod address;
|
||||||
constants_types::{CipherText, Nonce},
|
|
||||||
ephemeral_key_holder::EphemeralKeyHolder,
|
use crate::{
|
||||||
AddressKeyHolder,
|
account_core::address::AccountAddress,
|
||||||
|
key_management::{
|
||||||
|
constants_types::{CipherText, Nonce},
|
||||||
|
ephemeral_key_holder::EphemeralKeyHolder,
|
||||||
|
AddressKeyHolder,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type PublicKey = AffinePoint;
|
pub type PublicKey = AffinePoint;
|
||||||
pub type AccountAddress = TreeHashType;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Account {
|
pub struct Account {
|
||||||
@ -113,7 +117,8 @@ impl AccountPublicMask {
|
|||||||
impl Account {
|
impl Account {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let key_holder = AddressKeyHolder::new_os_random();
|
let key_holder = AddressKeyHolder::new_os_random();
|
||||||
let address = key_holder.address;
|
let public_key = *key_holder.get_pub_account_signing_key().verifying_key();
|
||||||
|
let address = address::from_public_key(&public_key);
|
||||||
let balance = 0;
|
let balance = 0;
|
||||||
let utxos = HashMap::new();
|
let utxos = HashMap::new();
|
||||||
|
|
||||||
@ -127,7 +132,8 @@ impl Account {
|
|||||||
|
|
||||||
pub fn new_with_balance(balance: u64) -> Self {
|
pub fn new_with_balance(balance: u64) -> Self {
|
||||||
let key_holder = AddressKeyHolder::new_os_random();
|
let key_holder = AddressKeyHolder::new_os_random();
|
||||||
let address = key_holder.address;
|
let public_key = *key_holder.get_pub_account_signing_key().verifying_key();
|
||||||
|
let address = address::from_public_key(&public_key);
|
||||||
let utxos = HashMap::new();
|
let utxos = HashMap::new();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
@ -217,6 +223,8 @@ impl Default for Account {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use common::transaction::SignaturePrivateKey;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn generate_dummy_utxo(address: TreeHashType, amount: u128) -> UTXO {
|
fn generate_dummy_utxo(address: TreeHashType, amount: u128) -> UTXO {
|
||||||
@ -228,7 +236,6 @@ mod tests {
|
|||||||
let account = Account::new();
|
let account = Account::new();
|
||||||
|
|
||||||
assert_eq!(account.balance, 0);
|
assert_eq!(account.balance, 0);
|
||||||
assert!(account.key_holder.address != [0u8; 32]); // Check if the address is not empty
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
use aes_gcm::{aead::Aead, Aes256Gcm, KeyInit};
|
use aes_gcm::{aead::Aead, Aes256Gcm, KeyInit};
|
||||||
use common::merkle_tree_public::TreeHashType;
|
use common::{merkle_tree_public::TreeHashType, transaction::SignaturePublicKey};
|
||||||
use constants_types::{CipherText, Nonce};
|
use constants_types::{CipherText, Nonce};
|
||||||
use elliptic_curve::point::AffineCoordinates;
|
use elliptic_curve::point::AffineCoordinates;
|
||||||
use k256::{ecdsa::SigningKey, AffinePoint, FieldBytes};
|
use k256::{ecdsa::SigningKey, AffinePoint, FieldBytes};
|
||||||
@ -24,7 +24,6 @@ pub struct AddressKeyHolder {
|
|||||||
top_secret_key_holder: TopSecretKeyHolder,
|
top_secret_key_holder: TopSecretKeyHolder,
|
||||||
pub utxo_secret_key_holder: UTXOSecretKeyHolder,
|
pub utxo_secret_key_holder: UTXOSecretKeyHolder,
|
||||||
pub_account_signing_key: PublicAccountSigningKey,
|
pub_account_signing_key: PublicAccountSigningKey,
|
||||||
pub address: TreeHashType,
|
|
||||||
pub nullifer_public_key: PublicKey,
|
pub nullifer_public_key: PublicKey,
|
||||||
pub viewing_public_key: PublicKey,
|
pub viewing_public_key: PublicKey,
|
||||||
}
|
}
|
||||||
@ -47,21 +46,9 @@ impl AddressKeyHolder {
|
|||||||
bytes
|
bytes
|
||||||
};
|
};
|
||||||
|
|
||||||
//Address is a Keccak(verification_key)
|
|
||||||
let field_bytes = FieldBytes::from_slice(&pub_account_signing_key);
|
|
||||||
let signing_key = SigningKey::from_bytes(field_bytes).unwrap();
|
|
||||||
|
|
||||||
let verifying_key = signing_key.verifying_key();
|
|
||||||
|
|
||||||
let mut address = [0; 32];
|
|
||||||
let mut keccak_hasher = Keccak::v256();
|
|
||||||
keccak_hasher.update(&verifying_key.to_sec1_bytes());
|
|
||||||
keccak_hasher.finalize(&mut address);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
top_secret_key_holder,
|
top_secret_key_holder,
|
||||||
utxo_secret_key_holder,
|
utxo_secret_key_holder,
|
||||||
address,
|
|
||||||
nullifer_public_key,
|
nullifer_public_key,
|
||||||
viewing_public_key,
|
viewing_public_key,
|
||||||
pub_account_signing_key,
|
pub_account_signing_key,
|
||||||
@ -214,7 +201,6 @@ mod tests {
|
|||||||
assert!(!Into::<bool>::into(
|
assert!(!Into::<bool>::into(
|
||||||
address_key_holder.viewing_public_key.is_identity()
|
address_key_holder.viewing_public_key.is_identity()
|
||||||
));
|
));
|
||||||
assert!(!address_key_holder.address.as_slice().is_empty()); // Assume TreeHashType has non-zero length for a valid address
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -343,21 +329,6 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_address_key_equal_keccak_pub_sign_key() {
|
|
||||||
let address_key_holder = AddressKeyHolder::new_os_random();
|
|
||||||
let signing_key = address_key_holder.get_pub_account_signing_key();
|
|
||||||
|
|
||||||
let verifying_key = signing_key.verifying_key();
|
|
||||||
|
|
||||||
let mut address = [0; 32];
|
|
||||||
let mut keccak_hasher = Keccak::v256();
|
|
||||||
keccak_hasher.update(&verifying_key.to_sec1_bytes());
|
|
||||||
keccak_hasher.finalize(&mut address);
|
|
||||||
|
|
||||||
assert_eq!(address, address_key_holder.address);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn key_generation_test() {
|
fn key_generation_test() {
|
||||||
let seed_holder = SeedHolder::new_os_random();
|
let seed_holder = SeedHolder::new_os_random();
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use accounts::account_core::{Account, AccountAddress};
|
use accounts::account_core::{address::AccountAddress, Account};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub struct NodeAccountsStore {
|
pub struct NodeAccountsStore {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||||
|
|
||||||
use accounts::account_core::{Account, AccountAddress};
|
use accounts::account_core::{address::AccountAddress, Account};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use block_store::NodeBlockStore;
|
use block_store::NodeBlockStore;
|
||||||
use common::{
|
use common::{
|
||||||
@ -342,40 +342,6 @@ mod tests {
|
|||||||
],
|
],
|
||||||
"balance": 100,
|
"balance": 100,
|
||||||
"key_holder": {
|
"key_holder": {
|
||||||
"address": [
|
|
||||||
244,
|
|
||||||
55,
|
|
||||||
238,
|
|
||||||
205,
|
|
||||||
74,
|
|
||||||
115,
|
|
||||||
179,
|
|
||||||
192,
|
|
||||||
65,
|
|
||||||
186,
|
|
||||||
166,
|
|
||||||
169,
|
|
||||||
221,
|
|
||||||
45,
|
|
||||||
6,
|
|
||||||
57,
|
|
||||||
200,
|
|
||||||
65,
|
|
||||||
195,
|
|
||||||
70,
|
|
||||||
118,
|
|
||||||
252,
|
|
||||||
206,
|
|
||||||
100,
|
|
||||||
215,
|
|
||||||
250,
|
|
||||||
72,
|
|
||||||
230,
|
|
||||||
19,
|
|
||||||
71,
|
|
||||||
217,
|
|
||||||
249
|
|
||||||
],
|
|
||||||
"nullifer_public_key": "03A340BECA9FAAB444CED0140681D72EA1318B5C611704FEE017DA9836B17DB718",
|
"nullifer_public_key": "03A340BECA9FAAB444CED0140681D72EA1318B5C611704FEE017DA9836B17DB718",
|
||||||
"pub_account_signing_key": [
|
"pub_account_signing_key": [
|
||||||
244,
|
244,
|
||||||
@ -460,40 +426,6 @@ mod tests {
|
|||||||
],
|
],
|
||||||
"balance": 200,
|
"balance": 200,
|
||||||
"key_holder": {
|
"key_holder": {
|
||||||
"address": [
|
|
||||||
72,
|
|
||||||
169,
|
|
||||||
70,
|
|
||||||
237,
|
|
||||||
1,
|
|
||||||
96,
|
|
||||||
35,
|
|
||||||
157,
|
|
||||||
25,
|
|
||||||
15,
|
|
||||||
83,
|
|
||||||
18,
|
|
||||||
52,
|
|
||||||
206,
|
|
||||||
202,
|
|
||||||
63,
|
|
||||||
48,
|
|
||||||
59,
|
|
||||||
173,
|
|
||||||
76,
|
|
||||||
78,
|
|
||||||
7,
|
|
||||||
254,
|
|
||||||
229,
|
|
||||||
28,
|
|
||||||
45,
|
|
||||||
194,
|
|
||||||
79,
|
|
||||||
6,
|
|
||||||
89,
|
|
||||||
58,
|
|
||||||
85
|
|
||||||
],
|
|
||||||
"nullifer_public_key": "02172F50274DE67C4087C344F5D58E11DF761D90285B095060E0994FAA6BCDE271",
|
"nullifer_public_key": "02172F50274DE67C4087C344F5D58E11DF761D90285B095060E0994FAA6BCDE271",
|
||||||
"pub_account_signing_key": [
|
"pub_account_signing_key": [
|
||||||
136,
|
136,
|
||||||
|
|||||||
@ -8,7 +8,7 @@ use common::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use accounts::{
|
use accounts::{
|
||||||
account_core::{Account, AccountAddress},
|
account_core::{address::AccountAddress, Account},
|
||||||
key_management::ephemeral_key_holder::EphemeralKeyHolder,
|
key_management::ephemeral_key_holder::EphemeralKeyHolder,
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
use std::collections::{BTreeMap, HashSet};
|
use std::collections::{BTreeMap, HashSet};
|
||||||
|
|
||||||
use accounts::account_core::{AccountAddress, AccountPublicMask};
|
use accounts::account_core::{address::AccountAddress, AccountPublicMask};
|
||||||
use common::merkle_tree_public::{merkle_tree::UTXOCommitmentsMerkleTree, TreeHashType};
|
use common::merkle_tree_public::{merkle_tree::UTXOCommitmentsMerkleTree, TreeHashType};
|
||||||
use serde::{ser::SerializeStruct, Serialize};
|
use serde::{ser::SerializeStruct, Serialize};
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use accounts::account_core::AccountAddress;
|
use accounts::account_core::address::{self, AccountAddress};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use common::{
|
use common::{
|
||||||
block::{Block, HashableBlockData},
|
block::{Block, HashableBlockData},
|
||||||
@ -15,7 +15,6 @@ use mempool::MemPool;
|
|||||||
use mempool_transaction::MempoolTransaction;
|
use mempool_transaction::MempoolTransaction;
|
||||||
use sequencer_store::SequecerChainStore;
|
use sequencer_store::SequecerChainStore;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tiny_keccak::{Hasher, Keccak};
|
|
||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod mempool_transaction;
|
pub mod mempool_transaction;
|
||||||
@ -147,13 +146,10 @@ impl SequencerCore {
|
|||||||
if let Ok(native_transfer_action) =
|
if let Ok(native_transfer_action) =
|
||||||
serde_json::from_slice::<PublicNativeTokenSend>(execution_input)
|
serde_json::from_slice::<PublicNativeTokenSend>(execution_input)
|
||||||
{
|
{
|
||||||
let mut output = [0; 32];
|
let signer_address = address::from_public_key(&tx.transaction().public_key);
|
||||||
let mut keccak_hasher = Keccak::v256();
|
|
||||||
keccak_hasher.update(&tx.transaction().public_key.to_sec1_bytes());
|
|
||||||
keccak_hasher.finalize(&mut output);
|
|
||||||
|
|
||||||
//Correct sender check
|
//Correct sender check
|
||||||
if native_transfer_action.from != output {
|
if native_transfer_action.from != signer_address {
|
||||||
return Err(TransactionMalformationErrorKind::IncorrectSender);
|
return Err(TransactionMalformationErrorKind::IncorrectSender);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -232,10 +228,7 @@ impl SequencerCore {
|
|||||||
let tx_hash = *mempool_tx.auth_tx.hash();
|
let tx_hash = *mempool_tx.auth_tx.hash();
|
||||||
|
|
||||||
// Nonce check
|
// Nonce check
|
||||||
let mut signer_addres = [0; 32];
|
let signer_addres = address::from_public_key(&mempool_tx.auth_tx.transaction().public_key);
|
||||||
let mut keccak_hasher = Keccak::v256();
|
|
||||||
keccak_hasher.update(&mempool_tx.auth_tx.transaction().public_key.to_sec1_bytes());
|
|
||||||
keccak_hasher.finalize(&mut signer_addres);
|
|
||||||
if self.store.acc_store.get_account_nonce(&signer_addres) != *nonce {
|
if self.store.acc_store.get_account_nonce(&signer_addres) != *nonce {
|
||||||
return Err(TransactionMalformationErrorKind::NonceMismatch { tx: tx_hash });
|
return Err(TransactionMalformationErrorKind::NonceMismatch { tx: tx_hash });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use accounts::account_core::AccountAddress;
|
use accounts::account_core::address::AccountAddress;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use accounts::account_core::AccountAddress;
|
use accounts::account_core::address::AccountAddress;
|
||||||
use common::ExecutionFailureKind;
|
use common::ExecutionFailureKind;
|
||||||
use rand::{rngs::OsRng, RngCore};
|
use rand::{rngs::OsRng, RngCore};
|
||||||
use risc0_zkvm::{default_executor, default_prover, sha::Digest, ExecutorEnv, Receipt};
|
use risc0_zkvm::{default_executor, default_prover, sha::Digest, ExecutorEnv, Receipt};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user