Alexis Hernandez
7 years ago
6 changed files with 88 additions and 40 deletions
@ -1,32 +1,26 @@ |
|||
package com.xsn.explorer.models |
|||
|
|||
import com.xsn.explorer.models.rpc.ScriptPubKey |
|||
import play.api.libs.functional.syntax._ |
|||
import play.api.libs.json.{JsObject, Reads, __} |
|||
import play.api.libs.json.{Reads, __} |
|||
|
|||
case class TransactionVOUT( |
|||
value: BigDecimal, |
|||
n: Int, |
|||
scriptPubKeyType: String, |
|||
address: Option[Address]) |
|||
scriptPubKey: Option[ScriptPubKey] = None) { |
|||
|
|||
val address: Option[Address] = scriptPubKey.flatMap(_.addresses.headOption) |
|||
} |
|||
|
|||
object TransactionVOUT { |
|||
|
|||
implicit val reads: Reads[TransactionVOUT] = { |
|||
val builder = (__ \ 'value).read[BigDecimal] and |
|||
(__ \ 'n).read[Int] and |
|||
(__ \ 'scriptPubKey).read[JsObject].map { json => |
|||
val t = (json \ "type").as[String] |
|||
val a = (json \ "addresses") |
|||
.asOpt[List[String]] |
|||
.flatMap(_.headOption) |
|||
.flatMap(Address.from) |
|||
|
|||
(t, a) |
|||
} |
|||
(__ \ 'scriptPubKey).readNullable[ScriptPubKey] |
|||
|
|||
builder.apply { (value, n, tuple) => |
|||
val (scriptType, address) = tuple |
|||
TransactionVOUT(value, n, scriptType, address) |
|||
builder.apply { (value, n, script) => |
|||
TransactionVOUT(value, n, script) |
|||
} |
|||
} |
|||
} |
|||
|
@ -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