Browse Source

Fix ismine and addwitnessaddress: no uncompressed keys in segwit

0.14
Pieter Wuille 8 years ago committed by Johnson Lau
parent
commit
248f3a76a8
  1. 62
      src/script/ismine.cpp
  2. 11
      src/script/ismine.h
  3. 17
      src/wallet/rpcwallet.cpp

62
src/script/ismine.cpp

@ -29,13 +29,25 @@ unsigned int HaveKeys(const vector<valtype>& pubkeys, const CKeyStore& keystore)
return nResult; return nResult;
} }
isminetype IsMine(const CKeyStore &keystore, const CTxDestination& dest) isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey, SigVersion 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); CScript script = GetScriptForDestination(dest);
return IsMine(keystore, script); return IsMine(keystore, script, isInvalid, sigversion);
} }
isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey, bool& isInvalid, SigVersion sigversion)
{ {
vector<valtype> vSolutions; vector<valtype> vSolutions;
txnouttype whichType; txnouttype whichType;
@ -53,12 +65,35 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
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) {
isInvalid = true;
return ISMINE_NO;
}
if (keystore.HaveKey(keyID)) if (keystore.HaveKey(keyID))
return ISMINE_SPENDABLE; return ISMINE_SPENDABLE;
break; break;
case TX_PUBKEYHASH:
case TX_WITNESS_V0_KEYHASH: case TX_WITNESS_V0_KEYHASH:
{
if (!keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
// We do not support bare witness outputs unless the P2SH version of it would be
// acceptable as well. This protects against matching before segwit activates.
// This also applies to the P2WSH case.
break;
}
isminetype ret = ::IsMine(keystore, GetScriptForDestination(CKeyID(uint160(vSolutions[0]))), isInvalid, SIGVERSION_WITNESS_V0);
if (ret == ISMINE_SPENDABLE || ret == ISMINE_WATCH_SOLVABLE || (ret == ISMINE_NO && isInvalid))
return ret;
break;
}
case TX_PUBKEYHASH:
keyID = CKeyID(uint160(vSolutions[0])); keyID = CKeyID(uint160(vSolutions[0]));
if (sigversion != SIGVERSION_BASE) {
CPubKey pubkey;
if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) {
isInvalid = true;
return ISMINE_NO;
}
}
if (keystore.HaveKey(keyID)) if (keystore.HaveKey(keyID))
return ISMINE_SPENDABLE; return ISMINE_SPENDABLE;
break; break;
@ -67,21 +102,24 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
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); isminetype ret = IsMine(keystore, subscript, isInvalid);
if (ret == ISMINE_SPENDABLE) if (ret == ISMINE_SPENDABLE || ret == ISMINE_WATCH_SOLVABLE || (ret == ISMINE_NO && isInvalid))
return ret; return ret;
} }
break; break;
} }
case TX_WITNESS_V0_SCRIPTHASH: case TX_WITNESS_V0_SCRIPTHASH:
{ {
if (!keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
break;
}
uint160 hash; uint160 hash;
CRIPEMD160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(hash.begin()); CRIPEMD160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(hash.begin());
CScriptID scriptID = CScriptID(hash); CScriptID scriptID = CScriptID(hash);
CScript subscript; CScript subscript;
if (keystore.GetCScript(scriptID, subscript)) { if (keystore.GetCScript(scriptID, subscript)) {
isminetype ret = IsMine(keystore, subscript); isminetype ret = IsMine(keystore, subscript, isInvalid, SIGVERSION_WITNESS_V0);
if (ret == ISMINE_SPENDABLE) if (ret == ISMINE_SPENDABLE || ret == ISMINE_WATCH_SOLVABLE || (ret == ISMINE_NO && isInvalid))
return ret; return ret;
} }
break; break;
@ -95,6 +133,14 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
// 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.
vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1); vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
if (sigversion != SIGVERSION_BASE) {
for (size_t i = 0; i < keys.size(); i++) {
if (keys[i].size() != 33) {
isInvalid = true;
return ISMINE_NO;
}
}
}
if (HaveKeys(keys, keystore) == keys.size()) if (HaveKeys(keys, keystore) == keys.size())
return ISMINE_SPENDABLE; return ISMINE_SPENDABLE;
break; break;

11
src/script/ismine.h

@ -28,7 +28,14 @@ enum isminetype
/** used for bitflags of isminetype */ /** used for bitflags of isminetype */
typedef uint8_t isminefilter; typedef uint8_t isminefilter;
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); /* isInvalid becomes true when the script is found invalid by consensus or policy. This will terminate the recursion
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest); * and return a ISMINE_NO immediately, as an invalid script should never be considered as "mine". This is needed as
* different SIGVERSION may have different network rules. Currently the only use of isInvalid is indicate uncompressed
* keys in SIGVERSION_WITNESS_V0 script, but could also be used in similar cases in the future
*/
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey, bool& isInvalid, SigVersion = SIGVERSION_BASE);
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey, SigVersion = SIGVERSION_BASE);
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest, bool& isInvalid, SigVersion = SIGVERSION_BASE);
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest, SigVersion = SIGVERSION_BASE);
#endif // BITCOIN_SCRIPT_ISMINE_H #endif // BITCOIN_SCRIPT_ISMINE_H

17
src/wallet/rpcwallet.cpp

@ -1,5 +1,5 @@
// Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers // Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -1025,9 +1025,12 @@ public:
bool operator()(const CKeyID &keyID) { bool operator()(const CKeyID &keyID) {
CPubKey pubkey; CPubKey pubkey;
if (pwalletMain && pwalletMain->GetPubKey(keyID, pubkey)) { if (pwalletMain) {
CScript basescript; CScript basescript = GetScriptForDestination(keyID);
basescript << ToByteVector(pubkey) << OP_CHECKSIG; isminetype typ;
typ = IsMine(*pwalletMain, basescript, SIGVERSION_WITNESS_V0);
if (typ != ISMINE_SPENDABLE && typ != ISMINE_WATCH_SOLVABLE)
return false;
CScript witscript = GetScriptForWitness(basescript); CScript witscript = GetScriptForWitness(basescript);
pwalletMain->AddCScript(witscript); pwalletMain->AddCScript(witscript);
result = CScriptID(witscript); result = CScriptID(witscript);
@ -1045,6 +1048,10 @@ public:
result = scriptID; result = scriptID;
return true; return true;
} }
isminetype typ;
typ = IsMine(*pwalletMain, subscript, SIGVERSION_WITNESS_V0);
if (typ != ISMINE_SPENDABLE && typ != ISMINE_WATCH_SOLVABLE)
return false;
CScript witscript = GetScriptForWitness(subscript); CScript witscript = GetScriptForWitness(subscript);
pwalletMain->AddCScript(witscript); pwalletMain->AddCScript(witscript);
result = CScriptID(witscript); result = CScriptID(witscript);
@ -1090,7 +1097,7 @@ UniValue addwitnessaddress(const UniValue& params, bool fHelp)
CTxDestination dest = address.Get(); CTxDestination dest = address.Get();
bool ret = boost::apply_visitor(w, dest); bool ret = boost::apply_visitor(w, dest);
if (!ret) { if (!ret) {
throw JSONRPCError(RPC_WALLET_ERROR, "Public key or redeemscript not known to wallet"); throw JSONRPCError(RPC_WALLET_ERROR, "Public key or redeemscript not known to wallet, or the key is uncompressed");
} }
pwalletMain->SetAddressBook(w.result, "", "receive"); pwalletMain->SetAddressBook(w.result, "", "receive");

Loading…
Cancel
Save