全局配置逻辑实现
This commit is contained in:
1 parent
da4af93a1e
commit
cf8220edbd
13 files changed
+358
-32
No files matched your search
@@ -38,4 +38,9 @@ public @interface Log
|
|||||||
* 是否保存请求的参数
|
* 是否保存请求的参数
|
||||||
*/
|
*/
|
||||||
public boolean isSaveRequestData() default true;
|
public boolean isSaveRequestData() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否保存响应结果。返回一次性凭据或其他敏感数据的接口必须关闭。
|
||||||
|
*/
|
||||||
|
public boolean isSaveResponseData() default true;
|
||||||
}
|
}
|
||||||
@@ -92,8 +92,11 @@ public class LogAspect
|
|||||||
// 请求的地址
|
// 请求的地址
|
||||||
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
||||||
operLog.setOperIp(ip);
|
operLog.setOperIp(ip);
|
||||||
// 返回参数
|
// 返回一次性凭据或敏感数据的接口可通过注解显式禁止保存响应体。
|
||||||
|
if (controllerLog.isSaveResponseData())
|
||||||
|
{
|
||||||
operLog.setJsonResult(JSON.toJSONString(jsonResult));
|
operLog.setJsonResult(JSON.toJSONString(jsonResult));
|
||||||
|
}
|
||||||
|
|
||||||
operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
|
operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
|
||||||
if (loginUser != null)
|
if (loginUser != null)
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ import com.ruoyi.system.domain.UsdtConfigPlus;
|
|||||||
import com.ruoyi.system.service.IUsdtConfigService;
|
import com.ruoyi.system.service.IUsdtConfigService;
|
||||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||||
import com.ruoyi.common.core.page.TableDataInfo;
|
import com.ruoyi.common.core.page.TableDataInfo;
|
||||||
|
import com.ruoyi.common.exception.CustomException;
|
||||||
|
import com.ruoyi.system.domain.dto.UsdtConfigCreateRequest;
|
||||||
|
import com.ruoyi.system.domain.dto.UsdtConfigRotateRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局配置Controller
|
* 全局配置Controller
|
||||||
@@ -34,6 +37,32 @@ public class UsdtConfigController extends BaseController
|
|||||||
{
|
{
|
||||||
@Autowired
|
@Autowired
|
||||||
private IUsdtConfigService usdtConfigService;
|
private IUsdtConfigService usdtConfigService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 首次创建全局配置和钱包加密信封。
|
||||||
|
* 助记词只在本接口成功响应中返回一次,必须由管理员立即离线备份。
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('system:usdtConfig:add')")
|
||||||
|
@Log(title = "首次创建全局钱包配置", businessType = BusinessType.INSERT,
|
||||||
|
isSaveRequestData = false, isSaveResponseData = false)
|
||||||
|
@PostMapping("/initialize")
|
||||||
|
public AjaxResult initialize(@RequestBody UsdtConfigCreateRequest request)
|
||||||
|
{
|
||||||
|
return AjaxResult.success(usdtConfigService.createInitialConfig(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 轮换配置密码。该操作不会更换助记词、主钱包地址或子钱包派生关系。
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('system:usdtConfig:edit')")
|
||||||
|
@Log(title = "轮换全局钱包配置密码", businessType = BusinessType.UPDATE,
|
||||||
|
isSaveRequestData = false)
|
||||||
|
@PutMapping("/{id}/rotate-password")
|
||||||
|
public AjaxResult rotatePassword(@PathVariable("id") Long id,
|
||||||
|
@RequestBody UsdtConfigRotateRequest request)
|
||||||
|
{
|
||||||
|
return AjaxResult.success(usdtConfigService.rotateConfigurationPassword(id, request));
|
||||||
|
}
|
||||||
@PreAuthorize("@ss.hasPermi('system:usdtConfig:getByIds')")
|
@PreAuthorize("@ss.hasPermi('system:usdtConfig:getByIds')")
|
||||||
@PostMapping("/getByIds")
|
@PostMapping("/getByIds")
|
||||||
public AjaxResult getByIds(@RequestBody Ids ids)
|
public AjaxResult getByIds(@RequestBody Ids ids)
|
||||||
@@ -80,21 +109,35 @@ public class UsdtConfigController extends BaseController
|
|||||||
* 新增全局配置
|
* 新增全局配置
|
||||||
*/
|
*/
|
||||||
@PreAuthorize("@ss.hasPermi('system:usdtConfig:add')")
|
@PreAuthorize("@ss.hasPermi('system:usdtConfig:add')")
|
||||||
@Log(title = "全局配置", businessType = BusinessType.INSERT)
|
@Log(title = "全局配置", businessType = BusinessType.INSERT, isSaveRequestData = false)
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public AjaxResult add(@RequestBody UsdtConfig usdtConfig)
|
public AjaxResult add(@RequestBody UsdtConfig usdtConfig)
|
||||||
{
|
{
|
||||||
return toAjax(usdtConfigService.insertUsdtConfig(usdtConfig));
|
throw new CustomException("请使用首次创建接口初始化全局配置");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改全局配置
|
* 修改全局配置
|
||||||
*/
|
*/
|
||||||
@PreAuthorize("@ss.hasPermi('system:usdtConfig:edit')")
|
@PreAuthorize("@ss.hasPermi('system:usdtConfig:edit')")
|
||||||
@Log(title = "全局配置", businessType = BusinessType.UPDATE)
|
@Log(title = "全局配置", businessType = BusinessType.UPDATE, isSaveRequestData = false)
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public AjaxResult edit(@RequestBody UsdtConfig usdtConfig)
|
public AjaxResult edit(@RequestBody UsdtConfig usdtConfig)
|
||||||
{
|
{
|
||||||
|
// 普通编辑只允许修改业务参数;钱包信封和主地址必须走专用初始化/轮换流程。
|
||||||
|
usdtConfig.setVersion(null);
|
||||||
|
usdtConfig.setEncryptionAlgorithm(null);
|
||||||
|
usdtConfig.setPasswordKdf(null);
|
||||||
|
usdtConfig.setIterations(null);
|
||||||
|
usdtConfig.setEncryptedMnemonic(null);
|
||||||
|
usdtConfig.setMnemonicNonce(null);
|
||||||
|
usdtConfig.setEncryptedDek(null);
|
||||||
|
usdtConfig.setDekNonce(null);
|
||||||
|
usdtConfig.setKeySalt(null);
|
||||||
|
usdtConfig.setPasswordSalt(null);
|
||||||
|
usdtConfig.setPasswordHash(null);
|
||||||
|
usdtConfig.setRotatedAt(null);
|
||||||
|
usdtConfig.setTokenAddress(null);
|
||||||
return toAjax(usdtConfigService.updateUsdtConfig(usdtConfig));
|
return toAjax(usdtConfigService.updateUsdtConfig(usdtConfig));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.ruoyi.system.domain;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
import com.ruoyi.common.annotation.Excel;
|
import com.ruoyi.common.annotation.Excel;
|
||||||
@@ -50,31 +51,31 @@ public class UsdtConfig extends BaseEntity
|
|||||||
private Long iterations;
|
private Long iterations;
|
||||||
|
|
||||||
/** AES-GCM 加密后的助记词密文,Base64;禁止保存明文助记词 */
|
/** AES-GCM 加密后的助记词密文,Base64;禁止保存明文助记词 */
|
||||||
@Excel(name = "AES-GCM 加密后的助记词密文,Base64;禁止保存明文助记词")
|
@JsonIgnore
|
||||||
private String encryptedMnemonic;
|
private String encryptedMnemonic;
|
||||||
|
|
||||||
/** 助记词 AES-GCM nonce,Base64,通常为 12 字节 */
|
/** 助记词 AES-GCM nonce,Base64,通常为 12 字节 */
|
||||||
@Excel(name = "助记词 AES-GCM nonce,Base64,通常为 12 字节")
|
@JsonIgnore
|
||||||
private String mnemonicNonce;
|
private String mnemonicNonce;
|
||||||
|
|
||||||
/** 由 KEK 包装后的 DEK,Base64;禁止保存明文 DEK */
|
/** 由 KEK 包装后的 DEK,Base64;禁止保存明文 DEK */
|
||||||
@Excel(name = "由 KEK 包装后的 DEK,Base64;禁止保存明文 DEK")
|
@JsonIgnore
|
||||||
private String encryptedDek;
|
private String encryptedDek;
|
||||||
|
|
||||||
/** DEK AES-GCM nonce,Base64,通常为 12 字节 */
|
/** DEK AES-GCM nonce,Base64,通常为 12 字节 */
|
||||||
@Excel(name = "DEK AES-GCM nonce,Base64,通常为 12 字节")
|
@JsonIgnore
|
||||||
private String dekNonce;
|
private String dekNonce;
|
||||||
|
|
||||||
/** 配置密码派生 KEK 使用的 salt,Base64 */
|
/** 配置密码派生 KEK 使用的 salt,Base64 */
|
||||||
@Excel(name = "配置密码派生 KEK 使用的 salt,Base64")
|
@JsonIgnore
|
||||||
private String keySalt;
|
private String keySalt;
|
||||||
|
|
||||||
/** 配置密码校验哈希使用的独立 salt,Base64 */
|
/** 配置密码校验哈希使用的独立 salt,Base64 */
|
||||||
@Excel(name = "配置密码校验哈希使用的独立 salt,Base64")
|
@JsonIgnore
|
||||||
private String passwordSalt;
|
private String passwordSalt;
|
||||||
|
|
||||||
/** PBKDF2 配置密码校验值,Base64;不是可逆密码 */
|
/** PBKDF2 配置密码校验值,Base64;不是可逆密码 */
|
||||||
@Excel(name = "PBKDF2 配置密码校验值,Base64;不是可逆密码")
|
@JsonIgnore
|
||||||
private String passwordHash;
|
private String passwordHash;
|
||||||
|
|
||||||
/** 最近一次配置密码轮换时间 */
|
/** 最近一次配置密码轮换时间 */
|
||||||
@@ -264,13 +265,7 @@ public class UsdtConfig extends BaseEntity
|
|||||||
.append("encryptionAlgorithm", getEncryptionAlgorithm())
|
.append("encryptionAlgorithm", getEncryptionAlgorithm())
|
||||||
.append("passwordKdf", getPasswordKdf())
|
.append("passwordKdf", getPasswordKdf())
|
||||||
.append("iterations", getIterations())
|
.append("iterations", getIterations())
|
||||||
.append("encryptedMnemonic", getEncryptedMnemonic())
|
.append("walletEnvelope", "<redacted>")
|
||||||
.append("mnemonicNonce", getMnemonicNonce())
|
|
||||||
.append("encryptedDek", getEncryptedDek())
|
|
||||||
.append("dekNonce", getDekNonce())
|
|
||||||
.append("keySalt", getKeySalt())
|
|
||||||
.append("passwordSalt", getPasswordSalt())
|
|
||||||
.append("passwordHash", getPasswordHash())
|
|
||||||
.append("rotatedAt", getRotatedAt())
|
.append("rotatedAt", getRotatedAt())
|
||||||
.append("trxNum", getTrxNum())
|
.append("trxNum", getTrxNum())
|
||||||
.append("tokenAddress", getTokenAddress())
|
.append("tokenAddress", getTokenAddress())
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.ruoyi.system.domain;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
import com.ruoyi.common.annotation.Excel;
|
import com.ruoyi.common.annotation.Excel;
|
||||||
@@ -45,25 +46,25 @@ public class UsdtConfigPlus implements Serializable
|
|||||||
@Excel(name = "PBKDF2 迭代次数;轮换时按当前安全策略更新")
|
@Excel(name = "PBKDF2 迭代次数;轮换时按当前安全策略更新")
|
||||||
private Long iterations;
|
private Long iterations;
|
||||||
/** AES-GCM 加密后的助记词密文,Base64;禁止保存明文助记词 */
|
/** AES-GCM 加密后的助记词密文,Base64;禁止保存明文助记词 */
|
||||||
@Excel(name = "AES-GCM 加密后的助记词密文,Base64;禁止保存明文助记词")
|
@JsonIgnore
|
||||||
private String encryptedMnemonic;
|
private String encryptedMnemonic;
|
||||||
/** 助记词 AES-GCM nonce,Base64,通常为 12 字节 */
|
/** 助记词 AES-GCM nonce,Base64,通常为 12 字节 */
|
||||||
@Excel(name = "助记词 AES-GCM nonce,Base64,通常为 12 字节")
|
@JsonIgnore
|
||||||
private String mnemonicNonce;
|
private String mnemonicNonce;
|
||||||
/** 由 KEK 包装后的 DEK,Base64;禁止保存明文 DEK */
|
/** 由 KEK 包装后的 DEK,Base64;禁止保存明文 DEK */
|
||||||
@Excel(name = "由 KEK 包装后的 DEK,Base64;禁止保存明文 DEK")
|
@JsonIgnore
|
||||||
private String encryptedDek;
|
private String encryptedDek;
|
||||||
/** DEK AES-GCM nonce,Base64,通常为 12 字节 */
|
/** DEK AES-GCM nonce,Base64,通常为 12 字节 */
|
||||||
@Excel(name = "DEK AES-GCM nonce,Base64,通常为 12 字节")
|
@JsonIgnore
|
||||||
private String dekNonce;
|
private String dekNonce;
|
||||||
/** 配置密码派生 KEK 使用的 salt,Base64 */
|
/** 配置密码派生 KEK 使用的 salt,Base64 */
|
||||||
@Excel(name = "配置密码派生 KEK 使用的 salt,Base64")
|
@JsonIgnore
|
||||||
private String keySalt;
|
private String keySalt;
|
||||||
/** 配置密码校验哈希使用的独立 salt,Base64 */
|
/** 配置密码校验哈希使用的独立 salt,Base64 */
|
||||||
@Excel(name = "配置密码校验哈希使用的独立 salt,Base64")
|
@JsonIgnore
|
||||||
private String passwordSalt;
|
private String passwordSalt;
|
||||||
/** PBKDF2 配置密码校验值,Base64;不是可逆密码 */
|
/** PBKDF2 配置密码校验值,Base64;不是可逆密码 */
|
||||||
@Excel(name = "PBKDF2 配置密码校验值,Base64;不是可逆密码")
|
@JsonIgnore
|
||||||
private String passwordHash;
|
private String passwordHash;
|
||||||
/** 最近一次配置密码轮换时间 */
|
/** 最近一次配置密码轮换时间 */
|
||||||
@JsonFormat(pattern = "yyyy-MM-dd")
|
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||||
@@ -249,13 +250,7 @@ public class UsdtConfigPlus implements Serializable
|
|||||||
.append("encryptionAlgorithm", getEncryptionAlgorithm())
|
.append("encryptionAlgorithm", getEncryptionAlgorithm())
|
||||||
.append("passwordKdf", getPasswordKdf())
|
.append("passwordKdf", getPasswordKdf())
|
||||||
.append("iterations", getIterations())
|
.append("iterations", getIterations())
|
||||||
.append("encryptedMnemonic", getEncryptedMnemonic())
|
.append("walletEnvelope", "<redacted>")
|
||||||
.append("mnemonicNonce", getMnemonicNonce())
|
|
||||||
.append("encryptedDek", getEncryptedDek())
|
|
||||||
.append("dekNonce", getDekNonce())
|
|
||||||
.append("keySalt", getKeySalt())
|
|
||||||
.append("passwordSalt", getPasswordSalt())
|
|
||||||
.append("passwordHash", getPasswordHash())
|
|
||||||
.append("rotatedAt", getRotatedAt())
|
.append("rotatedAt", getRotatedAt())
|
||||||
.append("trxNum", getTrxNum())
|
.append("trxNum", getTrxNum())
|
||||||
.append("tokenAddress", getTokenAddress())
|
.append("tokenAddress", getTokenAddress())
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.ruoyi.system.domain.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局钱包配置首次创建请求。
|
||||||
|
*
|
||||||
|
* 配置密码仅用于本次请求中派生密钥,不会保存到数据库,也不会写入接口响应。
|
||||||
|
*/
|
||||||
|
public class UsdtConfigCreateRequest {
|
||||||
|
private BigDecimal payUsdtNum;
|
||||||
|
private BigDecimal receiveUsdtNum;
|
||||||
|
private Integer tronNetType;
|
||||||
|
private BigDecimal trxNum;
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
|
||||||
|
private String configurationPassword;
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
|
||||||
|
private String confirmPassword;
|
||||||
|
|
||||||
|
public BigDecimal getPayUsdtNum() { return payUsdtNum; }
|
||||||
|
public void setPayUsdtNum(BigDecimal payUsdtNum) { this.payUsdtNum = payUsdtNum; }
|
||||||
|
public BigDecimal getReceiveUsdtNum() { return receiveUsdtNum; }
|
||||||
|
public void setReceiveUsdtNum(BigDecimal receiveUsdtNum) { this.receiveUsdtNum = receiveUsdtNum; }
|
||||||
|
public Integer getTronNetType() { return tronNetType; }
|
||||||
|
public void setTronNetType(Integer tronNetType) { this.tronNetType = tronNetType; }
|
||||||
|
public BigDecimal getTrxNum() { return trxNum; }
|
||||||
|
public void setTrxNum(BigDecimal trxNum) { this.trxNum = trxNum; }
|
||||||
|
public String getConfigurationPassword() { return configurationPassword; }
|
||||||
|
public void setConfigurationPassword(String configurationPassword) { this.configurationPassword = configurationPassword; }
|
||||||
|
public String getConfirmPassword() { return confirmPassword; }
|
||||||
|
public void setConfirmPassword(String confirmPassword) { this.confirmPassword = confirmPassword; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.ruoyi.system.domain.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
/** 配置密码轮换请求。 */
|
||||||
|
public class UsdtConfigRotateRequest {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
|
||||||
|
private String oldPassword;
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
|
||||||
|
private String newPassword;
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
|
||||||
|
private String confirmPassword;
|
||||||
|
|
||||||
|
public String getOldPassword() { return oldPassword; }
|
||||||
|
public void setOldPassword(String oldPassword) { this.oldPassword = oldPassword; }
|
||||||
|
public String getNewPassword() { return newPassword; }
|
||||||
|
public void setNewPassword(String newPassword) { this.newPassword = newPassword; }
|
||||||
|
public String getConfirmPassword() { return confirmPassword; }
|
||||||
|
public void setConfirmPassword(String confirmPassword) { this.confirmPassword = confirmPassword; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.ruoyi.system.domain.vo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局钱包配置首次创建结果。
|
||||||
|
* mnemonic 只在首次创建成功时返回一次,调用方必须立即离线备份。
|
||||||
|
*/
|
||||||
|
public class UsdtConfigCreateResult {
|
||||||
|
private final Long id;
|
||||||
|
private final Long version;
|
||||||
|
private final String tokenAddress;
|
||||||
|
private final String mnemonic;
|
||||||
|
|
||||||
|
public UsdtConfigCreateResult(Long id, Long version, String tokenAddress, String mnemonic) {
|
||||||
|
this.id = id;
|
||||||
|
this.version = version;
|
||||||
|
this.tokenAddress = tokenAddress;
|
||||||
|
this.mnemonic = mnemonic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() { return id; }
|
||||||
|
public Long getVersion() { return version; }
|
||||||
|
public String getTokenAddress() { return tokenAddress; }
|
||||||
|
public String getMnemonic() { return mnemonic; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.ruoyi.system.domain.vo;
|
||||||
|
|
||||||
|
/** 配置密码轮换结果,不包含密码及任何钱包密钥材料。 */
|
||||||
|
public class UsdtConfigRotateResult {
|
||||||
|
private final Long id;
|
||||||
|
private final Long version;
|
||||||
|
private final String tokenAddress;
|
||||||
|
|
||||||
|
public UsdtConfigRotateResult(Long id, Long version, String tokenAddress) {
|
||||||
|
this.id = id;
|
||||||
|
this.version = version;
|
||||||
|
this.tokenAddress = tokenAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() { return id; }
|
||||||
|
public Long getVersion() { return version; }
|
||||||
|
public String getTokenAddress() { return tokenAddress; }
|
||||||
|
}
|
||||||
@@ -15,6 +15,11 @@ import com.ruoyi.system.domain.UsdtConfigPlus;
|
|||||||
*/
|
*/
|
||||||
public interface UsdtConfigMapper extends BaseMapper<UsdtConfigPlus>
|
public interface UsdtConfigMapper extends BaseMapper<UsdtConfigPlus>
|
||||||
{
|
{
|
||||||
|
/** 锁定全局配置记录;空表时在 MySQL InnoDB 下同时锁定首记录间隙。 */
|
||||||
|
UsdtConfig selectFirstForUpdate();
|
||||||
|
|
||||||
|
/** 使用旧版本作为条件原子更新加密信封,防止并发轮换互相覆盖。 */
|
||||||
|
int rotateEnvelope(@Param("config") UsdtConfig config, @Param("oldVersion") Long oldVersion);
|
||||||
/**
|
/**
|
||||||
* 查询全局配置
|
* 查询全局配置
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ import com.ruoyi.system.domain.UsdtConfigPlus;
|
|||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.ruoyi.common.select.SelectItem;
|
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接口
|
* 全局配置Service接口
|
||||||
*
|
*
|
||||||
@@ -15,6 +19,12 @@ import com.ruoyi.common.select.SelectItem;
|
|||||||
public interface IUsdtConfigService extends IService<UsdtConfigPlus>
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import com.ruoyi.common.select.SelectItem;
|
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业务层处理
|
* 全局配置Service业务层处理
|
||||||
*
|
*
|
||||||
@@ -32,6 +42,145 @@ public class UsdtConfigServiceImpl extends ServiceImpl<UsdtConfigMapper, UsdtCon
|
|||||||
@Autowired
|
@Autowired
|
||||||
private JdbcTemplate jdbcTemplate;
|
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
|
@Override
|
||||||
public List<UsdtConfigPlus> getByIds(List<Long> ids, SFunction<UsdtConfigPlus, ?>... columns) {
|
public List<UsdtConfigPlus> getByIds(List<Long> ids, SFunction<UsdtConfigPlus, ?>... columns) {
|
||||||
LambdaQueryWrapper<UsdtConfigPlus> lambdaQueryWrapper = new LambdaQueryWrapper<UsdtConfigPlus>()
|
LambdaQueryWrapper<UsdtConfigPlus> lambdaQueryWrapper = new LambdaQueryWrapper<UsdtConfigPlus>()
|
||||||
|
|||||||
@@ -57,6 +57,27 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
where id = #{id}
|
where id = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="selectFirstForUpdate" resultMap="UsdtConfigResult">
|
||||||
|
<include refid="selectUsdtConfigVo"/>
|
||||||
|
order by id asc limit 1 for update
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<update id="rotateEnvelope">
|
||||||
|
update usdt_config
|
||||||
|
set version = #{config.version},
|
||||||
|
encryption_algorithm = #{config.encryptionAlgorithm},
|
||||||
|
password_kdf = #{config.passwordKdf},
|
||||||
|
iterations = #{config.iterations},
|
||||||
|
encrypted_dek = #{config.encryptedDek},
|
||||||
|
dek_nonce = #{config.dekNonce},
|
||||||
|
key_salt = #{config.keySalt},
|
||||||
|
password_salt = #{config.passwordSalt},
|
||||||
|
password_hash = #{config.passwordHash},
|
||||||
|
rotated_at = #{config.rotatedAt}
|
||||||
|
where id = #{config.id}
|
||||||
|
and version = #{oldVersion}
|
||||||
|
</update>
|
||||||
|
|
||||||
<insert id="insertUsdtConfig" parameterType="UsdtConfig" useGeneratedKeys="true" keyProperty="id">
|
<insert id="insertUsdtConfig" parameterType="UsdtConfig" useGeneratedKeys="true" keyProperty="id">
|
||||||
insert into usdt_config
|
insert into usdt_config
|
||||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
|||||||
Reference in new issue
Block a user