forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Address.cpp
66 lines (55 loc) · 2.06 KB
/
Address.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright © 2017-2023 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 "Address.h"
#include "Base58.h"
#include "HexCoding.h"
#include <stdexcept>
using namespace TW;
namespace TW::NEAR {
bool Address::isValid(const std::string& string) {
const auto data = Address::decodeLegacyAddress(string);
if (data.has_value()) {
return true;
}
const auto parsed = parse_hex(string);
return parsed.size() == PublicKey::ed25519Size;
}
/// Decode and verifies the key data from a base58 string.
std::optional<Data> Address::decodeLegacyAddress(const std::string& string) {
const auto prefix = std::string("NEAR");
if (string.substr(0, prefix.size()) != prefix) {
return std::nullopt;
}
const Data& decoded = Base58::decode(string.substr(prefix.size()));
if (decoded.size() != size + legacyChecksumSize) {
return std::nullopt;
}
return Data(decoded.begin(), decoded.end() - legacyChecksumSize);
}
/// Initializes a NEAR address from a string representation.
Address::Address(const std::string& string) {
const auto data = Address::decodeLegacyAddress(string);
if (data.has_value()) {
std::copy(std::begin(*data), std::end(*data), std::begin(bytes));
} else {
const auto parsed = parse_hex(string);
if (parsed.size() != PublicKey::ed25519Size) {
throw std::invalid_argument("Invalid address string!");
}
std::copy(std::begin(parsed), std::end(parsed), std::begin(bytes));
}
}
/// Initializes a NEAR address from a public key.
Address::Address(const PublicKey& publicKey) {
// copy the raw, compressed key data
auto data = publicKey.compressed().bytes;
std::copy(std::begin(data), std::end(data), std::begin(bytes));
}
/// Returns a string representation of the NEAR address.
std::string Address::string() const {
return hex(bytes);
}
} // namespace TW::NEAR