Skip to content

Commit

Permalink
fix the use of the comms key, adding another key type to make managed…
Browse files Browse the repository at this point in the history
…ment easier
  • Loading branch information
SWvheerden committed Aug 6, 2024
1 parent 90a4c28 commit 061e0b6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub enum Branch {
Spend = 0x07,
RandomKey = 0x08,
PreMine = 0x09,
Comms = 0x0A,
}

impl Branch {
Expand All @@ -143,6 +144,7 @@ impl Branch {
0x07 => Some(Branch::Spend),
0x08 => Some(Branch::RandomKey),
0x09 => Some(Branch::PreMine),
0x0A => Some(Branch::Comms),
_ => None,
}
}
Expand Down
19 changes: 18 additions & 1 deletion base_layer/common_types/src/key_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum TransactionKeyManagerBranch {
Spend = Branch::Spend as u8,
RandomKey = Branch::RandomKey as u8,
PreMine = Branch::PreMine as u8,
Comms = Branch::Comms as u8,
}

const DATA_ENCRYPTION: &str = "data encryption";
Expand Down Expand Up @@ -67,6 +68,7 @@ impl TransactionKeyManagerBranch {
TransactionKeyManagerBranch::RandomKey => RANDOM_KEY.to_string(),
TransactionKeyManagerBranch::Spend => WALLET_COMMS_AND_SPEND_KEY_BRANCH.to_string(),
TransactionKeyManagerBranch::PreMine => PRE_MINE.to_string(),
TransactionKeyManagerBranch::Comms => WALLET_COMMS_AND_SPEND_KEY_BRANCH.to_string(),
}
}

Expand All @@ -80,7 +82,10 @@ impl TransactionKeyManagerBranch {
SENDER_OFFSET => TransactionKeyManagerBranch::SenderOffset,
ONE_SIDED_SENDER_OFFSET => TransactionKeyManagerBranch::OneSidedSenderOffset,
RANDOM_KEY => TransactionKeyManagerBranch::RandomKey,
WALLET_COMMS_AND_SPEND_KEY_BRANCH => TransactionKeyManagerBranch::Spend,
// We need to make this Comms as to ensure if you actually save the key, it must have been comms, as for
// normal wallets, the comms and spend key are the same, but for ledger and provided wallets they do not
// have access to the spend key so they could only have saved the comms key
WALLET_COMMS_AND_SPEND_KEY_BRANCH => TransactionKeyManagerBranch::Comms,
PRE_MINE => TransactionKeyManagerBranch::PreMine,
_ => TransactionKeyManagerBranch::Nonce,
}
Expand All @@ -102,6 +107,7 @@ impl TransactionKeyManagerBranch {
Some(Branch::Spend) => Some(TransactionKeyManagerBranch::Spend),
Some(Branch::RandomKey) => Some(TransactionKeyManagerBranch::RandomKey),
Some(Branch::PreMine) => Some(TransactionKeyManagerBranch::PreMine),
Some(Branch::Comms) => Some(TransactionKeyManagerBranch::Comms),
None => None,
}
}
Expand Down Expand Up @@ -173,6 +179,11 @@ mod test {
RANDOM_KEY,
),
(Branch::PreMine as u8, TransactionKeyManagerBranch::PreMine, PRE_MINE),
(
Branch::Comms as u8,
TransactionKeyManagerBranch::Comms,
WALLET_COMMS_AND_SPEND_KEY_BRANCH,
),
];

for (expected_byte, branch, key) in &mappings {
Expand Down Expand Up @@ -237,6 +248,12 @@ mod test {
assert_eq!(&branch.get_branch_key(), *key);
assert_eq!(TransactionKeyManagerBranch::from_key(key), *branch);
},
TransactionKeyManagerBranch::Comms => {
assert_eq!(branch.as_byte(), *expected_byte);
assert_eq!(TransactionKeyManagerBranch::from_byte(*expected_byte), Some(*branch));
assert_eq!(&branch.get_branch_key(), *key);
assert_eq!(TransactionKeyManagerBranch::from_key(key), *branch);
},
}
}
}
Expand Down
36 changes: 10 additions & 26 deletions base_layer/core/src/transactions/key_manager/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,9 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static

// 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(key_id.to_string()),
// );
return Ok(PrivateKey::default())
return wallet.private_spend_key.clone().ok_or(
KeyManagerServiceError::ImportedPrivateKeyInaccessible(key_id.to_string()),
);
}
},
}
Expand Down Expand Up @@ -483,7 +482,7 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static

pub async fn get_comms_key(&self) -> Result<KeyAndId<PublicKey>, KeyManagerServiceError> {
let key_id = KeyId::Managed {
branch: TransactionKeyManagerBranch::Spend.get_branch_key(),
branch: TransactionKeyManagerBranch::Comms.get_branch_key(),
index: 0,
};
let private_key = self.get_private_comms_key().await?;
Expand Down Expand Up @@ -532,28 +531,13 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
}

async fn get_private_comms_key(&self) -> Result<PrivateKey, KeyManagerServiceError> {
let branch = TransactionKeyManagerBranch::Spend.get_branch_key();
let branch = TransactionKeyManagerBranch::Comms.get_branch_key();
let index = 0;

match self.wallet_type {
WalletType::DerivedKeys | WalletType::ProvidedKeys(_) => {
self.get_private_key(&TariKeyId::Managed {
branch: branch.clone(),
index,
})
.await
},
WalletType::Ledger(_) => {
let km = self
.key_managers
.get(&branch)
.ok_or_else(|| self.unknown_key_branch_error("get_private_comms_key", &branch))?
.read()
.await;
let key = km.get_private_key(index)?;
Ok(key)
},
}
self.get_private_key(&TariKeyId::Managed {
branch: branch.clone(),
index,
})
.await
}

/// Calculates a script key id from the spend key id, if a public key is provided, it will only return a result of
Expand Down

0 comments on commit 061e0b6

Please sign in to comment.