forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddress.cpp
More file actions
58 lines (47 loc) · 1.78 KB
/
Copy pathAddress.cpp
File metadata and controls
58 lines (47 loc) · 1.78 KB
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
// 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 "Address.h"
#include "Identifiers.h"
#include <Base58.h>
#include <Coin.h>
#include <HexCoding.h>
using namespace TW::Aeternity;
/// Determines whether a string makes a valid address.
bool Address::isValid(const std::string &string) {
if (string.empty()) {
return false;
}
auto prefixSize = Identifiers::prefixAccountPubkey.size();
auto type = string.substr(0, prefixSize);
auto payload = string.substr(prefixSize, string.size() - 1);
return checkType(type) && checkPayload(payload);
}
/// Initializes an address from a public key.
Address::Address(const PublicKey &publicKey) {
if (publicKey.type != TWPublicKeyTypeED25519) {
throw std::invalid_argument("Invalid public key type");
}
bytes = publicKey.bytes;
}
/// Initializes an address from a string representation.
Address::Address(const std::string &string) {
if (!isValid(string)) {
throw std::invalid_argument("Invalid address");
}
auto payload = string.substr(Identifiers::prefixAccountPubkey.size(), string.size());
bytes = Base58::bitcoin.decodeCheck(payload);
}
/// Returns a string representation of the Bravo address.
std::string Address::string() const {
return Identifiers::prefixAccountPubkey + Base58::bitcoin.encodeCheck(bytes);
}
bool Address::checkType(const std::string &type) {
return type == Identifiers::prefixAccountPubkey;
}
bool Address::checkPayload(const std::string &payload) {
unsigned long base58 = Base58::bitcoin.decodeCheck(payload).size();
return base58 == size;
}