Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2023-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_ADDRESSTYPE_H6#define BITCOIN_ADDRESSTYPE_H8#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>15#include <algorithm>16#include <variant>17#include <vector>19class CNoDestination20{21private:22CScript m_script;24public:25CNoDestination() = default;26explicit CNoDestination(const CScript& script) : m_script(script) {}28const CScript& GetScript() const LIFETIMEBOUND { return m_script; }30friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); }31friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); }32};34struct PubKeyDestination {35private:36CPubKey m_pubkey;38public:39explicit PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}41const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; }43friend bool operator==(const PubKeyDestination& a, const PubKeyDestination& b) { return a.GetPubKey() == b.GetPubKey(); }44friend bool operator<(const PubKeyDestination& a, const PubKeyDestination& b) { return a.GetPubKey() < b.GetPubKey(); }45};47struct PKHash : public BaseHash<uint160>48{49PKHash() : BaseHash() {}50explicit PKHash(const uint160& hash) : BaseHash(hash) {}51explicit PKHash(const CPubKey& pubkey);52explicit PKHash(const CKeyID& pubkey_id);53};54CKeyID ToKeyID(const PKHash& key_hash);56struct WitnessV0KeyHash;58struct ScriptHash : public BaseHash<uint160>59{60ScriptHash() : BaseHash() {}61// These don't do what you'd expect.62// Use ScriptHash(GetScriptForDestination(...)) instead.63explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;64explicit ScriptHash(const PKHash& hash) = delete;66explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}67explicit ScriptHash(const CScript& script);68explicit ScriptHash(const CScriptID& script);69};70CScriptID ToScriptID(const ScriptHash& script_hash);72struct WitnessV0ScriptHash : public BaseHash<uint256>73{74WitnessV0ScriptHash() : BaseHash() {}75explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}76explicit WitnessV0ScriptHash(const CScript& script);77};79struct WitnessV0KeyHash : public BaseHash<uint160>80{81WitnessV0KeyHash() : BaseHash() {}82explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}83explicit WitnessV0KeyHash(const CPubKey& pubkey);84explicit WitnessV0KeyHash(const PKHash& pubkey_hash);85};86CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);88struct WitnessV1Taproot : public XOnlyPubKey89{90WitnessV1Taproot() : XOnlyPubKey() {}91explicit WitnessV1Taproot(const XOnlyPubKey& xpk) : XOnlyPubKey(xpk) {}92};94//! CTxDestination subtype to encode any future Witness version95struct WitnessUnknown96{97private:98unsigned int m_version;99std::vector<unsigned char> m_program;101public:102WitnessUnknown(unsigned int version, const std::vector<unsigned char>& program) : m_version(version), m_program(program) {}103WitnessUnknown(int version, const std::vector<unsigned char>& program) : m_version(static_cast<unsigned int>(version)), m_program(program) {}105unsigned int GetWitnessVersion() const { return m_version; }106const std::vector<unsigned char>& GetWitnessProgram() const LIFETIMEBOUND { return m_program; }108friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {109if (w1.GetWitnessVersion() != w2.GetWitnessVersion()) return false;110return w1.GetWitnessProgram() == w2.GetWitnessProgram();111}113friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {114if (w1.GetWitnessVersion() < w2.GetWitnessVersion()) return true;115if (w1.GetWitnessVersion() > w2.GetWitnessVersion()) return false;116return w1.GetWitnessProgram() < w2.GetWitnessProgram();117}118};120/** Witness program for Pay-to-Anchor output script type */121inline const std::vector<unsigned char> ANCHOR_BYTES{0x4e, 0x73};123struct PayToAnchor : public WitnessUnknown124{125PayToAnchor() : WitnessUnknown(1, ANCHOR_BYTES) {126Assume(CScript::IsPayToAnchor(1, ANCHOR_BYTES));127};128};130/**131* A txout script categorized into standard templates.132* * CNoDestination: Optionally a script, no corresponding address.133* * PubKeyDestination: TxoutType::PUBKEY (P2PK), no corresponding address134* * 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 address142*/143using CTxDestination = std::variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>;145/** Check whether a CTxDestination corresponds to one with an address. */146bool IsValidDestination(const CTxDestination& dest);148/**149* Parse a scriptPubKey for the destination.150*151* For standard scripts that have addresses (and P2PK as an exception), a corresponding CTxDestination152* 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*/158bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);160/**161* Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH162* script for a CKeyID destination, a P2SH script for a CScriptID, and an empty163* script for CNoDestination.164*/165CScript GetScriptForDestination(const CTxDestination& dest);167#endif // BITCOIN_ADDRESSTYPE_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.