Browse Source

server: Enable constraints on the Address model

prometheus-integration
Alexis Hernandez 6 years ago
parent
commit
f348eaf86f
  1. 15
      server/app/com/xsn/explorer/models/Address.scala
  2. 32
      server/test/com/xsn/explorer/models/AddressSpec.scala
  3. 6
      server/test/controllers/TransactionsControllerSpec.scala

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

@ -10,21 +10,14 @@ class Address private (val string: String) extends AnyVal with WrappedString
object Address {
// TODO: add support for bech32 format
private val pattern = "(^[a-zA-Z0-9]{34}$)|(^[a-zA-Z0-9]{42}$)".r.pattern
private val pattern = "^[a-zA-Z0-9]{34,64}$".r.pattern
def from(string: String): Option[Address] = {
// TODO: add validations when bech32 is supported
if (string.trim.isEmpty) {
None
} else {
if (pattern.matcher(string).matches()) {
Some(new Address(string))
} else {
None
}
// if (pattern.matcher(string).matches()) {
// Some(new Address(string))
// } else {
// None
// }
}
def fromHex(hex: String): Option[Address] = {

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

@ -17,31 +17,25 @@ class AddressSpec extends WordSpec with MustMatchers with OptionValues {
result.value.string mustEqual input
}
"allow to create segwit address" in {
"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 {
pending
val input = ""
val result = Address.from(input)
result mustEqual Option.empty
}
"reject whitespaces" in {
pending
val input = " Xvjue2ZLnJwTrSLUBx7DTHaSHTdpWrxtLF "
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid characters" in {
pending
val input = "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx.uvddt"
val result = Address.from(input)
result mustEqual Option.empty
@ -54,29 +48,5 @@ class AddressSpec extends WordSpec with MustMatchers with OptionValues {
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid address, 1 extra character" in {
pending
val input = "Xvjue2ZLnJwTrSLUBx7DTHaSHTdpWrxtLFf"
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid address, 1 character missing on P2WPKH" in {
pending
val input = "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx5uvdd"
val result = Address.from(input)
result mustEqual Option.empty
}
"reject invalid address, 1 extra character on P2WPKH" in {
pending
val input = "bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx5uvddt1"
val result = Address.from(input)
result mustEqual Option.empty
}
}
}

6
server/test/controllers/TransactionsControllerSpec.scala

@ -24,8 +24,8 @@ class TransactionsControllerSpec extends MyAPISpec {
val nonCoinbaseTx = TransactionLoader.get("0834641a7d30d8a2d2b451617599670445ee94ed7736e146c13be260c576c641")
val nonCoinbasePreviousTx = TransactionLoader.get("585cec5009c8ca19e83e33d282a6a8de65eb2ca007b54d6572167703768967d9")
val severalInputsTx = TransactionLoader.get("a3c43d22bbba31a6e5c00f565cb9c5a1a365407df4cc90efa8a865656b52c0eb")
val firstAddress = createAddress("fygsydgfygsdyfgsdyg")
val secondAddress = createAddress("56wedf5wedweedw")
val firstAddress = createAddress("Xvjue2ZLnJwTrSLUBx7DTHaSHTdpWrxtLF")
val secondAddress = createAddress("bc1qzhayf65p2j4h3pfw22aujgr5w42xfqzx5uvddt")
val firstTxId = DataHelper.createTransactionId("a3c43d223658a8656a31a6e5c407df4bbb0f565cb9c5a1acc90efa056b52c0eb")
val secondTxId = DataHelper.createTransactionId("8a865656b5a3c43d22b00f565cb9c5a1a3bba31a6e5c65407df4cc90efa2c0eb")
@ -230,7 +230,7 @@ class TransactionsControllerSpec extends MyAPISpec {
"return the latest transactions for the addresses" in {
val addresses = List(firstAddress.string, secondAddress.string, "3rdaddress")
val addresses = List(firstAddress.string, secondAddress.string, s"3rd${secondAddress.string}")
.map(x => s""" "$x" """)
.mkString("[", ",", "]")
val body = s"""{ "addresses": $addresses }"""

Loading…
Cancel
Save