Skip to content

Commit

Permalink
Merge pull request #3231 from autonomys/check-entropy-length-explicitly
Browse files Browse the repository at this point in the history
Check for entropy length explicitly
  • Loading branch information
nazar-pc authored Nov 12, 2024
2 parents 64b6dd9 + dc0e2e2 commit 52c1b72
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions crates/subspace-farmer/src/single_disk_farm/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub enum IdentityError {
/// I/O error occurred
#[error("Identity I/O error: {0}")]
Io(#[from] io::Error),
/// Invalid contents
#[error("Invalid contents")]
InvalidContents,
/// Decoding error
#[error("Decoding error: {0}")]
Decoding(#[from] parity_scale_codec::Error),
Expand Down Expand Up @@ -95,6 +98,10 @@ impl Identity {
let IdentityFileContents { entropy } =
IdentityFileContents::decode(&mut bytes.as_ref())?;

if entropy.len() != ENTROPY_LENGTH {
return Err(IdentityError::InvalidContents);
}

Ok(Some(Self {
keypair: Zeroizing::new(keypair_from_entropy(&entropy)),
entropy: Zeroizing::new(entropy),
Expand Down Expand Up @@ -124,29 +131,6 @@ impl Identity {
})
}

/// Create identity from given entropy, overrides identity that might already exist.
///
/// Primarily used for testing.
#[doc(hidden)]
pub fn from_entropy<B: AsRef<Path>>(
base_directory: B,
entropy: Vec<u8>,
) -> Result<Self, IdentityError> {
let identity_file = base_directory.as_ref().join(Self::FILE_NAME);
debug!("Creating identity from provided entropy");

let identity_file_contents = IdentityFileContents { entropy };
fs::write(identity_file, identity_file_contents.encode())?;

let IdentityFileContents { entropy } = identity_file_contents;

Ok(Self {
keypair: Zeroizing::new(keypair_from_entropy(&entropy)),
entropy: Zeroizing::new(entropy),
substrate_ctx: schnorrkel::context::signing_context(REWARD_SIGNING_CONTEXT),
})
}

/// Returns the public key of the identity.
pub fn public_key(&self) -> &PublicKey {
&self.keypair.public
Expand Down

0 comments on commit 52c1b72

Please sign in to comment.