Skip to content

Commit

Permalink
Merge pull request #5 from metalurgical/prefer_web3j
Browse files Browse the repository at this point in the history
refactor: pass in web3j to reduce overall parameters in functions.
  • Loading branch information
grvgoel81 authored Sep 12, 2023
2 parents 52305b3 + c7a5a8f commit d127441
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 83 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -74,48 +69,31 @@ 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);
EthereumTssAccount account = new EthereumTssAccount(params);
// 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);
EthereumTssAccount account = new EthereumTssAccount(params);
// 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 = "";
Expand All @@ -83,7 +91,7 @@ public String signLegacyTransaction(BigInteger chainID, String toAddress, Double
}

RawTransaction rawTransaction = RawTransaction.createTransaction(
chainID,
chainId,
nonce,
gasLimit,
toAddress,
Expand All @@ -110,16 +118,27 @@ 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) {
txData = data;
}

RawTransaction rawTransaction = RawTransaction.createTransaction(
chainID.longValue(),
chainId.longValue(),
nonce,
gasLimit,
toAddress,
Expand All @@ -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,
Expand All @@ -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<BigInteger, BigInteger, Byte> sign(String hash) throws TSSClientError, CustomSigningError {
TSSClient client;
Map<String, String> coeffs;
Expand Down

This file was deleted.

0 comments on commit d127441

Please sign in to comment.