2014-08-23 03:35:51 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2023-04-25 13:51:26 +02:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-09-09 10:00:42 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-08-23 03:35:51 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#ifndef BITCOIN_SCRIPT_STANDARD_H
|
|
|
|
#define BITCOIN_SCRIPT_STANDARD_H
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <script/interpreter.h>
|
|
|
|
#include <uint256.h>
|
2023-04-26 08:28:10 +02:00
|
|
|
#include <util/hash_type.h>
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2021-01-04 11:20:02 +01:00
|
|
|
#include <variant>
|
2020-05-27 13:15:53 +02:00
|
|
|
#include <string>
|
|
|
|
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2015-11-09 19:16:38 +01:00
|
|
|
static const bool DEFAULT_ACCEPT_DATACARRIER = true;
|
|
|
|
|
2014-10-28 22:47:18 +01:00
|
|
|
class CKeyID;
|
2014-11-04 14:34:04 +01:00
|
|
|
class CScript;
|
2023-04-05 23:48:02 +02:00
|
|
|
struct ScriptHash;
|
|
|
|
|
2014-09-25 04:24:46 +02:00
|
|
|
/** A reference to a CScript: the Hash160 of its serialization (see script.h) */
|
2023-04-05 23:48:02 +02:00
|
|
|
class CScriptID : public BaseHash<uint160>
|
2014-09-25 04:24:46 +02:00
|
|
|
{
|
|
|
|
public:
|
2023-04-05 23:48:02 +02:00
|
|
|
CScriptID() : BaseHash() {}
|
Merge #13002: Do not treat bare multisig outputs as IsMine unless watched
7d0f80b Use anonymous namespace instead of static functions (Pieter Wuille)
b61fb71 Mention removal of bare multisig IsMine in release notes (Pieter Wuille)
9c2a8b8 Do not treat bare multisig as IsMine (Pieter Wuille)
08f3228 Optimization: only test for witness scripts at top level (Pieter Wuille)
3619735 Track difference between scriptPubKey and P2SH execution in IsMine (Pieter Wuille)
ac6ec62 Switch to a private version of SigVersion inside IsMine (Pieter Wuille)
19fc973 Do not expose SigVersion argument to IsMine (Pieter Wuille)
fb1dfbb Remove unused IsMine overload (Pieter Wuille)
952d821 Make CScript -> CScriptID conversion explicit (Pieter Wuille)
Pull request description:
Currently our wallet code will treat bare multisig outputs (meaning scriptPubKeys with multiple public keys + `OP_CHECKMULTISIG` operator in it) as ours without the user asking for it, as long as all private keys in it are in our wallet.
This is a pointless feature. As it only works when all private keys are in one place, it's useless compared to single key outputs (P2PK, P2PKH, P2WPKH, P2SH-P2WPKH), and worse in terms of space, cost, UTXO size, and ability to test (due to lack of address format for them).
Furthermore, they are problematic in that producing a list of all `scriptPubKeys` we accept is not tractable (it involves all combinations of all public keys that are ours). In further wallet changes I'd like to move to a model where all scriptPubKeys that are treated as ours are explicit, rather than defined by whatever keys we have. The current behavior of the wallet is very hard to model in such a design, so I'd like to get rid of it.
I think there are two options:
* Remove it entirely (do not ever accept bare multisig outputs as ours, unless watched)
* Only accept bare multisig outputs in situations where the P2SH version of that output would also be acceptable
This PR implements the first option. The second option was explored in #12874.
Tree-SHA512: 917ed45b3cac864cee53e27f9a3e900390c576277fbd6751b1250becea04d692b3b426fa09065a3399931013bd579c4f3dbeeb29d51d19ed0c64da75d430ad9a
2018-04-26 19:48:50 +02:00
|
|
|
explicit CScriptID(const CScript& in);
|
2023-04-05 23:48:02 +02:00
|
|
|
explicit CScriptID(const uint160& in) : BaseHash(in) {}
|
|
|
|
explicit CScriptID(const ScriptHash& in);
|
2014-09-25 04:24:46 +02:00
|
|
|
};
|
|
|
|
|
2017-08-22 09:31:27 +02:00
|
|
|
/**
|
|
|
|
* Default setting for nMaxDatacarrierBytes. 80 bytes of data, +1 for OP_RETURN,
|
|
|
|
* +2 for the pushdata opcodes.
|
|
|
|
*/
|
|
|
|
static const unsigned int MAX_OP_RETURN_RELAY = 83;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A data carrying output is an unspendable output containing data. The script
|
2022-09-15 16:24:07 +02:00
|
|
|
* type is designated as TxoutType::NULL_DATA.
|
2017-08-22 09:31:27 +02:00
|
|
|
*/
|
2015-06-27 21:21:41 +02:00
|
|
|
extern bool fAcceptDatacarrier;
|
2017-08-22 09:31:27 +02:00
|
|
|
|
2022-09-15 16:24:07 +02:00
|
|
|
/** Maximum size of TxoutType::NULL_DATA scripts that this node considers standard. */
|
2014-10-11 01:55:14 +02:00
|
|
|
extern unsigned nMaxDatacarrierBytes;
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2014-11-10 07:40:01 +01:00
|
|
|
/**
|
|
|
|
* Mandatory script verification flags that all new blocks must comply with for
|
|
|
|
* them to be valid. (but old blocks may not comply with) Currently just P2SH,
|
2020-05-27 12:14:55 +02:00
|
|
|
* but in the future other flags may be added.
|
2017-08-22 09:31:27 +02:00
|
|
|
*
|
2020-01-02 17:08:52 +01:00
|
|
|
* Failing one of these tests may trigger a DoS ban - see CheckInputScripts() for
|
2014-11-10 07:40:01 +01:00
|
|
|
* details.
|
|
|
|
*/
|
2014-08-23 03:35:51 +02:00
|
|
|
static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH;
|
|
|
|
|
2022-09-15 16:24:07 +02:00
|
|
|
enum class TxoutType
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
2022-09-15 16:24:07 +02:00
|
|
|
NONSTANDARD,
|
2014-08-23 03:35:51 +02:00
|
|
|
// 'standard' transaction types:
|
2022-09-15 16:24:07 +02:00
|
|
|
PUBKEY,
|
|
|
|
PUBKEYHASH,
|
|
|
|
SCRIPTHASH,
|
|
|
|
MULTISIG,
|
|
|
|
NULL_DATA, //!< unspendable OP_RETURN script that carries data
|
2014-08-23 03:35:51 +02:00
|
|
|
};
|
|
|
|
|
2014-09-11 19:15:29 +02:00
|
|
|
class CNoDestination {
|
|
|
|
public:
|
|
|
|
friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
|
|
|
|
friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
|
|
|
|
};
|
|
|
|
|
2023-04-05 23:48:02 +02:00
|
|
|
struct PKHash : public BaseHash<uint160>
|
2019-05-09 18:04:52 +02:00
|
|
|
{
|
2023-04-05 23:48:02 +02:00
|
|
|
PKHash() : BaseHash() {}
|
|
|
|
explicit PKHash(const uint160& hash) : BaseHash(hash) {}
|
2019-05-09 18:04:52 +02:00
|
|
|
explicit PKHash(const CPubKey& pubkey);
|
2023-04-05 23:48:02 +02:00
|
|
|
explicit PKHash(const CKeyID& pubkey_id);
|
2019-05-09 18:04:52 +02:00
|
|
|
};
|
2023-04-05 23:48:02 +02:00
|
|
|
CKeyID ToKeyID(const PKHash& key_hash);
|
2019-05-09 18:04:52 +02:00
|
|
|
|
2023-04-05 23:48:02 +02:00
|
|
|
struct ScriptHash : public BaseHash<uint160>
|
2019-05-09 18:04:52 +02:00
|
|
|
{
|
2023-04-05 23:48:02 +02:00
|
|
|
ScriptHash() : BaseHash() {}
|
2020-01-16 19:23:15 +01:00
|
|
|
// These don't do what you'd expect.
|
|
|
|
// Use ScriptHash(GetScriptForDestination(...)) instead.
|
|
|
|
explicit ScriptHash(const PKHash& hash) = delete;
|
2020-06-21 10:04:09 +02:00
|
|
|
|
2023-04-05 23:48:02 +02:00
|
|
|
explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
|
2019-05-09 18:04:52 +02:00
|
|
|
explicit ScriptHash(const CScript& script);
|
2023-04-05 23:48:02 +02:00
|
|
|
explicit ScriptHash(const CScriptID& script);
|
2019-05-09 18:04:52 +02:00
|
|
|
};
|
|
|
|
|
2017-08-22 09:31:27 +02:00
|
|
|
/**
|
2014-11-10 07:40:01 +01:00
|
|
|
* A txout script template with a specific destination. It is either:
|
2014-09-11 19:15:29 +02:00
|
|
|
* * CNoDestination: no destination set
|
2022-09-15 16:24:07 +02:00
|
|
|
* * CKeyID: TxoutType::PUBKEYHASH destination
|
|
|
|
* * CScriptID: TxoutType::SCRIPTHASH destination
|
Merge #11117: Prepare for non-Base58 addresses (#3294)
* Merge #11117: Prepare for non-Base58 addresses
864cd2787 Move CBitcoinAddress to base58.cpp (Pieter Wuille)
5c8ff0d44 Introduce wrappers around CBitcoinAddress (Pieter Wuille)
Pull request description:
This patch removes the need for the intermediary Base58 type `CBitcoinAddress`, by providing {`Encode`,`Decode`,`IsValid`}`Destination` functions that directly operate on the conversion between `std::string`s and `CTxDestination`.
As a side, it also fixes a number of indentation issues, and removes probably several unnecessary implicit `CTxDestination`<->`CBitcoinAddress` conversions.
This change is far from complete. In follow-ups I'd like to:
* Split off the specific address and key encoding logic from base58.h, and move it to a address.h or so.
* Replace `CTxDestination` with a non-`boost::variant` version (which can be more efficient as `boost::variant` allocates everything on the heap, and remove the need for `boost::get<...>` and `IsValidDestination` calls everywhere).
* Do the same for `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey`.
However, I've tried to keep this patch to be minimally invasive, but still enough to support non-Base58 addresses. Perhaps a smaller patch is possible to hack Bech32 support into `CBitcoinAddress`, but I would consider that a move in the wrong direction.
Tree-SHA512: c2c77ffb57caeadf2429b1c2562ce60e8c7be8aa9f8e51b591f354b6b441162625b2efe14c023a1ae485cf2ed417263afa35c892891dfaa7844e7fbabccab85e
* CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* fix CBitcoinAddress GetKeyID check
Signed-off-by: Pasta <pasta@dashboost.org>
* fix providertx.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* hopefully fix governance-classes.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* partially fix governance-validators.cpp, unable to resolve "address.IsScript()"
Signed-off-by: Pasta <pasta@dashboost.org>
* partially fix governance-classes.cpp, unable to resolve "address.IsScript()"
Signed-off-by: Pasta <pasta@dashboost.org>
* fix governance-classes.h
Signed-off-by: Pasta <pasta@dashboost.org>
* DecodeTransaction -> DecodeDestination, fix governance-validators.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* More fixes for 3294
* Move GetIndexKey into rpc/misc.cpp near getAddressesFromParams
No need to have it in base58.cpp anymore as this is only used in getAddressesFromParams
Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
Co-authored-by: Alexander Block <ablock84@gmail.com>
2020-01-22 11:35:04 +01:00
|
|
|
* A CTxDestination is the internal data type encoded in a bitcoin address
|
2014-09-11 19:15:29 +02:00
|
|
|
*/
|
2019-05-09 18:04:52 +02:00
|
|
|
using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash>;
|
2014-09-11 19:15:29 +02:00
|
|
|
|
Merge #11117: Prepare for non-Base58 addresses (#3294)
* Merge #11117: Prepare for non-Base58 addresses
864cd2787 Move CBitcoinAddress to base58.cpp (Pieter Wuille)
5c8ff0d44 Introduce wrappers around CBitcoinAddress (Pieter Wuille)
Pull request description:
This patch removes the need for the intermediary Base58 type `CBitcoinAddress`, by providing {`Encode`,`Decode`,`IsValid`}`Destination` functions that directly operate on the conversion between `std::string`s and `CTxDestination`.
As a side, it also fixes a number of indentation issues, and removes probably several unnecessary implicit `CTxDestination`<->`CBitcoinAddress` conversions.
This change is far from complete. In follow-ups I'd like to:
* Split off the specific address and key encoding logic from base58.h, and move it to a address.h or so.
* Replace `CTxDestination` with a non-`boost::variant` version (which can be more efficient as `boost::variant` allocates everything on the heap, and remove the need for `boost::get<...>` and `IsValidDestination` calls everywhere).
* Do the same for `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey`.
However, I've tried to keep this patch to be minimally invasive, but still enough to support non-Base58 addresses. Perhaps a smaller patch is possible to hack Bech32 support into `CBitcoinAddress`, but I would consider that a move in the wrong direction.
Tree-SHA512: c2c77ffb57caeadf2429b1c2562ce60e8c7be8aa9f8e51b591f354b6b441162625b2efe14c023a1ae485cf2ed417263afa35c892891dfaa7844e7fbabccab85e
* CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* fix CBitcoinAddress GetKeyID check
Signed-off-by: Pasta <pasta@dashboost.org>
* fix providertx.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* hopefully fix governance-classes.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* partially fix governance-validators.cpp, unable to resolve "address.IsScript()"
Signed-off-by: Pasta <pasta@dashboost.org>
* partially fix governance-classes.cpp, unable to resolve "address.IsScript()"
Signed-off-by: Pasta <pasta@dashboost.org>
* fix governance-classes.h
Signed-off-by: Pasta <pasta@dashboost.org>
* DecodeTransaction -> DecodeDestination, fix governance-validators.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* More fixes for 3294
* Move GetIndexKey into rpc/misc.cpp near getAddressesFromParams
No need to have it in base58.cpp anymore as this is only used in getAddressesFromParams
Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
Co-authored-by: Alexander Block <ablock84@gmail.com>
2020-01-22 11:35:04 +01:00
|
|
|
/** Check whether a CTxDestination is a CNoDestination. */
|
|
|
|
bool IsValidDestination(const CTxDestination& dest);
|
|
|
|
|
2022-09-15 16:24:07 +02:00
|
|
|
/** Get the name of a TxoutType as a C string, or nullptr if unknown. */
|
2020-05-27 13:15:53 +02:00
|
|
|
std::string GetTxnOutputType(TxoutType t);
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2017-08-22 09:31:27 +02:00
|
|
|
/**
|
|
|
|
* Parse a scriptPubKey and identify script type for standard scripts. If
|
|
|
|
* successful, returns script type and parsed pubkeys or hashes, depending on
|
|
|
|
* the type. For example, for a P2SH script, vSolutionsRet will contain the
|
|
|
|
* script hash, for P2PKH it will contain the key hash, etc.
|
|
|
|
*
|
|
|
|
* @param[in] scriptPubKey Script to parse
|
|
|
|
* @param[out] vSolutionsRet Vector of parsed pubkeys and hashes
|
2022-09-15 16:24:07 +02:00
|
|
|
* @return The script type. TxoutType::NONSTANDARD represents a failed solve.
|
2017-08-22 09:31:27 +02:00
|
|
|
*/
|
2022-09-15 16:24:07 +02:00
|
|
|
TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
|
2017-08-22 09:31:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a standard scriptPubKey for the destination address. Assigns result to
|
|
|
|
* the addressRet parameter and returns true if successful. For multisig
|
|
|
|
* scripts, instead use ExtractDestinations. Currently only works for P2PK,
|
|
|
|
* P2PKH, and P2SH scripts.
|
|
|
|
*/
|
2014-08-23 03:35:51 +02:00
|
|
|
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
|
2017-08-22 09:31:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a standard scriptPubKey with one or more destination addresses. For
|
|
|
|
* multisig scripts, this populates the addressRet vector with the pubkey IDs
|
|
|
|
* and nRequiredRet with the n required to spend. For other destinations,
|
|
|
|
* addressRet is populated with a single value and nRequiredRet is set to 1.
|
|
|
|
* Returns true if successful.
|
|
|
|
*/
|
2022-09-15 16:24:07 +02:00
|
|
|
bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2017-08-22 09:31:27 +02:00
|
|
|
/**
|
|
|
|
* Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH
|
|
|
|
* script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
|
|
|
|
* script for CNoDestination.
|
|
|
|
*/
|
2014-09-11 19:15:29 +02:00
|
|
|
CScript GetScriptForDestination(const CTxDestination& dest);
|
2017-08-22 09:31:27 +02:00
|
|
|
|
|
|
|
/** Generate a P2PK script for the given pubkey. */
|
2015-06-10 09:03:08 +02:00
|
|
|
CScript GetScriptForRawPubKey(const CPubKey& pubkey);
|
2017-08-22 09:31:27 +02:00
|
|
|
|
|
|
|
/** Generate a multisig script. */
|
2014-09-11 19:15:29 +02:00
|
|
|
CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_SCRIPT_STANDARD_H
|