Add derivation methods for ZIP 325 private use metadata keys#1686
Conversation
daira
left a comment
There was a problem hiding this comment.
utACK with non-blocking comments.
|
Force-pushed to remove the compilation hotfix commit that created another JNI mismatch. Instead I'm going to fix #1684 and #1657 in a separate PR, then rebase this on top of it. |
|
Rebased on 1687 to fix JNI issues and address comments. |
| let account_metadata_key_sk = utils::java_bytes_to_rust(env, &account_metadata_key_sk)?; | ||
| let account_metadata_key_c = utils::java_bytes_to_rust(env, &account_metadata_key_c)?; | ||
| let ufvk_string = utils::java_nullable_string_to_rust(env, &ufvk_string)?; | ||
| let private_use_subject = utils::java_bytes_to_rust(env, &private_use_subject)?; | ||
| let network = parse_network(network_id as u32)?; | ||
|
|
||
| let account_metadata_key = { | ||
| let sk = account_metadata_key_sk | ||
| .as_slice() | ||
| .try_into() | ||
| .map_err(|_| anyhow!("Incorrect length for account_metadata_key_sk"))?; | ||
|
|
||
| let chain_code = ChainCode::new( | ||
| account_metadata_key_c | ||
| .as_slice() | ||
| .try_into() | ||
| .map_err(|_| anyhow!("Incorrect length for account_metadata_key_c"))?, | ||
| ); | ||
|
|
||
| zip32::registered::SecretKey::from_parts(sk, chain_code) | ||
| }; |
There was a problem hiding this comment.
fn registered_secret_key_from_jni(
env: &JNIEnv,
key_sk: JByteArray,
key_c: JByteArray,
) -> anyhow::Result<zip32::registered::SecretKey> {
let sk = utils::java_bytes_to_rust(env, &key_sk)?
.as_slice()
.try_into()
.map_err(|_| anyhow!("Incorrect length for key_sk"))?;
let c = utils::java_bytes_to_rust(env, &key_c)?
.as_slice()
.try_into()
.map_err(|_| anyhow!("Incorrect length for key_c"))?;
Ok(zip32::registered::SecretKey::from_parts(sk, ChainCode::new(c)))
}| let account_metadata_key_sk = utils::java_bytes_to_rust(env, &account_metadata_key_sk)?; | |
| let account_metadata_key_c = utils::java_bytes_to_rust(env, &account_metadata_key_c)?; | |
| let ufvk_string = utils::java_nullable_string_to_rust(env, &ufvk_string)?; | |
| let private_use_subject = utils::java_bytes_to_rust(env, &private_use_subject)?; | |
| let network = parse_network(network_id as u32)?; | |
| let account_metadata_key = { | |
| let sk = account_metadata_key_sk | |
| .as_slice() | |
| .try_into() | |
| .map_err(|_| anyhow!("Incorrect length for account_metadata_key_sk"))?; | |
| let chain_code = ChainCode::new( | |
| account_metadata_key_c | |
| .as_slice() | |
| .try_into() | |
| .map_err(|_| anyhow!("Incorrect length for account_metadata_key_c"))?, | |
| ); | |
| zip32::registered::SecretKey::from_parts(sk, chain_code) | |
| }; | |
| let account_metadata_key = registered_secret_key_from_jni(env, account_metadata_key_sk, account_metadata_key_c)?; | |
| let ufvk_string = utils::java_nullable_string_to_rust(env, &ufvk_string)?; | |
| let private_use_subject = utils::java_bytes_to_rust(env, &private_use_subject)?; | |
| let network = parse_network(network_id as u32)?; |
There was a problem hiding this comment.
This file is useless cruft. The typesafe mapping is already done in DerivationToolExt.
In a follow-up PR, delete Typesafe from the method names in Derivation, then delete this file and the only use of it in DerivationTool, which can then just use:
private val instance = SuspendingLazy<Unit, DerivationTool> { RustDerivationTool.new() }|
|
||
| /** | ||
| * Derives a metadata key for private use from a ZIP 325 Account Metadata Key. | ||
| * |
There was a problem hiding this comment.
| * | |
| * | |
| * This method should only be used via `model.AccountMetadataKey` (#1685). | |
| * |
|
Rebased on |
|
The rest of the review comments I'm not addressing (either because they aren't relevant to this PR, or because I think the current abstraction is fine). |
|
I cannot fix the ktlint failure, as doing so creates a detekt lint failure. |
Co-authored-by: Daira-Emma Hopwood <daira@jacaranda.org>
250d113 to
b303662
Compare
|
Rebased on the rust-updates branch to fix the ktlint warning. |
HonzaR
left a comment
There was a problem hiding this comment.
Left one non-blocking comment inline.
| import cash.z.ecc.android.sdk.tool.DerivationTool | ||
|
|
||
| /** | ||
| * A [ZIP 325](https://zips.z.cash/zip-0325) Account Metadata Key. |
There was a problem hiding this comment.
Should this page exist already, or is it a pre-existing ZIP? I get 404 on this URL.
There was a problem hiding this comment.
That URL will exist once zcash/zips#979 is finished and merged.
Author
Reviewer
Footnotes
Code often looks different when reviewing the diff in a browser, making it easier to spot potential bugs. ↩
While we aim for automated testing of the SDK, some aspects require manual testing. If you had to manually test
something during development of this pull request, write those steps down. ↩
While we are not looking for perfect coverage, the tool can point out potential cases that have been missed. Code coverage can be generated with:
./gradlew checkfor Kotlin modules and./gradlew connectedCheck -PIS_ANDROID_INSTRUMENTATION_TEST_COVERAGE_ENABLED=truefor Android modules. ↩Having your code up to date and squashed will make it easier for others to review. Use best judgement when squashing commits, as some changes (such as refactoring) might be easier to review as a separate commit. ↩
In addition to a first pass using the code review guidelines, do a second pass using your best judgement and experience which may identify additional questions or comments. Research shows that code review is most effective when done in multiple passes, where reviewers look for different things through each pass. ↩
While the CI server runs the demo app to look for build failures or crashes, humans running the demo app are
more likely to notice unexpected log messages, UI inconsistencies, or bad output data. Perform this step last, after verifying the code changes are safe to run locally. ↩