Adding commitmentHeader constant, adding TODO: inside of RawScriptSignatureParser about using .toInt with UInt64

This commit is contained in:
Chris Stewart 2016-12-26 15:56:30 -06:00
parent 5021934824
commit 10bce4eb7f
4 changed files with 13 additions and 5 deletions

View file

@ -72,11 +72,11 @@ object CompactSizeUInt extends Factory[CompactSizeUInt] {
def calculateCompactSizeUInt(hex : String) : CompactSizeUInt = calculateCompactSizeUInt(BitcoinSUtil.decodeHex(hex))
/** Parses a VarInt from a string of hex characters
* https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers. */
* [[https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers]] */
def parseCompactSizeUInt(hex : String) : CompactSizeUInt = parseCompactSizeUInt(BitcoinSUtil.decodeHex(hex))
/** Parses a [[CompactSizeUInt]] from a sequence of bytes
* https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers. */
* [[https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers]] */
def parseCompactSizeUInt(bytes : Seq[Byte]) : CompactSizeUInt = {
require(bytes.nonEmpty, "Cannot parse a VarInt if the byte array is size 0")
//8 bit number

View file

@ -630,9 +630,10 @@ object WitnessCommitment extends ScriptFactory[WitnessCommitment] {
if (asm.size < 3) false
else {
val minCommitmentSize = 38
val commitmentHeader = "aa21a9ed"
val Seq(opReturn, pushOp, constant) = asm.take(3)
opReturn == OP_RETURN && pushOp == BytesToPushOntoStack(36) &&
constant.hex.take(8) == "aa21a9ed" && asm.flatMap(_.bytes).size >= minCommitmentSize
constant.hex.take(8) == commitmentHeader && asm.flatMap(_.bytes).size >= minCommitmentSize
}
}
}

View file

@ -8,6 +8,8 @@ import org.bitcoins.core.script.crypto.{OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY
import org.bitcoins.core.util.{BitcoinSLogger, BitcoinSUtil}
import org.slf4j.LoggerFactory
import scala.util.Try
/**
* Created by chris on 1/12/16.
*/
@ -17,7 +19,12 @@ trait RawScriptSignatureParser extends RawBitcoinSerializer[ScriptSignature] wit
if (bytes.isEmpty) EmptyScriptSignature
else {
val compactSizeUInt = CompactSizeUInt.parseCompactSizeUInt(bytes)
val scriptSigBytes = bytes.slice(compactSizeUInt.size.toInt, compactSizeUInt.num.toInt + compactSizeUInt.size.toInt)
//TODO: Figure out a better way to do this, we can theoretically have numbers larger than Int.MaxValue,
//but scala collections don't allow you to use 'slice' with longs
//the same problem happens inside of 'RawScriptPubKeyParser'
val len = Try(compactSizeUInt.num.toInt).getOrElse(Int.MaxValue)
val scriptSigBytes = bytes.slice(compactSizeUInt.size.toInt,
compactSizeUInt.num.toInt + compactSizeUInt.size.toInt)
val scriptTokens : List[ScriptToken] = ScriptParser.fromBytes(scriptSigBytes)
ScriptSignature.fromAsm(scriptTokens)
}

View file

@ -1,6 +1,7 @@
package org.bitcoins.core.protocol.blockchain
import org.bitcoins.core.protocol.CompactSizeUInt
import org.bitcoins.core.serializers.script.RawScriptSignatureParser
import org.bitcoins.core.util.BitcoinSLogger
import org.scalatest.{FlatSpec, MustMatchers}
@ -17,5 +18,4 @@ class BlockTest extends FlatSpec with MustMatchers with BitcoinSLogger {
val block = Block(hex)
block.hex must be (hex)
}
}