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