forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add TWFIOAccount * Move regex into Actor.cpp, add android test
- Loading branch information
1 parent
02eab8a
commit be69646
Showing
14 changed files
with
242 additions
and
57 deletions.
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
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,28 @@ | ||
// Copyright © 2017-2020 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#pragma once | ||
|
||
#include "TWBase.h" | ||
#include "TWString.h" | ||
|
||
TW_EXTERN_C_BEGIN | ||
|
||
/// Represents a FIO Account name | ||
TW_EXPORT_CLASS | ||
struct TWFIOAccount; | ||
|
||
TW_EXPORT_STATIC_METHOD | ||
struct TWFIOAccount *_Nullable TWFIOAccountCreateWithString(TWString *_Nonnull string); | ||
|
||
TW_EXPORT_METHOD | ||
void TWFIOAccountDelete(struct TWFIOAccount *_Nonnull account); | ||
|
||
/// Returns the short account string representation. | ||
TW_EXPORT_PROPERTY | ||
TWString *_Nonnull TWFIOAccountDescription(struct TWFIOAccount *_Nonnull account); | ||
|
||
TW_EXTERN_C_END |
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,60 @@ | ||
// Copyright © 2017-2020 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#include "Actor.h" | ||
|
||
#include <regex> | ||
|
||
using namespace TW::FIO; | ||
using namespace std; | ||
|
||
string Actor::actor(const Address& addr) | ||
{ | ||
uint64_t shortenedKey = shortenKey(addr.bytes); | ||
string name13 = name(shortenedKey); | ||
// trim to 12 chracters | ||
return name13.substr(0, 12); | ||
} | ||
|
||
bool Actor::validate(const std::string& addr) { | ||
regex pattern(R"(\b([a-z1-5]{3,})[.@]?\b)"); | ||
smatch match; | ||
return regex_search(addr, match, pattern); | ||
} | ||
|
||
uint64_t Actor::shortenKey(const array<byte, Address::size>& addrKey) | ||
{ | ||
uint64_t res = 0; | ||
int i = 1; // Ignore key head | ||
int len = 0; | ||
while (len <= 12) { | ||
assert(i < 33); // Means the key has > 20 bytes with trailing zeroes... | ||
|
||
auto trimmed_char = uint64_t(addrKey[i] & (len == 12 ? 0x0f : 0x1f)); | ||
if (trimmed_char == 0) { i++; continue; } // Skip a zero and move to next | ||
|
||
auto shuffle = len == 12 ? 0 : 5 * (12 - len) - 1; | ||
res |= trimmed_char << shuffle; | ||
|
||
len++; i++; | ||
} | ||
return res; | ||
} | ||
|
||
string Actor::name(uint64_t shortKey) { | ||
static const char* charmap = ".12345abcdefghijklmnopqrstuvwxyz"; | ||
|
||
string str(13,'.'); //We are forcing the string to be 13 characters | ||
|
||
uint64_t tmp = shortKey; | ||
for(uint32_t i = 0; i <= 12; i++ ) { | ||
char c = charmap[tmp & (i == 0 ? 0x0f : 0x1f)]; | ||
str[12 - i] = c; | ||
tmp >>= (i == 0 ? 4 : 5); | ||
} | ||
|
||
return str; | ||
} |
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,27 @@ | ||
// Copyright © 2017-2020 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#pragma once | ||
|
||
#include "Address.h" | ||
#include <string> | ||
|
||
namespace TW::FIO { | ||
/// Helper class for Actor name generation from address | ||
class Actor { | ||
public: | ||
/// Generate the actor name of the address | ||
static std::string actor(const Address& addr); | ||
|
||
/// Check if the address is valid | ||
static bool validate(const std::string& addr); | ||
|
||
/// Used internally, derive shortened uint64 key from adddr bytes | ||
static uint64_t shortenKey(const std::array<byte, Address::size>& addrKey); | ||
/// Used internally, derive name from uint64 shortened key | ||
static std::string name(uint64_t shortKey); | ||
}; | ||
} // namespace TW::FIO |
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
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,39 @@ | ||
// Copyright © 2017-2020 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#include <TrustWalletCore/TWFIOAccount.h> | ||
|
||
#include "FIO/Actor.h" | ||
#include "FIO/Address.h" | ||
|
||
#include <string> | ||
|
||
using namespace TW; | ||
using namespace TW::FIO; | ||
|
||
struct TWFIOAccount { | ||
std::string description; | ||
}; | ||
|
||
struct TWFIOAccount *_Nullable TWFIOAccountCreateWithString(TWString *_Nonnull string) { | ||
const auto& account = *reinterpret_cast<const std::string*>(string); | ||
if (Address::isValid(account)) { | ||
const auto addr = Address(account); | ||
return new TWFIOAccount{Actor::actor(addr)}; | ||
} | ||
if (Actor::validate(account)) { | ||
return new TWFIOAccount{account}; | ||
} | ||
return nullptr; | ||
} | ||
|
||
void TWFIOAccountDelete(struct TWFIOAccount *_Nonnull account) { | ||
delete account; | ||
} | ||
|
||
TWString *_Nonnull TWFIOAccountDescription(struct TWFIOAccount *_Nonnull account) { | ||
return TWStringCreateWithUTF8Bytes(account->description.c_str()); | ||
} |
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,46 @@ | ||
// Copyright © 2017-2020 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#include "../interface/TWTestUtilities.h" | ||
#include <TrustWalletCore/TWFIOAccount.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(TWFIO, Account) { | ||
{ | ||
auto string = STRING("FIO6XKiobBGrmPZDaHne47TF25CamjFc3cRe8hs7oAiCN59TbJzes"); | ||
auto account = TWFIOAccountCreateWithString(string.get()); | ||
auto description = WRAPS(TWFIOAccountDescription(account)); | ||
assertStringsEqual(description, "rpxtnpsr3gxd"); | ||
TWFIOAccountDelete(account); | ||
} | ||
|
||
{ | ||
auto account = TWFIOAccountCreateWithString(STRING("lhp1ytjibtea").get()); | ||
auto account2 = TWFIOAccountCreateWithString(STRING("wrcjejslfplp").get()); | ||
auto account3 = TWFIOAccountCreateWithString(STRING("rewards@wallet").get()); | ||
auto description = WRAPS(TWFIOAccountDescription(account)); | ||
auto description2 = WRAPS(TWFIOAccountDescription(account2)); | ||
auto description3 = WRAPS(TWFIOAccountDescription(account3)); | ||
assertStringsEqual(description, "lhp1ytjibtea"); | ||
assertStringsEqual(description2, "wrcjejslfplp"); | ||
assertStringsEqual(description3, "rewards@wallet"); | ||
|
||
TWFIOAccountDelete(account); | ||
TWFIOAccountDelete(account2); | ||
TWFIOAccountDelete(account3); | ||
} | ||
|
||
{ | ||
auto string = STRING("asdf19s"); | ||
auto string2 = STRING("0x320196ef1b137786be51aa391e78e0a2c756f46b"); | ||
auto account = TWFIOAccountCreateWithString(string.get()); | ||
auto account2 = TWFIOAccountCreateWithString(string2.get()); | ||
|
||
ASSERT_EQ(account, nullptr); | ||
ASSERT_EQ(account2, nullptr); | ||
} | ||
} |