-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|