Skip to content

Commit

Permalink
Merge pull request #9067 from brave/wallet-bip39-validation
Browse files Browse the repository at this point in the history
Add bip39 validation when restoring from mnemonic
  • Loading branch information
darkdh authored Jun 10, 2021
2 parents eaa523b + 6ed2296 commit bda6571
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ std::string GenerateMnemonicForTest(const std::vector<uint8_t>& entropy) {
std::unique_ptr<std::vector<uint8_t>> MnemonicToSeed(
const std::string& mnemonic,
const std::string& passphrase) {
if (bip39_mnemonic_validate(nullptr, mnemonic.c_str()) != WALLY_OK) {
LOG(ERROR) << __func__ << ": Invalid mnemonic: " << mnemonic;
return nullptr;
}
std::unique_ptr<std::vector<uint8_t>> seed =
std::make_unique<std::vector<uint8_t>>(64);
const std::string salt = "mnemonic" + passphrase;
Expand Down
17 changes: 17 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ TEST(BraveWalletUtilsUnitTest, Mnemonic) {
}
}

TEST(BraveWalletUtilsUnitTest, MnemonicToSeed) {
EXPECT_NE(MnemonicToSeed("kingdom possible coast island six arrow fluid "
"spell chunk loud glue street",
""),
nullptr);
EXPECT_EQ(MnemonicToSeed("lingdom possible coast island six arrow fluid "
"spell chunk loud glue street",
""),
nullptr);
EXPECT_EQ(
MnemonicToSeed(
"kingdom possible coast island six arrow fluid spell chunk loud glue",
""),
nullptr);
EXPECT_EQ(MnemonicToSeed("", ""), nullptr);
}

TEST(BraveWalletUtilsUnitTest, EncodeString) {
std::string output;
EXPECT_TRUE(EncodeString("one", &output));
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/keyring_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ bool KeyringController::CreateDefaultKeyringInternal(

const std::unique_ptr<std::vector<uint8_t>> seed =
MnemonicToSeed(mnemonic, "");
if (!seed)
return false;
default_keyring_ = std::make_unique<HDKeyring>();
default_keyring_->ConstructRootHDKey(*seed, "m/44'/60'/0'/0");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ TEST_F(KeyringControllerUnitTest, RestoreDefaultKeyring) {
"0xf81229FE54D8a20fBc1e1e2a3451D1c7489437Db");

EXPECT_EQ(controller.RestoreDefaultKeyring(seed_phrase, ""), nullptr);

EXPECT_EQ(controller.RestoreDefaultKeyring("", "brave"), nullptr);
}

TEST_F(KeyringControllerUnitTest, UnlockResumesDefaultKeyring) {
Expand Down

0 comments on commit bda6571

Please sign in to comment.