LCOV - code coverage report
Current view: top level - src - addresstype.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.4 % 77 75
Test Date: 2026-10-25 07:18:46 Functions: 100.0 % 19 19
Branches: 59.4 % 64 38

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or https://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <addresstype.h>
       6                 :             : 
       7                 :             : #include <crypto/sha256.h>
       8                 :             : #include <hash.h>
       9                 :             : #include <pubkey.h>
      10                 :             : #include <script/script.h>
      11                 :             : #include <script/solver.h>
      12                 :             : #include <uint256.h>
      13                 :             : #include <util/hash_type.h>
      14                 :             : 
      15                 :             : #include <cassert>
      16                 :             : #include <vector>
      17                 :             : 
      18                 :             : typedef std::vector<unsigned char> valtype;
      19                 :             : 
      20                 :      149072 : ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in)) {}
      21                 :             : 
      22                 :         956 : PKHash::PKHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
      23                 :      177656 : PKHash::PKHash(const CKeyID& pubkey_id) : BaseHash(pubkey_id) {}
      24                 :             : 
      25                 :         804 : WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
      26                 :           1 : WitnessV0KeyHash::WitnessV0KeyHash(const PKHash& pubkey_hash) : BaseHash{pubkey_hash} {}
      27                 :             : 
      28                 :         235 : CKeyID ToKeyID(const PKHash& key_hash)
      29                 :             : {
      30                 :         235 :     return CKeyID{uint160{key_hash}};
      31                 :             : }
      32                 :             : 
      33                 :         810 : CKeyID ToKeyID(const WitnessV0KeyHash& key_hash)
      34                 :             : {
      35                 :         810 :     return CKeyID{uint160{key_hash}};
      36                 :             : }
      37                 :             : 
      38                 :         225 : CScriptID ToScriptID(const ScriptHash& script_hash)
      39                 :             : {
      40                 :         225 :     return CScriptID{uint160{script_hash}};
      41                 :             : }
      42                 :             : 
      43                 :       18966 : WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in)
      44                 :             : {
      45   [ +  +  +  + ]:       55677 :     CSHA256().Write(in.data(), in.size()).Finalize(begin());
      46                 :       18966 : }
      47                 :             : 
      48                 :      102743 : bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
      49                 :             : {
      50                 :      102743 :     std::vector<valtype> vSolutions;
      51         [ +  - ]:      102743 :     TxoutType whichType = Solver(scriptPubKey, vSolutions);
      52                 :             : 
      53   [ +  +  +  +  :      102743 :     switch (whichType) {
          +  +  +  +  +  
                      - ]
      54                 :         627 :     case TxoutType::PUBKEY: {
      55         [ -  + ]:         627 :         CPubKey pubKey(vSolutions[0]);
      56         [ -  + ]:         627 :         if (!pubKey.IsValid()) {
      57                 :           0 :             addressRet = CNoDestination(scriptPubKey);
      58                 :             :         } else {
      59                 :         627 :             addressRet = PubKeyDestination(pubKey);
      60                 :             :         }
      61                 :             :         return false;
      62                 :             :     }
      63                 :       23405 :     case TxoutType::PUBKEYHASH: {
      64         [ -  + ]:       23405 :         addressRet = PKHash(uint160(vSolutions[0]));
      65                 :       23405 :         return true;
      66                 :             :     }
      67                 :        7283 :     case TxoutType::SCRIPTHASH: {
      68         [ -  + ]:        7283 :         addressRet = ScriptHash(uint160(vSolutions[0]));
      69                 :        7283 :         return true;
      70                 :             :     }
      71                 :       44463 :     case TxoutType::WITNESS_V0_KEYHASH: {
      72                 :       44463 :         WitnessV0KeyHash hash;
      73                 :       44463 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
      74                 :       44463 :         addressRet = hash;
      75                 :       44463 :         return true;
      76                 :             :     }
      77                 :         755 :     case TxoutType::WITNESS_V0_SCRIPTHASH: {
      78                 :         755 :         WitnessV0ScriptHash hash;
      79                 :         755 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
      80                 :         755 :         addressRet = hash;
      81                 :         755 :         return true;
      82                 :             :     }
      83                 :       20634 :     case TxoutType::WITNESS_V1_TAPROOT: {
      84                 :       20634 :         WitnessV1Taproot tap;
      85                 :       20634 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), tap.begin());
      86                 :       20634 :         addressRet = tap;
      87                 :       20634 :         return true;
      88                 :             :     }
      89                 :          49 :     case TxoutType::ANCHOR: {
      90         [ +  - ]:          49 :         addressRet = PayToAnchor();
      91                 :          49 :         return true;
      92                 :             :     }
      93                 :          54 :     case TxoutType::WITNESS_UNKNOWN: {
      94         [ +  - ]:          54 :         addressRet = WitnessUnknown{vSolutions[0][0], vSolutions[1]};
      95                 :          54 :         return true;
      96                 :             :     }
      97                 :        5473 :     case TxoutType::MULTISIG:
      98                 :        5473 :     case TxoutType::NULL_DATA:
      99                 :        5473 :     case TxoutType::NONSTANDARD:
     100                 :        5473 :         addressRet = CNoDestination(scriptPubKey);
     101                 :        5473 :         return false;
     102                 :             :     } // no default case, so the compiler can warn about missing cases
     103                 :           0 :     assert(false);
     104                 :      102743 : }
     105                 :             : 
     106                 :             : namespace {
     107                 :             : class CScriptVisitor
     108                 :             : {
     109                 :             : public:
     110                 :          78 :     CScript operator()(const CNoDestination& dest) const
     111                 :             :     {
     112                 :          78 :         return dest.GetScript();
     113                 :             :     }
     114                 :             : 
     115                 :          28 :     CScript operator()(const PubKeyDestination& dest) const
     116                 :             :     {
     117   [ +  -  +  - ]:          56 :         return CScript() << ToByteVector(dest.GetPubKey()) << OP_CHECKSIG;
     118                 :             :     }
     119                 :             : 
     120                 :      214345 :     CScript operator()(const PKHash& keyID) const
     121                 :             :     {
     122   [ +  -  +  -  :      428690 :         return CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
          +  -  +  -  +  
                      - ]
     123                 :             :     }
     124                 :             : 
     125                 :      155862 :     CScript operator()(const ScriptHash& scriptID) const
     126                 :             :     {
     127   [ +  -  +  -  :      311724 :         return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
                   +  - ]
     128                 :             :     }
     129                 :             : 
     130                 :      443827 :     CScript operator()(const WitnessV0KeyHash& id) const
     131                 :             :     {
     132   [ +  -  +  - ]:      887654 :         return CScript() << OP_0 << ToByteVector(id);
     133                 :             :     }
     134                 :             : 
     135                 :       19349 :     CScript operator()(const WitnessV0ScriptHash& id) const
     136                 :             :     {
     137   [ +  -  +  - ]:       38698 :         return CScript() << OP_0 << ToByteVector(id);
     138                 :             :     }
     139                 :             : 
     140                 :      148188 :     CScript operator()(const WitnessV1Taproot& tap) const
     141                 :             :     {
     142   [ +  -  +  - ]:      296376 :         return CScript() << OP_1 << ToByteVector(tap);
     143                 :             :     }
     144                 :             : 
     145                 :         123 :     CScript operator()(const WitnessUnknown& id) const
     146                 :             :     {
     147   [ +  -  -  + ]:         123 :         return CScript() << CScript::EncodeOP_N(id.GetWitnessVersion()) << id.GetWitnessProgram();
     148                 :             :     }
     149                 :             : };
     150                 :             : 
     151                 :             : class ValidDestinationVisitor
     152                 :             : {
     153                 :             : public:
     154                 :             :     bool operator()(const CNoDestination& dest) const { return false; }
     155                 :             :     bool operator()(const PubKeyDestination& dest) const { return false; }
     156                 :             :     bool operator()(const PKHash& dest) const { return true; }
     157                 :             :     bool operator()(const ScriptHash& dest) const { return true; }
     158                 :             :     bool operator()(const WitnessV0KeyHash& dest) const { return true; }
     159                 :             :     bool operator()(const WitnessV0ScriptHash& dest) const { return true; }
     160                 :             :     bool operator()(const WitnessV1Taproot& dest) const { return true; }
     161                 :             :     bool operator()(const WitnessUnknown& dest) const { return true; }
     162                 :             : };
     163                 :             : } // namespace
     164                 :             : 
     165                 :      981800 : CScript GetScriptForDestination(const CTxDestination& dest)
     166                 :             : {
     167                 :      981800 :     return std::visit(CScriptVisitor(), dest);
     168                 :             : }
     169                 :             : 
     170                 :       25462 : bool IsValidDestination(const CTxDestination& dest) {
     171                 :       25462 :     return std::visit(ValidDestinationVisitor(), dest);
     172                 :             : }
        

Generated by: LCOV version 2.0-1