Skip to content

Commit f6457ab

Browse files
committed
btc/policies: move some cheap validation checks to the beginning
1 parent 7c2c8a9 commit f6457ab

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,9 @@ impl<'a> ParsedPolicy<'a> {
251251
/// - At least one of the keys is ours
252252
/// - There are no duplicate or missing xpubs
253253
/// - No duplicate keys in the policy
254-
fn validate(&self, coin: BtcCoin) -> Result<(), Error> {
255-
check_enabled(coin)?;
256-
254+
fn validate(&self) -> Result<(), Error> {
257255
let policy = self.policy;
258256

259-
if policy.keys.len() > MAX_KEYS {
260-
return Err(Error::InvalidInput);
261-
}
262-
263257
self.validate_keys()?;
264258

265259
let our_root_fingerprint = crate::keystore::root_fingerprint()?;
@@ -368,6 +362,11 @@ impl<'a> ParsedPolicy<'a> {
368362
/// The parsed output keeps the key strings as is (e.g. "@0/**"). They will be processed and
369363
/// replaced with actual pubkeys in a later step.
370364
pub fn parse(policy: &Policy, coin: BtcCoin) -> Result<ParsedPolicy, Error> {
365+
check_enabled(coin)?;
366+
if policy.keys.len() > MAX_KEYS {
367+
return Err(Error::InvalidInput);
368+
}
369+
371370
let desc = policy.policy.as_str();
372371
let parsed = match desc.as_bytes() {
373372
// Match wsh(...).
@@ -383,7 +382,7 @@ pub fn parse(policy: &Policy, coin: BtcCoin) -> Result<ParsedPolicy, Error> {
383382
}
384383
_ => return Err(Error::InvalidInput),
385384
};
386-
parsed.validate(coin)?;
385+
parsed.validate()?;
387386
Ok(parsed)
388387
}
389388

0 commit comments

Comments
 (0)