Merge #13002: Do not treat bare multisig outputs as IsMine unless watched

7d0f80b Use anonymous namespace instead of static functions (Pieter Wuille)
b61fb71 Mention removal of bare multisig IsMine in release notes (Pieter Wuille)
9c2a8b8 Do not treat bare multisig as IsMine (Pieter Wuille)
08f3228 Optimization: only test for witness scripts at top level (Pieter Wuille)
3619735 Track difference between scriptPubKey and P2SH execution in IsMine (Pieter Wuille)
ac6ec62 Switch to a private version of SigVersion inside IsMine (Pieter Wuille)
19fc973 Do not expose SigVersion argument to IsMine (Pieter Wuille)
fb1dfbb Remove unused IsMine overload (Pieter Wuille)
952d821 Make CScript -> CScriptID conversion explicit (Pieter Wuille)

Pull request description:

  Currently our wallet code will treat bare multisig outputs (meaning scriptPubKeys with multiple public keys + `OP_CHECKMULTISIG` operator in it) as ours without the user asking for it, as long as all private keys in it are in our wallet.

  This is a pointless feature. As it only works when all private keys are in one place, it's useless compared to single key outputs (P2PK, P2PKH, P2WPKH, P2SH-P2WPKH), and worse in terms of space, cost, UTXO size, and ability to test (due to lack of address format for them).

  Furthermore, they are problematic in that producing a list of all `scriptPubKeys` we accept is not tractable (it involves all combinations of all public keys that are ours). In further wallet changes I'd like to move to a model where all scriptPubKeys that are treated as ours are explicit, rather than defined by whatever keys we have. The current behavior of the wallet is very hard to model in such a design, so I'd like to get rid of it.

  I think there are two options:
  * Remove it entirely (do not ever accept bare multisig outputs as ours, unless watched)
  * Only accept bare multisig outputs in situations where the P2SH version of that output would also be acceptable

  This PR implements the first option. The second option was explored in #12874.

Tree-SHA512: 917ed45b3cac864cee53e27f9a3e900390c576277fbd6751b1250becea04d692b3b426fa09065a3399931013bd579c4f3dbeeb29d51d19ed0c64da75d430ad9a
This commit is contained in:
Wladimir J. van der Laan 2018-04-26 19:48:50 +02:00 committed by UdjinM6
parent 4ae641ca18
commit 370ec04a02
No known key found for this signature in database
GPG Key ID: 83592BD1400D58D9
5 changed files with 68 additions and 37 deletions

View File

