Skip to content

Commit

Permalink
Fix Eth1AddressSignerIdentifier and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansaleem committed Oct 24, 2024
1 parent afc10d9 commit bfb35d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package tech.pegasys.web3signer.core;

import static org.web3j.crypto.Keys.getAddress;
import static tech.pegasys.web3signer.signing.secp256k1.EthPublicKeyUtils.ecPublicKeyToBigInteger;
import static tech.pegasys.web3signer.signing.secp256k1.EthPublicKeyUtils.toHexString;
import static tech.pegasys.web3signer.signing.secp256k1.util.AddressUtil.remove0xPrefix;

Expand All @@ -31,11 +32,7 @@ public Eth1AddressSignerIdentifier(final String address) {
}

public static SignerIdentifier fromPublicKey(final ECPublicKey publicKey) {
return new Eth1AddressSignerIdentifier(getAddress(toHexString(publicKey)));
}

public static SignerIdentifier fromPublicKey(final String publicKey) {
return new Eth1AddressSignerIdentifier(getAddress(publicKey));
return new Eth1AddressSignerIdentifier(getAddress(ecPublicKeyToBigInteger(publicKey)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
import tech.pegasys.web3signer.core.Eth1AddressSignerIdentifier;
import tech.pegasys.web3signer.signing.secp256k1.EthPublicKeyUtils;
import tech.pegasys.web3signer.signing.secp256k1.SignerIdentifier;
import tech.pegasys.web3signer.signing.secp256k1.util.AddressUtil;

import java.security.KeyPair;
import java.security.SecureRandom;
import java.security.interfaces.ECPublicKey;
import java.util.Locale;
import java.util.Random;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -42,15 +39,18 @@ static void generateKeyPair() throws Exception {

@Test
void prefixIsRemovedFromAddress() {
// web3j.crypto.Keys.getAddress() returns lower case address without 0x prefix
final String address =
Keys.getAddress(
EthPublicKeyUtils.ecPublicKeyToBigInteger((ECPublicKey) secp256k1KeyPair.getPublic()));
// forcefully convert some random alphabets to uppercase
final String mixCaseAddress = convertRandomAlphabetsToUpperCase(address);
// forcefully convert first two alphabets to uppercase and add prefix
final String mixCaseAddress = "0X" + convertAlphabetsToUpperCase(address);

final Eth1AddressSignerIdentifier signerIdentifier =
new Eth1AddressSignerIdentifier(mixCaseAddress);
assertThat(signerIdentifier.toStringIdentifier()).isEqualTo(address.toLowerCase(Locale.US));
assertThat(signerIdentifier.toStringIdentifier()).isEqualTo(address);
assertThat(signerIdentifier.toStringIdentifier()).doesNotStartWithIgnoringCase("0x");
assertThat(signerIdentifier.toStringIdentifier()).isLowerCase();
}

@Test
Expand Down Expand Up @@ -78,21 +78,22 @@ void validateFailsForNullPrimaryKey() {
void correctEth1AddressIsGeneratedFromPublicKey() {
final ECPublicKey publicKey = (ECPublicKey) secp256k1KeyPair.getPublic();
final SignerIdentifier signerIdentifier = Eth1AddressSignerIdentifier.fromPublicKey(publicKey);
final String prefixRemovedAddress =
AddressUtil.remove0xPrefix(
Keys.getAddress(EthPublicKeyUtils.toHexString(publicKey)).toLowerCase(Locale.US));
assertThat(signerIdentifier.toStringIdentifier()).isEqualTo(prefixRemovedAddress);

// web3j.crypto.Keys.getAddress() returns lower case address without 0x prefix
final String expectedAddress =
Keys.getAddress(EthPublicKeyUtils.ecPublicKeyToBigInteger(publicKey));
assertThat(signerIdentifier.toStringIdentifier()).isEqualTo(expectedAddress);
assertThat(signerIdentifier.toStringIdentifier()).doesNotStartWithIgnoringCase("0x");
assertThat(signerIdentifier.toStringIdentifier()).isLowerCase();
}

private static String convertRandomAlphabetsToUpperCase(final String input) {
public static String convertAlphabetsToUpperCase(final String input) {
final char[] chars = input.toCharArray();
final Random random = new Random();
int count = 0;

while (count < 2 || count < 3 && random.nextBoolean()) {
int index = random.nextInt(chars.length);
if (Character.isLetter(chars[index]) && Character.isLowerCase(chars[index])) {
chars[index] = Character.toUpperCase(chars[index]);
for (int i = 0; i < chars.length && count < 2; i++) {
if (Character.isLetter(chars[i]) && Character.isLowerCase(chars[i])) {
chars[i] = Character.toUpperCase(chars[i]);
count++;
}
}
Expand Down

0 comments on commit bfb35d3

Please sign in to comment.