Add example regex address verification and test

This commit is contained in:
Manfred Karrer 2016-12-02 17:00:51 +01:00
parent d7ed22c2b8
commit 86aac3967d
2 changed files with 80 additions and 6 deletions

View file

@ -18,7 +18,11 @@
package io.bitsquare.gui.util.validation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class AltCoinAddressValidator extends InputValidator {
private static final Logger log = LoggerFactory.getLogger(AltCoinAddressValidator.class);
private String currencyCode;
@ -32,21 +36,49 @@ public final class AltCoinAddressValidator extends InputValidator {
@Override
public ValidationResult validate(String input) {
if (currencyCode != null) {
ValidationResult validationResult = super.validate(input);
if (!validationResult.isValid || currencyCode == null) {
return validationResult;
} else {
// Validation:
// 1: With a regex checking the correct structure of an address
// 2: If the address contains a checksum, verify the checksum
ValidationResult wrongChecksum = new ValidationResult(false, "Address validation failed because checksum was not correct.");
ValidationResult wrongStructure = new ValidationResult(false, "Address validation failed because it does not match the structure of a " + currencyCode + " address.");
switch (currencyCode) {
// Example for BTC, though for BTC we use the BitcoinJ library address check
case "BTC":
log.error("" + input.length());
// taken form: https://stackoverflow.com/questions/21683680/regex-to-match-bitcoin-addresses
if (input.matches("^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$")) {
if (verifyChecksum(input))
return new ValidationResult(true);
else
return wrongStructure;
} else {
return wrongChecksum;
}
case "ZEC":
if (input != null && input.startsWith("t"))
return super.validate(input);
// We only support t addresses (transparent transactions)
if (input.startsWith("t"))
return validationResult;
else
return new ValidationResult(false, "ZEC address need to start with t. Addresses starting with z are not supported.");
default:
return super.validate(input);
log.debug("Validation for AltCoinAddress not implemented yet. currencyCode:" + currencyCode);
return validationResult;
}
} else {
return super.validate(input);
}
}
private boolean verifyChecksum(String input) {
// TODO
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private methods

View file

@ -0,0 +1,42 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.gui.util.validation;
import org.junit.Test;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertTrue;
public class AltCoinAddressValidatorTest {
@Test
public void testBTC() {
AltCoinAddressValidator validator = new AltCoinAddressValidator();
validator.setCurrencyCode("BTC");
assertTrue(validator.validate("17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem").isValid);
assertTrue(validator.validate("3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX").isValid);
assertTrue(validator.validate("1111111111111111111114oLvT2").isValid);
assertTrue(validator.validate("1BitcoinEaterAddressDontSendf59kuE").isValid);
assertFalse(validator.validate("17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhemqq").isValid);
assertFalse(validator.validate("17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYheO").isValid);
assertFalse(validator.validate("17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhek#").isValid);
}
}