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 5e551f3..2f5647e 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 @@ -54,8 +54,7 @@ public void testSigningMessage() throws TSSClientError, CustomSigningError { EthereumTssAccount account = new EthereumTssAccount(params); String msg = "hello world"; - String signature = account.signMessage(msg); - assertNotNull(signature); + account.signMessage(msg); } @Test @@ -65,8 +64,6 @@ public void testSigningTransaction() throws TSSClientError, CustomSigningError, nodeIndexs, tssEndpoints, sigs); EthereumTssAccount account = new EthereumTssAccount(params); String toAddress = "0x048975d4997D7578A3419851639c10318db430b6"; - String transactionHash = account.signAndSendTransaction("https://rpc.ankr.com/eth_goerli", 0.001, toAddress); - assertNotNull(transactionHash); + account.signTransaction(new BigInteger("5"), toAddress, 0.001, null, new BigInteger("0"), new BigInteger("21000" ), new BigInteger("21000"), new BigInteger("21000")); } - } 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 96a5844..f0bf30c 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 @@ -5,6 +5,8 @@ import android.util.Base64; import android.util.Pair; +import androidx.annotation.Nullable; + import com.web3auth.tss_client_android.client.EndpointsData; import com.web3auth.tss_client_android.client.TSSClient; import com.web3auth.tss_client_android.client.TSSClientError; @@ -19,12 +21,7 @@ import org.web3j.crypto.Sign; import org.web3j.crypto.TransactionEncoder; 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.core.methods.response.EthSendTransaction; -import org.web3j.protocol.http.HttpService; import org.web3j.utils.Convert; import org.web3j.utils.Numeric; @@ -34,7 +31,6 @@ import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutionException; public class EthereumTssAccount { @@ -63,30 +59,28 @@ public String signMessage(String message) throws TSSClientError, CustomSigningEr return TSSHelpers.hexSignature(signatureResult.getFirst(), signatureResult.getSecond(), v); } - public String signAndSendTransaction(String url, Double amount, String toAddress) throws TSSClientError, CustomSigningError, IOException, ExecutionException, InterruptedException { - //setup Web3j - Web3j web3j = Web3j.build(new HttpService(url)); - EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount( - evmAddress, - DefaultBlockParameterName.LATEST - ).send(); - BigInteger nonce = ethGetTransactionCount.getTransactionCount(); + //todo: Method for signing eip712 structured data + + //todo Method for signing eip1559 transactions, note that v follows a formula here and is not just modified with 27 + + public String signTransaction(BigInteger chainID, String toAddress, Double amount, @Nullable String data, BigInteger nonce, BigInteger gasLimit, BigInteger maxPriorityFeePerGas, BigInteger maxFeePerGas) throws TSSClientError, CustomSigningError { BigInteger value = Convert.toWei(Double.toString(amount), Convert.Unit.ETHER).toBigInteger(); - BigInteger gasLimit = BigInteger.valueOf(21000); - EthGasPrice gasPriceResponse = web3j.ethGasPrice().send(); - BigInteger gasPrice = gasPriceResponse.getGasPrice(); - EthChainId chainIdResponse = web3j.ethChainId().sendAsync().get(); - BigInteger chainId = chainIdResponse.getChainId(); + + // todo: this appears to be a bug in web3j, if data is null it throws but is marked as nullable + String txData = ""; + if (data != null) { + txData = data; + } RawTransaction rawTransaction = RawTransaction.createTransaction( - chainId.longValue(), + chainID.longValue(), nonce, gasLimit, toAddress, value, - "", - gasPrice, - gasPrice + txData, + maxPriorityFeePerGas, + maxFeePerGas ); byte[] encodedTransaction = TransactionEncoder.encode(rawTransaction); @@ -99,14 +93,13 @@ public String signAndSendTransaction(String url, Double amount, String toAddress signatureResult.getFirst().toByteArray()); byte[] signedMsg = TransactionEncoder.encode(rawTransaction, signatureData); - String finalSig = Numeric.toHexString(signedMsg); - - EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(finalSig).send(); + return Numeric.toHexString(signedMsg); + } + 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()); - } else { - return ethSendTransaction.getTransactionHash(); } }