Skip to content

Commit

Permalink
Fix zilliqa address data tests unstable (#826)
Browse files Browse the repository at this point in the history
* Add create with raw bytes to TWString

* Fix codacy warnings
  • Loading branch information
hewigovens authored and vikmeup committed Jan 21, 2020
1 parent 39dd59b commit 0e08bc7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions include/TrustWalletCore/TWString.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ typedef const void TWString;
/// Creates a string from a null-terminated UTF8 byte array. It must be deleted at the end.
TWString *_Nonnull TWStringCreateWithUTF8Bytes(const char *_Nonnull bytes);

/// Creates a string from a raw byte array and size.
TWString *_Nonnull TWStringCreateWithRawBytes(const uint8_t *_Nonnull bytes, size_t size);

/// Creates a hexadecimal string from a block of data. It must be deleted at the end.
TWString *_Nonnull TWStringCreateWithHexData(TWData *_Nonnull data);

Expand Down
1 change: 1 addition & 0 deletions src/interface/TWAnyAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ TWData* _Nonnull TWAnyAddressData(struct TWAnyAddress* _Nonnull address) {
if (!Zilliqa::Address::decode(string, addr)) {
break;
}
// data in Zilliqa is a checksummed string with 0x prefix
auto str = Zilliqa::checkSum(addr.getKeyHash());
data = Data(str.begin(), str.end());
break;
Expand Down
7 changes: 7 additions & 0 deletions src/interface/TWString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ TWString *_Nonnull TWStringCreateWithUTF8Bytes(const char *_Nonnull bytes) {
return s;
}

TWString *_Nonnull TWStringCreateWithRawBytes(const uint8_t *_Nonnull bytes, size_t size) {
auto s = new std::string(bytes, bytes + size);
// append null terminator
s->append(size, '\0');
return s;
}

size_t TWStringSize(TWString *_Nonnull string) {
auto s = reinterpret_cast<const std::string*>(string);
return s->size();
Expand Down
8 changes: 5 additions & 3 deletions tests/interface/TWAnyAddressTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ TEST(AnyAddress, Data) {
{
auto string = STRING("zil1l8ddxvejeam70qang54wnqkgtmlu5mwlgzy64z");
auto addr = WRAP(TWAnyAddress, TWAnyAddressCreateWithString(string.get(), TWCoinTypeZilliqa));
auto data = WRAPD(TWAnyAddressData(addr.get()));
auto checksumed = WRAPS(TWStringCreateWithUTF8Bytes((const char *)TWDataBytes(data.get())));
assertStringsEqual(checksumed, "0xF9dad33332CF77E783B3452aE982c85effCa6DDf");

auto expectedKeyHash = "0xF9dad33332CF77E783B3452aE982c85effCa6DDf";
auto keyHash = WRAPD(TWAnyAddressData(addr.get()));
auto checksumed = WRAPS(TWStringCreateWithRawBytes(TWDataBytes(keyHash.get()), strnlen(expectedKeyHash, 42)));
assertStringsEqual(checksumed, expectedKeyHash);
}
// kusama
{
Expand Down
6 changes: 4 additions & 2 deletions tests/interface/TWZilliqaTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ TEST(Zilliqa, Address) {

auto address = WRAP(TWAnyAddress, TWAnyAddressCreateWithString(string.get(), TWCoinTypeZilliqa));
auto desc = WRAPS(TWAnyAddressDescription(address.get()));

auto expectedKeyHash = "0xDdb41006F7B6FA8e5FBF06A71c01F789FeBC66e8";
auto keyHash = WRAPD(TWAnyAddressData(address.get()));
auto keyHashString = WRAPS(TWStringCreateWithUTF8Bytes((const char *)TWDataBytes(keyHash.get())));
auto keyHashString = WRAPS(TWStringCreateWithRawBytes(TWDataBytes(keyHash.get()), strnlen(expectedKeyHash, 42)));

assertStringsEqual(desc, "zil1mk6pqphhkmaguhalq6n3cq0h38ltcehg0rfmv6");
assertStringsEqual(keyHashString, "0xDdb41006F7B6FA8e5FBF06A71c01F789FeBC66e8");
assertStringsEqual(keyHashString, expectedKeyHash);
}

TEST(Zilliqa, Signing) {
Expand Down

0 comments on commit 0e08bc7

Please sign in to comment.