-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use first 4 bits as type for address
- Loading branch information
Showing
5 changed files
with
172 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package conflux.web3j.types; | ||
|
||
import java.util.Optional; | ||
|
||
import org.web3j.utils.Numeric; | ||
|
||
public class Address { | ||
|
||
private static final int HEX_LENGTH_WITH_PREFIX = 42; | ||
|
||
public static void validate(String hexValue) throws AddressException { | ||
validate(hexValue, null); | ||
} | ||
|
||
public static void validate(String hexValue, AddressType expectedType) throws AddressException { | ||
if (!Numeric.containsHexPrefix(hexValue)) { | ||
throw AddressException.INVALID_PREFIX; | ||
} | ||
|
||
if (hexValue.length() != HEX_LENGTH_WITH_PREFIX) { | ||
throw AddressException.INVALID_LENGTH; | ||
} | ||
|
||
Optional<AddressType> type = AddressType.parse(hexValue.charAt(2)); | ||
if (!type.isPresent()) { | ||
throw AddressException.INVALID_TYPE; | ||
} | ||
|
||
if (expectedType != null && !type.get().equals(expectedType)) { | ||
throw expectedType.getTypeMismatchException(); | ||
} | ||
|
||
for (int i = 2; i < HEX_LENGTH_WITH_PREFIX; i++) { | ||
char ch = hexValue.charAt(i); | ||
if (ch < '0' || (ch > '9' && ch < 'A') || (ch > 'Z' && ch < 'a') || ch > 'z') { | ||
throw AddressException.INVALID_HEX; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package conflux.web3j.types; | ||
|
||
public class AddressException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 2338294090416527939L; | ||
|
||
public static final AddressException INVALID_PREFIX = new AddressException("HEX prefix 0x missed"); | ||
public static final AddressException INVALID_LENGTH = new AddressException("wrong length"); | ||
public static final AddressException INVALID_TYPE = new AddressException("wrong type"); | ||
public static final AddressException INVALID_HEX = new AddressException("wrong HEX format"); | ||
|
||
private String reason; | ||
|
||
public AddressException() { | ||
} | ||
|
||
public AddressException(String reason) { | ||
super(String.format("invalid address (%s)", reason)); | ||
|
||
this.reason = reason; | ||
} | ||
|
||
public String getReason() { | ||
return reason; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package conflux.web3j.types; | ||
|
||
import java.util.Optional; | ||
|
||
import org.web3j.utils.Numeric; | ||
|
||
public enum AddressType { | ||
User('1', new AddressException("user address type required")), | ||
Contract('8', new AddressException("contract address type required")); | ||
|
||
private char value; | ||
private AddressException typeMismatchException; | ||
|
||
private AddressType(char value, AddressException ae) { | ||
this.value = value; | ||
this.typeMismatchException = ae; | ||
} | ||
|
||
public char getValue() { | ||
return this.value; | ||
} | ||
|
||
public AddressException getTypeMismatchException() { | ||
return typeMismatchException; | ||
} | ||
|
||
public String normalize(String address) { | ||
return String.format("0x%s%s", this.value, Numeric.cleanHexPrefix(address).substring(1)); | ||
} | ||
|
||
public static Optional<AddressType> parse(char ch) { | ||
for (AddressType type : AddressType.values()) { | ||
if (type.value == ch) { | ||
return Optional.of(type); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
} |