Refactor GetKey for sets to internally use Xpriv::get_key()

This commit is contained in:
Nadav Ivgi 2024-09-13 11:35:32 +03:00
parent d25c62bf45
commit d15c57bd1f
No known key found for this signature in database
GPG Key ID: 81F6104CD0F150FC
1 changed files with 5 additions and 12 deletions

View File

@ -803,18 +803,11 @@ impl GetKey for $set<Xpriv> {
key_request: KeyRequest,
secp: &Secp256k1<C>
) -> Result<Option<PrivateKey>, Self::Error> {
match key_request {
KeyRequest::Pubkey(_) => Err(GetKeyError::NotSupported),
KeyRequest::Bip32((fingerprint, path)) => {
for xpriv in self.iter() {
if xpriv.fingerprint(secp) == fingerprint {
let k = xpriv.derive_priv(secp, &path);
return Ok(Some(k.to_priv()));
}
}
Ok(None)
}
}
// OK to stop at the first error because Xpriv::get_key() can only fail
// if this isn't a KeyRequest::Bip32, which would fail for all Xprivs.
self.iter()
.find_map(|xpriv| xpriv.get_key(key_request.clone(), secp).transpose())
.transpose()
}
}}}
impl_get_key_for_set!(BTreeSet);