Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge #16226 #4640

Merged
merged 1 commit into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ BITCOIN_CORE_H = \
saltedhasher.h \
scheduler.h \
script/descriptor.h \
script/ismine.h \
script/sigcache.h \
script/sign.h \
script/standard.h \
Expand Down Expand Up @@ -325,6 +324,7 @@ BITCOIN_CORE_H = \
wallet/crypter.h \
wallet/db.h \
wallet/fees.h \
wallet/ismine.h \
wallet/load.h \
wallet/psbtwallet.h \
wallet/rpcwallet.h \
Expand Down Expand Up @@ -485,6 +485,7 @@ libdash_wallet_a_SOURCES = \
wallet/crypter.cpp \
wallet/db.cpp \
wallet/fees.cpp \
wallet/ismine.cpp \
wallet/load.cpp \
wallet/psbtwallet.cpp \
wallet/rpcdump.cpp \
Expand Down Expand Up @@ -661,7 +662,6 @@ libdash_common_a_SOURCES = \
saltedhasher.cpp \
scheduler.cpp \
script/descriptor.cpp \
script/ismine.cpp \
script/sign.cpp \
script/standard.cpp \
versionbitsinfo.cpp \
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ BITCOIN_TESTS += \
wallet/test/wallet_tests.cpp \
wallet/test/wallet_crypto_tests.cpp \
wallet/test/coinselector_tests.cpp \
wallet/test/init_tests.cpp
wallet/test/init_tests.cpp \
wallet/test/ismine_tests.cpp

BITCOIN_TEST_SUITE += \
wallet/test/wallet_test_fixture.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <interfaces/handler.h>
#include <policy/fees.h>
#include <primitives/transaction.h>
#include <script/ismine.h>
#include <script/standard.h>
#include <support/allocators/secure.h>
#include <sync.h>
Expand All @@ -20,6 +19,7 @@
#include <util/system.h>
#include <validation.h>
#include <wallet/fees.h>
#include <wallet/ismine.h>
#include <wallet/rpcwallet.h>
#include <wallet/load.h>
#include <wallet/wallet.h>
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <amount.h> // For CAmount
#include <fs.h> // For fs::path
#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
#include <script/ismine.h> // For isminefilter, isminetype
#include <script/standard.h> // For CTxDestination
#include <support/allocators/secure.h> // For SecureString
#include <ui_interface.h> // For ChangeType
Expand All @@ -26,7 +25,9 @@ class CCoinControl;
class CFeeRate;
class CKey;
class CWallet;
enum isminetype : unsigned int;
enum class FeeReason;
typedef uint8_t isminefilter;
struct CRecipient;

namespace interfaces {
Expand Down
1 change: 1 addition & 0 deletions src/qt/transactiondesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <validation.h>
#include <script/script.h>
#include <util/system.h>
#include <wallet/ismine.h>

#include <stdint.h>
#include <string>
Expand Down
2 changes: 2 additions & 0 deletions src/qt/transactionrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <interfaces/wallet.h>
#include <interfaces/node.h>

#include <wallet/ismine.h>

#include <stdint.h>

#include <QDateTime>
Expand Down
3 changes: 0 additions & 3 deletions src/test/script_p2sh_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <script/script.h>
#include <script/script_error.h>
#include <script/sign.h>
#include <script/ismine.h>
#include <test/util/setup_common.h>

#include <vector>
Expand Down Expand Up @@ -97,7 +96,6 @@ BOOST_AUTO_TEST_CASE(sign)
txTo[i].vin[0].prevout.n = i;
txTo[i].vin[0].prevout.hash = txFrom.GetHash();
txTo[i].vout[0].nValue = 1;
BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
}
for (int i = 0; i < 8; i++)
{
Expand Down Expand Up @@ -194,7 +192,6 @@ BOOST_AUTO_TEST_CASE(set)
txTo[i].vin[0].prevout.hash = txFrom.GetHash();
txTo[i].vout[0].nValue = 1*CENT;
txTo[i].vout[0].scriptPubKey = inner[i];
BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
}
for (int i = 0; i < 4; i++)
{
Expand Down
188 changes: 0 additions & 188 deletions src/test/script_standard_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <key.h>
#include <keystore.h>
#include <script/ismine.h>
#include <script/script.h>
#include <script/standard.h>
#include <test/util/setup_common.h>
Expand Down Expand Up @@ -295,191 +294,4 @@ BOOST_AUTO_TEST_CASE(script_standard_GetScriptFor_)
BOOST_CHECK(result == expected);
}