@ -13,7 +13,26 @@
typedef std::vector<unsigned char> valtype; typedef std::vector<unsigned char> valtype;
static bool HaveKeys(const std::vector<valtype>& pubkeys, const CKeyStore& keystore) namespace {
/**
* This is an enum that tracks the execution context of a script, similar to
* SigVersion in script/interpreter. It is separate however because we want to
* distinguish between top-level scriptPubKey execution and P2SH redeemScript
* execution (a distinction that has no impact on consensus rules).
*/
enum class IsMineSigVersion
{
TOP = 0, //! scriptPubKey execution
P2SH = 1, //! P2SH redeemScript
};
bool PermitsUncompressed(IsMineSigVersion sigversion)
{
return sigversion == IsMineSigVersion::TOP || sigversion == IsMineSigVersion::P2SH;
}
bool HaveKeys(const std::vector<valtype>& pubkeys, const CKeyStore& keystore)
{ {
for (const valtype& pubkey : pubkeys) { for (const valtype& pubkey : pubkeys) {
CKeyID keyID = CPubKey(pubkey).GetID(); CKeyID keyID = CPubKey(pubkey).GetID();
@ -22,25 +41,7 @@ static bool HaveKeys(const std::vector<valtype>& pubkeys, const CKeyStore& keyst
return true; return true;
} }
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey, SigVersion sigversion) isminetype IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey, bool& isInvalid, IsMineSigVersion sigversion)
{
bool isInvalid = false;
return IsMine(keystore, scriptPubKey, isInvalid, sigversion);
}
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest, SigVersion sigversion)
{
bool isInvalid = false;
return IsMine(keystore, dest, isInvalid, sigversion);
}
isminetype IsMine(const CKeyStore &keystore, const CTxDestination& dest, bool& isInvalid, SigVersion sigversion)
{
CScript script = GetScriptForDestination(dest);
return IsMine(keystore, script, isInvalid, sigversion);
}
isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey, bool& isInvalid, SigVersion sigversion)
{ {
std::vector<valtype> vSolutions; std::vector<valtype> vSolutions;
txnouttype whichType; txnouttype whichType;
@ -58,7 +59,7 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey, bool&
break; break;
case TX_PUBKEY: case TX_PUBKEY:
keyID = CPubKey(vSolutions[0]).GetID(); keyID = CPubKey(vSolutions[0]).GetID();
if (sigversion != SigVersion::BASE && vSolutions[0].size() != 33) { if (!PermitsUncompressed(sigversion) && vSolutions[0].size() != 33) {
isInvalid = true; isInvalid = true;
return ISMINE_NO; return ISMINE_NO;
} }
@ -67,7 +68,7 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey, bool&
break; break;
case TX_PUBKEYHASH: case TX_PUBKEYHASH:
keyID = CKeyID(uint160(vSolutions[0])); keyID = CKeyID(uint160(vSolutions[0]));
if (sigversion != SigVersion::BASE) { if (!PermitsUncompressed(sigversion)) {
CPubKey pubkey; CPubKey pubkey;
if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) { if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) {
isInvalid = true; isInvalid = true;
@ -82,7 +83,7 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey, bool&
CScriptID scriptID = CScriptID(uint160(vSolutions[0])); CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
CScript subscript; CScript subscript;
if (keystore.GetCScript(scriptID, subscript)) { if (keystore.GetCScript(scriptID, subscript)) {
isminetype ret = IsMine(keystore, subscript, isInvalid); isminetype ret = IsMineInner(keystore, subscript, isInvalid, IsMineSigVersion::P2SH);
if (ret == ISMINE_SPENDABLE || ret == ISMINE_WATCH_SOLVABLE || (ret == ISMINE_NO && isInvalid)) if (ret == ISMINE_SPENDABLE || ret == ISMINE_WATCH_SOLVABLE || (ret == ISMINE_NO && isInvalid))
return ret; return ret;
} }
@ -90,13 +91,16 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey, bool&
} }
case TX_MULTISIG: case TX_MULTISIG:
{ {
// Never treat bare multisig outputs as ours (they can still be made watchonly-though)
if (sigversion == IsMineSigVersion::TOP) break;
// Only consider transactions "mine" if we own ALL the // Only consider transactions "mine" if we own ALL the
// keys involved. Multi-signature transactions that are // keys involved. Multi-signature transactions that are
// partially owned (somebody else has a key that can spend // partially owned (somebody else has a key that can spend
// them) enable spend-out-from-under-you attacks, especially // them) enable spend-out-from-under-you attacks, especially
// in shared-wallet situations. // in shared-wallet situations.
std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1); std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
if (sigversion != SigVersion::BASE) { if (!PermitsUncompressed(sigversion)) {
for (size_t i = 0; i < keys.size(); i++) { for (size_t i = 0; i < keys.size(); i++) {
if (keys[i].size() != 33) { if (keys[i].size() != 33) {
isInvalid = true; isInvalid = true;
@ -117,3 +121,22 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey, bool&
} }
return ISMINE_NO; return ISMINE_NO;
} }
} // namespace
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey, bool& isInvalid)
{
return IsMineInner(keystore, scriptPubKey, isInvalid, IsMineSigVersion::TOP);
}
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey)
{
bool isInvalid = false;
return IsMine(keystore, scriptPubKey, isInvalid);
}
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest)
{
CScript script = GetScriptForDestination(dest);
return IsMine(keystore, script);
}

View File

@ -33,9 +33,8 @@ typedef uint8_t isminefilter;
* different SIGVERSION may have different network rules. Currently there is no use of isInvalid but it could be * different SIGVERSION may have different network rules. Currently there is no use of isInvalid but it could be
* used in the future. See https://github.com/bitcoin/bitcoin/pull/8499 (segwit policy limits) as an example. * used in the future. See https://github.com/bitcoin/bitcoin/pull/8499 (segwit policy limits) as an example.
*/ */
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey, bool& isInvalid, SigVersion = SigVersion::BASE); isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey, bool& isInvalid);
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey, SigVersion = SigVersion::BASE); isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest, bool& isInvalid, SigVersion = SigVersion::BASE); isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest);
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest, SigVersion = SigVersion::BASE);
#endif // BITCOIN_SCRIPT_ISMINE_H #endif // BITCOIN_SCRIPT_ISMINE_H

