Bitcoin Core 28.99.0
P2P Digital Currency
addresstype.h
Go to the documentation of this file.
1// Copyright (c) 2023 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_ADDRESSTYPE_H
6#define BITCOIN_ADDRESSTYPE_H
7
8#include <attributes.h>
9#include <pubkey.h>
10#include <script/script.h>
11#include <uint256.h>
12#include <util/check.h>
13#include <util/hash_type.h>
14
15#include <algorithm>
16#include <variant>
17#include <vector>
18
20{
21private:
23
24public:
25 CNoDestination() = default;
27
28 const CScript& GetScript() const LIFETIMEBOUND { return m_script; }
29
30 friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); }
31 friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); }
32};
33
35private:
37
38public:
39 explicit PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
40
41 const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; }
42
43 friend bool operator==(const PubKeyDestination& a, const PubKeyDestination& b) { return a.GetPubKey() == b.GetPubKey(); }
44 friend bool operator<(const PubKeyDestination& a, const PubKeyDestination& b) { return a.GetPubKey() < b.GetPubKey(); }
45};
46
47struct PKHash : public BaseHash<uint160>
48{
50 explicit PKHash(const uint160& hash) : BaseHash(hash) {}
51 explicit PKHash(const CPubKey& pubkey);
52 explicit PKHash(const CKeyID& pubkey_id);
53};
54CKeyID ToKeyID(const PKHash& key_hash);
55
56struct WitnessV0KeyHash;
57
58struct ScriptHash : public BaseHash<uint160>
59{
61 // These don't do what you'd expect.
62 // Use ScriptHash(GetScriptForDestination(...)) instead.
63 explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
64 explicit ScriptHash(const PKHash& hash) = delete;
65
66 explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
67 explicit ScriptHash(const CScript& script);
68 explicit ScriptHash(const CScriptID& script);
69};
70CScriptID ToScriptID(const ScriptHash& script_hash);
71
72struct WitnessV0ScriptHash : public BaseHash<uint256>
73{
75 explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
76 explicit WitnessV0ScriptHash(const CScript& script);
77};
78
79struct WitnessV0KeyHash : public BaseHash<uint160>
80{
82 explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
83 explicit WitnessV0KeyHash(const CPubKey& pubkey);
84 explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
85};
86CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
87
89{
91 explicit WitnessV1Taproot(const XOnlyPubKey& xpk) : XOnlyPubKey(xpk) {}
92};
93
96{
97private:
98 unsigned int m_version;
99 std::vector<unsigned char> m_program;
100
101public:
102 WitnessUnknown(unsigned int version, const std::vector<unsigned char>& program) : m_version(version), m_program(program) {}
103 WitnessUnknown(int version, const std::vector<unsigned char>& program) : m_version(static_cast<unsigned int>(version)), m_program(program) {}
104
105 unsigned int GetWitnessVersion() const { return m_version; }
106 const std::vector<unsigned char>& GetWitnessProgram() const LIFETIMEBOUND { return m_program; }
107
108 friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
109 if (w1.GetWitnessVersion() != w2.GetWitnessVersion()) return false;
110 return w1.GetWitnessProgram() == w2.GetWitnessProgram();
111 }
112
113 friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
114 if (w1.GetWitnessVersion() < w2.GetWitnessVersion()) return true;
115 if (w1.GetWitnessVersion() > w2.GetWitnessVersion()) return false;
116 return w1.GetWitnessProgram() < w2.GetWitnessProgram();
117 }
118};
119
121{
122 PayToAnchor() : WitnessUnknown(1, {0x4e, 0x73}) {
123 Assume(CScript::IsPayToAnchor(1, {0x4e, 0x73}));
124 };
125};
126
140using CTxDestination = std::variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>;
141
143bool IsValidDestination(const CTxDestination& dest);
144
155bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
156
163
164#endif // BITCOIN_ADDRESSTYPE_H
CScriptID ToScriptID(const ScriptHash &script_hash)
Definition: addresstype.cpp:39
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
Definition: addresstype.cpp:49
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
CKeyID ToKeyID(const PKHash &key_hash)
Definition: addresstype.cpp:29
#define LIFETIMEBOUND
Definition: attributes.h:16
#define Assume(val)
Assume is the identity function.
Definition: check.h:97
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: addresstype.h:31
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: addresstype.h:30
CScript m_script
Definition: addresstype.h:22
CNoDestination(const CScript &script)
Definition: addresstype.h:26
CNoDestination()=default
const CScript & GetScript() const LIFETIMEBOUND
Definition: addresstype.h:28
An encapsulated public key.
Definition: pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
bool IsPayToAnchor() const
Definition: script.cpp:207
A reference to a CScript: the Hash160 of its serialization.
Definition: script.h:602
160-bit opaque blob.
Definition: uint256.h:178
256-bit opaque blob.
Definition: uint256.h:190
PKHash(const uint160 &hash)
Definition: addresstype.h:50
PKHash()
Definition: addresstype.h:49
const CPubKey & GetPubKey() const LIFETIMEBOUND
Definition: addresstype.h:41
friend bool operator<(const PubKeyDestination &a, const PubKeyDestination &b)
Definition: addresstype.h:44
PubKeyDestination(const CPubKey &pubkey)
Definition: addresstype.h:39
friend bool operator==(const PubKeyDestination &a, const PubKeyDestination &b)
Definition: addresstype.h:43
ScriptHash(const WitnessV0KeyHash &hash)=delete
ScriptHash(const uint160 &hash)
Definition: addresstype.h:66
ScriptHash(const PKHash &hash)=delete
CTxDestination subtype to encode any future Witness version.
Definition: addresstype.h:96
WitnessUnknown(int version, const std::vector< unsigned char > &program)
Definition: addresstype.h:103
WitnessUnknown(unsigned int version, const std::vector< unsigned char > &program)
Definition: addresstype.h:102
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: addresstype.h:113
unsigned int m_version
Definition: addresstype.h:98
const std::vector< unsigned char > & GetWitnessProgram() const LIFETIMEBOUND
Definition: addresstype.h:106
unsigned int GetWitnessVersion() const
Definition: addresstype.h:105
std::vector< unsigned char > m_program
Definition: addresstype.h:99
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: addresstype.h:108
WitnessV0KeyHash(const uint160 &hash)
Definition: addresstype.h:82
WitnessV0ScriptHash(const uint256 &hash)
Definition: addresstype.h:75
WitnessV1Taproot(const XOnlyPubKey &xpk)
Definition: addresstype.h:91