bitcoin/src/script/standard.cpp

324 lines
10 KiB
C++
Raw Normal View History

2014-08-23 03:35:51 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2019 The Bitcoin Core developers
// 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.
#include <script/standard.h>
2014-08-23 03:35:51 +02:00
#include <crypto/sha256.h>
#include <pubkey.h>
#include <script/script.h>
2014-08-23 03:35:51 +02:00
#include <string>
typedef std::vector<unsigned char> valtype;
2014-08-23 03:35:51 +02:00
bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
CScriptID::CScriptID(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
ScriptHash::ScriptHash(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
PKHash::PKHash(const CPubKey& pubkey) : uint160(pubkey.GetID()) {}
WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : uint160(pubkey.GetID()) {}
CKeyID ToKeyID(const PKHash& key_hash)
{
return CKeyID{static_cast<uint160>(key_hash)};
}
WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in)
{
CSHA256().Write(in.data(), in.size()).Finalize(begin());
}
std::string GetTxnOutputType(txnouttype t)
2014-08-23 03:35:51 +02:00
{
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";
2016-03-31 14:54:58 +02:00
case TX_WITNESS_V0_KEYHASH: return "witness_v0_keyhash";
case TX_WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
2017-08-25 19:55:52 -07:00
case TX_WITNESS_UNKNOWN: return "witness_unknown";
2014-08-23 03:35:51 +02:00
}
assert(false);
2014-08-23 03:35:51 +02:00
}
static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)
2014-08-23 03:35:51 +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);
return CPubKey::ValidSize(pubkey);
}
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);
return CPubKey::ValidSize(pubkey);
}
return false;
}
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;
}
2014-08-23 03:35:51 +02:00
/** 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
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
}
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
txnouttype Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet)
{
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())
{
std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
2014-08-23 03:35:51 +02:00
vSolutionsRet.push_back(hashBytes);
return TX_SCRIPTHASH;
2014-08-23 03:35:51 +02:00
}
2016-03-31 14:54:58 +02:00
int witnessversion;
std::vector<unsigned char> witnessprogram;
if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_KEYHASH_SIZE) {
2016-03-31 14:54:58 +02:00
vSolutionsRet.push_back(witnessprogram);
return TX_WITNESS_V0_KEYHASH;
2016-03-31 14:54:58 +02:00
}
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
2016-03-31 14:54:58 +02:00
vSolutionsRet.push_back(witnessprogram);
return TX_WITNESS_V0_SCRIPTHASH;
2016-03-31 14:54:58 +02:00
}
2017-08-25 19:55:52 -07:00
if (witnessversion != 0) {
vSolutionsRet.push_back(std::vector<unsigned char>{(unsigned char)witnessversion});
vSolutionsRet.push_back(std::move(witnessprogram));
return TX_WITNESS_UNKNOWN;
2017-08-25 19:55:52 -07:00
}
return TX_NONSTANDARD;
2016-03-31 14:54:58 +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)) {
return TX_NULL_DATA;
}
std::vector<unsigned char> data;
if (MatchPayToPubkey(scriptPubKey, data)) {
vSolutionsRet.push_back(std::move(data));
return TX_PUBKEY;
}
if (MatchPayToPubkeyHash(scriptPubKey, data)) {
vSolutionsRet.push_back(std::move(data));
return TX_PUBKEYHASH;
}
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
return TX_MULTISIG;
2014-08-23 03:35:51 +02:00
}
vSolutionsRet.clear();
return TX_NONSTANDARD;
2014-08-23 03:35:51 +02:00
}
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
{
std::vector<valtype> vSolutions;
txnouttype whichType = Solver(scriptPubKey, vSolutions);
2014-08-23 03:35:51 +02:00
if (whichType == TX_PUBKEY) {
CPubKey pubKey(vSolutions[0]);
if (!pubKey.IsValid())
return false;
addressRet = PKHash(pubKey);
2014-08-23 03:35:51 +02:00
return true;
}
else if (whichType == TX_PUBKEYHASH)
{
addressRet = PKHash(uint160(vSolutions[0]));
2014-08-23 03:35:51 +02:00
return true;
}
else if (whichType == TX_SCRIPTHASH)
{
addressRet = ScriptHash(uint160(vSolutions[0]));
2014-08-23 03:35:51 +02:00
return true;
2017-08-25 19:55:52 -07:00
} else if (whichType == TX_WITNESS_V0_KEYHASH) {
WitnessV0KeyHash hash;
std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
addressRet = hash;
return true;
} else if (whichType == TX_WITNESS_V0_SCRIPTHASH) {
WitnessV0ScriptHash hash;
std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
addressRet = hash;
return true;
} else if (whichType == TX_WITNESS_UNKNOWN) {
WitnessUnknown unk;
unk.version = vSolutions[0][0];
std::copy(vSolutions[1].begin(), vSolutions[1].end(), unk.program);
unk.length = vSolutions[1].size();
addressRet = unk;
return true;
2014-08-23 03:35:51 +02:00
}
// Multisig txns have more than one address...
return false;
}
bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
2014-08-23 03:35:51 +02:00
{
addressRet.clear();
std::vector<valtype> vSolutions;
typeRet = Solver(scriptPubKey, vSolutions);
if (typeRet == TX_NONSTANDARD) {
2014-08-23 03:35:51 +02:00
return false;
} 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++)
{
CPubKey pubKey(vSolutions[i]);
if (!pubKey.IsValid())
continue;
CTxDestination address = PKHash(pubKey);
2014-08-23 03:35:51 +02:00
addressRet.push_back(address);
}
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;
}
namespace
{
class CScriptVisitor : public boost::static_visitor<CScript>
{
public:
CScript operator()(const CNoDestination& dest) const
{
return CScript();
}
CScript operator()(const PKHash& keyID) const
{
return CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
}
CScript operator()(const ScriptHash& scriptID) const
{
return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
}
2017-08-25 19:55:52 -07:00
CScript operator()(const WitnessV0KeyHash& id) const
2017-08-25 19:55:52 -07:00
{
return CScript() << OP_0 << ToByteVector(id);
2017-08-25 19:55:52 -07:00
}
CScript operator()(const WitnessV0ScriptHash& id) const
2017-08-25 19:55:52 -07:00
{
return CScript() << OP_0 << ToByteVector(id);
2017-08-25 19:55:52 -07:00
}
CScript operator()(const WitnessUnknown& id) const
2017-08-25 19:55:52 -07:00
{
return CScript() << CScript::EncodeOP_N(id.version) << std::vector<unsigned char>(id.program, id.program + id.length);
2017-08-25 19:55:52 -07:00
}
};
const CScriptVisitor g_script_visitor;
} // namespace
CScript GetScriptForDestination(const CTxDestination& dest)
{
return boost::apply_visitor(::g_script_visitor, dest);
}
CScript GetScriptForRawPubKey(const CPubKey& pubKey)
{
return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
}
CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
{
CScript script;
script << CScript::EncodeOP_N(nRequired);
for (const CPubKey& key : keys)
script << ToByteVector(key);
script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
return script;
}
2016-03-31 14:54:58 +02:00
CScript GetScriptForWitness(const CScript& redeemscript)
{
std::vector<std::vector<unsigned char> > vSolutions;
txnouttype typ = Solver(redeemscript, vSolutions);
if (typ == TX_PUBKEY) {
return GetScriptForDestination(WitnessV0KeyHash(Hash160(vSolutions[0].begin(), vSolutions[0].end())));
} else if (typ == TX_PUBKEYHASH) {
2020-01-14 22:37:16 -08:00
return GetScriptForDestination(WitnessV0KeyHash(uint160{vSolutions[0]}));
2016-03-31 14:54:58 +02:00
}
return GetScriptForDestination(WitnessV0ScriptHash(redeemscript));
2016-03-31 14:54:58 +02:00
}
bool IsValidDestination(const CTxDestination& dest) {
return dest.which() != 0;
}