LCOV - code coverage report
Current view: top level - src - addresstype.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 96.1 % 77 74
Test Date: 2026-10-25 06:31:49 Functions: 94.7 % 19 18
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                 :      350716 : ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in)) {}
      21                 :             : 
      22                 :       22207 : PKHash::PKHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
      23                 :      155521 : PKHash::PKHash(const CKeyID& pubkey_id) : BaseHash(pubkey_id) {}
      24                 :             : 
      25                 :       18064 : WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
      26                 :           0 : WitnessV0KeyHash::WitnessV0KeyHash(const PKHash& pubkey_hash) : BaseHash{pubkey_hash} {}
      27                 :             : 
      28                 :       10053 : CKeyID ToKeyID(const PKHash& key_hash)
      29                 :             : {
      30                 :       10053 :     return CKeyID{uint160{key_hash}};
      31                 :             : }
      32                 :             : 
      33                 :        2464 : CKeyID ToKeyID(const WitnessV0KeyHash& key_hash)
      34                 :             : {
      35                 :        2464 :     return CKeyID{uint160{key_hash}};
      36                 :             : }
      37                 :             : 
      38                 :        2923 : CScriptID ToScriptID(const ScriptHash& script_hash)
      39                 :             : {
      40                 :        2923 :     return CScriptID{uint160{script_hash}};
      41                 :             : }
      42                 :             : 
      43                 :       65431 : WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in)
      44                 :             : {
      45   [ +  +  +  + ]:      169960 :     CSHA256().Write(in.data(), in.size()).Finalize(begin());
      46                 :       65431 : }
      47                 :             : 
      48                 :     3313263 : bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
      49                 :             : {
      50                 :     3313263 :     std::vector<valtype> vSolutions;
      51         [ +  - ]:     3313263 :     TxoutType whichType = Solver(scriptPubKey, vSolutions);
      52                 :             : 
      53   [ +  +  +  +  :     3313263 :     switch (whichType) {
          +  +  +  +  +  
                      - ]
      54                 :       33381 :     case TxoutType::PUBKEY: {
      55         [ -  + ]:       33381 :         CPubKey pubKey(vSolutions[0]);
      56         [ -  + ]:       33381 :         if (!pubKey.IsValid()) {
      57                 :           0 :             addressRet = CNoDestination(scriptPubKey);
      58                 :             :         } else {
      59                 :       33381 :             addressRet = PubKeyDestination(pubKey);
      60                 :             :         }
      61                 :             :         return false;
      62                 :             :     }
      63                 :      114909 :     case TxoutType::PUBKEYHASH: {
      64         [ -  + ]:      114909 :         addressRet = PKHash(uint160(vSolutions[0]));
      65                 :      114909 :         return true;
      66                 :             :     }
      67                 :      343369 :     case TxoutType::SCRIPTHASH: {
      68         [ -  + ]:      343369 :         addressRet = ScriptHash(uint160(vSolutions[0]));
      69                 :      343369 :         return true;
      70                 :             :     }
      71                 :      150707 :     case TxoutType::WITNESS_V0_KEYHASH: {
      72                 :      150707 :         WitnessV0KeyHash hash;
      73                 :      150707 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
      74                 :      150707 :         addressRet = hash;
      75                 :      150707 :         return true;
      76                 :             :     }
      77                 :      132394 :     case TxoutType::WITNESS_V0_SCRIPTHASH: {
      78                 :      132394 :         WitnessV0ScriptHash hash;
      79                 :      132394 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
      80                 :      132394 :         addressRet = hash;
      81                 :      132394 :         return true;
      82                 :             :     }
      83                 :      303268 :     case TxoutType::WITNESS_V1_TAPROOT: {
      84                 :      303268 :         WitnessV1Taproot tap;
      85                 :      303268 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), tap.begin());
      86                 :      303268 :         addressRet = tap;
      87                 :      303268 :         return true;
      88                 :             :     }
      89                 :         626 :     case TxoutType::ANCHOR: {
      90         [ +  - ]:         626 :         addressRet = PayToAnchor();
      91                 :         626 :         return true;
      92                 :             :     }
      93                 :       58024 :     case TxoutType::WITNESS_UNKNOWN: {
      94         [ +  - ]:       58024 :         addressRet = WitnessUnknown{vSolutions[0][0], vSolutions[1]};
      95                 :       58024 :         return true;
      96                 :             :     }
      97                 :     2176585 :     case TxoutType::MULTISIG:
      98                 :     2176585 :     case TxoutType::NULL_DATA:
      99                 :     2176585 :     case TxoutType::NONSTANDARD:
     100                 :     2176585 :         addressRet = CNoDestination(scriptPubKey);
     101                 :     2176585 :         return false;
     102                 :             :     } // no default case, so the compiler can warn about missing cases
     103                 :           0 :     assert(false);
     104                 :     3313263 : }
     105                 :             : 
     106                 :             : namespace {
     107                 :             : class CScriptVisitor
     108                 :             : {
     109                 :             : public:
     110                 :       23667 :     CScript operator()(const CNoDestination& dest) const
     111                 :             :     {
     112                 :       23667 :         return dest.GetScript();
     113                 :             :     }
     114                 :             : 
     115                 :        2702 :     CScript operator()(const PubKeyDestination& dest) const
     116                 :             :     {
     117   [ +  -  +  - ]:        5404 :         return CScript() << ToByteVector(dest.GetPubKey()) << OP_CHECKSIG;
     118                 :             :     }
     119                 :             : 
     120                 :      384364 :     CScript operator()(const PKHash& keyID) const
     121                 :             :     {
     122   [ +  -  +  -  :      768728 :         return CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
          +  -  +  -  +  
                      - ]
     123                 :             :     }
     124                 :             : 
     125                 :      575809 :     CScript operator()(const ScriptHash& scriptID) const
     126                 :             :     {
     127   [ +  -  +  -  :     1151618 :         return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
                   +  - ]
     128                 :             :     }
     129                 :             : 
     130                 :      411422 :     CScript operator()(const WitnessV0KeyHash& id) const
     131                 :             :     {
     132   [ +  -  +  - ]:      822844 :         return CScript() << OP_0 << ToByteVector(id);
     133                 :             :     }
     134                 :             : 
     135                 :      130320 :     CScript operator()(const WitnessV0ScriptHash& id) const
     136                 :             :     {
     137   [ +  -  +  - ]:      260640 :         return CScript() << OP_0 << ToByteVector(id);
     138                 :             :     }
     139                 :             : 
     140                 :     1338557 :     CScript operator()(const WitnessV1Taproot& tap) const
     141                 :             :     {
     142   [ +  -  +  - ]:     2677114 :         return CScript() << OP_1 << ToByteVector(tap);
     143                 :             :     }
     144                 :             : 
     145                 :       34650 :     CScript operator()(const WitnessUnknown& id) const
     146                 :             :     {
     147   [ +  -  -  + ]:       34650 :         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                 :     2901491 : CScript GetScriptForDestination(const CTxDestination& dest)
     166                 :             : {
     167                 :     2901491 :     return std::visit(CScriptVisitor(), dest);
     168                 :             : }
     169                 :             : 
     170                 :       26186 : bool IsValidDestination(const CTxDestination& dest) {
     171                 :       26186 :     return std::visit(ValidDestinationVisitor(), dest);
     172                 :             : }
        

Generated by: LCOV version 2.5.0-full