LCOV - code coverage report
Current view: top level - src/script - signingprovider.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 65 361 18.0 %
Date: 2023-11-10 23:46:46 Functions: 18 50 36.0 %
Branches: 31 434 7.1 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 The Bitcoin Core developers
       3                 :            : // Distributed under the MIT software license, see the accompanying
       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :            : 
       6                 :            : #include <script/keyorigin.h>
       7                 :            : #include <script/interpreter.h>
       8                 :            : #include <script/signingprovider.h>
       9                 :            : 
      10                 :            : #include <logging.h>
      11                 :            : 
      12                 :          2 : const SigningProvider& DUMMY_SIGNING_PROVIDER = SigningProvider();
      13                 :            : 
      14                 :            : template<typename M, typename K, typename V>
      15                 :     541763 : bool LookupHelper(const M& map, const K& key, V& value)
      16                 :            : {
      17                 :     541763 :     auto it = map.find(key);
      18 [ +  - ][ +  - ]:     541763 :     if (it != map.end()) {
         [ +  + ][ +  + ]
                 [ +  - ]
      19                 :     442655 :         value = it->second;
      20                 :     442655 :         return true;
      21                 :            :     }
      22                 :      99108 :     return false;
      23                 :     541763 : }
      24                 :            : 
      25                 :          0 : bool HidingSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const
      26                 :            : {
      27                 :          0 :     return m_provider->GetCScript(scriptid, script);
      28                 :            : }
      29                 :            : 
      30                 :          0 : bool HidingSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
      31                 :            : {
      32                 :          0 :     return m_provider->GetPubKey(keyid, pubkey);
      33                 :            : }
      34                 :            : 
      35                 :          0 : bool HidingSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const
      36                 :            : {
      37         [ #  # ]:          0 :     if (m_hide_secret) return false;
      38                 :          0 :     return m_provider->GetKey(keyid, key);
      39                 :          0 : }
      40                 :            : 
      41                 :          0 : bool HidingSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
      42                 :            : {
      43         [ #  # ]:          0 :     if (m_hide_origin) return false;
      44                 :          0 :     return m_provider->GetKeyOrigin(keyid, info);
      45                 :          0 : }
      46                 :            : 
      47                 :          0 : bool HidingSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
      48                 :            : {
      49                 :          0 :     return m_provider->GetTaprootSpendData(output_key, spenddata);
      50                 :            : }
      51                 :          0 : bool HidingSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const
      52                 :            : {
      53                 :          0 :     return m_provider->GetTaprootBuilder(output_key, builder);
      54                 :            : }
      55                 :            : 
      56                 :        288 : bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
      57                 :       1330 : bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
      58                 :     475501 : bool FlatSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
      59                 :            : {
      60                 :     475501 :     std::pair<CPubKey, KeyOriginInfo> out;
      61         [ +  - ]:     475501 :     bool ret = LookupHelper(origins, keyid, out);
      62         [ +  + ]:     475501 :     if (ret) info = std::move(out.second);
      63                 :     475501 :     return ret;
      64                 :     475501 : }
      65                 :      60288 : bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
      66                 :       4356 : bool FlatSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
      67                 :            : {
      68                 :       4356 :     TaprootBuilder builder;
      69 [ +  - ][ +  - ]:       4356 :     if (LookupHelper(tr_trees, output_key, builder)) {
      70         [ +  - ]:       4356 :         spenddata = builder.GetSpendData();
      71                 :       4356 :         return true;
      72                 :            :     }
      73                 :          0 :     return false;
      74                 :       4356 : }
      75                 :          0 : bool FlatSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const
      76                 :            : {
      77                 :          0 :     return LookupHelper(tr_trees, output_key, builder);
      78                 :            : }
      79                 :            : 
      80                 :     449244 : FlatSigningProvider& FlatSigningProvider::Merge(FlatSigningProvider&& b)
      81                 :            : {
      82                 :     449244 :     scripts.merge(b.scripts);
      83                 :     449244 :     pubkeys.merge(b.pubkeys);
      84                 :     449244 :     keys.merge(b.keys);
      85                 :     449244 :     origins.merge(b.origins);
      86                 :     449244 :     tr_trees.merge(b.tr_trees);
      87                 :     449244 :     return *this;
      88                 :            : }
      89                 :            : 
      90                 :          0 : void FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey)
      91                 :            : {
      92                 :          0 :     AssertLockHeld(cs_KeyStore);
      93                 :          0 :     CKeyID key_id = pubkey.GetID();
      94                 :            :     // This adds the redeemscripts necessary to detect P2WPKH and P2SH-P2WPKH
      95                 :            :     // outputs. Technically P2WPKH outputs don't have a redeemscript to be
      96                 :            :     // spent. However, our current IsMine logic requires the corresponding
      97                 :            :     // P2SH-P2WPKH redeemscript to be present in the wallet in order to accept
      98                 :            :     // payment even to P2WPKH outputs.
      99                 :            :     // Also note that having superfluous scripts in the keystore never hurts.
     100                 :            :     // They're only used to guide recursion in signing and IsMine logic - if
     101                 :            :     // a script is present but we can't do anything with it, it has no effect.
     102                 :            :     // "Implicitly" refers to fact that scripts are derived automatically from
     103                 :            :     // existing keys, and are present in memory, even without being explicitly
     104                 :            :     // loaded (e.g. from a file).
     105         [ #  # ]:          0 :     if (pubkey.IsCompressed()) {
     106         [ #  # ]:          0 :         CScript script = GetScriptForDestination(WitnessV0KeyHash(key_id));
     107                 :            :         // This does not use AddCScript, as it may be overridden.
     108         [ #  # ]:          0 :         CScriptID id(script);
     109         [ #  # ]:          0 :         mapScripts[id] = std::move(script);
     110                 :          0 :     }
     111                 :          0 : }
     112                 :            : 
     113                 :          0 : bool FillableSigningProvider::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
     114                 :            : {
     115                 :          0 :     CKey key;
     116 [ #  # ][ #  # ]:          0 :     if (!GetKey(address, key)) {
     117                 :          0 :         return false;
     118                 :            :     }
     119         [ #  # ]:          0 :     vchPubKeyOut = key.GetPubKey();
     120                 :          0 :     return true;
     121                 :          0 : }
     122                 :            : 
     123                 :          0 : bool FillableSigningProvider::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
     124                 :            : {
     125                 :          0 :     LOCK(cs_KeyStore);
     126 [ #  # ][ #  # ]:          0 :     mapKeys[pubkey.GetID()] = key;
                 [ #  # ]
     127         [ #  # ]:          0 :     ImplicitlyLearnRelatedKeyScripts(pubkey);
     128                 :            :     return true;
     129                 :          0 : }
     130                 :            : 
     131                 :          0 : bool FillableSigningProvider::HaveKey(const CKeyID &address) const
     132                 :            : {
     133                 :          0 :     LOCK(cs_KeyStore);
     134         [ #  # ]:          0 :     return mapKeys.count(address) > 0;
     135                 :          0 : }
     136                 :            : 
     137                 :          0 : std::set<CKeyID> FillableSigningProvider::GetKeys() const
     138                 :            : {
     139                 :          0 :     LOCK(cs_KeyStore);
     140                 :          0 :     std::set<CKeyID> set_address;
     141         [ #  # ]:          0 :     for (const auto& mi : mapKeys) {
     142         [ #  # ]:          0 :         set_address.insert(mi.first);
     143                 :            :     }
     144                 :          0 :     return set_address;
     145         [ #  # ]:          0 : }
     146                 :            : 
     147                 :          0 : bool FillableSigningProvider::GetKey(const CKeyID &address, CKey &keyOut) const
     148                 :            : {
     149                 :          0 :     LOCK(cs_KeyStore);
     150         [ #  # ]:          0 :     KeyMap::const_iterator mi = mapKeys.find(address);
     151         [ #  # ]:          0 :     if (mi != mapKeys.end()) {
     152         [ #  # ]:          0 :         keyOut = mi->second;
     153                 :          0 :         return true;
     154                 :            :     }
     155                 :          0 :     return false;
     156                 :          0 : }
     157                 :            : 
     158                 :          0 : bool FillableSigningProvider::AddCScript(const CScript& redeemScript)
     159                 :            : {
     160         [ #  # ]:          0 :     if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
     161                 :          0 :         return error("FillableSigningProvider::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
     162                 :            : 
     163                 :          0 :     LOCK(cs_KeyStore);
     164 [ #  # ][ #  # ]:          0 :     mapScripts[CScriptID(redeemScript)] = redeemScript;
                 [ #  # ]
     165                 :          0 :     return true;
     166                 :          0 : }
     167                 :            : 
     168                 :          0 : bool FillableSigningProvider::HaveCScript(const CScriptID& hash) const
     169                 :            : {
     170                 :          0 :     LOCK(cs_KeyStore);
     171         [ #  # ]:          0 :     return mapScripts.count(hash) > 0;
     172                 :          0 : }
     173                 :            : 
     174                 :          0 : std::set<CScriptID> FillableSigningProvider::GetCScripts() const
     175                 :            : {
     176                 :          0 :     LOCK(cs_KeyStore);
     177                 :          0 :     std::set<CScriptID> set_script;
     178         [ #  # ]:          0 :     for (const auto& mi : mapScripts) {
     179         [ #  # ]:          0 :         set_script.insert(mi.first);
     180                 :            :     }
     181                 :          0 :     return set_script;
     182         [ #  # ]:          0 : }
     183                 :            : 
     184                 :          0 : bool FillableSigningProvider::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
     185                 :            : {
     186                 :          0 :     LOCK(cs_KeyStore);
     187         [ #  # ]:          0 :     ScriptMap::const_iterator mi = mapScripts.find(hash);
     188         [ #  # ]:          0 :     if (mi != mapScripts.end())
     189                 :            :     {
     190         [ #  # ]:          0 :         redeemScriptOut = (*mi).second;
     191                 :          0 :         return true;
     192                 :            :     }
     193                 :          0 :     return false;
     194                 :          0 : }
     195                 :            : 
     196                 :          0 : CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& dest)
     197                 :            : {
     198                 :            :     // Only supports destinations which map to single public keys:
     199                 :            :     // P2PKH, P2WPKH, P2SH-P2WPKH, P2TR
     200         [ #  # ]:          0 :     if (auto id = std::get_if<PKHash>(&dest)) {
     201                 :          0 :         return ToKeyID(*id);
     202                 :            :     }
     203         [ #  # ]:          0 :     if (auto witness_id = std::get_if<WitnessV0KeyHash>(&dest)) {
     204                 :          0 :         return ToKeyID(*witness_id);
     205                 :            :     }
     206         [ #  # ]:          0 :     if (auto script_hash = std::get_if<ScriptHash>(&dest)) {
     207                 :          0 :         CScript script;
     208         [ #  # ]:          0 :         CScriptID script_id = ToScriptID(*script_hash);
     209         [ #  # ]:          0 :         CTxDestination inner_dest;
     210 [ #  # ][ #  # ]:          0 :         if (store.GetCScript(script_id, script) && ExtractDestination(script, inner_dest)) {
         [ #  # ][ #  # ]
     211         [ #  # ]:          0 :             if (auto inner_witness_id = std::get_if<WitnessV0KeyHash>(&inner_dest)) {
     212         [ #  # ]:          0 :                 return ToKeyID(*inner_witness_id);
     213                 :            :             }
     214                 :          0 :         }
     215         [ #  # ]:          0 :     }
     216         [ #  # ]:          0 :     if (auto output_key = std::get_if<WitnessV1Taproot>(&dest)) {
     217                 :          0 :         TaprootSpendData spenddata;
     218         [ #  # ]:          0 :         CPubKey pub;
     219 [ #  # ][ #  # ]:          0 :         if (store.GetTaprootSpendData(*output_key, spenddata)
     220 [ #  # ][ #  # ]:          0 :             && !spenddata.internal_key.IsNull()
     221 [ #  # ][ #  # ]:          0 :             && spenddata.merkle_root.IsNull()
     222 [ #  # ][ #  # ]:          0 :             && store.GetPubKeyByXOnly(spenddata.internal_key, pub)) {
     223         [ #  # ]:          0 :             return pub.GetID();
     224                 :            :         }
     225         [ #  # ]:          0 :     }
     226                 :          0 :     return CKeyID();
     227                 :          0 : }
     228                 :            : 
     229                 :     198394 : void MultiSigningProvider::AddProvider(std::unique_ptr<SigningProvider> provider)
     230                 :            : {
     231                 :     198394 :     m_providers.push_back(std::move(provider));
     232                 :     198394 : }
     233                 :            : 
     234                 :          0 : bool MultiSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const
     235                 :            : {
     236         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     237         [ #  # ]:          0 :         if (provider->GetCScript(scriptid, script)) return true;
     238                 :            :     }
     239                 :          0 :     return false;
     240                 :          0 : }
     241                 :            : 
     242                 :          0 : bool MultiSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
     243                 :            : {
     244         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     245         [ #  # ]:          0 :         if (provider->GetPubKey(keyid, pubkey)) return true;
     246                 :            :     }
     247                 :          0 :     return false;
     248                 :          0 : }
     249                 :            : 
     250                 :            : 
     251                 :     102178 : bool MultiSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
     252                 :            : {
     253         [ +  - ]:     102178 :     for (const auto& provider: m_providers) {
     254         [ +  - ]:     102178 :         if (provider->GetKeyOrigin(keyid, info)) return true;
     255                 :            :     }
     256                 :          0 :     return false;
     257                 :     102178 : }
     258                 :            : 
     259                 :          0 : bool MultiSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const
     260                 :            : {
     261         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     262         [ #  # ]:          0 :         if (provider->GetKey(keyid, key)) return true;
     263                 :            :     }
     264                 :          0 :     return false;
     265                 :          0 : }
     266                 :            : 
     267                 :          0 : bool MultiSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
     268                 :            : {
     269         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     270         [ #  # ]:          0 :         if (provider->GetTaprootSpendData(output_key, spenddata)) return true;
     271                 :            :     }
     272                 :          0 :     return false;
     273                 :          0 : }
     274                 :            : 
     275                 :          0 : bool MultiSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const
     276                 :            : {
     277         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     278         [ #  # ]:          0 :         if (provider->GetTaprootBuilder(output_key, builder)) return true;
     279                 :            :     }
     280                 :          0 :     return false;
     281                 :          0 : }
     282                 :            : 
     283                 :          0 : /*static*/ TaprootBuilder::NodeInfo TaprootBuilder::Combine(NodeInfo&& a, NodeInfo&& b)
     284                 :            : {
     285                 :          0 :     NodeInfo ret;
     286                 :            :     /* Iterate over all tracked leaves in a, add b's hash to their Merkle branch, and move them to ret. */
     287         [ #  # ]:          0 :     for (auto& leaf : a.leaves) {
     288         [ #  # ]:          0 :         leaf.merkle_branch.push_back(b.hash);
     289         [ #  # ]:          0 :         ret.leaves.emplace_back(std::move(leaf));
     290                 :            :     }
     291                 :            :     /* Iterate over all tracked leaves in b, add a's hash to their Merkle branch, and move them to ret. */
     292         [ #  # ]:          0 :     for (auto& leaf : b.leaves) {
     293         [ #  # ]:          0 :         leaf.merkle_branch.push_back(a.hash);
     294         [ #  # ]:          0 :         ret.leaves.emplace_back(std::move(leaf));
     295                 :            :     }
     296 [ #  # ][ #  # ]:          0 :     ret.hash = ComputeTapbranchHash(a.hash, b.hash);
                 [ #  # ]
     297                 :          0 :     return ret;
     298         [ #  # ]:          0 : }
     299                 :            : 
     300                 :          0 : void TaprootSpendData::Merge(TaprootSpendData other)
     301                 :            : {
     302                 :            :     // TODO: figure out how to better deal with conflicting information
     303                 :            :     // being merged.
     304 [ #  # ][ #  # ]:          0 :     if (internal_key.IsNull() && !other.internal_key.IsNull()) {
     305                 :          0 :         internal_key = other.internal_key;
     306                 :          0 :     }
     307 [ #  # ][ #  # ]:          0 :     if (merkle_root.IsNull() && !other.merkle_root.IsNull()) {
     308                 :          0 :         merkle_root = other.merkle_root;
     309                 :          0 :     }
     310         [ #  # ]:          0 :     for (auto& [key, control_blocks] : other.scripts) {
     311                 :          0 :         scripts[key].merge(std::move(control_blocks));
     312                 :            :     }
     313                 :          0 : }
     314                 :            : 
     315                 :          0 : void TaprootBuilder::Insert(TaprootBuilder::NodeInfo&& node, int depth)
     316                 :            : {
     317 [ #  # ][ #  # ]:          0 :     assert(depth >= 0 && (size_t)depth <= TAPROOT_CONTROL_MAX_NODE_COUNT);
     318                 :            :     /* We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing
     319                 :            :      * so would mean the Add() invocations do not correspond to a DFS traversal of a
     320                 :            :      * binary tree. */
     321         [ #  # ]:          0 :     if ((size_t)depth + 1 < m_branch.size()) {
     322                 :          0 :         m_valid = false;
     323                 :          0 :         return;
     324                 :            :     }
     325                 :            :     /* As long as an entry in the branch exists at the specified depth, combine it and propagate up.
     326                 :            :      * The 'node' variable is overwritten here with the newly combined node. */
     327 [ #  # ][ #  # ]:          0 :     while (m_valid && m_branch.size() > (size_t)depth && m_branch[depth].has_value()) {
                 [ #  # ]
     328                 :          0 :         node = Combine(std::move(node), std::move(*m_branch[depth]));
     329                 :          0 :         m_branch.pop_back();
     330         [ #  # ]:          0 :         if (depth == 0) m_valid = false; /* Can't propagate further up than the root */
     331                 :          0 :         --depth;
     332                 :            :     }
     333         [ #  # ]:          0 :     if (m_valid) {
     334                 :            :         /* Make sure the branch is big enough to place the new node. */
     335         [ #  # ]:          0 :         if (m_branch.size() <= (size_t)depth) m_branch.resize((size_t)depth + 1);
     336         [ #  # ]:          0 :         assert(!m_branch[depth].has_value());
     337                 :          0 :         m_branch[depth] = std::move(node);
     338                 :          0 :     }
     339                 :          0 : }
     340                 :            : 
     341                 :          2 : /*static*/ bool TaprootBuilder::ValidDepths(const std::vector<int>& depths)
     342                 :            : {
     343                 :          2 :     std::vector<bool> branch;
     344         [ -  + ]:          2 :     for (int depth : depths) {
     345                 :            :         // This inner loop corresponds to effectively the same logic on branch
     346                 :            :         // as what Insert() performs on the m_branch variable. Instead of
     347                 :            :         // storing a NodeInfo object, just remember whether or not there is one
     348                 :            :         // at that depth.
     349 [ #  # ][ #  # ]:          0 :         if (depth < 0 || (size_t)depth > TAPROOT_CONTROL_MAX_NODE_COUNT) return false;
     350         [ #  # ]:          0 :         if ((size_t)depth + 1 < branch.size()) return false;
     351 [ #  # ][ #  # ]:          0 :         while (branch.size() > (size_t)depth && branch[depth]) {
                 [ #  # ]
     352         [ #  # ]:          0 :             branch.pop_back();
     353         [ #  # ]:          0 :             if (depth == 0) return false;
     354                 :          0 :             --depth;
     355                 :            :         }
     356 [ #  # ][ #  # ]:          0 :         if (branch.size() <= (size_t)depth) branch.resize((size_t)depth + 1);
     357 [ #  # ][ #  # ]:          0 :         assert(!branch[depth]);
     358         [ #  # ]:          0 :         branch[depth] = true;
     359                 :            :     }
     360                 :            :     // And this check corresponds to the IsComplete() check on m_branch.
     361 [ +  - ][ #  # ]:          2 :     return branch.size() == 0 || (branch.size() == 1 && branch[0]);
                 [ #  # ]
     362                 :          2 : }
     363                 :            : 
     364                 :          0 : TaprootBuilder& TaprootBuilder::Add(int depth, Span<const unsigned char> script, int leaf_version, bool track)
     365                 :            : {
     366         [ #  # ]:          0 :     assert((leaf_version & ~TAPROOT_LEAF_MASK) == 0);
     367         [ #  # ]:          0 :     if (!IsValid()) return *this;
     368                 :            :     /* Construct NodeInfo object with leaf hash and (if track is true) also leaf information. */
     369                 :          0 :     NodeInfo node;
     370         [ #  # ]:          0 :     node.hash = ComputeTapleafHash(leaf_version, script);
     371                 :            :     // due to bug in clang-tidy-17:
     372                 :            :     // NOLINTNEXTLINE(modernize-use-emplace)
     373 [ #  # ][ #  # ]:          0 :     if (track) node.leaves.emplace_back(LeafInfo{std::vector<unsigned char>(script.begin(), script.end()), leaf_version, {}});
                 [ #  # ]
     374                 :            :     /* Insert into the branch. */
     375         [ #  # ]:          0 :     Insert(std::move(node), depth);
     376                 :          0 :     return *this;
     377                 :          0 : }
     378                 :            : 
     379                 :          0 : TaprootBuilder& TaprootBuilder::AddOmitted(int depth, const uint256& hash)
     380                 :            : {
     381         [ #  # ]:          0 :     if (!IsValid()) return *this;
     382                 :            :     /* Construct NodeInfo object with the hash directly, and insert it into the branch. */
     383                 :          0 :     NodeInfo node;
     384                 :          0 :     node.hash = hash;
     385         [ #  # ]:          0 :     Insert(std::move(node), depth);
     386                 :          0 :     return *this;
     387                 :          0 : }
     388                 :            : 
     389                 :      10059 : TaprootBuilder& TaprootBuilder::Finalize(const XOnlyPubKey& internal_key)
     390                 :            : {
     391                 :            :     /* Can only call this function when IsComplete() is true. */
     392         [ +  - ]:      10059 :     assert(IsComplete());
     393                 :      10059 :     m_internal_key = internal_key;
     394         [ -  + ]:      10059 :     auto ret = m_internal_key.CreateTapTweak(m_branch.size() == 0 ? nullptr : &m_branch[0]->hash);
     395         [ -  + ]:      10059 :     assert(ret.has_value());
     396                 :      10059 :     std::tie(m_output_key, m_parity) = *ret;
     397                 :      10059 :     return *this;
     398                 :            : }
     399                 :            : 
     400                 :      10059 : WitnessV1Taproot TaprootBuilder::GetOutput() { return WitnessV1Taproot{m_output_key}; }
     401                 :            : 
     402                 :       4356 : TaprootSpendData TaprootBuilder::GetSpendData() const
     403                 :            : {
     404         [ +  - ]:       4356 :     assert(IsComplete());
     405         [ +  - ]:       4356 :     assert(m_output_key.IsFullyValid());
     406                 :       4356 :     TaprootSpendData spd;
     407 [ -  + ][ +  - ]:       4356 :     spd.merkle_root = m_branch.size() == 0 ? uint256() : m_branch[0]->hash;
     408                 :       4356 :     spd.internal_key = m_internal_key;
     409         [ +  - ]:       4356 :     if (m_branch.size()) {
     410                 :            :         // If any script paths exist, they have been combined into the root m_branch[0]
     411                 :            :         // by now. Compute the control block for each of its tracked leaves, and put them in
     412                 :            :         // spd.scripts.
     413         [ #  # ]:          0 :         for (const auto& leaf : m_branch[0]->leaves) {
     414                 :          0 :             std::vector<unsigned char> control_block;
     415         [ #  # ]:          0 :             control_block.resize(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size());
     416                 :          0 :             control_block[0] = leaf.leaf_version | (m_parity ? 1 : 0);
     417 [ #  # ][ #  # ]:          0 :             std::copy(m_internal_key.begin(), m_internal_key.end(), control_block.begin() + 1);
                 [ #  # ]
     418         [ #  # ]:          0 :             if (leaf.merkle_branch.size()) {
     419 [ #  # ][ #  # ]:          0 :                 std::copy(leaf.merkle_branch[0].begin(),
     420         [ #  # ]:          0 :                           leaf.merkle_branch[0].begin() + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size(),
     421                 :          0 :                           control_block.begin() + TAPROOT_CONTROL_BASE_SIZE);
     422                 :          0 :             }
     423 [ #  # ][ #  # ]:          0 :             spd.scripts[{leaf.script, leaf.leaf_version}].insert(std::move(control_block));
                 [ #  # ]
     424                 :          0 :         }
     425                 :          0 :     }
     426                 :       4356 :     return spd;
     427         [ +  - ]:       4356 : }
     428                 :            : 
     429                 :       4356 : std::optional<std::vector<std::tuple<int, std::vector<unsigned char>, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output)
     430                 :            : {
     431                 :            :     // Verify that the output matches the assumed Merkle root and internal key.
     432         [ +  - ]:       4356 :     auto tweak = spenddata.internal_key.CreateTapTweak(spenddata.merkle_root.IsNull() ? nullptr : &spenddata.merkle_root);
     433 [ +  - ][ -  + ]:       4356 :     if (!tweak || tweak->first != output) return std::nullopt;
     434                 :            :     // If the Merkle root is 0, the tree is empty, and we're done.
     435                 :       4356 :     std::vector<std::tuple<int, std::vector<unsigned char>, int>> ret;
     436 [ +  - ][ -  + ]:       4356 :     if (spenddata.merkle_root.IsNull()) return ret;
     437                 :            : 
     438                 :            :     /** Data structure to represent the nodes of the tree we're going to build. */
     439                 :          0 :     struct TreeNode {
     440                 :            :         /** Hash of this node, if known; 0 otherwise. */
     441                 :            :         uint256 hash;
     442                 :            :         /** The left and right subtrees (note that their order is irrelevant). */
     443                 :            :         std::unique_ptr<TreeNode> sub[2];
     444                 :            :         /** If this is known to be a leaf node, a pointer to the (script, leaf_ver) pair.
     445                 :            :          *  nullptr otherwise. */
     446                 :          0 :         const std::pair<std::vector<unsigned char>, int>* leaf = nullptr;
     447                 :            :         /** Whether or not this node has been explored (is known to be a leaf, or known to have children). */
     448                 :          0 :         bool explored = false;
     449                 :            :         /** Whether or not this node is an inner node (unknown until explored = true). */
     450                 :            :         bool inner;
     451                 :            :         /** Whether or not we have produced output for this subtree. */
     452                 :          0 :         bool done = false;
     453                 :            :     };
     454                 :            : 
     455                 :            :     // Build tree from the provided branches.
     456         [ #  # ]:          0 :     TreeNode root;
     457                 :          0 :     root.hash = spenddata.merkle_root;
     458         [ #  # ]:          0 :     for (const auto& [key, control_blocks] : spenddata.scripts) {
     459                 :          0 :         const auto& [script, leaf_ver] = key;
     460         [ #  # ]:          0 :         for (const auto& control : control_blocks) {
     461                 :            :             // Skip script records with nonsensical leaf version.
     462 [ #  # ][ #  # ]:          0 :             if (leaf_ver < 0 || leaf_ver >= 0x100 || leaf_ver & 1) continue;
                 [ #  # ]
     463                 :            :             // Skip script records with invalid control block sizes.
     464 [ #  # ][ #  # ]:          0 :             if (control.size() < TAPROOT_CONTROL_BASE_SIZE || control.size() > TAPROOT_CONTROL_MAX_SIZE ||
                 [ #  # ]
     465                 :          0 :                 ((control.size() - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE) != 0) continue;
     466                 :            :             // Skip script records that don't match the control block.
     467 [ #  # ][ #  # ]:          0 :             if ((control[0] & TAPROOT_LEAF_MASK) != leaf_ver) continue;
     468                 :            :             // Skip script records that don't match the provided Merkle root.
     469 [ #  # ][ #  # ]:          0 :             const uint256 leaf_hash = ComputeTapleafHash(leaf_ver, script);
                 [ #  # ]
     470 [ #  # ][ #  # ]:          0 :             const uint256 merkle_root = ComputeTaprootMerkleRoot(control, leaf_hash);
     471 [ #  # ][ #  # ]:          0 :             if (merkle_root != spenddata.merkle_root) continue;
     472                 :            : 
     473                 :          0 :             TreeNode* node = &root;
     474                 :          0 :             size_t levels = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE;
     475         [ #  # ]:          0 :             for (size_t depth = 0; depth < levels; ++depth) {
     476                 :            :                 // Can't descend into a node which we already know is a leaf.
     477 [ #  # ][ #  # ]:          0 :                 if (node->explored && !node->inner) return std::nullopt;
     478                 :            : 
     479                 :            :                 // Extract partner hash from Merkle branch in control block.
     480         [ #  # ]:          0 :                 uint256 hash;
     481         [ #  # ]:          0 :                 std::copy(control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - 1 - depth) * TAPROOT_CONTROL_NODE_SIZE,
     482                 :          0 :                           control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - depth) * TAPROOT_CONTROL_NODE_SIZE,
     483         [ #  # ]:          0 :                           hash.begin());
     484                 :            : 
     485         [ #  # ]:          0 :                 if (node->sub[0]) {
     486                 :            :                     // Descend into the existing left or right branch.
     487                 :          0 :                     bool desc = false;
     488         [ #  # ]:          0 :                     for (int i = 0; i < 2; ++i) {
     489 [ #  # ][ #  # ]:          0 :                         if (node->sub[i]->hash == hash || (node->sub[i]->hash.IsNull() && node->sub[1-i]->hash != hash)) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     490                 :          0 :                             node->sub[i]->hash = hash;
     491                 :          0 :                             node = &*node->sub[1-i];
     492                 :          0 :                             desc = true;
     493                 :          0 :                             break;
     494                 :            :                         }
     495                 :          0 :                     }
     496         [ #  # ]:          0 :                     if (!desc) return std::nullopt; // This probably requires a hash collision to hit.
     497                 :          0 :                 } else {
     498                 :            :                     // We're in an unexplored node. Create subtrees and descend.
     499                 :          0 :                     node->explored = true;
     500                 :          0 :                     node->inner = true;
     501         [ #  # ]:          0 :                     node->sub[0] = std::make_unique<TreeNode>();
     502         [ #  # ]:          0 :                     node->sub[1] = std::make_unique<TreeNode>();
     503                 :          0 :                     node->sub[1]->hash = hash;
     504                 :          0 :                     node = &*node->sub[0];
     505                 :            :                 }
     506                 :          0 :             }
     507                 :            :             // Cannot turn a known inner node into a leaf.
     508         [ #  # ]:          0 :             if (node->sub[0]) return std::nullopt;
     509                 :          0 :             node->explored = true;
     510                 :          0 :             node->inner = false;
     511                 :          0 :             node->leaf = &key;
     512                 :          0 :             node->hash = leaf_hash;
     513                 :            :         }
     514                 :            :     }
     515                 :            : 
     516                 :            :     // Recursive processing to turn the tree into flattened output. Use an explicit stack here to avoid
     517                 :            :     // overflowing the call stack (the tree may be 128 levels deep).
     518         [ #  # ]:          0 :     std::vector<TreeNode*> stack{&root};
     519         [ #  # ]:          0 :     while (!stack.empty()) {
     520                 :          0 :         TreeNode& node = *stack.back();
     521         [ #  # ]:          0 :         if (!node.explored) {
     522                 :            :             // Unexplored node, which means the tree is incomplete.
     523                 :          0 :             return std::nullopt;
     524         [ #  # ]:          0 :         } else if (!node.inner) {
     525                 :            :             // Leaf node; produce output.
     526         [ #  # ]:          0 :             ret.emplace_back(stack.size() - 1, node.leaf->first, node.leaf->second);
     527                 :          0 :             node.done = true;
     528                 :          0 :             stack.pop_back();
     529 [ #  # ][ #  # ]:          0 :         } else if (node.sub[0]->done && !node.sub[1]->done && !node.sub[1]->explored && !node.sub[1]->hash.IsNull() &&
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     530 [ #  # ][ #  # ]:          0 :                    ComputeTapbranchHash(node.sub[1]->hash, node.sub[1]->hash) == node.hash) {
         [ #  # ][ #  # ]
     531                 :            :             // Whenever there are nodes with two identical subtrees under it, we run into a problem:
     532                 :            :             // the control blocks for the leaves underneath those will be identical as well, and thus
     533                 :            :             // they will all be matched to the same path in the tree. The result is that at the location
     534                 :            :             // where the duplicate occurred, the left child will contain a normal tree that can be explored
     535                 :            :             // and processed, but the right one will remain unexplored.
     536                 :            :             //
     537                 :            :             // This situation can be detected, by encountering an inner node with unexplored right subtree
     538                 :            :             // with known hash, and H_TapBranch(hash, hash) is equal to the parent node (this node)'s hash.
     539                 :            :             //
     540                 :            :             // To deal with this, simply process the left tree a second time (set its done flag to false;
     541                 :            :             // noting that the done flag of its children have already been set to false after processing
     542                 :            :             // those). To avoid ending up in an infinite loop, set the done flag of the right (unexplored)
     543                 :            :             // subtree to true.
     544                 :          0 :             node.sub[0]->done = false;
     545                 :          0 :             node.sub[1]->done = true;
     546 [ #  # ][ #  # ]:          0 :         } else if (node.sub[0]->done && node.sub[1]->done) {
     547                 :            :             // An internal node which we're finished with.
     548                 :          0 :             node.sub[0]->done = false;
     549                 :          0 :             node.sub[1]->done = false;
     550                 :          0 :             node.done = true;
     551                 :          0 :             stack.pop_back();
     552         [ #  # ]:          0 :         } else if (!node.sub[0]->done) {
     553                 :            :             // An internal node whose left branch hasn't been processed yet. Do so first.
     554         [ #  # ]:          0 :             stack.push_back(&*node.sub[0]);
     555         [ #  # ]:          0 :         } else if (!node.sub[1]->done) {
     556                 :            :             // An internal node whose right branch hasn't been processed yet. Do so first.
     557         [ #  # ]:          0 :             stack.push_back(&*node.sub[1]);
     558                 :          0 :         }
     559                 :            :     }
     560                 :            : 
     561                 :          0 :     return ret;
     562                 :       4356 : }
     563                 :            : 
     564                 :          0 : std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> TaprootBuilder::GetTreeTuples() const
     565                 :            : {
     566         [ #  # ]:          0 :     assert(IsComplete());
     567                 :          0 :     std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> tuples;
     568         [ #  # ]:          0 :     if (m_branch.size()) {
     569                 :          0 :         const auto& leaves = m_branch[0]->leaves;
     570         [ #  # ]:          0 :         for (const auto& leaf : leaves) {
     571         [ #  # ]:          0 :             assert(leaf.merkle_branch.size() <= TAPROOT_CONTROL_MAX_NODE_COUNT);
     572                 :          0 :             uint8_t depth = (uint8_t)leaf.merkle_branch.size();
     573                 :          0 :             uint8_t leaf_ver = (uint8_t)leaf.leaf_version;
     574         [ #  # ]:          0 :             tuples.emplace_back(depth, leaf_ver, leaf.script);
     575                 :            :         }
     576                 :          0 :     }
     577                 :          0 :     return tuples;
     578         [ #  # ]:          0 : }

Generated by: LCOV version 1.14