Skip to content

Commit

Permalink
Merge pull request #3 from metalurgical/seperate_send
Browse files Browse the repository at this point in the history
fix: separate send and sign
  • Loading branch information
grvgoel81 authored Sep 11, 2023
2 parents 73dbdac + 70a7e00 commit 29c43b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -34,7 +31,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class EthereumTssAccount {

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}

Expand Down

0 comments on commit 29c43b7

Please sign in to comment.