PR #11883 - fixes to EthereumCredentials

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2024-07-05 18:59:24 +10:00
parent ac5925ad00
commit 70e619208d
1 changed files with 6 additions and 11 deletions

View File

@ -44,7 +44,10 @@ public class EthereumCredentials
{
try
{
KeyPair keyPair = generateECKeyPair();
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", provider);
ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256k1");
keyPairGenerator.initialize(ecGenParameterSpec, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
this.privateKey = keyPair.getPrivate();
this.publicKey = keyPair.getPublic();
this.address = EthereumSignatureVerifier.toAddress(((BCECPublicKey)publicKey).getQ());
@ -55,14 +58,6 @@ public class EthereumCredentials
}
}
private KeyPair generateECKeyPair() throws Exception
{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", provider);
ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256k1");
keyPairGenerator.initialize(ecGenParameterSpec, new SecureRandom());
return keyPairGenerator.generateKeyPair();
}
public String getAddress()
{
return address;
@ -122,8 +117,8 @@ public class EthereumCredentials
ECPoint publicKeyPoint = ((BCECPublicKey)publicKey).getQ();
for (int v = 0; v < 4; v++)
{
ECPoint Q = EthereumSignatureVerifier.ecRecover(hash, v, new BigInteger(1, r), new BigInteger(1, s));
if (Q != null && Q.equals(publicKeyPoint))
ECPoint qPoint = EthereumSignatureVerifier.ecRecover(hash, v, new BigInteger(1, r), new BigInteger(1, s));
if (qPoint != null && qPoint.equals(publicKeyPoint))
return (byte)v;
}
throw new RuntimeException("Could not recover public key from signature");