PRCYCoin  2.0.0.7rc1
P2P Digital Currency
wallet_ismine.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #include "wallet/wallet_ismine.h"
9 
10 #include "key.h"
11 #include "keystore.h"
12 #include "script/script.h"
13 #include "script/standard.h"
14 #include "util.h"
15 
16 
17 typedef std::vector<unsigned char> valtype;
18 
19 unsigned int HaveKeys(const std::vector<valtype>& pubkeys, const CKeyStore& keystore)
20 {
21  unsigned int nResult = 0;
22  for (const valtype& pubkey : pubkeys) {
23  CKeyID keyID = CPubKey(pubkey).GetID();
24  if(keystore.HaveKey(keyID))
25  ++nResult;
26  }
27  return nResult;
28 }
29 
30 isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest)
31 {
32  CScript script = GetScriptForDestination(dest);
33  return IsMine(keystore, script);
34 }
35 
36 isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey)
37 {
38  if(keystore.HaveWatchOnly(scriptPubKey))
39  return ISMINE_WATCH_ONLY;
40 
41  std::vector<valtype> vSolutions;
42  txnouttype whichType;
43  if(!Solver(scriptPubKey, whichType, vSolutions)) {
44  if(keystore.HaveWatchOnly(scriptPubKey))
45  return ISMINE_WATCH_ONLY;
46 
47  return ISMINE_NO;
48  }
49 
50  CKeyID keyID;
51  switch (whichType) {
52  case TX_NONSTANDARD:
53  case TX_NULL_DATA:
54  break;
55  case TX_PUBKEY:
56  keyID = CPubKey(vSolutions[0]).GetID();
57  if(keystore.HaveKey(keyID)) {
58  return ISMINE_SPENDABLE;
59  }
60  break;
61  case TX_PUBKEYHASH:
62  keyID = CKeyID(uint160(vSolutions[0]));
63  if(keystore.HaveKey(keyID))
64  return ISMINE_SPENDABLE;
65  break;
66  case TX_SCRIPTHASH: {
67  CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
68  CScript subscript;
69  if(keystore.GetCScript(scriptID, subscript)) {
70  isminetype ret = IsMine(keystore, subscript);
71  if(ret != ISMINE_NO)
72  return ret;
73  }
74  break;
75  }
76  case TX_MULTISIG: {
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  std::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  return ISMINE_WATCH_ONLY;
91 
92  return ISMINE_NO;
93 }
CKeyStore
A virtual base class for key stores.
Definition: keystore.h:21
IsMine
isminetype IsMine(const CKeyStore &keystore, const CTxDestination &dest)
Definition: wallet_ismine.cpp:30
ISMINE_NO
@ ISMINE_NO
Definition: wallet_ismine.h:17
CKeyStore::HaveWatchOnly
virtual bool HaveWatchOnly(const CScript &dest) const =0
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Definition: standard.cpp:285
isminetype
isminetype
IsMine() return codes.
Definition: wallet_ismine.h:16
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
TX_NONSTANDARD
@ TX_NONSTANDARD
Definition: standard.h:59
HaveKeys
unsigned int HaveKeys(const std::vector< valtype > &pubkeys, const CKeyStore &keystore)
Definition: wallet_ismine.cpp:19
TX_PUBKEYHASH
@ TX_PUBKEYHASH
Definition: standard.h:62
TX_MULTISIG
@ TX_MULTISIG
Definition: standard.h:64
CKeyStore::GetCScript
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const =0
TX_PUBKEY
@ TX_PUBKEY
Definition: standard.h:61
TX_SCRIPTHASH
@ TX_SCRIPTHASH
Definition: standard.h:63
standard.h
ISMINE_SPENDABLE
@ ISMINE_SPENDABLE
Definition: wallet_ismine.h:20
keystore.h
CKeyStore::HaveKey
virtual bool HaveKey(const CKeyID &address) const =0
Check whether a key corresponding to a given address is present in the store.
TX_NULL_DATA
@ TX_NULL_DATA
Definition: standard.h:65
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:363
ISMINE_WATCH_ONLY
@ ISMINE_WATCH_ONLY
Indicates that we dont know how to create a scriptSig that would solve this if we were given the appr...
Definition: wallet_ismine.h:19
CTxDestination
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:81
key.h
uint160
160-bit unsigned big integer.
Definition: uint256.h:27
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
Solver
bool Solver(const CKeyStore &keystore, const CScript &scriptPubKey, uint256 hash, int nHashType, CScript &scriptSigRet, txnouttype &whichTypeRet)
Sign scriptPubKey with private keys stored in keystore, given transaction hash and hash type.
Definition: sign.cpp:50
valtype
std::vector< unsigned char > valtype
Definition: wallet_ismine.cpp:17
valtype
std::vector< unsigned char > valtype
Definition: interpreter.cpp:19
script.h
txnouttype
txnouttype
Definition: standard.h:57
wallet_ismine.h
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:20
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:143