Changeset View
Changeset View
Standalone View
Standalone View
src/script/sign.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 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. | ||||
#include <script/sign.h> | #include <script/sign.h> | ||||
#include <key.h> | #include <key.h> | ||||
#include <keystore.h> | |||||
#include <policy/policy.h> | #include <policy/policy.h> | ||||
#include <primitives/transaction.h> | #include <primitives/transaction.h> | ||||
#include <script/standard.h> | #include <script/standard.h> | ||||
#include <uint256.h> | #include <uint256.h> | ||||
typedef std::vector<uint8_t> valtype; | typedef std::vector<uint8_t> valtype; | ||||
TransactionSignatureCreator::TransactionSignatureCreator( | TransactionSignatureCreator::TransactionSignatureCreator( | ||||
const CKeyStore *keystoreIn, const CTransaction *txToIn, unsigned int nInIn, | const SigningProvider *provider, const CTransaction *txToIn, | ||||
const Amount amountIn, SigHashType sigHashTypeIn) | unsigned int nInIn, const Amount amountIn, SigHashType sigHashTypeIn) | ||||
: BaseSignatureCreator(keystoreIn), txTo(txToIn), nIn(nInIn), | : BaseSignatureCreator(provider), txTo(txToIn), nIn(nInIn), | ||||
amount(amountIn), sigHashType(sigHashTypeIn), | amount(amountIn), sigHashType(sigHashTypeIn), | ||||
checker(txTo, nIn, amountIn) {} | checker(txTo, nIn, amountIn) {} | ||||
bool TransactionSignatureCreator::CreateSig(std::vector<uint8_t> &vchSig, | bool TransactionSignatureCreator::CreateSig(std::vector<uint8_t> &vchSig, | ||||
const CKeyID &address, | const CKeyID &address, | ||||
const CScript &scriptCode) const { | const CScript &scriptCode) const { | ||||
CKey key; | CKey key; | ||||
if (!keystore->GetKey(address, key)) { | if (!m_provider->GetKey(address, key)) { | ||||
return false; | return false; | ||||
} | } | ||||
uint256 hash = SignatureHash(scriptCode, *txTo, nIn, sigHashType, amount); | uint256 hash = SignatureHash(scriptCode, *txTo, nIn, sigHashType, amount); | ||||
if (!key.SignECDSA(hash, vchSig)) { | if (!key.SignECDSA(hash, vchSig)) { | ||||
return false; | return false; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | switch (whichTypeRet) { | ||||
return Sign1(keyID, creator, scriptPubKey, ret); | return Sign1(keyID, creator, scriptPubKey, ret); | ||||
case TX_PUBKEYHASH: { | case TX_PUBKEYHASH: { | ||||
keyID = CKeyID(uint160(vSolutions[0])); | keyID = CKeyID(uint160(vSolutions[0])); | ||||
if (!Sign1(keyID, creator, scriptPubKey, ret)) { | if (!Sign1(keyID, creator, scriptPubKey, ret)) { | ||||
return false; | return false; | ||||
} | } | ||||
CPubKey vch; | CPubKey vch; | ||||
creator.KeyStore().GetPubKey(keyID, vch); | creator.Provider().GetPubKey(keyID, vch); | ||||
ret.push_back(ToByteVector(vch)); | ret.push_back(ToByteVector(vch)); | ||||
return true; | return true; | ||||
} | } | ||||
case TX_SCRIPTHASH: | case TX_SCRIPTHASH: | ||||
if (creator.KeyStore().GetCScript(uint160(vSolutions[0]), | if (creator.Provider().GetCScript(uint160(vSolutions[0]), | ||||
scriptRet)) { | scriptRet)) { | ||||
ret.push_back( | ret.push_back( | ||||
std::vector<uint8_t>(scriptRet.begin(), scriptRet.end())); | std::vector<uint8_t>(scriptRet.begin(), scriptRet.end())); | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
case TX_MULTISIG: | case TX_MULTISIG: | ||||
// workaround CHECKMULTISIG bug | // workaround CHECKMULTISIG bug | ||||
ret.push_back(valtype()); | ret.push_back(valtype()); | ||||
return (SignN(vSolutions, creator, scriptPubKey, ret)); | return (SignN(vSolutions, creator, scriptPubKey, ret)); | ||||
default: | default: | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
static CScript PushAll(const std::vector<valtype> &values) { | static CScript PushAll(const std::vector<valtype> &values) { | ||||
CScript result; | CScript result; | ||||
for (const valtype &v : values) { | for (const valtype &v : values) { | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
void UpdateTransaction(CMutableTransaction &tx, unsigned int nIn, | void UpdateTransaction(CMutableTransaction &tx, unsigned int nIn, | ||||
const SignatureData &data) { | const SignatureData &data) { | ||||
assert(tx.vin.size() > nIn); | assert(tx.vin.size() > nIn); | ||||
UpdateInput(tx.vin[nIn], data); | UpdateInput(tx.vin[nIn], data); | ||||
} | } | ||||
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, | bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, | ||||
CMutableTransaction &txTo, unsigned int nIn, | CMutableTransaction &txTo, unsigned int nIn, | ||||
const Amount amount, SigHashType sigHashType) { | const Amount amount, SigHashType sigHashType) { | ||||
assert(nIn < txTo.vin.size()); | assert(nIn < txTo.vin.size()); | ||||
CTransaction txToConst(txTo); | CTransaction txToConst(txTo); | ||||
TransactionSignatureCreator creator(&keystore, &txToConst, nIn, amount, | TransactionSignatureCreator creator(&provider, &txToConst, nIn, amount, | ||||
sigHashType); | sigHashType); | ||||
SignatureData sigdata; | SignatureData sigdata; | ||||
bool ret = ProduceSignature(creator, fromPubKey, sigdata); | bool ret = ProduceSignature(creator, fromPubKey, sigdata); | ||||
UpdateTransaction(txTo, nIn, sigdata); | UpdateTransaction(txTo, nIn, sigdata); | ||||
return ret; | return ret; | ||||
} | } | ||||
bool SignSignature(const CKeyStore &keystore, const CTransaction &txFrom, | bool SignSignature(const SigningProvider &provider, const CTransaction &txFrom, | ||||
CMutableTransaction &txTo, unsigned int nIn, | CMutableTransaction &txTo, unsigned int nIn, | ||||
SigHashType sigHashType) { | SigHashType sigHashType) { | ||||
assert(nIn < txTo.vin.size()); | assert(nIn < txTo.vin.size()); | ||||
CTxIn &txin = txTo.vin[nIn]; | CTxIn &txin = txTo.vin[nIn]; | ||||
assert(txin.prevout.GetN() < txFrom.vout.size()); | assert(txin.prevout.GetN() < txFrom.vout.size()); | ||||
const CTxOut &txout = txFrom.vout[txin.prevout.GetN()]; | const CTxOut &txout = txFrom.vout[txin.prevout.GetN()]; | ||||
return SignSignature(keystore, txout.scriptPubKey, txTo, nIn, txout.nValue, | return SignSignature(provider, txout.scriptPubKey, txTo, nIn, txout.nValue, | ||||
sigHashType); | sigHashType); | ||||
} | } | ||||
static std::vector<valtype> CombineMultisig( | static std::vector<valtype> CombineMultisig( | ||||
const CScript &scriptPubKey, const BaseSignatureChecker &checker, | const CScript &scriptPubKey, const BaseSignatureChecker &checker, | ||||
const std::vector<valtype> &vSolutions, const std::vector<valtype> &sigs1, | const std::vector<valtype> &vSolutions, const std::vector<valtype> &sigs1, | ||||
const std::vector<valtype> &sigs2) { | const std::vector<valtype> &sigs2) { | ||||
// Combine all the signatures we've got: | // Combine all the signatures we've got: | ||||
▲ Show 20 Lines • Show All 174 Lines • Show Last 20 Lines |