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>
|
|
|
|
#include <util.h>
|
|
|
|
#include <utilstrencodings.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
|
|
|
}
|
|
|
|
|
2017-03-09 08:14:27 +01:00
|
|
|
bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet)
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
|
|
|
// Templates
|
2017-03-09 08:14:27 +01:00
|
|
|
static std::multimap<txnouttype, CScript> mTemplates;
|
2014-08-23 03:35:51 +02:00
|
|
|
if (mTemplates.empty())
|
|
|
|
{
|
|
|
|
// Standard tx, sender provides pubkey, receiver adds signature
|
2017-03-09 08:14:27 +01:00
|
|
|
mTemplates.insert(std::make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));
|
2014-08-23 03:35:51 +02:00
|
|
|
|
|
|
|
// Bitcoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
|
2017-03-09 08:14:27 +01:00
|
|
|
mTemplates.insert(std::make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));
|
2014-08-23 03:35:51 +02:00
|
|
|
|
|
|
|
// Sender provides N pubkeys, receivers provides M signatures
|
2017-03-09 08:14:27 +01:00
|
|
|
mTemplates.insert(std::make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
|
2014-08-23 03:35:51 +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())
|
|
|
|
{
|
|
|
|
typeRet = TX_SCRIPTHASH;
|
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);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
typeRet = TX_NULL_DATA;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-23 03:35:51 +02:00
|
|
|
// Scan templates
|
|
|
|
const CScript& script1 = scriptPubKey;
|
2019-07-05 09:08:44 +02:00
|
|
|
for (const std::pair<txnouttype, CScript>& tplate : mTemplates)
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
|
|
|
const CScript& script2 = tplate.second;
|
|
|
|
vSolutionsRet.clear();
|
|
|
|
|
|
|
|
opcodetype opcode1, opcode2;
|
2017-03-09 08:14:27 +01:00
|
|
|
std::vector<unsigned char> vch1, vch2;
|
2014-08-23 03:35:51 +02:00
|
|
|
|
|
|
|
// Compare
|
|
|
|
CScript::const_iterator pc1 = script1.begin();
|
|
|
|
CScript::const_iterator pc2 = script2.begin();
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (pc1 == script1.end() && pc2 == script2.end())
|
|
|
|
{
|
|
|
|
// Found a match
|
|
|
|
typeRet = tplate.first;
|
|
|
|
if (typeRet == TX_MULTISIG)
|
|
|
|
{
|
|
|
|
// Additional checks for TX_MULTISIG:
|
|
|
|
unsigned char m = vSolutionsRet.front()[0];
|
|
|
|
unsigned char n = vSolutionsRet.back()[0];
|
|
|
|
if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!script1.GetOp(pc1, opcode1, vch1))
|
|
|
|
break;
|
|
|
|
if (!script2.GetOp(pc2, opcode2, vch2))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Template matching opcodes:
|
|
|
|
if (opcode2 == OP_PUBKEYS)
|
|
|
|
{
|
|
|
|
while (vch1.size() >= 33 && vch1.size() <= 65)
|
|
|
|
{
|
|
|
|
vSolutionsRet.push_back(vch1);
|
|
|
|
if (!script1.GetOp(pc1, opcode1, vch1))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!script2.GetOp(pc2, opcode2, vch2))
|
|
|
|
break;
|
|
|
|
// Normal situation is to fall through
|
|
|
|
// to other if/else statements
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode2 == OP_PUBKEY)
|
|
|
|
{
|
|
|
|
if (vch1.size() < 33 || vch1.size() > 65)
|
|
|
|
break;
|
|
|
|
vSolutionsRet.push_back(vch1);
|
|
|
|
}
|
|
|
|
else if (opcode2 == OP_PUBKEYHASH)
|
|
|
|
{
|
|
|
|
if (vch1.size() != sizeof(uint160))
|
|
|
|
break;
|
|
|
|
vSolutionsRet.push_back(vch1);
|
|
|
|
}
|
|
|
|
else if (opcode2 == OP_SMALLINTEGER)
|
|
|
|
{ // Single-byte small integer pushed onto vSolutions
|
|
|
|
if (opcode1 == OP_0 ||
|
|
|
|
(opcode1 >= OP_1 && opcode1 <= OP_16))
|
|
|
|
{
|
|
|
|
char n = (char)CScript::DecodeOP_N(opcode1);
|
|
|
|
vSolutionsRet.push_back(valtype(1, n));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (opcode1 != opcode2 || vch1 != vch2)
|
|
|
|
{
|
|
|
|
// Others must match exactly
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vSolutionsRet.clear();
|
|
|
|
typeRet = TX_NONSTANDARD;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
|
|
|
|
{
|
2017-03-09 08:14:27 +01:00
|
|
|
std::vector<valtype> vSolutions;
|
2014-08-23 03:35:51 +02:00
|
|
|
txnouttype whichType;
|
|
|
|
if (!Solver(scriptPubKey, whichType, vSolutions))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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();
|
|
|
|
typeRet = TX_NONSTANDARD;
|
2017-03-09 08:14:27 +01:00
|
|
|
std::vector<valtype> vSolutions;
|
2014-08-23 03:35:51 +02:00
|
|
|
if (!Solver(scriptPubKey, typeRet, vSolutions))
|
|
|
|
return false;
|
|
|
|
if (typeRet == TX_NULL_DATA){
|
|
|
|
// 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
|
|
|
|
{
|
|
|
|
class CScriptVisitor : public boost::static_visitor<bool>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
CScript *script;
|
|
|
|
public:
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit CScriptVisitor(CScript *scriptin) { script = scriptin; }
|
2014-09-11 19:15:29 +02:00
|
|
|
|
|
|
|
bool operator()(const CNoDestination &dest) const {
|
|
|
|
script->clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(const CKeyID &keyID) const {
|
|
|
|
script->clear();
|
2014-09-25 04:54:08 +02:00
|
|
|
*script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
|
2014-09-11 19:15:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(const CScriptID &scriptID) const {
|
|
|
|
script->clear();
|
2014-09-25 04:54:08 +02:00
|
|
|
*script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
|
2014-09-11 19:15:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2017-06-26 13:37:42 +02:00
|
|
|
} // namespace
|
2014-09-11 19:15:29 +02:00
|
|
|
|
|
|
|
CScript GetScriptForDestination(const CTxDestination& dest)
|
|
|
|
{
|
|
|
|
CScript script;
|
|
|
|
|
|
|
|
boost::apply_visitor(CScriptVisitor(&script), dest);
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|