-
Notifications
You must be signed in to change notification settings - Fork 893
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
[ZCash] Fetch consensus branch id from the backend #27089
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
#include <utility> | ||
|
||
#include "base/containers/span.h" | ||
#include "base/containers/to_vector.h" | ||
#include "base/numerics/byte_conversions.h" | ||
#include "brave/components/brave_wallet/common/btc_like_serializer_stream.h" | ||
#include "brave/components/brave_wallet/common/hex_utils.h" | ||
|
@@ -25,10 +26,7 @@ constexpr char kSaplingHashPersonalizer[] = "ZTxIdSaplingHash"; | |
constexpr char kOrchardHashPersonalizer[] = "ZTxIdOrchardHash"; | ||
|
||
// https://zips.z.cash/zip-0244#txid-digest-1 | ||
constexpr uint32_t kConsensusBranchId = 0xC2D6D0B4; | ||
constexpr char kTxHashPersonalizer[] = | ||
"ZcashTxHash_" | ||
"\xB4\xD0\xD6\xC2"; | ||
constexpr char kTxHashPersonalizerPrefix[] = "ZcashTxHash_"; | ||
|
||
constexpr uint32_t kV5TxVersion = 5 | 1 << 31 /* overwintered bit */; | ||
// https://zips.z.cash/protocol/protocol.pdf#txnconsensus | ||
|
@@ -56,7 +54,7 @@ std::array<uint8_t, kZCashDigestSize> blake2b256( | |
void PushHeader(const ZCashTransaction& tx, BtcLikeSerializerStream& stream) { | ||
stream.Push32(kV5TxVersion); | ||
stream.Push32(kV5VersionGroupId); | ||
stream.Push32(kConsensusBranchId); | ||
stream.Push32(tx.consensus_brach_id()); | ||
stream.Push32(tx.locktime()); | ||
stream.Push32(tx.expiry_height()); | ||
} | ||
|
@@ -93,6 +91,20 @@ std::array<uint8_t, 32> HashScriptPubKeys(const ZCashTransaction& tx) { | |
return blake2b256(data, base::byte_span_from_cstring("ZTxTrScriptsHash")); | ||
} | ||
|
||
std::array<uint8_t, kBlake2bPersonalizationSize> GetHashPersonalizer( | ||
const ZCashTransaction& tx) { | ||
std::array<uint8_t, kBlake2bPersonalizationSize> result; | ||
uint32_t consensusBranchId = tx.consensus_brach_id(); | ||
base::span(result) | ||
.subspan(0, sizeof(kTxHashPersonalizerPrefix) - 1) | ||
.copy_from(base::byte_span_from_cstring(kTxHashPersonalizerPrefix)); | ||
base::span(result) | ||
.subspan(sizeof(kTxHashPersonalizerPrefix) - 1, sizeof(consensusBranchId)) | ||
.copy_from(base::byte_span_from_ref(base::numerics::U32FromLittleEndian( | ||
base::byte_span_from_ref(consensusBranchId)))); | ||
return result; | ||
} | ||
|
||
} // namespace | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That should be done with SpanWriter
|
||
// static | ||
|
@@ -260,8 +272,7 @@ std::array<uint8_t, kZCashDigestSize> ZCashSerializer::CalculateTxIdDigest( | |
stream.PushBytes(sapling_hash); | ||
stream.PushBytes(orchard_hash); | ||
|
||
digest_hash = | ||
blake2b256(data, base::byte_span_from_cstring(kTxHashPersonalizer)); | ||
digest_hash = blake2b256(data, GetHashPersonalizer(zcash_transaction)); | ||
} | ||
|
||
std::reverse(digest_hash.begin(), digest_hash.end()); | ||
|
@@ -315,8 +326,7 @@ std::array<uint8_t, kZCashDigestSize> ZCashSerializer::CalculateSignatureDigest( | |
stream.PushBytes(sapling_hash); | ||
stream.PushBytes(orchard_hash); | ||
|
||
digest_hash = | ||
blake2b256(data, base::byte_span_from_cstring(kTxHashPersonalizer)); | ||
digest_hash = blake2b256(data, GetHashPersonalizer(zcash_transaction)); | ||
} | ||
|
||
return digest_hash; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,9 +54,9 @@ void ZCashTransactionCompleteManager::CompleteTransaction( | |
const ZCashTransaction& transaction, | ||
const mojom::AccountIdPtr& account_id, | ||
CompleteTransactionCallback callback) { | ||
zcash_wallet_service_->zcash_rpc().GetLatestBlock( | ||
zcash_wallet_service_->zcash_rpc().GetLightdInfo( | ||
chain_id, | ||
base::BindOnce(&ZCashTransactionCompleteManager::OnGetLatestBlockHeight, | ||
base::BindOnce(&ZCashTransactionCompleteManager::OnGetLightdInfo, | ||
weak_ptr_factory_.GetWeakPtr(), | ||
Comment on lines
+57
to
60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How frequently branch id changes? Does it make sense to cache it per browser instance, or persist it per network? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Several times a year i guess, we have to check whether it has been changed anyway |
||
ParamsBundle{chain_id, transaction, account_id.Clone(), | ||
std::move(callback)})); | ||
|
@@ -155,6 +155,31 @@ void ZCashTransactionCompleteManager::OnSignOrchardPartComplete( | |
|
||
#endif // BUILDFLAG(ENABLE_ORCHARD) | ||
|
||
void ZCashTransactionCompleteManager::OnGetLightdInfo( | ||
ParamsBundle params, | ||
base::expected<zcash::mojom::LightdInfoPtr, std::string> result) { | ||
if (!result.has_value()) { | ||
std::move(params.callback).Run(base::unexpected("get lightd info error")); | ||
return; | ||
} | ||
|
||
uint32_t consensus_branch_id; | ||
if (!base::HexStringToUInt(result.value()->consensusBranchId, | ||
&consensus_branch_id)) { | ||
std::move(params.callback) | ||
.Run(base::unexpected("wrong consensus branch format")); | ||
return; | ||
} | ||
|
||
params.transaction.set_consensus_brach_id(consensus_branch_id); | ||
std::string chain_id = params.chain_id; | ||
|
||
zcash_wallet_service_->zcash_rpc().GetLatestBlock( | ||
chain_id, | ||
base::BindOnce(&ZCashTransactionCompleteManager::OnGetLatestBlockHeight, | ||
weak_ptr_factory_.GetWeakPtr(), std::move(params))); | ||
} | ||
|
||
void ZCashTransactionCompleteManager::SignTransparentPart(ParamsBundle params) { | ||
// Sign transparent part | ||
if (!ZCashSerializer::SignTransparentPart( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,7 +334,6 @@ void ZCashWalletService::CompleteTransactionDone( | |
result.error()); | ||
return; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need this change? |
||
auto tx = ZCashSerializer::SerializeRawTransaction(result.value()); | ||
zcash_rpc_->SendTransaction( | ||
chain_id, tx, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include "base/containers/to_vector.h"