Dash Core  0.12.2.1
P2P Digital Currency
wallet_ismine.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 "keystore.h"
10 #include "script/script.h"
11 #include "script/standard.h"
12 #include "script/sign.h"
13 
14 #include <boost/foreach.hpp>
15 
16 using namespace std;
17 
18 typedef vector<unsigned char> valtype;
19 
20 unsigned int HaveKeys(const vector<valtype>& pubkeys, const CKeyStore& keystore)
21 {
22  unsigned int nResult = 0;
23  BOOST_FOREACH(const valtype& pubkey, pubkeys)
24  {
25  CKeyID keyID = CPubKey(pubkey).GetID();
26  if (keystore.HaveKey(keyID))
27  ++nResult;
28  }
29  return nResult;
30 }
31 
32 isminetype IsMine(const CKeyStore &keystore, const CTxDestination& dest)
33 {
34  CScript script = GetScriptForDestination(dest);
35  return IsMine(keystore, script);
36 }
37 
38 isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
39 {
40  vector<valtype> vSolutions;
41  txnouttype whichType;
42  if (!Solver(scriptPubKey, whichType, vSolutions)) {
43  if (keystore.HaveWatchOnly(scriptPubKey))
45  return ISMINE_NO;
46  }
47 
48  CKeyID keyID;
49  switch (whichType)
50  {
51  case TX_NONSTANDARD:
52  case TX_NULL_DATA:
53  break;
54  case TX_PUBKEY:
55  keyID = CPubKey(vSolutions[0]).GetID();
56  if (keystore.HaveKey(keyID))
57  return ISMINE_SPENDABLE;
58  break;
59  case TX_PUBKEYHASH:
60  keyID = CKeyID(uint160(vSolutions[0]));
61  if (keystore.HaveKey(keyID))
62  return ISMINE_SPENDABLE;
63  break;
64  case TX_SCRIPTHASH:
65  {
66  CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
67  CScript subscript;
68  if (keystore.GetCScript(scriptID, subscript)) {
69  isminetype ret = IsMine(keystore, subscript);
70  if (ret == ISMINE_SPENDABLE)
71  return ret;
72  }
73  break;
74  }
75  case TX_MULTISIG:
76  {
77  // Only consider transactions "mine" if we own ALL the
78  // keys involved. Multi-signature transactions that are
79  // partially owned (somebody else has a key that can spend
80  // them) enable spend-out-from-under-you attacks, especially
81  // in shared-wallet situations.
82  vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
83  if (HaveKeys(keys, keystore) == keys.size())
84  return ISMINE_SPENDABLE;
85  break;
86  }
87  }
88 
89  if (keystore.HaveWatchOnly(scriptPubKey)) {
90  // TODO: This could be optimized some by doing some work after the above solver
91  CScript scriptSig;
92  return ProduceSignature(DummySignatureCreator(&keystore), scriptPubKey, scriptSig) ? ISMINE_WATCH_SOLVABLE : ISMINE_WATCH_UNSOLVABLE;
93  }
94  return ISMINE_NO;
95 }
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
Definition: standard.h:69
isminetype IsMine(const CKeyStore &keystore, const CTxDestination &dest)
Definition: pubkey.h:27
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const =0
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:144
CScript GetScriptForDestination(const CTxDestination &dest)
Definition: standard.cpp:262
virtual bool HaveWatchOnly(const CScript &dest) const =0
vector< unsigned char > valtype
txnouttype
Definition: standard.h:45
unsigned int HaveKeys(const vector< valtype > &pubkeys, const CKeyStore &keystore)
vector< unsigned char > valtype
Definition: interpreter.cpp:18
virtual bool HaveKey(const CKeyID &address) const =0
Check whether a key corresponding to a given address is present in the store.
Indicates that we know how to create a scriptSig that would solve this if we were given the appropria...
Definition: wallet_ismine.h:23
bool ProduceSignature(const BaseSignatureCreator &creator, const CScript &fromPubKey, CScript &scriptSig)
Definition: sign.cpp:104
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, vector< vector< unsigned char > > &vSolutionsRet)
Definition: standard.cpp:41
Definition: pubkey.h:37
isminetype
Definition: wallet_ismine.h:17
Indicates that we don&#39;t know how to create a scriptSig that would solve this if we were given the app...
Definition: wallet_ismine.h:21