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
97 lines (77 loc) · 2.7 KB
/
Address.cpp
File metadata and controls
97 lines (77 loc) · 2.7 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// 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 "../Base58.h"
#include "../BinaryCoding.h"
#include "Address.h"
#include <TrezorCrypto/ripemd160.h>
#include <stdexcept>
using namespace TW;
using namespace TW::FIO;
bool Address::isValid(const std::string& string) {
return decodeKeyData(string).has_value();
}
/// Determines whether the given byte vector is a valid keyBuffer
bool Address::isValid(const Data& bytes) {
if (bytes.size() != size) {
return false;
}
// last Address::ChecksumSize bytes are a checksum
uint32_t checksum = decode32LE(bytes.data() + PublicKey::secp256k1Size);
if (createChecksum(bytes) != checksum) {
return false;
}
return true;
}
/// Creates a checksum of PublicKeyDataSize bytes at the buffer
uint32_t Address::createChecksum(const Data& bytes) {
// create checksum and compare
uint8_t hash[RIPEMD160_DIGEST_LENGTH];
RIPEMD160_CTX ctx;
ripemd160_Init(&ctx);
// add the bytes to the hash input
ripemd160_Update(&ctx, bytes.data(), PublicKey::secp256k1Size);
// finalize the hash
ripemd160_Final(&ctx, hash);
return decode32LE(hash);
}
/// Decode and verifies the key data from a base58 string.
std::optional<Data> Address::decodeKeyData(const std::string& string) {
size_t prefixSize = prefix().size();
if (string.substr(0, prefixSize) != prefix()) {
return {};
}
const Data& decodedBytes = Base58::bitcoin.decode(string.substr(prefixSize));
if (decodedBytes.size() != size) {
return {};
}
if (!isValid(decodedBytes)) {
return {};
}
return decodedBytes;
}
/// Initializes a FIO address from a string representation.
Address::Address(const std::string& string) {
auto data = Address::decodeKeyData(string);
if (!data.has_value()) {
throw std::invalid_argument("Invalid address string!");
}
std::copy(std::begin(*data), std::end(*data), std::begin(bytes));
}
/// Initializes a FIO address from a public key.
Address::Address(const PublicKey& publicKey) {
// copy the raw, compressed key data
auto data = publicKey.compressed().bytes;
// append the checksum
uint32_t checksum = createChecksum(data);
Data checksumBytes;
encode32LE(checksum, checksumBytes);
append(data, checksumBytes);
std::copy(std::begin(data), std::end(data), std::begin(bytes));
}
/// Returns a string representation of the FIO address.
std::string Address::string() const {
return prefix() + Base58::bitcoin.encode(bytes);
}