Skip to content

Commit

Permalink
1. contracts to java
Browse files Browse the repository at this point in the history
2. fix transaction data conversion errors
  • Loading branch information
sin3A committed Aug 18, 2023
1 parent 442745d commit ef8ca0d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 284 deletions.
36 changes: 1 addition & 35 deletions src/main/java/irita/sdk/crypto/eth/LegacyTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public LegacyTransaction(long nonce,String gasPrice,long gasLimit,String to,Stri
this.maxPriorityFeePerGas = Optional.empty();
}

public LegacyTransaction(Tx.LegacyTx legacyTx) {
public LegacyTransaction(Tx.LegacyTx legacyTx) {
this.transactionType = TransactionType.FRONTIER;
this.nonce = legacyTx.getNonce();
this.gasPrice = Optional.of(Wei.of(Long.parseLong(legacyTx.getGasPrice())));
Expand Down Expand Up @@ -232,22 +232,6 @@ public Bytes32 decodeTransaction(){
return dataHash;
}

/*public SECPSignature sign(final KeyPair keyPair){
Bytes32 dataHash = decodeTransaction();
final ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
X9ECParameters params = SECNamedCurves.getByName("secp256k1");
ECDomainParameters curve =new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
final ECPrivateKeyParameters privKey =
new ECPrivateKeyParameters(
keyPair.getPrivateKey().getEncodedBytes().toUnsignedBigInteger(), curve);
signer.init(true, privKey);
final BigInteger[] components = signer.generateSignature(dataHash.toArrayUnsafe());
signature = getInstance().normaliseSignature(components[0], components[1], keyPair.getPublicKey(), dataHash);
return signature;
}*/

private Bytes32 getOrComputeSenderRecoveryHash() {
if (hashNoSignature == null) {
hashNoSignature =
Expand All @@ -271,8 +255,6 @@ private Bytes32 computeSenderRecoveryHash(final TransactionType transactionType,
if (transactionType.requiresChainId()) {
checkArgument(chainId.isPresent(), "Transaction type %s requires chainId", transactionType);
}
/*final Bytes preimage = frontierPreimage(nonce, gasPrice, gasLimit, to, value, payload, chainId);
return Hash.keccak256(preimage);*/
final Bytes preimage;
switch (transactionType) {
case FRONTIER:
Expand Down Expand Up @@ -434,22 +416,6 @@ public static void encodeAccessListInner(
rlpOutput.writeBytes(to.map(Bytes::copy).orElse(Bytes.EMPTY));
rlpOutput.writeUInt256Scalar(value);
rlpOutput.writeBytes(payload);
/*
Access List encoding should look like this
where hex strings represent raw bytes
[
[
"0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
[
"0x0000000000000000000000000000000000000000000000000000000000000003",
"0x0000000000000000000000000000000000000000000000000000000000000007"
]
],
[
"0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
[]
]
] */
writeAccessList(rlpOutput, Optional.of(accessList));
}

Expand Down
26 changes: 4 additions & 22 deletions src/main/java/irita/sdk/module/evm/EvmClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,15 @@
import io.grpc.stub.CallStreamObserver;
import irita.sdk.client.BaseClient;
import irita.sdk.constant.enums.BroadcastMode;
import irita.sdk.crypto.eth.Hash;
import irita.sdk.crypto.eth.LegacyTransaction;
import irita.sdk.model.Account;
import irita.sdk.model.BaseTx;
import irita.sdk.model.ResultTx;
import irita.sdk.util.HashUtils;
import irita.sdk.util.MsgParser;
import org.apache.commons.codec.Decoder;
import org.bouncycastle.util.encoders.Hex;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.SignedRawTransaction;
import org.web3j.crypto.TransactionDecoder;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.rlp.RlpDecoder;
import org.web3j.rlp.RlpList;
import org.web3j.crypto.*;
import org.web3j.utils.Numeric;
import proto.cosmos.base.v1beta1.CoinOuterClass;
import proto.cosmos.tx.v1beta1.TxOuterClass;
import proto.ethermint.evm.v1.MsgGrpc;
import proto.ethermint.evm.v1.Tx;

import java.io.IOException;
import java.util.Base64;
import java.util.Collections;
import java.util.List;

Expand All @@ -41,26 +27,22 @@ public EvmClient(BaseClient baseClient) {
public ResultTx EthereumTx(String msg,String payer,String denom) throws IOException {
RawTransaction transaction = TransactionDecoder.decode(msg);
SignedRawTransaction signedRawTransaction = (SignedRawTransaction) transaction;
byte[] data = Numeric.hexStringToByteArray(transaction.getData());
proto.ethermint.evm.v1.Tx.LegacyTx legacyTx = proto.ethermint.evm.v1.Tx.LegacyTx.newBuilder()
.setData(ByteString.copyFrom(transaction.getData().getBytes()))
.setData(ByteString.copyFrom(data))
.setGas(transaction.getGasLimit().intValue())
.setNonce(transaction.getNonce().intValue())
.setGasPrice(transaction.getGasPrice().toString())
.setTo(transaction.getTo())
.setValue(transaction.getValue()+"")
//.setGasPrice(8+"")
.setR(ByteString.copyFrom(signedRawTransaction.getSignatureData().getR()))
.setS(ByteString.copyFrom(signedRawTransaction.getSignatureData().getS()))
.setV(ByteString.copyFrom(signedRawTransaction.getSignatureData().getV()))
.build();
LegacyTransaction legacyTransaction = new LegacyTransaction(legacyTx);
System.out.println(legacyTransaction.getSender());
Tx.MsgEthereumTx tx = Tx.MsgEthereumTx.newBuilder()
.setData(Any.pack(legacyTx,""))
.setSize(msg.length())
//.setFrom(legacyTransaction.getSender())
//.setHash(HashUtils.sha256(legacyTx.toByteArray()).toString())
//.setFeePayer(payer)
.setFeePayer(payer)
.build();
List<GeneratedMessageV3> msgs = Collections.singletonList(tx);
Tx.ExtensionOptionsEthereumTx extensionOptionsEthereumTx = Tx.ExtensionOptionsEthereumTx.newBuilder().build();
Expand Down
226 changes: 0 additions & 226 deletions src/test/java/irita/sdk/EvmTest.java

This file was deleted.

Loading

0 comments on commit ef8ca0d

Please sign in to comment.