fix: 校准天河链返回字段映射
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { TianheProvider } from './tianhe.provider';
|
||||
|
||||
describe('TianheProvider', () => {
|
||||
const provider = new TianheProvider({
|
||||
baseUrl: 'https://test.api.tichain.tianhecloud.com',
|
||||
appId: 'app-id',
|
||||
appKey: 'app-key',
|
||||
userId: 'user-id',
|
||||
userKey: 'user-key',
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('按天河链 upload 文档映射交易哈希、存证链接和文件哈希', async () => {
|
||||
const response = {
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: {
|
||||
digest: 'file-digest-001',
|
||||
url: 'https://test.explorer.tianhecloud.com/proof/001',
|
||||
transactionHash: '0x1234567890abcdef',
|
||||
},
|
||||
};
|
||||
jest.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
text: async () => JSON.stringify(response),
|
||||
} as Response);
|
||||
|
||||
const result = await provider.attest({
|
||||
dataHash: '0xlocaldatahash',
|
||||
meta: { traceCode: 'TRACE-001' },
|
||||
});
|
||||
|
||||
expect(result.txHash).toBe(response.data.transactionHash);
|
||||
expect(result.explorerUrl).toBe(response.data.url);
|
||||
expect(result.raw.digest).toBe(response.data.digest);
|
||||
expect(result.raw.transactionStatus).toBe('submitted');
|
||||
});
|
||||
});
|
||||
@@ -95,17 +95,18 @@ export class TianheProvider implements AttestationProvider {
|
||||
const data = responseData(raw);
|
||||
const txHash =
|
||||
pickString(data, [
|
||||
'txHash',
|
||||
'transactionHash',
|
||||
'txHash',
|
||||
'hash',
|
||||
'proofHash',
|
||||
'certHash',
|
||||
'attestationHash',
|
||||
'chainHash',
|
||||
]) || payload.dataHash;
|
||||
const digest = pickString(data, ['digest']);
|
||||
const explorerUrl = pickString(data, ['url', 'link', 'proofUrl', 'viewUrl', 'certUrl', 'explorerUrl', 'uri']);
|
||||
const blockHash = pickString(data, ['blockHash']);
|
||||
const blockHeight = pickNumber(data, ['blockHeight', 'height']);
|
||||
const blockHeight = pickNumber(data, ['blockHeight', 'blockNum', 'height']);
|
||||
const onchainAtText = pickString(data, ['onchainAt', 'chainTime', 'createdAt', 'time']);
|
||||
const onchainAt = onchainAtText ? new Date(onchainAtText) : new Date();
|
||||
|
||||
@@ -117,7 +118,13 @@ export class TianheProvider implements AttestationProvider {
|
||||
chainName: '天河链',
|
||||
explorerUrl,
|
||||
onchainAt: Number.isNaN(onchainAt.getTime()) ? new Date() : onchainAt,
|
||||
raw: { requestMeta: meta, response: raw },
|
||||
raw: {
|
||||
requestMeta: meta,
|
||||
response: raw,
|
||||
digest,
|
||||
transactionStatus: 'submitted',
|
||||
transactionStatusNote: '天河链接口成功仅表示交易已提交;最终链上结果需通过交易查询接口确认。',
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user