2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2013-10-20 21:25:06 +02:00
|
|
|
// Copyright (c) 2009-2013 The Bitcoin developers
|
2010-08-29 18:58:15 +02:00
|
|
|
// Distributed under the MIT/X11 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
|
|
|
|
2014-08-14 13:54:05 +02:00
|
|
|
#include "scriptutils.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include "core.h"
|
2012-04-15 22:10:54 +02:00
|
|
|
#include "key.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "keystore.h"
|
|
|
|
#include "uint256.h"
|
2012-05-22 19:56:14 +02:00
|
|
|
#include "util.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
|
|
|
|
2013-05-05 07:35:51 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2010-08-29 18:58:15 +02:00
|
|
|
typedef vector<unsigned char> valtype;
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
bool Sign1(const CKeyID& address, const CKeyStore& keystore, uint256 hash, int nHashType, CScript& scriptSigRet)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
2011-10-03 19:05:43 +02:00
|
|
|
CKey key;
|
|
|
|
if (!keystore.GetKey(address, key))
|
|
|
|
return false;
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-10-03 19:05:43 +02:00
|
|
|
vector<unsigned char> vchSig;
|
|
|
|
if (!key.Sign(hash, vchSig))
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2011-10-03 19:05:43 +02:00
|
|
|
vchSig.push_back((unsigned char)nHashType);
|
|
|
|
scriptSigRet << vchSig;
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-10-03 19:05:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-28 18:30:06 +02:00
|
|
|
|
2011-10-03 19:05:43 +02:00
|
|
|
bool SignN(const vector<valtype>& multisigdata, const CKeyStore& keystore, uint256 hash, int nHashType, CScript& scriptSigRet)
|
|
|
|
{
|
|
|
|
int nSigned = 0;
|
|
|
|
int nRequired = multisigdata.front()[0];
|
2012-07-05 19:25:52 +02:00
|
|
|
for (unsigned int i = 1; i < multisigdata.size()-1 && nSigned < nRequired; i++)
|
2011-10-03 19:05:43 +02:00
|
|
|
{
|
2012-05-31 22:01:16 +02:00
|
|
|
const valtype& pubkey = multisigdata[i];
|
2012-05-14 23:44:52 +02:00
|
|
|
CKeyID keyID = CPubKey(pubkey).GetID();
|
|
|
|
if (Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
|
2011-10-03 19:05:43 +02:00
|
|
|
++nSigned;
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
2011-10-03 19:05:43 +02:00
|
|
|
return nSigned==nRequired;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Sign scriptPubKey with private keys stored in keystore, given transaction hash and hash type.
|
2012-01-05 03:40:52 +01:00
|
|
|
// Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed),
|
|
|
|
// unless whichTypeRet is TX_SCRIPTHASH, in which case scriptSigRet is the redemption script.
|
2012-07-26 02:48:39 +02:00
|
|
|
// Returns false if scriptPubKey could not be completely satisfied.
|
2011-10-03 19:05:43 +02:00
|
|
|
//
|
2012-01-05 03:40:52 +01:00
|
|
|
bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash, int nHashType,
|
|
|
|
CScript& scriptSigRet, txnouttype& whichTypeRet)
|
2011-10-03 19:05:43 +02:00
|
|
|
{
|
|
|
|
scriptSigRet.clear();
|
|
|
|
|
|
|
|
vector<valtype> vSolutions;
|
2012-01-05 03:40:52 +01:00
|
|
|
if (!Solver(scriptPubKey, whichTypeRet, vSolutions))
|
2011-09-28 18:30:06 +02:00
|
|
|
return false;
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
CKeyID keyID;
|
2012-01-05 03:40:52 +01:00
|
|
|
switch (whichTypeRet)
|
2011-09-28 18:30:06 +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:
|
2011-10-03 19:05:43 +02:00
|
|
|
return false;
|
|
|
|
case TX_PUBKEY:
|
2012-05-14 23:44:52 +02:00
|
|
|
keyID = CPubKey(vSolutions[0]).GetID();
|
|
|
|
return Sign1(keyID, keystore, hash, nHashType, scriptSigRet);
|
2011-10-03 19:05:43 +02:00
|
|
|
case TX_PUBKEYHASH:
|
2012-05-14 23:44:52 +02:00
|
|
|
keyID = CKeyID(uint160(vSolutions[0]));
|
|
|
|
if (!Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
|
2011-10-03 19:05:43 +02:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
{
|
2012-05-14 19:07:52 +02:00
|
|
|
CPubKey vch;
|
2012-05-14 23:44:52 +02:00
|
|
|
keystore.GetPubKey(keyID, vch);
|
2011-10-03 19:05:43 +02:00
|
|
|
scriptSigRet << vch;
|
|
|
|
}
|
2012-01-05 03:40:52 +01:00
|
|
|
return true;
|
2011-10-03 19:05:43 +02:00
|
|
|
case TX_SCRIPTHASH:
|
2012-01-05 03:40:52 +01:00
|
|
|
return keystore.GetCScript(uint160(vSolutions[0]), scriptSigRet);
|
|
|
|
|
2011-10-03 19:05:43 +02:00
|
|
|
case TX_MULTISIG:
|
|
|
|
scriptSigRet << OP_0; // workaround CHECKMULTISIG bug
|
|
|
|
return (SignN(vSolutions, keystore, hash, nHashType, scriptSigRet));
|
2011-09-28 18:30:06 +02:00
|
|
|
}
|
2012-01-05 03:40:52 +01:00
|
|
|
return false;
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 20:14:03 +02:00
|
|
|
unsigned int HaveKeys(const 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;
|
|
|
|
}
|
|
|
|
|
2014-06-09 21:11:59 +02:00
|
|
|
isminetype IsMine(const CKeyStore &keystore, const CTxDestination& dest)
|
2012-05-14 23:44:52 +02:00
|
|
|
{
|
2014-06-09 21:11:59 +02:00
|
|
|
CScript script;
|
|
|
|
script.SetDestination(dest);
|
|
|
|
return IsMine(keystore, script);
|
2012-05-14 23:44:52 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 01:06:01 +02:00
|
|
|
isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
2011-10-03 19:05:43 +02:00
|
|
|
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))
|
2014-07-01 11:00:22 +02:00
|
|
|
return ISMINE_WATCH_ONLY;
|
|
|
|
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();
|
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_PUBKEYHASH:
|
2012-05-14 23:44:52 +02:00
|
|
|
keyID = CKeyID(uint160(vSolutions[0]));
|
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)) {
|
|
|
|
isminetype ret = IsMine(keystore, subscript);
|
2014-07-01 11:00:22 +02:00
|
|
|
if (ret == ISMINE_SPENDABLE)
|
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
|
|
|
}
|
2011-10-03 19:05:43 +02:00
|
|
|
case TX_MULTISIG:
|
|
|
|
{
|
|
|
|
// Only consider transactions "mine" if we own ALL the
|
|
|
|
// keys involved. multi-signature transactions that are
|
|
|
|
// partially owned (somebody else has a key that can spend
|
|
|
|
// them) enable spend-out-from-under-you attacks, especially
|
|
|
|
// in shared-wallet situations.
|
|
|
|
vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
|
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
|
|
|
|
2014-06-09 21:11:59 +02:00
|
|
|
if (keystore.HaveWatchOnly(scriptPubKey))
|
2014-07-01 11:00:22 +02:00
|
|
|
return ISMINE_WATCH_ONLY;
|
|
|
|
return ISMINE_NO;
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
|
2013-04-29 19:50:02 +02:00
|
|
|
class CAffectedKeysVisitor : public boost::static_visitor<void> {
|
|
|
|
private:
|
|
|
|
const CKeyStore &keystore;
|
|
|
|
std::vector<CKeyID> &vKeys;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CAffectedKeysVisitor(const CKeyStore &keystoreIn, std::vector<CKeyID> &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {}
|
|
|
|
|
|
|
|
void Process(const CScript &script) {
|
|
|
|
txnouttype type;
|
|
|
|
std::vector<CTxDestination> vDest;
|
|
|
|
int nRequired;
|
|
|
|
if (ExtractDestinations(script, type, vDest, nRequired)) {
|
|
|
|
BOOST_FOREACH(const CTxDestination &dest, vDest)
|
|
|
|
boost::apply_visitor(*this, dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const CKeyID &keyId) {
|
|
|
|
if (keystore.HaveKey(keyId))
|
|
|
|
vKeys.push_back(keyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const CScriptID &scriptId) {
|
|
|
|
CScript script;
|
|
|
|
if (keystore.GetCScript(scriptId, script))
|
|
|
|
Process(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const CNoDestination &none) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys) {
|
|
|
|
CAffectedKeysVisitor(keystore, vKeys).Process(scriptPubKey);
|
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
bool SignSignature(const CKeyStore &keystore, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, int nHashType)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
assert(nIn < txTo.vin.size());
|
|
|
|
CTxIn& txin = txTo.vin[nIn];
|
|
|
|
|
|
|
|
// Leave out the signature from the hash, since a signature can't sign itself.
|
|
|
|
// The checksig op will also drop the signatures from its hash.
|
2012-06-21 23:05:42 +02:00
|
|
|
uint256 hash = SignatureHash(fromPubKey, txTo, nIn, nHashType);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2012-01-05 03:40:52 +01:00
|
|
|
txnouttype whichType;
|
2012-06-21 23:05:42 +02:00
|
|
|
if (!Solver(keystore, fromPubKey, hash, nHashType, txin.scriptSig, whichType))
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
|
|
|
|
2012-01-05 03:40:52 +01:00
|
|
|
if (whichType == TX_SCRIPTHASH)
|
|
|
|
{
|
|
|
|
// Solver returns the subscript that need to be evaluated;
|
|
|
|
// the final scriptSig is the signatures from that
|
|
|
|
// and then the serialized subscript:
|
|
|
|
CScript subscript = txin.scriptSig;
|
|
|
|
|
|
|
|
// Recompute txn hash using subscript in place of scriptPubKey:
|
|
|
|
uint256 hash2 = SignatureHash(subscript, txTo, nIn, nHashType);
|
2012-05-31 22:01:16 +02:00
|
|
|
|
2012-01-05 03:40:52 +01:00
|
|
|
txnouttype subType;
|
2012-05-31 22:01:16 +02:00
|
|
|
bool fSolved =
|
|
|
|
Solver(keystore, subscript, hash2, nHashType, txin.scriptSig, subType) && subType != TX_SCRIPTHASH;
|
|
|
|
// Append serialized subscript whether or not it is completely signed:
|
|
|
|
txin.scriptSig << static_cast<valtype>(subscript);
|
|
|
|
if (!fSolved) return false;
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
2010-08-29 18:58:15 +02:00
|
|
|
// Test solution
|
2014-03-10 22:31:46 +01:00
|
|
|
return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, STANDARD_SCRIPT_VERIFY_FLAGS, 0);
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType)
|
2012-06-21 23:05:42 +02:00
|
|
|
{
|
|
|
|
assert(nIn < txTo.vin.size());
|
|
|
|
CTxIn& txin = txTo.vin[nIn];
|
|
|
|
assert(txin.prevout.n < txFrom.vout.size());
|
|
|
|
const CTxOut& txout = txFrom.vout[txin.prevout.n];
|
|
|
|
|
|
|
|
return SignSignature(keystore, txout.scriptPubKey, txTo, nIn, nHashType);
|
|
|
|
}
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2012-05-31 22:01:16 +02:00
|
|
|
static CScript PushAll(const vector<valtype>& values)
|
|
|
|
{
|
|
|
|
CScript result;
|
|
|
|
BOOST_FOREACH(const valtype& v, values)
|
|
|
|
result << v;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
static CScript CombineMultisig(CScript scriptPubKey, const CMutableTransaction& txTo, unsigned int nIn,
|
2012-05-31 22:01:16 +02:00
|
|
|
const vector<valtype>& vSolutions,
|
|
|
|
vector<valtype>& sigs1, vector<valtype>& sigs2)
|
|
|
|
{
|
|
|
|
// Combine all the signatures we've got:
|
|
|
|
set<valtype> allsigs;
|
|
|
|
BOOST_FOREACH(const valtype& v, sigs1)
|
|
|
|
{
|
|
|
|
if (!v.empty())
|
|
|
|
allsigs.insert(v);
|
|
|
|
}
|
|
|
|
BOOST_FOREACH(const valtype& v, sigs2)
|
|
|
|
{
|
|
|
|
if (!v.empty())
|
|
|
|
allsigs.insert(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a map of pubkey -> signature by matching sigs to pubkeys:
|
2012-07-05 19:25:52 +02:00
|
|
|
assert(vSolutions.size() > 1);
|
|
|
|
unsigned int nSigsRequired = vSolutions.front()[0];
|
|
|
|
unsigned int nPubKeys = vSolutions.size()-2;
|
2012-05-31 22:01:16 +02:00
|
|
|
map<valtype, valtype> sigs;
|
|
|
|
BOOST_FOREACH(const valtype& sig, allsigs)
|
|
|
|
{
|
2012-07-05 19:25:52 +02:00
|
|
|
for (unsigned int i = 0; i < nPubKeys; i++)
|
2012-05-31 22:01:16 +02:00
|
|
|
{
|
|
|
|
const valtype& pubkey = vSolutions[i+1];
|
|
|
|
if (sigs.count(pubkey))
|
|
|
|
continue; // Already got a sig for this pubkey
|
|
|
|
|
2012-12-08 22:49:04 +01:00
|
|
|
if (CheckSig(sig, pubkey, scriptPubKey, txTo, nIn, 0, 0))
|
2012-05-31 22:01:16 +02:00
|
|
|
{
|
|
|
|
sigs[pubkey] = sig;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now build a merged CScript:
|
|
|
|
unsigned int nSigsHave = 0;
|
|
|
|
CScript result; result << OP_0; // pop-one-too-many workaround
|
2012-07-05 19:25:52 +02:00
|
|
|
for (unsigned int i = 0; i < nPubKeys && nSigsHave < nSigsRequired; i++)
|
2012-05-31 22:01:16 +02:00
|
|
|
{
|
|
|
|
if (sigs.count(vSolutions[i+1]))
|
|
|
|
{
|
|
|
|
result << sigs[vSolutions[i+1]];
|
|
|
|
++nSigsHave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fill any missing with OP_0:
|
2012-07-05 19:25:52 +02:00
|
|
|
for (unsigned int i = nSigsHave; i < nSigsRequired; i++)
|
2012-05-31 22:01:16 +02:00
|
|
|
result << OP_0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
|
|
|
|
const txnouttype txType, const vector<valtype>& vSolutions,
|
|
|
|
vector<valtype>& sigs1, vector<valtype>& sigs2)
|
|
|
|
{
|
|
|
|
switch (txType)
|
|
|
|
{
|
|
|
|
case TX_NONSTANDARD:
|
2013-06-24 21:09:50 +02:00
|
|
|
case TX_NULL_DATA:
|
2012-05-31 22:01:16 +02:00
|
|
|
// Don't know anything about this, assume bigger one is correct:
|
|
|
|
if (sigs1.size() >= sigs2.size())
|
|
|
|
return PushAll(sigs1);
|
|
|
|
return PushAll(sigs2);
|
|
|
|
case TX_PUBKEY:
|
|
|
|
case TX_PUBKEYHASH:
|
|
|
|
// Signatures are bigger than placeholders or empty scripts:
|
|
|
|
if (sigs1.empty() || sigs1[0].empty())
|
|
|
|
return PushAll(sigs2);
|
|
|
|
return PushAll(sigs1);
|
|
|
|
case TX_SCRIPTHASH:
|
|
|
|
if (sigs1.empty() || sigs1.back().empty())
|
|
|
|
return PushAll(sigs2);
|
|
|
|
else if (sigs2.empty() || sigs2.back().empty())
|
|
|
|
return PushAll(sigs1);
|
|
|
|
else
|
|
|
|
{
|
2012-07-26 02:48:39 +02:00
|
|
|
// Recur to combine:
|
2012-05-31 22:01:16 +02:00
|
|
|
valtype spk = sigs1.back();
|
|
|
|
CScript pubKey2(spk.begin(), spk.end());
|
|
|
|
|
|
|
|
txnouttype txType2;
|
|
|
|
vector<vector<unsigned char> > vSolutions2;
|
|
|
|
Solver(pubKey2, txType2, vSolutions2);
|
|
|
|
sigs1.pop_back();
|
|
|
|
sigs2.pop_back();
|
|
|
|
CScript result = CombineSignatures(pubKey2, txTo, nIn, txType2, vSolutions2, sigs1, sigs2);
|
|
|
|
result << spk;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
case TX_MULTISIG:
|
|
|
|
return CombineMultisig(scriptPubKey, txTo, nIn, vSolutions, sigs1, sigs2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
|
|
|
|
const CScript& scriptSig1, const CScript& scriptSig2)
|
|
|
|
{
|
|
|
|
txnouttype txType;
|
|
|
|
vector<vector<unsigned char> > vSolutions;
|
|
|
|
Solver(scriptPubKey, txType, vSolutions);
|
|
|
|
|
|
|
|
vector<valtype> stack1;
|
2012-11-13 23:03:25 +01:00
|
|
|
EvalScript(stack1, scriptSig1, CTransaction(), 0, SCRIPT_VERIFY_STRICTENC, 0);
|
2012-05-31 22:01:16 +02:00
|
|
|
vector<valtype> stack2;
|
2012-11-13 23:03:25 +01:00
|
|
|
EvalScript(stack2, scriptSig2, CTransaction(), 0, SCRIPT_VERIFY_STRICTENC, 0);
|
2012-05-31 22:01:16 +02:00
|
|
|
|
|
|
|
return CombineSignatures(scriptPubKey, txTo, nIn, txType, vSolutions, stack1, stack2);
|
|
|
|
}
|