forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBech32Address.h
67 lines (48 loc) · 2.36 KB
/
Bech32Address.h
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
// 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.
#pragma once
#include "Data.h"
#include "PublicKey.h"
#include "Hash.h"
#include <string>
#include <memory>
namespace TW {
/// Bech32 string address, used by Cosmos, etc. Variation are the hash function.
class Bech32Address {
private:
/// Chain-specific human-readable part. If missing (""), no prefix check is done.
/// \see https://github.com/satoshilabs/slips/blob/master/slip-0173.md
std::string hrp;
/// Public key hash.
Data keyHash;
public:
/// Determines whether a string makes a valid Bech32 address.
static bool isValid(const std::string& addr);
/// Determines whether a string makes a valid Bech32 address, and the HRP matches.
static bool isValid(const std::string& addr, const std::string& hrp);
/// Decodes an address and create an address object out of it.
/// obj_out: Pass-by-ref, result is initialized here if possible, it can be a derived address type.
/// hrp: the expected hrp prefix (if missing ("") no prefix check is done).
/// \returns true if success.
static bool decode(const std::string& addr, Bech32Address& obj_out, const std::string& hrp);
/// Initializes an empty address.
Bech32Address(const std::string& hrp_in) : hrp(std::move(hrp_in)) {}
/// Initializes an address with a key hash.
Bech32Address(const std::string& hrp, const Data& keyHash) : hrp(std::move(hrp)), keyHash(std::move(keyHash)) {}
/// Initialization from public key --> chain specific hash methods
Bech32Address(const std::string& hrp, Hash::Hasher hasher, const PublicKey& publicKey);
void setHrp(const std::string& hrp_in) { hrp = std::move(hrp_in); }
void setKey(const Data& keyHash_in) { keyHash = std::move(keyHash_in); }
inline const std::string& getHrp() const { return hrp; }
inline const Data& getKeyHash() const { return keyHash; }
/// Encodes the address.
/// \returns encoded address string, or empty string on failure.
std::string string() const;
bool operator==(const Bech32Address& rhs) const { return hrp == rhs.hrp && keyHash == rhs.keyHash; }
private:
Bech32Address() = default;
};
} // namespace TW