LCOV - code coverage report
Current view: top level - src - addresstype.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 88.5 % 78 69
Test Date: 2024-08-28 04:44:32 Functions: 90.0 % 20 18
Branches: 57.1 % 56 32

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023 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                 :       72764 : ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in)) {}
      21                 :           1 : ScriptHash::ScriptHash(const CScriptID& in) : BaseHash{in} {}
      22                 :             : 
      23                 :        2258 : PKHash::PKHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
      24                 :       72460 : PKHash::PKHash(const CKeyID& pubkey_id) : BaseHash(pubkey_id) {}
      25                 :             : 
      26                 :          34 : WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
      27                 :           1 : WitnessV0KeyHash::WitnessV0KeyHash(const PKHash& pubkey_hash) : BaseHash{pubkey_hash} {}
      28                 :             : 
      29                 :           1 : CKeyID ToKeyID(const PKHash& key_hash)
      30                 :             : {
      31                 :           1 :     return CKeyID{uint160{key_hash}};
      32                 :             : }
      33                 :             : 
      34                 :           0 : CKeyID ToKeyID(const WitnessV0KeyHash& key_hash)
      35                 :             : {
      36                 :           0 :     return CKeyID{uint160{key_hash}};
      37                 :             : }
      38                 :             : 
      39                 :           0 : CScriptID ToScriptID(const ScriptHash& script_hash)
      40                 :             : {
      41                 :           0 :     return CScriptID{uint160{script_hash}};
      42                 :             : }
      43                 :             : 
      44                 :        1328 : WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in)
      45                 :             : {
      46   [ +  +  +  + ]:        3771 :     CSHA256().Write(in.data(), in.size()).Finalize(begin());
      47                 :        1328 : }
      48                 :             : 
      49                 :        6385 : bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
      50                 :             : {
      51                 :        6385 :     std::vector<valtype> vSolutions;
      52         [ +  - ]:        6385 :     TxoutType whichType = Solver(scriptPubKey, vSolutions);
      53                 :             : 
      54   [ +  +  +  +  :        6385 :     switch (whichType) {
          +  +  -  +  +  
                      - ]
      55                 :          29 :     case TxoutType::PUBKEY: {
      56                 :          29 :         CPubKey pubKey(vSolutions[0]);
      57         [ -  + ]:          29 :         if (!pubKey.IsValid()) {
      58                 :           0 :             addressRet = CNoDestination(scriptPubKey);
      59                 :             :         } else {
      60                 :          29 :             addressRet = PubKeyDestination(pubKey);
      61                 :             :         }
      62                 :             :         return false;
      63                 :             :     }
      64                 :          65 :     case TxoutType::PUBKEYHASH: {
      65                 :          65 :         addressRet = PKHash(uint160(vSolutions[0]));
      66                 :          65 :         return true;
      67                 :             :     }
      68                 :          43 :     case TxoutType::SCRIPTHASH: {
      69                 :          43 :         addressRet = ScriptHash(uint160(vSolutions[0]));
      70                 :          43 :         return true;
      71                 :             :     }
      72                 :        6194 :     case TxoutType::WITNESS_V0_KEYHASH: {
      73                 :        6194 :         WitnessV0KeyHash hash;
      74                 :        6194 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
      75                 :        6194 :         addressRet = hash;
      76                 :        6194 :         return true;
      77                 :             :     }
      78                 :          14 :     case TxoutType::WITNESS_V0_SCRIPTHASH: {
      79                 :          14 :         WitnessV0ScriptHash hash;
      80                 :          14 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
      81                 :          14 :         addressRet = hash;
      82                 :          14 :         return true;
      83                 :             :     }
      84                 :          15 :     case TxoutType::WITNESS_V1_TAPROOT: {
      85                 :          15 :         WitnessV1Taproot tap;
      86                 :          15 :         std::copy(vSolutions[0].begin(), vSolutions[0].end(), tap.begin());
      87                 :          15 :         addressRet = tap;
      88                 :          15 :         return true;
      89                 :             :     }
      90                 :           0 :     case TxoutType::ANCHOR: {
      91         [ #  # ]:           0 :         addressRet = PayToAnchor();
      92                 :           0 :         return true;
      93                 :             :     }
      94                 :           9 :     case TxoutType::WITNESS_UNKNOWN: {
      95         [ +  - ]:           9 :         addressRet = WitnessUnknown{vSolutions[0][0], vSolutions[1]};
      96                 :           9 :         return true;
      97                 :             :     }
      98                 :          16 :     case TxoutType::MULTISIG:
      99                 :          16 :     case TxoutType::NULL_DATA:
     100                 :          16 :     case TxoutType::NONSTANDARD:
     101                 :          16 :         addressRet = CNoDestination(scriptPubKey);
     102                 :          16 :         return false;
     103                 :             :     } // no default case, so the compiler can warn about missing cases
     104                 :           0 :     assert(false);
     105                 :        6385 : }
     106                 :             : 
     107                 :             : namespace {
     108                 :             : class CScriptVisitor
     109                 :             : {
     110                 :             : public:
     111                 :           3 :     CScript operator()(const CNoDestination& dest) const
     112                 :             :     {
     113                 :           3 :         return dest.GetScript();
     114                 :             :     }
     115                 :             : 
     116                 :          27 :     CScript operator()(const PubKeyDestination& dest) const
     117                 :             :     {
     118   [ +  -  +  - ]:          27 :         return CScript() << ToByteVector(dest.GetPubKey()) << OP_CHECKSIG;
     119                 :             :     }
     120                 :             : 
     121                 :       74802 :     CScript operator()(const PKHash& keyID) const
     122                 :             :     {
     123   [ +  -  +  -  :       74802 :         return CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
          +  -  +  -  +  
                      - ]
     124                 :             :     }
     125                 :             : 
     126                 :       72800 :     CScript operator()(const ScriptHash& scriptID) const
     127                 :             :     {
     128   [ +  -  +  -  :       72800 :         return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
                   +  - ]
     129                 :             :     }
     130                 :             : 
     131                 :      186908 :     CScript operator()(const WitnessV0KeyHash& id) const
     132                 :             :     {
     133   [ +  -  +  - ]:      186908 :         return CScript() << OP_0 << ToByteVector(id);
     134                 :             :     }
     135                 :             : 
     136                 :         747 :     CScript operator()(const WitnessV0ScriptHash& id) const
     137                 :             :     {
     138   [ +  -  +  - ]:         747 :         return CScript() << OP_0 << ToByteVector(id);
     139                 :             :     }
     140                 :             : 
     141                 :       74256 :     CScript operator()(const WitnessV1Taproot& tap) const
     142                 :             :     {
     143   [ +  -  +  - ]:       74256 :         return CScript() << OP_1 << ToByteVector(tap);
     144                 :             :     }
     145                 :             : 
     146                 :          17 :     CScript operator()(const WitnessUnknown& id) const
     147                 :             :     {
     148         [ +  - ]:          17 :         return CScript() << CScript::EncodeOP_N(id.GetWitnessVersion()) << id.GetWitnessProgram();
     149                 :             :     }
     150                 :             : };
     151                 :             : 
     152                 :             : class ValidDestinationVisitor
     153                 :             : {
     154                 :             : public:
     155                 :             :     bool operator()(const CNoDestination& dest) const { return false; }
     156                 :             :     bool operator()(const PubKeyDestination& dest) const { return false; }
     157                 :             :     bool operator()(const PKHash& dest) const { return true; }
     158                 :             :     bool operator()(const ScriptHash& dest) const { return true; }
     159                 :             :     bool operator()(const WitnessV0KeyHash& dest) const { return true; }
     160                 :             :     bool operator()(const WitnessV0ScriptHash& dest) const { return true; }
     161                 :             :     bool operator()(const WitnessV1Taproot& dest) const { return true; }
     162                 :             :     bool operator()(const WitnessUnknown& dest) const { return true; }
     163                 :             : };
     164                 :             : } // namespace
     165                 :             : 
     166                 :      409560 : CScript GetScriptForDestination(const CTxDestination& dest)
     167                 :             : {
     168                 :      409560 :     return std::visit(CScriptVisitor(), dest);
     169                 :             : }
     170                 :             : 
     171                 :         498 : bool IsValidDestination(const CTxDestination& dest) {
     172                 :         498 :     return std::visit(ValidDestinationVisitor(), dest);
     173                 :             : }
        

Generated by: LCOV version 2.0-1