Adding property for serialization symmetry for TransactionInputWitness

This commit is contained in:
Chris Stewart 2016-11-28 13:08:47 -06:00
parent 9c3b3f0eb2
commit 600597ee45
2 changed files with 12 additions and 9 deletions

View File

@ -16,28 +16,29 @@ import scala.annotation.tailrec
trait RawTransactionInputWitnessParser extends RawBitcoinSerializer[TransactionInputWitness] {
override def read(bytes: List[Byte]): TransactionInputWitness = {
logger.info("Bytes for witness: " + BitcoinSUtil.encodeHex(bytes))
logger.debug("Bytes for witness: " + BitcoinSUtil.encodeHex(bytes))
//first byte is the number of stack items
val stackSize = CompactSizeUInt.parseCompactSizeUInt(bytes)
logger.info("Stack size: " + stackSize)
logger.debug("Stack size: " + stackSize)
val (_,stackBytes) = bytes.splitAt(stackSize.size.toInt)
logger.info("Stack bytes: " + BitcoinSUtil.encodeHex(stackBytes))
logger.debug("Stack bytes: " + BitcoinSUtil.encodeHex(stackBytes))
@tailrec
def loop(remainingBytes: Seq[Byte], accum: Seq[Seq[Byte]], remainingStackItems: UInt64): Seq[Seq[Byte]] = {
if (remainingStackItems <= UInt64.zero) accum.reverse
if (remainingStackItems <= UInt64.zero) accum
else {
val elementSize = CompactSizeUInt.parseCompactSizeUInt(remainingBytes)
val (_,stackElementBytes) = remainingBytes.splitAt(elementSize.size.toInt)
val stackElement = stackElementBytes.take(elementSize.num.toInt)
logger.info("Parsed stack element: " + BitcoinSUtil.encodeHex(stackElement))
logger.debug("Parsed stack element: " + BitcoinSUtil.encodeHex(stackElement))
val (_,newRemainingBytes) = stackElementBytes.splitAt(stackElement.size)
logger.info("New remaining bytes: " + BitcoinSUtil.encodeHex(newRemainingBytes))
logger.debug("New remaining bytes: " + BitcoinSUtil.encodeHex(newRemainingBytes))
loop(newRemainingBytes, stackElement +: accum, remainingStackItems - UInt64.one)
}
}
//note there is no 'reversing' the accum, in bitcoin-s we assume the stop of the stack is the 'head' element in the sequence
val stack = loop(stackBytes,Nil,stackSize.num)
val witness = ScriptWitness(stack)
logger.info("Parsed witness: " + witness)
logger.debug("Parsed witness: " + witness)
TransactionInputWitness(witness)
}

View File

@ -452,13 +452,15 @@ class TransactionSignatureSerializerTest extends FlatSpec with MustMatchers with
val spendingTx = Transaction(hex)
spendingTx.isInstanceOf[WitnessTransaction] must be (true)
spendingTx.hex must be (hex)
val inputIndex = UInt32.zero
val scriptPubKey = ScriptPubKey("0020b95237b48faaa69eb078e1170be3b5cbb3fddf16d0a991e14ad274f7b33a4f64")
val witnessRedeemScript = ScriptPubKey("410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac")
val amount = Satoshis(Int64(1))
val witnessStack = Seq("304402200d461c140cfdfcf36b94961db57ae8c18d1cb80e9d95a9e47ac22470c1bf125502201c8dc1cbfef6a3ef90acbbb992ca22fe9466ee6f9d4898eda277a7ac3ab4b25101",
"410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac")
val witness = ScriptWitness(witnessStack.map(BitcoinSUtil.decodeHex(_)))
val serializedForSig = TransactionSignatureSerializer.serializeForSignature(spendingTx,inputIndex,scriptPubKey.asm,HashType.sigHashAll,amount)
val serializedForSig = TransactionSignatureSerializer.serializeForSignature(spendingTx,inputIndex,witnessRedeemScript.asm,
HashType.sigHashAll,amount)
BitcoinSUtil.encodeHex(serializedForSig) must be (expected)
}