From 7cb16bc84de2c9fa9f9d3ad707949ed30e4f2010 Mon Sep 17 00:00:00 2001
From: luqingjie <544309509@qq.com>
Date: Sun, 15 Nov 2026 09:45:14 -0500
Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=BE=9D=E8=B5=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +
pom.xml | 4 +-
ruoyi-system/pom.xml | 5 --
.../java/com/ruoyi/system/chain/TronKey.java | 75 +++++++++++++++++++
.../demo/WalletEnvelopeTronTransferDemo.java | 4 +-
.../system/rental/RentalProvisioning.java | 4 +-
.../system/service/WalletSigningService.java | 4 +-
.../service/impl/TronNileServiceImpl.java | 36 +++++----
.../service/impl/TronProServiceImpl.java | 38 +++++-----
.../service/impl/TronShastaServiceImpl.java | 40 +++++-----
.../com/ruoyi/system/chain/TronKeyTest.java | 42 +++++++++++
.../WalletEnvelopeEncryptionDemoTest.java | 4 +-
12 files changed, 183 insertions(+), 75 deletions(-)
create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/chain/TronKey.java
create mode 100644 ruoyi-system/src/test/java/com/ruoyi/system/chain/TronKeyTest.java
diff --git a/README.md b/README.md
index d3cfe99..7aeb6af 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,7 @@
## 平台简介
+TRON 密钥生成、地址校验和离线签名恢复通过现有 Trident 与 `TronKey` 适配实现,不再依赖 `tron-wallet-cli`;钱包派生路径、地址编码和投票交易行为保持兼容。后端请使用 JDK 8 编译。
+
归集、代付支持可选的[双平台资源租赁](docs/resource-rental.md):人工确认租赁后等待链上资源到账,再输入密码广播;独立执行器自动异步准备资源。默认关闭,启用前执行增量SQL并配置加密的平台凭据。
新增可选的独立[内网链上执行服务](docs/chain-executor.md):默认关闭,保留原配置密码方式;启用前执行 `sql/chain_executor.sql`,并按文档配置仅内网监听、来源白名单、TLS、服务凭据及执行器环境密码。
diff --git a/pom.xml b/pom.xml
index 11f9e9e..6128c66 100644
--- a/pom.xml
+++ b/pom.xml
@@ -35,11 +35,11 @@
maven-releases
- http://gitlab.chatshop.com.cn:8081/repository/maven-releases/
+ http://192.168.3.243:8081/repository/maven-releases/
snapshots
- http://gitlab.chatshop.com.cn:8081/repository/maven-snapshots/
+ http://192.168.3.243:8081/repository/maven-snapshots/
diff --git a/ruoyi-system/pom.xml b/ruoyi-system/pom.xml
index c64c720..e75b0e4 100644
--- a/ruoyi-system/pom.xml
+++ b/ruoyi-system/pom.xml
@@ -47,11 +47,6 @@
4.13.2
test
-
- com.github.tronprotocol
- tron-wallet-cli
- 1.2.0
-
io.grpc
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/chain/TronKey.java b/ruoyi-system/src/main/java/com/ruoyi/system/chain/TronKey.java
new file mode 100644
index 0000000..bb0dc71
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/chain/TronKey.java
@@ -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);
+ }
+}
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/demo/WalletEnvelopeTronTransferDemo.java b/ruoyi-system/src/main/java/com/ruoyi/system/demo/WalletEnvelopeTronTransferDemo.java
index fcbd61e..db03eb4 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/demo/WalletEnvelopeTronTransferDemo.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/demo/WalletEnvelopeTronTransferDemo.java
@@ -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);
}
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalProvisioning.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalProvisioning.java
index bff2f7a..a158d52 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalProvisioning.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalProvisioning.java
@@ -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);
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/WalletSigningService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/WalletSigningService.java
index f1c78e0..395a4c9 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/WalletSigningService.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/WalletSigningService.java
@@ -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);
}
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronNileServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronNileServiceImpl.java
index f169e3a..0c9fc49 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronNileServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronNileServiceImpl.java
@@ -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 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);
}
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronProServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronProServiceImpl.java
index 0cc9ff0..c541e86 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronProServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronProServiceImpl.java
@@ -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 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);
}
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronShastaServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronShastaServiceImpl.java
index 3d0fd11..0fb144a 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronShastaServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronShastaServiceImpl.java
@@ -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 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);
}
diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/chain/TronKeyTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/chain/TronKeyTest.java
new file mode 100644
index 0000000..444b6ca
--- /dev/null
+++ b/ruoyi-system/src/test/java/com/ruoyi/system/chain/TronKeyTest.java
@@ -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==");
+ }
+}
diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/demo/WalletEnvelopeEncryptionDemoTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/demo/WalletEnvelopeEncryptionDemoTest.java
index 14a1ca5..4c5dc0d 100644
--- a/ruoyi-system/src/test/java/com/ruoyi/system/demo/WalletEnvelopeEncryptionDemoTest.java
+++ b/ruoyi-system/src/test/java/com/ruoyi/system/demo/WalletEnvelopeEncryptionDemoTest.java
@@ -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());