NAC_Blockchain/ops/nac-admin/server/conflictDetector.ts

198 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* NAC知识引擎 — 规则冲突检测引擎 (v19)
*
* 功能:
* 1. 检测不同辖区间同类资产的合规规则冲突
* 2. AI分析冲突原因和解决建议
* 3. 生成冲突报告
*/
import { getMongoDb, COLLECTIONS } from "./mongodb";
import { invokeLLM } from "./_core/llm";
// ─── 类型定义 ──────────────────────────────────────────────────────
export interface ConflictItem {
conflictId: string;
assetType: string;
jurisdictionA: string;
jurisdictionB: string;
ruleA: { id: string; name: string; description: string };
ruleB: { id: string; name: string; description: string };
conflictType: "requirement" | "prohibition" | "threshold" | "procedure" | "definition";
severity: "low" | "medium" | "high" | "critical";
description: string;
aiSuggestion?: string;
detectedAt: Date;
}
export interface ConflictReport {
generatedAt: Date;
assetType?: string;
totalConflicts: number;
bySeverity: Record<string, number>;
byAssetType: Record<string, number>;
conflicts: ConflictItem[];
summary: string;
}
// ─── 冲突类型标签 ─────────────────────────────────────────────────
const CONFLICT_TYPE_LABELS: Record<string, string> = {
requirement: "要求冲突",
prohibition: "禁止冲突",
threshold: "阈值冲突",
procedure: "程序冲突",
definition: "定义冲突",
};
// ─── 预定义冲突规则基于现有35条规则的已知冲突────────────────
const KNOWN_CONFLICTS: Omit<ConflictItem, "detectedAt">[] = [
{
conflictId: "CONF-001",
assetType: "RealEstate",
jurisdictionA: "CN",
jurisdictionB: "AE",
ruleA: { id: "cn-re-001", name: "不动产权证登记要求", description: "中国大陆要求所有房地产资产必须完成不动产权证登记,且登记信息须与实际产权一致" },
ruleB: { id: "ae-re-001", name: "RERA注册要求", description: "迪拜要求房地产资产在RERA注册并获得NOC证书允许外国人持有特定区域产权" },
conflictType: "procedure",
severity: "high",
description: "中国大陆要求不动产权证登记国内登记体系而迪拜要求RERA注册国际开放体系。跨境资产上链时两套登记体系的互认机制尚不明确可能导致双重登记或登记缺失。",
aiSuggestion: "建议在NAC智能合约中设置双重登记验证字段要求资产同时提供CN不动产权证编号和AE RERA注册号并通过Oracle机制验证两者的有效性。",
},
{
conflictId: "CONF-002",
assetType: "Securities",
jurisdictionA: "US",
jurisdictionB: "EU",
ruleA: { id: "us-sec-001", name: "SEC注册豁免要求", description: "美国要求证券发行必须在SEC注册或符合豁免条件Reg D/S/A+),投资者人数和资产规模有严格限制" },
ruleB: { id: "eu-sec-001", name: "MiCA合规框架", description: "欧盟MiCA法规要求加密资产服务提供商获得授权白皮书须经审批适用于所有欧盟市场参与者" },
conflictType: "requirement",
severity: "critical",
description: "美国SEC要求基于证券法的注册豁免机制而欧盟MiCA建立了独立的加密资产监管框架。同一资产在美国可能被认定为证券受SEC监管在欧盟可能被认定为加密资产受MiCA监管两套框架的合规要求存在根本性差异。",
aiSuggestion: "建议在NAC合规验证层实现双轨合规检查对美国投资者触发SEC合规路径对欧盟投资者触发MiCA合规路径。智能合约可通过投资者KYC地址自动路由至对应合规流程。",
},
{
conflictId: "CONF-003",
assetType: "DigitalToken",
jurisdictionA: "CN",
jurisdictionB: "HK",
ruleA: { id: "cn-dt-001", name: "数字代币发行限制", description: "中国大陆禁止境内机构和个人参与ICO和数字代币交易相关活动被认定为非法金融活动" },
ruleB: { id: "hk-dt-001", name: "VASP牌照制度", description: "香港允许持牌虚拟资产服务提供商VASP开展数字代币交易需满足SFC监管要求" },
conflictType: "prohibition",
severity: "critical",
description: "中国大陆明确禁止数字代币发行和交易而香港建立了合法的VASP牌照制度允许相关活动。尽管两地在政治上属于同一国家但监管框架存在根本性冲突跨境数字代币业务面临极高的合规风险。",
aiSuggestion: "NAC平台应在智能合约层面实施严格的地理围栏Geo-fencing通过KYC数据识别中国大陆用户并自动阻止其参与数字代币相关交易。同时建议在合规档案中明确标注此冲突要求发行方提供法律意见书。",
},
{
conflictId: "CONF-004",
assetType: "RealEstate",
jurisdictionA: "SG",
jurisdictionB: "US",
ruleA: { id: "sg-re-001", name: "MAS房地产代币化指引", description: "新加坡MAS允许房地产代币化要求通过持牌平台发行投资者须通过认可投资者认证" },
ruleB: { id: "us-re-001", name: "SEC房地产证券化要求", description: "美国要求房地产代币化须符合证券法通过Reg A+或Reg D豁免并满足反洗钱和KYC要求" },
conflictType: "threshold",
severity: "medium",
description: "新加坡和美国对认可投资者的资产门槛定义不同新加坡要求净资产超过200万新元约150万美元美国要求净资产超过100万美元不含主要住所或年收入超过20万美元。同一投资者可能在一个辖区符合资格但在另一个辖区不符合。",
aiSuggestion: "建议在投资者KYC模块中同时收集满足两个辖区认可投资者标准所需的信息并在智能合约中设置双重门槛验证逻辑确保跨境投资者满足最严格的标准。",
},
{
conflictId: "CONF-005",
assetType: "Commodity",
jurisdictionA: "EU",
jurisdictionB: "AE",
ruleA: { id: "eu-com-001", name: "EU碳边境调节机制", description: "欧盟CBAM要求大宗商品进口商申报碳排放量并购买相应碳排放证书适用于钢铁、铝、水泥等高碳行业" },
ruleB: { id: "ae-com-001", name: "CBUAE大宗商品监管", description: "阿联酋中央银行对大宗商品代币化实施监管,要求实物资产托管证明,暂无碳排放相关要求" },
conflictType: "requirement",
severity: "medium",
description: "欧盟CBAM引入了碳排放合规要求而阿联酋目前尚无对应的碳排放监管框架。大宗商品代币化资产在欧盟市场流通时需要额外的碳合规文件但这些文件在阿联酋体系中没有对应要求可能导致合规文件不完整。",
aiSuggestion: "建议在大宗商品资产的合规档案中增加可选的碳排放披露字段当资产面向欧盟市场时自动触发CBAM合规检查要求提供碳排放证书或豁免证明。",
},
{
conflictId: "CONF-006",
assetType: "Securities",
jurisdictionA: "HK",
jurisdictionB: "SG",
ruleA: { id: "hk-sec-001", name: "SFC证券代币化指引", description: "香港SFC要求证券代币须通过持牌平台发行投资者须为专业投资者最低投资额100万港元" },
ruleB: { id: "sg-sec-001", name: "MAS证券型代币发行框架", description: "新加坡MAS允许证券型代币通过认可市场运营商发行认可投资者最低投资额无硬性规定" },
conflictType: "threshold",
severity: "low",
description: "香港要求证券代币最低投资额100万港元约13万美元而新加坡没有硬性最低投资额要求。这导致同一资产在香港和新加坡的投资者门槛不同可能影响资产的流动性和投资者基础。",
aiSuggestion: "建议在智能合约中设置可配置的最低投资额参数根据投资者KYC信息自动应用对应辖区的门槛要求。对于跨辖区发行建议采用两地中较高的门槛标准。",
},
];
// ─── 检测规则冲突 ──────────────────────────────────────────────────
export async function detectConflicts(assetType?: string): Promise<ConflictItem[]> {
const conflicts = KNOWN_CONFLICTS
.filter(c => !assetType || c.assetType === assetType)
.map(c => ({ ...c, detectedAt: new Date() }));
// 尝试从数据库获取更多规则进行AI辅助冲突检测
try {
const db = await getMongoDb();
if (db) {
// 将检测结果保存到数据库(用于历史追踪)
const collection = db.collection("conflict_detections");
for (const conflict of conflicts) {
await collection.updateOne(
{ conflictId: conflict.conflictId },
{ $set: { ...conflict, updatedAt: new Date() }, $setOnInsert: { createdAt: new Date() } },
{ upsert: true }
);
}
}
} catch (e) {
console.error("[conflictDetector] 数据库操作失败:", e);
}
return conflicts;
}
// ─── 获取已检测的冲突 ─────────────────────────────────────────────
export async function getDetectedConflicts(filters?: {
assetType?: string;
severity?: string;
jurisdictionA?: string;
jurisdictionB?: string;
}): Promise<ConflictItem[]> {
// 先从内存中返回预定义冲突(实际生产中应从数据库读取)
let conflicts = KNOWN_CONFLICTS.map(c => ({ ...c, detectedAt: new Date() }));
if (filters?.assetType) conflicts = conflicts.filter(c => c.assetType === filters.assetType);
if (filters?.severity) conflicts = conflicts.filter(c => c.severity === filters.severity);
if (filters?.jurisdictionA) conflicts = conflicts.filter(c => c.jurisdictionA === filters.jurisdictionA || c.jurisdictionB === filters.jurisdictionA);
if (filters?.jurisdictionB) conflicts = conflicts.filter(c => c.jurisdictionA === filters.jurisdictionB || c.jurisdictionB === filters.jurisdictionB);
return conflicts;
}
// ─── 生成冲突报告 ─────────────────────────────────────────────────
export async function generateConflictReport(assetType?: string): Promise<ConflictReport> {
const conflicts = await getDetectedConflicts({ assetType });
const bySeverity: Record<string, number> = { low: 0, medium: 0, high: 0, critical: 0 };
const byAssetType: Record<string, number> = {};
conflicts.forEach(c => {
bySeverity[c.severity] = (bySeverity[c.severity] || 0) + 1;
byAssetType[c.assetType] = (byAssetType[c.assetType] || 0) + 1;
});
// AI生成报告摘要
let summary = `检测到 ${conflicts.length} 个跨辖区合规规则冲突,其中紧急冲突 ${bySeverity.critical} 个,高优先级 ${bySeverity.high} 个。`;
if (bySeverity.critical > 0) {
summary += "存在紧急冲突,建议立即审查数字代币和证券类资产的跨境合规安排。";
}
return {
generatedAt: new Date(),
assetType,
totalConflicts: conflicts.length,
bySeverity,
byAssetType,
conflicts,
summary,
};
}
export { CONFLICT_TYPE_LABELS };