Skip to content

Commit

Permalink
Merge pull request #21 from tronprotocol/develop
Browse files Browse the repository at this point in the history
Update main to 0.2.0
  • Loading branch information
Starsakary authored Aug 5, 2021
2 parents dc38c37 + f9b359b commit 06fd05f
Show file tree
Hide file tree
Showing 17 changed files with 616 additions and 456 deletions.
1 change: 1 addition & 0 deletions trident-java/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ build/
.gradle/
.idea
.vscode
demo/
17 changes: 12 additions & 5 deletions trident-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Trident-Java document: https://developers.tron.network/docs/trident-java

Due to safety concerns, trident-java will no longer upload packaged files to maven. Please clone the code from GitHub and do the packaging.

Trident-java is compiled with java version 13.0.2+8 and gradle 5.6.4.
Trident-java is compiled with java version 1.8.0_231 and gradle 5.6.4.

## How to use

Expand Down Expand Up @@ -37,26 +37,33 @@ dependencies {
}
```

Or if you are using the jar files as your dependencies:

```groovy
dependencies {
implementation fileTree(dir:'your path', include: '*.jar')
}
```

### Maven Settings

```xml
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>abi</artifactId>
<version>0.1.1</version>
<version>0.2.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>utils</artifactId>
<version>0.1.1</version>
<version>0.2.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>core</artifactId>
<version>0.1.1</version>
<version>0.2.0</version>
<type>pom</type>
</dependency>
```

26 changes: 1 addition & 25 deletions trident-java/abi/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'com.jfrog.bintray'

}

description 'TRON Application Binary Interface (ABI) for working with smart contracts'
Expand All @@ -8,30 +8,6 @@ dependencies {
implementation project(':utils')
}

bintray {
user = System.getenv('BINTRAY_USER_TRIDENT')
key = System.getenv('BINTRAY_KEY_TRIDENT')

configurations = ['archives']

pkg {
repo = 'trident'
name = 'abi'
desc = description
websiteUrl = 'https://github.com/tronprotocol/trident'
vcsUrl = 'https://github.com/tronprotocol/trident.git'
licenses = ['Apache-2.0']
publish = true
publicDownloadNumbers = true
version {
afterEvaluate {
name = "${project.version}"
released = new Date()
}
}
}
}

tasks.create('buildLib', Jar) {
baseName = 'trident'
version = null
Expand Down
4 changes: 1 addition & 3 deletions trident-java/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
plugins {
id 'java-library'
id 'com.jfrog.bintray' apply false
id 'signing'
}

ext {
Expand All @@ -10,7 +8,7 @@ ext {
}

allprojects {
version '0.1.1'
version '0.2.0'
group = 'org.tron.trident'

repositories {
Expand Down
24 changes: 0 additions & 24 deletions trident-java/core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
plugins {
id 'com.jfrog.bintray'
id 'com.google.protobuf' version '0.8.12'
}

Expand Down Expand Up @@ -40,26 +39,3 @@ protobuf {
generatedFilesBaseDir = "$projectDir/src"
}

bintray {
user = System.getenv('BINTRAY_USER_TRIDENT')
key = System.getenv('BINTRAY_KEY_TRIDENT')

configurations = ['archives']

pkg {
repo = 'trident'
name = 'core'
desc = description
websiteUrl = 'https://github.com/tronprotocol/trident'
vcsUrl = 'https://github.com/tronprotocol/trident.git'
licenses = ['Apache-2.0']
publish = true
publicDownloadNumbers = true
version {
afterEvaluate {
name = "${project.version}"
released = new Date()
}
}
}
}
131 changes: 71 additions & 60 deletions trident-java/core/src/main/java/org/tron/trident/core/ApiWrapper.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
package org.tron.trident.core;


/**
* A {@code ApiWrapper} object is the entry point for calling the functions.
*
*<p>A {@code ApiWrapper} object is bind with a private key and a full node.
* {@link #broadcastTransaction}, {@link #signTransaction} and other transaction related
* operations can be done via a {@code ApiWrapper} object.</p>
*
* @since jdk13.0.2+8
* @see org.tron.trident.core.contract.Contract
* @see org.tron.trident.proto.Chain.Transaction
* @see org.tron.trident.proto.Contract
*/


import com.google.protobuf.InvalidProtocolBufferException;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Function;
import org.tron.trident.api.GrpcAPI.BytesMessage;

import org.tron.trident.core.contract.Contract;
import org.tron.trident.core.Constant;
import org.tron.trident.api.WalletGrpc;
import org.tron.trident.api.WalletSolidityGrpc;
import org.tron.trident.core.contract.ContractFunction;
import org.tron.trident.core.exceptions.IllegalException;
import org.tron.trident.core.key.KeyPair;
import org.tron.trident.core.transaction.TransactionBuilder;
import org.tron.trident.crypto.SECP256K1;
import org.tron.trident.proto.Chain.Transaction;
Expand All @@ -44,6 +31,7 @@
import org.tron.trident.proto.Contract.AssetIssueContract;
import org.tron.trident.proto.Contract.SetAccountIdContract;
import org.tron.trident.proto.Contract.UpdateAssetContract;
import org.tron.trident.proto.Contract.UpdateBrokerageContract;
import org.tron.trident.proto.Contract.ParticipateAssetIssueContract;
import org.tron.trident.proto.Contract.UnfreezeAssetContract;
import org.tron.trident.proto.Contract.AccountPermissionUpdateContract;
Expand Down Expand Up @@ -94,10 +82,23 @@

import static org.tron.trident.proto.Response.TransactionReturn.response_code.SUCCESS;

/**
* A {@code ApiWrapper} object is the entry point for calling the functions.
*
*<p>A {@code ApiWrapper} object is bind with a private key and a full node.
* {@link #broadcastTransaction}, {@link #signTransaction} and other transaction related
* operations can be done via a {@code ApiWrapper} object.</p>
*
* @since java version 1.8.0_231
* @see org.tron.trident.core.contract.Contract
* @see org.tron.trident.proto.Chain.Transaction
* @see org.tron.trident.proto.Contract
*/

public class ApiWrapper {
public final WalletGrpc.WalletBlockingStub blockingStub;
public final WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity;
public final SECP256K1.KeyPair keyPair;
public final KeyPair keyPair;
public final ManagedChannel channel;
public final ManagedChannel channelSolidity;

Expand All @@ -106,7 +107,7 @@ public ApiWrapper(String grpcEndpoint, String grpcEndpointSolidity, String hexPr
channelSolidity = ManagedChannelBuilder.forTarget(grpcEndpointSolidity).usePlaintext().build();
blockingStub = WalletGrpc.newBlockingStub(channel);
blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelSolidity);
keyPair = SECP256K1.KeyPair.create(SECP256K1.PrivateKey.create(Bytes32.fromHexString(hexPrivateKey)));
keyPair = new KeyPair(hexPrivateKey);
}

public ApiWrapper(String grpcEndpoint, String grpcEndpointSolidity, String hexPrivateKey, String apiKey) {
Expand All @@ -121,7 +122,7 @@ public ApiWrapper(String grpcEndpoint, String grpcEndpointSolidity, String hexPr
blockingStub = (WalletGrpc.WalletBlockingStub)MetadataUtils.attachHeaders(WalletGrpc.newBlockingStub(channel), header);
blockingStubSolidity = (WalletSolidityGrpc.WalletSolidityBlockingStub)MetadataUtils.attachHeaders(WalletSolidityGrpc.newBlockingStub(channelSolidity), header);

keyPair = SECP256K1.KeyPair.create(SECP256K1.PrivateKey.create(Bytes32.fromHexString(hexPrivateKey)));
keyPair = new KeyPair(hexPrivateKey);
}

public void close() {
Expand All @@ -142,7 +143,7 @@ public void close() {
* @return a ApiWrapper object
*/
public static ApiWrapper ofMainnet(String hexPrivateKey, String apiKey) {
return new ApiWrapper("grpc.trongrid.io:50051", "grpc.trongrid.io:50052", hexPrivateKey, apiKey);
return new ApiWrapper(Constant.TRONGRID_MAIN_NET, Constant.TRONGRID_MAIN_NET_SOLIDITY, hexPrivateKey, apiKey);
}

/**
Expand All @@ -154,9 +155,9 @@ public static ApiWrapper ofMainnet(String hexPrivateKey, String apiKey) {
* @param apiKey this function works with TronGrid, an API key is required.
* @return a ApiWrapper object
*/
@Deprecated(since = "0.2.0", forRemoval = true)
@Deprecated
public static ApiWrapper ofMainnet(String hexPrivateKey) {
return new ApiWrapper("grpc.trongrid.io:50051", "grpc.trongrid.io:50052", hexPrivateKey);
return new ApiWrapper(Constant.TRONGRID_MAIN_NET, Constant.TRONGRID_MAIN_NET_SOLIDITY, hexPrivateKey);
}

/**
Expand All @@ -166,7 +167,7 @@ public static ApiWrapper ofMainnet(String hexPrivateKey) {
* @return a ApiWrapper object
*/
public static ApiWrapper ofShasta(String hexPrivateKey) {
return new ApiWrapper("grpc.shasta.trongrid.io:50051", "grpc.shasta.trongrid.io:50052", hexPrivateKey);
return new ApiWrapper(Constant.TRONGRID_SHASTA, Constant.TRONGRID_SHASTA_SOLIDITY, hexPrivateKey);
}

/**
Expand All @@ -175,30 +176,16 @@ public static ApiWrapper ofShasta(String hexPrivateKey) {
* @return a ApiWrapper object
*/
public static ApiWrapper ofNile(String hexPrivateKey) {
return new ApiWrapper("47.252.19.181:50051", "47.252.19.181:50061", hexPrivateKey);
return new ApiWrapper(Constant.FULLNODE_NILE, Constant.FULLNODE_NILE_SOLIDITY, hexPrivateKey);
}

/**
* Generate random address
* @return A list, inside are the public key and private key
*/
public static List generateAddress() {
public static KeyPair generateAddress() {
// generate random address
SECP256K1.KeyPair kp = SECP256K1.KeyPair.generate();

SECP256K1.PublicKey pubKey = kp.getPublicKey();
Keccak.Digest256 digest = new Keccak.Digest256();
digest.update(pubKey.getEncoded(), 0, 64);
byte[] raw = digest.digest();
byte[] rawAddr = new byte[21];
rawAddr[0] = 0x41;
System.arraycopy(raw, 12, rawAddr, 1, 20);

List keyPairReturn = new ArrayList<String>();
keyPairReturn.add(Hex.toHexString(rawAddr));
keyPairReturn.add(Hex.toHexString(kp.getPrivateKey().getEncoded()));

return keyPairReturn;
return KeyPair.generate();
}

/**
Expand All @@ -224,6 +211,13 @@ public static ByteString parseAddress(String address) {
return ByteString.copyFrom(raw);
}

public static byte[] calculateTransactionHash (Transaction txn) {
SHA256.Digest digest = new SHA256.Digest();
digest.update(txn.getRawData().toByteArray());
byte[] txid = digest.digest();

return txid;
}

public static ByteString parseHex(String hexString) {
byte[] raw = Hex.decode(hexString);
Expand All @@ -238,22 +232,30 @@ public static String toHex(ByteString raw) {
return Hex.toHexString(raw.toByteArray());
}

public Transaction signTransaction(TransactionExtention txnExt, SECP256K1.KeyPair kp) {
SECP256K1.Signature sig = SECP256K1.sign(Bytes32.wrap(txnExt.getTxid().toByteArray()), kp);
Transaction signedTxn =
txnExt.getTransaction().toBuilder().addSignature(ByteString.copyFrom(sig.encodedBytes().toArray())).build();
public Transaction signTransaction(TransactionExtention txnExt, KeyPair keyPair) {
byte[] txid = txnExt.getTxid().toByteArray();
byte[] signature = KeyPair.signTransaction(txid, keyPair);
Transaction signedTxn =
txnExt.getTransaction().toBuilder().addSignature(ByteString.copyFrom(signature)).build();

return signedTxn;
}

public Transaction signTransaction(Transaction txn, SECP256K1.KeyPair kp) {
SHA256.Digest digest = new SHA256.Digest();
digest.update(txn.getRawData().toByteArray());
byte[] txid = digest.digest();
SECP256K1.Signature sig = SECP256K1.sign(Bytes32.wrap(txid), kp);
Transaction signedTxn = txn.toBuilder().addSignature(ByteString.copyFrom(sig.encodedBytes().toArray())).build();
public Transaction signTransaction(Transaction txn, KeyPair keyPair) {
byte[] txid = calculateTransactionHash(txn);
byte[] signature = KeyPair.signTransaction(txid, keyPair);
Transaction signedTxn = txn.toBuilder().addSignature(ByteString.copyFrom(signature)).build();
return signedTxn;
}

public Transaction signTransaction(TransactionExtention txnExt) {
return signTransaction(txnExt, keyPair);
}

public Transaction signTransaction(Transaction txn) {
return signTransaction(txn, keyPair);
}

/**
* Resolve the result code from TransactionReturn objects.
* @param code the result code.
Expand Down Expand Up @@ -304,21 +306,11 @@ public String broadcastTransaction(Transaction txn) throws RuntimeException{
String message = resolveResultCode(ret.getCodeValue()) + ", " + ret.getMessage();
throw new RuntimeException(message);
} else {
SHA256.Digest digest = new SHA256.Digest();
digest.update(txn.getRawData().toByteArray());
byte[] txid = digest.digest();
byte[] txid = calculateTransactionHash(txn);
return ByteString.copyFrom(Hex.encode(txid)).toStringUtf8();
}
}

public Transaction signTransaction(TransactionExtention txnExt) {
return signTransaction(txnExt, keyPair);
}

public Transaction signTransaction(Transaction txn) {
return signTransaction(txn, keyPair);
}

/**
* Transfer TRX. amount in SUN
* @param fromAddress owner address
Expand Down Expand Up @@ -1307,6 +1299,25 @@ public static UnfreezeAssetContract createUnfreezeAssetContract(ByteString addre
return builder.build();
}

public TransactionExtention updateBrokerage(String address, int brokerage) throws IllegalException{
ByteString ownerAddr = parseAddress(address);
UpdateBrokerageContract upContract =
UpdateBrokerageContract.newBuilder()
.setOwnerAddress(ownerAddr)
.setBrokerage(brokerage)
.build();
return blockingStub.updateBrokerage(upContract);
}

public long getBrokerageInfo(String address) {
ByteString sr = parseAddress(address);
BytesMessage param =
BytesMessage.newBuilder()
.setValue(sr)
.build();
return blockingStub.getBrokerageInfo(param).getNum();
}

/*public void transferTrc20(String from, String to, String cntr, long feeLimit, long amount, int precision) {
System.out.println("============ TRC20 transfer =============");
Expand Down
Loading

0 comments on commit 06fd05f

Please sign in to comment.