addresstype.h
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 class CNoDestination 20 { 21 private: 22 CScript m_script; 23 24 public: 25 CNoDestination() = default; 26 explicit CNoDestination(const CScript& script) : m_script(script) {} 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 34 struct PubKeyDestination { 35 private: 36 CPubKey m_pubkey; 37 38 public: 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 47 struct PKHash : public BaseHash<uint160> 48 { 49 PKHash() : BaseHash() {} 50 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 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 explicit ScriptHash(const uint160& hash) : BaseHash(hash) {} 67 explicit ScriptHash(const CScript& script); 68 explicit ScriptHash(const CScriptID& script); 69 }; 70 CScriptID ToScriptID(const ScriptHash& script_hash); 71 72 struct WitnessV0ScriptHash : public BaseHash<uint256> 73 { 74 WitnessV0ScriptHash() : BaseHash() {} 75 explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {} 76 explicit WitnessV0ScriptHash(const CScript& script); 77 }; 78 79 struct WitnessV0KeyHash : public BaseHash<uint160> 80 { 81 WitnessV0KeyHash() : BaseHash() {} 82 explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {} 83 explicit WitnessV0KeyHash(const CPubKey& pubkey); 84 explicit WitnessV0KeyHash(const PKHash& pubkey_hash); 85 }; 86 CKeyID ToKeyID(const WitnessV0KeyHash& key_hash); 87 88 struct WitnessV1Taproot : public XOnlyPubKey 89 { 90 WitnessV1Taproot() : XOnlyPubKey() {} 91 explicit WitnessV1Taproot(const XOnlyPubKey& xpk) : XOnlyPubKey(xpk) {} 92 }; 93 94 //! CTxDestination subtype to encode any future Witness version 95 struct WitnessUnknown 96 { 97 private: 98 unsigned int m_version; 99 std::vector<unsigned char> m_program; 100 101 public: 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 120 /** Witness program for Pay-to-Anchor output script type */ 121 static const std::vector<unsigned char> ANCHOR_BYTES{0x4e, 0x73}; 122 123 struct PayToAnchor : public WitnessUnknown 124 { 125 PayToAnchor() : WitnessUnknown(1, ANCHOR_BYTES) { 126 Assume(CScript::IsPayToAnchor(1, ANCHOR_BYTES)); 127 }; 128 }; 129 130 /** 131 * A txout script categorized into standard templates. 132 * * CNoDestination: Optionally a script, no corresponding address. 133 * * PubKeyDestination: TxoutType::PUBKEY (P2PK), no corresponding address 134 * * PKHash: TxoutType::PUBKEYHASH destination (P2PKH address) 135 * * ScriptHash: TxoutType::SCRIPTHASH destination (P2SH address) 136 * * WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH destination (P2WSH address) 137 * * WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH destination (P2WPKH address) 138 * * WitnessV1Taproot: TxoutType::WITNESS_V1_TAPROOT destination (P2TR address) 139 * * PayToAnchor: TxoutType::ANCHOR destination (P2A address) 140 * * WitnessUnknown: TxoutType::WITNESS_UNKNOWN destination (P2W??? address) 141 * A CTxDestination is the internal data type encoded in a bitcoin address 142 */ 143 using CTxDestination = std::variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>; 144 145 /** Check whether a CTxDestination corresponds to one with an address. */ 146 bool IsValidDestination(const CTxDestination& dest); 147 148 /** 149 * Parse a scriptPubKey for the destination. 150 * 151 * For standard scripts that have addresses (and P2PK as an exception), a corresponding CTxDestination 152 * is assigned to addressRet. 153 * For all other scripts. addressRet is assigned as a CNoDestination containing the scriptPubKey. 154 * 155 * Returns true for standard destinations with addresses - P2PKH, P2SH, P2WPKH, P2WSH, P2TR and P2W??? scripts. 156 * Returns false for non-standard destinations and those without addresses - P2PK, bare multisig, null data, and nonstandard scripts. 157 */ 158 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet); 159 160 /** 161 * Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH 162 * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty 163 * script for CNoDestination. 164 */ 165 CScript GetScriptForDestination(const CTxDestination& dest); 166 167 #endif // BITCOIN_ADDRESSTYPE_H