Browse Source

server: Update Address format to allow bitcoin addresses

master
Alexis Hernandez 6 years ago
parent
commit
9dd24a4212
  1. 2
      server/app/com/xsn/explorer/models/values/Address.scala
  2. 21
      server/test/com/xsn/explorer/models/AddressSpec.scala

2
server/app/com/xsn/explorer/models/values/Address.scala

@ -10,7 +10,7 @@ class Address private (val string: String) extends AnyVal with WrappedString
object Address { object Address {
private val pattern = "^[a-zA-Z0-9]{34,64}$".r.pattern private val pattern = "^[a-zA-Z0-9]{22,64}$".r.pattern
def from(string: String): Option[Address] = { def from(string: String): Option[Address] = {
if (pattern.matcher(string).matches()) { if (pattern.matcher(string).matches()) {

21
server/test/com/xsn/explorer/models/AddressSpec.scala

@ -5,23 +5,20 @@ import org.scalatest.{MustMatchers, OptionValues, WordSpec}
class AddressSpec extends WordSpec with MustMatchers with OptionValues { class AddressSpec extends WordSpec with MustMatchers with OptionValues {
val addresses = Map(
"xsn legacy" -> "19Gfgd95swmT8jmktPK19mbYw9hiBGYV4",
"P2WPKH" -> "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx5uvddt",
"bech32" -> "xc1qphyjl73szcnz3jfpjryljx8elwc6wpdqqccy3s8g57gw7e44hw4q2jqdds",
"btc address 1" -> "1111111111111111111114oLvT2",
)
"from" should { "from" should {
"allow to create a legacy address" in {
val input = "Xvjue2ZLnJwTrSLUBx7DTHaSHTdpWrxtLF"
val result = Address.from(input)
result.value.string mustEqual input
}
"allow to create a P2WPKH address" in { addresses.foreach { case (tpe, input) =>
val input = "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx5uvddt" s"allow $tpe" in {
val result = Address.from(input) val result = Address.from(input)
result.value.string mustEqual input result.value.string mustEqual input
} }
"allow to create segwit address (bech32)" in {
val input = "xc1qphyjl73szcnz3jfpjryljx8elwc6wpdqqccy3s8g57gw7e44hw4q2jqdds"
val result = Address.from(input)
result.value.string mustEqual input
} }
"reject an empty string" in { "reject an empty string" in {

Loading…
Cancel
Save