Skip to content
Open
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
10 changes: 10 additions & 0 deletions CommonJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
namespace dev
{

std::string toHexBytes(dev::bytes const& data)
{
return data.empty() ? std::string("0x") : toHexPrefixed(data);
}

std::string toHexQuantity(uint64_t value)
{
return toJS(value);
}

bytes jsToBytes(std::string const& _s, OnFailed _f)
{
try
Expand Down
3 changes: 3 additions & 0 deletions CommonJS.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ template<typename T> std::string toJS(T const& _i)
return stream.str();
}

std::string toHexBytes(dev::bytes const& data);
std::string toHexQuantity(uint64_t value);

enum class OnFailed { InterpretRaw, Empty, Throw };

/// Convert string to byte array. Input parameter is hex, optionally prefixed by "0x".
Expand Down
34 changes: 34 additions & 0 deletions HexUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Olympus: mcp C++ client, tools and libraries.
// Licensed under the GNU General Public License, Version 3.

#include "HexUtils.h"

std::string dev::toCompactHexFromIntx(intx::uint256 value)
{
std::ostringstream out;
out << "0x";

if (value == 0)
{
out << '0';
return out.str();
}

std::string hex;
hex.reserve(64);

while (value != 0)
{
auto nibble = static_cast<unsigned>(value & intx::uint256{0xF});
hex.push_back(nibble < 10 ? static_cast<char>('0' + nibble)
: static_cast<char>('a' + (nibble - 10)));
value >>= 4;
}

if (hex.size() % 2 != 0)
hex.push_back('0');

std::reverse(hex.begin(), hex.end());
out << hex;
return out.str();
}
14 changes: 14 additions & 0 deletions HexUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Olympus: mcp C++ client, tools and libraries.
// Licensed under the GNU General Public License, Version 3.

#pragma once

#include <intx/intx.hpp>
#include <string>
#include <algorithm>
#include <sstream>

namespace dev
{
std::string toCompactHexFromIntx(intx::uint256 value);
}