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

Add API to set nonce and add ability to reset nonce information #11721

Merged
merged 2 commits into from
Jan 3, 2022
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
14 changes: 13 additions & 1 deletion app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -995,14 +995,26 @@ Are you sure you want to do this?
'<ph name="KEY_NAME">$1<ex>MyCustomKey</ex></ph>' key export failed
</message>
<message name="IDS_SETTINGS_WALLET_RESET" desc="Reset wallet settings item text">
Reset Wallet
Reset Brave Wallet
</message>
<message name="IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO" desc="Reset wallet transactions">
Clear wallet transaction and nonce information
</message>
<message name="IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO_DESC" desc="Reset wallet transactions description">
Clearing transactions may be useful for developers or when clearing state on a local server
</message>
<message name="IDS_SETTINGS_WALLET_RESET_CONFIRMATION" desc="Reset wallet settings confirmation message text">
Are you sure you want to reset your wallet? If your wallet is not backed up, resetting will cause you to lose all account data (including any associated funds). Type "<ph name="CONFIRMATION_PHRASE">$1<ex>Yes</ex></ph>" to confirm.
</message>
<message name="IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO_CONFIRMATION" desc="Reset wallet settings confirmation for resetting transaction info message text">
Are you sure you want to reset all of your wallet transaction and nonce information? This option is mostly used by developers running a local test server. Type "<ph name="CONFIRMATION_PHRASE">$1<ex>Yes</ex></ph>" to confirm.
</message>
<message name="IDS_SETTINGS_WALLET_RESET_CONFIRMED" desc="Reset wallet settings confirmed message text">
Your wallet was reset.
</message>
<message name="IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO_CONFIRMED" desc="Reset wallet settings confirmed message text">
Your wallet transaction information was reset.
</message>
<message name="IDS_SETTINGS_WALLET_RESET_CONFIRMATION_PHRASE" desc="The phrase users should type to reset wallet">
Yes
</message>
Expand Down
5 changes: 4 additions & 1 deletion browser/brave_wallet/brave_wallet_reset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

