Skip to content

Commit

Permalink
[resolves trustwallet#260] implement Address::isValid function + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Defuera committed May 31, 2019
1 parent e29e7eb commit b301488
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
30 changes: 12 additions & 18 deletions src/Aeternity/Address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,12 @@ using namespace TW::Aeternity;
/// Determines whether a string makes a valid address.
/// copied from \see https://github.com/aeternity/aepp-sdk-js/blob/develop/es/utils/crypto.js
bool TW::Aeternity::Address::isValid(const std::string &string) {
const std::string &type = assertedType(string, prefix);
unsigned long base58 = Base58::bitcoin.decodeCheck(type).size();
return base58 == 32;
}

std::string Address::assertedType(const std::string &data, const std::string &type) {
const std::basic_regex base_regex("^${ak\\_}_.+ $");
std::smatch base_match;

if (std::regex_match(data, base_regex)) {

int n = data.length();
char charBuffer[n + 1];
strcpy(charBuffer, data.c_str());

char *token = strtok(charBuffer, "_");
return token;
if (string.empty()) {
return false;
} else {
return nullptr;
auto type = string.substr(0, prefix.size());
auto payload = string.substr(prefix.size(), string.size() - 1);
return checkType(type) && checkPayload(payload);
}
}

Expand All @@ -55,3 +42,10 @@ TW::Aeternity::Address::Address(const PublicKey &publicKey) {
std::string Address::string() const {
return ""; // prefix + Base58::bitcoin.encodeCheck("");
}
bool Address::checkType(const std::string &type) {
return type == prefix;
}
bool Address::checkPayload(const std::string &payload) {
unsigned long base58 = Base58::bitcoin.decodeCheck(payload).size();
return base58 == 32;
}
14 changes: 6 additions & 8 deletions src/Aeternity/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,27 @@

#include <PublicKey.h>
#include <string>
#include <regex>

namespace TW::Aeternity {

static const std::string prefix = "ak_"; //todo should I use 'ak_'?
static const std::string prefix = "ak_";

class Address {
public:

/// Determines whether a string makes a valid address.
static bool isValid(const std::string& string);
static bool isValid(const std::string &string);

/// Initializes an address from a string representation.
Address(const std::string& string);
explicit Address(const std::string &string);

/// Initializes an address from a public key.
Address(const PublicKey& publicKey);
explicit Address(const PublicKey &publicKey);

/// Returns a string representation of the Aeternity address.
std::string string() const;

private:
static std::string assertedType(const std::string& data, const std::string& type);
std::regex* regex;
static bool checkType(const std::string& type);
static bool checkPayload(const std::string& payload);
};
} // namespace TW::Aeternity

0 comments on commit b301488

Please sign in to comment.