Skip to content

Commit

Permalink
update file
Browse files Browse the repository at this point in the history
  • Loading branch information
sin3A committed Aug 17, 2023
1 parent 6bd6d03 commit 442745d
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 0 deletions.
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;
}
}
87 changes: 87 additions & 0 deletions src/main/java/irita/sdk/crypto/eth/SECPPrivateKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;

import java.math.BigInteger;

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

public class SECPPrivateKey implements java.security.PrivateKey {

private final Bytes32 encoded;
private final String algorithm;

private SECPPrivateKey(final Bytes32 encoded, final String algorithm) {
checkNotNull(encoded);
checkNotNull(algorithm);
this.encoded = encoded;
this.algorithm = algorithm;
}

public static SECPPrivateKey create(final BigInteger key, final String algorithm) {
checkNotNull(key);
return create(UInt256.valueOf(key), algorithm);
}

public static SECPPrivateKey create(final Bytes32 key, final String algorithm) {
return new SECPPrivateKey(key, algorithm);
}

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

final SECPPrivateKey that = (SECPPrivateKey) other;
return this.encoded.equals(that.encoded) && this.algorithm.equals(that.algorithm);
}

@Override
public byte[] getEncoded() {
return encoded.toArrayUnsafe();
}

public Bytes32 getEncodedBytes() {
return encoded;
}

public BigInteger getD() {
return encoded.toUnsignedBigInteger();
}

@Override
public String getAlgorithm() {
return algorithm;
}

@Override
public String getFormat() {
return null;
}

@Override
public int hashCode() {
return encoded.hashCode();
}

@Override
public String toString() {
return encoded.toString();
}
}
31 changes: 31 additions & 0 deletions src/main/java/irita/sdk/model/Error.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package irita.sdk.model;

public class Error {
private Integer code;
private String message;
private String data;

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}
95 changes: 95 additions & 0 deletions src/test/java/irita/sdk/Counter_sol_Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package irita.sdk;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;

/**
* <p>Auto generated code.
* <p><strong>Do not modify!</strong>
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 1.5.0.
*/
@SuppressWarnings("rawtypes")
public class Counter_sol_Counter extends Contract {
public static final String BINARY = "Bin file was not provided";

public static final String FUNC_ADD = "add";

public static final String FUNC_GETCOUNTER = "getCounter";

public static final String FUNC_SUBTRACT = "subtract";

@Deprecated
protected Counter_sol_Counter(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}

protected Counter_sol_Counter(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
}

@Deprecated
protected Counter_sol_Counter(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

protected Counter_sol_Counter(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
}

public RemoteFunctionCall<TransactionReceipt> add() {
final Function function = new Function(
FUNC_ADD,
Arrays.<Type>asList(),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<BigInteger> getCounter() {
final Function function = new Function(FUNC_GETCOUNTER,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}

public RemoteFunctionCall<TransactionReceipt> subtract() {
final Function function = new Function(
FUNC_SUBTRACT,
Arrays.<Type>asList(),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

@Deprecated
public static Counter_sol_Counter load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new Counter_sol_Counter(contractAddress, web3j, credentials, gasPrice, gasLimit);
}

@Deprecated
public static Counter_sol_Counter load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new Counter_sol_Counter(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public static Counter_sol_Counter load(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
return new Counter_sol_Counter(contractAddress, web3j, credentials, contractGasProvider);
}

public static Counter_sol_Counter load(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
return new Counter_sol_Counter(contractAddress, web3j, transactionManager, contractGasProvider);
}
}

0 comments on commit 442745d

Please sign in to comment.