Fixed all test cases that needed to be change due to moving CompactSizeUInts into ScriptSignature/ScriptPubKey -- now need to implement rest of segwit

This commit is contained in:
Chris Stewart 2016-12-02 19:04:14 -06:00
parent ed0a1b37e7
commit f8b15175f8
4 changed files with 21 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package org.bitcoins.core.crypto
import org.bitcoins.core.currency.{CurrencyUnit, Satoshis}
import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.CompactSizeUInt
import org.bitcoins.core.protocol.script._
import org.bitcoins.core.protocol.transaction._
import org.bitcoins.core.script.constant.ScriptToken
@ -189,12 +190,12 @@ trait TransactionSignatureSerializer extends RawBitcoinSerializerHelper with Bit
logger.debug("outputHash: " + outputHash.map(BitcoinSUtil.encodeHex(_)))
logger.debug("Script: " + script)
val scriptBytes = script.flatMap(_.bytes)
val fe: Seq[Byte] => Seq[Byte] = { bytes: Seq[Byte] => BitcoinSUtil.decodeHex(BitcoinSUtil.flipEndianness(bytes)) }
val serializationForSig: Seq[Byte] = fe(spendingTx.version.bytes) ++ outPointHash.getOrElse(Nil) ++ sequenceHash.getOrElse(Nil) ++
spendingTx.inputs(inputIndexInt).previousOutput.bytes ++
script.flatMap(_.bytes) ++ fe(amount.bytes) ++ fe(spendingTx.inputs(inputIndexInt).sequence.bytes) ++
outputHash.getOrElse(Nil) ++ fe(spendingTx.lockTime.bytes) ++
hashType.num.bytes.reverse
spendingTx.inputs(inputIndexInt).previousOutput.bytes ++ CompactSizeUInt.calculateCompactSizeUInt(scriptBytes).bytes ++
scriptBytes ++ fe(amount.bytes) ++ fe(spendingTx.inputs(inputIndexInt).sequence.bytes) ++
outputHash.getOrElse(Nil) ++ fe(spendingTx.lockTime.bytes) ++ hashType.num.bytes.reverse
logger.info("Serialization for signature for WitnessV0Sig: " + BitcoinSUtil.encodeHex(serializationForSig))
serializationForSig
}

View File

