Skip to content

Commit

Permalink
Shielded inputs support WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Aug 20, 2024
1 parent 95be7b0 commit bbaaa84
Show file tree
Hide file tree
Showing 27 changed files with 2,535 additions and 22 deletions.
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ static_library("browser") {
"zcash/zcash_create_shield_all_transaction_task.h",
"zcash/zcash_orchard_storage.cc",
"zcash/zcash_orchard_storage.h",
"zcash/zcash_orchard_sync_state.cc",
"zcash/zcash_orchard_sync_state.h",
"zcash/zcash_shield_sync_service.cc",
"zcash/zcash_shield_sync_service.h",
]
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/internal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ if (enable_orchard) {
"orchard_block_scanner.h",
"orchard_bundle_manager.cc",
"orchard_bundle_manager.h",
"orchard_shard_tree_manager.cc",
"orchard_shard_tree_manager.h",
]
deps = [ "//brave/components/brave_wallet/browser/zcash/rust" ]
}
Expand Down
21 changes: 18 additions & 3 deletions components/brave_wallet/browser/internal/orchard_block_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ namespace brave_wallet {
OrchardBlockScanner::Result::Result() = default;

OrchardBlockScanner::Result::Result(std::vector<OrchardNote> discovered_notes,
std::vector<OrchardNullifier> spent_notes)
std::vector<OrchardNullifier> spent_notes,
std::vector<OrchardCommitment> commitments)
: discovered_notes(std::move(discovered_notes)),
spent_notes(std::move(spent_notes)) {}
spent_notes(std::move(spent_notes)),
commitments(std::move(commitments)) {}

OrchardBlockScanner::Result::Result(const Result&) = default;

