Fixed some OctroCoin bugs. Tests still failing. Need fix

This commit is contained in:
Manfred Karrer 2017-03-30 09:24:30 -05:00
parent 928668004a
commit 4c8f20d237
3 changed files with 54 additions and 55 deletions

View File

@ -21,8 +21,8 @@ package io.bitsquare.gui.util.validation;
import io.bitsquare.gui.util.validation.altcoins.ByteballAddressValidator;
import io.bitsquare.gui.util.validation.altcoins.OctocoinAddressValidator;
import io.bitsquare.gui.util.validation.params.IOPParams;
import io.bitsquare.gui.util.validation.params.OctocoinParams;
import io.bitsquare.gui.util.validation.params.PivxParams;
import io.bitsquare.gui.util.validation.params.OctcoinParams;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.AddressFormatException;
import org.bitcoinj.params.MainNetParams;
@ -60,11 +60,11 @@ public final class AltCoinAddressValidator extends InputValidator {
switch (currencyCode) {
case "ETH":
// https://github.com/ethereum/web3.js/blob/master/lib/utils/utils.js#L403
if (!input.matches("^(0x)?[0-9a-fA-F]{40}$"))
if (!input.matches("^(0x)?[0-9a-fA-F]{40}$"))
return regexTestFailed;
else
return new ValidationResult(true);
// Example for BTC, though for BTC we use the BitcoinJ library address check
return new ValidationResult(true);
// Example for BTC, though for BTC we use the BitcoinJ library address check
case "BTC":
// 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}$")) {
@ -80,8 +80,8 @@ public final class AltCoinAddressValidator extends InputValidator {
} else {
return regexTestFailed;
}
case "PIVX":
if (input.matches("^[D][a-km-zA-HJ-NP-Z1-9]{25,34}$")) {
case "PIVX":
if (input.matches("^[D][a-km-zA-HJ-NP-Z1-9]{25,34}$")) {
if (verifyChecksum(input)) {
try {
new Address(PivxParams.get(), input);
@ -112,7 +112,7 @@ public final class AltCoinAddressValidator extends InputValidator {
}
case "888":
if (input.matches("^[8][a-km-zA-HJ-NP-Z1-9]{25,34}$")) {
if (return OctocoinAddressValidator.ValidateAddress(input);) {
if (OctocoinAddressValidator.ValidateAddress(input)) {
try {
new Address(OctocoinParams.get(), input);
return new ValidationResult(true);

View File

@ -1,48 +0,0 @@
package io.bitsquare.gui.util.validation.altcoins;
import java.util.Arrays;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class OctocoinAddressValidator{
private final static String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
public static boolean ValidateAddress(String addr) {
if (addr.length() < 26 || addr.length() > 35) return false;
byte[] decoded = DecodeBase58(addr, 58, 25);
if (decoded == null) return false;
byte[] hash = Sha256(decoded, 0, 21, 2);
return Arrays.equals(Arrays.copyOfRange(hash, 0, 4), Arrays.copyOfRange(decoded, 21, 25));
}
private static byte[] DecodeBase58(String input, int base, int len) {
byte[] output = new byte[len];
for (int i = 0; i < input.length(); i++) {
char t = input.charAt(i);
int p = ALPHABET.indexOf(t);
if (p == -1) return null;
for (int j = len - 1; j >= 0; j--, p /= 256) {
p += base * (output[j] & 0xFF);
output[j] = (byte) (p % 256);
}
if (p != 0) return null;
}
return output;
}
private static byte[] Sha256(byte[] data, int start, int len, int recursion) {
if (recursion == 0) return data;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(Arrays.copyOfRange(data, start, start + len));
return Sha256(md.digest(), 0, 32, recursion - 1);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
}

View File

@ -0,0 +1,47 @@
package io.bitsquare.gui.util.validation.altcoins;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class OctocoinAddressValidator {
private final static String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
public static boolean ValidateAddress(String addr) {
if (addr.length() < 26 || addr.length() > 35) return false;
byte[] decoded = decodeBase58(addr, 58, 25);
if (decoded == null) return false;
byte[] hash = getSha256(decoded, 0, 21, 2);
return hash != null && Arrays.equals(Arrays.copyOfRange(hash, 0, 4), Arrays.copyOfRange(decoded, 21, 25));
}
private static byte[] decodeBase58(String input, int base, int len) {
byte[] output = new byte[len];
for (int i = 0; i < input.length(); i++) {
char t = input.charAt(i);
int p = ALPHABET.indexOf(t);
if (p == -1) return null;
for (int j = len - 1; j >= 0; j--, p /= 256) {
p += base * (output[j] & 0xFF);
output[j] = (byte) (p % 256);
}
if (p != 0) return null;
}
return output;
}
private static byte[] getSha256(byte[] data, int start, int len, int recursion) {
if (recursion == 0) return data;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(Arrays.copyOfRange(data, start, start + len));
return getSha256(md.digest(), 0, 32, recursion - 1);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
}