Skip to content

Commit

Permalink
Merge pull request #8648 from brave/web3_provider_request
Browse files Browse the repository at this point in the history
Replaces input and output of web3 provider object from strings to values
  • Loading branch information
SergeyZhukovsky authored Apr 28, 2021
2 parents 83e8565 + 8fe32e5 commit 33d8be7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 38 deletions.
33 changes: 25 additions & 8 deletions components/brave_wallet/renderer/brave_wallet_js_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

#include <utility>

#include "base/json/json_writer.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/components/brave_wallet/renderer/brave_wallet_response_helpers.h"
#include "brave/components/brave_wallet/renderer/web3_provider_constants.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/v8_value_converter.h"
#include "gin/arguments.h"
#include "gin/function_template.h"
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
Expand Down Expand Up @@ -91,9 +93,22 @@ void BraveWalletJSHandler::BindFunctionToObject(
.Check();
}

v8::Local<v8::Promise> BraveWalletJSHandler::Request(v8::Isolate* isolate,
const std::string& input) {
if (!EnsureConnected())
v8::Local<v8::Promise> BraveWalletJSHandler::Request(
v8::Isolate* isolate,
v8::Local<v8::Value> input) {
if (!EnsureConnected() || !input->IsObject())
return v8::Local<v8::Promise>();

std::unique_ptr<base::Value> out(
content::V8ValueConverter::Create()->FromV8Value(
input, isolate->GetCurrentContext()));

base::DictionaryValue* out_dict;
if (!out || !out->is_dict() || !out->GetAsDictionary(&out_dict))
return v8::Local<v8::Promise>();

std::string formed_input;
if (!base::JSONWriter::Write(*out_dict, &formed_input))
return v8::Local<v8::Promise>();

v8::MaybeLocal<v8::Promise::Resolver> resolver =
Expand All @@ -104,7 +119,7 @@ v8::Local<v8::Promise> BraveWalletJSHandler::Request(v8::Isolate* isolate,
auto context_old(
v8::Global<v8::Context>(isolate, isolate->GetCurrentContext()));
brave_wallet_provider_->Request(
input,
formed_input,
base::BindOnce(&BraveWalletJSHandler::OnRequest, base::Unretained(this),
std::move(promise_resolver), isolate,
std::move(context_old)));
Expand All @@ -126,20 +141,22 @@ void BraveWalletJSHandler::OnRequest(
v8::Context::Scope context_scope(context);

v8::Local<v8::Promise::Resolver> resolver = promise_resolver.Get(isolate);
v8::Local<v8::String> result;
bool reject = http_code != 200;
ProviderErrors code = ProviderErrors::kDisconnected;
std::string message;
std::string formed_response;
std::unique_ptr<base::Value> formed_response;
if (reject) {
code = ProviderErrors::kUnsupportedMethod;
message = "HTTP Status code: " + base::NumberToString(http_code);
formed_response = FormProviderResponse(code, message);
} else {
formed_response = FormProviderResponse(response, &reject);
}
result = v8::String::NewFromUtf8(isolate, formed_response.c_str())
.ToLocalChecked();
v8::Local<v8::Value> result;
if (formed_response) {
result = content::V8ValueConverter::Create()->ToV8Value(
formed_response.get(), context);
}

if (reject) {
ALLOW_UNUSED_LOCAL(resolver->Reject(context, result));
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/renderer/brave_wallet_js_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BraveWalletJSHandler {

// A function to be called from JS
v8::Local<v8::Promise> Request(v8::Isolate* isolate,
const std::string& input);
v8::Local<v8::Value> input);
void OnRequest(v8::Global<v8::Promise::Resolver> promise_resolver,
v8::Isolate* isolate,
v8::Global<v8::Context> context_old,
Expand Down
31 changes: 13 additions & 18 deletions components/brave_wallet/renderer/brave_wallet_response_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@

#include "brave/components/brave_wallet/renderer/brave_wallet_response_helpers.h"

#include <utility>

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/values.h"

namespace brave_wallet {

std::string FormProviderResponse(ProviderErrors code,
const std::string& message) {
std::unique_ptr<base::Value> FormProviderResponse(ProviderErrors code,
const std::string& message) {
std::string formed_response;
base::DictionaryValue json_root;
json_root.SetIntKey("code", static_cast<int>(code));
json_root.SetStringKey("message", message);
base::JSONWriter::Write(json_root, &formed_response);
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
result->SetIntKey("code", static_cast<int>(code));
result->SetStringKey("message", message);

return formed_response;
return std::move(result);
}

std::string FormProviderResponse(const std::string& controller_response,
bool* reject) {
std::unique_ptr<base::Value> FormProviderResponse(
const std::string& controller_response,
bool* reject) {
DCHECK(reject);
base::JSONReader::ValueWithError value_with_error =
base::JSONReader::ReadAndReturnValueWithError(
Expand All @@ -38,24 +38,19 @@ std::string FormProviderResponse(const std::string& controller_response,
return FormProviderResponse(code, message);
}

std::string formed_response;
// Check, do we have any error message
const base::Value* error = response->FindKey("error");
if (error) {
base::JSONWriter::Write(*error, &formed_response);
*reject = true;

return formed_response;
return base::Value::ToUniquePtrValue(error->Clone());
}
*reject = false;

// We have a result
const base::Value* result = response->FindKey("result");
DCHECK(result);
if (result)
base::JSONWriter::Write(*result, &formed_response);

return formed_response;
return base::Value::ToUniquePtrValue(result->Clone());
}

} // namespace brave_wallet
11 changes: 7 additions & 4 deletions components/brave_wallet/renderer/brave_wallet_response_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_RENDERER_BRAVE_WALLET_RESPONSE_HELPERS_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_RENDERER_BRAVE_WALLET_RESPONSE_HELPERS_H_

#include <memory>
#include <string>

#include "base/values.h"
#include "brave/components/brave_wallet/renderer/web3_provider_constants.h"

namespace brave_wallet {

std::string FormProviderResponse(ProviderErrors code,
const std::string& message);
std::string FormProviderResponse(const std::string& controller_response,
bool* reject);
std::unique_ptr<base::Value> FormProviderResponse(ProviderErrors code,
const std::string& message);
std::unique_ptr<base::Value> FormProviderResponse(
const std::string& controller_response,
bool* reject);

} // namespace brave_wallet

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
* 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/renderer/brave_wallet_response_helpers.h"
#include <memory>
#include <string>

#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "brave/components/brave_wallet/renderer/brave_wallet_response_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace brave_wallet {

TEST(BraveWalletResponseHelpersTest, ResponseCode400) {
ProviderErrors code = ProviderErrors::kUnsupportedMethod;
std::string message = "HTTP Status code: " + base::NumberToString(400);
ASSERT_EQ(FormProviderResponse(code, message),
"{\"code\":4200,\"message\":\"HTTP Status code: 400\"}");
std::unique_ptr<base::Value> result = FormProviderResponse(code, message);
ASSERT_TRUE(result != nullptr);
const base::Value* result_code = result->FindKey("code");
const base::Value* result_message = result->FindKey("message");
ASSERT_TRUE(result_code && result_code->is_int());
ASSERT_TRUE(result_message && result_message->is_string());

ASSERT_EQ(result_code->GetInt(), static_cast<int>(code));
ASSERT_EQ(result_message->GetString(), message);
}

TEST(BraveWalletResponseHelpersTest, ErrorResponse) {
Expand All @@ -22,18 +33,30 @@ TEST(BraveWalletResponseHelpersTest, ErrorResponse) {
"\"message\":\"The method eth_accountsq does not exist/is not "
"available\"}}";
bool reject = false;
ASSERT_EQ(FormProviderResponse(response, &reject),
"{\"code\":-32601,\"message\":\"The method eth_accountsq does not "
"exist/is not available\"}");
std::unique_ptr<base::Value> result = FormProviderResponse(response, &reject);

ASSERT_TRUE(result != nullptr);
ASSERT_TRUE(reject);
const base::Value* result_code = result->FindKey("code");
const base::Value* result_message = result->FindKey("message");
ASSERT_TRUE(result_code && result_code->is_int());
ASSERT_TRUE(result_message && result_message->is_string());

ASSERT_EQ(result_code->GetInt(), -32601);
ASSERT_EQ(result_message->GetString(),
"The method eth_accountsq does not exist/is not available");
}

TEST(BraveWalletResponseHelpersTest, CorrectResultResponse) {
std::string response =
"{\"jsonrpc\":\"2.0\",\"id\":2025678280,\"result\":\"0xbb4323\"}";
bool reject = false;
ASSERT_EQ(FormProviderResponse(response, &reject), "\"0xbb4323\"");
std::unique_ptr<base::Value> result = FormProviderResponse(response, &reject);

ASSERT_TRUE(result != nullptr);
ASSERT_TRUE(result->is_string());
ASSERT_FALSE(reject);
ASSERT_EQ(result->GetString(), "0xbb4323");
}

} // namespace brave_wallet

0 comments on commit 33d8be7

Please sign in to comment.