addresstype.h raw

   1  // Copyright (c) 2023 The Limenka 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  #ifndef LIMENKA_ADDRESSTYPE_H
   6  #define LIMENKA_ADDRESSTYPE_H
   7  
   8  #include <attributes.h>
   9  #include <pubkey.h>
  10  #include <script/script.h>
  11  #include <uint256.h>
  12  #include <util/check.h>
  13  #include <util/hash_type.h>
  14  
  15  #include <algorithm>
  16  #include <variant>
  17  #include <vector>
  18  
  19  class CNoDestination
  20  {
  21  private:
  22      CScript m_script;
  23  
  24  public:
  25      CNoDestination() = default;
  26      explicit CNoDestination(const CScript& script) : m_script(script) {}
  27  
  28      const CScript& GetScript() const LIFETIMEBOUND { return m_script; }
  29  
  30      friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); }
  31      friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); }
  32  };
  33  
  34  struct PubKeyDestination {
  35  private:
  36      CPubKey m_pubkey;
  37  
  38  public:
  39      explicit PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
  40  
  41      const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; }
  42  
  43      friend bool operator==(const PubKeyDestination& a, const PubKeyDestination& b) { return a.GetPubKey() == b.GetPubKey(); }
  44      friend bool operator<(const PubKeyDestination& a, const PubKeyDestination& b) { return a.GetPubKey() < b.GetPubKey(); }
  45  };
  46  
  47  struct PKHash : public BaseHash<uint160>
  48  {
  49      PKHash() : BaseHash() {}
  50      explicit PKHash(const uint160& hash) : BaseHash(hash) {}
  51      explicit PKHash(const CPubKey& pubkey);
  52      explicit PKHash(const CKeyID& pubkey_id);
  53  };
  54  CKeyID ToKeyID(const PKHash& key_hash);
  55  
  56  struct WitnessV0KeyHash;
  57  
  58  struct ScriptHash : public BaseHash<uint160>
  59  {
  60      ScriptHash() : BaseHash() {}
  61      // These don't do what you'd expect.
  62      // Use ScriptHash(GetScriptForDestination(...)) instead.
  63      explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
  64      explicit ScriptHash(const PKHash& hash) = delete;
  65  
  66      explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
  67      explicit ScriptHash(const CScript& script);
  68      explicit ScriptHash(const CScriptID& script);
  69  };
  70  CScriptID ToScriptID(const ScriptHash& script_hash);
  71  
  72  struct WitnessV0ScriptHash : public BaseHash<uint256>
  73  {
  74      WitnessV0ScriptHash() : BaseHash() {}
  75      explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
  76      explicit WitnessV0ScriptHash(const CScript& script);
  77  };
  78  
  79  struct WitnessV0KeyHash : public BaseHash<uint160>
  80  {
  81      WitnessV0KeyHash() : BaseHash() {}
  82      explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
  83      explicit WitnessV0KeyHash(const CPubKey& pubkey);
  84      explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
  85  };
  86  CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
  87  
  88  struct WitnessV1Taproot : public XOnlyPubKey
  89  {
  90      WitnessV1Taproot() : XOnlyPubKey() {}
  91      explicit WitnessV1Taproot(const XOnlyPubKey& xpk) : XOnlyPubKey(xpk) {}
  92  };
  93  
  94  struct WitnessV3SpkHash : public BaseHash<uint256>
  95  {
  96      WitnessV3SpkHash() : BaseHash() {}
  97      explicit WitnessV3SpkHash(const uint256& hash) : BaseHash(hash) {}
  98      explicit WitnessV3SpkHash(const XOnlyPubKey& xpk);
  99  };
 100  
 101  //! CTxDestination subtype to encode any future Witness version
 102  //! CTxDestination subtype for the P2BPCT stealth address.
 103  //! Carries the receiver's static (view, spend) keys; the sender derives the
 104  //! confidential commitment per payment, so this maps to no fixed script.
 105  struct WitnessV4StealthAddress
 106  {
 107      CPubKey view;
 108      CPubKey spend;
 109  
 110      WitnessV4StealthAddress() = default;
 111      WitnessV4StealthAddress(const CPubKey& viewIn, const CPubKey& spendIn)
 112          : view{viewIn}, spend{spendIn} {}
 113  
 114      bool operator==(const WitnessV4StealthAddress& o) const
 115      {
 116          return view == o.view && spend == o.spend;
 117      }
 118      bool operator<(const WitnessV4StealthAddress& o) const
 119      {
 120          if (view != o.view) return view < o.view;
 121          return spend < o.spend;
 122      }
 123  };
 124  
 125  struct WitnessUnknown
 126  {
 127  private:
 128      unsigned int m_version;
 129      std::vector<unsigned char> m_program;
 130  
 131  public:
 132      WitnessUnknown(unsigned int version, const std::vector<unsigned char>& program) : m_version(version), m_program(program) {}
 133      WitnessUnknown(int version, const std::vector<unsigned char>& program) : m_version(static_cast<unsigned int>(version)), m_program(program) {}
 134  
 135      unsigned int GetWitnessVersion() const { return m_version; }
 136      const std::vector<unsigned char>& GetWitnessProgram() const LIFETIMEBOUND { return m_program; }
 137  
 138      friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
 139          if (w1.GetWitnessVersion() != w2.GetWitnessVersion()) return false;
 140          return w1.GetWitnessProgram() == w2.GetWitnessProgram();
 141      }
 142  
 143      friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
 144          if (w1.GetWitnessVersion() < w2.GetWitnessVersion()) return true;
 145          if (w1.GetWitnessVersion() > w2.GetWitnessVersion()) return false;
 146          return w1.GetWitnessProgram() < w2.GetWitnessProgram();
 147      }
 148  };
 149  
 150  struct PayToAnchor : public WitnessUnknown
 151  {
 152      PayToAnchor() : WitnessUnknown(1, {0x4e, 0x73}) {
 153          Assume(CScript::IsPayToAnchor(1, {0x4e, 0x73}));
 154      };
 155  };
 156  
 157  /**
 158   * A txout script categorized into standard templates.
 159   *  * CNoDestination: Optionally a script, no corresponding address.
 160   *  * PubKeyDestination: TxoutType::PUBKEY (P2PK), no corresponding address
 161   *  * PKHash: TxoutType::PUBKEYHASH destination (P2PKH address)
 162   *  * ScriptHash: TxoutType::SCRIPTHASH destination (P2SH address)
 163   *  * WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH destination (P2WSH address)
 164   *  * WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH destination (P2WPKH address)
 165   *  * WitnessV1Taproot: TxoutType::WITNESS_V1_TAPROOT destination (P2TR address)
 166   *  * WitnessV3SpkHash: TxoutType::WITNESS_V3_SPKHASH destination (P2SPKH address)
 167   *  * PayToAnchor: TxoutType::ANCHOR destination (P2A address)
 168   *  * WitnessUnknown: TxoutType::WITNESS_UNKNOWN destination (P2W??? address)
 169   *  A CTxDestination is the internal data type encoded in a limenka address
 170   */
 171  using CTxDestination = std::variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessV3SpkHash, WitnessV4StealthAddress, PayToAnchor, WitnessUnknown>;
 172  
 173  /** Check whether a CTxDestination corresponds to one with an address. */
 174  bool IsValidDestination(const CTxDestination& dest);
 175  
 176  /**
 177   * Parse a scriptPubKey for the destination.
 178   *
 179   * For standard scripts that have addresses (and P2PK as an exception), a corresponding CTxDestination
 180   * is assigned to addressRet.
 181   * For all other scripts. addressRet is assigned as a CNoDestination containing the scriptPubKey.
 182   *
 183   * Returns true for standard destinations with addresses - P2PKH, P2SH, P2WPKH, P2WSH, P2TR and P2W??? scripts.
 184   * Returns false for non-standard destinations and those without addresses - P2PK, bare multisig, null data, and nonstandard scripts.
 185   */
 186  bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
 187  
 188  /**
 189   * Generate a Limenka scriptPubKey for the given CTxDestination. Returns a P2PKH
 190   * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
 191   * script for CNoDestination.
 192   */
 193  CScript GetScriptForDestination(const CTxDestination& dest);
 194  
 195  #endif // LIMENKA_ADDRESSTYPE_H
 196