| 48 | BOOST_FIXTURE_TEST_SUITE(script_p2sh_tests, BasicTestingSetup) |
| 49 | |
| 50 | BOOST_AUTO_TEST_CASE(sign) |
| 51 | { |
| 52 | LOCK(cs_main); |
| 53 | // Pay-to-script-hash looks like this: |
| 54 | // scriptSig: <sig> <sig...> <serialized_script> |
| 55 | // scriptPubKey: HASH160 <hash> EQUAL |
| 56 | |
| 57 | // Test SignSignature() (and therefore the version of Solver() that signs transactions) |
| 58 | FillableSigningProvider keystore; |
| 59 | CKey key[4]; |
| 60 | for (int i = 0; i < 4; i++) |
| 61 | { |
| 62 | key[i].MakeNewKey(true); |
| 63 | BOOST_CHECK(keystore.AddKey(key[i])); |
| 64 | } |
| 65 | |
| 66 | // 8 Scripts: checking all combinations of |
| 67 | // different keys, straight/P2SH, pubkey/pubkeyhash |
| 68 | CScript standardScripts[4]; |
| 69 | standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; |
| 70 | standardScripts[1] = GetScriptForDestination(PKHash(key[1].GetPubKey())); |
| 71 | standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG; |
| 72 | standardScripts[3] = GetScriptForDestination(PKHash(key[2].GetPubKey())); |
| 73 | CScript evalScripts[4]; |
| 74 | for (int i = 0; i < 4; i++) |
| 75 | { |
| 76 | BOOST_CHECK(keystore.AddCScript(standardScripts[i])); |
| 77 | evalScripts[i] = GetScriptForDestination(ScriptHash(standardScripts[i])); |
| 78 | } |
| 79 | |
| 80 | CMutableTransaction txFrom; // Funding transaction: |
| 81 | std::string reason; |
| 82 | txFrom.vout.resize(8); |
| 83 | for (int i = 0; i < 4; i++) |
| 84 | { |
| 85 | txFrom.vout[i].scriptPubKey = evalScripts[i]; |
| 86 | txFrom.vout[i].nValue = COIN; |
| 87 | txFrom.vout[i+4].scriptPubKey = standardScripts[i]; |
| 88 | txFrom.vout[i+4].nValue = COIN; |
| 89 | } |
| 90 | BOOST_CHECK(IsStandardTx(CTransaction(txFrom), reason)); |
| 91 | |
| 92 | CMutableTransaction txTo[8]; // Spending transactions |
| 93 | for (int i = 0; i < 8; i++) |
| 94 | { |
| 95 | txTo[i].vin.resize(1); |
| 96 | txTo[i].vout.resize(1); |
| 97 | txTo[i].vin[0].prevout.n = i; |
| 98 | txTo[i].vin[0].prevout.hash = txFrom.GetHash(); |
| 99 | txTo[i].vout[0].nValue = 1; |
| 100 | } |
| 101 | for (int i = 0; i < 8; i++) |
| 102 | { |
| 103 | BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), txTo[i], 0, SIGHASH_ALL), strprintf("SignSignature %d", i)); |
| 104 | } |
| 105 | // All of the above should be OK, and the txTos have valid signatures |
| 106 | // Check to make sure signature verification fails if we use the wrong ScriptSig: |
| 107 | for (int i = 0; i < 8; i++) { |
nothing calls this directly
no test coverage detected