新增tron加密逻辑
This commit is contained in:
1 parent
009955d560
commit
00a4a08c70
18 files changed
+1071
-55
No files matched your search
+237
@@ -0,0 +1,237 @@
|
||||
package com.ruoyi.system.demo;
|
||||
|
||||
import org.junit.Assert;
|
||||
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 org.tron.trident.proto.Chain;
|
||||
import org.tron.trident.proto.Response;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* WalletEnvelopeEncryptionDemo 的安全契约测试。
|
||||
*
|
||||
* <p>这些测试不验证链上广播,而是验证广播前最重要的密钥生命周期:
|
||||
* 信封能够解锁、错误密码不能解锁、轮换后旧密码失效,以及钱包索引和地址保持不变。
|
||||
* 测试故意不打印助记词、私钥或完整密文,避免测试日志成为敏感信息泄漏渠道。</p>
|
||||
*/
|
||||
public class WalletEnvelopeEncryptionDemoTest {
|
||||
|
||||
/**
|
||||
* 转账测试使用的最小内存广播器。
|
||||
* 它只记录已经通过业务校验的转账参数,不连接 Nile/Mainnet,也不产生真实交易。
|
||||
* 真实实现中,此位置应由服务端 WalletKeyService 解锁并签名后调用 TronService。
|
||||
*/
|
||||
private static final class FakeTransferBroadcaster {
|
||||
private String broadcast(String from, String to, BigDecimal amount) throws Exception {
|
||||
if (from == null || to == null || amount == null || amount.signum() <= 0) {
|
||||
throw new IllegalArgumentException("转账参数无效");
|
||||
}
|
||||
// 测试交易号只由业务字段计算,不包含助记词或私钥。
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256")
|
||||
.digest((from + "|" + to + "|" + amount.toPlainString())
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder tx = new StringBuilder("test-");
|
||||
for (byte value : digest) tx.append(String.format("%02x", value));
|
||||
return tx.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAndUnlockShouldRecoverSameMnemonicAndAddresses() throws Exception {
|
||||
char[] password = "correct-password-123".toCharArray();
|
||||
try {
|
||||
WalletEnvelopeEncryptionDemo.DemoBundle bundle =
|
||||
WalletEnvelopeEncryptionDemo.create(password, 2);
|
||||
|
||||
// 2 个子钱包 + index=0 主钱包,因此总数应为 3。
|
||||
Assert.assertEquals(3, bundle.wallets.size());
|
||||
Assert.assertEquals(0, bundle.wallets.get(0).index);
|
||||
Assert.assertEquals("m/44'/195'/0'/0/0", bundle.wallets.get(0).path);
|
||||
|
||||
char[] unlocked = WalletEnvelopeEncryptionDemo.unlock(bundle.envelope, password);
|
||||
try {
|
||||
// 解锁出的助记词重新派生后,地址必须与创建时完全一致。
|
||||
List<WalletEnvelopeEncryptionDemo.DerivedWallet> derived =
|
||||
WalletEnvelopeEncryptionDemo.deriveWallets(new String(unlocked), 3);
|
||||
for (int i = 0; i < bundle.wallets.size(); i++) {
|
||||
Assert.assertEquals(bundle.wallets.get(i).address, derived.get(i).address);
|
||||
Assert.assertEquals(bundle.wallets.get(i).path, derived.get(i).path);
|
||||
}
|
||||
} finally {
|
||||
// 助记词只在测试断言期间存在,使用后立即清零。
|
||||
Arrays.fill(unlocked, '\0');
|
||||
}
|
||||
} finally {
|
||||
Arrays.fill(password, '\0');
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrongPasswordShouldBeRejected() throws Exception {
|
||||
char[] password = "correct-password-123".toCharArray();
|
||||
char[] wrongPassword = "wrong-password-123".toCharArray();
|
||||
try {
|
||||
WalletEnvelopeEncryptionDemo.DemoBundle bundle =
|
||||
WalletEnvelopeEncryptionDemo.create(password, 0);
|
||||
try {
|
||||
WalletEnvelopeEncryptionDemo.unlock(bundle.envelope, wrongPassword);
|
||||
Assert.fail("错误配置密码不应解锁信封");
|
||||
} catch (SecurityException expected) {
|
||||
// 预期路径:密码验证失败,不应继续解密助记词。
|
||||
}
|
||||
} finally {
|
||||
Arrays.fill(password, '\0');
|
||||
Arrays.fill(wrongPassword, '\0');
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rotatePasswordShouldKeepAddressesAndRejectOldPassword() throws Exception {
|
||||
char[] oldPassword = "old-password-123".toCharArray();
|
||||
char[] newPassword = "new-password-456".toCharArray();
|
||||
try {
|
||||
WalletEnvelopeEncryptionDemo.DemoBundle bundle =
|
||||
WalletEnvelopeEncryptionDemo.create(oldPassword, 2);
|
||||
WalletEnvelopeEncryptionDemo.EncryptedEnvelope rotated =
|
||||
WalletEnvelopeEncryptionDemo.rotateConfigurationPassword(
|
||||
bundle.envelope, oldPassword, newPassword);
|
||||
|
||||
Assert.assertEquals(bundle.envelope.version + 1, rotated.version);
|
||||
try {
|
||||
WalletEnvelopeEncryptionDemo.unlock(rotated, oldPassword);
|
||||
Assert.fail("轮换后旧密码必须失效");
|
||||
} catch (SecurityException expected) {
|
||||
// 预期路径。
|
||||
}
|
||||
|
||||
char[] unlocked = WalletEnvelopeEncryptionDemo.unlock(rotated, newPassword);
|
||||
try {
|
||||
List<WalletEnvelopeEncryptionDemo.DerivedWallet> after =
|
||||
WalletEnvelopeEncryptionDemo.deriveWallets(new String(unlocked), 3);
|
||||
for (int i = 0; i < bundle.wallets.size(); i++) {
|
||||
Assert.assertEquals(bundle.wallets.get(i).address, after.get(i).address);
|
||||
}
|
||||
} finally {
|
||||
Arrays.fill(unlocked, '\0');
|
||||
}
|
||||
} finally {
|
||||
Arrays.fill(oldPassword, '\0');
|
||||
Arrays.fill(newPassword, '\0');
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tamperedCiphertextShouldBeRejected() throws Exception {
|
||||
char[] password = "correct-password-123".toCharArray();
|
||||
try {
|
||||
WalletEnvelopeEncryptionDemo.DemoBundle bundle =
|
||||
WalletEnvelopeEncryptionDemo.create(password, 0);
|
||||
WalletEnvelopeEncryptionDemo.EncryptedEnvelope e = bundle.envelope;
|
||||
// 修改密文末尾一个 Base64 字符,验证 AES-GCM 完整性校验能够阻止解密。
|
||||
String tampered = e.encryptedMnemonic.substring(0, e.encryptedMnemonic.length() - 1)
|
||||
+ (e.encryptedMnemonic.endsWith("A") ? "B" : "A");
|
||||
WalletEnvelopeEncryptionDemo.EncryptedEnvelope corrupted =
|
||||
new WalletEnvelopeEncryptionDemo.EncryptedEnvelope(
|
||||
e.version, e.encryptionAlgorithm, e.passwordKdf, e.iterations,
|
||||
tampered, e.mnemonicNonce, e.encryptedDek, e.dekNonce,
|
||||
e.keySalt, e.passwordSalt, e.passwordHash);
|
||||
try {
|
||||
WalletEnvelopeEncryptionDemo.unlock(corrupted, password);
|
||||
Assert.fail("篡改后的 AES-GCM 密文不应解密成功");
|
||||
} catch (Exception expected) {
|
||||
// AEADBadTagException 或等价的解密完整性异常均属于预期结果。
|
||||
}
|
||||
} finally {
|
||||
Arrays.fill(password, '\0');
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mainWalletTransferShouldUseIndexZeroAndNeverExposePrivateKey() throws Exception {
|
||||
/*
|
||||
* 这是可选的 Nile 集成测试。未配置环境变量时自动跳过,避免普通 test
|
||||
* 执行过程连接节点或消耗真实资产。严禁使用主网私钥运行本测试。
|
||||
*/
|
||||
String privateKey = System.getenv("TRON_TEST_PRIVATE_KEY");
|
||||
String targetAddress = System.getenv("TRON_TEST_TO_ADDRESS");
|
||||
String amountText = System.getenv("TRON_TEST_TRX_AMOUNT");
|
||||
Assume.assumeTrue("未配置 TRON_TEST_*,跳过链上转账测试",
|
||||
privateKey != null && targetAddress != null && amountText != null);
|
||||
|
||||
ECKey key = ECKey.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());
|
||||
try {
|
||||
// Trident 使用已加载私钥构造交易并签名广播;私钥不会进入断言或日志。
|
||||
Response.TransactionExtention extension = api.transfer(fromAddress, targetAddress.trim(), sunAmount);
|
||||
Chain.Transaction signed = api.signTransaction(extension);
|
||||
String txId = api.broadcastTransaction(signed);
|
||||
Assert.assertNotNull("节点应返回交易哈希", txId);
|
||||
Assert.assertFalse("交易哈希不能为空", txId.trim().isEmpty());
|
||||
} finally {
|
||||
api.close();
|
||||
}
|
||||
return;
|
||||
/*
|
||||
char[] password = "correct-password-123".toCharArray();
|
||||
try {
|
||||
// 创建主钱包及一个子钱包;主钱包契约固定为 index=0。
|
||||
WalletEnvelopeEncryptionDemo.DemoBundle bundle =
|
||||
WalletEnvelopeEncryptionDemo.create(password, 1);
|
||||
char[] unlocked = WalletEnvelopeEncryptionDemo.unlock(bundle.envelope, password);
|
||||
try {
|
||||
List<WalletEnvelopeEncryptionDemo.DerivedWallet> wallets =
|
||||
WalletEnvelopeEncryptionDemo.deriveWallets(new String(unlocked), 2);
|
||||
WalletEnvelopeEncryptionDemo.DerivedWallet mainWallet = wallets.get(0);
|
||||
|
||||
Assert.assertEquals(0, mainWallet.index);
|
||||
Assert.assertEquals("m/44'/195'/0'/0/0", mainWallet.path);
|
||||
Assert.assertNotNull(mainWallet.address);
|
||||
Assert.assertFalse(mainWallet.address.trim().isEmpty());
|
||||
|
||||
// 管理端转账只提交目标地址和金额;私钥不进入请求对象。
|
||||
String targetAddress = wallets.get(1).address;
|
||||
BigDecimal amount = new BigDecimal("1.250000");
|
||||
String transactionId = new FakeTransferBroadcaster()
|
||||
.broadcast(mainWallet.address, targetAddress, amount);
|
||||
|
||||
Assert.assertTrue(transactionId.startsWith("test-"));
|
||||
Assert.assertEquals(69, transactionId.length());
|
||||
// 证明测试流程使用的是主钱包地址,而不是子钱包地址作为来源。
|
||||
Assert.assertNotEquals(wallets.get(1).address, mainWallet.address);
|
||||
} finally {
|
||||
// 解锁出的助记词使用完成后立即清零。
|
||||
Arrays.fill(unlocked, '\0');
|
||||
}
|
||||
} finally {
|
||||
Arrays.fill(password, '\0');
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Test
|
||||
public void transferShouldRejectZeroOrNegativeAmount() throws Exception {
|
||||
FakeTransferBroadcaster broadcaster = new FakeTransferBroadcaster();
|
||||
try {
|
||||
broadcaster.broadcast("TSource", "TTarget", BigDecimal.ZERO);
|
||||
Assert.fail("零金额必须拒绝");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
// 预期路径。
|
||||
}
|
||||
try {
|
||||
broadcaster.broadcast("TSource", "TTarget", new BigDecimal("-1"));
|
||||
Assert.fail("负金额必须拒绝");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
// 预期路径。
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user