Skip to content

Commit

Permalink
[session_key] directly generate session key via random
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed May 28, 2024
1 parent 50ee00b commit a055ed0
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 186 deletions.
7 changes: 6 additions & 1 deletion crates/rooch-key/src/key_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ fn validate_derivation_path(path: Option<DerivationPath>) -> Result<DerivationPa
{
Ok(p)
} else {
Err(anyhow::anyhow!("Invalid derivation path: {}, purpose:{}, coin_type: {}", p, p_purpose, p_coin_type))
Err(anyhow::anyhow!(
"Invalid derivation path: {}, purpose:{}, coin_type: {}",
p,
p_purpose,
p_coin_type
))
}
} else {
Err(anyhow::anyhow!("Invalid derivation path: {}", p))
Expand Down
52 changes: 0 additions & 52 deletions crates/rooch-key/src/keystore/account_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ pub trait AccountKeystore {
is_password_empty: bool,
) -> Result<(), anyhow::Error>;

// fn update_address_encryption_data(
// &mut self,
// address: &RoochAddress,
// encryption: EncryptionData,
// ) -> Result<(), anyhow::Error>;
fn nullify(&mut self, address: &RoochAddress) -> Result<(), anyhow::Error>;

fn sign_hashed(
Expand Down Expand Up @@ -132,53 +127,6 @@ pub trait AccountKeystore {

fn addresses(&self) -> Vec<RoochAddress>;

// fn import_from_mnemonic(
// &mut self,
// phrase: &str,
// derivation_path: Option<DerivationPath>,
// password: Option<String>,
// ) -> Result<ImportedMnemonic, anyhow::Error> {
// let mnemonic = Mnemonic::from_phrase(phrase, Language::English)?;
// let seed = Seed::new(&mnemonic, "");

// let sk = derive_private_key_from_path(seed.as_bytes(), derivation_path)?;

// let encryption = encrypt_key(&sk, password).expect("Encryption failed for private key");

// let address = derive_address_from_private_key(sk)?;

// let result = ImportedMnemonic {
// address,
// encryption: encryption.clone(),
// };

// self.add_address_encryption_data(result.address, encryption)?;

// Ok(result)
// }

// fn update_address_with_encryption_data(
// &mut self,
// _address: &RoochAddress,
// phrase: String,
// derivation_path: Option<DerivationPath>,
// password: Option<String>,
// ) -> Result<EncryptionData, anyhow::Error> {
// let mnemonic = Mnemonic::from_phrase(&phrase, Language::English)?;
// let seed = Seed::new(&mnemonic, "");

// let sk = derive_private_key_from_path(seed.as_bytes(), derivation_path)?;

// let encryption_data =
// encrypt_key(&sk, password).expect("Encryption failed for private key");

// let address = derive_address_from_private_key(sk)?;

// self.update_address_encryption_data(&address, encryption_data.clone())?;

// Ok(encryption_data)
// }

fn nullify_address(&mut self, address: &RoochAddress) -> Result<(), anyhow::Error> {
self.nullify(address)?;
Ok(())
Expand Down
66 changes: 2 additions & 64 deletions crates/rooch-key/src/keystore/base_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ impl AccountKeystore for BaseKeyStore {
Ok(())
}

// fn update_address_encryption_data(
// &mut self,
// address: &RoochAddress,
// encryption: EncryptionData,
// ) -> Result<(), anyhow::Error> {
// self.keys.entry(*address).or_insert(encryption);
// Ok(())
// }

fn nullify(&mut self, address: &RoochAddress) -> Result<(), anyhow::Error> {
self.keys.remove(address);
Ok(())
Expand All @@ -193,10 +184,7 @@ impl AccountKeystore for BaseKeyStore {
address: &RoochAddress,
password: Option<String>,
) -> Result<AuthenticationKey, anyhow::Error> {
//TODO define derivation_path for session key
let result = generate_new_key_pair(None, None, None, password.clone())?;
let kp: RoochKeyPair =
retrieve_key_pair(&result.key_pair_data.private_key_encryption, password)?;
let kp: RoochKeyPair = RoochKeyPair::generate();
let authentication_key = kp.public().authentication_key();
let inner_map = self.session_keys.entry(*address).or_default();
let local_session_key = LocalSessionKey {
Expand Down Expand Up @@ -243,15 +231,7 @@ impl AccountKeystore for BaseKeyStore {
"Cannot find SessionKey for authentication_key: [{authentication_key}]"
))
})?;
//TODO should we check the scope of session key here?
// let session_key = local_session_key.session_key.as_ref().ok_or_else(||{
// signature::Error::from_source(
// format!("SessionKey for authentication_key:[{authentication_key}] do not binding to on-chain SessionKey")
// )
// })?;
// ensure!(session_key.is_scope_match_with_action(&msg.action), signature::Error::from_source(
// format!("SessionKey for authentication_key:[{authentication_key}] scope do not match with transaction")
// ));

let kp: RoochKeyPair = retrieve_key_pair(&local_session_key.private_key, password)
.map_err(signature::Error::from_source)?;

Expand Down Expand Up @@ -329,47 +309,5 @@ impl AccountKeystore for BaseKeyStore {
"Cannot find mnemonic data, please init the keystore first".to_string(),
))),
}
// match self.mnemonic.first_key_value() {
// Some((k, v)) => {
// let nonce = Base64::decode(&v.mnemonic_phrase_encryption.nonce).map_err(|e| {
// anyhow::Error::new(RoochError::KeyConversionError(e.to_string()))
// })?;
// let ciphertext =
// Base64::decode(&v.mnemonic_phrase_encryption.ciphertext).map_err(|e| {
// anyhow::Error::new(RoochError::KeyConversionError(e.to_string()))
// })?;
// let tag = Base64::decode(&v.mnemonic_phrase_encryption.tag).map_err(|e| {
// anyhow::Error::new(RoochError::KeyConversionError(e.to_string()))
// })?;

// let mnemonic_phrase = decrypt_key(
// nonce.as_slice(),
// ciphertext.as_slice(),
// tag.as_slice(),
// password,
// )?;

// let mnemonic_phrase = String::from_utf8(mnemonic_phrase)
// .map_err(|e| anyhow::anyhow!("Parse mnemonic phrase error:{}", e))?;
// let mnemonic_generated_address = MnemonicResult {
// mnemonic_phrase,
// mnemonic_phrase_key: k.clone(),
// mnemonic_data: v.clone(),
// };
// Ok(vec![mnemonic_generated_address])
// }
// None => Ok(vec![]),
// }
}

