Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Orchard support to Zcash keyring #22870

Merged
merged 10 commits into from
Apr 30, 2024
Merged

Add Orchard support to Zcash keyring #22870

merged 10 commits into from
Apr 30, 2024

Conversation

cypt4
Copy link
Collaborator

@cypt4 cypt4 commented Apr 2, 2024

Resolves brave/brave-browser#32303
Pr targets brave_36128 until it is merged

Adds Orchard key generation to the ZCashKeyring.
Orchard keys are generated using Orchard crate. Orchard uses different keys generation strategy described here.
ExtendedSpendingKey is a sort of account private key. Using this key we can generate public or internal address then.

Also added orchard address support to Unified Addresses . Now we can generate address string using either transparent and orchard address parts.

Audit: https://github.com/brave/reviews/issues/1585

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

@cypt4 cypt4 requested review from a team as code owners April 2, 2024 10:05
@cypt4 cypt4 force-pushed the brave_37201_2 branch 2 times, most recently from 1a8d839 to 2a2c6f2 Compare April 2, 2024 12:40
@cypt4 cypt4 requested a review from bridiver as a code owner April 2, 2024 12:40
Copy link
Contributor

@nuo-xu nuo-xu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS build failed for Presubmit ERRORS please run npm: run presubmit -- --fix
Other than that, iOS lgtm

@darkdh darkdh self-requested a review April 2, 2024 22:41
components/brave_wallet/rust/lib.rs Outdated Show resolved Hide resolved
components/brave_wallet/rust/lib.rs Outdated Show resolved Hide resolved
components/brave_wallet/rust/BUILD.gn Outdated Show resolved Hide resolved
components/brave_wallet/rust/lib.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot added the CI/run-audit-deps Check for known npm/cargo vulnerabilities (audit_deps) label Apr 3, 2024
@cypt4 cypt4 requested review from a team as code owners April 4, 2024 14:34
@cypt4
Copy link
Collaborator Author

cypt4 commented Apr 4, 2024

@darkdh , @rillian - I've moved Orchard related code to the separate component, because of 2 reasons:

  1. I had problems with feature flags in FFI, so FFI didn't see passed features.
  2. There will be another PR with quite lot of Orchard related code, so it is better to have it separately.
    Not sure if macroses should be moved to separate component, any ideas?

components/zcash/BUILD.gn Outdated Show resolved Hide resolved
components/zcash/rs/lib.rs Outdated Show resolved Hide resolved
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

import("//build/rust/rust_static_library.gni")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, how did this end up in components/brave_wallet/zcash? It was supposed to go in the existing brave_wallet/browser/zcash directory. Adding yet another top level directory is exactly the opposite of what we want here. I'll move this to the correct place in my changes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to components/brave_wallet/zcash/rs


