Skip to content

Commit

Permalink
Merge pull request #283 from skalenetwork/bug/SKALE-2589-IMA-token-fi…
Browse files Browse the repository at this point in the history
…elds-verification-fix

SKALE-2589 fixed IMA token message verification
  • Loading branch information
sergiy-skalelabs authored Jun 4, 2020
2 parents 1b7e035 + 33bdc48 commit 39126ff
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 27 deletions.
10 changes: 10 additions & 0 deletions libdevcore/BMPBN.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ inline T decode( const uint8_t* input, size_t length ) {
return result;
}

template < class T >
inline T decode_inv( const uint8_t* input, size_t length ) {
std::vector< uint8_t > vi;
if ( input && length ) {
for ( size_t i = 0; i < length; ++i )
vi.insert( vi.begin(), input[i] );
}
return decode< T >( vi.data(), vi.size() );
}

template < class T >
inline std::string toHexStringWithPadding(
const T& value, size_t nPadding = std::string::npos, bool bWith0x = true ) {
Expand Down
111 changes: 84 additions & 27 deletions libweb3jsonrpc/SkaleStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,64 +897,100 @@ Json::Value SkaleStats::skale_imaVerifyAndSign( const Json::Value& request ) {
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(1), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 contractPosition =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
// std::cout << "\"contractPosition\" is " << toJS( contractPosition ) << std::endl;
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(2), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 addressTo =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
// std::cout << "\"addressTo\" is " << toJS( addressTo ) << std::endl;
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(3), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 amount =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
// std::cout << "\"amount\" is " << toJS( amount ) << std::endl;
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(4), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 sizeOfName =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
// std::cout << "\"sizeOfName\" is " << toJS( sizeOfName ) << std::endl;
nPos += nFiledSize;
nFiledSize = sizeOfName.convert_to< size_t >();
// std::cout << "\"nFiledSize\" is " << nFiledSize << std::endl;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(5), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
std::string strName( "" );
strName.insert( strName.end(), ( ( char* ) ( vecBytes.data() ) ) + nPos,
( ( char* ) ( vecBytes.data() ) ) + nPos + nFiledSize );
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(6), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 sizeOfSymbol =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
// std::cout << "\"sizeOfSymbol\" is " << toJS( sizeOfSymbol ) << std::endl;
nPos += 32;
nFiledSize = sizeOfSymbol.convert_to< size_t >();
// std::cout << "\"nFiledSize\" is " << nFiledSize << std::endl;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(7), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
std::string strSymbol( "" );
strSymbol.insert( strSymbol.end(), ( ( char* ) ( vecBytes.data() ) ) + nPos,
( ( char* ) ( vecBytes.data() ) ) + nPos + nFiledSize );
nPos += nFiledSize;
//
nFiledSize = 1;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(8), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const uint8_t nDecimals = uint8_t( vecBytes[nPos] );
// std::cout << "\"nDecimals\" is " << nDecimals << std::endl;
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC20(9), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 totalSupply =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
// std::cout << "\"totalSupply\" is " << toJS( totalSupply ) << std::endl;
nPos += nFiledSize;
//
if ( nPos > cntMessageBytes ) {
Expand Down Expand Up @@ -1003,48 +1039,69 @@ Json::Value SkaleStats::skale_imaVerifyAndSign( const Json::Value& request ) {
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC721(1), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 contractPosition =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC721(2), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 addressTo =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC721(3), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 tokenID =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC721(4), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 sizeOfName =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
nPos += nFiledSize;
nFiledSize = sizeOfName.convert_to< size_t >();
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC721(5), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
std::string strName( "" );
strName.insert( strName.end(), ( ( char* ) ( vecBytes.data() ) ) + nPos,
( ( char* ) ( vecBytes.data() ) ) + nPos + nFiledSize );
nPos += nFiledSize;
//
nFiledSize = 32;
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC721(6), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
const dev::u256 sizeOfSymbol =
BMPBN::decode< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
BMPBN::decode_inv< dev::u256 >( vecBytes.data() + nPos, nFiledSize );
nPos += 32;
nFiledSize = sizeOfSymbol.convert_to< size_t >();
if ( ( nPos + nFiledSize ) > cntMessageBytes )
throw std::runtime_error( "IMA message to short" );
throw std::runtime_error(
skutils::tools::format( "IMA message too short, ERC721(7), nPos=%zu, "
"nFiledSize=%zu, cntMessageBytes=%zu",
nPos, nFiledSize, cntMessageBytes ) );
std::string strSymbol( "" );
strSymbol.insert( strSymbol.end(), ( ( char* ) ( vecBytes.data() ) ) + nPos,
( ( char* ) ( vecBytes.data() ) ) + nPos + nFiledSize );
Expand Down

0 comments on commit 39126ff

Please sign in to comment.