Bitcoin Core  0.18.99
P2P Digital Currency
ismine.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <wallet/ismine.h>
7 
8 #include <key.h>
9 #include <script/script.h>
10 #include <script/sign.h>
11 #include <wallet/wallet.h>
12 
13 typedef std::vector<unsigned char> valtype;
14 
15 namespace {
16 
23 enum class IsMineSigVersion
24 {
25  TOP = 0,
26  P2SH = 1,
27  WITNESS_V0 = 2,
28 };
29 
35 enum class IsMineResult
36 {
37  NO = 0,
38  WATCH_ONLY = 1,
39  SPENDABLE = 2,
40  INVALID = 3,
41 };
42 
43 bool PermitsUncompressed(IsMineSigVersion sigversion)
44 {
45  return sigversion == IsMineSigVersion::TOP || sigversion == IsMineSigVersion::P2SH;
46 }
47 
48 bool HaveKeys(const std::vector<valtype>& pubkeys, const CWallet& keystore)
49 {
50  for (const valtype& pubkey : pubkeys) {
51  CKeyID keyID = CPubKey(pubkey).GetID();
52  if (!keystore.HaveKey(keyID)) return false;
53  }
54  return true;
55 }
56 
57 IsMineResult IsMineInner(const CWallet& keystore, const CScript& scriptPubKey, IsMineSigVersion sigversion)
58 {
59  IsMineResult ret = IsMineResult::NO;
60 
61  std::vector<valtype> vSolutions;
62  txnouttype whichType = Solver(scriptPubKey, vSolutions);
63 
64  CKeyID keyID;
65  switch (whichType)
66  {
67  case TX_NONSTANDARD:
68  case TX_NULL_DATA:
69  case TX_WITNESS_UNKNOWN:
70  break;
71  case TX_PUBKEY:
72  keyID = CPubKey(vSolutions[0]).GetID();
73  if (!PermitsUncompressed(sigversion) && vSolutions[0].size() != 33) {
74  return IsMineResult::INVALID;
75  }
76  if (keystore.HaveKey(keyID)) {
77  ret = std::max(ret, IsMineResult::SPENDABLE);
78  }
79  break;
81  {
82  if (sigversion == IsMineSigVersion::WITNESS_V0) {
83  // P2WPKH inside P2WSH is invalid.
84  return IsMineResult::INVALID;
85  }
86  if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
87  // We do not support bare witness outputs unless the P2SH version of it would be
88  // acceptable as well. This protects against matching before segwit activates.
89  // This also applies to the P2WSH case.
90  break;
91  }
92  ret = std::max(ret, IsMineInner(keystore, GetScriptForDestination(PKHash(uint160(vSolutions[0]))), IsMineSigVersion::WITNESS_V0));
93  break;
94  }
95  case TX_PUBKEYHASH:
96  keyID = CKeyID(uint160(vSolutions[0]));
97  if (!PermitsUncompressed(sigversion)) {
98  CPubKey pubkey;
99  if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) {
100  return IsMineResult::INVALID;
101  }
102  }
103  if (keystore.HaveKey(keyID)) {
104  ret = std::max(ret, IsMineResult::SPENDABLE);
105  }
106  break;
107  case TX_SCRIPTHASH:
108  {
109  if (sigversion != IsMineSigVersion::TOP) {
110  // P2SH inside P2WSH or P2SH is invalid.
111  return IsMineResult::INVALID;
112  }
113  CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
114  CScript subscript;
115  if (keystore.GetCScript(scriptID, subscript)) {
116  ret = std::max(ret, IsMineInner(keystore, subscript, IsMineSigVersion::P2SH));
117  }
118  break;
119  }
121  {
122  if (sigversion == IsMineSigVersion::WITNESS_V0) {
123  // P2WSH inside P2WSH is invalid.
124  return IsMineResult::INVALID;
125  }
126  if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
127  break;
128  }
129  uint160 hash;
130  CRIPEMD160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(hash.begin());
131  CScriptID scriptID = CScriptID(hash);
132  CScript subscript;
133  if (keystore.GetCScript(scriptID, subscript)) {
134  ret = std::max(ret, IsMineInner(keystore, subscript, IsMineSigVersion::WITNESS_V0));
135  }
136  break;
137  }
138 
139  case TX_MULTISIG:
140  {
141  // Never treat bare multisig outputs as ours (they can still be made watchonly-though)
142  if (sigversion == IsMineSigVersion::TOP) {
143  break;
144  }
145 
146  // Only consider transactions "mine" if we own ALL the
147  // keys involved. Multi-signature transactions that are
148  // partially owned (somebody else has a key that can spend
149  // them) enable spend-out-from-under-you attacks, especially
150  // in shared-wallet situations.
151  std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
152  if (!PermitsUncompressed(sigversion)) {
153  for (size_t i = 0; i < keys.size(); i++) {
154  if (keys[i].size() != 33) {
155  return IsMineResult::INVALID;
156  }
157  }
158  }
159  if (HaveKeys(keys, keystore)) {
160  ret = std::max(ret, IsMineResult::SPENDABLE);
161  }
162  break;
163  }
164  }
165 
166  if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
167  ret = std::max(ret, IsMineResult::WATCH_ONLY);
168  }
169  return ret;
170 }
171 
172 } // namespace
173 
174 isminetype IsMine(const CWallet& keystore, const CScript& scriptPubKey)
175 {
176  switch (IsMineInner(keystore, scriptPubKey, IsMineSigVersion::TOP)) {
177  case IsMineResult::INVALID:
178  case IsMineResult::NO:
179  return ISMINE_NO;
180  case IsMineResult::WATCH_ONLY:
181  return ISMINE_WATCH_ONLY;
182  case IsMineResult::SPENDABLE:
183  return ISMINE_SPENDABLE;
184  }
185  assert(false);
186 }
187 
188 isminetype IsMine(const CWallet& keystore, const CTxDestination& dest)
189 {
190  CScript script = GetScriptForDestination(dest);
191  return IsMine(keystore, script);
192 }
Top-level scriptPubKey.
unspendable OP_RETURN script that carries data
Definition: standard.h:64
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: crypter.cpp:252
IsMineSigVersion
This is an enum that tracks the execution context of a script, similar to SigVersion in script/interp...
Definition: ismine.cpp:23
std::vector< unsigned char > valtype
Definition: ismine.cpp:13
bool HaveCScript(const CScriptID &hash) const override
Definition: keystore.cpp:96
bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
Definition: keystore.cpp:112
txnouttype Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:92
IsMineResult
This is an internal representation of isminetype + invalidity.
Definition: ismine.cpp:35
unsigned char * begin()
Definition: uint256.h:55
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:155
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: crypter.cpp:278
Only for Witness versions not already defined above.
Definition: standard.h:67
An encapsulated public key.
Definition: pubkey.h:30
isminetype
IsMine() return codes.
Definition: ismine.h:18
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:290
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
P2SH redeemScript.
isminetype IsMine(const CWallet &keystore, const CScript &scriptPubKey)
Definition: ismine.cpp:174
txnouttype
Definition: standard.h:56
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:712
160-bit opaque blob.
Definition: uint256.h:110
std::vector< unsigned char > valtype
Definition: interpreter.cpp:15
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:22
bool HaveWatchOnly(const CScript &dest) const override
Definition: keystore.cpp:165
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:139
Definition: script.h:57
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:180