1 #include <boost/assert.hpp>
2 #include <boost/assign/list_of.hpp>
3 #include <boost/assign/list_inserter.hpp>
4 #include <boost/assign/std/vector.hpp>
5 #include <boost/test/unit_test.hpp>
6 #include <boost/foreach.hpp>
10 #include "../wallet.h"
18 static std::vector<unsigned char>
21 std::vector<unsigned char> sSerialized(s);
26 Verify(
const CScript& scriptSig,
const CScript& scriptPubKey,
bool fStrict)
30 txFrom.
vout.resize(1);
31 txFrom.
vout[0].scriptPubKey = scriptPubKey;
36 txTo.
vin[0].prevout.n = 0;
38 txTo.
vin[0].scriptSig = scriptSig;
39 txTo.
vout[0].nValue = 1;
45 BOOST_AUTO_TEST_SUITE(script_P2SH_tests)
56 for (
int i = 0; i < 4; i++)
70 for (
int i = 0; i < 4; i++)
77 txFrom.
vout.resize(8);
78 for (
int i = 0; i < 4; i++)
80 txFrom.
vout[i].scriptPubKey = evalScripts[i];
81 txFrom.
vout[i].nValue = COIN;
82 txFrom.
vout[i+4].scriptPubKey = standardScripts[i];
83 txFrom.
vout[i+4].nValue = COIN;
88 for (
int i = 0; i < 8; i++)
90 txTo[i].
vin.resize(1);
91 txTo[i].vout.resize(1);
92 txTo[i].vin[0].prevout.n = i;
93 txTo[i].vin[0].prevout.hash = txFrom.
GetHash();
94 txTo[i].vout[0].nValue = 1;
95 BOOST_CHECK_MESSAGE(
IsMine(keystore, txFrom.
vout[i].scriptPubKey),
strprintf(
"IsMine %d", i));
97 for (
int i = 0; i < 8; i++)
103 for (
int i = 0; i < 8; i++)
104 for (
int j = 0; j < 8; j++)
106 CScript sigSave = txTo[i].vin[0].scriptSig;
107 txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
110 BOOST_CHECK_MESSAGE(sigOK,
strprintf(
"VerifySignature %d %d", i, j));
112 BOOST_CHECK_MESSAGE(!sigOK,
strprintf(
"VerifySignature %d %d", i, j));
113 txTo[i].vin[0].scriptSig = sigSave;
131 BOOST_CHECK(!Verify(scriptSig, p2sh,
true));
138 scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh);
140 BOOST_CHECK(Verify(scriptSig2, p2sh2,
true));
148 std::vector<CPubKey> keys;
149 for (
int i = 0; i < 4; i++)
153 keys.push_back(key[i].GetPubKey());
158 inner[1].
SetMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
159 inner[2].
SetMultisig(1, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
160 inner[3].
SetMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+3));
163 for (
int i = 0; i < 4; i++)
170 txFrom.
vout.resize(4);
171 for (
int i = 0; i < 4; i++)
173 txFrom.
vout[i].scriptPubKey = outer[i];
174 txFrom.
vout[i].nValue = CENT;
179 for (
int i = 0; i < 4; i++)
181 txTo[i].
vin.resize(1);
182 txTo[i].vout.resize(1);
183 txTo[i].vin[0].prevout.n = i;
184 txTo[i].vin[0].prevout.hash = txFrom.
GetHash();
185 txTo[i].vout[0].nValue = 1*CENT;
186 txTo[i].vout[0].scriptPubKey = inner[i];
187 BOOST_CHECK_MESSAGE(
IsMine(keystore, txFrom.
vout[i].scriptPubKey),
strprintf(
"IsMine %d", i));
189 for (
int i = 0; i < 4; i++)
205 static const unsigned char direct[] = {
OP_HASH160, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
206 BOOST_CHECK(
CScript(direct, direct+
sizeof(direct)).IsPayToScriptHash());
207 static const unsigned char pushdata1[] = {
OP_HASH160,
OP_PUSHDATA1, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
208 BOOST_CHECK(!
CScript(pushdata1, pushdata1+
sizeof(pushdata1)).IsPayToScriptHash());
209 static const unsigned char pushdata2[] = {
OP_HASH160,
OP_PUSHDATA2, 20,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
210 BOOST_CHECK(!
CScript(pushdata2, pushdata2+
sizeof(pushdata2)).IsPayToScriptHash());
211 static const unsigned char pushdata4[] = {
OP_HASH160,
OP_PUSHDATA4, 20,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
212 BOOST_CHECK(!
CScript(pushdata4, pushdata4+
sizeof(pushdata4)).IsPayToScriptHash());
240 BOOST_CHECK(Verify(scriptSig, fund,
false));
242 BOOST_CHECK(!Verify(scriptSig, fund,
true));
251 vector<CPubKey> keys;
252 for (
int i = 0; i < 3; i++)
256 keys.push_back(key[i].GetPubKey());
260 txFrom.
vout.resize(6);
268 txFrom.
vout[0].scriptPubKey = payScriptHash1;
269 txFrom.
vout[0].nValue = 1000;
270 txFrom.
vout[1].scriptPubKey = pay1;
271 txFrom.
vout[1].nValue = 2000;
272 txFrom.
vout[2].scriptPubKey = pay1of3;
273 txFrom.
vout[2].nValue = 3000;
278 txFrom.
vout[3].scriptPubKey = empty;
279 txFrom.
vout[3].nValue = 4000;
282 txFrom.
vout[4].nValue = 5000;
285 for (
int i = 0; i < 11; i++)
286 oneOfEleven << key[0].GetPubKey();
288 txFrom.
vout[5].scriptPubKey.SetDestination(oneOfEleven.
GetID());
289 txFrom.
vout[5].nValue = 6000;
295 txTo.
vout[0].scriptPubKey.SetDestination(key[1].GetPubKey().GetID());
298 txTo.
vin[0].prevout.n = 0;
301 txTo.
vin[1].prevout.n = 1;
304 txTo.
vin[2].prevout.n = 2;
312 for (
int i = 0; i < 3; i++)
315 txTo.
vin[i].scriptSig = (
CScript() << 11) + t;
317 txTo.
vin[i].scriptSig = t;
321 txToNonStd.
vout.resize(1);
322 txToNonStd.
vout[0].scriptPubKey.SetDestination(key[1].GetPubKey().GetID());
323 txToNonStd.
vout[0].nValue = 1000;
324 txToNonStd.
vin.resize(2);
325 txToNonStd.
vin[0].prevout.n = 4;
326 txToNonStd.
vin[0].prevout.hash = txFrom.
GetHash();
328 txToNonStd.
vin[1].prevout.n = 5;
329 txToNonStd.
vin[1].prevout.hash = txFrom.
GetHash();
335 txToNonStd.
vin[0].scriptSig.clear();
339 BOOST_AUTO_TEST_SUITE_END()
#define strprintf(format,...)
BOOST_AUTO_TEST_CASE(sign)
bool IsPayToScriptHash() const
void Serialize(Stream &s, char a, int, int=0)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType)
bool AreInputsStandard(CCoinsViewCache &mapInputs) const
Check for standard transaction types.
uint256 SignatureHash(CScript scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType)
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
uint160 Hash160(const T1 pbegin, const T1 pend)
void SetDestination(const CTxDestination &address)
virtual bool AddCScript(const CScript &redeemScript)
bool VerifySignature(const CCoins &txFrom, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Verify a signature.
unsigned int GetP2SHSigOpCount(CCoinsViewCache &mapInputs) const
Count ECDSA signature operations in pay-to-script-hash inputs.
int std::string int dummy
Abstract view on the open txout dataset.
CPubKey GetPubKey() const
std::vector< CTxOut > vout
void MakeNewKey(bool fCompressed)
virtual bool AddKey(const CKey &key)
Serialized script, used inside transaction inputs and outputs.
void SetMultisig(int nRequired, const std::vector< CPubKey > &keys)
bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
bool IsStandard(const CScript &scriptPubKey)
bool SetCoins(const uint256 &txid, const CCoins &coins)
An encapsulated private key.
The basic transaction that is broadcasted on the network and contained in blocks. ...
CCoinsView that adds a memory cache for transactions to another CCoinsView.
bool IsStandard(std::string &strReason) const
Check for standard transaction types.
Basic key store, that keeps keys in an address->secret map.