mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-25 07:17:32 +01:00
Adding .toInt function inside of NumberUtil
This commit is contained in:
parent
c80be9e810
commit
a7a7850966
2 changed files with 83 additions and 19 deletions
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue