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

Some wallet refactorings #27325

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/brave_wallet/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static_library("browser") {
"asset_ratio_response_parser.h",
"asset_ratio_service.cc",
"asset_ratio_service.h",
"bip39.cc",
"bip39.h",
"bitcoin/bitcoin_block_tracker.cc",
"bitcoin/bitcoin_block_tracker.h",
"bitcoin/bitcoin_discover_account_task.cc",
Expand Down Expand Up @@ -293,7 +295,6 @@ static_library("browser") {
"//brave/components/services/brave_wallet/public/mojom",
"//brave/components/services/brave_wallet/public/proto:zcash_proto",
"//brave/net/base:utils",
"//brave/third_party/argon2",
"//components/component_updater",
"//components/content_settings/core/browser",
"//components/country_codes",
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include_rules = [
]

specific_include_rules = {
"brave_wallet_utils\.cc": [
"bip39\.cc": [
"+brave/third_party/bip39wally-core-native/include",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "brave/components/brave_wallet/browser/bip39.h"
#include "brave/components/brave_wallet/browser/bitcoin/bitcoin_hd_keyring.h"
#include "brave/components/brave_wallet/browser/bitcoin/bitcoin_test_utils.h"
#include "brave/components/brave_wallet/browser/bitcoin/bitcoin_wallet_service.h"
#include "brave/components/brave_wallet/browser/brave_wallet_prefs.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/network_manager.h"
#include "brave/components/brave_wallet/browser/test_utils.h"
Expand Down Expand Up @@ -60,7 +60,7 @@ class AccountDiscoveryManagerUnitTest : public testing::Test {
bitcoin_test_rpc_server_->SetUpBitcoinRpc(std::nullopt, std::nullopt);

keyring_ = std::make_unique<BitcoinHDKeyring>(
*MnemonicToSeed(kMnemonicDivideCruise), false);
*bip39::MnemonicToSeed(kMnemonicDivideCruise), false);
}

AccountUtils GetAccountUtils() {
Expand Down
98 changes: 98 additions & 0 deletions components/brave_wallet/browser/bip39.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Copyright (c) 2025 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/bip39.h"

#include "base/containers/span.h"
#include "base/strings/strcat.h"
#include "brave/third_party/bip39wally-core-native/include/wally_bip39.h"
#include "third_party/boringssl/src/include/openssl/digest.h"
#include "third_party/boringssl/src/include/openssl/evp.h"

namespace brave_wallet::bip39 {

namespace {

// https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#generating-the-mnemonic
inline constexpr uint32_t kMaxSupportedEntropySize = 32;

// https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#from-mnemonic-to-seed
inline constexpr uint32_t kPBKDF2Iterations = 2048;
inline constexpr uint32_t kSeedSize = 64;

std::optional<std::string> GenerateMnemonicInternal(
base::span<const uint8_t> entropy) {
char* words = nullptr;
if (bip39_mnemonic_from_bytes(nullptr, entropy.data(), entropy.size(),
&words) != WALLY_OK) {
return std::nullopt;
}
std::string result(words);
wally_free_string(words);
return result;
}

bool IsValidEntropySize(size_t entropy_size) {
// entropy size should be 128, 160, 192, 224, 256 bits
if (entropy_size < 16 || entropy_size > 32 || entropy_size % 4 != 0) {
return false;
}
return true;
}

} // namespace

std::optional<std::string> GenerateMnemonic(base::span<const uint8_t> entropy) {
if (!IsValidEntropySize(entropy.size())) {
return std::nullopt;
}
return GenerateMnemonicInternal(entropy);
}

std::optional<std::vector<uint8_t>> MnemonicToSeed(
std::string_view mnemonic,
std::string_view passphrase) {
if (!IsValidMnemonic(mnemonic)) {
return std::nullopt;
}

std::vector<uint8_t> seed(kSeedSize, 0);
const std::string salt = base::StrCat({"mnemonic", passphrase});
if (PKCS5_PBKDF2_HMAC(mnemonic.data(), mnemonic.length(),
base::as_byte_span(salt).data(), salt.length(),
kPBKDF2Iterations, EVP_sha512(), seed.size(),
seed.data())) {
return seed;
}

return std::nullopt;
}

std::optional<std::vector<uint8_t>> MnemonicToEntropy(
std::string_view mnemonic) {
if (!IsValidMnemonic(mnemonic)) {
return std::nullopt;
}

std::vector<uint8_t> entropy(kMaxSupportedEntropySize, 0);
size_t written = 0;
if (bip39_mnemonic_to_bytes(nullptr, std::string(mnemonic).c_str(),
entropy.data(), entropy.size(),
&written) != WALLY_OK) {
return std::nullopt;
}
entropy.resize(written);
return entropy;
}

bool IsValidMnemonic(std::string_view mnemonic) {
if (bip39_mnemonic_validate(nullptr, std::string(mnemonic).c_str()) !=
WALLY_OK) {
return false;
}
return true;
}

} // namespace brave_wallet::bip39
41 changes: 41 additions & 0 deletions components/brave_wallet/browser/bip39.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright (c) 2025 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_BIP39_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BIP39_H_

#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/containers/span.h"

// Utility functions for BIP39 mnemonics support.
// https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
namespace brave_wallet::bip39 {

inline constexpr uint32_t kDefaultEntropySize = 16;
inline constexpr uint32_t kLegacyEthEntropySize = 32;

// Generate mnemonic from entropy bytes following BIP39.
// If |entropy.size()| is not in 16, 20, 24, 28, 32 range or
// allocation failure, the std::nullopt will be returned.
std::optional<std::string> GenerateMnemonic(base::span<const uint8_t> entropy);

// Generate 64 bytes seed from mnemonic following BIP39.
std::optional<std::vector<uint8_t>> MnemonicToSeed(
std::string_view mnemonic,
std::string_view passphrase = "");

// This is mainly used for restoring legacy brave crypto wallet
std::optional<std::vector<uint8_t>> MnemonicToEntropy(
std::string_view mnemonic);

bool IsValidMnemonic(std::string_view mnemonic);

} // namespace brave_wallet::bip39

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BIP39_H_
Loading
Loading