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

Check if we can spend or stake remote staking outputs #517

Merged
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
7 changes: 2 additions & 5 deletions src/esperanza/walletextension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ void WalletExtension::ForEachStakeableCoin(Callable f) const {
continue;
}
const CTxOut &coin = coins[outix];
if (m_enclosing_wallet.GetCredit(coin, ISMINE_SPENDABLE) <= 0) {
continue;
}
// UNIT-E TODO: Restrict to P2WPKH only once #212 is merged (fixes #48)
if (!coin.scriptPubKey.IsPayToWitnessScriptHash() && !coin.scriptPubKey.IsPayToPublicKeyHash()) {
if (!IsStakeableByMe(m_enclosing_wallet, coin.scriptPubKey) || coin.nValue <= 0) {
continue;
}

f(tx, std::uint32_t(outix), depth);
}
}
Expand Down
61 changes: 52 additions & 9 deletions src/script/ismine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
return IsMineResult::INVALID;
}
if (keystore.HaveKey(keyID)) {
ret = std::max(ret, IsMineResult::SPENDABLE);
ret = IsMineResult::SPENDABLE;
}
if (keystore.HaveHardwareKey(keyID)) {
ret = std::max(ret, IsMineResult::HW_DEVICE);
ret = IsMineResult::HW_DEVICE;
}
break;
case TX_WITNESS_V0_KEYHASH:
Expand All @@ -112,7 +112,7 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
// This also applies to the P2WSH case.
break;
}
ret = std::max(ret, IsMineInner(keystore, GetScriptForDestination(CKeyID(uint160(vSolutions[0]))), IsMineSigVersion::WITNESS_V0));
ret = IsMineInner(keystore, GetScriptForDestination(CKeyID(uint160(vSolutions[0]))), IsMineSigVersion::WITNESS_V0);
break;
}
case TX_PUBKEYHASH:
Expand All @@ -124,10 +124,10 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
}
}
if (keystore.HaveKey(keyID)) {
ret = std::max(ret, IsMineResult::SPENDABLE);
ret = IsMineResult::SPENDABLE;
}
if (keystore.HaveHardwareKey(keyID)) {
ret = std::max(ret, IsMineResult::HW_DEVICE);
ret = IsMineResult::HW_DEVICE;
}
break;
case TX_SCRIPTHASH:
Expand All @@ -139,7 +139,7 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
CScript subscript;
if (keystore.GetCScript(scriptID, subscript)) {
ret = std::max(ret, IsMineInner(keystore, subscript, IsMineSigVersion::P2SH));
ret = IsMineInner(keystore, subscript, IsMineSigVersion::P2SH);
}
break;
}
Expand All @@ -157,7 +157,7 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
CScriptID scriptID = CScriptID(hash);
CScript subscript;
if (keystore.GetCScript(scriptID, subscript)) {
ret = std::max(ret, IsMineInner(keystore, subscript, IsMineSigVersion::WITNESS_V0));
ret = IsMineInner(keystore, subscript, IsMineSigVersion::WITNESS_V0);
}
break;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
{
if (vSolutions[0].size() == 33) {
keyID = CPubKey(vSolutions[0]).GetID();
//UNIT-E: At the moment we do not support SegWit deposit or vote transactions
// UNIT-E: At the moment we do not support deposit or vote transactions nested in P2SH/P2WSH
if (sigversion != IsMineSigVersion::TOP) {
return IsMineResult::INVALID;
}
Expand All @@ -206,10 +206,25 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
}
break;
}

case TX_WITNESS_V1_REMOTE_STAKING:
{
if (sigversion != IsMineSigVersion::TOP) {
return IsMineResult::INVALID;
}

uint160 hash;
CRIPEMD160().Write(&vSolutions[1][0], vSolutions[1].size()).Finalize(hash.begin());
CKeyID spending_key_id = CKeyID(hash);

ret = IsMineInner(keystore, GetScriptForDestination(spending_key_id), IsMineSigVersion::WITNESS_V0);

break;
}
}

if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
ret = std::max(ret, IsMineResult::WATCH_ONLY);
ret = IsMineResult::WATCH_ONLY;
}
return ret;
}
Expand Down Expand Up @@ -237,3 +252,31 @@ isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest)
CScript script = GetScriptForDestination(dest);
return IsMine(keystore, script);
}

bool IsStakeableByMe(const CKeyStore &keystore, const CScript &script_pub_key)
{
std::vector<valtype> solutions;
txnouttype which_type;
Solver(script_pub_key, which_type, solutions);

// UNIT-E TODO: Restrict to witness programs only once #212 is merged (fixes #48)
switch (which_type)
{
case TX_PUBKEYHASH:
case TX_WITNESS_V0_KEYHASH:
case TX_WITNESS_V1_REMOTE_STAKING: {
CKeyID key_id = CKeyID(uint160(solutions[0]));
CPubKey pubkey;
if (keystore.GetPubKey(key_id, pubkey) && !pubkey.IsCompressed()) {
return false;
}

if (keystore.HaveKey(key_id)) {
return true;
}
}
default:
return false;
}
return false;
}
3 changes: 3 additions & 0 deletions src/script/ismine.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ typedef uint8_t isminefilter;
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest);

