Skip to content

Commit

Permalink
zcash_transparent: Add AccountPubKey::derive_pubkey_at_bip32_path
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Dec 16, 2024
1 parent 7bff034 commit 831f898
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions zcash_transparent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog
All notable changes to this library will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this library adheres to Rust's notion of
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

The entries below are relative to the `zcash_primitives` crate as of the tag
`zcash_primitives-0.20.0`.

### Added
- `zcash_transparent::keys::AccountPubKey::derive_pubkey_at_bip32_path`
34 changes: 34 additions & 0 deletions zcash_transparent/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,40 @@ impl AccountPubKey {
.public_key())
}

/// Derives the public key corresponding to the given full BIP 32 path.
///
/// This enforces that the path has a prefix that could have been used to derive this
/// `AccountPubKey`.
pub fn derive_pubkey_at_bip32_path<P: consensus::Parameters>(

Check warning on line 270 in zcash_transparent/src/keys.rs

View check run for this annotation

Codecov / codecov/patch

zcash_transparent/src/keys.rs#L270

Added line #L270 was not covered by tests
&self,
params: &P,
expected_account_index: AccountId,
path: &[ChildNumber],
) -> Result<secp256k1::PublicKey, bip32::Error> {
if path.len() > 3 {
Err(bip32::Error::ChildNumber)

Check warning on line 277 in zcash_transparent/src/keys.rs

View check run for this annotation

Codecov / codecov/patch

zcash_transparent/src/keys.rs#L276-L277

Added lines #L276 - L277 were not covered by tests
} else {
match path.split_at(3) {

Check warning on line 279 in zcash_transparent/src/keys.rs

View check run for this annotation

Codecov / codecov/patch

zcash_transparent/src/keys.rs#L279

Added line #L279 was not covered by tests
(
[ChildNumber(44 | ChildNumber::HARDENED_FLAG), coin_type, account_index],
sub_path,
) if coin_type.is_hardened()
&& coin_type.index() == params.network_type().coin_type()
&& account_index.is_hardened()
&& account_index.index() == expected_account_index.into() =>

Check warning on line 286 in zcash_transparent/src/keys.rs

View check run for this annotation

Codecov / codecov/patch

zcash_transparent/src/keys.rs#L281-L286

Added lines #L281 - L286 were not covered by tests
{
sub_path

Check warning on line 288 in zcash_transparent/src/keys.rs

View check run for this annotation

Codecov / codecov/patch

zcash_transparent/src/keys.rs#L288

Added line #L288 was not covered by tests
.iter()
.try_fold(self.0.clone(), |acc, child_index| {
acc.derive_child(*child_index)

Check warning on line 291 in zcash_transparent/src/keys.rs

View check run for this annotation

Codecov / codecov/patch

zcash_transparent/src/keys.rs#L290-L291

Added lines #L290 - L291 were not covered by tests
})
.map(|k| *k.public_key())

Check warning on line 293 in zcash_transparent/src/keys.rs

View check run for this annotation

Codecov / codecov/patch

zcash_transparent/src/keys.rs#L293

Added line #L293 was not covered by tests
}
_ => Err(bip32::Error::ChildNumber),

Check warning on line 295 in zcash_transparent/src/keys.rs

View check run for this annotation

Codecov / codecov/patch

zcash_transparent/src/keys.rs#L295

Added line #L295 was not covered by tests
}
}
}

/// Derives the internal ovk and external ovk corresponding to this
/// transparent fvk. As specified in [ZIP 316][transparent-ovk].
///
Expand Down

0 comments on commit 831f898

Please sign in to comment.