From a7a7850966c4153b0410c5db1a9af8845f80505b Mon Sep 17 00:00:00 2001 From: Chris Stewart Date: Sun, 5 Jun 2016 15:18:39 -0500 Subject: [PATCH] Adding .toInt function inside of NumberUtil --- .../org/bitcoins/core/number/NumberType.scala | 63 ++++++++++++++++--- .../org/bitcoins/core/util/NumberUtil.scala | 39 +++++++++--- 2 files changed, 83 insertions(+), 19 deletions(-) diff --git a/src/main/scala/org/bitcoins/core/number/NumberType.scala b/src/main/scala/org/bitcoins/core/number/NumberType.scala index 2a1e188d29..48b6e75cd1 100644 --- a/src/main/scala/org/bitcoins/core/number/NumberType.scala +++ b/src/main/scala/org/bitcoins/core/number/NumberType.scala @@ -1,22 +1,65 @@ package org.bitcoins.core.number +import org.bitcoins.core.util.{BitcoinSUtil, Factory, NumberUtil} + /** * Created by chris on 6/4/16. */ -sealed trait NumberType { - def + (num : NumberType) : NumberType - def - (num : NumberType) : NumberType - def * (num : NumberType) : NumberType +sealed trait Number { + def + (num : Number) : Number = ??? + def - (num : Number) : Number = ??? + def * (num : Number) : Number = ??? } -sealed trait SignedNumberType extends NumberType +sealed trait SignedNumber extends Number -sealed trait UnsignedNumberType extends NumberType +object SignedNumber extends Factory[SignedNumber] { + private case class SignedNumberImpl(underlying : BigInt) extends SignedNumber + override def fromBytes(bytes : Seq[Byte]): SignedNumber = SignedNumberImpl(BigInt(bytes.toArray)) +} -sealed trait UInt32 extends UnsignedNumberType +sealed trait UnsignedNumber extends Number -sealed trait UInt64 extends UnsignedNumberType +object UnsignedNumber extends Factory[UnsignedNumber] { + private case class UnsignedNumberImpl(underlying : BigInt) extends UnsignedNumber -sealed trait Int32 extends SignedNumberType + override def fromBytes(bytes : Seq[Byte]): UnsignedNumber = UnsignedNumberImpl(BigInt(bytes.toArray)) +} -sealed trait Int64 extends SignedNumberType +sealed trait UInt32 extends UnsignedNumber { + def underlying : Long +} + +object UInt32 extends Factory[UInt32] { + private case class UInt32Impl(underlying : Long) extends UInt32 + + override def fromBytes(bytes : Seq[Byte]): UInt32 = UInt32Impl(NumberUtil.toLong(bytes.toArray)) +} + +sealed trait UInt64 extends UnsignedNumber { + def underlying : BigInt +} + +object UInt64 extends Factory[UInt64] { + private case class UInt64Impl(underlying : BigInt) extends UInt64 + + override def fromBytes(bytes : Seq[Byte]): UInt64 = UInt64Impl(BigInt(bytes.toArray)) +} + +sealed trait Int32 extends SignedNumber { + def underlying : Int +} +object Int32 extends Factory[Int32] { + private case class Int32Impl(underlying : Int) extends Int32 + + override def fromBytes(bytes : Seq[Byte]): Int32 = Int32Impl(NumberUtil.toInt(bytes)) +} + +sealed trait Int64 extends SignedNumber { + def underlying : Long +} +object Int64 extends Factory[Int64] { + private case class Int64Impl(underlying : Long) extends Int64 + + override def fromBytes(bytes : Seq[Byte]): Int64 = Int64Impl(NumberUtil.toLong(bytes)) +} diff --git a/src/main/scala/org/bitcoins/core/util/NumberUtil.scala b/src/main/scala/org/bitcoins/core/util/NumberUtil.scala index 08579108b2..54b6d23458 100644 --- a/src/main/scala/org/bitcoins/core/util/NumberUtil.scala +++ b/src/main/scala/org/bitcoins/core/util/NumberUtil.scala @@ -2,7 +2,6 @@ package org.bitcoins.core.util import org.bitcoins.core.protocol.script.{ScriptPubKey, ScriptSignature} import org.bitcoins.core.protocol.{CompactSizeUInt, CompactSizeUIntImpl} -import org.slf4j.LoggerFactory /** * Created by chris on 2/8/16. @@ -10,19 +9,21 @@ import org.slf4j.LoggerFactory trait NumberUtil extends BitcoinSLogger { /** - * Takes a hex number and converts it into a signed number - * used in the bitcoin numbering system - * @param hex - * @return + * Takes a hex number and converts it into a signed number + * used in the bitcoin numbering system. + * This function interprets the bytes as little endian numbers + * @param hex + * @return */ def toLong(hex : String) : Long = toLong(BitcoinSUtil.decodeHex(hex)) /** - * Takes a list of bytes and converts it in to signed number inside of bitcoins - * numbering system - * @param bytes - * @return + * Takes a sequence of bytes and converts it in to signed number inside of bitcoins + * numbering system + * This function interprets the bytes as little endian numbers + * @param bytes + * @return */ def toLong(bytes : Seq[Byte]) : Long = { val reversedBytes = bytes.reverse @@ -60,6 +61,24 @@ trait NumberUtil extends BitcoinSLogger { } } + /** + * Takes in a sequence of bytes and converts it into a signed number + * @param bytes + * @return + */ + def toInt(bytes : Seq[Byte]) : Int = { + require(bytes.size <= 4, "We cannot have an integer with more than 4 bytes (32 bits)") + toLong(bytes).toInt + } + + /** + * Takes in a hex string and converts it into a signed number + * This function interprets the bytes as little endian numbers + * @param hex + * @return + */ + def toInt(hex : String) : Int = toInt(BitcoinSUtil.decodeHex(hex)) + /** * Determines if a given hex string is a positive number * @param hex @@ -205,3 +224,5 @@ trait NumberUtil extends BitcoinSLogger { private def parseLong(bytes : Seq[Byte]) : Long = parseLong(bytes.toList) } + +object NumberUtil extends NumberUtil