2014-08-23 03:35:51 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-12-31 09:48:25 +01: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.
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <script/standard.h>
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2018-06-17 19:44:50 -07:00
|
|
|
#include <crypto/sha256.h>
|
2021-02-01 18:53:24 -08:00
|
|
|
#include <hash.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <pubkey.h>
|
2021-02-01 18:53:24 -08:00
|
|
|
#include <script/interpreter.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <script/script.h>
|
2021-02-01 18:53:24 -08:00
|
|
|
#include <util/strencodings.h>
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2020-05-18 17:14:10 +09:00
|
|
|
#include <string>
|
|
|
|
|
2017-01-04 14:42:14 +09: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-10 23:55:14 +00:00
|
|
|
unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
|
|
|
|
|
2020-06-26 13:36:41 -07:00
|
|
|
CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
|
2020-01-15 14:03:57 -08:00
|
|
|
CScriptID::CScriptID(const ScriptHash& in) : BaseHash(static_cast<uint160>(in)) {}
|
2014-09-24 22:24:46 -04:00
|
|
|
|
2020-06-26 13:36:41 -07:00
|
|
|
ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in)) {}
|
2020-01-15 14:03:57 -08:00
|
|
|
ScriptHash::ScriptHash(const CScriptID& in) : BaseHash(static_cast<uint160>(in)) {}
|
2019-02-19 17:00:45 -05:00
|
|
|
|
2020-01-15 14:03:57 -08:00
|
|
|
PKHash::PKHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
|
|
|
|
PKHash::PKHash(const CKeyID& pubkey_id) : BaseHash(pubkey_id) {}
|
2020-01-15 21:48:59 +00:00
|
|
|
|
2020-01-15 14:03:57 -08:00
|
|
|
WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
|
|
|
|
WitnessV0KeyHash::WitnessV0KeyHash(const PKHash& pubkey_hash) : BaseHash(static_cast<uint160>(pubkey_hash)) {}
|
2019-02-19 17:00:45 -05:00
|
|
|
|
2020-01-15 13:40:14 -08:00
|
|
|
CKeyID ToKeyID(const PKHash& key_hash)
|
|
|
|
{
|
|
|
|
return CKeyID{static_cast<uint160>(key_hash)};
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:41:25 -08:00
|
|
|
CKeyID ToKeyID(const WitnessV0KeyHash& key_hash)
|
|
|
|
{
|
|
|
|
return CKeyID{static_cast<uint160>(key_hash)};
|
|
|
|
}
|
|
|
|
|
2018-06-17 19:44:50 -07:00
|
|
|
WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in)
|
|
|
|
{
|
|
|
|
CSHA256().Write(in.data(), in.size()).Finalize(begin());
|
|
|
|
}
|
|
|
|
|
2020-05-30 09:16:05 -04:00
|
|
|
std::string GetTxnOutputType(TxoutType t)
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
2020-10-21 13:57:24 +02:00
|
|
|
switch (t) {
|
2020-05-30 09:16:05 -04:00
|
|
|
case TxoutType::NONSTANDARD: return "nonstandard";
|
|
|
|
case TxoutType::PUBKEY: return "pubkey";
|
|
|
|
case TxoutType::PUBKEYHASH: return "pubkeyhash";
|
|
|
|
case TxoutType::SCRIPTHASH: return "scripthash";
|
|
|
|
case TxoutType::MULTISIG: return "multisig";
|
|
|
|
case TxoutType::NULL_DATA: return "nulldata";
|
|
|
|
case TxoutType::WITNESS_V0_KEYHASH: return "witness_v0_keyhash";
|
|
|
|
case TxoutType::WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
|
2020-09-11 14:34:10 -07:00
|
|
|
case TxoutType::WITNESS_V1_TAPROOT: return "witness_v1_taproot";
|
2020-05-30 09:16:05 -04:00
|
|
|
case TxoutType::WITNESS_UNKNOWN: return "witness_unknown";
|
2020-05-30 10:23:21 -04:00
|
|
|
} // no default case, so the compiler can warn about missing cases
|
2020-05-18 17:14:10 +09:00
|
|
|
assert(false);
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
2018-05-08 20:24:06 -07:00
|
|
|
static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
2018-04-12 13:30:04 -07: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-08 20:24:06 -07:00
|
|
|
return CPubKey::ValidSize(pubkey);
|
|
|
|
}
|
2018-04-12 13:30:04 -07: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-08 20:24:06 -07:00
|
|
|
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
|
|
|
|
2018-05-08 20:24:06 -07: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
|
|
|
|
2021-01-05 13:32:41 +01:00
|
|
|
static constexpr bool IsPushdataOp(opcodetype opcode)
|
|
|
|
{
|
|
|
|
return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr bool IsValidMultisigKeyCount(int n_keys)
|
|
|
|
{
|
|
|
|
return n_keys > 0 && n_keys <= MAX_PUBKEYS_PER_MULTISIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetMultisigKeyCount(opcodetype opcode, valtype data, int& count)
|
|
|
|
{
|
|
|
|
if (IsSmallInteger(opcode)) {
|
|
|
|
count = CScript::DecodeOP_N(opcode);
|
|
|
|
return IsValidMultisigKeyCount(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPushdataOp(opcode)) {
|
|
|
|
if (!CheckMinimalPush(data, opcode)) return false;
|
|
|
|
try {
|
|
|
|
count = CScriptNum(data, /* fRequireMinimal = */ true).getint();
|
|
|
|
return IsValidMultisigKeyCount(count);
|
|
|
|
} catch (const scriptnum_error&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool MatchMultisig(const CScript& script, int& required_sigs, std::vector<valtype>& pubkeys)
|
2018-05-08 20:24:06 -07:00
|
|
|
{
|
|
|
|
opcodetype opcode;
|
|
|
|
valtype data;
|
2021-01-05 13:32:41 +01:00
|
|
|
int num_keys;
|
|
|
|
|
2018-05-08 20:24:06 -07:00
|
|
|
CScript::const_iterator it = script.begin();
|
|
|
|
if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) return false;
|
|
|
|
|
2021-01-05 13:32:41 +01:00
|
|
|
if (!script.GetOp(it, opcode, data) || !GetMultisigKeyCount(opcode, data, required_sigs)) return false;
|
2018-05-08 20:24:06 -07:00
|
|
|
while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
|
|
|
|
pubkeys.emplace_back(std::move(data));
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
2021-01-05 13:32:41 +01:00
|
|
|
if (!GetMultisigKeyCount(opcode, data, num_keys)) return false;
|
|
|
|
|
|
|
|
if (pubkeys.size() != static_cast<unsigned long>(num_keys) || num_keys < required_sigs) return false;
|
|
|
|
|
2018-05-08 20:24:06 -07:00
|
|
|
return (it + 1 == script.end());
|
|
|
|
}
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2020-05-30 09:16:05 -04:00
|
|
|
TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet)
|
2018-05-08 20:24:06 -07:00
|
|
|
{
|
2014-10-13 10:14:58 -04: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-01-04 14:42:14 +09:00
|
|
|
std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
|
2014-08-23 03:35:51 +02:00
|
|
|
vSolutionsRet.push_back(hashBytes);
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::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)) {
|
2018-04-10 20:13:32 -07:00
|
|
|
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_KEYHASH_SIZE) {
|
2021-05-23 18:00:18 -07:00
|
|
|
vSolutionsRet.push_back(std::move(witnessprogram));
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::WITNESS_V0_KEYHASH;
|
2016-03-31 14:54:58 +02:00
|
|
|
}
|
2018-04-10 20:13:32 -07:00
|
|
|
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
|
2021-05-23 18:00:18 -07:00
|
|
|
vSolutionsRet.push_back(std::move(witnessprogram));
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::WITNESS_V0_SCRIPTHASH;
|
2016-03-31 14:54:58 +02:00
|
|
|
}
|
2020-09-11 14:34:10 -07:00
|
|
|
if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE) {
|
|
|
|
vSolutionsRet.push_back(std::move(witnessprogram));
|
|
|
|
return TxoutType::WITNESS_V1_TAPROOT;
|
|
|
|
}
|
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));
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::WITNESS_UNKNOWN;
|
2017-08-25 19:55:52 -07:00
|
|
|
}
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::NONSTANDARD;
|
2016-03-31 14:54:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 10:18:05 -04: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)) {
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::NULL_DATA;
|
2014-10-13 10:18:05 -04:00
|
|
|
}
|
|
|
|
|
2018-05-08 20:24:06 -07:00
|
|
|
std::vector<unsigned char> data;
|
|
|
|
if (MatchPayToPubkey(scriptPubKey, data)) {
|
|
|
|
vSolutionsRet.push_back(std::move(data));
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::PUBKEY;
|
2018-05-08 20:24:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MatchPayToPubkeyHash(scriptPubKey, data)) {
|
|
|
|
vSolutionsRet.push_back(std::move(data));
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::PUBKEYHASH;
|
2018-05-08 20:24:06 -07:00
|
|
|
}
|
|
|
|
|
2021-01-05 13:32:41 +01:00
|
|
|
int required;
|
2018-05-08 20:24:06 -07:00
|
|
|
std::vector<std::vector<unsigned char>> keys;
|
|
|
|
if (MatchMultisig(scriptPubKey, required, keys)) {
|
2021-01-05 14:14:55 +01:00
|
|
|
vSolutionsRet.push_back({static_cast<unsigned char>(required)}); // safe as required is in range 1..20
|
2018-05-08 20:24:06 -07:00
|
|
|
vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
|
2021-01-05 14:14:55 +01:00
|
|
|
vSolutionsRet.push_back({static_cast<unsigned char>(keys.size())}); // safe as size is in range 1..20
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::MULTISIG;
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vSolutionsRet.clear();
|
2020-05-30 09:16:05 -04:00
|
|
|
return TxoutType::NONSTANDARD;
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
|
|
|
|
{
|
2017-01-04 14:42:14 +09:00
|
|
|
std::vector<valtype> vSolutions;
|
2020-05-30 09:16:05 -04:00
|
|
|
TxoutType whichType = Solver(scriptPubKey, vSolutions);
|
2014-08-23 03:35:51 +02:00
|
|
|
|
2020-10-21 13:57:24 +02:00
|
|
|
switch (whichType) {
|
|
|
|
case TxoutType::PUBKEY: {
|
2014-09-20 16:13:18 -07:00
|
|
|
CPubKey pubKey(vSolutions[0]);
|
|
|
|
if (!pubKey.IsValid())
|
|
|
|
return false;
|
|
|
|
|
2019-02-19 17:00:45 -05:00
|
|
|
addressRet = PKHash(pubKey);
|
2014-08-23 03:35:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-10-21 13:57:24 +02:00
|
|
|
case TxoutType::PUBKEYHASH: {
|
2019-02-19 17:00:45 -05:00
|
|
|
addressRet = PKHash(uint160(vSolutions[0]));
|
2014-08-23 03:35:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-10-21 13:57:24 +02:00
|
|
|
case TxoutType::SCRIPTHASH: {
|
2019-02-19 17:00:45 -05:00
|
|
|
addressRet = ScriptHash(uint160(vSolutions[0]));
|
2014-08-23 03:35:51 +02:00
|
|
|
return true;
|
2020-10-21 13:57:24 +02:00
|
|
|
}
|
|
|
|
case TxoutType::WITNESS_V0_KEYHASH: {
|
2017-08-25 19:55:52 -07:00
|
|
|
WitnessV0KeyHash hash;
|
|
|
|
std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
|
|
|
|
addressRet = hash;
|
|
|
|
return true;
|
2020-10-21 13:57:24 +02:00
|
|
|
}
|
|
|
|
case TxoutType::WITNESS_V0_SCRIPTHASH: {
|
2017-08-25 19:55:52 -07:00
|
|
|
WitnessV0ScriptHash hash;
|
|
|
|
std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
|
|
|
|
addressRet = hash;
|
|
|
|
return true;
|
2020-10-21 13:57:24 +02:00
|
|
|
}
|
|
|
|
case TxoutType::WITNESS_V1_TAPROOT: {
|
2021-05-23 17:38:40 -07:00
|
|
|
WitnessV1Taproot tap;
|
|
|
|
std::copy(vSolutions[0].begin(), vSolutions[0].end(), tap.begin());
|
|
|
|
addressRet = tap;
|
2021-05-23 18:00:18 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case TxoutType::WITNESS_UNKNOWN: {
|
2017-08-25 19:55:52 -07:00
|
|
|
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
|
|
|
}
|
2020-10-21 13:57:24 +02:00
|
|
|
case TxoutType::MULTISIG:
|
|
|
|
case TxoutType::NULL_DATA:
|
|
|
|
case TxoutType::NONSTANDARD:
|
|
|
|
return false;
|
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
assert(false);
|
2014-08-23 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
2021-02-01 09:52:07 -06:00
|
|
|
// TODO: from v23 ("addresses" and "reqSigs" deprecated) "ExtractDestinations" should be removed
|
2020-05-30 09:16:05 -04:00
|
|
|
bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
|
|
|
addressRet.clear();
|
2017-01-04 14:42:14 +09:00
|
|
|
std::vector<valtype> vSolutions;
|
2018-06-10 22:19:07 -07:00
|
|
|
typeRet = Solver(scriptPubKey, vSolutions);
|
2020-05-30 09:16:05 -04:00
|
|
|
if (typeRet == TxoutType::NONSTANDARD) {
|
2014-08-23 03:35:51 +02:00
|
|
|
return false;
|
2020-05-30 09:16:05 -04:00
|
|
|
} else if (typeRet == TxoutType::NULL_DATA) {
|
2014-08-23 03:35:51 +02:00
|
|
|
// This is data, not addresses
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-30 09:16:05 -04:00
|
|
|
if (typeRet == TxoutType::MULTISIG)
|
2014-08-23 03:35:51 +02:00
|
|
|
{
|
|
|
|
nRequiredRet = vSolutions.front()[0];
|
|
|
|
for (unsigned int i = 1; i < vSolutions.size()-1; i++)
|
|
|
|
{
|
2014-09-20 16:13:18 -07:00
|
|
|
CPubKey pubKey(vSolutions[i]);
|
|
|
|
if (!pubKey.IsValid())
|
|
|
|
continue;
|
|
|
|
|
2019-02-19 17:00:45 -05:00
|
|
|
CTxDestination address = PKHash(pubKey);
|
2014-08-23 03:35:51 +02:00
|
|
|
addressRet.push_back(address);
|
|
|
|
}
|
2014-09-20 16:13:18 -07: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
|
|
|
|
2021-01-04 11:20:02 +01:00
|
|
|
namespace {
|
|
|
|
class CScriptVisitor
|
2014-09-11 19:15:29 +02:00
|
|
|
{
|
|
|
|
public:
|
2020-05-04 10:50:43 +01:00
|
|
|
CScript operator()(const CNoDestination& dest) const
|
|
|
|
{
|
|
|
|
return CScript();
|
2014-09-11 19:15:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 10:50:43 +01:00
|
|
|
CScript operator()(const PKHash& 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 10:50:43 +01:00
|
|
|
CScript operator()(const ScriptHash& scriptID) const
|
|
|
|
{
|
|
|
|
return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
|
2014-09-11 19:15:29 +02:00
|
|
|
}
|
2017-08-25 19:55:52 -07:00
|
|
|
|
2020-05-04 10:50:43 +01:00
|
|
|
CScript operator()(const WitnessV0KeyHash& id) const
|
2017-08-25 19:55:52 -07:00
|
|
|
{
|
2020-05-04 10:50:43 +01:00
|
|
|
return CScript() << OP_0 << ToByteVector(id);
|
2017-08-25 19:55:52 -07:00
|
|
|
}
|
|
|
|
|
2020-05-04 10:50:43 +01:00
|
|
|
CScript operator()(const WitnessV0ScriptHash& id) const
|
2017-08-25 19:55:52 -07:00
|
|
|
{
|
2020-05-04 10:50:43 +01:00
|
|
|
return CScript() << OP_0 << ToByteVector(id);
|
2017-08-25 19:55:52 -07:00
|
|
|
}
|
|
|
|
|
2021-05-23 17:38:40 -07:00
|
|
|
CScript operator()(const WitnessV1Taproot& tap) const
|
|
|
|
{
|
|
|
|
return CScript() << OP_1 << ToByteVector(tap);
|
|
|
|
}
|
|
|
|
|
2020-05-04 10:50:43 +01:00
|
|
|
CScript operator()(const WitnessUnknown& id) const
|
2017-08-25 19:55:52 -07:00
|
|
|
{
|
2020-05-04 10:50:43 +01: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
|
|
|
}
|
2014-09-11 19:15:29 +02:00
|
|
|
};
|
2017-05-31 22:21:25 +02:00
|
|
|
} // namespace
|
2014-09-11 19:15:29 +02:00
|
|
|
|
|
|
|
CScript GetScriptForDestination(const CTxDestination& dest)
|
|
|
|
{
|
2021-01-04 11:20:02 +01:00
|
|
|
return std::visit(CScriptVisitor(), dest);
|
2014-09-11 19:15:29 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 00:03:08 -07: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;
|
|
|
|
|
2021-01-05 14:14:55 +01:00
|
|
|
script << nRequired;
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const CPubKey& key : keys)
|
2014-09-24 22:54:08 -04:00
|
|
|
script << ToByteVector(key);
|
2021-01-05 14:14:55 +01:00
|
|
|
script << keys.size() << OP_CHECKMULTISIG;
|
|
|
|
|
2014-09-11 19:15:29 +02:00
|
|
|
return script;
|
|
|
|
}
|
2016-03-31 14:54:58 +02:00
|
|
|
|
2017-08-22 18:02:33 -07:00
|
|
|
bool IsValidDestination(const CTxDestination& dest) {
|
2021-01-04 11:20:02 +01:00
|
|
|
return dest.index() != 0;
|
2017-08-22 18:02:33 -07:00
|
|
|
}
|
2021-02-01 18:53:24 -08:00
|
|
|
|
|
|
|
/*static*/ TaprootBuilder::NodeInfo TaprootBuilder::Combine(NodeInfo&& a, NodeInfo&& b)
|
|
|
|
{
|
|
|
|
NodeInfo ret;
|
|
|
|
/* Lexicographically sort a and b's hash, and compute parent hash. */
|
|
|
|
if (a.hash < b.hash) {
|
|
|
|
ret.hash = (CHashWriter(HASHER_TAPBRANCH) << a.hash << b.hash).GetSHA256();
|
|
|
|
} else {
|
|
|
|
ret.hash = (CHashWriter(HASHER_TAPBRANCH) << b.hash << a.hash).GetSHA256();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaprootBuilder::Insert(TaprootBuilder::NodeInfo&& node, int depth)
|
|
|
|
{
|
|
|
|
assert(depth >= 0 && (size_t)depth <= TAPROOT_CONTROL_MAX_NODE_COUNT);
|
|
|
|
/* We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing
|
|
|
|
* so would mean the Add() invocations do not correspond to a DFS traversal of a
|
|
|
|
* binary tree. */
|
|
|
|
if ((size_t)depth + 1 < m_branch.size()) {
|
|
|
|
m_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* As long as an entry in the branch exists at the specified depth, combine it and propagate up.
|
|
|
|
* The 'node' variable is overwritten here with the newly combined node. */
|
|
|
|
while (m_valid && m_branch.size() > (size_t)depth && m_branch[depth].has_value()) {
|
|
|
|
node = Combine(std::move(node), std::move(*m_branch[depth]));
|
|
|
|
m_branch.pop_back();
|
|
|
|
if (depth == 0) m_valid = false; /* Can't propagate further up than the root */
|
|
|
|
--depth;
|
|
|
|
}
|
|
|
|
if (m_valid) {
|
|
|
|
/* Make sure the branch is big enough to place the new node. */
|
|
|
|
if (m_branch.size() <= (size_t)depth) m_branch.resize((size_t)depth + 1);
|
|
|
|
assert(!m_branch[depth].has_value());
|
|
|
|
m_branch[depth] = std::move(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ bool TaprootBuilder::ValidDepths(const std::vector<int>& depths)
|
|
|
|
{
|
|
|
|
std::vector<bool> branch;
|
|
|
|
for (int depth : depths) {
|
|
|
|
// This inner loop corresponds to effectively the same logic on branch
|
|
|
|
// as what Insert() performs on the m_branch variable. Instead of
|
|
|
|
// storing a NodeInfo object, just remember whether or not there is one
|
|
|
|
// at that depth.
|
|
|
|
if (depth < 0 || (size_t)depth > TAPROOT_CONTROL_MAX_NODE_COUNT) return false;
|
|
|
|
if ((size_t)depth + 1 < branch.size()) return false;
|
|
|
|
while (branch.size() > (size_t)depth && branch[depth]) {
|
|
|
|
branch.pop_back();
|
|
|
|
if (depth == 0) return false;
|
|
|
|
--depth;
|
|
|
|
}
|
|
|
|
if (branch.size() <= (size_t)depth) branch.resize((size_t)depth + 1);
|
|
|
|
assert(!branch[depth]);
|
|
|
|
branch[depth] = true;
|
|
|
|
}
|
|
|
|
// And this check corresponds to the IsComplete() check on m_branch.
|
|
|
|
return branch.size() == 0 || (branch.size() == 1 && branch[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TaprootBuilder& TaprootBuilder::Add(int depth, const CScript& script, int leaf_version)
|
|
|
|
{
|
|
|
|
assert((leaf_version & ~TAPROOT_LEAF_MASK) == 0);
|
|
|
|
if (!IsValid()) return *this;
|
|
|
|
/* Construct NodeInfo object with leaf hash. */
|
|
|
|
NodeInfo node;
|
|
|
|
node.hash = (CHashWriter{HASHER_TAPLEAF} << uint8_t(leaf_version) << script).GetSHA256();
|
|
|
|
/* Insert into the branch. */
|
|
|
|
Insert(std::move(node), depth);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TaprootBuilder& TaprootBuilder::AddOmitted(int depth, const uint256& hash)
|
|
|
|
{
|
|
|
|
if (!IsValid()) return *this;
|
|
|
|
/* Construct NodeInfo object with the hash directly, and insert it into the branch. */
|
|
|
|
NodeInfo node;
|
|
|
|
node.hash = hash;
|
|
|
|
Insert(std::move(node), depth);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TaprootBuilder& TaprootBuilder::Finalize(const XOnlyPubKey& internal_key)
|
|
|
|
{
|
|
|
|
/* Can only call this function when IsComplete() is true. */
|
|
|
|
assert(IsComplete());
|
|
|
|
m_internal_key = internal_key;
|
|
|
|
auto ret = m_internal_key.CreateTapTweak(m_branch.size() == 0 ? nullptr : &m_branch[0]->hash);
|
|
|
|
assert(ret.has_value());
|
|
|
|
std::tie(m_output_key, std::ignore) = *ret;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WitnessV1Taproot TaprootBuilder::GetOutput() { return WitnessV1Taproot{m_output_key}; }
|