 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
17 typedef std::vector<unsigned char>
valtype;
135 pubkeys.emplace_back(std::move(data));
139 if (pubkeys.size() !=
static_cast<unsigned long>(num_keys) || num_keys < required_sigs)
return false;
141 return (it + 1 == script.
end());
146 vSolutionsRet.clear();
152 std::vector<unsigned char> hashBytes(scriptPubKey.
begin()+2, scriptPubKey.
begin()+22);
153 vSolutionsRet.push_back(hashBytes);
158 std::vector<unsigned char> witnessprogram;
161 vSolutionsRet.push_back(std::move(witnessprogram));
165 vSolutionsRet.push_back(std::move(witnessprogram));
169 vSolutionsRet.push_back(std::move(witnessprogram));
172 if (witnessversion != 0) {
173 vSolutionsRet.push_back(std::vector<unsigned char>{(
unsigned char)witnessversion});
174 vSolutionsRet.push_back(std::move(witnessprogram));
189 std::vector<unsigned char> data;
191 vSolutionsRet.push_back(std::move(data));
196 vSolutionsRet.push_back(std::move(data));
201 std::vector<std::vector<unsigned char>> keys;
203 vSolutionsRet.push_back({
static_cast<unsigned char>(required)});
204 vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
205 vSolutionsRet.push_back({
static_cast<unsigned char>(keys.size())});
209 vSolutionsRet.clear();
215 std::vector<valtype> vSolutions;
224 addressRet =
PKHash(pubKey);
237 std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.
begin());
243 std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.
begin());
249 std::copy(vSolutions[0].begin(), vSolutions[0].end(), tap.
begin());
255 unk.
version = vSolutions[0][0];
256 std::copy(vSolutions[1].begin(), vSolutions[1].end(), unk.
program);
257 unk.
length = vSolutions[1].size();
312 return std::visit(CScriptVisitor(), dest);
325 for (
const CPubKey& key : keys)
333 return dest.index() != 0;
340 for (
auto& leaf : a.leaves) {
341 leaf.merkle_branch.push_back(b.hash);
342 ret.
leaves.emplace_back(std::move(leaf));
345 for (
auto& leaf : b.leaves) {
346 leaf.merkle_branch.push_back(a.hash);
347 ret.
leaves.emplace_back(std::move(leaf));
350 if (a.hash < b.hash) {
368 for (
auto& [key, control_blocks] : other.
scripts) {
373 for (
auto& control_block: control_blocks) {
374 target.insert(std::move(control_block));
385 if ((
size_t)depth + 1 <
m_branch.size()) {
394 if (depth == 0)
m_valid =
false;
407 std::vector<bool> branch;
408 for (
int depth : depths) {
414 if ((
size_t)depth + 1 < branch.size())
return false;
415 while (branch.size() > (
size_t)depth && branch[depth]) {
417 if (depth == 0)
return false;
420 if (branch.size() <= (
size_t)depth) branch.resize((
size_t)depth + 1);
422 branch[depth] =
true;
425 return branch.size() == 0 || (branch.size() == 1 && branch[0]);
435 if (track)
node.leaves.emplace_back(
LeafInfo{script, leaf_version, {}});
474 for (
const auto& leaf :
m_branch[0]->leaves) {
475 std::vector<unsigned char> control_block;
477 control_block[0] = leaf.leaf_version | (
m_parity ? 1 : 0);
479 if (leaf.merkle_branch.size()) {
480 std::copy(leaf.merkle_branch[0].begin(),
484 spd.
scripts[{leaf.script, leaf.leaf_version}].insert(std::move(control_block));
494 if (!tweak || tweak->first != output)
return std::nullopt;
496 std::vector<std::tuple<int, CScript, int>> ret;
504 std::unique_ptr<TreeNode> sub[2];
507 const std::pair<CScript, int>* leaf =
nullptr;
509 bool explored =
false;
519 for (
const auto& [key, control_blocks] : spenddata.
scripts) {
520 const auto& [script, leaf_ver] = key;
521 for (
const auto& control : control_blocks) {
523 if (leaf_ver < 0 || leaf_ver >= 0x100 || leaf_ver & 1)
continue;
532 if (merkle_root != spenddata.
merkle_root)
continue;
534 TreeNode*
node = &root;
536 for (
size_t depth = 0; depth < levels; ++depth) {
538 if (
node->explored && !
node->inner)
return std::nullopt;
549 for (
int i = 0; i < 2; ++i) {
550 if (
node->sub[i]->hash == hash || (
node->sub[i]->hash.IsNull() &&
node->sub[1-i]->hash != hash)) {
551 node->sub[i]->hash = hash;
557 if (!desc)
return std::nullopt;
560 node->explored =
true;
562 node->sub[0] = std::make_unique<TreeNode>();
563 node->sub[1] = std::make_unique<TreeNode>();
564 node->sub[1]->hash = hash;
569 if (
node->sub[0])
return std::nullopt;
570 node->explored =
true;
573 node->hash = leaf_hash;
579 std::vector<TreeNode*> stack{&root};
580 while (!stack.empty()) {
581 TreeNode&
node = *stack.back();
582 if (!
node.explored) {
585 }
else if (!
node.inner) {
587 ret.emplace_back(stack.size() - 1,
node.leaf->first,
node.leaf->second);
590 }
else if (
node.sub[0]->done && !
node.sub[1]->done && !
node.sub[1]->explored && !
node.sub[1]->hash.IsNull() &&
605 node.sub[0]->done =
false;
606 node.sub[1]->done =
true;
607 }
else if (
node.sub[0]->done &&
node.sub[1]->done) {
609 node.sub[0]->done =
false;
610 node.sub[1]->done =
false;
613 }
else if (!
node.sub[0]->done) {
615 stack.push_back(&*
node.sub[0]);
616 }
else if (!
node.sub[1]->done) {
618 stack.push_back(&*
node.sub[1]);
static constexpr size_t WITNESS_V0_KEYHASH_SIZE
static constexpr size_t WITNESS_V0_SCRIPTHASH_SIZE
Signature hash sizes.
CTxDestination subtype to encode any future Witness version.
const CHashWriter HASHER_TAPBRANCH
Hasher with tag "TapBranch" pre-fed to it.
CSHA256 & Write(const unsigned char *data, size_t len)
static bool MatchMultisig(const CScript &script, int &required_sigs, std::vector< valtype > &pubkeys)
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
std::map< std::pair< CScript, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
static constexpr size_t TAPROOT_CONTROL_BASE_SIZE
@ WITNESS_UNKNOWN
Only for Witness versions not already defined above.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static constexpr unsigned int SIZE
secp256k1:
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
void Insert(NodeInfo &&node, int depth)
Insert information about a node at a certain depth, and propagate information up.
Information associated with a node in the Merkle tree.
opcodetype
Script opcodes.
static opcodetype EncodeOP_N(int n)
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
A reference to a CKey: the Hash160 of its serialized public key.
bool IsPayToScriptHash() const
static bool ValidSize(const std::vector< unsigned char > &vch)
static const int MAX_PUBKEYS_PER_MULTISIG
static constexpr size_t TAPROOT_CONTROL_MAX_SIZE
static bool MatchPayToPubkey(const CScript &script, valtype &pubkey)
bool CheckMinimalPush(const valtype &data, opcodetype opcode)
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
const unsigned char * begin() const
const unsigned char * begin() const
uint256 ComputeTaprootMerkleRoot(Span< const unsigned char > control, const uint256 &tapleaf_hash)
Compute the BIP341 taproot script tree Merkle root from control block and leaf hash.
std::vector< unsigned char > ToByteVector(const T &in)
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
static bool GetMultisigKeyCount(opcodetype opcode, valtype data, int &count)
WitnessV1Taproot GetOutput()
Compute scriptPubKey (after Finalize()).
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
bool m_valid
Whether the builder is in a valid state so far.
const unsigned char * end() const
const unsigned char * end() const
std::vector< LeafInfo > leaves
Tracked leaves underneath this node (either from the node itself, or its children).
static constexpr bool IsSmallInteger(opcodetype opcode)
Test for "small positive integer" script opcodes - OP_1 through OP_16.
static constexpr bool IsValidMultisigKeyCount(int n_keys)
static constexpr uint8_t TAPROOT_LEAF_MASK
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
TaprootBuilder & Add(int depth, const CScript &script, int leaf_version, bool track=true)
Add a new script at a certain depth in the tree.
static constexpr bool IsPushdataOp(opcodetype opcode)
static constexpr unsigned int COMPRESSED_SIZE
unsigned nMaxDatacarrierBytes
Maximum size of TxoutType::NULL_DATA scripts that this node considers standard.
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
void Finalize(unsigned char hash[OUTPUT_SIZE])
uint256 hash
Merkle hash of this node.
std::vector< std::optional< NodeInfo > > m_branch
The current state of the builder.
Serialized script, used inside transaction inputs and outputs.
XOnlyPubKey m_output_key
The output key, computed when finalizing.
static bool ValidDepths(const std::vector< int > &depths)
Check if a list of depths is legal (will lead to IsComplete()).
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
XOnlyPubKey internal_key
The BIP341 internal key.
@ NULL_DATA
unspendable OP_RETURN script that carries data
CKeyID ToKeyID(const PKHash &key_hash)
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
An encapsulated public key.
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
const CHashWriter HASHER_TAPLEAF
Hasher with tag "TapLeaf" pre-fed to it.
std::optional< std::vector< std::tuple< int, CScript, int > > > InferTaprootTree(const TaprootSpendData &spenddata, const XOnlyPubKey &output)
Given a TaprootSpendData and the output key, reconstruct its script tree.
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
XOnlyPubKey m_internal_key
The internal key, set when finalizing.
Information about a tracked leaf in the Merkle tree.
A writer stream (for serialization) that computes a 256-bit hash.
bool m_parity
The tweak parity, computed when finalizing.
A hasher class for SHA-256.
bool IsValid() const
Return true if so far all input was valid.
static constexpr size_t WITNESS_V1_TAPROOT_SIZE
std::vector< unsigned char > valtype
static bool MatchPayToPubkeyHash(const CScript &script, valtype &pubkeyhash)
static constexpr size_t TAPROOT_CONTROL_NODE_SIZE
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
Utility class to construct Taproot outputs from internal key and script tree.
uint256 ComputeTapleafHash(uint8_t leaf_version, const CScript &script)
Compute the BIP341 tapleaf hash from leaf version & script.
A reference to a CScript: the Hash160 of its serialization (see script.h)
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
TaprootBuilder & AddOmitted(int depth, const uint256 &hash)
Like Add(), but for a Merkle node with a given hash to the tree.
static NodeInfo Combine(NodeInfo &&a, NodeInfo &&b)
Combine information about a parent Merkle tree node from its child nodes.
std::vector< unsigned char > valtype
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
unsigned char program[40]
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
static constexpr size_t TAPROOT_CONTROL_MAX_NODE_COUNT
static const bool DEFAULT_ACCEPT_DATACARRIER