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
@@ -38,6 +38,80 @@ describe('TianheProvider', () => {
expect(result.raw.digest).toBe(response.data.digest);
expect(result.raw.transactionStatus).toBe('submitted');
expect(result.confirmationRequired).toBe(true);
// upload 不返回区块信息,由 confirm 阶段补齐
expect(result.blockHash).toBeNull();
expect(result.blockHeight).toBeNull();
});
it('upload 的 userDto 分段必须为 application/json 类型', async () => {
const fetchMock = jest.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
text: async () =>
JSON.stringify({ code: 0, data: { digest: 'd', url: 'u', transactionHash: '0xtx' } }),
} as Response);
await provider.attest({ dataHash: '0xhash' });
const body = fetchMock.mock.calls[0][1]?.body as FormData;
const userDto = body.get('userDto');
expect(userDto).toBeInstanceOf(Blob);
expect((userDto as Blob).type).toBe('application/json');
});
it('响应缺少 transactionHash 时抛错,不用本地 dataHash 冒充', async () => {
jest.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
text: async () => JSON.stringify({ code: 0, message: 'success', data: { digest: 'only-digest' } }),
} as Response);
await expect(provider.attest({ dataHash: '0xlocal' })).rejects.toThrow('transactionHash');
});
it('响应缺少 code 字段时视为异常而非成功', async () => {
jest.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
text: async () => JSON.stringify({ something: 'else' }),
} as Response);
await expect(provider.attest({ dataHash: '0xlocal' })).rejects.toThrow('code=缺失');
});
it('code 非 0 时抛出接口错误信息', async () => {
jest.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
text: async () => JSON.stringify({ code: 500, message: 'appKey invalid' }),
} as Response);
await expect(provider.attest({ dataHash: '0xlocal' })).rejects.toThrow('appKey invalid');
});
it('registerUser 按接口 1 解析托管钱包地址', async () => {
jest.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
text: async () =>
JSON.stringify({ code: 0, data: { pubKey: { address: '0xwallet', publicKey: 'ignored' } } }),
} as Response);
const result = await provider.registerUser();
expect(result.address).toBe('0xwallet');
});
it('ensureRegistered 优先查询接口 8,失败则回退注册', async () => {
const fetchMock = jest
.spyOn(globalThis, 'fetch')
.mockResolvedValueOnce({
ok: true,
text: async () => JSON.stringify({ code: 1, message: 'user not found' }),
} as Response)
.mockResolvedValueOnce({
ok: true,
text: async () => JSON.stringify({ code: 0, data: { pubKey: { address: '0xnewwallet' } } }),
} as Response);
const result = await provider.ensureRegistered();
expect(result.address).toBe('0xnewwallet');
expect(String(fetchMock.mock.calls[0][0])).toContain('/api/v2/user/info');
expect(String(fetchMock.mock.calls[1][0])).toContain('/api/v2/user');
});
it('按交易状态接口确认链上区块信息', async () => {