From c7a5a8f37a8c2f48db2d4d69613571b405a9d8ee Mon Sep 17 00:00:00 2001
From: metalurgical <97008724+metalurgical@users.noreply.github.com>
Date: Tue, 12 Sep 2023 10:40:55 +0200
Subject: [PATCH] refactor: pass in web3j to reduce overall parameters in
functions.
---
.../ExampleInstrumentedTest.java | 26 ------------
.../MpcProviderTests.java | 30 ++------------
.../EthereumTssAccount.java | 40 ++++++++++++-------
.../ExampleUnitTest.java | 17 --------
4 files changed, 30 insertions(+), 83 deletions(-)
delete mode 100644 web3-android-mpc-provider/src/androidTest/java/com/web3auth/web3_android_mpc_provider/ExampleInstrumentedTest.java
delete mode 100644 web3-android-mpc-provider/src/test/java/com/web3auth/web3_android_mpc_provider/ExampleUnitTest.java
diff --git a/web3-android-mpc-provider/src/androidTest/java/com/web3auth/web3_android_mpc_provider/ExampleInstrumentedTest.java b/web3-android-mpc-provider/src/androidTest/java/com/web3auth/web3_android_mpc_provider/ExampleInstrumentedTest.java
deleted file mode 100644
index a549ecb..0000000
--- a/web3-android-mpc-provider/src/androidTest/java/com/web3auth/web3_android_mpc_provider/ExampleInstrumentedTest.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.web3auth.web3_android_mpc_provider;
-
-import static org.junit.Assert.assertEquals;
-
-import android.content.Context;
-
-import androidx.test.ext.junit.runners.AndroidJUnit4;
-import androidx.test.platform.app.InstrumentationRegistry;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-/**
- * Instrumented test, which will execute on an Android device.
- *
- * @see Testing documentation
- */
-@RunWith(AndroidJUnit4.class)
-public class ExampleInstrumentedTest {
- @Test
- public void useAppContext() {
- // Context of the app under test.
- Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
- assertEquals("com.web3auth.web3_android_mpc_provider.test", appContext.getPackageName());
- }
-}
\ No newline at end of file
diff --git a/web3-android-mpc-provider/src/androidTest/java/com/web3auth/web3_android_mpc_provider/MpcProviderTests.java b/web3-android-mpc-provider/src/androidTest/java/com/web3auth/web3_android_mpc_provider/MpcProviderTests.java
index 9cc56ed..672a426 100644
--- a/web3-android-mpc-provider/src/androidTest/java/com/web3auth/web3_android_mpc_provider/MpcProviderTests.java
+++ b/web3-android-mpc-provider/src/androidTest/java/com/web3auth/web3_android_mpc_provider/MpcProviderTests.java
@@ -7,15 +7,10 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.web3j.protocol.Web3j;
-import org.web3j.protocol.core.DefaultBlockParameterName;
-import org.web3j.protocol.core.methods.response.EthChainId;
-import org.web3j.protocol.core.methods.response.EthGasPrice;
-import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
import java.math.BigInteger;
-import java.security.SignatureException;
import java.util.concurrent.ExecutionException;
@RunWith(AndroidJUnit4.class)
@@ -74,7 +69,7 @@ public void testSignTypedData() throws TSSClientError, IOException, CustomSignin
}
@Test
- public void testSigningLegacyTransaction() throws TSSClientError, CustomSigningError, ExecutionException, InterruptedException, IOException {
+ public void testSigningLegacyTransaction() throws TSSClientError, CustomSigningError, ExecutionException, InterruptedException {
EthTssAccountParams params = new EthTssAccountParams(
fullAddress, factorKey, tssNonce, tssShare, tssIndex, selected_tag, verifier, verifierId,
nodeIndexs, tssEndpoints, sigs);
@@ -82,21 +77,13 @@ public void testSigningLegacyTransaction() throws TSSClientError, CustomSigningE
// setup Web3j
String url = "https://rpc.ankr.com/eth_goerli";
Web3j web3j = Web3j.build(new HttpService(url));
- EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
- account.evmAddress,
- DefaultBlockParameterName.LATEST
- ).send();
- BigInteger nonce = ethGetTransactionCount.getTransactionCount();
BigInteger gasLimit = BigInteger.valueOf(21000);
- EthChainId chainIdResponse = web3j.ethChainId().sendAsync().get();
- BigInteger chainId = chainIdResponse.getChainId();
-
String toAddress = "0xE09543f1974732F5D6ad442dDf176D9FA54a5Be0";
- account.signLegacyTransaction(chainId, toAddress, 0.001, null, nonce, gasLimit);
+ account.signLegacyTransaction(web3j, toAddress, 0.001, null, gasLimit);
}
@Test
- public void testSigningTransaction() throws TSSClientError, CustomSigningError, SignatureException, ExecutionException, InterruptedException, IOException {
+ public void testSigningTransaction() throws TSSClientError, CustomSigningError, ExecutionException, InterruptedException, IOException {
EthTssAccountParams params = new EthTssAccountParams(
fullAddress, factorKey, tssNonce, tssShare, tssIndex, selected_tag, verifier, verifierId,
nodeIndexs, tssEndpoints, sigs);
@@ -104,18 +91,9 @@ public void testSigningTransaction() throws TSSClientError, CustomSigningError,
// setup Web3j
String url = "https://rpc.ankr.com/eth_goerli";
Web3j web3j = Web3j.build(new HttpService(url));
- EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
- account.evmAddress,
- DefaultBlockParameterName.LATEST
- ).send();
- BigInteger nonce = ethGetTransactionCount.getTransactionCount();
BigInteger gasLimit = BigInteger.valueOf(21000);
- EthChainId chainIdResponse = web3j.ethChainId().sendAsync().get();
- BigInteger chainId = chainIdResponse.getChainId();
- EthGasPrice gasPriceResponse = web3j.ethGasPrice().send();
- BigInteger gasPrice = gasPriceResponse.getGasPrice();
String toAddress = "0xE09543f1974732F5D6ad442dDf176D9FA54a5Be0";
- account.signTransaction(chainId, toAddress, 0.001, null, nonce, gasLimit, gasPrice, gasPrice);
+ account.signTransaction(web3j, toAddress, 0.001, 0.001, null, gasLimit);
}
}
diff --git a/web3-android-mpc-provider/src/main/java/com/web3auth/web3_android_mpc_provider/EthereumTssAccount.java b/web3-android-mpc-provider/src/main/java/com/web3auth/web3_android_mpc_provider/EthereumTssAccount.java
index e848d01..a4547aa 100644
--- a/web3-android-mpc-provider/src/main/java/com/web3auth/web3_android_mpc_provider/EthereumTssAccount.java
+++ b/web3-android-mpc-provider/src/main/java/com/web3auth/web3_android_mpc_provider/EthereumTssAccount.java
@@ -22,17 +22,20 @@
import org.web3j.crypto.StructuredDataEncoder;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
-import org.web3j.protocol.core.methods.response.EthSendTransaction;
+import org.web3j.protocol.core.DefaultBlockParameterName;
+import org.web3j.protocol.core.methods.response.EthChainId;
+import org.web3j.protocol.core.methods.response.EthGasPrice;
+import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;
import java.io.IOException;
import java.math.BigInteger;
-import java.security.SignatureException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.ExecutionException;
public class EthereumTssAccount {
@@ -74,7 +77,12 @@ public String signTypedData(String jsonData) throws IOException, TSSClientError,
}
- public String signLegacyTransaction(BigInteger chainID, String toAddress, Double amount, @Nullable String data, BigInteger nonce, BigInteger gasLimit) throws TSSClientError, CustomSigningError {
+ public String signLegacyTransaction(Web3j web3j, String toAddress, Double amount, @Nullable String data, BigInteger gasLimit) throws TSSClientError, CustomSigningError, ExecutionException, InterruptedException {
+ EthChainId chainIdResponse = web3j.ethChainId().sendAsync().get();
+ BigInteger chainId = chainIdResponse.getChainId();
+ EthGetTransactionCount countResponse = web3j.ethGetTransactionCount(
+ evmAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
+ BigInteger nonce = countResponse.getTransactionCount();
BigInteger value = Convert.toWei(Double.toString(amount), Convert.Unit.ETHER).toBigInteger();
String txData = "";
@@ -83,7 +91,7 @@ public String signLegacyTransaction(BigInteger chainID, String toAddress, Double
}
RawTransaction rawTransaction = RawTransaction.createTransaction(
- chainID,
+ chainId,
nonce,
gasLimit,
toAddress,
@@ -110,8 +118,19 @@ public String signLegacyTransaction(BigInteger chainID, String toAddress, Double
return Numeric.toHexString(signedTransaction);
}
- public String signTransaction(BigInteger chainID, String toAddress, Double amount, @Nullable String data, BigInteger nonce, BigInteger gasLimit, BigInteger maxPriorityFeePerGas, BigInteger maxFeePerGas) throws TSSClientError, CustomSigningError, SignatureException {
+ public String signTransaction(Web3j web3j, String toAddress, Double amount, Double minerTip, @Nullable String data, BigInteger gasLimit) throws TSSClientError, CustomSigningError, ExecutionException, InterruptedException, IOException {
+ EthChainId chainIdResponse = web3j.ethChainId().sendAsync().get();
+ BigInteger chainId = chainIdResponse.getChainId();
+ EthGetTransactionCount countResponse = web3j.ethGetTransactionCount(
+ evmAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
+ BigInteger nonce = countResponse.getTransactionCount();
+ EthGasPrice gasPriceResponse = web3j.ethGasPrice().send();
+ BigInteger gasPrice = gasPriceResponse.getGasPrice();
+
BigInteger value = Convert.toWei(Double.toString(amount), Convert.Unit.ETHER).toBigInteger();
+ BigInteger maxPriorityFeePerGas = Convert.toWei(Double.toString(minerTip), Convert.Unit.ETHER).toBigInteger();
+
+ BigInteger maxFeePerGas = gasPrice.add(maxPriorityFeePerGas);
String txData = "";
if (data != null) {
@@ -119,7 +138,7 @@ public String signTransaction(BigInteger chainID, String toAddress, Double amoun
}
RawTransaction rawTransaction = RawTransaction.createTransaction(
- chainID.longValue(),
+ chainId.longValue(),
nonce,
gasLimit,
toAddress,
@@ -136,7 +155,7 @@ public String signTransaction(BigInteger chainID, String toAddress, Double amoun
Byte v = signatureResult.getThird();
if (v < 35) {
- v = (byte) ((chainID.byteValue() * 2) + (v + 35));
+ v = (byte) ((chainId.byteValue() * 2) + (v + 35));
}
Sign.SignatureData signatureData = new Sign.SignatureData(v,
@@ -148,13 +167,6 @@ public String signTransaction(BigInteger chainID, String toAddress, Double amoun
return Numeric.toHexString(signedTransaction);
}
- public void sendTransaction(Web3j web3j, String signedTx) throws IOException, CustomSigningError {
- EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(signedTx).send();
- if (ethSendTransaction.getError() != null) {
- throw new CustomSigningError(ethSendTransaction.getError().getMessage());
- }
- }
-
private Triple sign(String hash) throws TSSClientError, CustomSigningError {
TSSClient client;
Map coeffs;
diff --git a/web3-android-mpc-provider/src/test/java/com/web3auth/web3_android_mpc_provider/ExampleUnitTest.java b/web3-android-mpc-provider/src/test/java/com/web3auth/web3_android_mpc_provider/ExampleUnitTest.java
deleted file mode 100644
index cd83df3..0000000
--- a/web3-android-mpc-provider/src/test/java/com/web3auth/web3_android_mpc_provider/ExampleUnitTest.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.web3auth.web3_android_mpc_provider;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
-
-/**
- * Example local unit test, which will execute on the development machine (host).
- *
- * @see Testing documentation
- */
-public class ExampleUnitTest {
- @Test
- public void addition_isCorrect() {
- assertEquals(4, 2 + 2);
- }
-}
\ No newline at end of file