Skip to content

Commit

Permalink
feat: add address abi decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
as-iotex authored and as-iotex committed Apr 17, 2022
1 parent 4a571b0 commit 738dfae
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/abi/abiDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ ResultCode iotex::abi::decode::decodeString(const char* pData, size_t size, Iote

return ResultCode::SUCCESS;
}

iotex::ResultCode iotex::abi::decode::decodeAddress(const char data[64], char out[ETH_ADDRESS_C_STRING_SIZE])
{
// The address is encoded as a uint160
// Ie. big-endian encoding padded on the higher-order (left) side with zero-bytes such that the length is 32 bytes.
out[ETH_ADDRESS_C_STRING_SIZE-1] = 0;
const char* pStart = data + 24; // 12 padding bytes
memcpy(out, pStart, ETH_ADDRESS_C_STRING_SIZE - 1);

return ResultCode::SUCCESS;
}
1 change: 1 addition & 0 deletions src/abi/abiDecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ bool decodeBool(const char data[64]);
iotex::ResultCode decodeUintGeneric(const char* pData, size_t uintSize, uint64_t* out);
iotex::ResultCode decodeIntGeneric(const char* pData, size_t uintSize, int64_t* out);
iotex::ResultCode decodeString(const char* pData, size_t size, IotexString& out);
iotex::ResultCode decodeAddress(const char data[64], char out[ETH_ADDRESS_C_STRING_SIZE]);

template<uint8_t size>
ResultCode decodeUint(const char* pData, size_t dataSize, uint64_t* out)
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ set (IOTEX_UNIT_TESTS_SOURCES
src/abi/abiTests.cpp
src/abi/decode/abiDecodeUintTests.cpp
src/abi/decode/abiDecodeIntTests.cpp
src/abi/decode/abiDecodeStringTests.cpp
src/abi/decode/abiDecodeAddressTests.cpp
src/contract/contractTests.cpp
src/contract/xrc20ContractTests.cpp
src/account/accountTests_Execution.cpp
Expand Down
27 changes: 27 additions & 0 deletions tests/src/abi/decode/abiDecodeAddressTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "abi/abiDecode.h"
#include "contract/contract.h"
#include "signer/signer.h"

using namespace std;
using namespace testing;
using namespace iotex;
using namespace iotex::abi::decode;

class AbiDecodeAddressTests : public Test
{
void SetUp() override {}

void TearDown() override {}
};

TEST_F(AbiDecodeAddressTests, DecodesCorrectly)
{
const char encoded[] = "00000000000000000000000019e7e376e7c213b7e7e7e46cc70a5dd086daff2a";
char decoded[ETH_ADDRESS_C_STRING_SIZE];
ResultCode res = decodeAddress(encoded, decoded);
ASSERT_EQ(ResultCode::SUCCESS, res);
ASSERT_STREQ("19e7e376e7c213b7e7e7e46cc70a5dd086daff2a", decoded);
}

0 comments on commit 738dfae

Please sign in to comment.