namespace brave_wallet {

HDKeyZip32::HDKeyZip32(std::unique_ptr<orchard::ExtendedSpendingKey> esk)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me it's a bit confusing that HDKeyZip32 does not have zcash or orchard anywhere in the name directory, but that's an internal wallet thing I guess for you guys to sort out so just providing some feedback. Technically you could use ExtendedSpendingKey directly if you wanted, but I didn't want to make any architectual changes outside of the rust related code


ExtendedSpendingKeyImpl::~ExtendedSpendingKeyImpl() = default;

std::unique_ptr<ExtendedSpendingKey>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be changed to base::expected with brave/brave-browser#37858
I just left it like this for now because I'm not sure what the errors should map to

deps = [
"//brave/components/brave_wallet/rust:rust_lib",
"//brave/third_party/rust/orchard/v0_7:lib",
"//third_party/rust/cxx/v1:lib",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be needed as it automatically gets added to rust_static_library as a public dep when cxx_bindings are enabled. What was the exact error you saw?

bytes: &[u8]
) -> Box<OrchardExtendedSpendingKeyResult> {
Box::new(OrchardExtendedSpendingKeyResult::from(
ExtendedSpendingKey::master(&bytes).map_err(Error::from))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: bytes is already a slice here, you don't need to pass it by reference.

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
Error::Zip32(e) => write!(f, "Error: {}", e.to_string()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: orchard implements the Display trait for Zip32Error, so this can just be

write!(f, "Zip32 Error: {e}"),

No need to call to_string().

Base automatically changed from brave_36128 to master April 29, 2024 18:57
Copy link
Contributor

[puLL-Merge] - brave/brave-core@22870

Here is my review of the PR in the desired format:

Description

This PR adds support for Orchard shielded transactions in the Brave Wallet for ZCash. It implements the ZIP 32 protocol for deriving Orchard keys and allows generating unified shielded addresses that include an Orchard component.

The main motivation seems to be to expand the ZCash functionality in the Brave Wallet to allow shielded transactions using the latest Orchard protocol, in addition to the existing transparent transactions.

Changes

Changes

  • components/brave_wallet/browser/BUILD.gn, components/brave_wallet/browser/internal/BUILD.gn:
    • Add build targets and dependencies for Orchard key generation code
  • components/brave_wallet/browser/internal/hd_key_zip32.cc, components/brave_wallet/browser/internal/hd_key_zip32.h:
    • Implement ZIP 32 protocol for deriving Orchard keys from a seed
  • components/brave_wallet/browser/internal/hd_key_zip32_unittest.cc:
    • Add unit tests for Orchard key derivation
  • components/brave_wallet/browser/keyring_service.cc:
    • Update to get transparent and shielded addresses from the ZCash keyring
  • components/brave_wallet/browser/zcash/rust/*:
    • Add Rust code to interface with the orchard crate for key derivation
  • components/brave_wallet/browser/zcash/zcash_keyring.cc, components/brave_wallet/browser/zcash/zcash_keyring.h:
    • Update ZCash keyring to derive Orchard keys and generate unified addresses
  • components/brave_wallet/common/*:
    • Update common code to support enabling Orchard and parsing unified addresses
  • Modify build files, add BUILD.gn for new targets

Security Hotspots

  1. Secure generation of Orchard keys from seed - ensure key generation is done safely without leaking sensitive data. The seed and derived keys must be kept private.
  2. Validating and parsing untrusted external input when decoding unified addresses in zcash_utils.cc. Failure to properly validate could lead to memory corruption or unexpected behavior if addresses are malformed.
  3. Clear secrets from memory after use, especially in the Rust key generation code. Ensure no sensitive key data is leaked or left in memory longer than needed.

In summary, the main security considerations are around safe key generation, protecting private keys, and validating external inputs. The changes look well-structured and have test coverage. Getting an external security review of the cryptography code would provide additional assurance.

@cypt4 cypt4 merged commit 9e9c99e into master Apr 30, 2024
19 checks passed
@cypt4 cypt4 deleted the brave_37201_2 branch April 30, 2024 14:50
@github-actions github-actions bot added this to the 1.67.x - Nightly milestone Apr 30, 2024
cdesouza-chromium added a commit that referenced this pull request May 1, 2024
bridiver pushed a commit that referenced this pull request May 1, 2024
* Revert "Fixes debug build."

This reverts commit 2aa6342.

* Revert "Add Orchard support to Zcash keyring (#22870)"

This reverts commit 9e9c99e.
cypt4 added a commit that referenced this pull request May 2, 2024
* Add Orchard support to Zcash keyring
Resolves brave/brave-browser#32303

* Review fix

* Review fix

* Review fix

* Review fix

* Switch enable_orchard buildflag

* Review fix

* encapsulate rust code inside wrappers so we don't expose cxx types

* Build fix

* Build&Review fix

---------

Co-authored-by: bridiver <[email protected]>
cypt4 added a commit that referenced this pull request May 6, 2024
* Add Orchard support to Zcash keyring
Resolves brave/brave-browser#32303

* Review fix

* Review fix

* Review fix

* Review fix

* Switch enable_orchard buildflag

* Review fix

* encapsulate rust code inside wrappers so we don't expose cxx types

* Build fix

* Build&Review fix

---------

Co-authored-by: bridiver <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/run-audit-deps Check for known npm/cargo vulnerabilities (audit_deps) feature/web3/wallet/core feature/web3/wallet puLL-Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ZCash] Implement Orchard Keyring