Alexis Hernandez
7 years ago
6 changed files with 88 additions and 40 deletions
@ -1,32 +1,26 @@ |
|||||
package com.xsn.explorer.models |
package com.xsn.explorer.models |
||||
|
|
||||
|
import com.xsn.explorer.models.rpc.ScriptPubKey |
||||
import play.api.libs.functional.syntax._ |
import play.api.libs.functional.syntax._ |
||||
import play.api.libs.json.{JsObject, Reads, __} |
import play.api.libs.json.{Reads, __} |
||||
|
|
||||
case class TransactionVOUT( |
case class TransactionVOUT( |
||||
value: BigDecimal, |
value: BigDecimal, |
||||
n: Int, |
n: Int, |
||||
scriptPubKeyType: String, |
scriptPubKey: Option[ScriptPubKey] = None) { |
||||
address: Option[Address]) |
|
||||
|
val address: Option[Address] = scriptPubKey.flatMap(_.addresses.headOption) |
||||
|
} |
||||
|
|
||||
object TransactionVOUT { |
object TransactionVOUT { |
||||
|
|
||||
implicit val reads: Reads[TransactionVOUT] = { |
implicit val reads: Reads[TransactionVOUT] = { |
||||
val builder = (__ \ 'value).read[BigDecimal] and |
val builder = (__ \ 'value).read[BigDecimal] and |
||||
(__ \ 'n).read[Int] and |
(__ \ 'n).read[Int] and |
||||
(__ \ 'scriptPubKey).read[JsObject].map { json => |
(__ \ 'scriptPubKey).readNullable[ScriptPubKey] |
||||
val t = (json \ "type").as[String] |
|
||||
val a = (json \ "addresses") |
|
||||
.asOpt[List[String]] |
|
||||
.flatMap(_.headOption) |
|
||||
.flatMap(Address.from) |
|
||||
|
|
||||
(t, a) |
|
||||
} |
|
||||
|
|
||||
builder.apply { (value, n, tuple) => |
builder.apply { (value, n, script) => |
||||
val (scriptType, address) = tuple |
TransactionVOUT(value, n, script) |
||||
TransactionVOUT(value, n, scriptType, address) |
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
@ -0,0 +1,24 @@ |
|||||
|
package com.xsn.explorer.models.rpc |
||||
|
|
||||
|
import com.xsn.explorer.models.Address |
||||
|
import play.api.libs.functional.syntax._ |
||||
|
import play.api.libs.json.{Reads, __} |
||||
|
|
||||
|
case class ScriptPubKey( |
||||
|
`type`: String, |
||||
|
asm: String, |
||||
|
addresses: List[Address] |
||||
|
) |
||||
|
|
||||
|
object ScriptPubKey { |
||||
|
|
||||
|
implicit val reads: Reads[ScriptPubKey] = { |
||||
|
val builder = (__ \ 'type).read[String] and |
||||
|
(__ \ 'asm).read[String] and |
||||
|
(__ \ 'addresses).readNullable[List[Address]].map(_ getOrElse List.empty) |
||||
|
|
||||
|
builder.apply { (t, asm, addresses) => |
||||
|
ScriptPubKey(t, asm, addresses) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
package com.xsn.explorer.helpers |
||||
|
|
||||
|
import com.xsn.explorer.models.rpc.ScriptPubKey |
||||
|
import com.xsn.explorer.models.{Address, TransactionId, TransactionVOUT} |
||||
|
|
||||
|
object DataHelper { |
||||
|
|
||||
|
def createAddress(string: String) = Address.from(string).get |
||||
|
|
||||
|
def createTransactionId(string: String) = TransactionId.from(string).get |
||||
|
|
||||
|
def createTransactionVOUT(n: Int, value: BigDecimal, scriptPubKey: ScriptPubKey) = { |
||||
|
TransactionVOUT( |
||||
|
n = n, |
||||
|
value = value, |
||||
|
scriptPubKey = Some(scriptPubKey)) |
||||
|
} |
||||
|
|
||||
|
def createScriptPubKey(scriptType: String, address: Address) = { |
||||
|
ScriptPubKey(scriptType, "", List(address)) |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue