Skip to content

Commit

Permalink
Merge pull request #5552 from nuttycom/feature/wallet_orchard-add_orc…
Browse files Browse the repository at this point in the history
…hard

Add Rust backend for the Orchard components of the zcashd wallet.
  • Loading branch information
nuttycom committed Feb 19, 2022
2 parents f6a3c68 + 1969add commit 98ab4bc
Show file tree
Hide file tree
Showing 24 changed files with 1,249 additions and 132 deletions.
7 changes: 6 additions & 1 deletion .cargo/config.offline
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ replace-with = "vendored-sources"

[source."https://github.com/zcash/orchard.git"]
git = "https://github.com/zcash/orchard.git"
rev = "c4cd541e6c9ca657fa33fdd686d70d81bf3e4e65"
rev = "4dc1ae059a59ee911134cb3e731c7be627a71d4d"
replace-with = "vendored-sources"

[source."https://github.com/nuttycom/hdwallet.git"]
git = "https://github.com/nuttycom/hdwallet.git"
rev = "576683b9f2865f1118c309017ff36e01f84420c9"
replace-with = "vendored-sources"

[source.vendored-sources]
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ codegen-units = 1

[patch.crates-io]
hdwallet = { git = "https://github.com/nuttycom/hdwallet", rev = "576683b9f2865f1118c309017ff36e01f84420c9" }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "c4cd541e6c9ca657fa33fdd686d70d81bf3e4e65" }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "4dc1ae059a59ee911134cb3e731c7be627a71d4d" }
zcash_address = { git = "https://github.com/zcash/librustzcash.git", rev = "3d935a94e75786a67c3ea4992d7c372af203086f" }
zcash_history = { git = "https://github.com/zcash/librustzcash.git", rev = "3d935a94e75786a67c3ea4992d7c372af203086f" }
zcash_note_encryption = { git = "https://github.com/zcash/librustzcash.git", rev = "3d935a94e75786a67c3ea4992d7c372af203086f" }
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.gtest.include
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ zcash_gtest_SOURCES += \
gtest/test_zip32.cpp
if ENABLE_WALLET
zcash_gtest_SOURCES += \
wallet/gtest/test_orchard_wallet.cpp \
wallet/gtest/test_paymentdisclosure.cpp \
wallet/gtest/test_wallet.cpp
endif
Expand Down
64 changes: 1 addition & 63 deletions src/gtest/test_transaction_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "pubkey.h"
#include "rpc/protocol.h"
#include "transaction_builder.h"
#include "gtest/test_transaction_builder.h"
#include "utiltest.h"
#include "zcash/Address.hpp"
#include "zcash/address/mnemonic.h"
Expand All @@ -16,69 +17,6 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

// Fake an empty view
class TransactionBuilderCoinsViewDB : public CCoinsView {
public:
std::map<uint256, SproutMerkleTree> sproutTrees;

TransactionBuilderCoinsViewDB() {}

bool GetSproutAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const {
auto it = sproutTrees.find(rt);
if (it != sproutTrees.end()) {
tree = it->second;
return true;
} else {
return false;
}
}

bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const {
return false;
}

bool GetOrchardAnchorAt(const uint256 &rt, OrchardMerkleFrontier &tree) const {
return false;
}

bool GetNullifier(const uint256 &nf, ShieldedType type) const {
return false;
}

bool GetCoins(const uint256 &txid, CCoins &coins) const {
return false;
}

bool HaveCoins(const uint256 &txid) const {
return false;
}

uint256 GetBestBlock() const {
uint256 a;
return a;
}

uint256 GetBestAnchor(ShieldedType type) const {
uint256 a;
return a;
}

bool BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashBlock,
const uint256 &hashSproutAnchor,
const uint256 &hashSaplingAnchor,
CAnchorsSproutMap &mapSproutAnchors,
CAnchorsSaplingMap &mapSaplingAnchors,
CNullifiersMap &mapSproutNullifiers,
CNullifiersMap saplingNullifiersMap) {
return false;
}

bool GetStats(CCoinsStats &stats) const {
return false;
}
};

TEST(TransactionBuilder, TransparentToSapling)
{
auto consensusParams = RegtestActivateSapling();
Expand Down
73 changes: 73 additions & 0 deletions src/gtest/test_transaction_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2022 The Zcash developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .

#ifndef ZCASH_GTEST_TEST_TRANSACTION_BUILDER_H
#define ZCASH_GTEST_TEST_TRANSACTION_BUILDER_H

#include "coins.h"

// Fake an empty view
class TransactionBuilderCoinsViewDB : public CCoinsView {
public:
std::map<uint256, SproutMerkleTree> sproutTrees;

TransactionBuilderCoinsViewDB() {}

bool GetSproutAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const {
auto it = sproutTrees.find(rt);
if (it != sproutTrees.end()) {
tree = it->second;
return true;
} else {
return false;
}
}

bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const {
return false;
}

bool GetOrchardAnchorAt(const uint256 &rt, OrchardMerkleFrontier &tree) const {
return false;
}

bool GetNullifier(const uint256 &nf, ShieldedType type) const {
return false;
}

bool GetCoins(const uint256 &txid, CCoins &coins) const {
return false;
}

bool HaveCoins(const uint256 &txid) const {
return false;
}

uint256 GetBestBlock() const {
uint256 a;
return a;
}

uint256 GetBestAnchor(ShieldedType type) const {
uint256 a;
return a;
}

bool BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashBlock,
const uint256 &hashSproutAnchor,
const uint256 &hashSaplingAnchor,
CAnchorsSproutMap &mapSproutAnchors,
CAnchorsSaplingMap &mapSaplingAnchors,
CNullifiersMap &mapSproutNullifiers,
CNullifiersMap saplingNullifiersMap) {
return false;
}

bool GetStats(CCoinsStats &stats) const {
return false;
}
};

