forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCashAddress.cpp
More file actions
101 lines (84 loc) · 3.42 KB
/
Copy pathCashAddress.cpp
File metadata and controls
101 lines (84 loc) · 3.42 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
98
99
100
101
// 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 "CashAddress.h"
#include "../Coin.h"
#include <TrezorCrypto/cash_addr.h>
#include <TrezorCrypto/ecdsa.h>
#include <array>
#include <cassert>
#include <cstring>
using namespace TW::Bitcoin;
/// Cash address human-readable part
static const std::string cashHRP = "bitcoincash";
/// From https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md
static const uint8_t p2khVersion = 0x00;
static const uint8_t p2shVersion = 0x08;
static constexpr size_t maxHRPSize = 20;
static constexpr size_t maxDataSize = 104;
bool CashAddress::isValid(const std::string& string) {
auto withPrefix = string;
if (!std::equal(cashHRP.begin(), cashHRP.end(), string.begin())) {
withPrefix = cashHRP + ":" + string;
}
std::array<char, maxHRPSize + 1> hrp = {0};
std::array<uint8_t, maxDataSize> data;
size_t dataLen;
if (cash_decode(hrp.data(), data.data(), &dataLen, withPrefix.c_str()) == 0 || dataLen != CashAddress::size) {
return false;
}
if (std::strncmp(hrp.data(), cashHRP.c_str(), std::min(cashHRP.size(), maxHRPSize)) != 0) {
return false;
}
return true;
}
CashAddress::CashAddress(const std::string& string) {
auto withPrefix = string;
if (!std::equal(cashHRP.begin(), cashHRP.end(), string.begin())) {
withPrefix = cashHRP + ":" + string;
}
std::array<char, maxHRPSize + 1> hrp;
std::array<uint8_t, maxDataSize> data;
size_t dataLen;
auto success = cash_decode(hrp.data(), data.data(), &dataLen, withPrefix.c_str()) != 0;
if (!success || std::strncmp(hrp.data(), cashHRP.c_str(), std::min(cashHRP.size(), maxHRPSize)) != 0 || dataLen != CashAddress::size) {
throw std::invalid_argument("Invalid address string");
}
std::copy(data.begin(), data.begin() + dataLen, bytes.begin());
}
CashAddress::CashAddress(const std::vector<uint8_t>& data) {
if (!isValid(data)) {
throw std::invalid_argument("Invalid address key data");
}
std::copy(data.begin(), data.end(), bytes.begin());
}
CashAddress::CashAddress(const PublicKey& publicKey) {
if (publicKey.type != TWPublicKeyTypeSECP256k1) {
throw std::invalid_argument("CashAddress needs a compressed SECP256k1 public key.");
}
std::array<uint8_t, 21> payload;
payload[0] = 0;
ecdsa_get_pubkeyhash(publicKey.bytes.data(), HASHER_SHA2_RIPEMD, payload.data() + 1);
size_t outlen = 0;
auto success = cash_addr_to_data(bytes.data(), &outlen, payload.data(), 21) != 0;
assert(success && outlen == CashAddress::size);
}
std::string CashAddress::string() const {
std::array<char, 129> result;
cash_encode(result.data(), cashHRP.c_str(), bytes.data(), CashAddress::size);
return result.data();
}
Address CashAddress::legacyAddress() const {
std::vector<uint8_t> result(Address::size);
size_t outlen = 0;
cash_data_to_addr(result.data(), &outlen, bytes.data(), CashAddress::size);
assert(outlen == 21 && "Invalid length");
if (result[0] == p2khVersion) {
result[0] = TW::p2pkhPrefix(TWCoinTypeBitcoinCash);
} else if (result[0] == p2shVersion) {
result[0] = TW::p2shPrefix(TWCoinTypeBitcoinCash);
}
return Address(result);
}