Load the secret key for an identity.
(
&self,
id: &IdentityId,
_password: Option<&str>,
)
| 570 | |
| 571 | /// Load the secret key for an identity. |
| 572 | pub fn load_secret_key( |
| 573 | &self, |
| 574 | id: &IdentityId, |
| 575 | _password: Option<&str>, |
| 576 | ) -> Result<SecretKey, IdentityError> { |
| 577 | let identity_dir = self.find_identity_dir(id)?; |
| 578 | let secret_path = identity_dir.join(Self::SECRET_KEY_FILE); |
| 579 | |
| 580 | if !secret_path.exists() { |
| 581 | return Err(IdentityError::NotFound { |
| 582 | name: format!("secret key for {}", id.short()), |
| 583 | }); |
| 584 | } |
| 585 | |
| 586 | let content = fs::read_to_string(&secret_path)?; |
| 587 | let stored: StoredSecretKey = toml::from_str(&content)?; |
| 588 | |
| 589 | if stored.encryption != "none" { |
| 590 | // TODO: Implement decryption |
| 591 | return Err(IdentityError::PasswordRequired); |
| 592 | } |
| 593 | |
| 594 | let bytes = data_encoding::BASE64 |
| 595 | .decode(stored.data.as_bytes()) |
| 596 | .map_err(|_| IdentityError::InvalidKey("Invalid base64 in secret key".to_string()))?; |
| 597 | |
| 598 | if bytes.len() != 32 { |
| 599 | return Err(IdentityError::InvalidKey(format!( |
| 600 | "Invalid secret key length: expected 32, got {}", |
| 601 | bytes.len() |
| 602 | ))); |
| 603 | } |
| 604 | |
| 605 | let mut arr = [0u8; 32]; |
| 606 | arr.copy_from_slice(&bytes); |
| 607 | Ok(SecretKey::from_bytes(&arr)) |
| 608 | } |
| 609 | |
| 610 | /// Load a keypair for an identity. |
| 611 | pub fn load_keypair( |
no test coverage detected