forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBech32Address.cpp
82 lines (71 loc) · 2.38 KB
/
Bech32Address.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright © 2017-2022 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 "Bech32Address.h"
#include "Bech32.h"
#include "Data.h"
#include <TrezorCrypto/ecdsa.h>
using namespace TW;
bool Bech32Address::isValid(const std::string& addr) {
return isValid(addr, "");
}
bool Bech32Address::isValid(const std::string& addr, const std::string& hrp) {
auto dec = Bech32::decode(addr);
// check hrp prefix (if given)
if (hrp.length() > 0 && std::get<0>(dec).compare(0, hrp.length(), hrp) != 0) {
return false;
}
if (std::get<1>(dec).empty()) {
return false;
}
Data conv;
auto success = Bech32::convertBits<5, 8, false>(conv, std::get<1>(dec));
if (!success || conv.size() < 2 || conv.size() > 40) {
return false;
}
return true;
}
bool Bech32Address::decode(const std::string& addr, Bech32Address& obj_out, const std::string& hrp) {
auto dec = Bech32::decode(addr);
// check hrp prefix (if given)
if (hrp.length() > 0 && std::get<0>(dec).compare(0, hrp.length(), hrp) != 0) {
return false;
}
if (std::get<1>(dec).empty()) {
return false;
}
Data conv;
auto success = Bech32::convertBits<5, 8, false>(conv, std::get<1>(dec));
if (!success || conv.size() < 2 || conv.size() > 40) {
return false;
}
obj_out.setHrp(std::get<0>(dec));
obj_out.setKey(conv);
return true;
}
Bech32Address::Bech32Address(const std::string& hrp, Hash::Hasher hasher, const PublicKey& publicKey)
: hrp(hrp) {
bool skipTypeByte = false;
// Extended-key / keccak-hash skips first byte (Evmos)
if (publicKey.type == TWPublicKeyTypeSECP256k1Extended || hasher == Hash::HasherKeccak256) {
skipTypeByte = true;
}
const auto hash = publicKey.hash({}, hasher, skipTypeByte);
auto key = subData(hash, hash.size() - 20, 20);
setKey(key);
}
std::string Bech32Address::string() const {
Data enc;
if (!Bech32::convertBits<8, 5, true>(enc, keyHash)) {
return "";
}
std::string result = Bech32::encode(hrp, enc, Bech32::ChecksumVariant::Bech32);
// check back
Bech32Address obj;
if (!decode(result, obj, hrp)) {
return "";
}
return result;
}