代理租赁实现
This commit is contained in:
1 parent
1c0544afe0
commit
2a9f1c4c7c
38 files changed
+5824
-6
No files matched your search
@@ -0,0 +1,290 @@
|
||||
package com.ruoyi.system.rental;
|
||||
|
||||
import static com.ruoyi.system.rental.RentalValues.*;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.tron.common.crypto.ECKey;
|
||||
import org.tron.trident.crypto.Hash;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
/** 应用、节点、公开钱包和不可变套餐版本的生命周期。绝不接收TRON私钥。 */
|
||||
@Service
|
||||
public class RentalProvisioning {
|
||||
private final RentalStore store;
|
||||
private final RentalOrders orders;
|
||||
|
||||
@Value("${rental.domain:localhost}")
|
||||
private String domain;
|
||||
|
||||
public RentalProvisioning(RentalStore store, RentalOrders orders) {
|
||||
this.store = store;
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public long create(String type, Map<String, Object> request, long actor) {
|
||||
Map<String, Object> row;
|
||||
if ("app".equals(type)) {
|
||||
String url = text(request, "notify_url");
|
||||
if (!url.isEmpty()) {
|
||||
RentalHttp.checkUrl(url);
|
||||
}
|
||||
row =
|
||||
map(
|
||||
"user_id",
|
||||
actor,
|
||||
"name",
|
||||
input(request, "name", 100),
|
||||
"status",
|
||||
"ACTIVE",
|
||||
"notify_url",
|
||||
url.isEmpty() ? null : url);
|
||||
} else if ("node".equals(type)) {
|
||||
row =
|
||||
map(
|
||||
"user_id",
|
||||
actor,
|
||||
"node_no",
|
||||
id("NODE"),
|
||||
"name",
|
||||
input(request, "name", 100),
|
||||
"agent_version",
|
||||
"1.0.0",
|
||||
"status",
|
||||
"PENDING",
|
||||
"created_by",
|
||||
actor);
|
||||
} else if ("wallet".equals(type)) {
|
||||
long nodeId = number(request, "node_id");
|
||||
Map<String, Object> node = store.get("node", nodeId, true);
|
||||
require(
|
||||
number(node, "user_id") == actor && !"REVOKED".equals(text(node, "status")),
|
||||
"无节点权限");
|
||||
String address = input(request, "address", 64);
|
||||
RentalOrders.address(address);
|
||||
row =
|
||||
map(
|
||||
"node_id",
|
||||
nodeId,
|
||||
"network",
|
||||
network(input(request, "network", 16)),
|
||||
"address",
|
||||
address,
|
||||
"status",
|
||||
"PENDING");
|
||||
} else {
|
||||
throw new IllegalArgumentException("未知创建类型");
|
||||
}
|
||||
long id = store.insert(type, row);
|
||||
store.event(type.toUpperCase(Locale.ROOT), id, "CREATE", actor, "租赁业务注册");
|
||||
return id;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public long product(Map<String, Object> request, long actor) {
|
||||
Map<String, Object> row = new LinkedHashMap<>();
|
||||
for (String key :
|
||||
Arrays.asList(
|
||||
"package_code",
|
||||
"revision",
|
||||
"name",
|
||||
"network",
|
||||
"resource_type",
|
||||
"resource_quantity",
|
||||
"redundancy_percent",
|
||||
"duration_seconds",
|
||||
"sale_amount",
|
||||
"supplier_amount",
|
||||
"supplier_fee_percent",
|
||||
"fee_limit_sun",
|
||||
"rules")) {
|
||||
require(request.containsKey(key), "缺少套餐字段:" + key);
|
||||
row.put(key, request.get(key));
|
||||
}
|
||||
input(row, "package_code", 32);
|
||||
input(row, "name", 100);
|
||||
require(number(row, "revision") > 0, "版本必须为正整数");
|
||||
if (!(row.get("rules") instanceof String)) {
|
||||
row.put("rules", orders.encode(row.get("rules")));
|
||||
}
|
||||
row.put("delegate_lock", 0);
|
||||
row.put("status", "DISABLED");
|
||||
RentalOrders.validatePackage(row);
|
||||
Map<String, Object> previous =
|
||||
store.one(
|
||||
"SELECT * FROM rent_package WHERE package_code=? ORDER BY revision DESC"
|
||||
+ " LIMIT 1 FOR UPDATE",
|
||||
row.get("package_code"));
|
||||
if (previous != null) {
|
||||
require(
|
||||
text(previous, "resource_type").equals(text(row, "resource_type"))
|
||||
&& number(row, "revision") > number(previous, "revision"),
|
||||
"同套餐资源类型不可变且版本必须递增");
|
||||
}
|
||||
long id = store.insert("package", row);
|
||||
store.event("PACKAGE", id, "CREATE_VERSION", actor, "新增不可变价格版本");
|
||||
return id;
|
||||
}
|
||||
|
||||
public Map<String, Object> ownedWallet(long id, long actor, boolean lock) {
|
||||
Map<String, Object> wallet = store.get("wallet", id, lock);
|
||||
Map<String, Object> node = store.get("node", number(wallet, "node_id"), false);
|
||||
require(number(node, "user_id") == actor, "无钱包权限");
|
||||
return wallet;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Map<String, Object> challenge(long id, long actor) {
|
||||
Map<String, Object> wallet = ownedWallet(id, actor, true);
|
||||
String nonce = RentalCrypto.random();
|
||||
String message =
|
||||
"TRON RENTAL WALLET OWNERSHIP V1\nDomain: "
|
||||
+ domain
|
||||
+ "\nPurpose: bind rental wallet only; not a transaction"
|
||||
+ "\nUser: "
|
||||
+ actor
|
||||
+ "\nWallet: "
|
||||
+ id
|
||||
+ "\nNetwork: "
|
||||
+ text(wallet, "network")
|
||||
+ "\nAddress: "
|
||||
+ text(wallet, "address")
|
||||
+ "\nNonce: "
|
||||
+ nonce
|
||||
+ "\nIssued: "
|
||||
+ now()
|
||||
+ "Z\nExpires: "
|
||||
+ after(300)
|
||||
+ "Z";
|
||||
store.update(
|
||||
"wallet",
|
||||
id,
|
||||
map(
|
||||
"ownership_method",
|
||||
"TRONLINK_SIGN_MESSAGE_V2",
|
||||
"ownership_challenge",
|
||||
nonce,
|
||||
"ownership_message",
|
||||
message,
|
||||
"challenge_issued_at",
|
||||
now(),
|
||||
"challenge_expires_at",
|
||||
after(300),
|
||||
"challenge_consumed_at",
|
||||
null));
|
||||
return map("message", message, "nonce", nonce);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void prove(long id, String nonce, String signature, long actor) {
|
||||
Map<String, Object> wallet = ownedWallet(id, actor, true);
|
||||
require(
|
||||
nonce.equals(text(wallet, "ownership_challenge"))
|
||||
&& wallet.get("challenge_consumed_at") == null
|
||||
&& wallet.get("challenge_expires_at") != null
|
||||
&& time(wallet.get("challenge_expires_at"))
|
||||
.isAfter(java.time.Instant.now()),
|
||||
"挑战已过期或已消费");
|
||||
require(signature.matches("(0x)?[0-9a-fA-F]{130}"), "TronLink消息签名格式无效");
|
||||
try {
|
||||
byte[] message = text(wallet, "ownership_message").getBytes(StandardCharsets.UTF_8);
|
||||
byte[] prefix =
|
||||
("\u0019TRON Signed Message:\n" + message.length)
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] plain = new byte[prefix.length + message.length];
|
||||
System.arraycopy(prefix, 0, plain, 0, prefix.length);
|
||||
System.arraycopy(message, 0, plain, prefix.length, message.length);
|
||||
byte[] sig =
|
||||
org.bouncycastle.util.encoders.Hex.decode(signature.replaceFirst("^0x", ""));
|
||||
int v = sig[64] & 255;
|
||||
if (v < 27) {
|
||||
v += 27;
|
||||
}
|
||||
byte[] compact = new byte[65];
|
||||
compact[0] = (byte) v;
|
||||
System.arraycopy(sig, 0, compact, 1, 64);
|
||||
byte[] address =
|
||||
ECKey.signatureToAddress(
|
||||
Hash.sha3(plain),
|
||||
java.util.Base64.getEncoder().encodeToString(compact));
|
||||
String recovered = org.tron.trident.utils.Base58Check.bytesToBase58(address);
|
||||
require(text(wallet, "address").equals(recovered), "恢复地址与钱包不符");
|
||||
store.update(
|
||||
"wallet",
|
||||
id,
|
||||
map(
|
||||
"ownership_signature",
|
||||
signature,
|
||||
"ownership_verified_address",
|
||||
recovered,
|
||||
"ownership_verified_at",
|
||||
now(),
|
||||
"challenge_consumed_at",
|
||||
now(),
|
||||
"ownership_evidence",
|
||||
orders.encode(
|
||||
map(
|
||||
"message_hash",
|
||||
RentalCrypto.sha256(text(wallet, "ownership_message")),
|
||||
"recovered",
|
||||
recovered))));
|
||||
} catch (IllegalArgumentException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalArgumentException("TronLink签名验证失败");
|
||||
}
|
||||
store.event("WALLET", id, "OWNERSHIP_VERIFIED", actor, "一次性离线消息验证成功,尚需审核与链上权限检查");
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void status(String type, long id, String status, long actor, String reason) {
|
||||
Map<String, Object> row = store.get(type, id, true);
|
||||
Map<String, Object> changes = map("status", status);
|
||||
if ("node".equals(type)) {
|
||||
require(Arrays.asList("ACTIVE", "PAUSED", "REVOKED").contains(status), "节点状态非法");
|
||||
if ("ACTIVE".equals(status)) {
|
||||
require(number(row, "created_by") != actor, "节点不能自审");
|
||||
changes.put("approved_by", actor);
|
||||
changes.put("approved_at", now());
|
||||
}
|
||||
} else if ("wallet".equals(type)) {
|
||||
require(Arrays.asList("ACTIVE", "PAUSED").contains(status), "钱包状态非法");
|
||||
require(
|
||||
!"ACTIVE".equals(status) || row.get("ownership_verified_at") != null,
|
||||
"钱包尚未完成TronLink验证");
|
||||
} else if ("package".equals(type)) {
|
||||
require(Arrays.asList("ACTIVE", "DISABLED").contains(status), "套餐状态非法");
|
||||
RentalOrders.validatePackage(row);
|
||||
} else if ("app".equals(type) || "account".equals(type)) {
|
||||
require(
|
||||
("app".equals(type)
|
||||
? Arrays.asList("ACTIVE", "DISABLED")
|
||||
: Arrays.asList("ACTIVE", "FROZEN"))
|
||||
.contains(status),
|
||||
"业务状态非法");
|
||||
} else {
|
||||
throw new IllegalArgumentException("该资源不支持直接修改状态");
|
||||
}
|
||||
store.update(type, id, changes);
|
||||
store.event(type.toUpperCase(Locale.ROOT), id, "STATUS", actor, reason);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void policy(long id, Map<String, Object> request, long actor) {
|
||||
ownedWallet(id, actor, true);
|
||||
Map<String, Object> changes = new LinkedHashMap<>();
|
||||
for (String type : Arrays.asList("energy", "bandwidth")) {
|
||||
long enabled = number(request, type + "_enabled"),
|
||||
retain = number(request, type + "_retain_sun");
|
||||
require((enabled == 0 || enabled == 1) && retain >= 0, "钱包出租开关或留存本金无效");
|
||||
changes.put(type + "_enabled", enabled);
|
||||
changes.put(type + "_retain_sun", retain);
|
||||
}
|
||||
store.update("wallet", id, changes);
|
||||
store.event("WALLET", id, "POLICY", actor, "供应商修改平台侧出租约束;不能替代本地广播授权");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user