1. Add solana.publicKey and solana.signTransaction

2. Move solana_utils to common
3. Move solana constants to common
This commit is contained in:
Anthony Tseng
2022-03-28 17:22:15 -07:00
parent a3e822d8ab
commit cb019cafd5
34 changed files with 294 additions and 64 deletions
+3 -15
View File
@@ -111,7 +111,6 @@ static_library("browser") {
":constants",
":hd_keyring",
":pref_names",
":solana_utils",
":transaction",
":utils",
"//base",
@@ -124,6 +123,7 @@ static_library("browser") {
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:common_constants",
"//brave/components/brave_wallet/common:mojom",
"//brave/components/brave_wallet/common:solana_utils",
"//brave/components/json/rs:cxx",
"//brave/components/json/rs:cxx_gen",
"//brave/components/resources:strings_grit",
@@ -207,18 +207,6 @@ source_set("utils") {
public_deps = [ "//brave/components/brave_wallet/common" ]
}
source_set("solana_utils") {
sources = [
"solana_utils.cc",
"solana_utils.h",
]
deps = [
"//base",
"//brave/third_party/bitcoin-core", # for base58
]
}
source_set("hd_keyring") {
sources = [
"ethereum_keyring.cc",
@@ -232,8 +220,6 @@ source_set("hd_keyring") {
]
deps = [
":constants",
":solana_utils",
":transaction",
"internal:hd_key",
"//base",
@@ -241,7 +227,9 @@ source_set("hd_keyring") {
"//brave/components/bls/rs:cxx",
"//brave/components/bls/rs:cxx_gen",
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:common_constants",
"//brave/components/brave_wallet/common:mojom",
"//brave/components/brave_wallet/common:solana_utils",
"//crypto",
]
@@ -652,21 +652,6 @@ constexpr char kCryptoEthAddressKey[] = "crypto.ETH.address";
// ERC-165 identifier for ERC721 interface.
constexpr char kERC721InterfaceId[] = "0x80ac58cd";
constexpr int kSolanaKeypairSize = 64;
constexpr int kSolanaPrikeySize = 32;
constexpr int kSolanaPubkeySize = 32;
constexpr int kSolanaBlockhashSize = 32;
// 1232 = 1280(IPv6 minimum MTU) - 40(size of the IPv6 header) - 8(size of the
// fragment header)
constexpr size_t kSolanaMaxTxSize = 1232;
constexpr char kSolanaSystemProgramId[] = "11111111111111111111111111111111";
constexpr char kSolanaTokenProgramId[] =
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
constexpr char kSolanaAssociatedTokenProgramId[] =
"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
constexpr char kSolanaSysvarRentProgramId[] =
"SysvarRent111111111111111111111111111111111";
constexpr char kEthereumPrefKey[] = "ethereum";
constexpr char kFilecoinPrefKey[] = "filecoin";
constexpr char kSolanaPrefKey[] = "solana";
@@ -17,7 +17,6 @@
#include "base/strings/utf_string_conversions.h"
#include "base/value_iterators.h"
#include "base/values.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.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/ethereum_keyring.h"
@@ -26,11 +25,12 @@
#include "brave/components/brave_wallet/browser/json_rpc_service.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
#include "brave/components/brave_wallet/browser/solana_keyring.h"
#include "brave/components/brave_wallet/browser/solana_utils.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom-forward.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom-shared.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/eth_address.h"
#include "brave/components/brave_wallet/common/hex_utils.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "components/grit/brave_components_strings.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
@@ -11,7 +11,7 @@
#include "base/base64.h"
#include "base/check.h"
#include "base/values.h"
#include "brave/components/brave_wallet/browser/solana_utils.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
namespace brave_wallet {
@@ -8,9 +8,9 @@
#include <type_traits>
#include <utility>
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/solana_account_meta.h"
#include "brave/components/brave_wallet/browser/solana_instruction.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/brave_wallet_types.h"
#include "build/build_config.h"
@@ -8,9 +8,9 @@
#include <string>
#include <vector>
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/solana_account_meta.h"
#include "brave/components/brave_wallet/browser/solana_instruction.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace brave_wallet {
@@ -10,8 +10,8 @@
#include "base/json/json_reader.h"
#include "base/test/gtest_util.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/solana_account_meta.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace brave_wallet {
@@ -8,9 +8,9 @@
#include <memory>
#include <utility>
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/internal/hd_key_ed25519.h"
#include "brave/components/brave_wallet/browser/solana_utils.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "brave/components/brave_wallet/rust/lib.rs.h"
#include "crypto/sha2.h"
@@ -6,10 +6,10 @@
#include <memory>
#include "base/strings/string_number_conversions.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/solana_keyring.h"
#include "brave/components/brave_wallet/browser/solana_utils.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace brave_wallet {
@@ -10,8 +10,8 @@
#include "base/check.h"
#include "base/values.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/solana_utils.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
namespace brave_wallet {
@@ -9,9 +9,9 @@
#include "base/json/json_reader.h"
#include "base/test/gtest_util.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/solana_account_meta.h"
#include "brave/components/brave_wallet/browser/solana_instruction.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace brave_wallet {
@@ -6,6 +6,7 @@
#include "brave/components/brave_wallet/browser/solana_provider_impl.h"
#include <utility>
#include <vector>
#include "base/notreached.h"
@@ -25,12 +26,40 @@ void SolanaProviderImpl::Connect(absl::optional<base::Value> arg,
}
void SolanaProviderImpl::Disconnect() {
NOTIMPLEMENTED();
// NOTIMPLEMENTED();
}
void SolanaProviderImpl::IsConnected(IsConnectedCallback callback) {
NOTIMPLEMENTED();
// NOTIMPLEMENTED();
std::move(callback).Run(false);
}
void SolanaProviderImpl::GetPublicKey(GetPublicKeyCallback callback) {
std::move(callback).Run("BrG44HdsEhzapvs8bEqzvkq4egwevS3fRE6ze2ENo6S8");
// std::move(callback).Run("");
}
void SolanaProviderImpl::SignTransaction(
const std::string& encoded_serialized_msg,
SignTransactionCallback callback) {
std::vector<uint8_t> result = {
1, 231, 78, 150, 120, 219, 234, 88, 44, 144, 225, 53, 221, 88, 82,
59, 51, 62, 211, 225, 231, 182, 123, 231, 229, 201, 48, 30, 137, 119,
233, 102, 88, 31, 65, 88, 147, 197, 72, 166, 241, 126, 26, 59, 239,
64, 196, 116, 28, 17, 124, 0, 123, 13, 28, 65, 242, 241, 226, 46,
227, 55, 234, 251, 10, 1, 0, 1, 2, 161, 51, 89, 91, 115, 210,
217, 212, 76, 159, 171, 200, 40, 150, 157, 70, 197, 71, 24, 44, 209,
108, 143, 4, 58, 251, 215, 62, 201, 172, 159, 197, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 84,
174, 253, 228, 77, 50, 164, 215, 178, 46, 88, 242, 49, 114, 246, 244,
48, 9, 18, 36, 41, 160, 254, 174, 6, 207, 115, 11, 58, 220, 167,
1, 1, 2, 0, 0, 12, 2, 0, 0, 0, 100, 0, 0, 0, 0,
0, 0, 0};
std::move(callback).Run(mojom::SolanaProviderError::kSuccess, "", result);
// NOTIMPLEMENTED();
// std::move(callback).Run(mojom::SolanaProviderError::kInternalError, "",
// std::vector<uint8_t>());
}
} // namespace brave_wallet
@@ -6,6 +6,8 @@
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_SOLANA_PROVIDER_IMPL_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_SOLANA_PROVIDER_IMPL_H_
#include <string>
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
namespace brave_wallet {
@@ -21,6 +23,9 @@ class SolanaProviderImpl final : public mojom::SolanaProvider {
ConnectCallback callback) override;
void Disconnect() override;
void IsConnected(IsConnectedCallback callback) override;
void GetPublicKey(GetPublicKeyCallback callback) override;
void SignTransaction(const std::string& encoded_serialized_msg,
SignTransactionCallback callback) override;
private:
};
@@ -10,11 +10,11 @@
#include "base/base64.h"
#include "base/check.h"
#include "base/strings/string_number_conversions.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/solana_instruction.h"
#include "brave/components/brave_wallet/browser/solana_utils.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
namespace brave_wallet {
@@ -14,11 +14,11 @@
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/browser/brave_wallet_prefs.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/solana_account_meta.h"
#include "brave/components/brave_wallet/browser/solana_instruction.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/features.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -15,6 +15,7 @@
#include "brave/components/brave_wallet/browser/solana_keyring.h"
#include "brave/components/brave_wallet/browser/solana_tx_meta.h"
#include "brave/components/brave_wallet/browser/solana_tx_state_manager.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/brave_wallet_types.h"
#include "components/grit/brave_components_strings.h"
#include "ui/base/l10n/l10n_util.h"
@@ -19,6 +19,7 @@
#include "brave/components/brave_wallet/browser/solana_transaction.h"
#include "brave/components/brave_wallet/browser/solana_tx_meta.h"
#include "brave/components/brave_wallet/browser/tx_service.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/brave_wallet_types.h"
#include "brave/components/brave_wallet/common/features.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
@@ -11,7 +11,7 @@
#include "base/json/json_reader.h"
#include "base/values.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace brave_wallet {
@@ -16,6 +16,7 @@
#include "brave/components/brave_wallet/browser/solana_instruction.h"
#include "brave/components/brave_wallet/browser/solana_transaction.h"
#include "brave/components/brave_wallet/browser/solana_tx_meta.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/brave_wallet_types.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
@@ -51,7 +51,6 @@ source_set("brave_wallet_unit_tests") {
"//brave/components/brave_wallet/browser/solana_tx_manager_unittest.cc",
"//brave/components/brave_wallet/browser/solana_tx_meta_unittest.cc",
"//brave/components/brave_wallet/browser/solana_tx_state_manager_unittest.cc",
"//brave/components/brave_wallet/browser/solana_utils_unittest.cc",
"//brave/components/brave_wallet/browser/swap_response_parser_unittest.cc",
"//brave/components/brave_wallet/browser/swap_service_unittest.cc",
"//brave/components/brave_wallet/browser/tx_meta_unittest.cc",
@@ -65,12 +64,13 @@ source_set("brave_wallet_unit_tests") {
"//brave/components/brave_wallet/browser:ethereum_permission_utils",
"//brave/components/brave_wallet/browser:hd_keyring",
"//brave/components/brave_wallet/browser:pref_names",
"//brave/components/brave_wallet/browser:solana_utils",
"//brave/components/brave_wallet/browser:transaction",
"//brave/components/brave_wallet/browser:utils",
"//brave/components/brave_wallet/browser/internal:hd_key",
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:common_constants",
"//brave/components/brave_wallet/common:mojom",
"//brave/components/brave_wallet/common:solana_utils",
"//brave/components/ipfs",
"//brave/components/resources:strings_grit",
"//components/prefs",
+16
View File
@@ -50,12 +50,25 @@ static_library("common") {
source_set("common_constants") {
sources = [
"brave_wallet_constants.h",
"web3_provider_constants.cc",
"web3_provider_constants.h",
]
deps = [ ":mojom" ]
}
source_set("solana_utils") {
sources = [
"solana_utils.cc",
"solana_utils.h",
]
deps = [
"//base",
"//brave/third_party/bitcoin-core", # for base58
]
}
preprocess_if_expr("preprocess_mojo") {
deps = [ "//brave/components/brave_wallet/common:mojom_js" ]
in_folder = "$target_gen_dir"
@@ -82,13 +95,16 @@ source_set("unit_tests") {
"hash_utils_unittest.cc",
"hex_utils_unittest.cc",
"mem_utils_unittest.cc",
"solana_utils_unittest.cc",
"string_utils_unittest.cc",
"value_conversion_utils_unittest.cc",
]
deps = [
":common",
":common_constants",
":mojom",
":solana_utils",
"//base/test:test_support",
"//testing/gtest",
"//url",
+1
View File
@@ -1,5 +1,6 @@
include_rules = [
"+brave/third_party/argon2",
"+brave/third_party/bitcoin-core/src/src/base58.h",
]
specific_include_rules = {
@@ -125,12 +125,21 @@ interface BraveWalletProvider {
interface SolanaProvider {
// Optional {onlyIfTrusted: true}, when that flag is enable, we will only
// connect and emit a connect event if the application is trusted.
// It will returns base58 encoded public key when success
Connect(mojo_base.mojom.DictionaryValue? arg)
=> (SolanaProviderError error, string message, string public_key);
=> (SolanaProviderError error, string error_message, string public_key);
Disconnect();
[Sync]
IsConnected() => (bool is_connected);
// SignTransaction
// return base58 encoded public key
[Sync]
GetPublicKey() => (string public_key);
// It takes a base58 encoded serialized Message and return a serialized
// Transaction with signature in wire format.
SignTransaction(string encoded_serialized_msg)
=> (SolanaProviderError error, string error_message,
array<uint8> serialized_tx);
// SignAndSendTransaction
// SignAllTransactions
// SignMessage, hex or utf8 encoding
@@ -0,0 +1,29 @@
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_BRAVE_WALLET_CONSTANTS_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_BRAVE_WALLET_CONSTANTS_H_
namespace brave_wallet {
constexpr int kSolanaKeypairSize = 64;
constexpr int kSolanaSignatureSize = 64;
constexpr int kSolanaPrikeySize = 32;
constexpr int kSolanaPubkeySize = 32;
constexpr int kSolanaBlockhashSize = 32;
// 1232 = 1280(IPv6 minimum MTU) - 40(size of the IPv6 header) - 8(size of the
// fragment header)
constexpr size_t kSolanaMaxTxSize = 1232;
constexpr char kSolanaSystemProgramId[] = "11111111111111111111111111111111";
constexpr char kSolanaTokenProgramId[] =
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
constexpr char kSolanaAssociatedTokenProgramId[] =
"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
constexpr char kSolanaSysvarRentProgramId[] =
"SysvarRent111111111111111111111111111111111";
} // namespace brave_wallet
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_BRAVE_WALLET_CONSTANTS_H_
@@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/components/brave_wallet/browser/solana_utils.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "base/check.h"
#include "brave/third_party/bitcoin-core/src/src/base58.h"
@@ -3,8 +3,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_SOLANA_UTILS_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_SOLANA_UTILS_H_
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_SOLANA_UTILS_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_SOLANA_UTILS_H_
#include <string>
#include <vector>
@@ -25,4 +25,4 @@ std::string Base58Encode(const std::vector<uint8_t>& bytes);
} // namespace brave_wallet
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_SOLANA_UTILS_H_
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_SOLANA_UTILS_H_
@@ -3,13 +3,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/components/brave_wallet/browser/solana_utils.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
#include <map>
#include <vector>
#include "base/test/gtest_util.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace brave_wallet {
@@ -13,6 +13,7 @@ source_set("renderer") {
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:common_constants",
"//brave/components/brave_wallet/common:mojom",
"//brave/components/brave_wallet/common:solana_utils",
"//brave/components/brave_wallet/resources:ethereum_provider_generated_resources",
"//content/public/renderer",
"//gin",
@@ -10,9 +10,11 @@
#include "base/notreached.h"
#include "brave/components/brave_wallet/common/brave_wallet_response_helpers.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "brave/components/brave_wallet/common/web3_provider_constants.h"
#include "brave/components/brave_wallet/renderer/v8_helper.h"
#include "content/public/renderer/v8_value_converter.h"
#include "gin/array_buffer.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
@@ -27,11 +29,38 @@ JSSolanaProvider::JSSolanaProvider(bool use_native_wallet,
render_frame_(render_frame),
v8_value_converter_(content::V8ValueConverter::Create()) {
EnsureConnected();
v8_value_converter_->SetStrategy(&strategy_);
}
JSSolanaProvider::~JSSolanaProvider() = default;
gin::WrapperInfo JSSolanaProvider::kWrapperInfo = {gin::kEmbedderNativeGin};
// Convert Uint8Array to base58 encoded string
bool JSSolanaProvider::V8ConverterStrategy::FromV8ArrayBuffer(
v8::Local<v8::Object> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate) {
if (!value->IsTypedArray()) {
return false;
}
std::vector<uint8_t> bytes;
char* data = NULL;
size_t data_length = 0;
gin::ArrayBufferView view;
if (gin::ConvertFromV8(isolate, value.As<v8::ArrayBufferView>(), &view)) {
data = reinterpret_cast<char*>(view.bytes());
data_length = view.num_bytes();
bytes.assign(data, data + data_length);
}
if (!bytes.size())
return false;
std::unique_ptr<base::Value> new_value =
std::make_unique<base::Value>(Base58Encode(bytes));
*out = std::move(new_value);
return true;
}
// static
std::unique_ptr<JSSolanaProvider> JSSolanaProvider::Install(
bool use_native_wallet,
@@ -55,6 +84,7 @@ gin::ObjectTemplateBuilder JSSolanaProvider::GetObjectTemplateBuilder(
return gin::Wrappable<JSSolanaProvider>::GetObjectTemplateBuilder(isolate)
.SetProperty("isPhantom", &JSSolanaProvider::GetIsPhantom)
.SetProperty("isConnected", &JSSolanaProvider::GetIsConnected)
.SetProperty("publicKey", &JSSolanaProvider::GetPublicKey)
.SetMethod("connect", &JSSolanaProvider::Connect)
.SetMethod("disconnect", &JSSolanaProvider::Disconnect)
.SetMethod("signAndSendTransaction",
@@ -99,6 +129,25 @@ bool JSSolanaProvider::GetIsConnected(gin::Arguments* arguments) {
return is_connected;
}
v8::Local<v8::Value> JSSolanaProvider::GetPublicKey(gin::Arguments* arguments) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
std::string public_key;
if (!solana_provider_->GetPublicKey(&public_key) || public_key.empty())
return v8::Null(isolate);
const base::Value public_key_value(public_key);
std::vector<v8::Local<v8::Value>> args;
args.push_back(v8_value_converter_->ToV8Value(&public_key_value, context));
v8::MaybeLocal<v8::Value> public_key_result =
CallMethodOfObject(render_frame_->GetWebFrame(), u"solana",
u"createPublickey", std::move(args));
v8::Local<v8::Value> v8_public_key;
CHECK(GetProperty(context, public_key_result.ToLocalChecked(), u"publicKey")
.ToLocal(&v8_public_key));
return v8_public_key;
}
v8::Local<v8::Promise> JSSolanaProvider::Connect(gin::Arguments* arguments) {
if (!EnsureConnected())
return v8::Local<v8::Promise>();
@@ -142,6 +191,8 @@ v8::Local<v8::Promise> JSSolanaProvider::Connect(gin::Arguments* arguments) {
}
v8::Local<v8::Promise> JSSolanaProvider::Disconnect(gin::Arguments* arguments) {
if (!EnsureConnected())
return v8::Local<v8::Promise>();
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::MaybeLocal<v8::Promise::Resolver> resolver =
v8::Promise::Resolver::New(isolate->GetCurrentContext());
@@ -178,8 +229,38 @@ v8::Local<v8::Promise> JSSolanaProvider::Request(gin::Arguments* arguments) {
// Deprecated
v8::Local<v8::Promise> JSSolanaProvider::SignTransaction(
gin::Arguments* arguments) {
NOTIMPLEMENTED();
return v8::Local<v8::Promise>();
if (!EnsureConnected())
return v8::Local<v8::Promise>();
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::MaybeLocal<v8::Promise::Resolver> resolver =
v8::Promise::Resolver::New(isolate->GetCurrentContext());
if (resolver.IsEmpty()) {
return v8::Local<v8::Promise>();
}
v8::Local<v8::Value> transaction;
if (arguments->Length() != 1 || !arguments->GetNext(&transaction)) {
arguments->ThrowError();
return v8::Local<v8::Promise>();
}
v8::MaybeLocal<v8::Value> serialized_msg = CallMethodOfObject(
render_frame_->GetWebFrame(), transaction, u"serializeMessage",
std::vector<v8::Local<v8::Value>>());
std::unique_ptr<base::Value> encoded_msg = v8_value_converter_->FromV8Value(
serialized_msg.ToLocalChecked(), isolate->GetCurrentContext());
if (!encoded_msg->is_string())
return v8::Local<v8::Promise>();
auto global_context(
v8::Global<v8::Context>(isolate, isolate->GetCurrentContext()));
auto promise_resolver(
v8::Global<v8::Promise::Resolver>(isolate, resolver.ToLocalChecked()));
solana_provider_->SignTransaction(
encoded_msg->GetString(),
base::BindOnce(&JSSolanaProvider::OnSignTransaction,
weak_ptr_factory_.GetWeakPtr(), std::move(global_context),
std::move(promise_resolver), isolate));
return resolver.ToLocalChecked()->GetPromise();
}
// Deprecated
@@ -242,6 +323,41 @@ void JSSolanaProvider::OnConnect(
FireEvent(kConnectEvent, std::move(args));
}
void JSSolanaProvider::OnSignTransaction(
v8::Global<v8::Context> global_context,
v8::Global<v8::Promise::Resolver> promise_resolver,
v8::Isolate* isolate,
mojom::SolanaProviderError error,
const std::string& error_message,
const std::vector<uint8_t>& serialized_tx) {
v8::HandleScope handle_scope(isolate);
v8::MicrotasksScope microtasks(isolate,
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::Context> context = global_context.Get(isolate);
v8::Local<v8::Value> result;
v8::Local<v8::Value> v8_serialized_tx;
if (error == mojom::SolanaProviderError::kSuccess) {
// use @solana/web3.js and create Transaction from serialized tx
const base::Value serialized_tx_value(serialized_tx);
std::vector<v8::Local<v8::Value>> args;
args.push_back(
v8_value_converter_->ToV8Value(&serialized_tx_value, context));
v8::MaybeLocal<v8::Value> transaction_result =
CallMethodOfObject(render_frame_->GetWebFrame(), u"solana",
u"createTransaction", std::move(args));
result = transaction_result.ToLocalChecked();
} else {
std::unique_ptr<base::Value> formed_response =
GetProviderErrorDictionary(error, error_message);
result = v8_value_converter_->ToV8Value(formed_response.get(), context);
}
SendResponse(std::move(global_context), std::move(promise_resolver), isolate,
std::move(result),
error == mojom::SolanaProviderError::kSuccess);
}
void JSSolanaProvider::SendResponse(
v8::Global<v8::Context> global_context,
v8::Global<v8::Promise::Resolver> promise_resolver,
@@ -29,6 +29,16 @@ class JSSolanaProvider final : public gin::Wrappable<JSSolanaProvider> {
static gin::WrapperInfo kWrapperInfo;
class V8ConverterStrategy : public content::V8ValueConverter::Strategy {
public:
V8ConverterStrategy() = default;
~V8ConverterStrategy() override = default;
bool FromV8ArrayBuffer(v8::Local<v8::Object> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate) override;
};
static std::unique_ptr<JSSolanaProvider> Install(
bool use_native_wallet,
content::RenderFrame* render_frame,
@@ -47,6 +57,7 @@ class JSSolanaProvider final : public gin::Wrappable<JSSolanaProvider> {
bool GetIsPhantom(gin::Arguments* arguments);
bool GetIsConnected(gin::Arguments* arguments);
v8::Local<v8::Value> GetPublicKey(gin::Arguments* arguments);
v8::Local<v8::Promise> Connect(gin::Arguments* arguments);
v8::Local<v8::Promise> Disconnect(gin::Arguments* arguments);
v8::Local<v8::Promise> SignAndSendTransaction(gin::Arguments* arguments);
@@ -68,6 +79,13 @@ class JSSolanaProvider final : public gin::Wrappable<JSSolanaProvider> {
const std::string& error_message,
const std::string& public_key);
void OnSignTransaction(v8::Global<v8::Context> global_context,
v8::Global<v8::Promise::Resolver> promise_resolver,
v8::Isolate* isolate,
mojom::SolanaProviderError error,
const std::string& error_message,
const std::vector<uint8_t>& serialized_tx);
void SendResponse(v8::Global<v8::Context> global_context,
v8::Global<v8::Promise::Resolver> promise_resolver,
v8::Isolate* isolate,
@@ -77,6 +95,7 @@ class JSSolanaProvider final : public gin::Wrappable<JSSolanaProvider> {
bool use_native_wallet_ = false;
raw_ptr<content::RenderFrame> render_frame_ = nullptr;
std::unique_ptr<content::V8ValueConverter> v8_value_converter_;
V8ConverterStrategy strategy_;
mojo::Remote<mojom::SolanaProvider> solana_provider_;
base::WeakPtrFactory<JSSolanaProvider> weak_ptr_factory_{this};
};
+22 -2
View File
@@ -5,6 +5,8 @@
#include "brave/components/brave_wallet/renderer/v8_helper.h"
#include <utility>
#include "gin/converter.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "v8/include/v8-function.h"
@@ -39,9 +41,27 @@ v8::MaybeLocal<v8::Value> CallMethodOfObject(
v8::MicrotasksScope microtasks(v8::Isolate::GetCurrent(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::Value> object;
if (!GetProperty(context, context->Global(), object_name).ToLocal(&object)) {
return v8::Local<v8::Value>();
}
return CallMethodOfObject(web_frame, object, method_name, std::move(args));
}
v8::MaybeLocal<v8::Value> CallMethodOfObject(
blink::WebLocalFrame* web_frame,
v8::Local<v8::Value> object,
const std::u16string& method_name,
std::vector<v8::Local<v8::Value>>&& args) {
if (web_frame->IsProvisional())
return v8::Local<v8::Value>();
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
v8::Local<v8::Context> context = web_frame->MainWorldScriptContext();
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(v8::Isolate::GetCurrent(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::Value> method;
if (!GetProperty(context, context->Global(), object_name).ToLocal(&object) ||
!GetProperty(context, object, method_name).ToLocal(&method)) {
if (!GetProperty(context, object, method_name).ToLocal(&method)) {
return v8::Local<v8::Value>();
}
@@ -29,6 +29,12 @@ v8::MaybeLocal<v8::Value> CallMethodOfObject(
const std::u16string& method_name,
std::vector<v8::Local<v8::Value>>&& args);
v8::MaybeLocal<v8::Value> CallMethodOfObject(
blink::WebLocalFrame* web_frame,
v8::Local<v8::Value> object,
const std::u16string& method_name,
std::vector<v8::Local<v8::Value>>&& args);
} // namespace brave_wallet
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_RENDERER_V8_HELPER_H_
@@ -72,6 +72,9 @@ function solanaSetup() {
result.publicKey = new solanaWeb3.PublicKey(base58Str)
return result
}
window.solana.createTransaction = function createTransaction(serializedTx) {
return solanaWeb3.Transaction.from(new Uint8Array(serializedTx))
}
const EventEmitter = require('events')
var SolanaEventEmitter = new EventEmitter()
window.solana.on = SolanaEventEmitter.on
+1 -1
View File
@@ -28,8 +28,8 @@ static_library("bitcoin-core") {
public_configs = [ ":external_config" ]
visibility = [
"//brave/components/brave_wallet/browser:solana_utils", # for base58
"//brave/components/brave_wallet/browser/internal:hd_key",
"//brave/components/brave_wallet/common:solana_utils", # for base58
]
# Do NOT add any files here without security review