Skip to content

Commit

Permalink
Don't use the ledger unless both keys are for ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Aug 21, 2024
1 parent 6e387a8 commit 2889e23
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 2 additions & 0 deletions applications/minotari_ledger_wallet/wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub enum KeyType {
OneSidedSenderOffset = 0x04,
Random = 0x06,
PreMine = 0x07,
MetadataEphemeralNonce = 0x08,
}

impl KeyType {
Expand All @@ -166,6 +167,7 @@ impl KeyType {
BranchMapping::Spend => Ok(Self::Spend),
BranchMapping::RandomKey => Ok(Self::Random),
BranchMapping::PreMine => Ok(Self::PreMine),
BranchMapping::MetadataEphemeralNonce => Ok(Self::MetadataEphemeralNonce),
_ => Err(AppSW::BadBranchKey),
}
} else {
Expand Down
12 changes: 12 additions & 0 deletions base_layer/common_types/src/key_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ impl TransactionKeyManagerBranch {
None => None,
}
}

pub fn is_ledger_branch(value: &str) -> bool {
let branch = TransactionKeyManagerBranch::from_key(value);
matches!(
branch,
TransactionKeyManagerBranch::OneSidedSenderOffset |
TransactionKeyManagerBranch::Spend |
TransactionKeyManagerBranch::RandomKey |
TransactionKeyManagerBranch::PreMine |
TransactionKeyManagerBranch::MetadataEphemeralNonce
)
}
}

#[cfg(test)]
Expand Down
31 changes: 21 additions & 10 deletions base_layer/core/src/transactions/key_manager/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,16 +1318,27 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
branch: nonce_branch,
index: nonce_index,
} => {
let signature = ledger_get_raw_schnorr_signature(
ledger.account,
*private_key_index,
TransactionKeyManagerBranch::from_key(private_key_branch),
*nonce_index,
TransactionKeyManagerBranch::from_key(nonce_branch),
challenge,
)
.map_err(|e| KeyManagerServiceError::LedgerError(e.to_string()))?;
Ok(signature)
if TransactionKeyManagerBranch::is_ledger_branch(&private_key_branch) &&
TransactionKeyManagerBranch::is_ledger_branch(&nonce_branch)
{
let signature = ledger_get_raw_schnorr_signature(
ledger.account,
*private_key_index,
TransactionKeyManagerBranch::from_key(private_key_branch),
*nonce_index,
TransactionKeyManagerBranch::from_key(nonce_branch),
challenge,
)
.map_err(|e| KeyManagerServiceError::LedgerError(e.to_string()))?;
Ok(signature)
} else {
let private_key = self.get_private_key(private_key_id).await?;
let private_nonce = self.get_private_key(nonce_key_id).await?;
let signature =
Signature::sign_raw_uniform(&private_key, private_nonce, challenge)?;

Ok(signature)
}
},
_ => Err(self.key_id_not_supported_error(
"sign_with_nonce_and_challenge",
Expand Down

0 comments on commit 2889e23

Please sign in to comment.