Browse Source

server: Add fromHex method to Address object

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
4c54b7676f
  1. 11
      server/app/com/xsn/explorer/models/Address.scala

11
server/app/com/xsn/explorer/models/Address.scala

@ -1,8 +1,12 @@
package com.xsn.explorer.models
import javax.xml.bind.DatatypeConverter
import com.xsn.explorer.models.base.WrappedString
import play.api.libs.json._
import scala.util.Try
class Address private (val string: String) extends AnyVal with WrappedString
object Address {
@ -17,6 +21,13 @@ object Address {
}
}
def fromHex(hex: String): Option[Address] = {
Try { DatatypeConverter.parseHexBinary(hex) }
.map { bytes => new String(bytes) }
.toOption
.flatMap(from)
}
implicit val reads: Reads[Address] = Reads { json =>
json.validate[String].flatMap { string =>
from(string)

Loading…
Cancel
Save