Expand All @@ -33,7 +35,9 @@ OrchardBlockScanner::ScanBlocks(
std::vector<zcash::mojom::CompactBlockPtr> blocks) {
std::vector<OrchardNullifier> found_nullifiers;
std::vector<OrchardNote> found_notes;
std::vector<OrchardCommitment> commitments;

uint32_t cmu_index = 0;
for (const auto& block : blocks) {
// Scan block using the decoder initialized with the provided fvk
// to find new spendable notes.
Expand All @@ -54,6 +58,16 @@ OrchardBlockScanner::ScanBlocks(
return base::unexpected(ErrorCode::kInputError);
}

if (orchard_action->cmx.size() != kOrchardCmxSize) {
return base::unexpected(ErrorCode::kInputError);
}

OrchardCommitment commitment;
base::ranges::copy(orchard_action->cmx.begin(),
orchard_action->cmx.end(), commitment.cmu.begin());
commitment.block_id = block->height;
commitment.index = cmu_index++;

std::array<uint8_t, kOrchardNullifierSize> action_nullifier;
base::ranges::copy(orchard_action->nullifier, action_nullifier.begin());

Expand All @@ -72,7 +86,8 @@ OrchardBlockScanner::ScanBlocks(
}
}
}
return Result({std::move(found_notes), std::move(found_nullifiers)});
return Result({std::move(found_notes), std::move(found_nullifiers),
std::move(commitments)});
}

} // namespace brave_wallet
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class OrchardBlockScanner {
struct Result {
Result();
Result(std::vector<OrchardNote> discovered_notes,
std::vector<OrchardNullifier> spent_notes);
std::vector<OrchardNullifier> spent_notes,
std::vector<OrchardCommitment> commitments);
Result(const Result&);
Result& operator=(const Result&);
~Result();
Expand All @@ -35,6 +36,8 @@ class OrchardBlockScanner {
std::vector<OrchardNote> discovered_notes;
// Nullifiers for the previously discovered notes
std::vector<OrchardNullifier> spent_notes;

std::vector<OrchardCommitment> commitments;
};

explicit OrchardBlockScanner(const OrchardFullViewKey& full_view_key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */

#include "brave/components/brave_wallet/browser/internal/orchard_shard_tree_manager.h"

#include "brave/components/brave_wallet/common/zcash_utils.h"

namespace brave_wallet {

// static
std::unique_ptr<OrchardShardTreeManager> OrchardShardTreeManager::Create(
std::unique_ptr<OrchardShardTreeDelegate> delegate) {
auto shard_tree = orchard::OrchardShardTree::Create(std::move(delegate));
if (!shard_tree) {
// NOTREACHED
return nullptr;
}
return std::make_unique<OrchardShardTreeManager>(std::move(shard_tree));
}

OrchardShardTreeManager::OrchardShardTreeManager(
std::unique_ptr<::brave_wallet::orchard::OrchardShardTree> shard_tree) {
orchard_shard_tree_ = std::move(shard_tree);
}

OrchardShardTreeManager::~OrchardShardTreeManager() {}

bool OrchardShardTreeManager::InsertCommitments(
std::vector<OrchardCommitment> commitments) {
return orchard_shard_tree_->InsertCommitments(std::move(commitments));
}

bool OrchardShardTreeManager::InsertSubtreeRoots(
std::vector<OrchardShard> roots) {
return orchard_shard_tree_->InsertSubtreeRoots(std::move(roots));
}

} // namespace brave_wallet
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */

#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_INTERNAL_ORCHARD_SHARD_TREE_MANAGER_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_INTERNAL_ORCHARD_SHARD_TREE_MANAGER_H_

#include "base/types/expected.h"
#include "brave/components/brave_wallet/browser/zcash/rust/orchard_block_decoder.h"
#include "brave/components/brave_wallet/browser/zcash/rust/orchard_shard_tree.h"
#include "brave/components/brave_wallet/common/zcash_utils.h"

namespace brave_wallet {

class OrchardShardTreeManager {
public:
OrchardShardTreeManager(
std::unique_ptr<::brave_wallet::orchard::OrchardShardTree> shard_tree);
~OrchardShardTreeManager();
bool InsertCommitments(std::vector<OrchardCommitment> commitments);
bool InsertSubtreeRoots(std::vector<OrchardShard> roots);

static std::unique_ptr<OrchardShardTreeManager> Create(
std::unique_ptr<OrchardShardTreeDelegate> delegate);

private:
std::unique_ptr<::brave_wallet::orchard::OrchardShardTree>
orchard_shard_tree_;
};

} // namespace brave_wallet

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_INTERNAL_ORCHARD_SHARD_TREE_MANAGER_H_
17 changes: 17 additions & 0 deletions components/brave_wallet/browser/zcash/rust/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ source_set("orchard_headers") {
"authorized_orchard_bundle.h",
"extended_spending_key.h",
"orchard_block_decoder.h",
"orchard_shard_tree.h",
"unauthorized_orchard_bundle.h",
]

Expand All @@ -44,6 +45,8 @@ source_set("orchard_impl") {
"extended_spending_key_impl.h",
"orchard_block_decoder_impl.cc",
"orchard_block_decoder_impl.h",
"orchard_shard_tree_impl.cc",
"orchard_shard_tree_impl.h",
"unauthorized_orchard_bundle_impl.cc",
"unauthorized_orchard_bundle_impl.h",
]
Expand All @@ -57,6 +60,16 @@ source_set("orchard_impl") {
]
}

source_set("shard_store") {
visibility = [ ":*" ]
sources = [ "cxx/src/shard_store.h" ]

public_deps = [
"//base",
"//build/rust:cxx_cppdeps",
]
}

rust_static_library("rust_lib") {
visibility = [ ":orchard_impl" ]

Expand All @@ -69,13 +82,17 @@ rust_static_library("rust_lib") {
deps = [
"librustzcash:zcash_client_backend",
"librustzcash:zcash_primitives",
"librustzcash:zcash_protocol",
"//brave/components/brave_wallet/rust:rust_lib",
"//brave/third_party/rust/byteorder/v1:lib",
"//brave/third_party/rust/incrementalmerkletree/v0_5:lib",
"//brave/third_party/rust/memuse/v0_2:lib",
"//brave/third_party/rust/nonempty/v0_7:lib",
"//brave/third_party/rust/orchard/v0_8:lib",
"//brave/third_party/rust/rand/v0_8:lib",
"//brave/third_party/rust/shardtree/v0_3:lib",
"//brave/third_party/rust/zcash_note_encryption/v0_4:lib",
]

public_deps = [ ":shard_store" ]
}
70 changes: 70 additions & 0 deletions components/brave_wallet/browser/zcash/rust/cxx/src/shard_store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2024 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_RUST_CXX_SRC_SHARD_STORE_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_RUST_CXX_SRC_SHARD_STORE_H_

#include "brave/components/brave_wallet/common/zcash_utils.h"
#include "third_party/rust/cxx/v1/cxx.h"

namespace brave_wallet::orchard {

enum class ShardStoreStatusCode : uint32_t;
struct FfiShardTree;
struct FfiShardAddress;
struct FfiCheckpoint;
struct FfiCap;

using ShardStoreContext = ::brave_wallet::OrchardShardTreeDelegate;

ShardStoreStatusCode orchard_last_shard(const ShardStoreContext& ctx,
FfiShardTree& into);
ShardStoreStatusCode orchard_put_shard(ShardStoreContext& ctx,
const FfiShardTree& tree);
ShardStoreStatusCode orchard_get_shard(const ShardStoreContext& ctx,
const FfiShardAddress& addr,
FfiShardTree& tree);
ShardStoreStatusCode orchard_get_shard_roots(
const ShardStoreContext& ctx,
::rust::Vec<FfiShardAddress>& into);
ShardStoreStatusCode orchard_truncate(ShardStoreContext& ctx,
const FfiShardAddress& address);
ShardStoreStatusCode orchard_get_cap(const ShardStoreContext& ctx,
FfiCap& into);
ShardStoreStatusCode orchard_put_cap(ShardStoreContext& ctx,
const FfiCap& tree);
ShardStoreStatusCode orchard_min_checkpoint_id(const ShardStoreContext& ctx, uint32_t& into);
ShardStoreStatusCode orchard_max_checkpoint_id(const ShardStoreContext& ctx, uint32_t& into);
ShardStoreStatusCode orchard_add_checkpoint(ShardStoreContext& ctx,
uint32_t checkpoint_id,
const FfiCheckpoint& checkpoint);
ShardStoreStatusCode orchard_checkpoint_count(const ShardStoreContext& ctx, size_t& into);
ShardStoreStatusCode orchard_get_checkpoint_at_depth(
const ShardStoreContext& ctx,
size_t depth,
uint32_t& into_checkpoint_id,
FfiCheckpoint& into_checpoint);
ShardStoreStatusCode orchard_get_checkpoint(const ShardStoreContext& ctx,
uint32_t checkpoint_id,
FfiCheckpoint& into);
ShardStoreStatusCode orchard_update_checkpoint(
ShardStoreContext& ctx,
uint32_t checkpoint_id,
const FfiCheckpoint& checkpoint);
ShardStoreStatusCode orchard_remove_checkpoint(ShardStoreContext& ctx,
uint32_t checkpoint_id);
ShardStoreStatusCode orchard_truncate_checkpoint(ShardStoreContext& ctx,
uint32_t checkpoint_id);
ShardStoreStatusCode orchard_with_checkpoints(const ShardStoreContext& ctx,
size_t limit,
rust::cxxbridge1::Fn<ShardStoreStatusCode(uint32_t checkpoint_id,
const FfiCheckpoint& checkpoint)> fn);
ShardStoreStatusCode orchard_get_checkpoints(const ShardStoreContext& ctx,
size_t limit,
::rust::Vec<FfiCheckpoint>& into);

} // namespace brave_wallet::orchard

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_RUST_CXX_SRC_SHARD_STORE_H_
Loading

0 comments on commit bbaaa84

Please sign in to comment.