#endif
4 changes: 4 additions & 0 deletions src/primitives/orchard.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

#include <amount.h>
#include <rust/orchard.h>
#include <rust/orchard/wallet.h>
#include "zcash/address/orchard.hpp"

class OrchardMerkleFrontier;
class OrchardWallet;
namespace orchard { class UnauthorizedBundle; }

/**
Expand All @@ -26,6 +29,7 @@ class OrchardBundle
OrchardBundle(OrchardBundlePtr* bundle) : inner(bundle, orchard_bundle_free) {}

friend class OrchardMerkleFrontier;
friend class OrchardWallet;
friend class orchard::UnauthorizedBundle;
public:
OrchardBundle() : inner(nullptr, orchard_bundle_free) {}
Expand Down
10 changes: 10 additions & 0 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,16 @@ class SaplingOutPoint : public BaseOutPoint
std::string ToString() const;
};

/** An outpoint - a combination of a txid and an index n into its orchard
* actions */
class OrchardOutPoint : public BaseOutPoint
{
public:
OrchardOutPoint() : BaseOutPoint() {};
OrchardOutPoint(uint256 hashIn, uint32_t nIn) : BaseOutPoint(hashIn, nIn) {};
std::string ToString() const;
};

/** An input of a transaction. It contains the location of the previous
* transaction's output that it claims and a signature that matches the
* output's public key.
Expand Down
2 changes: 1 addition & 1 deletion src/rust/include/rust/orchard/incremental_merkle_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void orchard_merkle_frontier_root(
// The total number of leaves that have been appended to obtain
// the current state of the frontier. Subtract 1 from this value
// to obtain the position of the most recently appended leaf.
size_t orchard_merkle_frontier_num_leaves(
uint64_t orchard_merkle_frontier_num_leaves(
const OrchardMerkleFrontierPtr* tree_ptr);

// Estimate the amount of memory consumed by the merkle frontier.
Expand Down
22 changes: 22 additions & 0 deletions src/rust/include/rust/orchard/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ OrchardRawAddressPtr* orchard_address_clone(
*/
void orchard_address_free(OrchardRawAddressPtr* ptr);

/**
* Implements the "less than" operation `k0 < k1` for comparing two Orchard
* addresses. This is a comparison of the raw bytes, only useful for cases
* where a semantically irrelevant ordering is needed (such as for map keys).
*/
bool orchard_address_lt(
const OrchardRawAddressPtr* k0,
const OrchardRawAddressPtr* k1);

//
// Incoming Viewing Keys
//
Expand Down Expand Up @@ -69,8 +78,12 @@ OrchardIncomingViewingKeyPtr* orchard_incoming_viewing_key_parse(
void* stream,
read_callback_t read_cb);


/**
* Serializes an Orchard incoming viewing key to the given stream.
*
* This will return `false` and leave the stream unmodified if
* `incoming_viewing_key == nullptr`.
*/
bool orchard_incoming_viewing_key_serialize(
const OrchardIncomingViewingKeyPtr* incoming_viewing_key,
Expand Down Expand Up @@ -123,6 +136,9 @@ OrchardFullViewingKeyPtr* orchard_full_viewing_key_parse(

/**
* Serializes an Orchard full viewing key to the given stream.
*
* This will return `false` and leave the stream unmodified if
* `full_viewing_key == nullptr`.
*/
bool orchard_full_viewing_key_serialize(
const OrchardFullViewingKeyPtr* full_viewing_key,
Expand Down Expand Up @@ -184,6 +200,9 @@ OrchardSpendingKeyPtr* orchard_spending_key_parse(

/**
* Serializes an Orchard spending key to the given stream.
*
* This will return `false` and leave the stream unmodified if
* `spending_key == nullptr`.
*/
bool orchard_spending_key_serialize(
const OrchardSpendingKeyPtr* spending_key,
Expand All @@ -192,6 +211,9 @@ bool orchard_spending_key_serialize(

/**
* Returns the full viewing key for the specified spending key.
*
* The resulting pointer must be ultimately freed by the caller
* using `orchard_full_viewing_key_free`.
*/
OrchardFullViewingKeyPtr* orchard_spending_key_to_full_viewing_key(
const OrchardSpendingKeyPtr* key);
Expand Down
Loading

0 comments on commit 98ab4bc

Please sign in to comment.