2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2016-12-31 19:01:21 +01:00
|
|
|
// Copyright (c) 2009-2016 The Bitcoin Core developers
|
2014-09-09 10:00:42 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2016-04-18 15:12:46 +02:00
|
|
|
#include "ismine.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-04-15 22:10:54 +02:00
|
|
|
#include "key.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "keystore.h"
|
2014-10-28 22:47:18 +01:00
|
|
|
#include "script/script.h"
|
2014-08-27 17:22:33 +02:00
|
|
|
#include "script/standard.h"
|
2015-06-10 08:36:36 +02:00
|
|
|
#include "script/sign.h"
|
2012-04-15 22:10:54 +02:00
|
|
|
|
2013-05-05 07:35:51 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2014-08-23 05:09:47 +02:00
|
|
|
|
2017-01-04 06:42:14 +01:00
|
|
|
typedef std::vector<unsigned char> valtype;
|
2013-05-05 07:35:51 +02:00
|
|
|
|
2017-01-04 06:42:14 +01:00
|
|
|
unsigned int HaveKeys(const std::vector<valtype>& pubkeys, const CKeyStore& keystore)
|
2011-10-03 19:05:43 +02:00
|
|
|
{
|
2012-04-23 20:14:03 +02:00
|
|
|
unsigned int nResult = 0;
|
2011-10-03 19:05:43 +02:00
|
|
|
BOOST_FOREACH(const valtype& pubkey, pubkeys)
|
|
|
|
{
|
2012-05-14 23:44:52 +02:00
|
|
|
CKeyID keyID = CPubKey(pubkey).GetID();
|
|
|
|
if (keystore.HaveKey(keyID))
|
2011-10-03 19:05:43 +02:00
|
|
|
++nResult;
|
|
|
|
}
|
|
|
|
return nResult;
|
|
|
|
}
|
|
|
|
|
2016-10-16 18:32:36 +02:00
|
|
|
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)
|
2012-05-14 23:44:52 +02:00
|
|
|
{
|
2014-09-11 19:15:29 +02:00
|
|
|
CScript script = GetScriptForDestination(dest);
|
2016-10-16 18:32:36 +02:00
|
|
|
return IsMine(keystore, script, isInvalid, sigversion);
|
2012-05-14 23:44:52 +02:00
|
|
|
}
|
|
|
|
|
2016-10-16 18:32:36 +02:00
|
|
|
isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey, bool& isInvalid, SigVersion sigversion)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
2017-01-04 06:42:14 +01:00
|
|
|
std::vector<valtype> vSolutions;
|
2011-11-08 19:20:29 +01:00
|
|
|
txnouttype whichType;
|
2013-07-26 01:06:01 +02:00
|
|
|
if (!Solver(scriptPubKey, whichType, vSolutions)) {
|
2014-06-09 21:11:59 +02:00
|
|
|
if (keystore.HaveWatchOnly(scriptPubKey))
|
2015-08-08 18:26:07 +02:00
|
|
|
return ISMINE_WATCH_UNSOLVABLE;
|
2014-07-01 11:00:22 +02:00
|
|
|
return ISMINE_NO;
|
2013-07-26 01:06:01 +02:00
|
|
|
}
|
Add wallet privkey encryption.
This commit adds support for ckeys, or enCrypted private keys, to the wallet.
All keys are stored in memory in their encrypted form and thus the passphrase
is required from the user to spend coins, or to create new addresses.
Keys are encrypted with AES-256-CBC using OpenSSL's EVP library. The key is
calculated via EVP_BytesToKey using SHA512 with (by default) 25000 rounds and
a random salt.
By default, the user's wallet remains unencrypted until they call the RPC
command encryptwallet <passphrase> or, from the GUI menu, Options->
Encrypt Wallet.
When the user is attempting to call RPC functions which require the password
to unlock the wallet, an error will be returned unless they call
walletpassphrase <passphrase> <time to keep key in memory> first.
A keypoolrefill command has been added which tops up the users keypool
(requiring the passphrase via walletpassphrase first).
keypoolsize has been added to the output of getinfo to show the user the
number of keys left before they need to specify their passphrase (and call
keypoolrefill).
Note that walletpassphrase will automatically fill keypool in a separate
thread which it spawns when the passphrase is set. This could cause some
delays in other threads waiting for locks on the wallet passphrase, including
one which could cause the passphrase to be stored longer than expected,
however it will not allow the passphrase to be used longer than expected as
ThreadCleanWalletPassphrase will attempt to get a lock on the key as soon
as the specified lock time has arrived.
When the keypool runs out (and wallet is locked) GetOrReuseKeyFromPool
returns vchDefaultKey, meaning miners may start to generate many blocks to
vchDefaultKey instead of a new key each time.
A walletpassphrasechange <oldpassphrase> <newpassphrase> has been added to
allow the user to change their password via RPC.
Whenever keying material (unencrypted private keys, the user's passphrase,
the wallet's AES key) is stored unencrypted in memory, any reasonable attempt
is made to mlock/VirtualLock that memory before storing the keying material.
This is not true in several (commented) cases where mlock/VirtualLocking the
memory is not possible.
Although encryption of private keys in memory can be very useful on desktop
systems (as some small amount of protection against stupid viruses), on an
RPC server, the password is entered fairly insecurely. Thus, the only main
advantage encryption has for RPC servers is for RPC servers that do not spend
coins, except in rare cases, eg. a webserver of a merchant which only receives
payment except for cases of manual intervention.
Thanks to jgarzik for the original patch and sipa, gmaxwell and many others
for all their input.
Conflicts:
src/wallet.cpp
2011-07-08 15:47:35 +02:00
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
CKeyID keyID;
|
2011-10-03 19:05:43 +02:00
|
|
|
switch (whichType)
|
Add wallet privkey encryption.
This commit adds support for ckeys, or enCrypted private keys, to the wallet.
All keys are stored in memory in their encrypted form and thus the passphrase
is required from the user to spend coins, or to create new addresses.
Keys are encrypted with AES-256-CBC using OpenSSL's EVP library. The key is
calculated via EVP_BytesToKey using SHA512 with (by default) 25000 rounds and
a random salt.
By default, the user's wallet remains unencrypted until they call the RPC
command encryptwallet <passphrase> or, from the GUI menu, Options->
Encrypt Wallet.
When the user is attempting to call RPC functions which require the password
to unlock the wallet, an error will be returned unless they call
walletpassphrase <passphrase> <time to keep key in memory> first.
A keypoolrefill command has been added which tops up the users keypool
(requiring the passphrase via walletpassphrase first).
keypoolsize has been added to the output of getinfo to show the user the
number of keys left before they need to specify their passphrase (and call
keypoolrefill).
Note that walletpassphrase will automatically fill keypool in a separate
thread which it spawns when the passphrase is set. This could cause some
delays in other threads waiting for locks on the wallet passphrase, including
one which could cause the passphrase to be stored longer than expected,
however it will not allow the passphrase to be used longer than expected as
ThreadCleanWalletPassphrase will attempt to get a lock on the key as soon
as the specified lock time has arrived.
When the keypool runs out (and wallet is locked) GetOrReuseKeyFromPool
returns vchDefaultKey, meaning miners may start to generate many blocks to
vchDefaultKey instead of a new key each time.
A walletpassphrasechange <oldpassphrase> <newpassphrase> has been added to
allow the user to change their password via RPC.
Whenever keying material (unencrypted private keys, the user's passphrase,
the wallet's AES key) is stored unencrypted in memory, any reasonable attempt
is made to mlock/VirtualLock that memory before storing the keying material.
This is not true in several (commented) cases where mlock/VirtualLocking the
memory is not possible.
Although encryption of private keys in memory can be very useful on desktop
systems (as some small amount of protection against stupid viruses), on an
RPC server, the password is entered fairly insecurely. Thus, the only main
advantage encryption has for RPC servers is for RPC servers that do not spend
coins, except in rare cases, eg. a webserver of a merchant which only receives
payment except for cases of manual intervention.
Thanks to jgarzik for the original patch and sipa, gmaxwell and many others
for all their input.
Conflicts:
src/wallet.cpp
2011-07-08 15:47:35 +02:00
|
|
|
{
|
2011-10-03 19:05:43 +02:00
|
|
|
case TX_NONSTANDARD:
|
2013-06-24 21:09:50 +02:00
|
|
|
case TX_NULL_DATA:
|
2013-07-26 01:06:01 +02:00
|
|
|
break;
|
2011-10-03 19:05:43 +02:00
|
|
|
case TX_PUBKEY:
|
2012-05-14 23:44:52 +02:00
|
|
|
keyID = CPubKey(vSolutions[0]).GetID();
|
2016-10-16 18:32:36 +02:00
|
|
|
if (sigversion != SIGVERSION_BASE && vSolutions[0].size() != 33) {
|
|
|
|
isInvalid = true;
|
|
|
|
return ISMINE_NO;
|
|
|
|
}
|
2013-07-26 01:06:01 +02:00
|
|
|
if (keystore.HaveKey(keyID))
|
2014-07-01 11:00:22 +02:00
|
|
|
return ISMINE_SPENDABLE;
|
2013-07-26 01:06:01 +02:00
|
|
|
break;
|
2016-03-31 14:54:58 +02:00
|
|
|
case TX_WITNESS_V0_KEYHASH:
|
2016-10-16 18:32:36 +02:00
|
|
|
{
|
|
|
|
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:
|
2012-05-14 23:44:52 +02:00
|
|
|
keyID = CKeyID(uint160(vSolutions[0]));
|
2016-10-16 18:32:36 +02:00
|
|
|
if (sigversion != SIGVERSION_BASE) {
|
|
|
|
CPubKey pubkey;
|
|
|
|
if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) {
|
|
|
|
isInvalid = true;
|
|
|
|
return ISMINE_NO;
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 01:06:01 +02:00
|
|
|
if (keystore.HaveKey(keyID))
|
2014-07-01 11:00:22 +02:00
|
|
|
return ISMINE_SPENDABLE;
|
2013-07-26 01:06:01 +02:00
|
|
|
break;
|
2011-10-03 19:05:43 +02:00
|
|
|
case TX_SCRIPTHASH:
|
|
|
|
{
|
2013-07-26 01:06:01 +02:00
|
|
|
CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
|
2011-11-08 19:20:29 +01:00
|
|
|
CScript subscript;
|
2013-07-26 01:06:01 +02:00
|
|
|
if (keystore.GetCScript(scriptID, subscript)) {
|
2016-10-16 18:32:36 +02:00
|
|
|
isminetype ret = IsMine(keystore, subscript, isInvalid);
|
|
|
|
if (ret == ISMINE_SPENDABLE || ret == ISMINE_WATCH_SOLVABLE || (ret == ISMINE_NO && isInvalid))
|
2013-07-26 01:06:01 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
Add wallet privkey encryption.
This commit adds support for ckeys, or enCrypted private keys, to the wallet.
All keys are stored in memory in their encrypted form and thus the passphrase
is required from the user to spend coins, or to create new addresses.
Keys are encrypted with AES-256-CBC using OpenSSL's EVP library. The key is
calculated via EVP_BytesToKey using SHA512 with (by default) 25000 rounds and
a random salt.
By default, the user's wallet remains unencrypted until they call the RPC
command encryptwallet <passphrase> or, from the GUI menu, Options->
Encrypt Wallet.
When the user is attempting to call RPC functions which require the password
to unlock the wallet, an error will be returned unless they call
walletpassphrase <passphrase> <time to keep key in memory> first.
A keypoolrefill command has been added which tops up the users keypool
(requiring the passphrase via walletpassphrase first).
keypoolsize has been added to the output of getinfo to show the user the
number of keys left before they need to specify their passphrase (and call
keypoolrefill).
Note that walletpassphrase will automatically fill keypool in a separate
thread which it spawns when the passphrase is set. This could cause some
delays in other threads waiting for locks on the wallet passphrase, including
one which could cause the passphrase to be stored longer than expected,
however it will not allow the passphrase to be used longer than expected as
ThreadCleanWalletPassphrase will attempt to get a lock on the key as soon
as the specified lock time has arrived.
When the keypool runs out (and wallet is locked) GetOrReuseKeyFromPool
returns vchDefaultKey, meaning miners may start to generate many blocks to
vchDefaultKey instead of a new key each time.
A walletpassphrasechange <oldpassphrase> <newpassphrase> has been added to
allow the user to change their password via RPC.
Whenever keying material (unencrypted private keys, the user's passphrase,
the wallet's AES key) is stored unencrypted in memory, any reasonable attempt
is made to mlock/VirtualLock that memory before storing the keying material.
This is not true in several (commented) cases where mlock/VirtualLocking the
memory is not possible.
Although encryption of private keys in memory can be very useful on desktop
systems (as some small amount of protection against stupid viruses), on an
RPC server, the password is entered fairly insecurely. Thus, the only main
advantage encryption has for RPC servers is for RPC servers that do not spend
coins, except in rare cases, eg. a webserver of a merchant which only receives
payment except for cases of manual intervention.
Thanks to jgarzik for the original patch and sipa, gmaxwell and many others
for all their input.
Conflicts:
src/wallet.cpp
2011-07-08 15:47:35 +02:00
|
|
|
}
|
2016-03-31 14:54:58 +02:00
|
|
|
case TX_WITNESS_V0_SCRIPTHASH:
|
|
|
|
{
|
2016-10-16 18:32:36 +02:00
|
|
|
if (!keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
|
|
|
|
break;
|
|
|
|
}
|
2016-03-31 14:54:58 +02:00
|
|
|
uint160 hash;
|
|
|
|
CRIPEMD160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(hash.begin());
|
|
|
|
CScriptID scriptID = CScriptID(hash);
|
|
|
|
CScript subscript;
|
|
|
|
if (keystore.GetCScript(scriptID, subscript)) {
|
2016-10-16 18:32:36 +02:00
|
|
|
isminetype ret = IsMine(keystore, subscript, isInvalid, SIGVERSION_WITNESS_V0);
|
|
|
|
if (ret == ISMINE_SPENDABLE || ret == ISMINE_WATCH_SOLVABLE || (ret == ISMINE_NO && isInvalid))
|
2016-03-31 14:54:58 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-03 19:05:43 +02:00
|
|
|
case TX_MULTISIG:
|
|
|
|
{
|
|
|
|
// Only consider transactions "mine" if we own ALL the
|
2014-11-25 18:54:36 +01:00
|
|
|
// keys involved. Multi-signature transactions that are
|
2011-10-03 19:05:43 +02:00
|
|
|
// partially owned (somebody else has a key that can spend
|
|
|
|
// them) enable spend-out-from-under-you attacks, especially
|
|
|
|
// in shared-wallet situations.
|
2017-01-04 06:42:14 +01:00
|
|
|
std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
|
2016-10-16 18:32:36 +02:00
|
|
|
if (sigversion != SIGVERSION_BASE) {
|
|
|
|
for (size_t i = 0; i < keys.size(); i++) {
|
|
|
|
if (keys[i].size() != 33) {
|
|
|
|
isInvalid = true;
|
|
|
|
return ISMINE_NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 01:06:01 +02:00
|
|
|
if (HaveKeys(keys, keystore) == keys.size())
|
2014-07-01 11:00:22 +02:00
|
|
|
return ISMINE_SPENDABLE;
|
2013-07-26 01:06:01 +02:00
|
|
|
break;
|
2011-10-03 19:05:43 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 01:06:01 +02:00
|
|
|
|
2015-06-10 08:36:36 +02:00
|
|
|
if (keystore.HaveWatchOnly(scriptPubKey)) {
|
|
|
|
// TODO: This could be optimized some by doing some work after the above solver
|
2016-03-31 14:54:58 +02:00
|
|
|
SignatureData sigs;
|
|
|
|
return ProduceSignature(DummySignatureCreator(&keystore), scriptPubKey, sigs) ? ISMINE_WATCH_SOLVABLE : ISMINE_WATCH_UNSOLVABLE;
|
2015-06-10 08:36:36 +02:00
|
|
|
}
|
2014-07-01 11:00:22 +02:00
|
|
|
return ISMINE_NO;
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|