Skip to content

Commit

Permalink
Use reverse ordering hex for hex addresses as EVM expects
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlz committed Jun 30, 2017
1 parent 2a061fc commit 217a5ba
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/arith_uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ void base_uint<BITS>::SetHex(const std::string& str)
SetHex(str.c_str());
}

template <unsigned int BITS>
std::string base_uint<BITS>::GetReverseHex() const
{
return ArithToUint256(*this).GetReverseHex();
}

template <unsigned int BITS>
void base_uint<BITS>::SetReverseHex(const char* psz)
{
uint256 tmp;
tmp.SetReverseHex(psz);
*this = UintToArith256(tmp);
}

template <unsigned int BITS>
void base_uint<BITS>::SetReverseHex(const std::string& str)
{
SetReverseHex(str.c_str());
}

template <unsigned int BITS>
std::string base_uint<BITS>::ToString() const
{
Expand Down Expand Up @@ -194,9 +214,12 @@ template int base_uint<256>::CompareTo(const base_uint<256>&) const;
template bool base_uint<256>::EqualTo(uint64_t) const;
template double base_uint<256>::getdouble() const;
template std::string base_uint<256>::GetHex() const;
template std::string base_uint<256>::GetReverseHex() const;
template std::string base_uint<256>::ToString() const;
template void base_uint<256>::SetHex(const char*);
template void base_uint<256>::SetHex(const std::string&);
template void base_uint<256>::SetReverseHex(const char*);
template void base_uint<256>::SetReverseHex(const std::string&);
template unsigned int base_uint<256>::bits() const;

// This implementation directly uses shifts instead of going
Expand Down
3 changes: 3 additions & 0 deletions src/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ class base_uint
std::string GetHex() const;
void SetHex(const char* psz);
void SetHex(const std::string& str);
std::string GetReverseHex() const;
void SetReverseHex(const char* psz);
void SetReverseHex(const std::string& str);
std::string ToString() const;

unsigned int size() const
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ UniValue gethexaddress(const JSONRPCRequest& request) {
if(!address.IsPubKeyHash())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Only pubkeyhash addresses are supported");

return boost::get<CKeyID>(address.Get()).GetHex();
return boost::get<CKeyID>(address.Get()).GetReverseHex();
}

UniValue fromhexaddress(const JSONRPCRequest& request) {
Expand All @@ -172,7 +172,7 @@ UniValue fromhexaddress(const JSONRPCRequest& request) {
if (request.params[0].get_str().size() != 40)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid pubkeyhash hex size (should be 40 hex characters)");
CKeyID raw;
raw.SetHex(request.params[0].get_str());
raw.SetReverseHex(request.params[0].get_str());
CBitcoinAddress address(raw);

return address.ToString();
Expand Down
39 changes: 39 additions & 0 deletions src/uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ std::string base_blob<BITS>::GetHex() const
return std::string(psz, psz + sizeof(data) * 2);
}

template <unsigned int BITS>
std::string base_blob<BITS>::GetReverseHex() const
{
char psz[sizeof(data) * 2 + 1];
int j=0;
for (int i = sizeof(data)-1; i >= 0; i--) {
sprintf(psz + j * 2, "%02x", data[sizeof(data) - i - 1]);
j++;
}
return std::string(psz, psz + sizeof(data) * 2);
}

template <unsigned int BITS>
void base_blob<BITS>::SetHex(const char* psz)
{
Expand Down Expand Up @@ -61,6 +73,27 @@ void base_blob<BITS>::SetHex(const std::string& str)
SetHex(str.c_str());
}

template <unsigned int BITS>
void base_blob<BITS>::SetReverseHex(const char* psz)
{
SetHex(psz);
//reverse in-place
int left = 0;
int right = size()-1;
unsigned char* p1 = (unsigned char*)data;
while(left < right){
int temp=p1[left];
p1[left++] = p1[right];
p1[right--] = temp;
}
}

template <unsigned int BITS>
void base_blob<BITS>::SetReverseHex(const std::string& str)
{
SetReverseHex(str.c_str());
}

template <unsigned int BITS>
std::string base_blob<BITS>::ToString() const
{
Expand All @@ -73,10 +106,16 @@ template std::string base_blob<160>::GetHex() const;
template std::string base_blob<160>::ToString() const;
template void base_blob<160>::SetHex(const char*);
template void base_blob<160>::SetHex(const std::string&);
template std::string base_blob<160>::GetReverseHex() const;
template void base_blob<160>::SetReverseHex(const char*);
template void base_blob<160>::SetReverseHex(const std::string&);

// Explicit instantiations for base_blob<256>
template base_blob<256>::base_blob(const std::vector<unsigned char>&);
template std::string base_blob<256>::GetHex() const;
template std::string base_blob<256>::ToString() const;
template void base_blob<256>::SetHex(const char*);
template void base_blob<256>::SetHex(const std::string&);
template std::string base_blob<256>::GetReverseHex() const;
template void base_blob<256>::SetReverseHex(const char*);
template void base_blob<256>::SetReverseHex(const std::string&);
3 changes: 3 additions & 0 deletions src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ class base_blob
friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }

std::string GetHex() const;
std::string GetReverseHex() const;
void SetHex(const char* psz);
void SetHex(const std::string& str);
void SetReverseHex(const char* psz);
void SetReverseHex(const std::string& str);
std::string ToString() const;

unsigned char* begin()
Expand Down

0 comments on commit 217a5ba

Please sign in to comment.