Skip to content

Commit

Permalink
Merge pull request #14057 from brave/sszaloki-23849-implement-uphold-…
Browse files Browse the repository at this point in the history
…capabilities-spec-change
  • Loading branch information
szilardszaloki authored Jul 7, 2022
2 parents 9ff7720 + 16551af commit d9addf9
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ void GetCapabilities::OnRequest(const type::UrlResponse& response,

Capabilities capabilities;
if (capability_map.count("receives") && capability_map.count("sends")) {
capabilities.can_receive = capability_map["receives"];
capabilities.can_send = capability_map["sends"];
const auto receives = capability_map["receives"];
const auto sends = capability_map["sends"];

capabilities.can_receive = receives.enabled && receives.requirements_empty;
capabilities.can_send = sends.enabled && sends.requirements_empty;
}

callback(result, std::move(capabilities));
}

std::pair<type::Result, std::map<std::string, bool>>
std::pair<type::Result, GetCapabilities::CapabilityMap>
GetCapabilities::ProcessResponse(const type::UrlResponse& response) {
const auto status_code = response.status_code;

Expand All @@ -69,9 +72,9 @@ GetCapabilities::ProcessResponse(const type::UrlResponse& response) {
std::move(capability_map)};
}

std::map<std::string, bool> GetCapabilities::ParseBody(
GetCapabilities::CapabilityMap GetCapabilities::ParseBody(
const std::string& body) {
std::map<std::string, bool> capability_map;
std::map<std::string, Capability> capability_map;

const auto value = base::JSONReader::Read(body);
const base::ListValue* list_value = nullptr;
Expand All @@ -82,13 +85,15 @@ std::map<std::string, bool> GetCapabilities::ParseBody(
for (const auto& item : list_value->GetList()) {
const auto* key = item.FindStringKey("key");
const auto enabled = item.FindBoolKey("enabled");
const auto* requirements = item.FindListKey("requirements");

if (!key || !enabled) {
if (!key || !enabled || !requirements) {
capability_map.clear();
break;
}

capability_map.emplace(*key, *enabled);
capability_map.emplace(
*key, Capability{*enabled, requirements->GetList().empty()});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,20 @@ class GetCapabilities {
void Request(const std::string& token, GetCapabilitiesCallback callback);

private:
struct Capability {
bool enabled = false;
bool requirements_empty = false;
};

using CapabilityMap = std::map<std::string, Capability>;

void OnRequest(const type::UrlResponse& response,
GetCapabilitiesCallback callback);

std::pair<type::Result, std::map<std::string, bool>> ProcessResponse(
std::pair<type::Result, CapabilityMap> ProcessResponse(
const type::UrlResponse& response);

std::map<std::string, bool> ParseBody(const std::string& body);
CapabilityMap ParseBody(const std::string& body);

LedgerImpl* ledger_; // NOT OWNED
};
Expand Down
Loading

0 comments on commit d9addf9

Please sign in to comment.