mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-20 14:03:11 +01:00
Commit add Opt/T fromBytes/fromHex methods similar to StringFactory (#2499)
This commit is contained in:
parent
27e50b2c34
commit
2c2646c78d
1 changed files with 30 additions and 0 deletions
|
@ -2,6 +2,8 @@ package org.bitcoins.crypto
|
|||
|
||||
import scodec.bits.ByteVector
|
||||
|
||||
import scala.util.Try
|
||||
|
||||
/**
|
||||
* Created by chris on 2/26/16.
|
||||
* Trait to implement ubiquitous factory functions across our codebase
|
||||
|
@ -11,12 +13,40 @@ abstract class Factory[+T] {
|
|||
/** Creates a T out of a hex string. */
|
||||
def fromHex(hex: String): T = fromBytes(CryptoBytesUtil.decodeHex(hex))
|
||||
|
||||
/** Deserializes the given hex string to a T
|
||||
* If the hex is not correct, [[None]] is returned
|
||||
*/
|
||||
def fromHexOpt(hex: String): Option[T] = {
|
||||
fromHexT(hex).toOption
|
||||
}
|
||||
|
||||
/** Deserializes the given hex string
|
||||
* if the hex is not correct, we give you a [[Failure]]
|
||||
*/
|
||||
def fromHexT(hex: String): Try[T] = {
|
||||
Try(fromHex(hex))
|
||||
}
|
||||
|
||||
/** Creates a T out of a hex string in little endian. */
|
||||
def fromHexLE(hex: String): T = fromBytesLE(CryptoBytesUtil.decodeHex(hex))
|
||||
|
||||
/** Creates a T out of a sequence of bytes. */
|
||||
def fromBytes(bytes: ByteVector): T
|
||||
|
||||
/** Deserializes the given [[ByteVector]] to a T
|
||||
* If the [[ByteVector]] is not correct, [[None]] is returned
|
||||
*/
|
||||
def fromBytesOpt(bytes: ByteVector): Option[T] = {
|
||||
fromBytesT(bytes).toOption
|
||||
}
|
||||
|
||||
/** Deserializes the given [[ByteVector]] string
|
||||
* if the [[ByteVector]] is not correct, we give you a [[Failure]]
|
||||
*/
|
||||
def fromBytesT(bytes: ByteVector): Try[T] = {
|
||||
Try(fromBytes(bytes))
|
||||
}
|
||||
|
||||
/** Creates a T out of a sequence of bytes in little endian. */
|
||||
def fromBytesLE(bytes: ByteVector): T = fromBytes(bytes.reverse)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue