去掉依赖

This commit is contained in:
lu7213 committed 2026-11-15 09:45:14 -05:00
1 parent 79183ee0d9
commit 7cb16bc84d
12 files changed
+183 -75

No files matched your search

@@ -0,0 +1,75 @@
package com.ruoyi.system.chain;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;
import org.bouncycastle.util.encoders.Hex;
import org.tron.trident.core.key.KeyPair;
import org.tron.trident.crypto.SECP256K1;
import org.tron.trident.crypto.tuwenitypes.Bytes32;
/**
* 基于现有 Trident 的 TRON 密钥适配,避免依赖钱包 CLI。
* 保持 32 字节私钥、0x41 地址前缀和旧版 compact 签名恢复格式。
* 本类不记录或在异常中拼接私钥、签名和消息内容。
*/
public final class TronKey {
private final KeyPair keyPair;
/** 使用调用方的安全随机源生成合法的 secp256k1 私钥。 */
public TronKey(SecureRandom random) {
BigInteger value;
do {
value = new BigInteger(256, random);
} while (value.signum() == 0 || value.compareTo(SECP256K1.CURVE.getN()) >= 0);
keyPair = pair(value);
}
private TronKey(BigInteger privateKey) {
keyPair = pair(privateKey);
}
private static KeyPair pair(BigInteger value) {
if (value == null || value.signum() <= 0 || value.compareTo(SECP256K1.CURVE.getN()) >= 0) {
throw new IllegalArgumentException("无效的 TRON 私钥");
}
return new KeyPair(String.format("%064x", value));
}
public static TronKey fromPrivate(BigInteger value) {
return new TronKey(value);
}
public byte[] getPrivKeyBytes() {
return Hex.decode(keyPair.toPrivateKey());
}
public byte[] getAddress() {
return KeyPair.publicKeyToAddress(keyPair.getRawPair().getPublicKey());
}
/**
* 恢复旧接口的 Base64(header || r || s) 签名。
* header 为 27..34,其中 31..34 表示压缩公钥;地址始终由完整公钥计算。
* 传入值已经是消息摘要,此处不得再次哈希。
*/
public static byte[] signatureToAddress(byte[] hash, String signature) {
byte[] compact = Base64.getDecoder().decode(signature);
if (compact.length != 65) {
throw new IllegalArgumentException("无效的 TRON 签名长度");
}
int header = compact[0] & 255;
if (header < 27 || header > 34) {
throw new IllegalArgumentException("无效的 TRON 签名恢复标记");
}
byte recoveryId = (byte) ((header - 27) & 3);
SECP256K1.Signature decoded = SECP256K1.Signature.create(
new BigInteger(1, Arrays.copyOfRange(compact, 1, 33)),
new BigInteger(1, Arrays.copyOfRange(compact, 33, 65)), recoveryId);
SECP256K1.PublicKey publicKey = SECP256K1.PublicKey.recoverFromSignature(
Bytes32.wrap(hash), decoded).orElseThrow(
() -> new IllegalArgumentException("无法恢复 TRON 签名地址"));
return KeyPair.publicKeyToAddress(publicKey);
}
}
@@ -6,7 +6,7 @@ import org.bitcoinj.crypto.HDPath;
import org.bitcoinj.crypto.MnemonicCode;
import org.bitcoinj.wallet.DeterministicKeyChain;
import org.bitcoinj.wallet.DeterministicSeed;
import org.tron.common.crypto.ECKey;
import com.ruoyi.system.chain.TronKey;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.proto.Chain;
import org.tron.trident.proto.Response;
@@ -77,7 +77,7 @@ public final class WalletEnvelopeTronTransferDemo {
new ChildNumber(0, true), ChildNumber.ZERO);
DeterministicKey key = chain.getKeyByPath(base.extend(new ChildNumber(index, false)), true);
byte[] privateKey = key.getPrivKeyBytes();
String address = Base58Check.bytesToBase58(ECKey.fromPrivate(new BigInteger(1, privateKey)).getAddress());
String address = Base58Check.bytesToBase58(TronKey.fromPrivate(new BigInteger(1, privateKey)).getAddress());
return new DerivedPrivateKey(privateKey, address);
}
@@ -5,7 +5,7 @@ import static com.ruoyi.system.rental.RentalValues.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tron.common.crypto.ECKey;
import com.ruoyi.system.chain.TronKey;
import org.tron.trident.crypto.Hash;
import java.nio.charset.StandardCharsets;
@@ -295,7 +295,7 @@ public class RentalProvisioning {
compact[0] = (byte) v;
System.arraycopy(sig, 0, compact, 1, 64);
byte[] address =
ECKey.signatureToAddress(
TronKey.signatureToAddress(
Hash.sha3(plain),
java.util.Base64.getEncoder().encodeToString(compact));
String recovered = org.tron.trident.utils.Base58Check.bytesToBase58(address);
@@ -15,7 +15,7 @@ import org.bitcoinj.wallet.DeterministicKeyChain;
import org.bitcoinj.wallet.DeterministicSeed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tron.common.crypto.ECKey;
import com.ruoyi.system.chain.TronKey;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.utils.Base58Check;
@@ -153,7 +153,7 @@ public class WalletSigningService {
new ChildNumber(0, true), ChildNumber.ZERO);
DeterministicKey key = chain.getKeyByPath(base.extend(new ChildNumber(index, false)), true);
byte[] bytes = key.getPrivKeyBytes();
String address = Base58Check.bytesToBase58(ECKey.fromPrivate(new BigInteger(1, bytes)).getAddress());
String address = Base58Check.bytesToBase58(TronKey.fromPrivate(new BigInteger(1, bytes)).getAddress());
return new DerivedKey(bytes, address);
}
@@ -24,9 +24,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.tron.api.GrpcAPI;
import org.tron.api.WalletGrpc;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.Base58;
import org.tron.common.utils.ByteArray;
import com.ruoyi.system.chain.TronKey;
import org.tron.protos.Protocol;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Address;
@@ -288,7 +286,7 @@ public class TronNileServiceImpl implements ITronService {
BigDecimal result = new BigDecimal(0);
for (ByteString byteString : list) {
String hexAddress = org.spongycastle.util.encoders.Hex.toHexString(byteString.toByteArray());
String address = encode58Check(org.tron.common.utils.ByteArray.fromHexString(hexAddress));
String address = encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
Response.DelegatedResourceList delegatedResourceList = api.getDelegatedResourceV2(address, ownerAddr);
int count = delegatedResourceList.getDelegatedResourceCount();
for (int i = 0; i < count; i++) {
@@ -318,15 +316,15 @@ public class TronNileServiceImpl implements ITronService {
BigDecimal result = new BigDecimal(0);
for (ByteString byteString : list) {
String hexAddress = org.spongycastle.util.encoders.Hex.toHexString(byteString.toByteArray());
String address = encode58Check(org.tron.common.utils.ByteArray.fromHexString(hexAddress));
String address = encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
Response.DelegatedResourceList delegatedResourceList = api.getDelegatedResourceV2(ownerAddr,address);
int count = delegatedResourceList.getDelegatedResourceCount();
for (int i = 0; i < count; i++) {
Response.DelegatedResource delegatedResource = delegatedResourceList.getDelegatedResource(i);
String fromHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getFrom().toByteArray());
String fromAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(fromHexAddress));
String fromAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(fromHexAddress));
String toHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getTo().toByteArray());
String toAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(toHexAddress));
String toAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(toHexAddress));
if (resourceCode == 0) {
BigDecimal divisor = new BigDecimal("1000000");
BigDecimal divide = new BigDecimal(delegatedResource.getFrozenBalanceForBandwidth()).divide(divisor, 4, RoundingMode.HALF_UP);
@@ -411,19 +409,19 @@ public class TronNileServiceImpl implements ITronService {
boolean needRevote = currentVotes.keySet().stream()
.anyMatch(sr -> !top27.contains(sr));
Iterator<String> iterator = currentVotes.keySet().iterator();
org.tron.protos.Contract.VoteWitnessContract.Builder builder = org.tron.protos.Contract.VoteWitnessContract.newBuilder()
org.tron.trident.proto.Contract.VoteWitnessContract.Builder builder = org.tron.trident.proto.Contract.VoteWitnessContract.newBuilder()
.setOwnerAddress(ApiWrapper.parseAddress(ownerAddress));
boolean cancle = false;
while (iterator.hasNext()) {
String address = iterator.next();
if (!top27.contains(address)) {
builder.addVotes(org.tron.protos.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(address))
builder.addVotes(org.tron.trident.proto.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(address))
.setVoteCount(1));
cancle = true;
}
}
if (cancle) {
org.tron.protos.Contract.VoteWitnessContract voteWitnessContract = builder.build();
org.tron.trident.proto.Contract.VoteWitnessContract voteWitnessContract = builder.build();
Response.TransactionExtention tx = api.createTransactionExtention(voteWitnessContract,
Chain.Transaction.Contract.ContractType.VoteWitnessContract);
Chain.Transaction signedTx = api.signTransaction(tx);
@@ -438,12 +436,12 @@ public class TronNileServiceImpl implements ITronService {
// 5. 构建新投票(平均投到备用 SR)
backupSRs.addAll(top27.stream().sorted().collect(Collectors.toList()).subList(0, size));
long perSRVotes = totalVotes / backupSRs.size();
builder = org.tron.protos.Contract.VoteWitnessContract.newBuilder()
builder = org.tron.trident.proto.Contract.VoteWitnessContract.newBuilder()
.setOwnerAddress(ApiWrapper.parseAddress(ownerAddress));
for (String sr : backupSRs) {
builder.addVotes(org.tron.protos.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(sr)).setVoteCount(perSRVotes));
builder.addVotes(org.tron.trident.proto.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(sr)).setVoteCount(perSRVotes));
}
org.tron.protos.Contract.VoteWitnessContract voteWitnessContract = builder.build();
org.tron.trident.proto.Contract.VoteWitnessContract voteWitnessContract = builder.build();
Response.TransactionExtention tx = api.createTransactionExtention(voteWitnessContract,
Chain.Transaction.Contract.ContractType.VoteWitnessContract);
Chain.Transaction signedTx = api.signTransaction(tx);
@@ -543,10 +541,10 @@ public class TronNileServiceImpl implements ITronService {
@Override
public TronAccountItem createTronAddress() {
ECKey eCkey = new ECKey(random);
String privateKey = ByteArray.toHexString(eCkey.getPrivKeyBytes());
TronKey eCkey = new TronKey(random);
String privateKey = org.bouncycastle.util.encoders.Hex.toHexString(eCkey.getPrivKeyBytes());
byte[] addressBytes = eCkey.getAddress();
String hexAddress = ByteArray.toHexString(addressBytes);
String hexAddress = org.bouncycastle.util.encoders.Hex.toHexString(addressBytes);
TronAccountItem tronAccount = new TronAccountItem();
tronAccount.setAddress(toViewAddress(hexAddress));
tronAccount.setHexAddress(hexAddress);
@@ -656,7 +654,7 @@ public class TronNileServiceImpl implements ITronService {
item.setPubKeyCompressed(Hex.toHexString(pubKeyCompressed));
item.setPubKeyUnCompressed(Hex.toHexString(pubKeyUnCompressed));
item.setPath(pathStr);
String hexAddress = ByteArray.toHexString(addressBytes);
String hexAddress = org.bouncycastle.util.encoders.Hex.toHexString(addressBytes);
item.setHexAddress(hexAddress);
String privateKeyBase58 = encode58Check(privateKey);
item.setPrivateKeyBase58(privateKeyBase58);
@@ -756,7 +754,7 @@ public class TronNileServiceImpl implements ITronService {
* @return
*/
private String toViewAddress(String hexAddress) {
return encode58Check(org.tron.common.utils.ByteArray.fromHexString(hexAddress));
return encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
}
/**
@@ -798,7 +796,7 @@ public class TronNileServiceImpl implements ITronService {
byte[] inputCheck = new byte[input.length + 4];
System.arraycopy(input, 0, inputCheck, 0, input.length);
System.arraycopy(hash1, 0, inputCheck, input.length, 4);
return Base58.encode(inputCheck);
return encode(inputCheck);
} catch (Throwable t) {
log.error(String.format("data error:%s", Hex.toHexString(input)), t);
}
@@ -22,9 +22,7 @@ import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;
import org.tron.api.GrpcAPI;
import org.tron.api.WalletGrpc;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.Base58;
import org.tron.common.utils.ByteArray;
import com.ruoyi.system.chain.TronKey;
import org.tron.protos.Protocol;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Address;
@@ -333,15 +331,15 @@ public class TronProServiceImpl implements ITronService {
BigDecimal result = new BigDecimal(0);
for (ByteString byteString : list) {
String hexAddress = org.spongycastle.util.encoders.Hex.toHexString(byteString.toByteArray());
String address = encode58Check(org.tron.common.utils.ByteArray.fromHexString(hexAddress));
String address = encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
Response.DelegatedResourceList delegatedResourceList = api.getDelegatedResourceV2(ownerAddr, address);
int count = delegatedResourceList.getDelegatedResourceCount();
for (int i = 0; i < count; i++) {
Response.DelegatedResource delegatedResource = delegatedResourceList.getDelegatedResource(i);
String fromHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getFrom().toByteArray());
String fromAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(fromHexAddress));
String fromAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(fromHexAddress));
String toHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getTo().toByteArray());
String toAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(toHexAddress));
String toAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(toHexAddress));
if (resourceCode == 0) {
BigDecimal divisor = new BigDecimal("1000000");
BigDecimal divide = new BigDecimal(delegatedResource.getFrozenBalanceForBandwidth()).divide(divisor, 4, RoundingMode.HALF_UP);
@@ -368,15 +366,15 @@ public class TronProServiceImpl implements ITronService {
BigDecimal result = new BigDecimal(0);
for (ByteString byteString : list) {
String hexAddress = org.spongycastle.util.encoders.Hex.toHexString(byteString.toByteArray());
String address = encode58Check(org.tron.common.utils.ByteArray.fromHexString(hexAddress));
String address = encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
Response.DelegatedResourceList delegatedResourceList = api.getDelegatedResourceV2(address, ownerAddr);
int count = delegatedResourceList.getDelegatedResourceCount();
for (int i = 0; i < count; i++) {
Response.DelegatedResource delegatedResource = delegatedResourceList.getDelegatedResource(i);
String fromHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getFrom().toByteArray());
String fromAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(fromHexAddress));
String fromAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(fromHexAddress));
String toHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getTo().toByteArray());
String toAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(toHexAddress));
String toAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(toHexAddress));
if (resourceCode == 0) {
BigDecimal divisor = new BigDecimal("1000000");
BigDecimal divide = new BigDecimal(delegatedResource.getFrozenBalanceForBandwidth()).divide(divisor, 4, RoundingMode.HALF_UP);
@@ -495,19 +493,19 @@ public class TronProServiceImpl implements ITronService {
boolean needRevote = currentVotes.keySet().stream()
.anyMatch(sr -> !top27.contains(sr));
Iterator<String> iterator = currentVotes.keySet().iterator();
org.tron.protos.Contract.VoteWitnessContract.Builder builder = org.tron.protos.Contract.VoteWitnessContract.newBuilder()
org.tron.trident.proto.Contract.VoteWitnessContract.Builder builder = org.tron.trident.proto.Contract.VoteWitnessContract.newBuilder()
.setOwnerAddress(ApiWrapper.parseAddress(ownerAddress));
boolean cancle = false;
while (iterator.hasNext()) {
String address = iterator.next();
if (!top27.contains(address)) {
builder.addVotes(org.tron.protos.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(address))
builder.addVotes(org.tron.trident.proto.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(address))
.setVoteCount(1));
cancle = true;
}
}
if (cancle) {
org.tron.protos.Contract.VoteWitnessContract voteWitnessContract = builder.build();
org.tron.trident.proto.Contract.VoteWitnessContract voteWitnessContract = builder.build();
Response.TransactionExtention tx = api.createTransactionExtention(voteWitnessContract,
Chain.Transaction.Contract.ContractType.VoteWitnessContract);
Chain.Transaction signedTx = api.signTransaction(tx);
@@ -522,12 +520,12 @@ public class TronProServiceImpl implements ITronService {
// 5. 构建新投票(平均投到备用 SR)
backupSRs.addAll(top27.stream().sorted().collect(Collectors.toList()).subList(0, size));
long perSRVotes = totalVotes / backupSRs.size();
builder = org.tron.protos.Contract.VoteWitnessContract.newBuilder()
builder = org.tron.trident.proto.Contract.VoteWitnessContract.newBuilder()
.setOwnerAddress(ApiWrapper.parseAddress(ownerAddress));
for (String sr : backupSRs) {
builder.addVotes(org.tron.protos.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(sr)).setVoteCount(perSRVotes));
builder.addVotes(org.tron.trident.proto.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(sr)).setVoteCount(perSRVotes));
}
org.tron.protos.Contract.VoteWitnessContract voteWitnessContract = builder.build();
org.tron.trident.proto.Contract.VoteWitnessContract voteWitnessContract = builder.build();
Response.TransactionExtention tx = api.createTransactionExtention(voteWitnessContract,
Chain.Transaction.Contract.ContractType.VoteWitnessContract);
Chain.Transaction signedTx = api.signTransaction(tx);
@@ -627,10 +625,10 @@ public class TronProServiceImpl implements ITronService {
@Override
public TronAccountItem createTronAddress() {
ECKey eCkey = new ECKey(random);
String privateKey = ByteArray.toHexString(eCkey.getPrivKeyBytes());
TronKey eCkey = new TronKey(random);
String privateKey = org.bouncycastle.util.encoders.Hex.toHexString(eCkey.getPrivKeyBytes());
byte[] addressBytes = eCkey.getAddress();
String hexAddress = ByteArray.toHexString(addressBytes);
String hexAddress = org.bouncycastle.util.encoders.Hex.toHexString(addressBytes);
TronAccountItem tronAccount = new TronAccountItem();
tronAccount.setAddress(toViewAddress(hexAddress));
tronAccount.setHexAddress(hexAddress);
@@ -759,7 +757,7 @@ public class TronProServiceImpl implements ITronService {
* @return
*/
private String toViewAddress(String hexAddress) {
return encode58Check(ByteArray.fromHexString(hexAddress));
return encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
}
/**
@@ -801,7 +799,7 @@ public class TronProServiceImpl implements ITronService {
byte[] inputCheck = new byte[input.length + 4];
System.arraycopy(input, 0, inputCheck, 0, input.length);
System.arraycopy(hash1, 0, inputCheck, input.length, 4);
return Base58.encode(inputCheck);
return encode(inputCheck);
} catch (Throwable t) {
log.error(String.format("data error:%s", Hex.toHexString(input)), t);
}
@@ -25,9 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.tron.api.GrpcAPI;
import org.tron.api.WalletGrpc;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.Base58;
import org.tron.common.utils.ByteArray;
import com.ruoyi.system.chain.TronKey;
import org.tron.protos.Protocol;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Address;
@@ -105,15 +103,15 @@ public class TronShastaServiceImpl implements ITronService {
BigDecimal result = new BigDecimal(0);
for (ByteString byteString : list) {
String hexAddress = org.spongycastle.util.encoders.Hex.toHexString(byteString.toByteArray());
String address = encode58Check(org.tron.common.utils.ByteArray.fromHexString(hexAddress));
String address = encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
Response.DelegatedResourceList delegatedResourceList = api.getDelegatedResourceV2(ownerAddr,address);
int count = delegatedResourceList.getDelegatedResourceCount();
for (int i = 0; i < count; i++) {
Response.DelegatedResource delegatedResource = delegatedResourceList.getDelegatedResource(i);
String fromHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getFrom().toByteArray());
String fromAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(fromHexAddress));
String fromAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(fromHexAddress));
String toHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getTo().toByteArray());
String toAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(toHexAddress));
String toAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(toHexAddress));
if (resourceCode == 0) {
BigDecimal divisor = new BigDecimal("1000000");
BigDecimal divide = new BigDecimal(delegatedResource.getFrozenBalanceForBandwidth()).divide(divisor, 4, RoundingMode.HALF_UP);
@@ -139,15 +137,15 @@ public class TronShastaServiceImpl implements ITronService {
BigDecimal result = new BigDecimal(0);
for (ByteString byteString : list) {
String hexAddress = org.spongycastle.util.encoders.Hex.toHexString(byteString.toByteArray());
String address = encode58Check(org.tron.common.utils.ByteArray.fromHexString(hexAddress));
String address = encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
Response.DelegatedResourceList delegatedResourceList = api.getDelegatedResourceV2(address, ownerAddr);
int count = delegatedResourceList.getDelegatedResourceCount();
for (int i = 0; i < count; i++) {
Response.DelegatedResource delegatedResource = delegatedResourceList.getDelegatedResource(i);
String fromHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getFrom().toByteArray());
String fromAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(fromHexAddress));
String fromAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(fromHexAddress));
String toHexAddress = org.spongycastle.util.encoders.Hex.toHexString(delegatedResource.getTo().toByteArray());
String toAddress = encode58Check(org.tron.common.utils.ByteArray.fromHexString(toHexAddress));
String toAddress = encode58Check(org.bouncycastle.util.encoders.Hex.decode(toHexAddress));
if(resourceCode == 0){
BigDecimal divisor = new BigDecimal("1000000");
BigDecimal divide = new BigDecimal(delegatedResource.getFrozenBalanceForBandwidth()).divide(divisor, 4, RoundingMode.HALF_UP);
@@ -437,19 +435,19 @@ public class TronShastaServiceImpl implements ITronService {
boolean needRevote = currentVotes.keySet().stream()
.anyMatch(sr -> !top27.contains(sr));
Iterator<String> iterator = currentVotes.keySet().iterator();
org.tron.protos.Contract.VoteWitnessContract.Builder builder = org.tron.protos.Contract.VoteWitnessContract.newBuilder()
org.tron.trident.proto.Contract.VoteWitnessContract.Builder builder = org.tron.trident.proto.Contract.VoteWitnessContract.newBuilder()
.setOwnerAddress(ApiWrapper.parseAddress(ownerAddress));
boolean cancle = false;
while (iterator.hasNext()) {
String address = iterator.next();
if (!top27.contains(address)) {
builder.addVotes(org.tron.protos.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(address))
builder.addVotes(org.tron.trident.proto.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(address))
.setVoteCount(1));
cancle = true;
}
}
if (cancle) {
org.tron.protos.Contract.VoteWitnessContract voteWitnessContract = builder.build();
org.tron.trident.proto.Contract.VoteWitnessContract voteWitnessContract = builder.build();
Response.TransactionExtention tx = api.createTransactionExtention(voteWitnessContract,
Chain.Transaction.Contract.ContractType.VoteWitnessContract);
Chain.Transaction signedTx = api.signTransaction(tx);
@@ -464,12 +462,12 @@ public class TronShastaServiceImpl implements ITronService {
// 5. 构建新投票(平均投到备用 SR)
backupSRs.addAll(top27.stream().sorted().collect(Collectors.toList()).subList(0, size));
long perSRVotes = totalVotes / backupSRs.size();
builder = org.tron.protos.Contract.VoteWitnessContract.newBuilder()
builder = org.tron.trident.proto.Contract.VoteWitnessContract.newBuilder()
.setOwnerAddress(ApiWrapper.parseAddress(ownerAddress));
for (String sr : backupSRs) {
builder.addVotes(org.tron.protos.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(sr)).setVoteCount(perSRVotes));
builder.addVotes(org.tron.trident.proto.Contract.VoteWitnessContract.Vote.newBuilder().setVoteAddress(ApiWrapper.parseAddress(sr)).setVoteCount(perSRVotes));
}
org.tron.protos.Contract.VoteWitnessContract voteWitnessContract = builder.build();
org.tron.trident.proto.Contract.VoteWitnessContract voteWitnessContract = builder.build();
Response.TransactionExtention tx = api.createTransactionExtention(voteWitnessContract,
Chain.Transaction.Contract.ContractType.VoteWitnessContract);
Chain.Transaction signedTx = api.signTransaction(tx);
@@ -569,10 +567,10 @@ public class TronShastaServiceImpl implements ITronService {
@Override
public TronAccountItem createTronAddress() {
ECKey eCkey = new ECKey(random);
String privateKey = ByteArray.toHexString(eCkey.getPrivKeyBytes());
TronKey eCkey = new TronKey(random);
String privateKey = org.bouncycastle.util.encoders.Hex.toHexString(eCkey.getPrivKeyBytes());
byte[] addressBytes = eCkey.getAddress();
String hexAddress = ByteArray.toHexString(addressBytes);
String hexAddress = org.bouncycastle.util.encoders.Hex.toHexString(addressBytes);
TronAccountItem tronAccount = new TronAccountItem();
tronAccount.setAddress(toViewAddress(hexAddress));
tronAccount.setHexAddress(hexAddress);
@@ -680,7 +678,7 @@ public class TronShastaServiceImpl implements ITronService {
item.setPubKeyCompressed(Hex.toHexString(pubKeyCompressed));
item.setPubKeyUnCompressed(Hex.toHexString(pubKeyUnCompressed));
item.setPath(pathStr);
String hexAddress = ByteArray.toHexString(addressBytes);
String hexAddress = org.bouncycastle.util.encoders.Hex.toHexString(addressBytes);
item.setHexAddress(hexAddress);
String privateKeyBase58 = encode58Check(privateKey);
item.setPrivateKeyBase58(privateKeyBase58);
@@ -749,7 +747,7 @@ public class TronShastaServiceImpl implements ITronService {
* @return
*/
private String toViewAddress(String hexAddress) {
return encode58Check(ByteArray.fromHexString(hexAddress));
return encode58Check(org.bouncycastle.util.encoders.Hex.decode(hexAddress));
}
/**
@@ -791,7 +789,7 @@ public class TronShastaServiceImpl implements ITronService {
byte[] inputCheck = new byte[input.length + 4];
System.arraycopy(input, 0, inputCheck, 0, input.length);
System.arraycopy(hash1, 0, inputCheck, input.length, 4);
return Base58.encode(inputCheck);
return encode(inputCheck);
} catch (Throwable t) {
log.error(String.format("data error:%s", Hex.toHexString(input)), t);
}
@@ -0,0 +1,42 @@
package com.ruoyi.system.chain;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.junit.Test;
import org.tron.trident.utils.Base58Check;
import static org.junit.Assert.*;
/** 固定公开测试密钥验证地址格式,所有测试均离线执行。 */
public class TronKeyTest {
@Test
public void privateKeyOneKeepsKnownAddressAndLeadingZeros() {
TronKey key = TronKey.fromPrivate(BigInteger.ONE);
byte[] bytes = key.getPrivKeyBytes();
assertEquals(32, bytes.length);
assertEquals(1, bytes[31]);
for (int i = 0; i < 31; i++) {
assertEquals(0, bytes[i]);
}
assertEquals("TMVQGm1qAQYVdetCeGRRkTWYYrLXuHK2HC",
Base58Check.bytesToBase58(key.getAddress()));
assertEquals(21, key.getAddress().length);
assertEquals(0x41, key.getAddress()[0]);
}
@Test
public void generatedKeyCanBeRestored() {
TronKey key = new TronKey(new SecureRandom());
assertArrayEquals(key.getAddress(),
TronKey.fromPrivate(new BigInteger(1, key.getPrivKeyBytes())).getAddress());
}
@Test(expected = IllegalArgumentException.class)
public void zeroPrivateKeyIsRejected() {
TronKey.fromPrivate(BigInteger.ZERO);
}
@Test(expected = IllegalArgumentException.class)
public void truncatedSignatureIsRejected() {
TronKey.signatureToAddress(new byte[32], "AA==");
}
}
@@ -5,7 +5,7 @@ import org.junit.Assume;
import org.junit.Test;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.utils.Base58Check;
import org.tron.common.crypto.ECKey;
import com.ruoyi.system.chain.TronKey;
import org.tron.trident.proto.Chain;
import org.tron.trident.proto.Response;
@@ -166,7 +166,7 @@ public class WalletEnvelopeEncryptionDemoTest {
Assume.assumeTrue("未配置 TRON_TEST_*,跳过链上转账测试",
privateKey != null && targetAddress != null && amountText != null);
ECKey key = ECKey.fromPrivate(new java.math.BigInteger(privateKey.trim(), 16));
TronKey key = TronKey.fromPrivate(new java.math.BigInteger(privateKey.trim(), 16));
String fromAddress = Base58Check.bytesToBase58(key.getAddress());
long sunAmount = new BigDecimal(amountText).movePointRight(6).longValueExact();
ApiWrapper api = ApiWrapper.ofNile(privateKey.trim());