aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2012-06-21 17:05:42 -0400
committerGavin Andresen <gavinandresen@gmail.com>2012-07-05 12:42:25 -0400
commit34420d655d8c1cf15e53803ca85e0ac2a2a62fe9 (patch)
treedf152867d93162ab6a5a6a52097bbeecc279c705
parent9a6ab7f1420691267e78e302ff224076945e9133 (diff)
Refactor: SignSignature/VerifyScript
Minor refactor to support signrawtx signing/verifying transactions when it might only have the previous transaction's txid and txOut.
-rw-r--r--src/script.cpp27
-rw-r--r--src/script.h3
2 files changed, 17 insertions, 13 deletions
diff --git a/src/script.cpp b/src/script.cpp
index 2e1e1ad7de..1451747fa2 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -1590,19 +1590,17 @@ bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const C
}
-bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType)
+bool SignSignature(const CKeyStore &keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType)
{
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];
// 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.
- uint256 hash = SignatureHash(txout.scriptPubKey, txTo, nIn, nHashType);
+ uint256 hash = SignatureHash(fromPubKey, txTo, nIn, nHashType);
txnouttype whichType;
- if (!Solver(keystore, txout.scriptPubKey, hash, nHashType, txin.scriptSig, whichType))
+ if (!Solver(keystore, fromPubKey, hash, nHashType, txin.scriptSig, whichType))
return false;
if (whichType == TX_SCRIPTHASH)
@@ -1623,12 +1621,18 @@ bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTrans
}
// Test solution
- if (!VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, true, 0))
- return false;
-
- return true;
+ return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, true, 0);
}
+bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType)
+{
+ 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);
+}
bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType)
{
@@ -1641,10 +1645,7 @@ bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsig
if (txin.prevout.hash != txFrom.GetHash())
return false;
- if (!VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, fValidatePayToScriptHash, nHashType))
- return false;
-
- return true;
+ return VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, fValidatePayToScriptHash, nHashType);
}
unsigned int CScript::GetSigOpCount(bool fAccurate) const
diff --git a/src/script.h b/src/script.h
index e2b83bd6ee..38f3469364 100644
--- a/src/script.h
+++ b/src/script.h
@@ -591,7 +591,10 @@ bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
bool IsMine(const CKeyStore& keystore, const CTxDestination &dest);
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
+bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
+bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
+ bool fValidatePayToScriptHash, int nHashType);
bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
#endif