Skip to content

Commit

Permalink
Merge pull request #126 from bianjieai/sv/evm
Browse files Browse the repository at this point in the history
evm
  • Loading branch information
prolenking authored Aug 18, 2023
2 parents d2e24d4 + 09285ad commit f75a010
Show file tree
Hide file tree
Showing 15 changed files with 814 additions and 195 deletions.
36 changes: 31 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.bianjieai</groupId>
<artifactId>opb-sdk</artifactId>
<version>0.1.14</version>
<version>0.1.15</version>
<url>https://github.com/bianjieai/opb-sdk-java</url>
<name>opb-sdk</name>
<licenses>
Expand Down Expand Up @@ -176,12 +176,32 @@
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>crypto</artifactId>
<version>5.0.0</version>
<artifactId>core</artifactId>
<version>4.10.2</version>
<exclusions>
<exclusion>
<artifactId>bcprov-jdk15on</artifactId>
<groupId>org.bouncycastle</groupId>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</exclusion>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
</exclusion>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand All @@ -195,6 +215,12 @@
<artifactId>Java-WebSocket</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.11.6</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/irita/sdk/client/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.InvalidProtocolBufferException;
import io.grpc.Channel;
import io.grpc.ManagedChannel;
import irita.sdk.config.ClientConfig;
import irita.sdk.config.OpbConfig;
import irita.sdk.constant.enums.BroadcastMode;
import irita.sdk.exception.IritaSDKException;
import irita.sdk.key.KeyInfo;
import irita.sdk.key.KeyManager;
Expand Down Expand Up @@ -81,6 +81,10 @@ public ResultTx buildAndSend(List<GeneratedMessageV3> msgs, BaseTx baseTx, Accou
return getRpcClient().broadcastTx(txBytes, baseTx.getMode());
}

public ResultTx buildAndSendEvm(byte[] txBytes, BroadcastMode mode) throws IOException {
return getRpcClient().broadcastTx(txBytes, mode);
}

public String buildTxHash(List<GeneratedMessageV3> msgs, BaseTx baseTx, Account account) {
if (account == null) {
account = queryAccount(baseTx);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/irita/sdk/client/IritaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import irita.sdk.key.KeyManager;
import irita.sdk.module.bank.BankClient;
import irita.sdk.module.community_gov.CommunityGovClient;
import irita.sdk.module.evm.EvmClient;
import irita.sdk.module.feegrant.FeeGrantClient;
import irita.sdk.module.identity.IdentityClient;
import irita.sdk.module.mt.MtClient;
Expand All @@ -28,6 +29,7 @@ public class IritaClient {
private ServiceClient serviceClient;
private FeeGrantClient feeGrantClient;
private MtClient mtClient;
private EvmClient evmClient;

private IritaClient() {
}
Expand All @@ -46,6 +48,7 @@ public IritaClient(ClientConfig clientConfig, OpbConfig opbConfig, KeyManager km
this.serviceClient = new ServiceClient(baseClient);
this.feeGrantClient = new FeeGrantClient(baseClient);
this.mtClient = new MtClient(baseClient);
this.evmClient = new EvmClient(baseClient);
}

public BaseClient getBaseClient() {
Expand Down Expand Up @@ -154,4 +157,12 @@ public MtClient getMtClient() {
public void setMtClient(MtClient mtClient) {
this.mtClient = mtClient;
}

public EvmClient getEvmClient() {
return evmClient;
}

public void setEvmClient(EvmClient evmClient) {
this.evmClient = evmClient;
}
}
1 change: 1 addition & 0 deletions src/main/java/irita/sdk/constant/enums/MsgEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public enum MsgEnum {
MSG_LEGACY_TX("ethermint.evm.v1.LegacyTx", proto.ethermint.evm.v1.Tx.LegacyTx.class),
MSG_ACCESS_LIST_TX("ethermint.evm.v1.AccessListTx", proto.ethermint.evm.v1.Tx.AccessListTx.class),
MSG_DYNAMIC_FEE_TX("ethermint.evm.v1.DynamicFeeTx", proto.ethermint.evm.v1.Tx.DynamicFeeTx.class),
MSG_EXTENSION_OPTIONS_ETHERMINT_TX("ethermint.evm.v1.ExtensionOptionsEthereumTx", proto.ethermint.evm.v1.Tx.ExtensionOptionsEthereumTx.class),

;

Expand Down
90 changes: 90 additions & 0 deletions src/main/java/irita/sdk/crypto/eth/KeyPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package irita.sdk.crypto.eth;

import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;

import java.math.BigInteger;
import java.security.KeyPairGenerator;
import java.util.Arrays;
import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;

public class KeyPair {

private final SECPPrivateKey privateKey;
private final SECPPublicKey publicKey;

public KeyPair(final SECPPrivateKey privateKey, final SECPPublicKey publicKey) {
checkNotNull(privateKey);
checkNotNull(publicKey);
this.privateKey = privateKey;
this.publicKey = publicKey;
}

public static KeyPair generate(final KeyPairGenerator keyPairGenerator, final String algorithm) {
final java.security.KeyPair rawKeyPair = keyPairGenerator.generateKeyPair();
final BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate();
final BCECPublicKey publicKey = (BCECPublicKey) rawKeyPair.getPublic();

final BigInteger privateKeyValue = privateKey.getD();

// Ethereum does not use encoded public keys like bitcoin - see
// https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
// Additionally, as the first bit is a constant prefix (0x04) we ignore this value
final byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
final BigInteger publicKeyValue =
new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

return new KeyPair(
SECPPrivateKey.create(privateKeyValue, algorithm),
SECPPublicKey.create(publicKeyValue, algorithm));
}

@Override
public int hashCode() {
return Objects.hash(privateKey, publicKey);
}

@Override
public boolean equals(final Object other) {
if (!(other instanceof KeyPair)) {
return false;
}

final KeyPair that = (KeyPair) other;
return this.privateKey.equals(that.privateKey) && this.publicKey.equals(that.publicKey);
}

@Override
public String toString() {
return ("KeyPair{privateKey: "
+ this.privateKey.toString()
+ ", publicKey: "
+ this.publicKey.toString()
+ "]");
}

public SECPPrivateKey getPrivateKey() {
return privateKey;
}

public SECPPublicKey getPublicKey() {
return publicKey;
}
}
Loading

0 comments on commit f75a010

Please sign in to comment.