Browse Source

server: Support P2WPKH address format (#33)

prometheus-integration
Alexis Hernandez 7 years ago
parent
commit
b9a035e9aa
  1. 2
      server/app/com/xsn/explorer/models/Address.scala
  2. 62
      server/test/com/xsn/explorer/models/AddressSpec.scala

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

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

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

@ -0,0 +1,62 @@
package com.xsn.explorer.models
import org.scalatest.{MustMatchers, OptionValues, WordSpec}
class AddressSpec extends WordSpec with MustMatchers with OptionValues {
"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 {
val input = "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx5uvddt"
val result = Address.from(input)
result.value.string mustEqual input
}
"reject an empty string" in {
val input = ""
val result = Address.from(input)
result mustEqual Option.empty
}
"reject whitespaces" in {
val input = " Xvjue2ZLnJwTrSLUBx7DTHaSHTdpWrxtLF "
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid characters" in {
val input = "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx.uvddt"
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid address, 1 character missing" in {
val input = "Xvjue2ZLnJwTrSLUBx7DTHaSHTdpWrxtL"
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid address, 1 extra character" in {
val input = "Xvjue2ZLnJwTrSLUBx7DTHaSHTdpWrxtLFf"
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid address, 1 character missing on P2WPKH" in {
val input = "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx5uvdd"
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid address, 1 extra character on P2WPKH" in {
val input = "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx5uvddt1"
val result = Address.from(input)
result mustEqual Option.empty
}
}
}
Loading…
Cancel
Save