/// Parse a PEM format threshold signature public key from a named file.
///
/// # Arguments
/// * `pem_file` names the filesystem path where the key to be read from is
///   located.
/// # Returns
/// The decoded `ThresholdSigPublicKey`
/// # Error
/// * `std::io::Error` if the file cannot be opened, or if the contents
/// are not PEM, or if the encoded key is not BLS12-381.
Parse a PEM format threshold signature public key from a named file.
Parse a PEM format threshold signature public key from a named file .
pub fn parse_threshold_sig_key(pem_file: &Path) -> Result<ThresholdSigPublicKey> {
    fn invalid_data_err(msg: impl std::string::ToString) -> Error {
        Error::new(ErrorKind::InvalidData, msg.to_string())
    }
    let buf = std::fs::read(pem_file)?;
    let s = String::from_utf8_lossy(&buf);
    let lines: Vec<_> = s.trim_end().lines().collect();
    let n = lines.len();
    if n < 3 {
        return Err(invalid_data_err("input file is too short"));
    }
    if !lines[0].starts_with("-----BEGIN PUBLIC KEY-----") {
        return Err(invalid_data_err(
            "PEM file doesn't start with 'BEGIN PUBLIC KEY' block",
        ));
    }
    if !lines[n - 1].starts_with("-----END PUBLIC KEY-----") {
        return Err(invalid_data_err(
            "PEM file doesn't end with 'END PUBLIC KEY' block",
        ));
    }
    let decoded = base64::decode(&lines[1..n - 1].join(""))
        .map_err(|err| invalid_data_err(format!("failed to decode base64: {}", err)))?;
    let pubkey_bytes = bls12_381::api::public_key_from_der(&decoded)
        .map_err(|err| invalid_data_err(format!("failed to decode public key: {}", err)))?;
    Ok(ThresholdSigPublicKey::from(pubkey_bytes))
}
pub fn parse_threshold_sig_key ( pem_file : & Path ) -> Result < ThresholdSigPublicKey > { fn invalid_data_err ( msg : impl std :: string :: ToString ) -> Error { Error :: new ( ErrorKind :: InvalidData , msg . to_string ( ) ) } let buf = std :: fs :: read ( pem_file ) ? ; let s = String :: from_utf8_lossy ( & buf ) ; let lines : Vec < _ > = s . trim_end ( ) . lines ( ) . collect ( ) ; let n = lines . len ( ) ; if n < 3 { return Err ( invalid_data_err ( " " ) ) ; } if ! lines [ 0 ] . starts_with ( " " ) { return Err ( invalid_data_err ( " " , ) ) ; } if ! lines [ n - 1 ] . starts_with ( " " ) { return Err ( invalid_data_err ( " " , ) ) ; } let decoded = base64 :: decode ( & lines [ 1 .. n - 1 ] . join ( " " ) ) . map_err ( | err | invalid_data_err ( format ! ( " " err ) ) ) ? ; let pubkey_bytes = bls12_381 :: api :: public_key_from_der ( & decoded ) . map_err ( | err | invalid_data_err ( format ! ( " " err ) ) ) ? ; Ok ( ThresholdSigPublicKey :: from ( pubkey_bytes ) ) }
Parse a PEM format threshold signature public key from a named file.
Parse a PEM format threshold signature public key from a named file .
The decoded `ThresholdSigPublicKey`
The decoded ` ThresholdSigPublicKey `
pem_file
&Path
error
`std::io::Error` if the file cannot be opened, or if the contents
are not PEM, or if the encoded key is not BLS12-381.
` std :: io :: Error ` if the file cannot be opened or if the contents are not PEM or if the encoded key is not BLS12 - 381 .