//! Check if we are able to use an output with given script_pub_key as a stake
bool IsStakeableByMe(const CKeyStore &keystore, const CScript &script_pub_key);

#endif // UNITE_SCRIPT_ISMINE_H
1 change: 1 addition & 0 deletions src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
witnessProgram.program.end());
return true;
}
typeRet = TX_NONSTANDARD;
return false;
}

Expand Down
94 changes: 94 additions & 0 deletions src/test/script_standard_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@

#include <boost/test/unit_test.hpp>

class FakeHWKeyStore : public CBasicKeyStore {
public:
std::set<CKeyID> hw_keys;

bool HaveHardwareKey(const CKeyID &address) const override
{
return hw_keys.count(address) == 1;
}
};

BOOST_FIXTURE_TEST_SUITE(script_standard_tests, ReducedTestingSetup)

Expand Down Expand Up @@ -121,6 +130,13 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_success)
BOOST_CHECK(solutions[0] == ToByteVector(pubkeys[0].GetID()));
BOOST_CHECK(solutions[1] == ToByteVector(pubkeys[1].GetSha256()));

// TX_NONSTANDARD: invalid witness v1 program
s.clear();
s << OP_1 << ToByteVector(pubkeys[0].GetID()) << ToByteVector(pubkeys[1].GetID());
BOOST_CHECK(!Solver(s, whichType, solutions));
BOOST_CHECK_EQUAL(whichType, TX_NONSTANDARD);
BOOST_CHECK_EQUAL(solutions.size(), 0);

// TX_NONSTANDARD
s.clear();
s << OP_9 << OP_ADD << OP_11 << OP_EQUAL;
Expand Down Expand Up @@ -430,6 +446,7 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)
keystore.AddKey(keys[0]);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));
}

// P2PK uncompressed
Expand All @@ -446,6 +463,7 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)
keystore.AddKey(uncompressedKey);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));
}

// P2PKH compressed
Expand All @@ -462,6 +480,7 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)
keystore.AddKey(keys[0]);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(IsStakeableByMe(keystore, scriptPubKey));
}

// P2PKH uncompressed
Expand All @@ -478,6 +497,7 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)
keystore.AddKey(uncompressedKey);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));
}

// P2SH
Expand Down Expand Up @@ -517,6 +537,7 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)
keystore.AddCScript(scriptPubKey);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(IsStakeableByMe(keystore, scriptPubKey));
}

// P2WPKH uncompressed
Expand Down Expand Up @@ -627,6 +648,7 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)
keystore.AddCScript(scriptPubKey);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));
}

// P2WSH multisig with uncompressed key
Expand Down Expand Up @@ -698,6 +720,7 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)
keystore.AddKey(keys[1]);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));
}

// OP_RETURN
Expand All @@ -710,6 +733,7 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)

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

// witness unspendable
Expand All @@ -722,6 +746,76 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)

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

// witness remote staking
{
CBasicKeyStore keystore;
CBasicKeyStore keystore2;

scriptPubKey.clear();
scriptPubKey << OP_1 << ToByteVector(pubkeys[1].GetID()) << ToByteVector(pubkeys[0].GetSha256());

result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));

keystore.AddKey(keys[0]);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));

keystore2.AddKey(keys[1]);
result = IsMine(keystore2, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
BOOST_CHECK(IsStakeableByMe(keystore2, scriptPubKey));

keystore.AddKey(keys[1]);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
BOOST_CHECK(IsStakeableByMe(keystore, scriptPubKey));
}

// witness remote staking with uncompressed public keys
{
CBasicKeyStore keystore;

scriptPubKey.clear();
scriptPubKey << OP_1 << ToByteVector(pubkeys[1].GetID()) << ToByteVector(uncompressedPubkey.GetSha256());

keystore.AddKey(uncompressedKey);
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));

scriptPubKey.clear();
scriptPubKey << OP_1 << ToByteVector(uncompressedPubkey.GetID()) << ToByteVector(pubkeys[0].GetSha256());

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

// witness remote staking with hardware keys
{
FakeHWKeyStore keystore;

scriptPubKey.clear();
scriptPubKey << OP_1 << ToByteVector(pubkeys[0].GetID()) << ToByteVector(pubkeys[1].GetSha256());

// staking key is a hardware key
keystore.hw_keys.insert(pubkeys[0].GetID());
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));

// spending key is a hardware key
keystore.hw_keys.clear();
keystore.hw_keys.insert(pubkeys[1].GetID());
result = IsMine(keystore, scriptPubKey);
BOOST_CHECK_EQUAL(result, ISMINE_HW_DEVICE);
BOOST_CHECK(!IsStakeableByMe(keystore, scriptPubKey));
}

// witness unknown
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ CAmount CWallet::GetChange(const CTxOut& txout) const
bool CWallet::IsMine(const CTransaction& tx) const
{
for (const CTxOut& txout : tx.vout) {
if (IsMine(txout)) {
if (IsMine(txout) || ::IsStakeableByMe(*this, txout.scriptPubKey)) {
return true;
}
}
Expand Down