Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[MINOR] added fromHexStringStrict to check for exactly 20 byte addresses #1128

Merged
merged 14 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public static Address extract(final Hash hash) {
*
* @param str An hexadecimal string (with or without the leading '0x') representing a valid
* account address.
* @return The parsed address.
* @throws NullPointerException if the provided string is {@code null}.
* @return The parsed address: {@code null} if the provided string is {@code null}.
* @throws IllegalArgumentException if the string is either not hexadecimal, or not the valid
* representation of an address.
*/
Expand All @@ -96,6 +95,21 @@ public static Address fromHexString(final String str) {
return new Address(BytesValue.fromHexStringLenient(str, SIZE));
}

/**
* Parse an hexadecimal string representing an account address.
*
* @param str An hexadecimal string representing a valid account address (strictly 20 bytes).
* @return The parsed address.
* @throws IllegalArgumentException if the provided string is {@code null}.
* @throws IllegalArgumentException if the string is either not hexadecimal, or not the valid
* representation of a 20 byte address.
*/
public static Address fromHexStringStrict(final String str) {
checkArgument(str != null);

return new Address(BytesValue.fromHexString(str));
}

private static Address precompiled(final int value) {
// Keep it simple while we don't need precompiled above 127.
checkArgument(value < Byte.MAX_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ public void accountAddresHashCode() {
public void invalidAccountAddress() {
Address.wrap(BytesValue.fromHexString("0x00101010"));
}

@Test(expected = IllegalArgumentException.class)
public void tooShortAccountAddress() {
Address.fromHexStringStrict("0x00101010");
}

@Test(expected = IllegalArgumentException.class)
public void nullAccountAddress() {
Address.fromHexStringStrict(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public void parse(
commandLine.addSubcommand(
RLPSubCommand.COMMAND_NAME, new RLPSubCommand(resultHandler.out(), in));

commandLine.registerConverter(Address.class, Address::fromHexString);
commandLine.registerConverter(Address.class, Address::fromHexStringStrict);
commandLine.registerConverter(BytesValue.class, BytesValue::fromHexString);
commandLine.registerConverter(Level.class, Level::valueOf);
commandLine.registerConverter(SyncMode.class, SyncMode::fromString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ public void permissionsEnabledWithInvalidContractAddressMustError() {
assertThat(commandOutput.toString()).isEmpty();
}

@Test
public void permissionsEnabledWithTooShortContractAddressMustError() {
parseCommand(
"--permissions-nodes-contract-enabled", "--permissions-nodes-contract-address", "0x1234");

verifyZeroInteractions(mockRunnerBuilder);

assertThat(commandErrorOutput.toString()).contains("Invalid value");
assertThat(commandOutput.toString()).isEmpty();
}

@Test
public void permissionsSmartContractMustUseOption() {

Expand Down Expand Up @@ -1800,7 +1811,7 @@ public void pantheonDoesNotStartInMiningModeIfCoinbaseNotSet() {

@Test
public void miningIsEnabledWhenSpecified() throws Exception {
final String coinbaseStr = String.format("%020x", 1);
final String coinbaseStr = String.format("%040x", 1);
parseCommand("--miner-enabled", "--miner-coinbase=" + coinbaseStr);

final ArgumentCaptor<MiningParameters> miningArg =
Expand Down