归集任务修改

This commit is contained in:
lu7213 committed 2026-10-12 22:37:19 -04:00
1 parent 38d038115a
commit 183bc87997
3 files changed
+88 -35

No files matched your search

@@ -57,7 +57,7 @@ public class UsdtAggregationTaskPlus implements Serializable
@Excel(name = "执行时间", width = 30, dateFormat = "yyyy-MM-dd")
private Date exeTime;
/** 审核状态 */
@Excel(name = "审核状态")
@Excel(name = "审核状态 1 通过 2 拒绝")
private Integer verifyStatus;
/** 归集地址 */
@Excel(name = "归集地址")
@@ -117,22 +117,24 @@ public class UsdtAggregationWorkflowService {
if (Integer.valueOf(1).equals(task.getStatus())) {
throw new CustomException("归集任务正在执行");
}
walletSigningService.validateConfigurationPassword(password);
task.setStatus(1);
task.setExeTime(DateUtils.getNowDate());
task.setUpdatedAt(DateUtils.getNowDate());
taskService.updateById(task);
List<UsdtAggregationTaskItemPlus> items = itemService.list(
new LambdaQueryWrapper<UsdtAggregationTaskItemPlus>()
.eq(UsdtAggregationTaskItemPlus::getParentId, taskId)
.in(UsdtAggregationTaskItemPlus::getStatus, 0L, 2L));
boolean failed = false;
for (UsdtAggregationTaskItemPlus item : items) {
if (!executeItemInternal(item, password)) {
failed = true;
boolean failed = walletSigningService.executeUnlocked(password, session -> {
task.setStatus(1);
task.setExeTime(DateUtils.getNowDate());
task.setUpdatedAt(DateUtils.getNowDate());
taskService.updateById(task);
boolean hasFailure = false;
for (UsdtAggregationTaskItemPlus item : items) {
if (!executeItemInternal(item, session)) {
hasFailure = true;
}
}
}
return hasFailure;
});
finishTask(task, failed);
}
@@ -151,8 +153,7 @@ public class UsdtAggregationWorkflowService {
if (!Long.valueOf(2L).equals(item.getStatus())) {
throw new CustomException("只有失败的归集明细可以手动重试");
}
walletSigningService.validateConfigurationPassword(password);
executeItemInternal(item, password);
walletSigningService.executeUnlocked(password, session -> executeItemInternal(item, session));
boolean remainsFailed = itemService.count(new LambdaQueryWrapper<UsdtAggregationTaskItemPlus>()
.eq(UsdtAggregationTaskItemPlus::getParentId, task.getId())
.eq(UsdtAggregationTaskItemPlus::getStatus, 2L)) > 0;
@@ -186,14 +187,15 @@ public class UsdtAggregationWorkflowService {
return item;
}
private boolean executeItemInternal(UsdtAggregationTaskItemPlus item, String password) {
private boolean executeItemInternal(UsdtAggregationTaskItemPlus item,
WalletSigningService.UnlockedWalletSession session) {
WalletAddressPlus wallet = walletService.getByTokenAndAppId(item.getTokenAddress(), item.getAppId());
if (wallet == null) {
markFailed(item, "钱包不存在或已不属于当前应用");
return false;
}
try {
String tx = walletSigningService.execute(wallet, password, (tron, api, current) ->
String tx = session.execute(wallet, (tron, api, current) ->
tron.usdtTransWithBroadcastRetry(api, current.getToken(),
item.getDistTokenAddress(), item.getOpeAmount(), MAX_ATTEMPTS));
item.setStatus(1L);
@@ -61,6 +61,23 @@ public class WalletSigningService {
public <T> T execute(WalletAddressPlus wallet, String configurationPassword,
SignedOperation<T> operation) {
validate(wallet, configurationPassword, operation);
return executeUnlocked(configurationPassword, session -> session.execute(wallet, operation));
}
/**
* 解密一次全局钱包配置,并在同一个短生命周期会话内执行一批签名操作。
*
* <p>适用于归集任务等批量场景。回调返回后立即清除密码和助记词字符数组;
* 会话不能保存到字段、线程池或回调之外使用。</p>
*/
public <T> T executeUnlocked(String configurationPassword,
UnlockedOperation<T> operation) {
if (StringUtils.isBlank(configurationPassword)) {
throw new CustomException("配置密码不能为空");
}
if (operation == null) {
throw new CustomException("解锁后的签名操作不能为空");
}
List<UsdtConfig> configs = usdtConfigMapper.selectUsdtConfigList(new UsdtConfig());
if (configs == null || configs.isEmpty()) {
throw new CustomException("全局钱包配置不存在");
@@ -68,19 +85,11 @@ public class WalletSigningService {
char[] password = configurationPassword.toCharArray();
char[] mnemonic = null;
byte[] privateKey = null;
ApiWrapper api = null;
try {
UsdtConfig config = configs.get(0);
mnemonic = WalletEnvelopeEncryptionDemo.unlock(toEnvelope(config), password);
DerivedKey derived = derive(new String(mnemonic), parseIndex(wallet.getTokenIndex()));
privateKey = derived.privateKey;
if (!wallet.getToken().equals(derived.address)) {
throw new CustomException("钱包地址与派生索引不匹配,已拒绝签名");
}
ITronService tronService = tronServiceProvider.getCurrentService();
api = tronService.buildApiWrapper(toHex(privateKey));
return operation.execute(tronService, api, wallet);
DeterministicKeyChain keyChain = buildKeyChain(mnemonic);
return operation.execute(new UnlockedWalletSession(keyChain));
} catch (SecurityException e) {
throw new CustomException("配置密码错误");
} catch (CustomException e) {
@@ -89,16 +98,10 @@ public class WalletSigningService {
log.error(e.getMessage(),e);
throw new CustomException("钱包解密或链上签名操作失败", e);
} finally {
if (api != null) {
api.close();
}
Arrays.fill(password, '\0');
if (mnemonic != null) {
Arrays.fill(mnemonic, '\0');
}
if (privateKey != null) {
Arrays.fill(privateKey, (byte) 0);
}
}
}
@@ -137,11 +140,14 @@ public class WalletSigningService {
config.getPasswordSalt(), config.getPasswordHash());
}
private DerivedKey derive(String mnemonic, int index) throws Exception {
List<String> words = Arrays.asList(mnemonic.trim().toLowerCase().split("\\s+"));
private DeterministicKeyChain buildKeyChain(char[] mnemonic) throws Exception {
List<String> words = Arrays.asList(new String(mnemonic).trim().toLowerCase().split("\\s+"));
MnemonicCode.INSTANCE.check(words);
DeterministicSeed seed = DeterministicSeed.ofMnemonic(words, "");
DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed).build();
return DeterministicKeyChain.builder().seed(seed).build();
}
private DerivedKey derive(DeterministicKeyChain chain, int index) {
HDPath base = HDPath.M().extend(new ChildNumber(44, true), new ChildNumber(195, true),
new ChildNumber(0, true), ChildNumber.ZERO);
DeterministicKey key = chain.getKeyByPath(base.extend(new ChildNumber(index, false)), true);
@@ -162,6 +168,51 @@ public class WalletSigningService {
T execute(ITronService tronService, ApiWrapper api, WalletAddressPlus wallet) throws Exception;
}
/** 在一次钱包解锁范围内执行的批量操作。 */
public interface UnlockedOperation<T> {
T execute(UnlockedWalletSession session) throws Exception;
}
/**
* 一次配置解锁产生的临时签名会话。
* 每次执行仅派生当前钱包私钥,操作结束后关闭客户端并清除私钥字节。
*/
public final class UnlockedWalletSession {
private final DeterministicKeyChain keyChain;
private UnlockedWalletSession(DeterministicKeyChain keyChain) {
this.keyChain = keyChain;
}
public <T> T execute(WalletAddressPlus wallet, SignedOperation<T> operation) throws Exception {
if (wallet == null || StringUtils.isBlank(wallet.getToken())) {
throw new CustomException("钱包不存在");
}
if (operation == null) {
throw new CustomException("签名操作不能为空");
}
byte[] privateKey = null;
ApiWrapper api = null;
try {
DerivedKey derived = derive(keyChain, parseIndex(wallet.getTokenIndex()));
privateKey = derived.privateKey;
if (!wallet.getToken().equals(derived.address)) {
throw new CustomException("钱包地址与派生索引不匹配,已拒绝签名");
}
ITronService tronService = tronServiceProvider.getCurrentService();
api = tronService.buildApiWrapper(toHex(privateKey));
return operation.execute(tronService, api, wallet);
} finally {
if (api != null) {
api.close();
}
if (privateKey != null) {
Arrays.fill(privateKey, (byte) 0);
}
}
}
}
private static final class DerivedKey {
private final byte[] privateKey;
private final String address;