全局配置逻辑实现

This commit is contained in:
lu7213 committed 2026-10-08 04:36:53 -04:00
1 parent da4af93a1e
commit cf8220edbd
13 files changed
+361 -35

No files matched your search

@@ -6,6 +6,10 @@ import com.ruoyi.system.domain.UsdtConfigPlus;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ruoyi.common.select.SelectItem;
import com.ruoyi.system.domain.dto.UsdtConfigCreateRequest;
import com.ruoyi.system.domain.dto.UsdtConfigRotateRequest;
import com.ruoyi.system.domain.vo.UsdtConfigCreateResult;
import com.ruoyi.system.domain.vo.UsdtConfigRotateResult;
/**
* 全局配置Service接口
*
@@ -15,6 +19,12 @@ import com.ruoyi.common.select.SelectItem;
public interface IUsdtConfigService extends IService<UsdtConfigPlus>
{
/** 首次创建全局钱包配置并返回仅出现一次的助记词。 */
UsdtConfigCreateResult createInitialConfig(UsdtConfigCreateRequest request);
/** 轮换配置密码,不改变助记词、派生地址及历史订单映射。 */
UsdtConfigRotateResult rotateConfigurationPassword(Long id, UsdtConfigRotateRequest request);
/**
* 查询全局配置
*
@@ -18,6 +18,16 @@ import com.ruoyi.system.service.IUsdtConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import com.ruoyi.common.select.SelectItem;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.demo.WalletEnvelopeEncryptionDemo;
import com.ruoyi.system.domain.dto.UsdtConfigCreateRequest;
import com.ruoyi.system.domain.dto.UsdtConfigRotateRequest;
import com.ruoyi.system.domain.vo.UsdtConfigCreateResult;
import com.ruoyi.system.domain.vo.UsdtConfigRotateResult;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.Date;
/**
* 全局配置Service业务层处理
*
@@ -32,6 +42,145 @@ public class UsdtConfigServiceImpl extends ServiceImpl<UsdtConfigMapper, UsdtCon
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 首次创建全局配置。
*
* 事务中的 FOR UPDATE 用于串行化首次创建;数据库只保存密文信封和主钱包地址。
* 助记词仅随本次成功响应返回,服务端不记录、不打印,也不另行持久化。
*/
@Override
@Transactional(rollbackFor = Exception.class)
public UsdtConfigCreateResult createInitialConfig(UsdtConfigCreateRequest request) {
validateCreateRequest(request);
if (usdtConfigMapper.selectFirstForUpdate() != null) {
throw new CustomException("全局配置已创建,不能重复初始化");
}
char[] password = request.getConfigurationPassword().toCharArray();
try {
WalletEnvelopeEncryptionDemo.DemoBundle bundle =
WalletEnvelopeEncryptionDemo.create(password, 0);
WalletEnvelopeEncryptionDemo.EncryptedEnvelope envelope = bundle.envelope;
UsdtConfig config = new UsdtConfig();
config.setPayUsdtNum(request.getPayUsdtNum());
config.setReceiveUsdtNum(request.getReceiveUsdtNum());
config.setTronNetType(request.getTronNetType());
config.setTrxNum(request.getTrxNum());
config.setTokenAddress(bundle.wallets.get(0).address);
applyEnvelope(config, envelope);
if (usdtConfigMapper.insertUsdtConfig(config) != 1) {
throw new CustomException("全局配置创建失败");
}
return new UsdtConfigCreateResult(config.getId(), config.getVersion(),
config.getTokenAddress(), bundle.mnemonic);
} catch (CustomException e) {
throw e;
} catch (Exception e) {
throw new CustomException("全局钱包加密配置创建失败", e);
} finally {
Arrays.fill(password, '\0');
}
}
/**
* 轮换配置密码时只重新包装 DEK,encrypted_mnemonic、mnemonic_nonce 和主地址保持不变。
* 最终更新带旧 version 条件;若并发请求已先完成轮换,则本次更新失败并整体回滚。
*/
@Override
@Transactional(rollbackFor = Exception.class)
public UsdtConfigRotateResult rotateConfigurationPassword(Long id, UsdtConfigRotateRequest request) {
validateRotateRequest(id, request);
UsdtConfig current = usdtConfigMapper.selectUsdtConfigById(id);
if (current == null) {
throw new CustomException("全局配置不存在");
}
char[] oldPassword = request.getOldPassword().toCharArray();
char[] newPassword = request.getNewPassword().toCharArray();
try {
Long oldVersion = current.getVersion();
WalletEnvelopeEncryptionDemo.EncryptedEnvelope rotated =
WalletEnvelopeEncryptionDemo.rotateConfigurationPassword(
toEnvelope(current), oldPassword, newPassword);
applyEnvelope(current, rotated);
current.setRotatedAt(new Date());
if (usdtConfigMapper.rotateEnvelope(current, oldVersion) != 1) {
throw new CustomException("配置已被其他请求更新,请刷新后重试");
}
return new UsdtConfigRotateResult(current.getId(), current.getVersion(), current.getTokenAddress());
} catch (SecurityException e) {
throw new CustomException("原配置密码错误");
} catch (CustomException e) {
throw e;
} catch (Exception e) {
throw new CustomException("配置密码轮换失败", e);
} finally {
Arrays.fill(oldPassword, '\0');
Arrays.fill(newPassword, '\0');
}
}
private void validateCreateRequest(UsdtConfigCreateRequest request) {
if (request == null) throw new CustomException("请求参数不能为空");
validatePassword(request.getConfigurationPassword(), "配置密码");
if (!request.getConfigurationPassword().equals(request.getConfirmPassword())) {
throw new CustomException("两次配置密码不一致");
}
if (request.getTronNetType() == null
|| (request.getTronNetType().intValue() != 0 && request.getTronNetType().intValue() != 1)) {
throw new CustomException("波场网络类型只能为 0(测试网)或 1(主网)");
}
if (request.getPayUsdtNum() == null || request.getPayUsdtNum().signum() < 0
|| request.getReceiveUsdtNum() == null || request.getReceiveUsdtNum().signum() < 0
|| request.getTrxNum() == null || request.getTrxNum().signum() < 0) {
throw new CustomException("手续费和 TRX 数量不能为空且不能小于 0");
}
}
private void validateRotateRequest(Long id, UsdtConfigRotateRequest request) {
if (id == null) throw new CustomException("配置 ID 不能为空");
if (request == null) throw new CustomException("请求参数不能为空");
validatePassword(request.getOldPassword(), "原配置密码");
validatePassword(request.getNewPassword(), "新配置密码");
if (!request.getNewPassword().equals(request.getConfirmPassword())) {
throw new CustomException("两次新配置密码不一致");
}
if (request.getOldPassword().equals(request.getNewPassword())) {
throw new CustomException("新配置密码不能与原配置密码相同");
}
}
private void validatePassword(String password, String fieldName) {
if (password == null || password.length() < 10) {
throw new CustomException(fieldName + "至少 10 位");
}
}
private WalletEnvelopeEncryptionDemo.EncryptedEnvelope toEnvelope(UsdtConfig config) {
if (config.getVersion() == null || config.getIterations() == null) {
throw new CustomException("钱包加密信封数据不完整");
}
return new WalletEnvelopeEncryptionDemo.EncryptedEnvelope(
config.getVersion().intValue(), config.getEncryptionAlgorithm(), config.getPasswordKdf(),
config.getIterations().intValue(), config.getEncryptedMnemonic(), config.getMnemonicNonce(),
config.getEncryptedDek(), config.getDekNonce(), config.getKeySalt(),
config.getPasswordSalt(), config.getPasswordHash());
}
private void applyEnvelope(UsdtConfig config, WalletEnvelopeEncryptionDemo.EncryptedEnvelope envelope) {
config.setVersion(Long.valueOf(envelope.version));
config.setEncryptionAlgorithm(envelope.encryptionAlgorithm);
config.setPasswordKdf(envelope.passwordKdf);
config.setIterations(Long.valueOf(envelope.iterations));
config.setEncryptedMnemonic(envelope.encryptedMnemonic);
config.setMnemonicNonce(envelope.mnemonicNonce);
config.setEncryptedDek(envelope.encryptedDek);
config.setDekNonce(envelope.dekNonce);
config.setKeySalt(envelope.keySalt);
config.setPasswordSalt(envelope.passwordSalt);
config.setPasswordHash(envelope.passwordHash);
}
@Override
public List<UsdtConfigPlus> getByIds(List<Long> ids, SFunction<UsdtConfigPlus, ?>... columns) {
LambdaQueryWrapper<UsdtConfigPlus> lambdaQueryWrapper = new LambdaQueryWrapper<UsdtConfigPlus>()