Ensure scripthash is of the expected size before casting to a FullHash

This commit is contained in:
Nadav Ivgi 2019-02-17 11:57:34 +02:00
parent 3384a178a6
commit b98bd62763
No known key found for this signature in database
GPG Key ID: 81F6104CD0F150FC

View File

@ -879,7 +879,7 @@ fn to_scripthash(
) -> Result<FullHash, HttpError> {
match script_type {
"address" => address_to_scripthash(script_str, network),
"scripthash" => Ok(full_hash(&hex::decode(script_str)?)),
"scripthash" => parse_scripthash(script_str),
_ => bail!("Invalid script type".to_string()),
}
}
@ -898,6 +898,15 @@ fn address_to_scripthash(addr: &str, network: &Network) -> Result<FullHash, Http
Ok(compute_script_hash(&addr.script_pubkey()))
}
fn parse_scripthash(scripthash: &str) -> Result<FullHash, HttpError> {
let bytes = hex::decode(scripthash)?;
if bytes.len() != 32 {
Err(HttpError::from("Invalid scripthash".to_string()))
} else {
Ok(full_hash(&bytes))
}
}
#[derive(Debug)]
struct HttpError(StatusCode, String);