-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package irita.sdk.module.evm; | ||
|
||
import com.google.protobuf.*; | ||
import io.grpc.ManagedChannel; | ||
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.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; | ||
|
||
public class EvmClient { | ||
private final BaseClient baseClient; | ||
|
||
public EvmClient(BaseClient baseClient) { | ||
this.baseClient = baseClient; | ||
} | ||
|
||
public ResultTx EthereumTx(String msg,String payer,String denom) throws IOException { | ||
RawTransaction transaction = TransactionDecoder.decode(msg); | ||
SignedRawTransaction signedRawTransaction = (SignedRawTransaction) transaction; | ||
proto.ethermint.evm.v1.Tx.LegacyTx legacyTx = proto.ethermint.evm.v1.Tx.LegacyTx.newBuilder() | ||
.setData(ByteString.copyFrom(transaction.getData().getBytes())) | ||
.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) | ||
.build(); | ||
List<GeneratedMessageV3> msgs = Collections.singletonList(tx); | ||
Tx.ExtensionOptionsEthereumTx extensionOptionsEthereumTx = Tx.ExtensionOptionsEthereumTx.newBuilder().build(); | ||
Any extensionOptionsEthereumTxAny = Any.pack(extensionOptionsEthereumTx,"/"); | ||
TxOuterClass.TxBody.Builder builder = TxOuterClass.TxBody.newBuilder(); | ||
builder.addMessages(Any.pack(tx, "/")); | ||
builder.addExtensionOptions(extensionOptionsEthereumTxAny); | ||
TxOuterClass.TxBody txBody = builder.build(); | ||
CoinOuterClass.Coin coin = CoinOuterClass.Coin.newBuilder() | ||
.setAmount(legacyTx.getGas()*Integer.parseInt(legacyTx.getGasPrice())+"") | ||
.setDenom(denom) | ||
.build(); | ||
TxOuterClass.Fee.Builder fee = TxOuterClass.Fee.newBuilder(); | ||
fee.setGasLimit(legacyTx.getGas()); | ||
fee.addAmount(coin); | ||
TxOuterClass.AuthInfo ai = TxOuterClass.AuthInfo.newBuilder() | ||
.setFee(fee) | ||
.build(); | ||
TxOuterClass.Tx txResult = TxOuterClass.Tx.newBuilder() | ||
.setBody(txBody) | ||
.setAuthInfo(ai) | ||
.build(); | ||
return baseClient.buildAndSendEvm(txResult.toByteArray(), BroadcastMode.Sync); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/irita/sdk/module/evm/MsgEthereumTxRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package irita.sdk.module.evm; | ||
|
||
import com.google.protobuf.Any; | ||
|
||
public class MsgEthereumTxRequest { | ||
|
||
private com.google.protobuf.Any value; | ||
private double size; | ||
private String from; | ||
private String hash; | ||
private String payer; | ||
|
||
public Any getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(Any value) { | ||
this.value = value; | ||
} | ||
|
||
public double getSize() { | ||
return size; | ||
} | ||
|
||
public void setSize(double size) { | ||
this.size = size; | ||
} | ||
|
||
public String getFrom() { | ||
return from; | ||
} | ||
|
||
public void setFrom(String from) { | ||
this.from = from; | ||
} | ||
|
||
public String getHash() { | ||
return hash; | ||
} | ||
|
||
public void setHash(String hash) { | ||
this.hash = hash; | ||
} | ||
|
||
public String getPayer() { | ||
return payer; | ||
} | ||
|
||
public void setPayer(String payer) { | ||
this.payer = payer; | ||
} | ||
} |