Skip to content

Commit

Permalink
review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Jul 17, 2024
1 parent adaa54d commit 7e602c2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
2 changes: 1 addition & 1 deletion base_layer/common_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// This is the string used to derive the comms/spend key of the wallet
pub const COMMS: &str = "comms";
pub const WALLET_COMMS_AND_SPEND_KEY_BRANCH: &str = "comms";

pub mod burnt_proof;
pub mod chain_metadata;
Expand Down
63 changes: 30 additions & 33 deletions base_layer/core/src/transactions/key_manager/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use tari_key_manager::{
},
};
use tari_script::CheckSigSchnorrSignature;
use tari_utilities::{hex::Hex, ByteArray};
use tari_utilities::ByteArray;
use tokio::sync::RwLock;

const LOG_TARGET: &str = "c::bn::key_manager::key_manager_service";
Expand Down Expand Up @@ -281,38 +281,37 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
pub(crate) async fn get_private_key(&self, key_id: &TariKeyId) -> Result<PrivateKey, KeyManagerServiceError> {
match key_id {
KeyId::Managed { branch, index } => {
// ledger has special rules here
if let WalletType::Ledger(wallet) = &self.wallet_type {
// In the event we're asking for the view key, and we use a ledger, reference the stored key
if &TransactionKeyManagerBranch::DataEncryption.get_branch_key() == branch {
return wallet
.view_key
.clone()
.ok_or(KeyManagerServiceError::LedgerViewKeyInaccessible);
}

// If we're trying to access any of the private keys, just say no bueno
if &TransactionKeyManagerBranch::Spend.get_branch_key() == branch ||
&TransactionKeyManagerBranch::SenderOffset.get_branch_key() == branch
{
return Err(KeyManagerServiceError::LedgerPrivateKeyInaccessible);
}
};
match &self.wallet_type {
WalletType::Software => {},
WalletType::Ledger(wallet) => {
if &TransactionKeyManagerBranch::DataEncryption.get_branch_key() == branch {
return wallet
.view_key
.clone()
.ok_or(KeyManagerServiceError::LedgerViewKeyInaccessible);
}

// imported wallet type has special rules here
if let WalletType::Imported(wallet) = &self.wallet_type {
if &TransactionKeyManagerBranch::DataEncryption.get_branch_key() == branch {
return Ok(wallet.view_key.clone());
}
// If we're trying to access any of the private keys, just say no bueno
if &TransactionKeyManagerBranch::Spend.get_branch_key() == branch ||
&TransactionKeyManagerBranch::SenderOffset.get_branch_key() == branch
{
return Err(KeyManagerServiceError::LedgerPrivateKeyInaccessible);
}
},
WalletType::Imported(wallet) => {
if &TransactionKeyManagerBranch::DataEncryption.get_branch_key() == branch {
return Ok(wallet.view_key.clone());
}

// If we're trying to access any of the private keys, just say no bueno
if &TransactionKeyManagerBranch::Spend.get_branch_key() == branch {
return wallet
.private_spend_key
.clone()
.ok_or(KeyManagerServiceError::ImportedPrivateKeyInaccessible);
}
};
// If we're trying to access any of the private keys, just say no bueno
if &TransactionKeyManagerBranch::Spend.get_branch_key() == branch {
return wallet
.private_spend_key
.clone()
.ok_or(KeyManagerServiceError::ImportedPrivateKeyInaccessible);
}
},
}

let km = self
.key_managers
Expand Down Expand Up @@ -436,9 +435,7 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static

pub async fn import_key(&self, private_key: PrivateKey) -> Result<TariKeyId, KeyManagerServiceError> {
let public_key = PublicKey::from_secret_key(&private_key);
let hex_key = public_key.to_hex();
self.db.insert_imported_key(public_key.clone(), private_key)?;
trace!(target: LOG_TARGET, "Imported key {}", hex_key);
let key_id = KeyId::Imported { key: public_key };
Ok(key_id)
}
Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/transactions/key_manager/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use digest::consts::U64;
use strum_macros::EnumIter;
use tari_common_types::{
types::{ComAndPubSignature, Commitment, PrivateKey, PublicKey, RangeProof, Signature},
COMMS,
WALLET_COMMS_AND_SPEND_KEY_BRANCH,
};
use tari_comms::types::CommsDHKE;
use tari_crypto::{hashing::DomainSeparatedHash, ristretto::RistrettoComSig};
Expand Down Expand Up @@ -82,7 +82,7 @@ impl TransactionKeyManagerBranch {
TransactionKeyManagerBranch::KernelNonce => "kernel nonce".to_string(),
TransactionKeyManagerBranch::SenderOffset => "sender offset".to_string(),
TransactionKeyManagerBranch::SenderOffsetLedger => "sender offset ledger".to_string(),
TransactionKeyManagerBranch::Spend => COMMS.to_string(),
TransactionKeyManagerBranch::Spend => WALLET_COMMS_AND_SPEND_KEY_BRANCH.to_string(),
}
}

Expand All @@ -95,7 +95,7 @@ impl TransactionKeyManagerBranch {
"sender offset" => TransactionKeyManagerBranch::SenderOffset,
"sender offset ledger" => TransactionKeyManagerBranch::SenderOffsetLedger,
"nonce" => TransactionKeyManagerBranch::Nonce,
COMMS => TransactionKeyManagerBranch::Spend,
WALLET_COMMS_AND_SPEND_KEY_BRANCH => TransactionKeyManagerBranch::Spend,
_ => TransactionKeyManagerBranch::Nonce,
}
}
Expand Down
4 changes: 2 additions & 2 deletions base_layer/key_manager/src/key_manager_service/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::{fmt, str::FromStr};

use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
use tari_common_types::COMMS;
use tari_common_types::WALLET_COMMS_AND_SPEND_KEY_BRANCH;
use tari_crypto::keys::{PublicKey, SecretKey};
use tari_utilities::{hex::Hex, ByteArray};

Expand All @@ -43,7 +43,7 @@ impl KeyManagerBranch {
/// recovery.
pub fn get_branch_key(self) -> String {
match self {
KeyManagerBranch::Comms => COMMS.to_string(),
KeyManagerBranch::Comms => WALLET_COMMS_AND_SPEND_KEY_BRANCH.to_string(),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions base_layer/key_manager/src/key_manager_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use tari_crypto::{
hashing::DomainSeparatedHasher,
keys::{PublicKey, SecretKey},
};
use tari_utilities::{hex::Hex, ByteArray};
use tari_utilities::ByteArray;

use crate::{
cipher_seed::CipherSeed,
Expand Down Expand Up @@ -217,9 +217,7 @@ where

pub async fn import_key(&self, private_key: PK::K) -> Result<KeyId<PK>, KeyManagerServiceError> {
let public_key = PK::from_secret_key(&private_key);
let hex_key = public_key.to_hex();
self.db.insert_imported_key(public_key.clone(), private_key)?;
trace!(target: LOG_TARGET, "Imported key {}", hex_key);
let key_id = KeyId::Imported { key: public_key };
Ok(key_id)
}
Expand Down

0 comments on commit 7e602c2

Please sign in to comment.