Skip to content

Commit

Permalink
Merge pull request #20 from Cletitia/develop
Browse files Browse the repository at this point in the history
Update to 0.2.0
  • Loading branch information
Starsakary authored Aug 5, 2021
2 parents 01cadf1 + 92d68ce commit f9b359b
Show file tree
Hide file tree
Showing 9 changed files with 493 additions and 306 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/
15 changes: 11 additions & 4 deletions trident-java/README.md
Original file line number Diff line number Diff line change
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.2</version>
<version>0.2.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>utils</artifactId>
<version>0.1.2</version>
<version>0.2.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>core</artifactId>
<version>0.1.2</version>
<version>0.2.0</version>
<type>pom</type>
</dependency>
```

2 changes: 1 addition & 1 deletion trident-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ext {
}

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

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand All @@ -30,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 @@ -141,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 @@ -155,7 +157,7 @@ public static ApiWrapper ofMainnet(String hexPrivateKey, String apiKey) {
*/
@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 @@ -165,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 @@ -174,7 +176,7 @@ 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);
}

/**
Expand Down Expand Up @@ -209,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 @@ -223,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 @@ -289,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.getRawPair());
}

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

/**
* Transfer TRX. amount in SUN
* @param fromAddress owner address
Expand Down Expand Up @@ -1292,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.tron.trident.core;

public final class Constant{

//TronGrid gRPC services, maintained by official team
public static final String TRONGRID_MAIN_NET = "grpc.trongrid.io:50051";
public static final String TRONGRID_MAIN_NET_SOLIDITY = "grpc.trongrid.io:50052";

public static final String TRONGRID_SHASTA = "grpc.shasta.trongrid.io:50051";
public static final String TRONGRID_SHASTA_SOLIDITY = "grpc.shasta.trongrid.io:50052";

//Public Fullnode, maintained by official team
public static final String FULLNODE_NILE = "47.252.19.181:50051";
public static final String FULLNODE_NILE_SOLIDITY = "47.252.19.181:50061";

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tron.trident.core.key;

import org.bouncycastle.jcajce.provider.digest.Keccak;
import org.bouncycastle.jcajce.provider.digest.SHA256;
import org.bouncycastle.util.encoders.Hex;
import org.tron.trident.crypto.SECP256K1;
import org.tron.trident.crypto.tuwenitypes.Bytes32;
Expand Down Expand Up @@ -39,25 +40,49 @@ public String toPublicKey() {

public String toBase58CheckAddress() {
SECP256K1.PublicKey pubKey = rawPair.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);

return Base58Check.bytesToBase58(rawAddr);

return publicKeyToBase58CheckAddress(pubKey);
}

public String toHexAddress() {
SECP256K1.PublicKey pubKey = rawPair.getPublicKey();

return publicKeyToHexAddress(pubKey);
}

public static byte[] publicKeyToAddress(final SECP256K1.PublicKey pubKey) {
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);

return rawAddr;
}

public static String publicKeyToBase58CheckAddress(final SECP256K1.PublicKey pubKey) {
byte[] rawAddr = publicKeyToAddress(pubKey);

return Base58Check.bytesToBase58(rawAddr);
}

public static String publicKeyToHexAddress(final SECP256K1.PublicKey pubKey) {
byte[] rawAddr = publicKeyToAddress(pubKey);

return Hex.toHexString(rawAddr);
}

/**
* Return a signature message in byte[]
* @param txid the transaction hash waiting for signature
* @param keyPair
* @return the signature message in byte[]
*/
public static byte[] signTransaction(byte[] txid, KeyPair keyPair) {
SECP256K1.KeyPair kp = keyPair.getRawPair();
SECP256K1.Signature sig = SECP256K1.sign(Bytes32.wrap(txid), kp);

return sig.encodedBytes().toArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.tron.trident.core.transaction;

import org.bouncycastle.util.encoders.Hex;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.core.key.KeyPair;
import org.tron.trident.crypto.SECP256K1;
import org.tron.trident.crypto.tuwenitypes.Bytes;
import org.tron.trident.crypto.tuwenitypes.Bytes32;
import org.tron.trident.proto.Chain.Transaction;

import java.util.Arrays;

public class SignatureValidator {

/**
* Verify if a transction contains a valid signature.
* @param txid the transaction hash
* @param signature the signature message corresponding to the transaction hash
* @param owner the owner of the transaction
* @return true if the signature is valid
*/
public static boolean verify(byte[] txid, byte[] signature, byte[] owner) {
SECP256K1.Signature sig = SECP256K1.Signature.decode(Bytes.wrap(signature));
//decode a public key from the signature
SECP256K1.PublicKey pubKey = SECP256K1.PublicKey.recoverFromSignature(Bytes32.wrap(txid), sig).get();

final byte[] addressFromPubKey = KeyPair.publicKeyToAddress(pubKey);

return Arrays.equals(addressFromPubKey, owner);
}

public static boolean verify(String txid, String signature, String owner) {
byte[] txidBytes = Hex.decode(txid);
byte[] sig = Hex.decode(signature);
byte[] ownerBytes = ApiWrapper.parseAddress(owner).toByteArray();

return verify(txidBytes, sig, ownerBytes);
}
}
Loading

0 comments on commit f9b359b

Please sign in to comment.