2014-08-23 03:35:51 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 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.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <script/standard.h>
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <pubkey.h>
|
|
|
|
#include <script/script.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/strencodings.h>
|
2014-08-23 03:35:51 +02:00
|
|
|
|
|
|
|
|
2017-03-09 08:14:27 +01:00
|
|
|
typedef std::vector<unsigned char> valtype;
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2015-11-09 19:16:38 +01:00
|
|
|
bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
|
2014-10-11 01:55:14 +02:00
|
|
|
unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
|
|
|
|
|
2014-12-25 09:12:17 +01:00
|
|
|
CScriptID::CScriptID(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
|
2014-09-25 04:24:46 +02:00
|
|
|
|
2014-08-23 03:35:51 +02:00
|
|
|
const char* GetTxnOutputType(txnouttype t)
|
|
|
|
{
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case TX_NONSTANDARD: return "nonstandard";
|
|
|
|
case TX_PUBKEY: return "pubkey";
|
|
|
|
case TX_PUBKEYHASH: return "pubkeyhash";
|
|
|
|
case TX_SCRIPTHASH: return "scripthash";
|
|
|
|
case TX_MULTISIG: return "multisig";
|
|
|
|
case TX_NULL_DATA: return "nulldata";
|
|
|
|
}
|
2019-08-06 05:08:33 +02:00
|
|
|
return nullptr;
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 16:13:02 +02:00
|
|
|
static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
2021-08-09 01:26:00 +02:00
|
|
|
if (script.size() == CPubKey::SIZE + 2 && script[0] == CPubKey::SIZE && script.back() == OP_CHECKSIG) {
|
|
|
|
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::SIZE + 1);
|
2018-05-30 16:13:02 +02:00
|
|
|
return CPubKey::ValidSize(pubkey);
|
|
|
|
}
|
2021-08-09 01:26:00 +02:00
|
|
|
if (script.size() == CPubKey::COMPRESSED_SIZE + 2 && script[0] == CPubKey::COMPRESSED_SIZE && script.back() == OP_CHECKSIG) {
|
|
|
|
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::COMPRESSED_SIZE + 1);
|
2018-05-30 16:13:02 +02:00
|
|
|
return CPubKey::ValidSize(pubkey);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2018-05-30 16:13:02 +02:00
|
|
|
static bool MatchPayToPubkeyHash(const CScript& script, valtype& pubkeyhash)
|
|
|
|
{
|
|
|
|
if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160 && script[2] == 20 && script[23] == OP_EQUALVERIFY && script[24] == OP_CHECKSIG) {
|
|
|
|
pubkeyhash = valtype(script.begin () + 3, script.begin() + 23);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Test for "small positive integer" script opcodes - OP_1 through OP_16. */
|
|
|
|
static constexpr bool IsSmallInteger(opcodetype opcode)
|
|
|
|
{
|
|
|
|
return opcode >= OP_1 && opcode <= OP_16;
|
|
|
|
}
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2018-05-30 16:13:02 +02:00
|
|
|
static bool MatchMultisig(const CScript& script, unsigned int& required, std::vector<valtype>& pubkeys)
|
|
|
|
{
|
|
|
|
opcodetype opcode;
|
|
|
|
valtype data;
|
|
|
|
CScript::const_iterator it = script.begin();
|
|
|
|
if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) return false;
|
|
|
|
|
|
|
|
if (!script.GetOp(it, opcode, data) || !IsSmallInteger(opcode)) return false;
|
|
|
|
required = CScript::DecodeOP_N(opcode);
|
|
|
|
while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
|
|
|
|
pubkeys.emplace_back(std::move(data));
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
2018-05-30 16:13:02 +02:00
|
|
|
if (!IsSmallInteger(opcode)) return false;
|
|
|
|
unsigned int keys = CScript::DecodeOP_N(opcode);
|
|
|
|
if (pubkeys.size() != keys || keys < required) return false;
|
|
|
|
return (it + 1 == script.end());
|
|
|
|
}
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2021-09-19 09:45:35 +02:00
|
|
|
txnouttype Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet)
|
2018-05-30 16:13:02 +02:00
|
|
|
{
|
2014-10-13 16:14:58 +02:00
|
|
|
vSolutionsRet.clear();
|
|
|
|
|
2014-08-23 03:35:51 +02:00
|
|
|
// Shortcut for pay-to-script-hash, which are more constrained than the other types:
|
|
|
|
// it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
|
|
|
|
if (scriptPubKey.IsPayToScriptHash())
|
|
|
|
{
|
2017-03-09 08:14:27 +01:00
|
|
|
std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
|
2014-08-23 03:35:51 +02:00
|
|
|
vSolutionsRet.push_back(hashBytes);
|
2021-09-19 09:45:35 +02:00
|
|
|
return TX_SCRIPTHASH;
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 16:18:05 +02:00
|
|
|
// Provably prunable, data-carrying output
|
|
|
|
//
|
|
|
|
// So long as script passes the IsUnspendable() test and all but the first
|
|
|
|
// byte passes the IsPushOnly() test we don't care what exactly is in the
|
|
|
|
// script.
|
|
|
|
if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
|
2021-09-19 09:45:35 +02:00
|
|
|
return TX_NULL_DATA;
|
2014-10-13 16:18:05 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 16:13:02 +02:00
|
|
|
std::vector<unsigned char> data;
|
|
|
|
if (MatchPayToPubkey(scriptPubKey, data)) {
|
|
|
|
vSolutionsRet.push_back(std::move(data));
|
2021-09-19 09:45:35 +02:00
|
|
|
return TX_PUBKEY;
|
2018-05-30 16:13:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MatchPayToPubkeyHash(scriptPubKey, data)) {
|
|
|
|
vSolutionsRet.push_back(std::move(data));
|
2021-09-19 09:45:35 +02:00
|
|
|
return TX_PUBKEYHASH;
|
2018-05-30 16:13:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int required;
|
|
|
|
std::vector<std::vector<unsigned char>> keys;
|
|
|
|
if (MatchMultisig(scriptPubKey, required, keys)) {
|
|
|
|
vSolutionsRet.push_back({static_cast<unsigned char>(required)}); // safe as required is in range 1..16
|
|
|
|
vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
|
|
|
|
vSolutionsRet.push_back({static_cast<unsigned char>(keys.size())}); // safe as size is in range 1..16
|
2021-09-19 09:45:35 +02:00
|
|
|
return TX_MULTISIG;
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vSolutionsRet.clear();
|
2021-09-19 09:45:35 +02:00
|
|
|
return TX_NONSTANDARD;
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
|
|
|
|
{
|
2017-03-09 08:14:27 +01:00
|
|
|
std::vector<valtype> vSolutions;
|
2021-09-19 09:45:35 +02:00
|
|
|
txnouttype whichType = Solver(scriptPubKey, vSolutions);
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2021-09-19 09:45:35 +02:00
|
|
|
if (whichType == TX_PUBKEY) {
|
2014-09-21 01:13:18 +02:00
|
|
|
CPubKey pubKey(vSolutions[0]);
|
|
|
|
if (!pubKey.IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
addressRet = pubKey.GetID();
|
2014-08-23 03:35:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (whichType == TX_PUBKEYHASH)
|
|
|
|
{
|
|
|
|
addressRet = CKeyID(uint160(vSolutions[0]));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (whichType == TX_SCRIPTHASH)
|
|
|
|
{
|
|
|
|
addressRet = CScriptID(uint160(vSolutions[0]));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Multisig txns have more than one address...
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:14:27 +01:00
|
|
|
bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
|
|
|
addressRet.clear();
|
2017-03-09 08:14:27 +01:00
|
|
|
std::vector<valtype> vSolutions;
|
2021-09-19 09:45:35 +02:00
|
|
|
typeRet = Solver(scriptPubKey, vSolutions);
|
|
|
|
if (typeRet == TX_NONSTANDARD) {
|
2014-08-23 03:35:51 +02:00
|
|
|
return false;
|
2021-09-19 09:45:35 +02:00
|
|
|
} else if (typeRet == TX_NULL_DATA) {
|
2014-08-23 03:35:51 +02:00
|
|
|
// This is data, not addresses
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeRet == TX_MULTISIG)
|
|
|
|
{
|
|
|
|
nRequiredRet = vSolutions.front()[0];
|
|
|
|
for (unsigned int i = 1; i < vSolutions.size()-1; i++)
|
|
|
|
{
|
2014-09-21 01:13:18 +02:00
|
|
|
CPubKey pubKey(vSolutions[i]);
|
|
|
|
if (!pubKey.IsValid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CTxDestination address = pubKey.GetID();
|
2014-08-23 03:35:51 +02:00
|
|
|
addressRet.push_back(address);
|
|
|
|
}
|
2014-09-21 01:13:18 +02:00
|
|
|
|
|
|
|
if (addressRet.empty())
|
|
|
|
return false;
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nRequiredRet = 1;
|
|
|
|
CTxDestination address;
|
|
|
|
if (!ExtractDestination(scriptPubKey, address))
|
|
|
|
return false;
|
|
|
|
addressRet.push_back(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-11 19:15:29 +02:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-05-04 11:50:43 +02:00
|
|
|
class CScriptVisitor : public boost::static_visitor<CScript>
|
2014-09-11 19:15:29 +02:00
|
|
|
{
|
|
|
|
public:
|
2020-05-04 11:50:43 +02:00
|
|
|
CScript operator()(const CNoDestination& dest) const
|
|
|
|
{
|
|
|
|
return CScript();
|
2014-09-11 19:15:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 11:50:43 +02:00
|
|
|
CScript operator()(const CKeyID& keyID) const
|
|
|
|
{
|
|
|
|
return CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
|
2014-09-11 19:15:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 11:50:43 +02:00
|
|
|
CScript operator()(const CScriptID& scriptID) const
|
|
|
|
{
|
|
|
|
return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
|
2014-09-11 19:15:29 +02:00
|
|
|
}
|
|
|
|
};
|
2017-06-26 13:37:42 +02:00
|
|
|
} // namespace
|
2014-09-11 19:15:29 +02:00
|
|
|
|
|
|
|
CScript GetScriptForDestination(const CTxDestination& dest)
|
|
|
|
{
|
2021-07-15 17:48:03 +02:00
|
|
|
return boost::apply_visitor(CScriptVisitor(), dest);
|
2014-09-11 19:15:29 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 09:03:08 +02:00
|
|
|
CScript GetScriptForRawPubKey(const CPubKey& pubKey)
|
|
|
|
{
|
|
|
|
return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
|
|
|
|
}
|
|
|
|
|
2014-09-11 19:15:29 +02:00
|
|
|
CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
|
|
|
|
{
|
|
|
|
CScript script;
|
|
|
|
|
|
|
|
script << CScript::EncodeOP_N(nRequired);
|
2019-07-05 09:06:28 +02:00
|
|
|
for (const CPubKey& key : keys)
|
2014-09-25 04:54:08 +02:00
|
|
|
script << ToByteVector(key);
|
2014-09-11 19:15:29 +02:00
|
|
|
script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
|
|
|
|
return script;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
|
|
bool IsValidDestination(const CTxDestination& dest) {
|
|
|
|
return dest.which() != 0;
|
|
|
|
}
|