Coverage Report

Created: 2024-10-21 15:10

/root/bitcoin/src/addresstype.h
Line
Count
Source (jump to first uncovered line)
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
19
class CNoDestination
20
{
21
private:
22
    CScript m_script;
23
24
public:
25
0
    CNoDestination() = default;
26
0
    explicit CNoDestination(const CScript& script) : m_script(script) {}
27
28
0
    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
0
    explicit PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
40
41
0
    const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; }
42
43
0
    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
0
    PKHash() : BaseHash() {}
50
0
    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
0
    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
0
    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
0
    WitnessV0ScriptHash() : BaseHash() {}
75
0
    explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
76
    explicit WitnessV0ScriptHash(const CScript& script);
77
};
78
79
struct WitnessV0KeyHash : public BaseHash<uint160>
80
{
81
0
    WitnessV0KeyHash() : BaseHash() {}
82
0
    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
0
    WitnessV1Taproot() : XOnlyPubKey() {}
91
0
    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
0
    WitnessUnknown(unsigned int version, const std::vector<unsigned char>& program) : m_version(version), m_program(program) {}
103
0
    WitnessUnknown(int version, const std::vector<unsigned char>& program) : m_version(static_cast<unsigned int>(version)), m_program(program) {}
104
105
0
    unsigned int GetWitnessVersion() const { return m_version; }
106
0
    const std::vector<unsigned char>& GetWitnessProgram() const LIFETIMEBOUND { return m_program; }
107
108
0
    friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
109
0
        if (w1.GetWitnessVersion() != w2.GetWitnessVersion()) return false;
110
0
        return w1.GetWitnessProgram() == w2.GetWitnessProgram();
111
0
    }
112
113
0
    friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
114
0
        if (w1.GetWitnessVersion() < w2.GetWitnessVersion()) return true;
115
0
        if (w1.GetWitnessVersion() > w2.GetWitnessVersion()) return false;
116
0
        return w1.GetWitnessProgram() < w2.GetWitnessProgram();
117
0
    }
118
};
119
120
struct PayToAnchor : public WitnessUnknown
121
{
122
0
    PayToAnchor() : WitnessUnknown(1, {0x4e, 0x73}) {
123
0
        Assume(CScript::IsPayToAnchor(1, {0x4e, 0x73}));
124
0
    };
125
};
126
127
/**
128
 * A txout script categorized into standard templates.
129
 *  * CNoDestination: Optionally a script, no corresponding address.
130
 *  * PubKeyDestination: TxoutType::PUBKEY (P2PK), no corresponding address
131
 *  * PKHash: TxoutType::PUBKEYHASH destination (P2PKH address)
132
 *  * ScriptHash: TxoutType::SCRIPTHASH destination (P2SH address)
133
 *  * WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH destination (P2WSH address)
134
 *  * WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH destination (P2WPKH address)
135
 *  * WitnessV1Taproot: TxoutType::WITNESS_V1_TAPROOT destination (P2TR address)
136
 *  * PayToAnchor: TxoutType::ANCHOR destination (P2A address)
137
 *  * WitnessUnknown: TxoutType::WITNESS_UNKNOWN destination (P2W??? address)
138
 *  A CTxDestination is the internal data type encoded in a bitcoin address
139
 */
140
using CTxDestination = std::variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>;
141
142
/** Check whether a CTxDestination corresponds to one with an address. */
143
bool IsValidDestination(const CTxDestination& dest);
144
145
/**
146
 * Parse a scriptPubKey for the destination.
147
 *
148
 * For standard scripts that have addresses (and P2PK as an exception), a corresponding CTxDestination
149
 * is assigned to addressRet.
150
 * For all other scripts. addressRet is assigned as a CNoDestination containing the scriptPubKey.
151
 *
152
 * Returns true for standard destinations with addresses - P2PKH, P2SH, P2WPKH, P2WSH, P2TR and P2W??? scripts.
153
 * Returns false for non-standard destinations and those without addresses - P2PK, bare multisig, null data, and nonstandard scripts.
154
 */
155
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
156
157
/**
158
 * Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH
159
 * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
160
 * script for CNoDestination.
161
 */
162
CScript GetScriptForDestination(const CTxDestination& dest);
163
164
#endif // BITCOIN_ADDRESSTYPE_H