// fn add_mnemonic_data(
// &mut self,
// mnemonic_phrase: String,
// mnemonic_data: MnemonicData,
// ) -> Result<(), anyhow::Error> {
// self.mnemonics
// .entry(mnemonic_phrase)
// .or_insert(mnemonic_data);
// Ok(())
// }
}
33 changes: 0 additions & 33 deletions crates/rooch-key/src/keystore/file_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@ impl AccountKeystore for FileBasedKeystore {
self.keystore.get_key_pair_with_password(address, password)
}

// fn update_address_encryption_data(
// &mut self,
// address: &RoochAddress,
// encryption: EncryptionData,
// ) -> Result<(), anyhow::Error> {
// self.keystore
// .update_address_encryption_data(address, encryption)?;
// self.save()?;
// Ok(())
// }

fn nullify(&mut self, address: &RoochAddress) -> Result<(), anyhow::Error> {
self.keystore.nullify(address)?;
self.save()?;
Expand Down Expand Up @@ -173,28 +162,6 @@ impl AccountKeystore for FileBasedKeystore {
fn get_mnemonic(&self, password: Option<String>) -> Result<MnemonicResult, anyhow::Error> {
self.keystore.get_mnemonic(password)
}

// fn add_mnemonic_data(
// &mut self,
// mnemonic_phrase: String,
// mnemonic_data: MnemonicData,
// ) -> Result<(), anyhow::Error> {
// self.keystore
// .add_mnemonic_data(mnemonic_phrase, mnemonic_data)?;
// self.save()?;
// Ok(())
// }

// fn update_mnemonic_data(
// &mut self,
// mnemonic_phrase: String,
// mnemonic_data: MnemonicData,
// ) -> Result<(), anyhow::Error> {
// self.keystore
// .update_mnemonic_data(mnemonic_phrase, mnemonic_data)?;
// self.save()?;
// Ok(())
// }
}

impl FileBasedKeystore {
Expand Down
31 changes: 0 additions & 31 deletions crates/rooch-key/src/keystore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,6 @@ impl AccountKeystore for Keystore {
}
}

// fn update_address_encryption_data(
// &mut self,
// address: &RoochAddress,
// encryption: EncryptionData,
// ) -> Result<(), anyhow::Error> {
// // Implement this method to update the key pair by coin ID for the appropriate variant (File or InMem)
// match self {
// Keystore::File(file_keystore) => {
// file_keystore.update_address_encryption_data(address, encryption)
// }
// Keystore::InMem(inmem_keystore) => {
// inmem_keystore.update_address_encryption_data(address, encryption)
// }
// }
// }

fn nullify(&mut self, address: &RoochAddress) -> Result<(), anyhow::Error> {
// Implement this method to nullify the key pair by coin ID for the appropriate variant (File or InMem)
match self {
Expand Down Expand Up @@ -264,21 +248,6 @@ impl AccountKeystore for Keystore {
Keystore::InMem(inmem_keystore) => inmem_keystore.get_mnemonic(password),
}
}

// fn add_mnemonic_data(
// &mut self,
// mnemonic_phrase: String,
// mnemonic_data: MnemonicData,
// ) -> Result<(), anyhow::Error> {
// match self {
// Keystore::File(file_keystore) => {
// file_keystore.add_mnemonic_data(mnemonic_phrase, mnemonic_data)
// }
// Keystore::InMem(inmem_keystore) => {
// inmem_keystore.add_mnemonic_data(mnemonic_phrase, mnemonic_data)
// }
// }
// }
}

impl Display for Keystore {
Expand Down
3 changes: 1 addition & 2 deletions crates/rooch-types/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ impl RoochKeyPair {
}
}

// #[cfg(test)]
pub fn generate_for_testing() -> Self {
pub fn generate() -> Self {
let rng = &mut rand::thread_rng();
let ed25519_keypair = Ed25519KeyPair::generate(rng);
RoochKeyPair::Ed25519(ed25519_keypair)
Expand Down
4 changes: 1 addition & 3 deletions crates/testsuite/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ async fn start_server(w: &mut World, _scenario: String) {

let mut server_opt = ServerOpt::new();

let result = generate_new_key_pair(None, None, None, None).unwrap();
let kp: RoochKeyPair =
retrieve_key_pair(&result.key_pair_data.private_key_encryption, None).unwrap();
let kp: RoochKeyPair = RoochKeyPair::generate();
server_opt.sequencer_keypair = Some(kp.copy());
server_opt.proposer_keypair = Some(kp.copy());

Expand Down

0 comments on commit a055ed0

Please sign in to comment.