diff --git a/src/arith_uint256.cpp b/src/arith_uint256.cpp index f9f2d19e68..ba611de480 100644 --- a/src/arith_uint256.cpp +++ b/src/arith_uint256.cpp @@ -162,6 +162,26 @@ void base_uint::SetHex(const std::string& str) SetHex(str.c_str()); } +template +std::string base_uint::GetReverseHex() const +{ + return ArithToUint256(*this).GetReverseHex(); +} + +template +void base_uint::SetReverseHex(const char* psz) +{ + uint256 tmp; + tmp.SetReverseHex(psz); + *this = UintToArith256(tmp); +} + +template +void base_uint::SetReverseHex(const std::string& str) +{ + SetReverseHex(str.c_str()); +} + template std::string base_uint::ToString() const { @@ -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 diff --git a/src/arith_uint256.h b/src/arith_uint256.h index 0f6b3d4fba..42a7a78332 100644 --- a/src/arith_uint256.h +++ b/src/arith_uint256.h @@ -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 diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 8606510b6f..ea16256586 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -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(address.Get()).GetHex(); + return boost::get(address.Get()).GetReverseHex(); } UniValue fromhexaddress(const JSONRPCRequest& request) { @@ -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(); diff --git a/src/uint256.cpp b/src/uint256.cpp index bd3d017085..a8c4bd2163 100644 --- a/src/uint256.cpp +++ b/src/uint256.cpp @@ -26,6 +26,18 @@ std::string base_blob::GetHex() const return std::string(psz, psz + sizeof(data) * 2); } +template +std::string base_blob::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 void base_blob::SetHex(const char* psz) { @@ -61,6 +73,27 @@ void base_blob::SetHex(const std::string& str) SetHex(str.c_str()); } +template +void base_blob::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 +void base_blob::SetReverseHex(const std::string& str) +{ + SetReverseHex(str.c_str()); +} + template std::string base_blob::ToString() const { @@ -73,6 +106,9 @@ 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&); @@ -80,3 +116,6 @@ 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&); diff --git a/src/uint256.h b/src/uint256.h index eacc2891bd..dc0a9c15fb 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -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()