Branch data Line data Source code
1 : : // Copyright (c) 2023-present 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 : :
19 [ + - + - ]: 147015 : class CNoDestination
20 : : {
21 : : private:
22 : : CScript m_script;
23 : :
24 : : public:
25 [ + - ]: 4180 : CNoDestination() = default;
26 : 5493 : explicit CNoDestination(const CScript& script) : m_script(script) {}
27 : :
28 : : const CScript& GetScript() const LIFETIMEBOUND { return m_script; }
29 : :
30 : 0 : friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); }
31 : 0 : friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); }
32 : : };
33 : :
34 : : struct PubKeyDestination {
35 : : private:
36 : : CPubKey m_pubkey;
37 : :
38 : : public:
39 [ + + ]: 634 : explicit PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
40 : :
41 [ + - ]: 34 : const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; }
42 : :
43 [ + - ]: 1 : friend bool operator==(const PubKeyDestination& a, const PubKeyDestination& b) { return a.GetPubKey() == b.GetPubKey(); }
44 : 0 : friend bool operator<(const PubKeyDestination& a, const PubKeyDestination& b) { return a.GetPubKey() < b.GetPubKey(); }
45 : : };
46 : :
47 : : struct PKHash : public BaseHash<uint160>
48 : : {
49 [ + - + - : 9 : PKHash() : BaseHash() {}
+ - + - +
- + - + -
+ - + - ]
50 [ + - + - ]: 33534 : explicit PKHash(const uint160& hash) : BaseHash(hash) {}
[ # # ]
51 : : explicit PKHash(const CPubKey& pubkey);
52 : : explicit PKHash(const CKeyID& pubkey_id);
53 : : };
54 : : CKeyID ToKeyID(const PKHash& key_hash);
55 : :
56 : : struct WitnessV0KeyHash;
57 : :
58 : : struct ScriptHash : public BaseHash<uint160>
59 : : {
60 [ + - + - : 5 : ScriptHash() : BaseHash() {}
+ - + - +
- ]
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 : 8669 : explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
67 : : explicit ScriptHash(const CScript& script);
68 : : };
69 : : CScriptID ToScriptID(const ScriptHash& script_hash);
70 : :
71 : : struct WitnessV0ScriptHash : public BaseHash<uint256>
72 : : {
73 [ + + ]: 1091 : WitnessV0ScriptHash() : BaseHash() {}
74 : : explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
75 : : explicit WitnessV0ScriptHash(const CScript& script);
76 : : };
77 : :
78 : : struct WitnessV0KeyHash : public BaseHash<uint160>
79 : : {
80 [ + + ]: 58761 : WitnessV0KeyHash() : BaseHash() {}
81 [ + - + - ]: 315263 : explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
[ + - ]
82 : : explicit WitnessV0KeyHash(const CPubKey& pubkey);
83 : : explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
84 : : };
85 : : CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
86 : :
87 : : struct WitnessV1Taproot : public XOnlyPubKey
88 : : {
89 : 21639 : WitnessV1Taproot() : XOnlyPubKey() {}
90 [ + - ]: 152184 : explicit WitnessV1Taproot(const XOnlyPubKey& xpk) : XOnlyPubKey(xpk) {}
[ + - + + ]
91 : : };
92 : :
93 : : //! CTxDestination subtype to encode any future Witness version
94 [ + - + - : 342 : struct WitnessUnknown
+ - ][ + -
# # # # ]
95 : : {
96 : : private:
97 : : unsigned int m_version;
98 : : std::vector<unsigned char> m_program;
99 : :
100 : : public:
101 : : WitnessUnknown(unsigned int version, const std::vector<unsigned char>& program) : m_version(version), m_program(program) {}
102 [ + - + - ]: 100 : WitnessUnknown(int version, const std::vector<unsigned char>& program) : m_version(static_cast<unsigned int>(version)), m_program(program) {}
[ + - ]
103 : :
104 [ + - ]: 277 : unsigned int GetWitnessVersion() const { return m_version; }
105 [ + + ]: 270 : const std::vector<unsigned char>& GetWitnessProgram() const LIFETIMEBOUND { return m_program; }
106 : :
107 : 3 : friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
108 [ + - + - : 3 : if (w1.GetWitnessVersion() != w2.GetWitnessVersion()) return false;
+ - ][ # #
# # # # ]
109 : 3 : return w1.GetWitnessProgram() == w2.GetWitnessProgram();
110 : : }
111 : :
112 : 20 : friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
113 [ + + ]: 20 : if (w1.GetWitnessVersion() < w2.GetWitnessVersion()) return true;
114 [ + + ]: 14 : if (w1.GetWitnessVersion() > w2.GetWitnessVersion()) return false;
115 : 13 : return w1.GetWitnessProgram() < w2.GetWitnessProgram();
116 : : }
117 : : };
118 : :
119 : : /** Witness program for Pay-to-Anchor output script type */
120 : : inline const std::vector<unsigned char> ANCHOR_BYTES{0x4e, 0x73};
121 : :
122 [ + - + - ]: 254 : struct PayToAnchor : public WitnessUnknown
123 : : {
124 : 58 : PayToAnchor() : WitnessUnknown(1, ANCHOR_BYTES) {
125 [ + - ]: 58 : Assume(CScript::IsPayToAnchor(1, ANCHOR_BYTES));
126 : 58 : };
127 : : };
128 : :
129 : : /**
130 : : * A txout script categorized into standard templates.
131 : : * * CNoDestination: Optionally a script, no corresponding address.
132 : : * * PubKeyDestination: TxoutType::PUBKEY (P2PK), no corresponding address
133 : : * * PKHash: TxoutType::PUBKEYHASH destination (P2PKH address)
134 : : * * ScriptHash: TxoutType::SCRIPTHASH destination (P2SH address)
135 : : * * WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH destination (P2WSH address)
136 : : * * WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH destination (P2WPKH address)
137 : : * * WitnessV1Taproot: TxoutType::WITNESS_V1_TAPROOT destination (P2TR address)
138 : : * * PayToAnchor: TxoutType::ANCHOR destination (P2A address)
139 : : * * WitnessUnknown: TxoutType::WITNESS_UNKNOWN destination (P2W??? address)
140 : : * A CTxDestination is the internal data type encoded in a bitcoin address
141 : : */
142 : : using CTxDestination = std::variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>;
143 : :
144 : : /** Check whether a CTxDestination corresponds to one with an address. */
145 : : bool IsValidDestination(const CTxDestination& dest);
146 : :
147 : : /**
148 : : * Parse a scriptPubKey for the destination.
149 : : *
150 : : * For standard scripts that have addresses (and P2PK as an exception), a corresponding CTxDestination
151 : : * is assigned to addressRet.
152 : : * For all other scripts. addressRet is assigned as a CNoDestination containing the scriptPubKey.
153 : : *
154 : : * Returns true for standard destinations with addresses - P2PKH, P2SH, P2WPKH, P2WSH, P2TR and P2W??? scripts.
155 : : * Returns false for non-standard destinations and those without addresses - P2PK, bare multisig, null data, and nonstandard scripts.
156 : : */
157 : : bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
158 : :
159 : : /**
160 : : * Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH
161 : : * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
162 : : * script for CNoDestination.
163 : : */
164 : : CScript GetScriptForDestination(const CTxDestination& dest);
165 : :
166 : : #endif // BITCOIN_ADDRESSTYPE_H
|