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

Unified Addresses support #21460

Merged
merged 8 commits into from
Jan 12, 2024
Merged

Unified Addresses support #21460

merged 8 commits into from
Jan 12, 2024

Conversation

cypt4
Copy link
Collaborator

@cypt4 cypt4 commented Dec 25, 2023

Resolves brave/brave-browser#32239

Resolves

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 lint, 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:

@github-actions github-actions bot added CI/storybook-url Deploy storybook and provide a unique URL for each build feature/web3/wallet feature/web3/wallet/core labels Dec 25, 2023
@cypt4 cypt4 force-pushed the brave_32239 branch 3 times, most recently from 367c99b to f7bcf22 Compare December 26, 2023 10:36
@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@@ -36,6 +43,9 @@ module.exports = function RunCommand (options) {
updatePatches(catapultDir, catapultPatchDir),
// third_party/devtools-frontend/src
updatePatches(devtoolsFrontendDir, devtoolsFrontendPatchDir),
// brave/third_party/bitcoin-core
updatePatches(bitcoinDir, bitcoinPatchDir),

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: empty line

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 48 to 51
return /^t1[a-km-zA-HJ-NP-Z1-9]{33}$/.test(value) || /^u1[a-z0-9]+$/.test(value)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: needs formatting

Comment on lines 16 to 18
std::string GetHPersonalizer(uint8_t i);
std::string GetGPersonalizer(uint8_t i, uint16_t j);

Copy link
Collaborator

Choose a reason for hiding this comment

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

These two do not need to be public

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They are used in tests

Comment on lines 78 to 94
std::vector<uint8_t> GetLeft(const std::vector<uint8_t>& message) {
size_t left_size =
std::min(static_cast<size_t>(kLeftSize), message.size() / 2);
return std::vector(message.begin(), message.begin() + left_size);
}

std::vector<uint8_t> GetRight(const std::vector<uint8_t>& message) {
size_t left_size =
std::min(static_cast<size_t>(kLeftSize), message.size() / 2);
return std::vector(message.begin() + left_size, message.end());
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

static_cast<size_t>(kLeftSize)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 21 to 50
std::string GetHPersonalizer(uint8_t i) {
return {85, 65, 95, 70, 52, 74, 117,
109, 98, 108, 101, 95, 72, static_cast<char>(i),
0, 0};
}

std::string GetGPersonalizer(uint8_t i, uint16_t j) {
return {85,
65,
95,
70,
52,
74,
117,
109,
98,
108,
101,
95,
71,
static_cast<char>(i),
static_cast<char>(j & 0xFF),
static_cast<char>(j >> 8)};
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

These should return std:array<uint8_t, 16>

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 41 to 22
TEST(F4JumbleUnitTest, ApplyF4Jumble) {
{
std::vector<uint8_t> input = {};

std::vector<uint8_t> expected = {};

EXPECT_EQ(expected, ApplyF4Jumble(input));
}

{
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to test Apply + Revert resulting in original input. Also covering corner cases for input sizes.
Something like

std::vector<uint8_t> input(1000, 'a');
auto input_span = make_span(input);
for (int i = 0; i < input_span.size(); ++i) {
  auto d = input_span.subspan(0, i);
  EXPECT_EQ(d, RevertF4Jumble(ApplyF4Jumble(d)));
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 52 to 56
if (personalizer.length() != sizeof(params.personal)) {
NOTREACHED();
return std::nullopt;
}

Copy link
Collaborator

@supermassive supermassive Dec 27, 2023

Choose a reason for hiding this comment

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

Personalizer should be a fixed size span, and this check should become a CHECK or a static assert

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 61 to 81
if (blake2b_init_param(&blake_state, &params) != 0) {
NOTREACHED();
return std::nullopt;
}
if (blake2b_update(&blake_state, payload.data(), payload.size()) != 0) {
NOTREACHED();
return std::nullopt;
}
std::vector<uint8_t> result(len);
if (blake2b_final(&blake_state, result.data(), len) != 0) {
NOTREACHED();
return std::nullopt;
}

return result;
Copy link
Collaborator

Choose a reason for hiding this comment

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

blake2b_* calls fail only on invalid inputs. We should just CHECK the result and blake2b function should not return an optional

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 97 to 101
if (!hash || hash->size() != left.size()) {
return std::nullopt;
}
std::vector<uint8_t> result;
Copy link
Collaborator

Choose a reason for hiding this comment

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

blake2b should never fail and always return hash size equal to left.size(). So this method never fails an no optional needed.
Same for g_round

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 29 to 39
// https://zips.z.cash/zip-0316#encoding-of-unified-addresses
enum AddrType {
P2PKH = 0x00,
P2PSH = 0x01,
Sapling = 0x02,
Orchard = 0x03,
End = 0x04
};

Copy link
Collaborator

Choose a reason for hiding this comment

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

The spec does not mention End = 0x04. What is it?

Copy link
Collaborator Author

@cypt4 cypt4 Dec 27, 2023

Choose a reason for hiding this comment

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

Just to check whether code is known (less than End)


for (const auto& part : parts.value()) {
if (part.first == AddrType::P2PKH) {
return PubkeyToTransparentAddress(part.second, false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

false is_testnet ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 195 to 216
auto reverted = RevertF4Jumble(data);
if (!reverted || reverted->size() < 16) {
return std::nullopt;
}

auto parts = ParseUnifiedAddress(
base::make_span(*reverted).subspan(0, reverted->size() - 16));
if (!parts.has_value()) {
return std::nullopt;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs a comment and a constant for what is 16

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

}

auto reverted = RevertF4Jumble(data);
// HRP with 16 bytes padding which is appened to the end of message
Copy link
Collaborator

Choose a reason for hiding this comment

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

appended

}

auto parts = ParseUnifiedAddress(
base::make_span(*reverted).subspan(0, reverted->size() - 16));
Copy link
Collaborator

Choose a reason for hiding this comment

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

kPaddedHrpSize

@@ -48,4 +48,13 @@ TEST(ZCashUtilsUnitTest, PubkeyToTransparentAddress) {
false));
}

TEST(ZCashUtilsUnitTest, ExtractTransparentPart) {
auto transparent_past = ExtractTransparentPart(
Copy link
Collaborator

Choose a reason for hiding this comment

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

transparent_part

Comment on lines 47 to 52
export function isValidZecAddress(value: string): boolean {
return /^t1[a-km-zA-HJ-NP-Z1-9]{33}$/.test(value)
return /^t1[a-km-zA-HJ-NP-Z1-9]{33}$/.test(value) ||
/^u1[a-z0-9]+$/.test(value)
}

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 handle testnet also

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Copy link
Collaborator

@mkarolin mkarolin left a comment

Choose a reason for hiding this comment

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

strings++

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Copy link
Member

@goodov goodov left a comment

Choose a reason for hiding this comment

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

f4_jumble.cc, chromium_src/patches lgtm.

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Copy link
Collaborator

@bridiver bridiver left a comment

Choose a reason for hiding this comment

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

util.js changes ok

Copy link
Contributor

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

Description

The pull request appears to be integrating Bitcoin Core support into Brave by adding patching capabilities and updating utility functions for ZCash addresses. It includes the proper patching for the new bitcoin-core directory, modification to the patching scripts, replacing absl::nullopt with std::nullopt, adding internationalization/localization improvements, and adding new functionalities related with F4 jumbling for unified addresses in ZCash.

Changes

Changes

Patch Updates

  • build/commands/lib/util.js

    • Added support for the patching of Bitcoin Core dependencies.
  • build/commands/scripts/updatePatches.js

    • Updated the script to handle patching of Bitcoin Core related sources.
  • components/brave_wallet/browser/keyring_service.cc

    • Replaced absl::nullopt with std::nullopt.

ZCash Address Handling

  • components/brave_wallet/browser/zcash/zcash_rpc.cc

    • Improved error handling by replacing generic error strings with localized string identifiers.
  • components/brave_wallet/browser/zcash/zcash_wallet_service.cc

    • Made similar improvements for error messages and implemented unified address extraction logic for ZCash.
  • components/brave_wallet/browser/zcash/zcash_wallet_service_tasks.cc

    • Updated error message handling in transaction tasks.

New Features (F4 Jumble)

  • components/brave_wallet/common/BUILD.gn

    • Added new sources for the F4 jumble implementation.
  • components/brave_wallet/common/DEPS

    • Included new third-party dependencies used in the F4 jumble implementation.
  • components/brave_wallet/common/f4_jumble.cc

    • New implementation file providing logic for F4 jumbling (a form of encryption/obfuscation).
  • components/brave_wallet/common/f4_jumble.h

    • Header file for F4 jumble utilities.
  • components/brave_wallet/common/f4_jumble_unittest.cc

    • Unit tests covering the F4 jumble functionalities.
  • components/brave_wallet/common/zcash_utils.cc

    • Additional utility functions to handle parsing and interaction with unified ZCash addresses.
  • components/brave_wallet/common/zcash_utils.h

    • Headers for the new ZCash utility functions.
  • components/brave_wallet/common/zcash_utils_unittest.cc

    • Unit tests for the ZCash utility functions.
  • components/brave_wallet_ui/utils/address-utils.ts

    • Updates to utility functions to accommodate unified ZCash addresses.

Localization

  • components/resources/wallet_strings.grdp
    • Added a new string definition for unified address error messaging.

Dependency Patching

  • a/patches/brave/third_party/bitcoin-core/src/src-bech32.cpp.patch
    • New patch file needed for Bitcoin Core dependency.

Build Script

  • script/brave_license_helper.py
    • Adjustment to the licensing helper script to cover the new patch directory structure.

Security Hotspots

  • components/brave_wallet/common/f4_jumble.cc

    • Introduction of new cryptographic utility, which should be reviewed for potential vulnerabilities in its implementation, such as information leakage or improper usage.
  • components/brave_wallet/browser/zcash/zcash_wallet_service_tasks.cc / components/brave_wallet/browser/zcash/zcash_wallet_service.cc / components/brave_wallet/common/zcash_utils.cc

    • For all the files related to ZCash address handling, there is a risk of mishandling the addresses, leading to potentially leaking user information or sending funds to incorrect addresses due to parsing and handling errors.
  • Dependency Patching (a/patches/brave/third_party/bitcoin-core/src/src-bech32.cpp.patch)

    • Any new patch introduces a risk of vulnerability if not properly reviewed. This specific patch should be checked for changes aligning with best practices for cryptographic algorithm modifications or potential introduction of weaknesses.

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@cypt4 cypt4 merged commit 3a58a81 into master Jan 12, 2024
18 checks passed
@cypt4 cypt4 deleted the brave_32239 branch January 12, 2024 07:14
@github-actions github-actions bot added this to the 1.63.x - Nightly milestone Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/storybook-url Deploy storybook and provide a unique URL for each build feature/web3/wallet/core feature/web3/wallet puLL-Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ZCash] Implement Unifier interface