Skip to content

Commit

Permalink
fix nits and add fromByte function to HashType
Browse files Browse the repository at this point in the history
  • Loading branch information
TomMcCabe committed Aug 7, 2016
1 parent 9012e1a commit 521cc04
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ trait TransactionSignatureChecker extends BitcoinSLogger {
logger.error("Signature did not have a low s value")
ScriptValidationFailureHighSValue
} else if (ScriptFlagUtil.requireStrictEncoding(flags) && signature.bytes.nonEmpty &&
!HashType.hashTypes.contains(HashType(Int32(signature.bytes.last)))) {
!HashType.hashTypes.contains(HashType(signature.bytes.last))) {
logger.error("signature: " + signature.bytes)
logger.error("Hash type was not defined on the signature")
ScriptValidationFailureHashType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ trait TransactionSignatureSerializer extends RawBitcoinSerializerHelper with Bit
}

val txWithInputSigsRemoved = Transaction(spendingTransaction,UpdateTransactionInputs(updatedInputs))
val sigHashBytes : List[Byte] = hashType.num.bytes.reverse.toList
val sigHashBytes = hashType.num.bytes.reverse
//check the hash type
hashType match {
case _ : SIGHASH_NONE =>
Expand Down Expand Up @@ -147,7 +147,7 @@ trait TransactionSignatureSerializer extends RawBitcoinSerializerHelper with Bit
if (inputIndex >= UInt32(spendingTransaction.inputs.size)) {
logger.warn("Our inputIndex is out of the range of the inputs in the spending transaction")
errorHash
} else if(HashType.isSIGHASH_SINGLE(hashType.num) && inputIndex >= UInt32(spendingTransaction.outputs.size)) {
} else if(hashType.isInstanceOf[SIGHASH_SINGLE] && inputIndex >= UInt32(spendingTransaction.outputs.size)) {
logger.warn("When we have a SIGHASH_SINGLE we cannot have more inputs than outputs")
errorHash
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/main/scala/org/bitcoins/core/script/crypto/HashType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ object HashType extends Factory[HashType] {
fromNumber(num)
}

def fromByte(byte: Byte) : HashType = fromBytes(Seq(byte))

def fromNumber(num : Int32) : HashType = {
if (isSIGHASH_NONE(num)) {
if (isSIGHASH_NONE_ANYONECANPAY(num)) SIGHASH_NONE_ANYONECANPAY(num) else SIGHASH_NONE(num)
Expand All @@ -36,6 +38,11 @@ object HashType extends Factory[HashType] {
}
}

/**
* Returns a hashtype's default byte value
* @param hashType
* @return
*/
def byte (hashType : HashType) : Byte = hashType match {
case _ : SIGHASH_ALL => SIGHASH_ALL.defaultValue.num.underlying.toByte
case _ : SIGHASH_NONE => SIGHASH_NONE.defaultValue.num.underlying.toByte
Expand Down Expand Up @@ -70,6 +77,7 @@ object HashType extends Factory[HashType] {
val num : Int32 = Int32(int)
fromNumber(num)
}
def apply (byte : Byte) : HashType = fromByte(byte)
}

/**
Expand Down
36 changes: 14 additions & 22 deletions src/test/scala/org/bitcoins/core/script/crypto/HashTypeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class HashTypeTest extends FlatSpec with MustMatchers {
}

it must "find a hash type by its byte value" in {
HashType(Seq(0.toByte)) must be (SIGHASH_ALL(Int32.zero))
HashType(Seq(1.toByte)) must be (SIGHASH_ALL(Int32.one))
HashType(Seq(2.toByte)) must be (SIGHASH_NONE.defaultValue)
HashType(Seq(3.toByte)) must be (SIGHASH_SINGLE.defaultValue)
HashType(Int32(0x80).bytes) must be (SIGHASH_ANYONECANPAY.defaultValue)
HashType(0.toByte) must be (SIGHASH_ALL(Int32.zero))
HashType(1.toByte) must be (SIGHASH_ALL(Int32.one))
HashType(2.toByte) must be (SIGHASH_NONE.defaultValue)
HashType(3.toByte) must be (SIGHASH_SINGLE.defaultValue)
HashType(0x80) must be (SIGHASH_ANYONECANPAY.defaultValue)

}

Expand All @@ -38,6 +38,7 @@ class HashTypeTest extends FlatSpec with MustMatchers {
//1190874345 & 0x80 = 0x80
val num = Int32(1190874345)
HashType(num) must be (SIGHASH_ANYONECANPAY(num))
HashType(num.bytes) must be (SIGHASH_ANYONECANPAY(num.bytes))
}

it must "determine if a given number is of hashType SIGHASH_ALL" in {
Expand All @@ -50,13 +51,13 @@ class HashTypeTest extends FlatSpec with MustMatchers {
}

it must "return the correct byte for a given hashtype" in {
HashType.byte(SIGHASH_ALL.defaultValue) must be (0x01.toByte)
HashType.byte(SIGHASH_NONE.defaultValue) must be (0x02.toByte)
HashType.byte(SIGHASH_SINGLE.defaultValue) must be (0x03.toByte)
HashType.byte(SIGHASH_ANYONECANPAY.defaultValue) must be (0x80.toByte)
HashType.byte(SIGHASH_ALL_ANYONECANPAY.defaultValue) must be (0x81.toByte)
HashType.byte(SIGHASH_NONE_ANYONECANPAY.defaultValue) must be (0x82.toByte)
HashType.byte(SIGHASH_SINGLE_ANYONECANPAY.defaultValue) must be (0x83.toByte)
SIGHASH_ALL.byte must be (0x01.toByte)
SIGHASH_NONE.byte must be (0x02.toByte)
SIGHASH_SINGLE.byte must be (0x03.toByte)
SIGHASH_ANYONECANPAY.byte must be (0x80.toByte)
SIGHASH_ALL_ANYONECANPAY.byte must be (0x81.toByte)
SIGHASH_NONE_ANYONECANPAY.byte must be (0x82.toByte)
SIGHASH_SINGLE_ANYONECANPAY.byte must be (0x83.toByte)
}

it must "intercept require statements for each hashType with illegal inputs" in {
Expand Down Expand Up @@ -84,6 +85,7 @@ class HashTypeTest extends FlatSpec with MustMatchers {
}

it must "find each specific hashType from byte sequence of default value" in {
//tests each hashtypes overriding fromBytes function
SIGHASH_ALL(SIGHASH_ALL.defaultValue.num.bytes).isInstanceOf[SIGHASH_ALL] must be (true)
SIGHASH_NONE(SIGHASH_NONE.defaultValue.num.bytes).isInstanceOf[SIGHASH_NONE] must be (true)
SIGHASH_SINGLE(SIGHASH_SINGLE.defaultValue.num.bytes).isInstanceOf[SIGHASH_SINGLE] must be (true)
Expand All @@ -93,16 +95,6 @@ class HashTypeTest extends FlatSpec with MustMatchers {
SIGHASH_SINGLE_ANYONECANPAY(SIGHASH_SINGLE_ANYONECANPAY.defaultValue.num.bytes).isInstanceOf[SIGHASH_SINGLE_ANYONECANPAY] must be (true)
}

it must "verify default byte value of each hashtype" in {
SIGHASH_ALL.byte must be (0x01.toByte)
SIGHASH_NONE.byte must be (0x02.toByte)
SIGHASH_SINGLE.byte must be (0x03.toByte)
SIGHASH_ANYONECANPAY.byte must be (0x80.toByte)
SIGHASH_ALL_ANYONECANPAY.byte must be (0x81.toByte)
SIGHASH_NONE_ANYONECANPAY.byte must be (0x82.toByte)
SIGHASH_SINGLE_ANYONECANPAY.byte must be (0x83.toByte)
}

it must "find a hashtype with only an integer" in {
HashType(105512910).isInstanceOf[SIGHASH_ANYONECANPAY] must be (true)
}
Expand Down

0 comments on commit 521cc04

Please sign in to comment.