@ -23,7 +23,7 @@ class TransactionSignatureCreatorTest extends FlatSpec with MustMatchers with Bi
val expectedSig = ECDigitalSignature("30440220357864ae2beba3d6ec34c0ce42262c1c12939502f0f8f4bd338c9d8b307593420220656687c327589dc3e464700fa7b784c7efc2b465c627a60c2f1ce402d05fc39d01")
val rawTx = "01000000021d50bf7c05b6169ea8d8fb5b79dd2978bbd2ac756a656a777279da43b19fd9d9000000006b4830450221008f2c818a55045a1c9dcda54fcd5b6377f5d09723a9ccd8c71df76ee4bdf7c16802201817cbd71d8148a5d53b11d33c9c58ad1086fe7ddf308da2a7cceb7d85df293e01210381c82dc267a958be06f1c920dc635bcd191d698c167e67a45a882a551c57ce1dfeffffffd4a6a37abfe003a9d10155df215e662f88d5b878b908d1a3772a9fbd195d008d010000006a4730440220357864ae2beba3d6ec34c0ce42262c1c12939502f0f8f4bd338c9d8b307593420220656687c327589dc3e464700fa7b784c7efc2b465c627a60c2f1ce402d05fc39d0121036301d848aec3dfc47789a63ee3c85c6d3bf757162ef77cb1580981b422838ed7feffffff0200e1f505000000001976a9146d39bac171d0bf450698fa0ebd93f51e79dcb6ac88ac35a96d00000000001976a914e11753f499ac7a910148e53156ab273557ed517e88acd6090b00"
val transaction = Transaction(rawTx)
val scriptPubKey = ScriptPubKey("76a914d7b4717a934386601ac3f980d01b48c83b8a0b4b88ac")
val scriptPubKey = ScriptPubKey("1976a914d7b4717a934386601ac3f980d01b48c83b8a0b4b88ac")
val txSignatureComponent = TransactionSignatureComponent(transaction, UInt32.one,
scriptPubKey, Policy.standardScriptVerifyFlags)
val privateKey = ECPrivateKey.fromWIFToPrivateKey("cTPg4Zc5Jis2EZXy3NXShgbn487GWBTapbU63BerLDZM3w2hQSjC")
@ -33,13 +33,12 @@ class TransactionSignatureCreatorTest extends FlatSpec with MustMatchers with Bi
txSignature.hex must be (expectedSig.hex)
}
it must "create the correct digital signature for a transaction with 1 input" in {
//66f48fa8ef5db20a3b4be6b13f024b6e23480fd83df26ffbe7449110b113a665 on testnet
val expectedSig = ECDigitalSignature("3044022075b4ab08ff34799ee6f8048a5044be98dff493fc5a0b8a36dcaee3bd7a9993ae02207bc532ceab09c10f1d54035d03ff9aad0e1004c3e0325a8b97b6be04b7d6c3a201")
val rawTx = "0100000001b8a1278696acfa85f1f576836aa30d335207b69bdaff43d9464cc1db40fe19ae000000006a473044022075b4ab08ff34799ee6f8048a5044be98dff493fc5a0b8a36dcaee3bd7a9993ae02207bc532ceab09c10f1d54035d03ff9aad0e1004c3e0325a8b97b6be04b7d6c3a2012102a01aaa27b468ec3fb2ae0c2a9fa1d5dce9b79b35062178f479156d8daa6c0e50feffffff02a0860100000000001976a914775bd9c79a9e988c0d6177a9205a611a50b7229188acb6342900000000001976a914f23a46f930320ab3cc7ad8c1660325f4c434d11688ac63b70d00"
val transaction = Transaction(rawTx)
val scriptPubKey = ScriptPubKey("76a914cd0385f813ec73f8fc340b7069daf566878a0d6b88ac")
val scriptPubKey = ScriptPubKey("1976a914cd0385f813ec73f8fc340b7069daf566878a0d6b88ac")
val txSignatureComponent = TransactionSignatureComponent(transaction, UInt32.zero, scriptPubKey,
Policy.standardScriptVerifyFlags)
val privateKey = ECPrivateKey.fromWIFToPrivateKey("cTTh7jNtZhg3vHTjvYK8zcHkLfsMAS8iqL7pfZ6eVAVHHF8fN1qy")

View File

@ -454,13 +454,13 @@ class TransactionSignatureSerializerTest extends FlatSpec with MustMatchers with
spendingTx.hex must be (hex)
val inputIndex = UInt32.zero
val witnessRedeemScript = ScriptPubKey("410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac")
val witnessRedeemScript = ScriptPubKey("43410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac")
val amount = Satoshis(Int64(1))
val witnessStack = Seq("304402200d461c140cfdfcf36b94961db57ae8c18d1cb80e9d95a9e47ac22470c1bf125502201c8dc1cbfef6a3ef90acbbb992ca22fe9466ee6f9d4898eda277a7ac3ab4b25101",
"410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac")
val witness = ScriptWitness(witnessStack.map(BitcoinSUtil.decodeHex(_)))
val serializedForSig = TransactionSignatureSerializer.serializeForSignature(spendingTx,inputIndex,witnessRedeemScript.asm,
val serializedForSig = TransactionSignatureSerializer.serializeForSignature(spendingTx,inputIndex, witnessRedeemScript.asm,
HashType.sigHashAll,amount)
BitcoinSUtil.encodeHex(serializedForSig) must be (expected)

View File

@ -4,6 +4,7 @@ package org.bitcoins.core.protocol.script
import org.bitcoins.core.crypto._
import org.bitcoins.core.number.Int32
import org.bitcoins.core.protocol.script.testprotocol.SignatureHashTestCase
import org.bitcoins.core.protocol.transaction.Transaction
import org.bitcoins.core.script.crypto.{HashType, SIGHASH_ALL, SIGHASH_SINGLE}
import org.bitcoins.core.serializers.script.RawScriptSignatureParser
import org.bitcoins.core.util.{BitcoinSLogger, BitcoinSUtil, TestUtil}
@ -96,13 +97,15 @@ class ScriptSignatureTest extends FlatSpec with MustMatchers with BitcoinSLogger
it must "read sighash.json and return result" in {
import org.bitcoins.core.protocol.script.testprotocol.SignatureHashTestCaseProtocol._
//["raw_transaction, script, input_index, hashType, signature_hash (result)"],
/* val lines =
/*
val lines =
"""
| [
| ["4ddaa680026ec4d8060640304b86823f1ac760c260cef81d85bd847952863d629a3002b54b0200000008526365636a656aab65457861fc6c24bdc760c8b2e906b6656edaf9ed22b5f50e1fb29ec076ceadd9e8ebcb6b000000000152ffffffff033ff04f00000000000551526a00657a1d900300000000002153af040000000003006a6300000000", "ab526a53acabab", 0, 1055317633, "7f21b62267ed52462e371a917eb3542569a4049b9dfca2de3c75872b39510b26"]
| ["a0aa3126041621a6dea5b800141aa696daf28408959dfb2df96095db9fa425ad3f427f2f6103000000015360290e9c6063fa26912c2e7fb6a0ad80f1c5fea1771d42f12976092e7a85a4229fdb6e890000000001abc109f6e47688ac0e4682988785744602b8c87228fcef0695085edf19088af1a9db126e93000000000665516aac536affffffff8fe53e0806e12dfd05d67ac68f4768fdbe23fc48ace22a5aa8ba04c96d58e2750300000009ac51abac63ab5153650524aa680455ce7b000000000000499e50030000000008636a00ac526563ac5051ee030000000003abacabd2b6fe000000000003516563910fb6b5", "65", 0, -1391424484, "48d6a1bd2cd9eec54eb866fc71209418a950402b5d7e52363bfb75c98e141175"]
| ]
|
""".stripMargin*/
""".stripMargin
*/
val source = Source.fromURL(this.getClass.getResource("/sighash.json"))
val lines = try source.getLines.filterNot(_.isEmpty).map(_.trim) mkString "\n" finally source.close()
@ -111,20 +114,22 @@ class ScriptSignatureTest extends FlatSpec with MustMatchers with BitcoinSLogger
for {
testCase <- testCases
} yield {
Transaction(testCase.transaction.hex) must be (testCase.transaction)
logger.info("Test case: " + testCase)
logger.info("Hash type num: " + testCase.hashTypeNum)
logger.info("Hash type: " + testCase.hashType)
val hashForSig = TransactionSignatureSerializer.hashForSignature(testCase.transaction, testCase.inputIndex, testCase.script.asm, testCase.hashType)
//the hash is returned with opposite endianness
logger.info("Expected hash: " + testCase.hash.hex)
val flipHash = BitcoinSUtil.flipEndianness(testCase.hash.hex)
hashForSig must be (DoubleSha256Digest(flipHash))
}
}
/* it must "create a cltvScriptSig with the correct underlying scriptSig" in {
val cltvScriptPubKey = CLTVScriptPubKey("04e71bbe57b17576a914da88dc82530f0a4d1327dcfe75cc60c44277532c88ac")
it must "create a cltvScriptSig with the correct underlying scriptSig" in {
val cltvScriptPubKey = CLTVScriptPubKey("2004e71bbe57b17576a914da88dc82530f0a4d1327dcfe75cc60c44277532c88ac")
val pubKey = ECPublicKey("039ba48e162b1f47246f4ce9dc40f197fab7bde11da1b2fe9ac21113959e9f381b")
val sig = ECDigitalSignature("3045022100d71cfe32fa4545c5a0fd665b3701eb458a1bacbba868a05fa703fd1fa4b4f5c502204ee706334f976d0bee9b0f0ff919c1dfe9ba027993bf3e39fc03416ba4255b2401")
CLTVScriptSignature(cltvScriptPubKey, Seq(sig), Seq(pubKey)).scriptSig.isInstanceOf[P2PKHScriptSignature]
}*/
CLTVScriptSignature(cltvScriptPubKey, Seq(sig), Seq(pubKey)).scriptSig.isInstanceOf[P2PKHScriptSignature] must be (true)
}
}