BOOST_AUTO_TEST_CASE(script_standard_IsMine)
{
CKey keys[2];
CPubKey pubkeys[2];
for (int i = 0; i < 2; i++) {
keys[i].MakeNewKey(true);
pubkeys[i] = keys[i].GetPubKey();
}

CKey uncompressedKey;
uncompressedKey.MakeNewKey(false);
CPubKey uncompressedPubkey = uncompressedKey.GetPubKey();

CScript scriptPubKey;
isminetype result;

// P2PK compressed
{
CBasicKeyStore keystore;
scriptPubKey = GetScriptForRawPubKey(pubkeys[0]);

// Keystore does not have key
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has key
BOOST_CHECK(keystore.AddKey(keys[0]));
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
}

// P2PK uncompressed
{
CBasicKeyStore keystore;
scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey);

// Keystore does not have key
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has key
BOOST_CHECK(keystore.AddKey(uncompressedKey));
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
}

// P2PKH compressed
{
CBasicKeyStore keystore;
scriptPubKey = GetScriptForDestination(pubkeys[0].GetID());

// Keystore does not have key
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has key
BOOST_CHECK(keystore.AddKey(keys[0]));
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
}

// P2PKH uncompressed
{
CBasicKeyStore keystore;
scriptPubKey = GetScriptForDestination(uncompressedPubkey.GetID());

// Keystore does not have key
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has key
BOOST_CHECK(keystore.AddKey(uncompressedKey));
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
}

// P2SH
{
CBasicKeyStore keystore;

CScript redeemScript = GetScriptForDestination(pubkeys[0].GetID());
scriptPubKey = GetScriptForDestination(CScriptID(redeemScript));

// Keystore does not have redeemScript or key
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has redeemScript but no key
BOOST_CHECK(keystore.AddCScript(redeemScript));
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has redeemScript and key
BOOST_CHECK(keystore.AddKey(keys[0]));
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
}

// (P2PKH inside) P2SH inside P2SH (invalid)
{
CBasicKeyStore keystore;

CScript redeemscript_inner = GetScriptForDestination(pubkeys[0].GetID());
CScript redeemscript = GetScriptForDestination(CScriptID(redeemscript_inner));
scriptPubKey = GetScriptForDestination(CScriptID(redeemscript));

BOOST_CHECK(keystore.AddCScript(redeemscript));
BOOST_CHECK(keystore.AddCScript(redeemscript_inner));
BOOST_CHECK(keystore.AddCScript(scriptPubKey));
BOOST_CHECK(keystore.AddKey(keys[0]));
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
}

// scriptPubKey multisig
{
CBasicKeyStore keystore;

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

// Keystore does not have any keys
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has 1/2 keys
BOOST_CHECK(keystore.AddKey(uncompressedKey));

result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has 2/2 keys
BOOST_CHECK(keystore.AddKey(keys[1]));

result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has 2/2 keys and the script
BOOST_CHECK(keystore.AddCScript(scriptPubKey));

result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
}

// P2SH multisig
{
CBasicKeyStore keystore;
BOOST_CHECK(keystore.AddKey(uncompressedKey));
BOOST_CHECK(keystore.AddKey(keys[1]));

CScript redeemScript = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
scriptPubKey = GetScriptForDestination(CScriptID(redeemScript));

// Keystore has no redeemScript
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);

// Keystore has redeemScript
BOOST_CHECK(keystore.AddCScript(redeemScript));
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
}

// OP_RETURN
{
CBasicKeyStore keystore;
BOOST_CHECK(keystore.AddKey(keys[0]));

scriptPubKey.clear();
scriptPubKey << OP_RETURN << ToByteVector(pubkeys[0]);

result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
}

// Nonstandard
{
CBasicKeyStore keystore;
BOOST_CHECK(keystore.AddKey(keys[0]));

scriptPubKey.clear();
scriptPubKey << OP_9 << OP_ADD << OP_11 << OP_EQUAL;

result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
}
}

BOOST_AUTO_TEST_SUITE_END()
13 changes: 6 additions & 7 deletions src/script/ismine.cpp → src/wallet/ismine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <script/ismine.h>
#include <wallet/ismine.h>

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont have #include <script/sign.h> in line 10 (as in upstream) So two lines are deleted

#include <wallet/wallet.h>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already a line break is here, do we need to add another?

typedef std::vector<unsigned char> valtype;

Expand Down Expand Up @@ -44,7 +43,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 @@ -53,7 +52,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 @@ -135,7 +134,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 @@ -149,7 +148,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
Loading