diff --git a/include/TrustWalletCore/TWString.h b/include/TrustWalletCore/TWString.h index a13b1d5d998..2495be45941 100644 --- a/include/TrustWalletCore/TWString.h +++ b/include/TrustWalletCore/TWString.h @@ -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); diff --git a/src/interface/TWAnyAddress.cpp b/src/interface/TWAnyAddress.cpp index e0498a7808c..364b8ae7762 100644 --- a/src/interface/TWAnyAddress.cpp +++ b/src/interface/TWAnyAddress.cpp @@ -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; diff --git a/src/interface/TWString.cpp b/src/interface/TWString.cpp index 550ea47fcfd..171dfdb8661 100644 --- a/src/interface/TWString.cpp +++ b/src/interface/TWString.cpp @@ -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(string); return s->size(); diff --git a/tests/interface/TWAnyAddressTests.cpp b/tests/interface/TWAnyAddressTests.cpp index 81a77034859..a2e268c574a 100644 --- a/tests/interface/TWAnyAddressTests.cpp +++ b/tests/interface/TWAnyAddressTests.cpp @@ -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 { diff --git a/tests/interface/TWZilliqaTests.cpp b/tests/interface/TWZilliqaTests.cpp index 8c3dcf1690e..6e31bc8d8e4 100644 --- a/tests/interface/TWZilliqaTests.cpp +++ b/tests/interface/TWZilliqaTests.cpp @@ -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) {