Skip to content

Commit

Permalink
Change ismine to take a CWallet instead of CKeyStore
Browse files Browse the repository at this point in the history
  • Loading branch information
achow101 committed Jun 19, 2019
1 parent 7c611e2 commit e61de63
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
11 changes: 5 additions & 6 deletions src/wallet/ismine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
#include <wallet/ismine.h>

#include <key.h>
#include <keystore.h>
#include <script/script.h>
#include <script/sign.h>

#include <wallet/wallet.h>

typedef std::vector<unsigned char> valtype;

Expand Down Expand Up @@ -46,7 +45,7 @@ bool PermitsUncompressed(IsMineSigVersion sigversion)
return sigversion == IsMineSigVersion::TOP || sigversion == IsMineSigVersion::P2SH;
}

bool HaveKeys(const std::vector<valtype>& pubkeys, const CKeyStore& keystore)
bool HaveKeys(const std::vector<valtype>& pubkeys, const CWallet& keystore)
{
for (const valtype& pubkey : pubkeys) {
CKeyID keyID = CPubKey(pubkey).GetID();
Expand All @@ -55,7 +54,7 @@ bool HaveKeys(const std::vector<valtype>& pubkeys, const CKeyStore& keystore)
return true;
}

IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey, IsMineSigVersion sigversion)
IsMineResult IsMineInner(const CWallet& keystore, const CScript& scriptPubKey, IsMineSigVersion sigversion)
{
IsMineResult ret = IsMineResult::NO;

Expand Down Expand Up @@ -172,7 +171,7 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,

} // namespace

isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey)
isminetype IsMine(const CWallet& keystore, const CScript& scriptPubKey)
{
switch (IsMineInner(keystore, scriptPubKey, IsMineSigVersion::TOP)) {
case IsMineResult::INVALID:
Expand All @@ -186,7 +185,7 @@ isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey)
assert(false);
}

isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest)
isminetype IsMine(const CWallet& keystore, const CTxDestination& dest)
{
CScript script = GetScriptForDestination(dest);
return IsMine(keystore, script);
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/ismine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <stdint.h>
#include <bitset>

class CKeyStore;
class CWallet;
class CScript;

/** IsMine() return codes */
Expand All @@ -28,8 +28,8 @@ enum isminetype : unsigned int
/** used for bitflags of isminetype */
typedef uint8_t isminefilter;

isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest);
isminetype IsMine(const CWallet& wallet, const CScript& scriptPubKey);
isminetype IsMine(const CWallet& wallet, const CTxDestination& dest);

/**
* Cachable amount subdivided into watchonly and spendable parts.
Expand Down
63 changes: 42 additions & 21 deletions src/wallet/test/ismine_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <key.h>
#include <keystore.h>
#include <script/script.h>
#include <script/script_error.h>
#include <script/standard.h>
#include <test/setup_common.h>
#include <wallet/ismine.h>
#include <wallet/wallet.h>

#include <boost/test/unit_test.hpp>

Expand All @@ -27,13 +27,15 @@ BOOST_AUTO_TEST_CASE(ismine_standard)
CKey uncompressedKey;
uncompressedKey.MakeNewKey(false);
CPubKey uncompressedPubkey = uncompressedKey.GetPubKey();
std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain();

CScript scriptPubKey;
isminetype result;

// P2PK compressed
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
scriptPubKey = GetScriptForRawPubKey(pubkeys[0]);

// Keystore does not have key
Expand All @@ -48,7 +50,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2PK uncompressed
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey);

// Keystore does not have key
Expand All @@ -63,7 +66,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2PKH compressed
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
scriptPubKey = GetScriptForDestination(PKHash(pubkeys[0]));

// Keystore does not have key
Expand All @@ -78,7 +82,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2PKH uncompressed
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey));

// Keystore does not have key
Expand All @@ -93,7 +98,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2SH
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);

CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
Expand All @@ -115,7 +121,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// (P2PKH inside) P2SH inside P2SH (invalid)
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);

CScript redeemscript_inner = GetScriptForDestination(PKHash(pubkeys[0]));
CScript redeemscript = GetScriptForDestination(ScriptHash(redeemscript_inner));
Expand All @@ -131,7 +138,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// (P2PKH inside) P2SH inside P2WSH (invalid)
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);

CScript redeemscript = GetScriptForDestination(PKHash(pubkeys[0]));
CScript witnessscript = GetScriptForDestination(ScriptHash(redeemscript));
Expand All @@ -147,7 +155,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2WPKH inside P2WSH (invalid)
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);

CScript witnessscript = GetScriptForDestination(WitnessV0KeyHash(PKHash(pubkeys[0])));
scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessscript));
Expand All @@ -161,7 +170,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// (P2PKH inside) P2WSH inside P2WSH (invalid)
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);

CScript witnessscript_inner = GetScriptForDestination(PKHash(pubkeys[0]));
CScript witnessscript = GetScriptForDestination(WitnessV0ScriptHash(witnessscript_inner));
Expand All @@ -177,7 +187,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2WPKH compressed
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(keys[0]));

scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(PKHash(pubkeys[0])));
Expand All @@ -190,7 +201,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2WPKH uncompressed
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(uncompressedKey));

scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(PKHash(uncompressedPubkey)));
Expand All @@ -207,7 +219,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// scriptPubKey multisig
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);

scriptPubKey = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});

Expand Down Expand Up @@ -236,7 +249,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2SH multisig
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(uncompressedKey));
BOOST_CHECK(keystore.AddKey(keys[1]));

Expand All @@ -255,7 +269,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2WSH multisig with compressed keys
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(keys[0]));
BOOST_CHECK(keystore.AddKey(keys[1]));

Expand All @@ -279,7 +294,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2WSH multisig with uncompressed key
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(uncompressedKey));
BOOST_CHECK(keystore.AddKey(keys[1]));

Expand All @@ -303,7 +319,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// P2WSH multisig wrapped in P2SH
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);

CScript witnessScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
Expand All @@ -328,7 +345,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// OP_RETURN
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(keys[0]));

scriptPubKey.clear();
Expand All @@ -340,7 +358,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// witness unspendable
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(keys[0]));

scriptPubKey.clear();
Expand All @@ -352,7 +371,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// witness unknown
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(keys[0]));

scriptPubKey.clear();
Expand All @@ -364,7 +384,8 @@ BOOST_AUTO_TEST_CASE(ismine_standard)

// Nonstandard
{
CBasicKeyStore keystore;
CWallet keystore(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
LOCK(keystore.cs_wallet);
BOOST_CHECK(keystore.AddKey(keys[0]));

scriptPubKey.clear();
Expand Down
1 change: 1 addition & 0 deletions test/lint/lint-circular-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ EXPECTED_CIRCULAR_DEPENDENCIES=(
"policy/fees -> txmempool -> validation -> policy/fees"
"qt/guiutil -> qt/walletmodel -> qt/optionsmodel -> qt/guiutil"
"txmempool -> validation -> validationinterface -> txmempool"
"wallet/ismine -> wallet/wallet -> wallet/ismine"
)

EXIT_CODE=0
Expand Down

0 comments on commit e61de63

Please sign in to comment.