namespace brave_wallet {

void ResetWallet(content::BrowserContext* context) {
void ResetTransactionInfo(content::BrowserContext* context) {
auto* eth_tx_controller =
brave_wallet::EthTxControllerFactory::GetControllerForContext(context);
eth_tx_controller->Reset();
}

void ResetWallet(content::BrowserContext* context) {
ResetTransactionInfo(context);
auto* rpc_controller =
brave_wallet::RpcControllerFactory::GetControllerForContext(context);
rpc_controller->Reset();
Expand Down
1 change: 1 addition & 0 deletions browser/brave_wallet/brave_wallet_reset.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BrowserContext;

namespace brave_wallet {

void ResetTransactionInfo(content::BrowserContext* context);
void ResetWallet(content::BrowserContext* context);

} // namespace brave_wallet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class BraveWalletBrowserProxy {
removeEthereumChain (chainId) {}
addEthereumChain (value) {}
setActiveNetwork (chainId) {}
resetTransactionInfo () {}
}

/**
Expand All @@ -29,6 +30,10 @@ export class BraveWalletBrowserProxyImpl {
chrome.send('resetWallet', [])
}
/** @override */
resetTransactionInfo() {
chrome.send('resetTransactionInfo', [])
}
/** @override */
setBraveWalletEnabled (value) {
chrome.send('setBraveWalletEnabled', [value])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
cursor: pointer;
color: red;
}
.reset-transaction-info {
cursor: pointer;
}
settings-dropdown-menu.wide-drop-down {
--md-select-width: 225px;
}
Expand Down Expand Up @@ -91,6 +94,12 @@
</template>
</settings-animated-pages>
<template is="dom-if" if="[[!isNetworkEditor_]]">
<div class="settings-box " on-click="onResetTransactionInfo_">
<div class="flex cr-padded-text reset-transaction-info">
<div>$i18n{resetTransactionInfo}</div>
<div class="secondary">$i18n{resetTransactionInfoDesc}</div>
</div>
</div>
<div class="settings-box " on-click="onResetWallet_">
<div class="flex cr-padded-text reset-wallet">
<div>$i18n{resetWallet}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ class SettingsBraveWalletPage extends SettingsBraveWalletPageBase {
this.browserProxy_.resetWallet()
window.alert(this.i18n('walletResetConfirmed'))
}

onResetTransactionInfo_() {
var message = this.i18n('walletResetTransactionInfoConfirmation')
if (window.prompt(message) !== this.i18n('walletResetConfirmationPhrase'))
return
this.browserProxy_.resetTransactionInfo()
window.alert(this.i18n('walletResetTransactionInfoConfirmed'))
}
}

customElements.define(
Expand Down
9 changes: 9 additions & 0 deletions browser/ui/webui/settings/brave_default_extensions_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ void BraveDefaultExtensionsHandler::RegisterMessages() {
"resetWallet",
base::BindRepeating(&BraveDefaultExtensionsHandler::ResetWallet,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"resetTransactionInfo",
base::BindRepeating(&BraveDefaultExtensionsHandler::ResetTransactionInfo,
base::Unretained(this)));

web_ui()->RegisterMessageCallback(
"setWebTorrentEnabled",
Expand Down Expand Up @@ -209,6 +213,11 @@ void BraveDefaultExtensionsHandler::ResetWallet(
brave_wallet::ResetWallet(profile_);
}

void BraveDefaultExtensionsHandler::ResetTransactionInfo(
base::Value::ConstListView args) {
brave_wallet::ResetTransactionInfo(profile_);
}

void BraveDefaultExtensionsHandler::SetWebTorrentEnabled(
base::Value::ConstListView args) {
CHECK_EQ(args.size(), 1U);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler
const std::string& error,
extensions::webstore_install::Result result);
void ResetWallet(base::Value::ConstListView args);
void ResetTransactionInfo(base::Value::ConstListView args);
void OnRestartNeededChanged();
bool IsRestartNeeded();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,15 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
{"ipfsKeyRemove", IDS_SETTINGS_IPNS_KEY_REMOVE_ITEM},
{"ipfsKeyExportError", IDS_SETTINGS_IPNS_KEYS_EXPORT_ERROR},
{"resetWallet", IDS_SETTINGS_WALLET_RESET},
{"resetTransactionInfo", IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO},
{"resetTransactionInfoDesc",
IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO_DESC},
{"walletResetConfirmation", IDS_SETTINGS_WALLET_RESET_CONFIRMATION},
{"walletResetTransactionInfoConfirmation",
IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO_CONFIRMATION},
{"walletResetConfirmed", IDS_SETTINGS_WALLET_RESET_CONFIRMED},
{"walletResetTransactionInfoConfirmed",
IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO_CONFIRMED},
{"walletNetworksLinkTitle", IDS_SETTINGS_WALLET_NETWORKS_ITEM},
{"walletAddNetworkDialogTitle", IDS_SETTINGS_WALLET_ADD_NETWORK_TITLE},
{"walletAddNetworkInvalidURLInput",
Expand Down Expand Up @@ -469,6 +476,11 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
auto confirmation_text = l10n_util::GetStringFUTF16(
IDS_SETTINGS_WALLET_RESET_CONFIRMATION, confirmation_phrase);
html_source->AddString("walletResetConfirmation", confirmation_text);
auto reset_tx_confirmation_text = l10n_util::GetStringFUTF16(
IDS_SETTINGS_WALLET_RESET_TRANSACTION_INFO_CONFIRMATION,
confirmation_phrase);
html_source->AddString("walletResetTransactionInfoConfirmation",
reset_tx_confirmation_text);
#if BUILDFLAG(ENABLE_EXTENSIONS)
html_source->AddString("webDiscoveryLearnMoreURL", kWebDiscoveryLearnMoreUrl);
#endif
Expand Down
23 changes: 23 additions & 0 deletions components/brave_wallet/browser/eth_tx_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,29 @@ void EthTxController::SetDataForUnapprovedTransaction(
std::move(callback).Run(true);
}

void EthTxController::SetNonceForUnapprovedTransaction(
const std::string& tx_meta_id,
const std::string& nonce,
SetNonceForUnapprovedTransactionCallback callback) {
std::unique_ptr<EthTxStateManager::TxMeta> tx_meta =
tx_state_manager_->GetTx(tx_meta_id);
if (!tx_meta || tx_meta->status != mojom::TransactionStatus::Unapproved) {
std::move(callback).Run(false);
return;
}

uint256_t nonce_uint;
if (!HexValueToUint256(nonce, &nonce_uint)) {
std::move(callback).Run(false);
return;
}

tx_meta->tx->set_nonce(nonce_uint);
tx_state_manager_->AddOrUpdateTx(*tx_meta);
NotifyUnapprovedTxUpdated(tx_meta.get());
std::move(callback).Run(true);
}

std::unique_ptr<EthTxStateManager::TxMeta> EthTxController::GetTxForTesting(
const std::string& tx_meta_id) {
return tx_state_manager_->GetTx(tx_meta_id);
Expand Down
4 changes: 4 additions & 0 deletions components/brave_wallet/browser/eth_tx_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class EthTxController : public KeyedService,
const std::string& tx_meta_id,
const std::vector<uint8_t>& data,
SetDataForUnapprovedTransactionCallback callback) override;
void SetNonceForUnapprovedTransaction(
const std::string& tx_meta_id,
const std::string& nonce,
SetNonceForUnapprovedTransactionCallback) override;
void GetNonceForHardwareTransaction(
const std::string& tx_meta_id,
GetNonceForHardwareTransactionCallback callback) override;
Expand Down
89 changes: 80 additions & 9 deletions components/brave_wallet/browser/eth_tx_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ class TestEthTxControllerObserver
: public brave_wallet::mojom::EthTxControllerObserver {
public:
TestEthTxControllerObserver(
const std::string& expected_nonce,
const std::string& expected_gas_price,
const std::string& expected_gas_limit,
const std::string& expected_max_priority_fee_per_gas = "",
const std::string& expected_max_fee_per_gas = "",
const std::vector<uint8_t>& expected_data = std::vector<uint8_t>(),
mojom::TransactionStatus expected_status =
mojom::TransactionStatus::Unapproved)
: expected_gas_price_(expected_gas_price),
: expected_nonce_(expected_nonce),
expected_gas_price_(expected_gas_price),
expected_gas_limit_(expected_gas_limit),
expected_max_priority_fee_per_gas_(expected_max_priority_fee_per_gas),
expected_max_fee_per_gas_(expected_max_fee_per_gas),
Expand All @@ -117,6 +119,8 @@ class TestEthTxControllerObserver
void OnNewUnapprovedTx(mojom::TransactionInfoPtr tx) override {}

void OnUnapprovedTxUpdated(mojom::TransactionInfoPtr tx) override {
EXPECT_EQ(tx->tx_data->base_data->nonce,
base::ToLowerASCII(expected_nonce_));
EXPECT_EQ(tx->tx_data->base_data->gas_price,
base::ToLowerASCII(expected_gas_price_));
EXPECT_EQ(tx->tx_data->base_data->gas_limit,
Expand Down Expand Up @@ -146,6 +150,7 @@ class TestEthTxControllerObserver
}

private:
std::string expected_nonce_;
std::string expected_gas_price_;
std::string expected_gas_limit_;
std::string expected_max_priority_fee_per_gas_;
Expand Down Expand Up @@ -637,7 +642,7 @@ TEST_F(EthTxControllerUnitTest, SetGasPriceAndLimitForUnapprovedTransaction) {
EXPECT_TRUE(
HexValueToUint256(update_gas_limit_hex_string, &update_gas_limit));

TestEthTxControllerObserver observer(update_gas_price_hex_string,
TestEthTxControllerObserver observer("0x6", update_gas_price_hex_string,
update_gas_limit_hex_string);
eth_tx_controller_->AddObserver(observer.GetReceiver());

Expand Down Expand Up @@ -690,7 +695,8 @@ TEST_F(EthTxControllerUnitTest, SetDataForUnapprovedTransaction) {
run_loop.Run();

std::vector<uint8_t> new_data2{1U, 3U, 3U, 7U};
TestEthTxControllerObserver observer("0x11", "0x22", "", "", new_data2);
TestEthTxControllerObserver observer("0x6", "0x11", "0x22", "", "",
new_data2);
eth_tx_controller_->AddObserver(observer.GetReceiver());

// Change the data
Expand All @@ -711,6 +717,67 @@ TEST_F(EthTxControllerUnitTest, SetDataForUnapprovedTransaction) {
EXPECT_EQ(tx_meta->tx->data(), new_data2);
}

TEST_F(EthTxControllerUnitTest, SetNonceForUnapprovedTransaction) {
auto tx_data =
mojom::TxData::New("0x06", "0x11" /* gas_price*/, "0x22" /* gas_limit */,
"0xbe862ad9abfe6f22bcb087716c7d89a26051f74c",
"0x016345785d8a0000", std::vector<uint8_t>());
bool callback_called = false;
std::string tx_meta_id;
eth_tx_controller_->AddUnapprovedTransaction(
std::move(tx_data), from(),
base::BindOnce(&AddUnapprovedTransactionSuccessCallback, &callback_called,
&tx_meta_id));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_called);

auto tx_meta = eth_tx_controller_->GetTxForTesting(tx_meta_id);
EXPECT_TRUE(tx_meta);

EXPECT_EQ(tx_meta->tx->nonce(), 6ULL);

// Invalid tx_meta id should fail
base::RunLoop run_loop;
eth_tx_controller_->SetNonceForUnapprovedTransaction(
"", "0x02", base::BindLambdaForTesting([&](bool success) {
EXPECT_FALSE(success);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_EQ(tx_meta->tx->nonce(), 6ULL);

// Invalid nonce value should fail
base::RunLoop run_loop2;
eth_tx_controller_->SetNonceForUnapprovedTransaction(
tx_meta_id, "invalid nonce",
base::BindLambdaForTesting([&](bool success) {
EXPECT_FALSE(success);
run_loop2.Quit();
}));
run_loop2.Run();
EXPECT_EQ(tx_meta->tx->nonce(), 6ULL);

TestEthTxControllerObserver observer("0x3", "0x11", "0x22");
eth_tx_controller_->AddObserver(observer.GetReceiver());

// Change the data
base::RunLoop run_loop3;
eth_tx_controller_->SetNonceForUnapprovedTransaction(
tx_meta_id, "0x3", base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success);
run_loop3.Quit();
}));
run_loop3.Run();

base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer.TxUpdated());

// Get the updated TX.
tx_meta = eth_tx_controller_->GetTxForTesting(tx_meta_id);
EXPECT_TRUE(tx_meta);
EXPECT_EQ(tx_meta->tx->nonce(), 3ULL);
}

TEST_F(EthTxControllerUnitTest, ValidateTxData) {
std::string error_message;
EXPECT_TRUE(EthTxController::ValidateTxData(
Expand Down Expand Up @@ -790,7 +857,8 @@ TEST_F(EthTxControllerUnitTest, ProcessHardwareSignature) {
tx_data.Clone(), from(),
base::BindOnce(&AddUnapprovedTransactionSuccessCallback, &callback_called,
&tx_meta_id));
TestEthTxControllerObserver observer("", "", "", "", std::vector<uint8_t>(),
TestEthTxControllerObserver observer("0x6", "", "", "", "",
std::vector<uint8_t>(),
mojom::TransactionStatus::Approved);
eth_tx_controller_->AddObserver(observer.GetReceiver());
base::RunLoop().RunUntilIdle();
Expand Down Expand Up @@ -828,7 +896,8 @@ TEST_F(EthTxControllerUnitTest, ProcessHardwareSignatureFail) {
tx_data.Clone(), from(),
base::BindOnce(&AddUnapprovedTransactionSuccessCallback, &callback_called,
&tx_meta_id));
TestEthTxControllerObserver observer("", "", "", "", std::vector<uint8_t>(),
TestEthTxControllerObserver observer("0x6", "", "", "", "",
std::vector<uint8_t>(),
mojom::TransactionStatus::Error);
eth_tx_controller_->AddObserver(observer.GetReceiver());
base::RunLoop().RunUntilIdle();
Expand Down Expand Up @@ -874,7 +943,8 @@ TEST_F(EthTxControllerUnitTest, GetNonceForHardwareTransaction) {

base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_called);
TestEthTxControllerObserver observer("", "", "", "", std::vector<uint8_t>(),
TestEthTxControllerObserver observer("", "", "", "", "",
std::vector<uint8_t>(),
mojom::TransactionStatus::Unapproved);
eth_tx_controller_->AddObserver(observer.GetReceiver());
callback_called = false;
Expand Down Expand Up @@ -927,7 +997,8 @@ TEST_F(EthTxControllerUnitTest, GetNonceForHardwareTransaction1559) {

base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_called);
TestEthTxControllerObserver observer("", "", "", "", std::vector<uint8_t>(),
TestEthTxControllerObserver observer("0x0", "", "", "", "",
std::vector<uint8_t>(),
mojom::TransactionStatus::Unapproved);
eth_tx_controller_->AddObserver(observer.GetReceiver());
callback_called = false;
Expand Down Expand Up @@ -962,7 +1033,7 @@ TEST_F(EthTxControllerUnitTest, GetNonceForHardwareTransaction1559) {

TEST_F(EthTxControllerUnitTest, GetNonceForHardwareTransactionFail) {
bool callback_called = false;
TestEthTxControllerObserver observer("", "");
TestEthTxControllerObserver observer("0x1", "", "");
eth_tx_controller_->AddObserver(observer.GetReceiver());
eth_tx_controller_->GetNonceForHardwareTransaction(
std::string(),
Expand Down Expand Up @@ -1330,7 +1401,7 @@ TEST_F(EthTxControllerUnitTest, SetGasFeeAndLimitForUnapprovedTransaction) {
HexValueToUint256(update_gas_limit_hex_string, &update_gas_limit));

TestEthTxControllerObserver observer(
"0x0", update_gas_limit_hex_string,
"0x1", "0x0", update_gas_limit_hex_string,
update_max_priority_fee_per_gas_hex_string,
update_max_fee_per_gas_hex_string, data_);
eth_tx_controller_->AddObserver(observer.GetReceiver());
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ interface EthTxController {
SetGasPriceAndLimitForUnapprovedTransaction(string tx_meta_id, string gas_price, string gas_limit) => (bool success);
SetGasFeeAndLimitForUnapprovedTransaction(string tx_meta_id, string max_priority_fee_per_gas, string max_fee_per_gas, string gas_limit) => (bool success);
SetDataForUnapprovedTransaction(string tx_meta_id, array<uint8> data) => (bool success);
SetNonceForUnapprovedTransaction(string tx_meta_id, string nonce) => (bool success);

// Used for creating transaction data
MakeERC20TransferData(string to_address, string amount) => (bool success, array<uint8> data);
Expand Down