fix: 天河链对接修复 T1-T3/T6-T8(托管钱包注册与身份生命周期)

- TianheProvider 新增 registerUser/userInfo/ensureRegistered(接口1/8),attest 前幂等确保钱包已注册
- userDto 分段改为 application/json 类型 Blob,避免 Media Type Error
- 严格校验 code===0 且必须返回 transactionHash,杜绝以本地 dataHash 冒充链上哈希的假上链
- upload 响应不再提取恒空的区块信息,注释固化"每物一条 proof、独立 txHash"不变量
- userKey 改为一次性随机生成并明文持久化(不再从 appKey 派生),明文存储保证备份跨重装可恢复
- 新增 /settings/tianhe/register|verify|identity 接口与设置页链上身份区(注册/显示地址/揭示复制/手动录入校验)
- 单测扩至 27 个:新增注册、严格校验、userDto 类型、ensureRegistered 回退用例
This commit is contained in:
Nixevol
2026-07-09 19:05:32 +08:00
parent 8a7032070a
commit fa4215bd52
9 changed files with 512 additions and 104 deletions
+30 -2
View File
@@ -1,14 +1,18 @@
import { Body, Controller, Get, Put } from '@nestjs/common';
import { Body, Controller, Get, Post, Put } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RoleType } from '@artveris/shared';
import { SettingsService } from './settings.service';
import { BlockchainService } from '../blockchain/blockchain.service';
import { Roles } from '../common/decorators/roles.decorator';
import { Public } from '../common/decorators/public.decorator';
@ApiTags('系统设置')
@Controller('settings')
export class SettingsController {
constructor(private readonly settingsService: SettingsService) {}
constructor(
private readonly settingsService: SettingsService,
private readonly blockchain: BlockchainService,
) {}
@ApiBearerAuth()
@Get()
@@ -32,4 +36,28 @@ export class SettingsController {
update(@Body() body: Record<string, string>) {
return this.settingsService.update(body);
}
@ApiBearerAuth()
@Roles(RoleType.SUPER_ADMIN)
@Post('tianhe/register')
@ApiOperation({ summary: '注册(或确认)天河链平台链上身份,返回明文 userId/userKey 供离线保存' })
registerTianheIdentity() {
return this.blockchain.registerTianheIdentity();
}
@ApiBearerAuth()
@Roles(RoleType.SUPER_ADMIN)
@Post('tianhe/verify')
@ApiOperation({ summary: '手动录入已有 userId/userKey 并校验(重装/迁移时复用既有钱包)' })
verifyTianheIdentity(@Body() body: { userId: string; userKey: string }) {
return this.blockchain.verifyTianheIdentity(body.userId, body.userKey);
}
@ApiBearerAuth()
@Roles(RoleType.SUPER_ADMIN)
@Get('tianhe/identity')
@ApiOperation({ summary: '揭示天河链链上身份明文(userId/userKey/钱包地址),供管理员离线备份' })
revealTianheIdentity() {
return this.blockchain.revealTianheUserKey();
}
}
+19 -7
View File
@@ -37,7 +37,13 @@ const EDITABLE_KEYS = [
'tianheUserKey',
];
const SECRET_KEYS = new Set(['ossAccessKeySecret', 'tianheAppKey', 'tianheUserKey']);
// 加密存储的密钥(enc:v1:)。注意:tianheUserKey 有意不加密——
// 它是全系统唯一无外部恢复途径的钱包密码,明文存库才能让备份跨重装恢复
// (加密依赖 .env 的 SETTING_ENCRYPTION_KEY,重装换密钥后密文将无法解密);
// 且单有 userKey 无 appKey(仍加密)无法操作钱包。
const SECRET_KEYS = new Set(['ossAccessKeySecret', 'tianheAppKey']);
// 展示层掩码的密钥(getAll 输出打码;明文经专用揭示接口获取)
const MASKED_KEYS = new Set(['ossAccessKeySecret', 'tianheAppKey', 'tianheUserKey']);
const ENCRYPTED_PREFIX = 'enc:v1:';
@Injectable()
@@ -91,14 +97,15 @@ export class SettingsService {
tianheUserKey: this.config.get<string>('blockchain.tianhe.userKey') || '',
};
for (const r of rows) {
const value = SECRET_KEYS.has(r.key) ? await this.decryptSecret(r.value) : r.value;
// 带 enc:v1: 前缀的值统一解密(兼容历史上曾加密存储、现已改为明文的键,如 tianheUserKey)
const value = await this.decryptSecret(r.value);
map[r.key] = r.key === 'appName' && value === '艺术品可信溯源与认证平台' ? '艺术品溯源平台' : value;
}
if (map.appIconUrl === '/app-icon.svg') map.appIconUrl = '';
if (maskSecrets) {
if (map.ossAccessKeySecret) map.ossAccessKeySecret = this.maskSecret(map.ossAccessKeySecret);
if (map.tianheAppKey) map.tianheAppKey = this.maskSecret(map.tianheAppKey);
if (map.tianheUserKey) map.tianheUserKey = this.maskSecret(map.tianheUserKey);
for (const key of MASKED_KEYS) {
if (map[key]) map[key] = this.maskSecret(map[key]);
}
delete map.settingsEncryptionKey;
}
return map;
@@ -107,7 +114,7 @@ export class SettingsService {
async get(key: string): Promise<string | null> {
const row = await this.prisma.systemSetting.findUnique({ where: { key } });
if (!row) return null;
return SECRET_KEYS.has(key) ? this.decryptSecret(row.value) : row.value;
return this.decryptSecret(row.value);
}
async getClientBaseUrl(): Promise<string> {
@@ -127,7 +134,7 @@ export class SettingsService {
async update(values: Record<string, string>): Promise<Record<string, string>> {
for (const [key, value] of Object.entries(values)) {
if (!EDITABLE_KEYS.includes(key)) continue;
if (SECRET_KEYS.has(key) && this.isMaskedSecret(value)) continue;
if (MASKED_KEYS.has(key) && this.isMaskedSecret(value)) continue;
const nextValue = SECRET_KEYS.has(key) ? await this.encryptSecret(String(value)) : String(value);
await this.prisma.systemSetting.upsert({
where: { key },
@@ -138,6 +145,11 @@ export class SettingsService {
return this.getAll();
}
// 服务内部直写(不做白名单/加密处理),用于系统自身生成的运行时值(如天河链钱包身份)
async setRaw(key: string, value: string): Promise<void> {
await this.prisma.systemSetting.upsert({ where: { key }, update: { value }, create: { key, value } });
}
private async encryptionKey(): Promise<Buffer> {
const row = await this.prisma.systemSetting.findUnique({ where: { key: 'settingsEncryptionKey' } }).catch(() => null);
const secret =