Skip to content

Commit

Permalink
Extract transparent part using pubkey hash instead of pubkey in UA (b…
Browse files Browse the repository at this point in the history
…rave#21960)

* Extract transparent part using pubkey hash instead of pubkey in UA
Resolves brave/brave-browser#35883
  • Loading branch information
cypt4 authored Feb 9, 2024
1 parent 7d57398 commit 3171408
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
22 changes: 19 additions & 3 deletions components/brave_wallet/common/zcash_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ std::optional<std::vector<ParsedAddress>> ParseUnifiedAddress(
return result;
}

std::vector<uint8_t> GetNetworkPrefix(bool is_testnet) {
return is_testnet ? std::vector<uint8_t>({0x1d, 0x25})
: std::vector<uint8_t>({0x1c, 0xb8});
}

} // namespace

DecodedZCashAddress::DecodedZCashAddress() = default;
Expand All @@ -108,14 +113,25 @@ bool IsUnifiedAddress(const std::string& address) {

std::string PubkeyToTransparentAddress(base::span<const uint8_t> pubkey,
bool testnet) {
std::vector<uint8_t> result = testnet ? std::vector<uint8_t>({0x1d, 0x25})
: std::vector<uint8_t>({0x1c, 0xb8});
std::vector<uint8_t> result = GetNetworkPrefix(testnet);

std::vector<uint8_t> data_part = Hash160(pubkey);
result.insert(result.end(), data_part.begin(), data_part.end());
return Base58EncodeWithCheck(result);
}

std::optional<std::string> PubkeyHashToTransparentAddress(
base::span<const uint8_t> pubkey_hash,
bool testnet) {
// Hash160 output size is 20 bytes
if (pubkey_hash.size() != 20u) {
return std::nullopt;
}
std::vector<uint8_t> result = GetNetworkPrefix(testnet);
result.insert(result.end(), pubkey_hash.begin(), pubkey_hash.end());
return Base58EncodeWithCheck(result);
}

std::optional<DecodedZCashAddress> DecodeZCashAddress(
const std::string& address) {
std::vector<uint8_t> decode_result;
Expand Down Expand Up @@ -213,7 +229,7 @@ std::optional<std::string> ExtractTransparentPart(

for (const auto& part : parts.value()) {
if (part.first == AddrType::kP2PKH) {
return PubkeyToTransparentAddress(part.second, is_testnet);
return PubkeyHashToTransparentAddress(part.second, is_testnet);
}
}

Expand Down
4 changes: 4 additions & 0 deletions components/brave_wallet/common/zcash_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ bool IsUnifiedAddress(const std::string& address);
std::string PubkeyToTransparentAddress(base::span<const uint8_t> pubkey,
bool testnet);

std::optional<std::string> PubkeyHashToTransparentAddress(
base::span<const uint8_t> pubkey_hash,
bool testnet);

std::optional<DecodedZCashAddress> DecodeZCashAddress(
const std::string& address);

Expand Down
60 changes: 57 additions & 3 deletions components/brave_wallet/common/zcash_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,44 @@ TEST(ZCashUtilsUnitTest, PubkeyToTransparentAddress) {
false));
}

TEST(ZCashUtilsUnitTest, PubkeyHashToTransparentAddress) {
EXPECT_FALSE(
PubkeyHashToTransparentAddress(std::vector<uint8_t>(19, 0), false));
EXPECT_FALSE(
PubkeyHashToTransparentAddress(std::vector<uint8_t>(21, 0), false));
// https://github.com/zcash/librustzcash/blob/zcash_address-0.3.1/components/zcash_address/src/encoding.rs#L243
EXPECT_EQ("t1Hsc1LR8yKnbbe3twRp88p6vFfC5t7DLbs",
PubkeyHashToTransparentAddress(std::vector<uint8_t>(20, 0), false)
.value());
}

TEST(ZCashUtilsUnitTest, ExtractTransparentPart) {
// https://github.com/Electric-Coin-Company/zcash-android-wallet-sdk/blob/v2.0.6/sdk-incubator-lib/src/main/java/cash/z/ecc/android/sdk/fixture/WalletFixture.kt
{
auto transparent_part = ExtractTransparentPart(
"u1lmy8anuylj33arxh3sx7ysq54tuw7zehsv6pdeeaqlrhkjhm3uvl9egqxqfd7hcsp3ms"
"zp6jxxx0gsw0ldp5wyu95r4mfzlueh8h5xhrjqgz7xtxp3hvw45dn4gfrz5j54ryg6reyf"
"0",
false);
EXPECT_EQ("t1JP7PHu72xHztsZiwH6cye4yvC9Prb3EvQ", transparent_part);
}
{
auto transparent_part = ExtractTransparentPart(
"u1czzc8jcl50svfezmfc9xsxnh63p374nptqplt0yw2uekr7v9wprp84y6esys6derp6uv"
"dcq6x6ykjrkpdyhjzneq5ud78h6j68n63hewg7xp9fpneuh64wgzt3d7mh6zh3qpqapzlc"
"4",
false);
EXPECT_EQ("t1duiEGg7b39nfQee3XaTY4f5McqfyJKhBi", transparent_part);
}
{
auto transparent_part = ExtractTransparentPart(
"utest16zd8zfx6n6few7mjsjpn6qtn8tlg6law7qnq33257855mdqekk7vru8lettx3vud"
"4mh99elglddltmfjkduar69h7vy08h3xdq6zuls9pqq7quyuehjqwtthc3hfd8gshhw42d"
"fr96e",
true);
EXPECT_EQ("tmCxJG72RWN66xwPtNgu4iKHpyysGrc7rEg", transparent_part);
}

// https://github.com/zcash/librustzcash/blob/zcash_primitives-0.13.0/components/zcash_address/src/kind/unified/address/test_vectors.rs#L17
{
auto transparent_part = ExtractTransparentPart(
Expand All @@ -57,22 +94,39 @@ TEST(ZCashUtilsUnitTest, ExtractTransparentPart) {
"f",
false);
EXPECT_EQ(
PubkeyToTransparentAddress(
PubkeyHashToTransparentAddress(
std::vector<uint8_t>({0x7b, 0xb8, 0x35, 0x70, 0xb8, 0xfa, 0xe1,
0x46, 0xe0, 0x3c, 0x53, 0x31, 0xa0, 0x20,
0xb1, 0xe0, 0x89, 0x2f, 0x63, 0x1d}),
false),
transparent_part);
}

// https://github.com/zcash/librustzcash/blob/zcash_address-0.3.1/components/zcash_address/src/kind/unified/address/test_vectors.rs#L981
{
auto transparent_part = ExtractTransparentPart(
"u1ap7zakdnuefrgdglr334cw62hnqjkhr65t7tketyym0amkhdvyedpucuyxwu9z2te5vp"
"0jf75jgsm36d7r09h6z3qe5rkgd8y28er6fz8z5rckspevxnx4y9wfk49njpcujh5gle7m"
"fan90m9tt9a2gltyh8hx27cwt7h6u8ndmzhtk8qrq8hjytnakjqm0n658llh4z0277cyl2"
"rcu",
false);
EXPECT_EQ(
PubkeyHashToTransparentAddress(
std::vector<uint8_t>({0xf1, 0xbc, 0x3d, 0x72, 0x61, 0xbf, 0x77,
0xfe, 0x80, 0x8e, 0x2b, 0x71, 0x78, 0x98,
0x1c, 0x7c, 0xfe, 0x55, 0x70, 0xfd}),
false),
transparent_part);
}

// Multily transparent addresses
{
auto transparent_part = ExtractTransparentPart(
"u1gfg995k4rre49al7x6m2u0t6rfhyjaecw9duhsmn4f7mdqeavfc3crny504e69yers2e"
"7fzy8fwet0r0pt4wkdfs794ycqrvhe2hc97tkevpjr8rh3uenj3kz3rdqy78hmsmsx69dw"
"raxwgy42xuhjh249uckn2elfwwhg36t7f9ms",
false);
EXPECT_EQ(PubkeyToTransparentAddress(
EXPECT_EQ(PubkeyHashToTransparentAddress(
std::vector<uint8_t>({0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
false),
Expand All @@ -98,7 +152,7 @@ TEST(ZCashUtilsUnitTest, ExtractTransparentPart) {
"na7kx9nfn0637np9k6tagzss48l6u9kcjf6gadlcnfusm42klsmmxnwj80q40cfwe8dnj7"
"373w0",
true);
EXPECT_EQ(PubkeyToTransparentAddress(
EXPECT_EQ(PubkeyHashToTransparentAddress(
std::vector<uint8_t>({0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
true),
Expand Down

0 comments on commit 3171408

Please sign in to comment.