From 183bc87997b0e7d9697ab87dcbbe2ffe2a3dabcc Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Mon, 12 Oct 2026 22:37:19 -0400 Subject: [PATCH] =?UTF-8?q?=E5=BD=92=E9=9B=86=E4=BB=BB=E5=8A=A1=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/UsdtAggregationTaskPlus.java | 2 +- .../UsdtAggregationWorkflowService.java | 32 +++---- .../system/service/WalletSigningService.java | 89 +++++++++++++++---- 3 files changed, 88 insertions(+), 35 deletions(-) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/UsdtAggregationTaskPlus.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/UsdtAggregationTaskPlus.java index 1e2f1d3..a811502 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/UsdtAggregationTaskPlus.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/UsdtAggregationTaskPlus.java @@ -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 = "归集地址") diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java index 6dd9538..7dd0705 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java @@ -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 items = itemService.list( new LambdaQueryWrapper() .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() .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); 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 4191265..664dd9e 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 @@ -61,6 +61,23 @@ public class WalletSigningService { public T execute(WalletAddressPlus wallet, String configurationPassword, SignedOperation operation) { validate(wallet, configurationPassword, operation); + return executeUnlocked(configurationPassword, session -> session.execute(wallet, operation)); + } + + /** + * 解密一次全局钱包配置,并在同一个短生命周期会话内执行一批签名操作。 + * + *

适用于归集任务等批量场景。回调返回后立即清除密码和助记词字符数组; + * 会话不能保存到字段、线程池或回调之外使用。

+ */ + public T executeUnlocked(String configurationPassword, + UnlockedOperation operation) { + if (StringUtils.isBlank(configurationPassword)) { + throw new CustomException("配置密码不能为空"); + } + if (operation == null) { + throw new CustomException("解锁后的签名操作不能为空"); + } List 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 words = Arrays.asList(mnemonic.trim().toLowerCase().split("\\s+")); + private DeterministicKeyChain buildKeyChain(char[] mnemonic) throws Exception { + List 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 execute(UnlockedWalletSession session) throws Exception; + } + + /** + * 一次配置解锁产生的临时签名会话。 + * 每次执行仅派生当前钱包私钥,操作结束后关闭客户端并清除私钥字节。 + */ + public final class UnlockedWalletSession { + private final DeterministicKeyChain keyChain; + + private UnlockedWalletSession(DeterministicKeyChain keyChain) { + this.keyChain = keyChain; + } + + public T execute(WalletAddressPlus wallet, SignedOperation 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;