From e875f5886aac2d499a0fb48b65516aa9d12c91bf Mon Sep 17 00:00:00 2001 From: Anton Paymyshev Date: Thu, 7 Nov 2024 14:42:45 +0700 Subject: [PATCH] Fix UNSAFE_TODO for wallet --- .../android/android_wallet_page_ui.cc | 12 +- .../ui/webui/brave_wallet/ledger/ledger_ui.cc | 3 +- .../brave_wallet/line_chart/line_chart_ui.cc | 12 +- .../ui/webui/brave_wallet/market/market_ui.cc | 16 +- browser/ui/webui/brave_wallet/nft/nft_ui.cc | 16 +- .../ui/webui/brave_wallet/trezor/trezor_ui.cc | 3 +- .../ui/webui/brave_wallet/wallet_page_ui.cc | 7 +- .../ui/webui/brave_wallet/wallet_panel_ui.cc | 7 +- .../bitcoin/bitcoin_discover_account_task.cc | 6 - .../bitcoin_knapsack_solver_unittest.cc | 3 +- .../bitcoin_max_send_solver_unittest.cc | 2 +- .../brave_wallet/browser/ens_resolver_task.cc | 4 +- .../brave_wallet/browser/eth_data_builder.cc | 2 +- .../brave_wallet/browser/filecoin_keyring.cc | 4 +- .../browser/json_rpc_service_unittest.cc | 21 +- .../browser/keyring_service_migrations.cc | 8 +- ...ld_integration_response_parser_unittest.cc | 83 ++++---- .../browser/password_encryptor_unittest.cc | 3 +- .../browser/permission_utils_unittest.cc | 201 ++++++++---------- .../brave_wallet/browser/sns_resolver_task.cc | 13 +- .../browser/solana_instruction_builder.cc | 73 ++----- .../brave_wallet/browser/solana_message.cc | 2 +- .../solana_message_address_table_lookup.cc | 2 +- .../browser/wallet_data_files_installer.cc | 5 +- 24 files changed, 191 insertions(+), 317 deletions(-) diff --git a/browser/ui/webui/brave_wallet/android/android_wallet_page_ui.cc b/browser/ui/webui/brave_wallet/android/android_wallet_page_ui.cc index 3f015b2cbd55..aa4ea491f59b 100644 --- a/browser/ui/webui/brave_wallet/android/android_wallet_page_ui.cc +++ b/browser/ui/webui/brave_wallet/android/android_wallet_page_ui.cc @@ -51,16 +51,8 @@ AndroidWalletPageUI::AndroidWalletPageUI(content::WebUI* web_ui, } // Add required resources. - if (url.host() == kWalletPageHost) { - webui::SetupWebUIDataSource( - source, - UNSAFE_TODO(base::make_span(kBraveWalletPageGenerated, - kBraveWalletPageGeneratedSize)), - IDR_WALLET_PAGE_HTML); - } else { - NOTREACHED_IN_MIGRATION() - << "Failed to find page resources for:" << url.path(); - } + webui::SetupWebUIDataSource(source, base::span(kBraveWalletPageGenerated), + IDR_WALLET_PAGE_HTML); source->AddBoolean("isAndroid", true); source->AddString("braveWalletLedgerBridgeUrl", kUntrustedLedgerURL); diff --git a/browser/ui/webui/brave_wallet/ledger/ledger_ui.cc b/browser/ui/webui/brave_wallet/ledger/ledger_ui.cc index d5f245d96bf2..10d6640b3e18 100644 --- a/browser/ui/webui/brave_wallet/ledger/ledger_ui.cc +++ b/browser/ui/webui/brave_wallet/ledger/ledger_ui.cc @@ -21,8 +21,7 @@ UntrustedLedgerUI::UntrustedLedgerUI(content::WebUI* web_ui) auto* untrusted_source = content::WebUIDataSource::CreateAndAdd( web_ui->GetWebContents()->GetBrowserContext(), kUntrustedLedgerURL); untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_LEDGER_BRIDGE_HTML); - untrusted_source->AddResourcePaths(UNSAFE_TODO( - base::make_span(kLedgerBridgeGenerated, kLedgerBridgeGeneratedSize))); + untrusted_source->AddResourcePaths(base::span(kLedgerBridgeGenerated)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL)); untrusted_source->OverrideContentSecurityPolicy( diff --git a/browser/ui/webui/brave_wallet/line_chart/line_chart_ui.cc b/browser/ui/webui/brave_wallet/line_chart/line_chart_ui.cc index 792941c73b39..92fa32ef4e32 100644 --- a/browser/ui/webui/brave_wallet/line_chart/line_chart_ui.cc +++ b/browser/ui/webui/brave_wallet/line_chart/line_chart_ui.cc @@ -3,12 +3,6 @@ * 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/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/browser/ui/webui/brave_wallet/line_chart/line_chart_ui.h" #include @@ -40,13 +34,11 @@ UntrustedLineChartUI::UntrustedLineChartUI(content::WebUI* web_ui) untrusted_source->SetDefaultResource( IDR_BRAVE_WALLET_LINE_CHART_DISPLAY_HTML); - untrusted_source->AddResourcePaths(base::make_span( - kLineChartDisplayGenerated, kLineChartDisplayGeneratedSize)); + untrusted_source->AddResourcePaths(base::span(kLineChartDisplayGenerated)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL)); webui::SetupWebUIDataSource(untrusted_source, - base::make_span(kLineChartDisplayGenerated, - kLineChartDisplayGeneratedSize), + base::span(kLineChartDisplayGenerated), IDR_BRAVE_WALLET_LINE_CHART_DISPLAY_HTML); untrusted_source->OverrideContentSecurityPolicy( network::mojom::CSPDirectiveName::ScriptSrc, diff --git a/browser/ui/webui/brave_wallet/market/market_ui.cc b/browser/ui/webui/brave_wallet/market/market_ui.cc index 3e7c588da4bf..176fd68c3096 100644 --- a/browser/ui/webui/brave_wallet/market/market_ui.cc +++ b/browser/ui/webui/brave_wallet/market/market_ui.cc @@ -3,12 +3,6 @@ * 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/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/browser/ui/webui/brave_wallet/market/market_ui.h" #include @@ -38,14 +32,12 @@ UntrustedMarketUI::UntrustedMarketUI(content::WebUI* web_ui) untrusted_source->AddString(str.name, l10n_str); } untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_MARKET_DISPLAY_HTML); - untrusted_source->AddResourcePaths( - base::make_span(kMarketDisplayGenerated, kMarketDisplayGeneratedSize)); + untrusted_source->AddResourcePaths(base::span(kMarketDisplayGenerated)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL)); - webui::SetupWebUIDataSource( - untrusted_source, - base::make_span(kMarketDisplayGenerated, kMarketDisplayGeneratedSize), - IDR_BRAVE_WALLET_MARKET_DISPLAY_HTML); + webui::SetupWebUIDataSource(untrusted_source, + base::span(kMarketDisplayGenerated), + IDR_BRAVE_WALLET_MARKET_DISPLAY_HTML); untrusted_source->OverrideContentSecurityPolicy( network::mojom::CSPDirectiveName::ScriptSrc, diff --git a/browser/ui/webui/brave_wallet/nft/nft_ui.cc b/browser/ui/webui/brave_wallet/nft/nft_ui.cc index ee357c0fe832..b2546db3b7be 100644 --- a/browser/ui/webui/brave_wallet/nft/nft_ui.cc +++ b/browser/ui/webui/brave_wallet/nft/nft_ui.cc @@ -3,12 +3,6 @@ * 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/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/browser/ui/webui/brave_wallet/nft/nft_ui.h" #include @@ -39,14 +33,12 @@ UntrustedNftUI::UntrustedNftUI(content::WebUI* web_ui) } untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_NFT_DISPLAY_HTML); - untrusted_source->AddResourcePaths( - base::make_span(kNftDisplayGenerated, kNftDisplayGeneratedSize)); + untrusted_source->AddResourcePaths(base::span(kNftDisplayGenerated)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL)); - webui::SetupWebUIDataSource( - untrusted_source, - base::make_span(kNftDisplayGenerated, kNftDisplayGeneratedSize), - IDR_BRAVE_WALLET_NFT_DISPLAY_HTML); + webui::SetupWebUIDataSource(untrusted_source, + base::span(kNftDisplayGenerated), + IDR_BRAVE_WALLET_NFT_DISPLAY_HTML); untrusted_source->OverrideContentSecurityPolicy( network::mojom::CSPDirectiveName::ScriptSrc, std::string("script-src 'self' chrome-untrusted://resources;")); diff --git a/browser/ui/webui/brave_wallet/trezor/trezor_ui.cc b/browser/ui/webui/brave_wallet/trezor/trezor_ui.cc index 9e97e7c2a3b4..3acb903743db 100644 --- a/browser/ui/webui/brave_wallet/trezor/trezor_ui.cc +++ b/browser/ui/webui/brave_wallet/trezor/trezor_ui.cc @@ -25,8 +25,7 @@ UntrustedTrezorUI::UntrustedTrezorUI(content::WebUI* web_ui) auto* untrusted_source = content::WebUIDataSource::CreateAndAdd( web_ui->GetWebContents()->GetBrowserContext(), kUntrustedTrezorURL); untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_TREZOR_BRIDGE_HTML); - untrusted_source->AddResourcePaths(UNSAFE_TODO( - base::make_span(kTrezorBridgeGenerated, kTrezorBridgeGeneratedSize))); + untrusted_source->AddResourcePaths(base::span(kTrezorBridgeGenerated)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL)); untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL)); untrusted_source->OverrideContentSecurityPolicy( diff --git a/browser/ui/webui/brave_wallet/wallet_page_ui.cc b/browser/ui/webui/brave_wallet/wallet_page_ui.cc index f69bd17d30af..c1c664e14b77 100644 --- a/browser/ui/webui/brave_wallet/wallet_page_ui.cc +++ b/browser/ui/webui/brave_wallet/wallet_page_ui.cc @@ -63,11 +63,8 @@ WalletPageUI::WalletPageUI(content::WebUI* web_ui) "braveWalletPendingTransactions", IDS_BRAVE_WALLET_PENDING_TRANSACTIONS); web_ui->AddMessageHandler(std::move(plural_string_handler)); NavigationBarDataProvider::Initialize(source, profile); - webui::SetupWebUIDataSource( - source, - UNSAFE_TODO(base::make_span(kBraveWalletPageGenerated, - kBraveWalletPageGeneratedSize)), - IDR_WALLET_PAGE_HTML); + webui::SetupWebUIDataSource(source, base::span(kBraveWalletPageGenerated), + IDR_WALLET_PAGE_HTML); source->AddString("braveWalletLedgerBridgeUrl", kUntrustedLedgerURL); source->OverrideContentSecurityPolicy( network::mojom::CSPDirectiveName::ImgSrc, diff --git a/browser/ui/webui/brave_wallet/wallet_panel_ui.cc b/browser/ui/webui/brave_wallet/wallet_panel_ui.cc index 2116b243aa46..fd393b4e925a 100644 --- a/browser/ui/webui/brave_wallet/wallet_panel_ui.cc +++ b/browser/ui/webui/brave_wallet/wallet_panel_ui.cc @@ -66,11 +66,8 @@ WalletPanelUI::WalletPanelUI(content::WebUI* web_ui) plural_string_handler->AddLocalizedString( "braveWalletPendingTransactions", IDS_BRAVE_WALLET_PENDING_TRANSACTIONS); web_ui->AddMessageHandler(std::move(plural_string_handler)); - webui::SetupWebUIDataSource( - source, - UNSAFE_TODO(base::make_span(kBraveWalletPanelGenerated, - kBraveWalletPanelGeneratedSize)), - IDR_WALLET_PANEL_HTML); + webui::SetupWebUIDataSource(source, base::span(kBraveWalletPanelGenerated), + IDR_WALLET_PANEL_HTML); source->AddString("braveWalletLedgerBridgeUrl", kUntrustedLedgerURL); source->OverrideContentSecurityPolicy( network::mojom::CSPDirectiveName::FrameSrc, diff --git a/components/brave_wallet/browser/bitcoin/bitcoin_discover_account_task.cc b/components/brave_wallet/browser/bitcoin/bitcoin_discover_account_task.cc index c114ce80655f..8c4bf9a803a8 100644 --- a/components/brave_wallet/browser/bitcoin/bitcoin_discover_account_task.cc +++ b/components/brave_wallet/browser/bitcoin/bitcoin_discover_account_task.cc @@ -3,12 +3,6 @@ * 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/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_wallet/browser/bitcoin/bitcoin_discover_account_task.h" #include diff --git a/components/brave_wallet/browser/bitcoin/bitcoin_knapsack_solver_unittest.cc b/components/brave_wallet/browser/bitcoin/bitcoin_knapsack_solver_unittest.cc index e67527a0192a..47a7d2eebeca 100644 --- a/components/brave_wallet/browser/bitcoin/bitcoin_knapsack_solver_unittest.cc +++ b/components/brave_wallet/browser/bitcoin/bitcoin_knapsack_solver_unittest.cc @@ -7,6 +7,7 @@ #include +#include "base/containers/span.h" #include "base/rand_util.h" #include "base/strings/string_number_conversions.h" #include "brave/components/brave_wallet/browser/bitcoin/bitcoin_hd_keyring.h" @@ -74,7 +75,7 @@ class BitcoinKnapsackSolverUnitTest : public testing::Test { tx_input.utxo_address = address; std::string txid_fake = address + base::NumberToString(amount); tx_input.utxo_outpoint.txid = - crypto::SHA256Hash(base::as_bytes(base::make_span(txid_fake))); + crypto::SHA256Hash(base::as_byte_span(txid_fake)); tx_input.utxo_outpoint.index = tx_input.utxo_outpoint.txid.back(); tx_input.utxo_value = amount; diff --git a/components/brave_wallet/browser/bitcoin/bitcoin_max_send_solver_unittest.cc b/components/brave_wallet/browser/bitcoin/bitcoin_max_send_solver_unittest.cc index d5d3d6e9ec88..c8e763318624 100644 --- a/components/brave_wallet/browser/bitcoin/bitcoin_max_send_solver_unittest.cc +++ b/components/brave_wallet/browser/bitcoin/bitcoin_max_send_solver_unittest.cc @@ -63,7 +63,7 @@ class BitcoinMaxSendSolverUnitTest : public testing::Test { tx_input.utxo_address = address; std::string txid_fake = address + base::NumberToString(amount); tx_input.utxo_outpoint.txid = - crypto::SHA256Hash(base::as_bytes(base::make_span(txid_fake))); + crypto::SHA256Hash(base::as_byte_span(txid_fake)); tx_input.utxo_outpoint.index = tx_input.utxo_outpoint.txid.back(); tx_input.utxo_value = amount; diff --git a/components/brave_wallet/browser/ens_resolver_task.cc b/components/brave_wallet/browser/ens_resolver_task.cc index 82aa5e597da3..e327bb3bf584 100644 --- a/components/brave_wallet/browser/ens_resolver_task.cc +++ b/components/brave_wallet/browser/ens_resolver_task.cc @@ -117,13 +117,13 @@ EnsResolverTaskError::~EnsResolverTaskError() = default; std::vector MakeAddrCall(const std::string& domain) { return eth_abi::TupleEncoder() .AddFixedBytes(Namehash(domain)) - .EncodeWithSelector(base::make_span(kAddrBytes32Selector)); + .EncodeWithSelector(base::span(kAddrBytes32Selector)); } std::vector MakeContentHashCall(const std::string& domain) { return eth_abi::TupleEncoder() .AddFixedBytes(Namehash(domain)) - .EncodeWithSelector(base::make_span(kContentHashBytes32Selector)); + .EncodeWithSelector(base::span(kContentHashBytes32Selector)); } OffchainLookupData::OffchainLookupData() = default; diff --git a/components/brave_wallet/browser/eth_data_builder.cc b/components/brave_wallet/browser/eth_data_builder.cc index daca8db10a7e..c7261a497b9f 100644 --- a/components/brave_wallet/browser/eth_data_builder.cc +++ b/components/brave_wallet/browser/eth_data_builder.cc @@ -72,7 +72,7 @@ std::optional> Forward(const FilAddress& fil_address) { return eth_abi::TupleEncoder() .AddBytes(fil_address.GetBytes()) - .EncodeWithSelector(base::make_span(kFilForwarderSelector)); + .EncodeWithSelector(base::span(kFilForwarderSelector)); } } // namespace filforwarder diff --git a/components/brave_wallet/browser/filecoin_keyring.cc b/components/brave_wallet/browser/filecoin_keyring.cc index 44fbd19ed4d5..5ec0d7ff7a05 100644 --- a/components/brave_wallet/browser/filecoin_keyring.cc +++ b/components/brave_wallet/browser/filecoin_keyring.cc @@ -121,7 +121,7 @@ std::string FilecoinKeyring::EncodePrivateKeyForExport( const std::string& address) { if (base::Contains(imported_bls_accounts_, address)) { return GetExportEncodedJSON( - base::Base64Encode(base::make_span(*imported_bls_accounts_[address])), + base::Base64Encode(base::span(*imported_bls_accounts_[address])), address); } @@ -209,7 +209,7 @@ std::optional FilecoinKeyring::SignTransaction( if (base::Contains(imported_bls_accounts_, address)) { return tx->GetSignedTransaction( - fil_address, base::make_span(*imported_bls_accounts_[address])); + fil_address, base::span(*imported_bls_accounts_[address])); } HDKey* hd_key = GetHDKeyFromAddress(address); diff --git a/components/brave_wallet/browser/json_rpc_service_unittest.cc b/components/brave_wallet/browser/json_rpc_service_unittest.cc index f90a58cf7995..9c7aa868ee4e 100644 --- a/components/brave_wallet/browser/json_rpc_service_unittest.cc +++ b/components/brave_wallet/browser/json_rpc_service_unittest.cc @@ -409,10 +409,8 @@ class GetAccountInfoHandler : public SolRpcCallHandler { static std::vector MakeMintData(int supply) { std::vector data(82); - base::as_writable_bytes(base::make_span(data)) - .subspan(36) - .first<8u>() - .copy_from(base::U64ToNativeEndian(supply)); + base::span(data).subspan(36).first<8u>().copy_from( + base::U64ToLittleEndian(supply)); return data; } @@ -420,7 +418,7 @@ class GetAccountInfoHandler : public SolRpcCallHandler { const SolanaAddress& owner, const std::vector& data = {}) { std::vector result(96 + data.size()); - auto result_span = base::as_writable_bytes(base::make_span(result)); + auto result_span = base::span(result); // Header. base::ranges::copy(owner.bytes(), result_span.subspan(32, 32).begin()); @@ -435,7 +433,7 @@ class GetAccountInfoHandler : public SolRpcCallHandler { const SolanaAddress& sol_record_address, const std::vector& signer_key) { std::vector result(32 + 64); // payload_address + signature. - auto result_span = base::as_writable_bytes(base::make_span(result)); + auto result_span = base::span(result); base::ranges::copy(sol_record_payload_address.bytes(), result_span.begin()); @@ -609,15 +607,12 @@ class GetProgramAccountsHandler : public SolRpcCallHandler { static std::vector MakeTokenAccountData(const SolanaAddress& mint, const SolanaAddress& owner) { std::vector data(165); - auto mint_span = - base::as_writable_bytes(base::make_span(data)).subspan(0, 32); + auto mint_span = base::span(data).subspan(0, 32); base::ranges::copy(mint.bytes(), mint_span.begin()); - auto owner_span = - base::as_writable_bytes(base::make_span(data)).subspan(32, 32); + auto owner_span = base::span(data).subspan(32, 32); base::ranges::copy(owner.bytes(), owner_span.begin()); - auto amount_span = - base::as_writable_bytes(base::make_span(data)).subspan(64, 1); + auto amount_span = base::span(data).subspan(64, 1); *amount_span.data() = 1; return data; @@ -632,7 +627,7 @@ class GetProgramAccountsHandler : public SolRpcCallHandler { auto* filters = (*dict.FindList("params"))[1].GetDict().FindList("filters"); EXPECT_TRUE(filters); - auto data_span = base::make_span(token_account_data_); + auto data_span = base::span(token_account_data_); base::Value::List expected_filters; expected_filters.Append(base::Value::Dict()); expected_filters.back().GetDict().SetByDottedPath("memcmp.offset", 0); diff --git a/components/brave_wallet/browser/keyring_service_migrations.cc b/components/brave_wallet/browser/keyring_service_migrations.cc index a55f8b225273..f2ab71b96d57 100644 --- a/components/brave_wallet/browser/keyring_service_migrations.cc +++ b/components/brave_wallet/browser/keyring_service_migrations.cc @@ -333,8 +333,8 @@ void MaybeMigratePBKDF2Iterations(PrefService* profile_prefs, /*force_create = */ true); SetPrefForKeyring(profile_prefs, kEncryptedMnemonicDeprecated, - base::Value(base::Base64Encode(encryptor->Encrypt( - base::make_span(*mnemonic), nonce))), + base::Value(base::Base64Encode( + encryptor->Encrypt(base::span(*mnemonic), nonce))), keyring_id); if (keyring_id == mojom::kDefaultKeyringId) { @@ -368,7 +368,7 @@ void MaybeMigratePBKDF2Iterations(PrefService* profile_prefs, } auto private_key = deprecated_encryptor->Decrypt( - base::make_span(*deprecated_private_key_decoded), *deprecated_nonce); + base::span(*deprecated_private_key_decoded), *deprecated_nonce); if (!private_key) { continue; } @@ -504,7 +504,7 @@ void MaybeMigrateToWalletMnemonic(PrefService* profile_prefs, } auto private_key = deprecated_encryptor->Decrypt( - base::make_span(*deprecated_private_key_decoded), *deprecated_nonce); + base::span(*deprecated_private_key_decoded), *deprecated_nonce); if (!private_key) { continue; } diff --git a/components/brave_wallet/browser/meld_integration_response_parser_unittest.cc b/components/brave_wallet/browser/meld_integration_response_parser_unittest.cc index f32683eaf292..fb2162f29701 100644 --- a/components/brave_wallet/browser/meld_integration_response_parser_unittest.cc +++ b/components/brave_wallet/browser/meld_integration_response_parser_unittest.cc @@ -3,12 +3,6 @@ * 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/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_wallet/browser/meld_integration_response_parser.h" #include @@ -50,33 +44,30 @@ TEST(MeldIntegrationResponseParserUnitTest, Parse_ServiceProvider) { auto service_providers = ParseServiceProviders(ParseJson(json)); EXPECT_TRUE(service_providers); - EXPECT_EQ(base::ranges::count_if( - *service_providers, - [](const auto& item) { - return item->name == "Banxa" && - item->service_provider == "BANXA" && - item->status == "LIVE" && - item->web_site_url == "http://www.banxa.com" && - !item->categories.empty() && - item->categories[0] == "CRYPTO_ONRAMP" && - !item->category_statuses.empty() && - item->category_statuses.contains("CRYPTO_ONRAMP") && - item->category_statuses["CRYPTO_ONRAMP"] == "LIVE" && - item->logo_images && - item->logo_images->dark_url == - "https://images-serviceprovider.meld.io/BANXA/" - "logo_dark.png" && - item->logo_images->dark_short_url == - "https://images-serviceprovider.meld.io/BANXA/" - "short_logo_dark.png" && - item->logo_images->light_url == - "https://images-serviceprovider.meld.io/BANXA/" - "logo_light.png" && - item->logo_images->light_short_url == - "https://images-serviceprovider.meld.io/BANXA/" - "short_logo_light.png"; - }), - 1); + EXPECT_EQ( + base::ranges::count_if( + *service_providers, + [](const auto& item) { + return item->name == "Banxa" && item->service_provider == "BANXA" && + item->status == "LIVE" && + item->web_site_url == "http://www.banxa.com" && + item->categories[0] == "CRYPTO_ONRAMP" && + item->category_statuses.at("CRYPTO_ONRAMP") == "LIVE" && + item->logo_images && + item->logo_images->dark_url == + "https://images-serviceprovider.meld.io/BANXA/" + "logo_dark.png" && + item->logo_images->dark_short_url == + "https://images-serviceprovider.meld.io/BANXA/" + "short_logo_dark.png" && + item->logo_images->light_url == + "https://images-serviceprovider.meld.io/BANXA/" + "logo_light.png" && + item->logo_images->light_short_url == + "https://images-serviceprovider.meld.io/BANXA/" + "short_logo_light.png"; + }), + 1); std::string json_null_logos(R"([ { "serviceProvider": "BANXA", @@ -90,20 +81,18 @@ TEST(MeldIntegrationResponseParserUnitTest, Parse_ServiceProvider) { }])"); service_providers = ParseServiceProviders(ParseJson(json_null_logos)); EXPECT_TRUE(service_providers); - EXPECT_EQ(base::ranges::count_if( - *service_providers, - [](const auto& item) { - return item->name == "Banxa" && - item->service_provider == "BANXA" && - item->status == "LIVE" && - item->web_site_url == "http://www.banxa.com" && - item->categories.empty() && - !item->category_statuses.empty() && - item->category_statuses.contains("CRYPTO_ONRAMP") && - item->category_statuses["CRYPTO_ONRAMP"] == "LIVE" && - !item->logo_images; - }), - 1); + EXPECT_EQ( + base::ranges::count_if( + *service_providers, + [](const auto& item) { + return item->name == "Banxa" && item->service_provider == "BANXA" && + item->status == "LIVE" && + item->web_site_url == "http://www.banxa.com" && + item->categories.empty() && + item->category_statuses.at("CRYPTO_ONRAMP") == "LIVE" && + !item->logo_images; + }), + 1); EXPECT_FALSE(ParseServiceProviders(ParseJson(R"([ { diff --git a/components/brave_wallet/browser/password_encryptor_unittest.cc b/components/brave_wallet/browser/password_encryptor_unittest.cc index b219355f8478..871edf6d3861 100644 --- a/components/brave_wallet/browser/password_encryptor_unittest.cc +++ b/components/brave_wallet/browser/password_encryptor_unittest.cc @@ -8,6 +8,7 @@ #include #include "base/base64.h" +#include "base/containers/span.h" #include "base/test/values_test_util.h" #include "crypto/aead.h" #include "testing/gtest/include/gtest/gtest.h" @@ -16,7 +17,7 @@ namespace brave_wallet { namespace { base::span ToSpan(std::string_view sv) { - return base::as_bytes(base::make_span(sv)); + return base::as_byte_span(sv); } std::string ToString(const std::vector& v) { return std::string(v.begin(), v.end()); diff --git a/components/brave_wallet/browser/permission_utils_unittest.cc b/components/brave_wallet/browser/permission_utils_unittest.cc index 482aee69d4a3..804438cc993c 100644 --- a/components/brave_wallet/browser/permission_utils_unittest.cc +++ b/components/brave_wallet/browser/permission_utils_unittest.cc @@ -3,12 +3,6 @@ * 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/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_wallet/browser/permission_utils.h" #include @@ -51,30 +45,26 @@ TEST(PermissionUtilsUnitTest, GetConcatOriginFromWalletAddresses) { }}; url::Origin origin = url::Origin::Create(GURL("https://test.com")); - for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { + for (const auto& test_case : cases) { + SCOPED_TRACE(testing::Message() << test_case.expected_out_origin); + url::Origin out_origin; - EXPECT_TRUE( - GetConcatOriginFromWalletAddresses(origin, cases[i].addrs, &out_origin)) - << "case: " << i; + EXPECT_TRUE(GetConcatOriginFromWalletAddresses(origin, test_case.addrs, + &out_origin)); EXPECT_EQ(out_origin, - url::Origin::Create(GURL(cases[i].expected_out_origin))) - << "case: " << i; + url::Origin::Create(GURL(test_case.expected_out_origin))); EXPECT_FALSE(GetConcatOriginFromWalletAddresses( - url::Origin(), cases[i].addrs, &out_origin)) - << "case: " << i; + url::Origin(), test_case.addrs, &out_origin)); EXPECT_FALSE(GetConcatOriginFromWalletAddresses( - origin, std::vector(), &out_origin)) - << "case: " << i; + origin, std::vector(), &out_origin)); // Origin with port case: EXPECT_TRUE(GetConcatOriginFromWalletAddresses( - url::Origin::Create(GURL("https://test.com:123")), cases[i].addrs, - &out_origin)) - << "case: " << i; - EXPECT_EQ(out_origin, - url::Origin::Create(GURL(cases[i].expected_out_origin_with_port))) - << "case: " << i; + url::Origin::Create(GURL("https://test.com:123")), test_case.addrs, + &out_origin)); + EXPECT_EQ(out_origin, url::Origin::Create( + GURL(test_case.expected_out_origin_with_port))); } } @@ -101,41 +91,35 @@ TEST(PermissionUtilsUnitTest, ParseRequestingOriginFromSubRequest) { url::Origin requesting_origin; std::string account; - for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { + for (auto& test_case : cases) { + SCOPED_TRACE(testing::Message() << test_case.account); + // Invalid requesting_origin format: EXPECT_FALSE(ParseRequestingOriginFromSubRequest( - cases[i].type, url::Origin::Create(GURL(cases[i].invalid_origin)), - nullptr, nullptr)) - << "case: " << i; + test_case.type, url::Origin::Create(GURL(test_case.invalid_origin)), + nullptr, nullptr)); EXPECT_FALSE(ParseRequestingOriginFromSubRequest( - cases[i].type, - url::Origin::Create(GURL(cases[i].invalid_origin_with_path)), nullptr, - nullptr)) - << "case: " << i; + test_case.type, + url::Origin::Create(GURL(test_case.invalid_origin_with_path)), nullptr, + nullptr)); EXPECT_FALSE(ParseRequestingOriginFromSubRequest( - cases[i].type, url::Origin(), nullptr, nullptr)) - << "case: " << i; + test_case.type, url::Origin(), nullptr, nullptr)); // invalid type EXPECT_FALSE(ParseRequestingOriginFromSubRequest( permissions::RequestType::kGeolocation, - url::Origin::Create(GURL(cases[i].valid_origin)), nullptr, nullptr)) - << "case: " << i; + url::Origin::Create(GURL(test_case.valid_origin)), nullptr, nullptr)); EXPECT_TRUE(ParseRequestingOriginFromSubRequest( - cases[i].type, url::Origin::Create(GURL(cases[i].valid_origin)), - &requesting_origin, &account)) - << "case: " << i; + test_case.type, url::Origin::Create(GURL(test_case.valid_origin)), + &requesting_origin, &account)); EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com")); - EXPECT_TRUE(base::EqualsCaseInsensitiveASCII(account, cases[i].account)) - << "case: " << i; + EXPECT_TRUE(base::EqualsCaseInsensitiveASCII(account, test_case.account)); EXPECT_TRUE(ParseRequestingOriginFromSubRequest( - cases[i].type, - url::Origin::Create(GURL(cases[i].valid_origin_with_port)), - &requesting_origin, &account)) - << "case: " << i; + test_case.type, + url::Origin::Create(GURL(test_case.valid_origin_with_port)), + &requesting_origin, &account)); EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com:123")); - EXPECT_TRUE(base::EqualsCaseInsensitiveASCII(account, cases[i].account)) - << "case: " << i; + EXPECT_TRUE(base::EqualsCaseInsensitiveASCII(account, test_case.account)); } // separator in domain would still work @@ -198,79 +182,69 @@ TEST(PermissionUtilsUnitTest, ParseRequestingOrigin) { "JDqrvDz8d8tFCADashbUKQDKfJZFobNy13ugN65t1wvV"}}; std::string account; - for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { + for (auto& test_case : cases) { + SCOPED_TRACE(testing::Message() << test_case.account1); + // Invalid requesting_origin format: EXPECT_FALSE(ParseRequestingOrigin( - cases[i].type, url::Origin::Create(GURL(cases[i].invalid_origin)), - nullptr, nullptr)) - << "case: " << i; + test_case.type, url::Origin::Create(GURL(test_case.invalid_origin)), + nullptr, nullptr)); EXPECT_FALSE(ParseRequestingOrigin( - cases[i].type, - url::Origin::Create(GURL(cases[i].invalid_origin_with_path)), nullptr, - nullptr)) - << "case: " << i; + test_case.type, + url::Origin::Create(GURL(test_case.invalid_origin_with_path)), nullptr, + nullptr)); EXPECT_FALSE( - ParseRequestingOrigin(cases[i].type, url::Origin(), nullptr, nullptr)) - << "case: " << i; + ParseRequestingOrigin(test_case.type, url::Origin(), nullptr, nullptr)); // invalid type EXPECT_FALSE(ParseRequestingOrigin( permissions::RequestType::kGeolocation, - url::Origin::Create(GURL(cases[i].valid_origin)), nullptr, nullptr)) - << "case: " << i; + url::Origin::Create(GURL(test_case.valid_origin)), nullptr, nullptr)); std::queue address_queue; // Origin without port: url::Origin requesting_origin; EXPECT_TRUE(ParseRequestingOrigin( - cases[i].type, url::Origin::Create(GURL(cases[i].valid_origin)), + test_case.type, url::Origin::Create(GURL(test_case.valid_origin)), &requesting_origin, &address_queue)); - EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com")) - << "case: " << i; - EXPECT_EQ(address_queue.size(), 1u) << "case: " << i; + EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com")); + EXPECT_EQ(address_queue.size(), 1u); EXPECT_TRUE(base::EqualsCaseInsensitiveASCII(address_queue.front(), - cases[i].account1)) - << "case: " << i; + test_case.account1)); EXPECT_TRUE(ParseRequestingOrigin( - cases[i].type, - url::Origin::Create(GURL(cases[i].valid_origin_two_accounts)), - &requesting_origin, nullptr)) - << "case: " << i; - EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com")) - << "case: " << i; + test_case.type, + url::Origin::Create(GURL(test_case.valid_origin_two_accounts)), + &requesting_origin, nullptr)); + EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com")); // Origin with port: EXPECT_TRUE(ParseRequestingOrigin( - cases[i].type, - url::Origin::Create(GURL(cases[i].valid_origin_with_port)), - &requesting_origin, nullptr)) - << "case: " << i; - EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com:123")) - << "case: " << i; + test_case.type, + url::Origin::Create(GURL(test_case.valid_origin_with_port)), + &requesting_origin, nullptr)); + EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com:123")); EXPECT_FALSE(ParseRequestingOrigin( - cases[i].type, - url::Origin::Create(GURL(cases[i].valid_origin_two_accounts_with_port)), + test_case.type, + url::Origin::Create( + GURL(test_case.valid_origin_two_accounts_with_port)), &requesting_origin, &address_queue)) - << "Non-empty address_queue param should return false. case: " << i; + << "Non-empty address_queue param should return false."; address_queue = std::queue(); EXPECT_TRUE(ParseRequestingOrigin( - cases[i].type, - url::Origin::Create(GURL(cases[i].valid_origin_two_accounts_with_port)), - &requesting_origin, &address_queue)) - << "case: " << i; - EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com:123")) - << "case: " << i; - EXPECT_EQ(address_queue.size(), 2u) << "case: " << i; + test_case.type, + url::Origin::Create( + GURL(test_case.valid_origin_two_accounts_with_port)), + &requesting_origin, &address_queue)); + EXPECT_EQ(requesting_origin.GetURL(), GURL("https://test.com:123")); + EXPECT_EQ(address_queue.size(), 2u); EXPECT_TRUE(base::EqualsCaseInsensitiveASCII(address_queue.front(), - cases[i].account1)) - << "case: " << i; + test_case.account1)); address_queue.pop(); EXPECT_TRUE(base::EqualsCaseInsensitiveASCII(address_queue.front(), - cases[i].account2)) - << "case: " << i; + test_case.account2)); } } @@ -295,32 +269,28 @@ TEST(PermissionUtilsUnitTest, GetSubRequestOrigin) { "https://test.com__BrG44HdsEhzapvs8bEqzvkq4egwevS3fRE6ze2ENo6S8", "https://" "test.com__BrG44HdsEhzapvs8bEqzvkq4egwevS3fRE6ze2ENo6S8:123"}}; - for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { - EXPECT_FALSE(GetSubRequestOrigin(cases[i].type, url::Origin(), - cases[i].account, &new_origin)) - << "case: " << i; + for (auto& test_case : cases) { + SCOPED_TRACE(testing::Message() << test_case.account); + + EXPECT_FALSE(GetSubRequestOrigin(test_case.type, url::Origin(), + test_case.account, &new_origin)); // invalid type EXPECT_FALSE(GetSubRequestOrigin(permissions::RequestType::kGeolocation, - old_origin, cases[i].account, &new_origin)) - << "case: " << i; + old_origin, test_case.account, + &new_origin)); EXPECT_FALSE( - GetSubRequestOrigin(cases[i].type, old_origin, "", &new_origin)) - << "case: " << i; - EXPECT_FALSE(GetSubRequestOrigin(cases[i].type, old_origin, - cases[i].account, nullptr)) - << "case: " << i; - - EXPECT_TRUE(GetSubRequestOrigin(cases[i].type, old_origin, cases[i].account, - &new_origin)) - << "case: " << i; - EXPECT_EQ(new_origin, - url::Origin::Create(GURL(cases[i].expected_new_origin))); - EXPECT_TRUE(GetSubRequestOrigin(cases[i].type, old_origin_with_port, - cases[i].account, &new_origin)) - << "case: " << i; + GetSubRequestOrigin(test_case.type, old_origin, "", &new_origin)); + EXPECT_FALSE(GetSubRequestOrigin(test_case.type, old_origin, + test_case.account, nullptr)); + + EXPECT_TRUE(GetSubRequestOrigin(test_case.type, old_origin, + test_case.account, &new_origin)); EXPECT_EQ(new_origin, - url::Origin::Create(GURL(cases[i].expected_new_origin_with_port))) - << "case: " << i; + url::Origin::Create(GURL(test_case.expected_new_origin))); + EXPECT_TRUE(GetSubRequestOrigin(test_case.type, old_origin_with_port, + test_case.account, &new_origin)); + EXPECT_EQ(new_origin, url::Origin::Create( + GURL(test_case.expected_new_origin_with_port))); } } @@ -343,9 +313,12 @@ TEST(PermissionUtilsUnitTest, GetConnectWithSiteWebUIURL) { "?addr=BrG44HdsEhzapvs8bEqzvkq4egwevS3fRE6ze2ENo6S8&addr=" "JDqrvDz8d8tFCADashbUKQDKfJZFobNy13ugN65t1wvV&origin-spec=https://" "a.test.com:123&etld-plus-one=test.com#connectWithSite"}}; - for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { - GURL url_out = GetConnectWithSiteWebUIURL(base_url, cases[i].addrs, origin); - EXPECT_EQ(url_out.spec(), cases[i].expected_out_url) << "case: " << i; + for (auto& test_case : cases) { + SCOPED_TRACE(testing::Message() << test_case.addrs[0]); + + GURL url_out = + GetConnectWithSiteWebUIURL(base_url, test_case.addrs, origin); + EXPECT_EQ(url_out.spec(), test_case.expected_out_url); } } diff --git a/components/brave_wallet/browser/sns_resolver_task.cc b/components/brave_wallet/browser/sns_resolver_task.cc index 4d9df75a222c..5ba7bcd5f4df 100644 --- a/components/brave_wallet/browser/sns_resolver_task.cc +++ b/components/brave_wallet/browser/sns_resolver_task.cc @@ -11,6 +11,7 @@ #include #include "base/base64.h" +#include "base/containers/span.h" #include "base/no_destructor.h" #include "base/notreached.h" #include "base/numerics/byte_conversions.h" @@ -497,18 +498,17 @@ SnsNamehash GetHashedName(const std::string& prefix, const std::string& name) { // https://github.com/Bonfida/solana-program-library/blob/6e3be3eedad3a7f4a83c1b7cd5f17f89231e0bca/name-service/js/src/constants.ts#L13 constexpr char kHashPrefix[] = "SPL Name Service"; const std::string input = kHashPrefix + prefix + name; - return crypto::SHA256Hash(base::as_bytes(base::make_span(input))); + return crypto::SHA256Hash(base::as_byte_span(input)); } // https://github.com/Bonfida/name-tokenizer#mint std::optional GetMintAddress( const SolanaAddress& domain_address) { constexpr char kMintPrefix[] = "tokenized_name"; - auto mint_prefix_bytes = base::make_span(kMintPrefix); + auto mint_prefix_bytes = base::span_from_cstring(kMintPrefix); std::vector> seeds; - seeds.emplace_back(mint_prefix_bytes.begin(), - std::prev(mint_prefix_bytes.end())); // trims last zero + seeds.emplace_back(mint_prefix_bytes.begin(), mint_prefix_bytes.end()); seeds.emplace_back(domain_address.bytes()); // https://github.com/Bonfida/name-tokenizer#program-id @@ -928,7 +928,7 @@ void SnsResolverTask::OnFetchNextRecord(APIRequestResult api_request_result) { if (cur_item.record == kSnsSolRecord) { DCHECK_EQ(task_type_, TaskType::kResolveWalletAddress); if (auto sol_record_payload_address = ParseAndVerifySolRecordData( - cur_item, base::make_span(record_name_registry_state->data), + cur_item, base::span(record_name_registry_state->data), domain_name_registry_state_->owner)) { SetAddressResult(*sol_record_payload_address); return; @@ -939,8 +939,7 @@ void SnsResolverTask::OnFetchNextRecord(APIRequestResult api_request_result) { SolanaAddress domain_owner = nft_owner_ ? *nft_owner_ : domain_name_registry_state_->owner; auto registry_string = ParseAndVerifyTextRecordData( - cur_item, base::make_span(record_name_registry_state->data), - domain_owner); + cur_item, base::span(record_name_registry_state->data), domain_owner); if (registry_string) { GURL ipfs_resolved_url; GURL url = (cur_item.record == kSnsIpfsRecord && diff --git a/components/brave_wallet/browser/solana_instruction_builder.cc b/components/brave_wallet/browser/solana_instruction_builder.cc index 72aad492afc1..5b3e8f7b119c 100644 --- a/components/brave_wallet/browser/solana_instruction_builder.cc +++ b/components/brave_wallet/browser/solana_instruction_builder.cc @@ -3,18 +3,14 @@ * 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/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_wallet/browser/solana_instruction_builder.h" #include #include #include +#include "base/containers/span.h" +#include "base/containers/span_writer.h" #include "brave/components/brave_wallet/browser/simple_hash_client.h" #include "brave/components/brave_wallet/browser/solana_account_meta.h" #include "brave/components/brave_wallet/browser/solana_instruction.h" @@ -28,23 +24,12 @@ namespace { // Solana uses bincode::serialize when encoding instruction data, which encodes // unsigned numbers in little endian byte order. template -void UintToLEBytes(T val, std::vector* bytes) { - static_assert( - std::is_same::value || std::is_same::value, - "Incorrect type passed to function UintToLEBytes."); - - DCHECK(bytes); - size_t vec_size = sizeof(T) / sizeof(uint8_t); - *bytes = std::vector(vec_size); - - uint8_t* ptr = reinterpret_cast(&val); - for (size_t i = 0; i < vec_size; i++) { -#if defined(ARCH_CPU_LITTLE_ENDIAN) - bytes->at(i) = *ptr++; -#else - bytes->at(vec_size - 1 - i) = *ptr++; -#endif - } + requires(std::same_as || std::same_as) +void UintToLEBytes(std::vector& bytes, T val) { + auto val_span = base::byte_span_from_ref(val); + bytes.resize(bytes.size() + val_span.size()); + + base::as_writable_byte_span(bytes).last(val_span.size()).copy_from(val_span); } } // namespace @@ -70,13 +55,9 @@ std::optional Transfer(const std::string& from_pubkey, // Instruction data is consisted of u32 instruction index and u64 lamport. std::vector instruction_data; UintToLEBytes( - static_cast(mojom::SolanaSystemInstruction::kTransfer), - &instruction_data); - - std::vector lamport_bytes; - UintToLEBytes(lamport, &lamport_bytes); - instruction_data.insert(instruction_data.end(), lamport_bytes.begin(), - lamport_bytes.end()); + instruction_data, + static_cast(mojom::SolanaSystemInstruction::kTransfer)); + UintToLEBytes(instruction_data, lamport); return SolanaInstruction( mojom::kSolanaSystemProgramId, @@ -124,11 +105,7 @@ std::optional TransferChecked( std::vector instruction_data = { static_cast(mojom::SolanaTokenInstruction::kTransferChecked)}; - std::vector amount_bytes; - UintToLEBytes(amount, &amount_bytes); - instruction_data.insert(instruction_data.end(), amount_bytes.begin(), - amount_bytes.end()); - + UintToLEBytes(instruction_data, amount); instruction_data.emplace_back(decimals); std::vector account_metas = { @@ -198,10 +175,8 @@ SolanaInstruction SetComputeUnitLimit(uint32_t units) { std::vector instruction_data = {static_cast( mojom::SolanaComputeBudgetInstruction::kSetComputeUnitLimit)}; - std::vector units_bytes; - UintToLEBytes(units, &units_bytes); - instruction_data.insert(instruction_data.end(), units_bytes.begin(), - units_bytes.end()); + UintToLEBytes(instruction_data, units); + return SolanaInstruction(mojom::kSolanaComputeBudgetProgramId, {}, instruction_data); } @@ -212,10 +187,7 @@ SolanaInstruction SetComputeUnitPrice(uint64_t price) { std::vector instruction_data = {static_cast( mojom::SolanaComputeBudgetInstruction::kSetComputeUnitPrice)}; - std::vector price_bytes; - UintToLEBytes(price, &price_bytes); - instruction_data.insert(instruction_data.end(), price_bytes.begin(), - price_bytes.end()); + UintToLEBytes(instruction_data, price); return SolanaInstruction(mojom::kSolanaComputeBudgetProgramId, {}, instruction_data); @@ -256,21 +228,13 @@ std::optional Transfer( instruction_data.insert(instruction_data.end(), creator_hash_bytes.begin(), creator_hash_bytes.end()); - std::vector tempVec; - // Nonce - tempVec.clear(); // Use leaf.index for nonce like the example // https://solana.com/developers/guides/javascript/compressed-nfts#build-the-transfer-instruction - UintToLEBytes(static_cast(proof.leaf_index), &tempVec); - instruction_data.insert(instruction_data.end(), tempVec.begin(), - tempVec.end()); + UintToLEBytes(instruction_data, static_cast(proof.leaf_index)); // Index - tempVec.clear(); - UintToLEBytes(proof.leaf_index, &tempVec); - instruction_data.insert(instruction_data.end(), tempVec.begin(), - tempVec.end()); + UintToLEBytes(instruction_data, proof.leaf_index); // Create account metas. std::vector account_metas({ @@ -292,8 +256,7 @@ std::optional Transfer( } size_t end = proof.proof.size() - proof.canopy_depth; for (size_t i = 0; i < end; ++i) { - account_metas.push_back( - SolanaAccountMeta(proof.proof[i], std::nullopt, false, false)); + account_metas.emplace_back(proof.proof[i], std::nullopt, false, false); } return SolanaInstruction(mojom::kSolanaBubbleGumProgramId, diff --git a/components/brave_wallet/browser/solana_message.cc b/components/brave_wallet/browser/solana_message.cc index 11447e4bb7b7..55fff8989f0c 100644 --- a/components/brave_wallet/browser/solana_message.cc +++ b/components/brave_wallet/browser/solana_message.cc @@ -393,7 +393,7 @@ std::optional SolanaMessage::Deserialize( return std::nullopt; } auto account_key = SolanaAddress::FromBytes( - base::make_span(bytes).subspan(bytes_index, kSolanaPubkeySize)); + base::span(bytes).subspan(bytes_index, kSolanaPubkeySize)); if (!account_key) { return std::nullopt; } diff --git a/components/brave_wallet/browser/solana_message_address_table_lookup.cc b/components/brave_wallet/browser/solana_message_address_table_lookup.cc index 20165914b7e6..092e707d1fca 100644 --- a/components/brave_wallet/browser/solana_message_address_table_lookup.cc +++ b/components/brave_wallet/browser/solana_message_address_table_lookup.cc @@ -80,7 +80,7 @@ SolanaMessageAddressTableLookup::Deserialize(const std::vector& bytes, return std::nullopt; } auto account_key = SolanaAddress::FromBytes( - base::make_span(bytes).subspan(*bytes_index, kSolanaPubkeySize)); + base::span(bytes).subspan(*bytes_index, kSolanaPubkeySize)); if (!account_key) { return std::nullopt; } diff --git a/components/brave_wallet/browser/wallet_data_files_installer.cc b/components/brave_wallet/browser/wallet_data_files_installer.cc index 96b73cf43284..2b8b86ebe64d 100644 --- a/components/brave_wallet/browser/wallet_data_files_installer.cc +++ b/components/brave_wallet/browser/wallet_data_files_installer.cc @@ -122,9 +122,8 @@ base::FilePath WalletDataFilesInstallerPolicy::GetRelativeInstallDir() const { } void WalletDataFilesInstallerPolicy::GetHash(std::vector* hash) const { - UNSAFE_TODO(hash->assign( - kWalletDataFilesSha2Hash, - kWalletDataFilesSha2Hash + std::size(kWalletDataFilesSha2Hash))); + hash->assign(std::begin(kWalletDataFilesSha2Hash), + std::end(kWalletDataFilesSha2Hash)); } std::string WalletDataFilesInstallerPolicy::GetName() const {