Skip to content

Commit 1a98a51

Browse files
committed
Allow CNoDestination to represent a raw script
Even if a script is not a standard destination type, it can still be useful to have a CTxDestination that stores the script.
1 parent 8dd0670 commit 1a98a51

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/addresstype.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
9393
case TxoutType::MULTISIG:
9494
case TxoutType::NULL_DATA:
9595
case TxoutType::NONSTANDARD:
96+
addressRet = CNoDestination(scriptPubKey);
9697
return false;
9798
} // no default case, so the compiler can warn about missing cases
9899
assert(false);
@@ -104,7 +105,7 @@ class CScriptVisitor
104105
public:
105106
CScript operator()(const CNoDestination& dest) const
106107
{
107-
return CScript();
108+
return dest.GetScript();
108109
}
109110

110111
CScript operator()(const PKHash& keyID) const

src/addresstype.h

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414
#include <algorithm>
1515

1616
class CNoDestination {
17+
private:
18+
CScript m_script;
19+
1720
public:
18-
friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
19-
friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
21+
CNoDestination() = default;
22+
CNoDestination(const CScript& script) : m_script(script) {}
23+
24+
const CScript& GetScript() const { return m_script; }
25+
26+
friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); }
27+
friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); }
2028
};
2129

2230
struct PKHash : public BaseHash<uint160>
@@ -93,14 +101,14 @@ struct WitnessUnknown
93101
};
94102

95103
/**
96-
* A txout script template with a specific destination. It is either:
97-
* * CNoDestination: no destination set
98-
* * PKHash: TxoutType::PUBKEYHASH destination (P2PKH)
99-
* * ScriptHash: TxoutType::SCRIPTHASH destination (P2SH)
100-
* * WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH destination (P2WSH)
101-
* * WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH destination (P2WPKH)
102-
* * WitnessV1Taproot: TxoutType::WITNESS_V1_TAPROOT destination (P2TR)
103-
* * WitnessUnknown: TxoutType::WITNESS_UNKNOWN destination (P2W???)
104+
* A txout script categorized into standard templates.
105+
* * CNoDestination: Optionally a script, no corresponding address.
106+
* * PKHash: TxoutType::PUBKEYHASH destination (P2PKH address)
107+
* * ScriptHash: TxoutType::SCRIPTHASH destination (P2SH address)
108+
* * WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH destination (P2WSH address)
109+
* * WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH destination (P2WPKH address)
110+
* * WitnessV1Taproot: TxoutType::WITNESS_V1_TAPROOT destination (P2TR address)
111+
* * WitnessUnknown: TxoutType::WITNESS_UNKNOWN destination (P2W??? address)
104112
* A CTxDestination is the internal data type encoded in a bitcoin address
105113
*/
106114
using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown>;
@@ -109,9 +117,14 @@ using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV
109117
bool IsValidDestination(const CTxDestination& dest);
110118

111119
/**
112-
* Parse a standard scriptPubKey for the destination address. Assigns result to
113-
* the addressRet parameter and returns true if successful. Currently only works for P2PK,
114-
* P2PKH, P2SH, P2WPKH, and P2WSH scripts.
120+
* Parse a scriptPubKey for the destination.
121+
*
122+
* For standard scripts that have addresses (and P2PK as an exception), a corresponding CTxDestination
123+
* is assigned to addressRet.
124+
* For all other scripts. addressRet is assigned as a CNoDestination containing the scriptPubKey.
125+
*
126+
* Returns true for standard destinations - P2PK, P2PKH, P2SH, P2WPKH, P2WSH, and P2TR scripts.
127+
* Returns false for non-standard destinations - P2PK with invalid pubkeys, P2W???, bare multisig, null data, and nonstandard scripts.
115128
*/
116129
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
117130

0 commit comments

Comments
 (0)