Skip to content

Commit

Permalink
Re-use version-to-variant function
Browse files Browse the repository at this point in the history
Check Bech32 variant last, matching BIP-0350's order of operations.
Update failing tests to deliver proper error.
  • Loading branch information
clarkmoody committed Feb 19, 2021
1 parent efc95dc commit 43a1ed1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/lib.rs
Expand Up @@ -218,18 +218,18 @@ impl FromStr for WitnessProgram {
let program = Vec::from_base32(p5)?;
(v[0], program)
};
if (version.to_u8() == 0 && variant != Variant::Bech32)
|| (version.to_u8() != 0 && variant != Variant::Bech32m)
{
return Err(Error::InvalidEncoding);
}
let wp = WitnessProgram {
version,
program,
network: network_classified,
bech32: s.to_string(),
};
wp.validate()?;
if let Some(version_variant) = program_version_to_variant(version) {
if version_variant != variant {
return Err(Error::InvalidEncoding);
}
}
Ok(wp)
}
}
Expand Down Expand Up @@ -450,9 +450,9 @@ mod tests {
),
(
"BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2",
Error::InvalidEncoding,
Error::InvalidScriptVersion,
),
("bc1rw5uspcuh", Error::InvalidEncoding),
("bc1rw5uspcuh", Error::InvalidLength),
(
"bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90",
Error::Bech32(bech32::Error::InvalidLength),
Expand Down

0 comments on commit 43a1ed1

Please sign in to comment.