Fixing bug with serializing single small VarInts

This commit is contained in:
Chris Stewart 2016-02-22 20:18:52 -06:00
parent 151c2220f5
commit 5c85d57be2
2 changed files with 18 additions and 10 deletions

View File

@ -21,7 +21,7 @@ trait VarInt {
def size : Long
def hex = size match {
case 1 => num.toHexString
case 1 => if (num.toHexString.size == 1) "0" + num.toHexString else num.toHexString
case 3 => "fd" + ScalacoinUtil.littleEndianToBigEndian(ScalacoinUtil.longToHex(num))
case 5 => "fe" + ScalacoinUtil.littleEndianToBigEndian(ScalacoinUtil.longToHex(num))
case _ => "ff" + ScalacoinUtil.littleEndianToBigEndian(ScalacoinUtil.longToHex(num))

View File

@ -7,7 +7,7 @@ import org.bitcoinj.core.Transaction.SigHash
import org.bitcoinj.params.TestNet3Params
import org.bitcoinj.script.{ScriptChunk, ScriptBuilder}
import org.scalacoin.protocol.script.{UpdateScriptPubKeyAsm, UpdateScriptPubKeyBytes, ScriptPubKey, ScriptPubKeyFactory}
import org.scalacoin.protocol.transaction.{UpdateTransactionInputs, TransactionOutput, UpdateTransactionOutputs, Transaction}
import org.scalacoin.protocol.transaction._
import org.scalacoin.script.ScriptOperationFactory
import org.scalacoin.script.bitwise.OP_EQUALVERIFY
import org.scalacoin.script.constant._
@ -56,6 +56,7 @@ class TransactionSignatureSerializerTest extends FlatSpec with MustMatchers {
val scriptPubKey = bitcoinjScriptToScriptPubKey(multiSigScript)
val creditingTx = Transaction.factory("01000000013df681ff83b43b6585fa32dd0e12b0b502e6481e04ee52ff0fdaf55a16a4ef61000000006b483045022100a84acca7906c13c5895a1314c165d33621cdcf8696145080895cbf301119b7cf0220730ff511106aa0e0a8570ff00ee57d7a6f24e30f592a10cae1deffac9e13b990012102b8d567bcd6328fd48a429f9cf4b315b859a58fd28c5088ef3cb1d98125fc4e8dffffffff02364f1c00000000001976a91439a02793b418de8ec748dd75382656453dc99bcb88ac40420f000000000017a9145780b80be32e117f675d6e0ada13ba799bf248e98700000000")
val output = creditingTx.outputs(1)
val address = new org.bitcoinj.core.Address(params, "n3CFiCmBXVt5d3HXKQ15EFZyhPz4yj5F3H");
val addressHash = new ScriptConstantImpl(address.getHash160.toList)
@ -63,18 +64,25 @@ class TransactionSignatureSerializerTest extends FlatSpec with MustMatchers {
UpdateScriptPubKeyAsm(Seq(OP_DUP, OP_HASH160, addressHash, OP_EQUALVERIFY, OP_CHECKSIG))
)
val txOut = TransactionOutput.factory(output.value,outputScript)
//val txIn = TransactionInput.factory(txOut)
val spendingTx = Transaction.empty.factory(
UpdateTransactionOutputs(Seq())
).factory(UpdateTransactionInputs(Seq()))
ScalacoinUtil.encodeHex(outputScript.bytes)
val spendingTxOut : TransactionOutput = TransactionOutput.factory(output.value,outputScript)
val spendingTxIn : TransactionInput = TransactionInput.factory(spendingTxOut,creditingTx)
val spendingTxWithOutput = Transaction.empty.factory(
UpdateTransactionOutputs(Seq(spendingTxOut))
)
println(spendingTxWithOutput.bytes)
val spendingTx = spendingTxWithOutput.factory(UpdateTransactionInputs(Seq(spendingTxIn)))
println(spendingTxIn.bytes)
println(spendingTx.bytes)
val txSignatureSerializer = new BaseTransactionSignatureSerializer(Transaction.empty)
val txSignatureSerializer = new BaseTransactionSignatureSerializer(spendingTx)
val bitcoinjSigHash = createBitcoinjMultiSigScriptHashForSig
txSignatureSerializer.serialize(0,scriptPubKey,SIGHASH_ALL) must be (bitcoinjSigHash)
val sigBytes = txSignatureSerializer.serialize(0,scriptPubKey,SIGHASH_ALL)
ScalacoinUtil.encodeHex(sigBytes) must be (bitcoinjSigHash)
}