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
65 lines (57 loc) · 2.06 KB
/
Copy pathAddress.cpp
File metadata and controls
65 lines (57 loc) · 2.06 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
59
60
61
62
63
64
65
// 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 "../HexCoding.h"
#include "../Hash.h"
#include "../Base32.h"
#include <array>
using namespace TW::Algorand;
bool Address::isValid(const std::string& string) {
if (string.size() != encodedSize) {
return false;
}
Data decoded;
if (!Base32::decode(string, decoded)) {
return false;
}
// compute public key hash
auto hash = Hash::sha512_256(decoded.data(), decoded.data() + decoded.size() - checksumSize);
// last 4 bytes are checksum
std::array<byte, checksumSize> checksum;
std::copy(hash.end() - checksumSize, hash.end(), checksum.data());
// compare checksum
if (!std::equal(decoded.end() - checksumSize, decoded.end(), checksum.begin())) {
return false;
}
return true;
}
Address::Address(const std::string& string) {
if (!isValid(string)) {
throw std::invalid_argument("Invalid address string");
}
Data decoded;
if (!Base32::decode(string, decoded)) {
throw std::invalid_argument("Invalid address string");
}
std::copy(decoded.begin(), decoded.begin() + PublicKey::ed25519Size, bytes.begin());
}
Address::Address(const PublicKey& publicKey) {
if (publicKey.type != TWPublicKeyTypeED25519) {
throw std::invalid_argument("Invalid public key type");
}
std::copy(publicKey.bytes.begin(), publicKey.bytes.end(), bytes.data());
}
std::string Address::string() const {
auto hash = Hash::sha512_256(bytes);
const size_t dataSize = PublicKey::ed25519Size + checksumSize;
Data data;
data.resize(dataSize);
// base32_encode(publickey + checksum)
std::copy(bytes.begin(), bytes.end(), data.data());
std::copy(hash.end() - checksumSize, hash.end(), data.data() + PublicKey::ed25519Size);
std::string encoded = Base32::encode(data);
return encoded;
}