Changeset View
Changeset View
Standalone View
Standalone View
src/test/script_p2sh_tests.cpp
Show First 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(sign) { | ||||
// scriptPubKey: HASH160 <hash> EQUAL | // scriptPubKey: HASH160 <hash> EQUAL | ||||
// Test SignSignature() (and therefore the version of Solver() that signs | // Test SignSignature() (and therefore the version of Solver() that signs | ||||
// transactions) | // transactions) | ||||
FillableSigningProvider keystore; | FillableSigningProvider keystore; | ||||
CKey key[4]; | CKey key[4]; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
key[i].MakeNewKey(true); | key[i].MakeNewKey(true); | ||||
keystore.AddKey(key[i]); | BOOST_CHECK(keystore.AddKey(key[i])); | ||||
} | } | ||||
// 8 Scripts: checking all combinations of | // 8 Scripts: checking all combinations of | ||||
// different keys, straight/P2SH, pubkey/pubkeyhash | // different keys, straight/P2SH, pubkey/pubkeyhash | ||||
CScript standardScripts[4]; | CScript standardScripts[4]; | ||||
standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; | standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; | ||||
standardScripts[1] = GetScriptForDestination(PKHash(key[1].GetPubKey())); | standardScripts[1] = GetScriptForDestination(PKHash(key[1].GetPubKey())); | ||||
standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG; | standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG; | ||||
standardScripts[3] = GetScriptForDestination(PKHash(key[2].GetPubKey())); | standardScripts[3] = GetScriptForDestination(PKHash(key[2].GetPubKey())); | ||||
CScript evalScripts[4]; | CScript evalScripts[4]; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
keystore.AddCScript(standardScripts[i]); | BOOST_CHECK(keystore.AddCScript(standardScripts[i])); | ||||
evalScripts[i] = | evalScripts[i] = | ||||
GetScriptForDestination(ScriptHash(standardScripts[i])); | GetScriptForDestination(ScriptHash(standardScripts[i])); | ||||
} | } | ||||
// Funding transaction: | // Funding transaction: | ||||
CMutableTransaction txFrom; | CMutableTransaction txFrom; | ||||
std::string reason; | std::string reason; | ||||
txFrom.vout.resize(8); | txFrom.vout.resize(8); | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(set) { | BOOST_AUTO_TEST_CASE(set) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
// Test the CScript::Set* methods | // Test the CScript::Set* methods | ||||
FillableSigningProvider keystore; | FillableSigningProvider keystore; | ||||
CKey key[4]; | CKey key[4]; | ||||
std::vector<CPubKey> keys; | std::vector<CPubKey> keys; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
key[i].MakeNewKey(true); | key[i].MakeNewKey(true); | ||||
keystore.AddKey(key[i]); | BOOST_CHECK(keystore.AddKey(key[i])); | ||||
keys.push_back(key[i].GetPubKey()); | keys.push_back(key[i].GetPubKey()); | ||||
} | } | ||||
CScript inner[4]; | CScript inner[4]; | ||||
inner[0] = GetScriptForDestination(PKHash(key[0].GetPubKey())); | inner[0] = GetScriptForDestination(PKHash(key[0].GetPubKey())); | ||||
inner[1] = GetScriptForMultisig( | inner[1] = GetScriptForMultisig( | ||||
2, std::vector<CPubKey>(keys.begin(), keys.begin() + 2)); | 2, std::vector<CPubKey>(keys.begin(), keys.begin() + 2)); | ||||
inner[2] = GetScriptForMultisig( | inner[2] = GetScriptForMultisig( | ||||
1, std::vector<CPubKey>(keys.begin(), keys.begin() + 2)); | 1, std::vector<CPubKey>(keys.begin(), keys.begin() + 2)); | ||||
inner[3] = GetScriptForMultisig( | inner[3] = GetScriptForMultisig( | ||||
2, std::vector<CPubKey>(keys.begin(), keys.begin() + 3)); | 2, std::vector<CPubKey>(keys.begin(), keys.begin() + 3)); | ||||
CScript outer[4]; | CScript outer[4]; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
outer[i] = GetScriptForDestination(ScriptHash(inner[i])); | outer[i] = GetScriptForDestination(ScriptHash(inner[i])); | ||||
keystore.AddCScript(inner[i]); | BOOST_CHECK(keystore.AddCScript(inner[i])); | ||||
} | } | ||||
// Funding transaction: | // Funding transaction: | ||||
CMutableTransaction txFrom; | CMutableTransaction txFrom; | ||||
std::string reason; | std::string reason; | ||||
txFrom.vout.resize(4); | txFrom.vout.resize(4); | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
txFrom.vout[i].scriptPubKey = outer[i]; | txFrom.vout[i].scriptPubKey = outer[i]; | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(AreInputsStandard) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CCoinsView coinsDummy; | CCoinsView coinsDummy; | ||||
CCoinsViewCache coins(&coinsDummy); | CCoinsViewCache coins(&coinsDummy); | ||||
FillableSigningProvider keystore; | FillableSigningProvider keystore; | ||||
CKey key[6]; | CKey key[6]; | ||||
std::vector<CPubKey> keys; | std::vector<CPubKey> keys; | ||||
for (int i = 0; i < 6; i++) { | for (int i = 0; i < 6; i++) { | ||||
key[i].MakeNewKey(true); | key[i].MakeNewKey(true); | ||||
keystore.AddKey(key[i]); | BOOST_CHECK(keystore.AddKey(key[i])); | ||||
} | } | ||||
for (int i = 0; i < 3; i++) { | for (int i = 0; i < 3; i++) { | ||||
keys.push_back(key[i].GetPubKey()); | keys.push_back(key[i].GetPubKey()); | ||||
} | } | ||||
CMutableTransaction txFrom; | CMutableTransaction txFrom; | ||||
txFrom.vout.resize(4); | txFrom.vout.resize(4); | ||||
// First three are standard: | // First three are standard: | ||||
CScript pay1 = GetScriptForDestination(PKHash(key[0].GetPubKey())); | CScript pay1 = GetScriptForDestination(PKHash(key[0].GetPubKey())); | ||||
keystore.AddCScript(pay1); | BOOST_CHECK(keystore.AddCScript(pay1)); | ||||
CScript pay1of3 = GetScriptForMultisig(1, keys); | CScript pay1of3 = GetScriptForMultisig(1, keys); | ||||
// P2SH (OP_CHECKSIG) | // P2SH (OP_CHECKSIG) | ||||
txFrom.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(pay1)); | txFrom.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(pay1)); | ||||
txFrom.vout[0].nValue = 1000 * SATOSHI; | txFrom.vout[0].nValue = 1000 * SATOSHI; | ||||
// ordinary OP_CHECKSIG | // ordinary OP_CHECKSIG | ||||
txFrom.vout[1].scriptPubKey = pay1; | txFrom.vout[1].scriptPubKey = pay1; | ||||
txFrom.vout[1].nValue = 2000 * SATOSHI; | txFrom.vout[1].nValue = 2000 * SATOSHI; | ||||
// ordinary OP_CHECKMULTISIG | // ordinary OP_CHECKMULTISIG | ||||
txFrom.vout[2].scriptPubKey = pay1of3; | txFrom.vout[2].scriptPubKey = pay1of3; | ||||
txFrom.vout[2].nValue = 3000 * SATOSHI; | txFrom.vout[2].nValue = 3000 * SATOSHI; | ||||
// vout[3] is complicated 1-of-3 AND 2-of-3 | // vout[3] is complicated 1-of-3 AND 2-of-3 | ||||
// ... that is OK if wrapped in P2SH: | // ... that is OK if wrapped in P2SH: | ||||
CScript oneAndTwo; | CScript oneAndTwo; | ||||
oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) | oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) | ||||
<< ToByteVector(key[1].GetPubKey()) | << ToByteVector(key[1].GetPubKey()) | ||||
<< ToByteVector(key[2].GetPubKey()); | << ToByteVector(key[2].GetPubKey()); | ||||
oneAndTwo << OP_3 << OP_CHECKMULTISIGVERIFY; | oneAndTwo << OP_3 << OP_CHECKMULTISIGVERIFY; | ||||
oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) | oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) | ||||
<< ToByteVector(key[4].GetPubKey()) | << ToByteVector(key[4].GetPubKey()) | ||||
<< ToByteVector(key[5].GetPubKey()); | << ToByteVector(key[5].GetPubKey()); | ||||
oneAndTwo << OP_3 << OP_CHECKMULTISIG; | oneAndTwo << OP_3 << OP_CHECKMULTISIG; | ||||
keystore.AddCScript(oneAndTwo); | BOOST_CHECK(keystore.AddCScript(oneAndTwo)); | ||||
txFrom.vout[3].scriptPubKey = | txFrom.vout[3].scriptPubKey = | ||||
GetScriptForDestination(ScriptHash(oneAndTwo)); | GetScriptForDestination(ScriptHash(oneAndTwo)); | ||||
txFrom.vout[3].nValue = 4000 * SATOSHI; | txFrom.vout[3].nValue = 4000 * SATOSHI; | ||||
AddCoins(coins, CTransaction(txFrom), 0); | AddCoins(coins, CTransaction(txFrom), 0); | ||||
CMutableTransaction txTo; | CMutableTransaction txTo; | ||||
txTo.vout.resize(1); | txTo.vout.resize(1); | ||||
Show All 17 Lines |