Skip to content

Commit

Permalink
server: Enable constraints on the Address model
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Dec 17, 2018
1 parent 19a711d commit f348eaf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 45 deletions.
15 changes: 4 additions & 11 deletions server/app/com/xsn/explorer/models/Address.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
32 changes: 1 addition & 31 deletions server/test/com/xsn/explorer/models/AddressSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 changes: 3 additions & 3 deletions server/test/controllers/TransactionsControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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 }"""
Expand Down

0 comments on commit f348eaf

Please sign in to comment.