View File

@ -23,7 +23,7 @@ class CScriptID : public uint160
{ {
public: public:
CScriptID() : uint160() {} CScriptID() : uint160() {}
CScriptID(const CScript& in); explicit CScriptID(const CScript& in);
CScriptID(const uint160& in) : uint160(in) {} CScriptID(const uint160& in) : uint160(in) {}
}; };

View File

@ -448,7 +448,14 @@ BOOST_AUTO_TEST_CASE(script_standard_IsMine)
keystore.AddKey(keys[1]); keystore.AddKey(keys[1]);
result = IsMine(keystore, scriptPubKey, isInvalid); result = IsMine(keystore, scriptPubKey, isInvalid);
BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); BOOST_CHECK_EQUAL(result, ISMINE_NO);
BOOST_CHECK(!isInvalid);
// Keystore has 2/2 keys and the script
keystore.AddCScript(scriptPubKey);
result = IsMine(keystore, scriptPubKey, isInvalid);
BOOST_CHECK_EQUAL(result, ISMINE_NO);
BOOST_CHECK(!isInvalid); BOOST_CHECK(!isInvalid);
} }

View File

@ -198,10 +198,11 @@ static void ImportScript(CWallet * const pwallet, const CScript& script, const s
} }
if (isRedeemScript) { if (isRedeemScript) {
if (!pwallet->HaveCScript(script) && !pwallet->AddCScript(script)) { const CScriptID id(script);
if (!pwallet->HaveCScript(id) && !pwallet->AddCScript(script)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding p2sh redeemScript to wallet"); throw JSONRPCError(RPC_WALLET_ERROR, "Error adding p2sh redeemScript to wallet");
} }
ImportAddress(pwallet, CScriptID(script), strLabel); ImportAddress(pwallet, id, strLabel);
} else { } else {
CTxDestination destination; CTxDestination destination;
if (ExtractDestination(script, destination)) { if (ExtractDestination(script, destination)) {
@ -578,7 +579,8 @@ UniValue importwallet(const JSONRPCRequest& request)
} else if(IsHex(vstr[0])) { } else if(IsHex(vstr[0])) {
std::vector<unsigned char> vData(ParseHex(vstr[0])); std::vector<unsigned char> vData(ParseHex(vstr[0]));
CScript script = CScript(vData.begin(), vData.end()); CScript script = CScript(vData.begin(), vData.end());
if (pwallet->HaveCScript(script)) { CScriptID id(script);
if (pwallet->HaveCScript(id)) {
LogPrintf("Skipping import of %s (script already present)\n", vstr[0]); LogPrintf("Skipping import of %s (script already present)\n", vstr[0]);
continue; continue;
} }
@ -589,7 +591,7 @@ UniValue importwallet(const JSONRPCRequest& request)
} }
int64_t birth_time = DecodeDumpTime(vstr[1]); int64_t birth_time = DecodeDumpTime(vstr[1]);
if (birth_time > 0) { if (birth_time > 0) {
pwallet->m_script_metadata[CScriptID(script)].nCreateTime = birth_time; pwallet->m_script_metadata[id].nCreateTime = birth_time;
nTimeBegin = std::min(nTimeBegin, birth_time); nTimeBegin = std::min(nTimeBegin, birth_time);
} }
} }
@ -1093,12 +1095,12 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet"); throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
} }
if (!pwallet->HaveCScript(redeemScript) && !pwallet->AddCScript(redeemScript)) { CScriptID redeem_id(redeemScript);
if (!pwallet->HaveCScript(redeem_id) && !pwallet->AddCScript(redeemScript)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding p2sh redeemScript to wallet"); throw JSONRPCError(RPC_WALLET_ERROR, "Error adding p2sh redeemScript to wallet");
} }
CTxDestination redeem_dest = CScriptID(redeemScript); CScript redeemDestination = GetScriptForDestination(redeem_id);
CScript redeemDestination = GetScriptForDestination(redeem_dest);
if (::IsMine(*pwallet, redeemDestination) == ISMINE_SPENDABLE) { if (::IsMine(*pwallet, redeemDestination) == ISMINE_SPENDABLE) {
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script"); throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");