Skip to content

Commit

Permalink
feat: address class
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 d462d00 commit a1fd14a
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ add_library(iotex-client STATIC
src/random/platformRand32.cpp
src/storage/os/storage.cpp
src/account/account.cpp
src/account/address.cpp
src/abi/abi.cpp
src/abi/abiDecode.cpp
src/helpers/json_helper.cpp
Expand Down
3 changes: 2 additions & 1 deletion src/IoTeX-blockchain-client.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef IOTEX_CLIENT_H
#define IOTEX_CLIENT_H

#include "abi/abiDecode.h"
#include "account/account.h"
#include "account/address.h"
#include "api/wallet/wallets.h"
#include "connection/connection.h"
#include "contract/contract.h"
Expand All @@ -11,7 +13,6 @@
#include "IoTeXResultCodes.h"
#include "random/random.h"
#include "storage/storage.h"
#include "abi/abiDecode.h"

using namespace iotex;
using namespace iotex::api;
Expand Down
64 changes: 64 additions & 0 deletions src/account/address.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "account/address.h"

#include "string.h"
#include <vector>
#include <string>
#include "signer/signer.h"
#include "encoder/encoder.h"

using namespace iotex;

iotex::Address::Address(const char* address, AddressFormat format)
{
memset(_io, 0, sizeof(_io));
memset(_eth, 0, sizeof(_eth));
memset(_bytes, 0, sizeof(_bytes));

if (format == AddressFormat::IO)
{
// TODO create from io format
}
else
{
ConstructFromEth(address);
}
}

const char* iotex::Address::Io()
{
return _io;
};

const char* iotex::Address::Eth()
{
return _eth;
};

const uint8_t* iotex::Address::Bytes()
{
return _bytes;
};

void iotex::Address::ConstructFromEth(const char* address)
{
// Set bytes
signer.str2hex(address, _bytes, 20);

// Copy 0x address
if(address[0] == '0' && address[1] == 'x')
{
memcpy(_eth, address, ETH_ADDRESS_C_STRING_SIZE);
}
else
{
_eth[0] = '0';
_eth[1] = 'x';
memcpy(_eth+2, address, ETH_ADDRESS_C_STRING_SIZE_NON_PREFIXED);
}

// Create io address
std::vector<uint8_t> bytes(_bytes, _bytes + ETH_ADDRESS_SIZE);
std::string io = _io;
encoder.bech32_encode(bytes, io);
memcpy(_io, io.data(), IOTEX_ADDRESS_C_STRING_SIZE);
}
35 changes: 35 additions & 0 deletions src/account/address.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef IOTEX_ADDRESS_H
#define IOTEX_ADDRESS_H

#include "IoTeXConstants.h"
#include <stdint.h>

namespace iotex
{
enum AddressFormat
{
IO,
ETH
};

class Address
{
public:
Address(const char* address, AddressFormat format);

const char* Io();

const char* Eth();

const uint8_t* Bytes();

private:
char _io[IOTEX_ADDRESS_C_STRING_SIZE];
char _eth[ETH_ADDRESS_C_STRING_SIZE];
uint8_t _bytes[ETH_ADDRESS_SIZE];

void ConstructFromEth(const char* address);
};
} // namespace iotex

#endif
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ set (IOTEX_UNIT_TESTS_SOURCES
src/contract/contractTests.cpp
src/contract/xrc20ContractTests.cpp
src/account/accountTests_Execution.cpp
src/account/addressTests.cpp
src/signer/signerTests.cpp
)

Expand Down
1 change: 1 addition & 0 deletions tests/include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace
const char testAddressPublicKey[] = "044f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa385b6b1b8ead809ca67454d9683fcf2ba03456d6fe2c4abe2b07f0fbdbb2f1c1";
const char testAddressIo[] = "io1r8n7xah8cgfm0el8u3kvwzja6zrd4le2zce7sk";
const char testAddressEth[] = "19e7e376e7c213b7e7e7e46cc70a5dd086daff2a";
const char testAddressEth_Prefixed[] = "0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a";
const uint8_t testAddressEthBytes[] = { 0x19, 0xe7, 0xe3, 0x76, 0xe7, 0xc2, 0x13, 0xb7, 0xe7, 0xe7, 0xe4, 0x6c, 0xc7, 0x0a, 0x5d, 0xd0, 0x86, 0xda, 0xff, 0x2a};

// Another address used for tests
Expand Down
23 changes: 23 additions & 0 deletions tests/src/account/addressTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "constants.h"

#include "account/address.h"

using namespace std;
using namespace testing;
using namespace iotex;

class AddressTests : public Test
{
};

TEST_F(AddressTests, createFromEth)
{
iotex::Address addr(testAddressEth, AddressFormat::ETH);
ASSERT_STREQ(testAddressEth_Prefixed, addr.Eth());
ASSERT_STREQ(testAddressIo, addr.Io());
ASSERT_EQ(0, memcmp(testAddressEthBytes, addr.Bytes(), ETH_ADDRESS_SIZE));
}

0 comments on commit a1fd14a

Please sign in to comment.