Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
addresstype.cpp
Go to the documentation of this file.
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#include <addresstype.h>
6
7#include <crypto/sha256.h>
8#include <hash.h>
9#include <pubkey.h>
10#include <script/script.h>
11#include <script/solver.h>
12#include <uint256.h>
13#include <util/hash_type.h>
14
15#include <cassert>
16#include <vector>
17
18typedef std::vector<unsigned char> valtype;
19
21
22PKHash::PKHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
24
25WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
27
29{
30 return CKeyID{uint160{key_hash}};
31}
32
37
42
47
49{
50 std::vector<valtype> vSolutions;
51 TxoutType whichType = Solver(scriptPubKey, vSolutions);
52
53 switch (whichType) {
54 case TxoutType::PUBKEY: {
56 if (!pubKey.IsValid()) {
57 addressRet = CNoDestination(scriptPubKey);
58 } else {
60 }
61 return false;
62 }
65 return true;
66 }
69 return true;
70 }
73 std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
74 addressRet = hash;
75 return true;
76 }
79 std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
80 addressRet = hash;
81 return true;
82 }
85 std::copy(vSolutions[0].begin(), vSolutions[0].end(), tap.begin());
87 return true;
88 }
89 case TxoutType::ANCHOR: {
91 return true;
92 }
95 return true;
96 }
100 addressRet = CNoDestination(scriptPubKey);
101 return false;
102 } // no default case, so the compiler can warn about missing cases
103 assert(false);
104}
105
106namespace {
107class CScriptVisitor
108{
109public:
110 CScript operator()(const CNoDestination& dest) const
111 {
112 return dest.GetScript();
113 }
114
115 CScript operator()(const PubKeyDestination& dest) const
116 {
117 return CScript() << ToByteVector(dest.GetPubKey()) << OP_CHECKSIG;
118 }
119
120 CScript operator()(const PKHash& keyID) const
121 {
123 }
124
125 CScript operator()(const ScriptHash& scriptID) const
126 {
128 }
129
130 CScript operator()(const WitnessV0KeyHash& id) const
131 {
132 return CScript() << OP_0 << ToByteVector(id);
133 }
134
135 CScript operator()(const WitnessV0ScriptHash& id) const
136 {
137 return CScript() << OP_0 << ToByteVector(id);
138 }
139
140 CScript operator()(const WitnessV1Taproot& tap) const
141 {
142 return CScript() << OP_1 << ToByteVector(tap);
143 }
144
145 CScript operator()(const WitnessUnknown& id) const
146 {
147 return CScript() << CScript::EncodeOP_N(id.GetWitnessVersion()) << id.GetWitnessProgram();
148 }
149};
150
151class ValidDestinationVisitor
152{
153public:
154 bool operator()(const CNoDestination& dest) const { return false; }
155 bool operator()(const PubKeyDestination& dest) const { return false; }
156 bool operator()(const PKHash& dest) const { return true; }
157 bool operator()(const ScriptHash& dest) const { return true; }
158 bool operator()(const WitnessV0KeyHash& dest) const { return true; }
159 bool operator()(const WitnessV0ScriptHash& dest) const { return true; }
160 bool operator()(const WitnessV1Taproot& dest) const { return true; }
161 bool operator()(const WitnessUnknown& dest) const { return true; }
162};
163} // namespace
164
166{
167 return std::visit(CScriptVisitor(), dest);
168}
169
171 return std::visit(ValidDestinationVisitor(), dest);
172}
CScriptID ToScriptID(const ScriptHash &script_hash)
std::vector< unsigned char > valtype
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
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)
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
unsigned char * begin()
Definition hash_type.h:18
A reference to a CKey: the Hash160 of its serialized public key.
Definition pubkey.h:29
const CScript & GetScript() const LIFETIMEBOUND
Definition addresstype.h:28
An encapsulated public key.
Definition pubkey.h:43
A hasher class for SHA-256.
Definition sha256.h:14
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition sha256.cpp:725
CSHA256 & Write(const unsigned char *data, size_t len)
Definition sha256.cpp:699
Serialized script, used inside transaction inputs and outputs.
Definition script.h:406
static opcodetype EncodeOP_N(int n)
Definition script.h:515
A reference to a CScript: the Hash160 of its serialization.
Definition script.h:597
size_type size() const
Definition prevector.h:247
value_type * data()
Definition prevector.h:464
160-bit opaque blob.
Definition uint256.h:184
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition hash.h:100
@ OP_CHECKSIG
Definition script.h:191
@ OP_EQUAL
Definition script.h:147
@ OP_DUP
Definition script.h:126
@ OP_HASH160
Definition script.h:188
@ OP_1
Definition script.h:84
@ OP_0
Definition script.h:77
@ OP_EQUALVERIFY
Definition script.h:148
std::vector< unsigned char > ToByteVector(const T &in)
Definition script.h:68
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition solver.cpp:141
TxoutType
Definition solver.h:22
@ WITNESS_V1_TAPROOT
@ WITNESS_UNKNOWN
Only for Witness versions not already defined above.
@ ANCHOR
anyone can spend script
@ WITNESS_V0_SCRIPTHASH
@ NULL_DATA
unspendable OP_RETURN script that carries data
@ WITNESS_V0_KEYHASH
const CPubKey & GetPubKey() const LIFETIMEBOUND
Definition addresstype.h:41
CTxDestination subtype to encode any future Witness version.
Definition addresstype.h:95
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())