feat: 接入天河链延时确认
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { AttestPayload, AttestResult, AttestationProvider } from './blockchain.types';
|
||||
import { AttestPayload, AttestResult, AttestationProvider, ConfirmTransactionResult } from './blockchain.types';
|
||||
|
||||
export interface TianheOptions {
|
||||
baseUrl: string;
|
||||
@@ -125,6 +125,53 @@ export class TianheProvider implements AttestationProvider {
|
||||
transactionStatus: 'submitted',
|
||||
transactionStatusNote: '天河链接口成功仅表示交易已提交;最终链上结果需通过交易查询接口确认。',
|
||||
},
|
||||
confirmationRequired: true,
|
||||
};
|
||||
}
|
||||
|
||||
async confirmTransaction(txHash: string): Promise<ConfirmTransactionResult> {
|
||||
const baseUrl = required(this.opts.baseUrl, 'Base URL').replace(/\/+$/, '');
|
||||
const appId = required(this.opts.appId, 'App ID');
|
||||
const appKey = required(this.opts.appKey, 'App Key');
|
||||
const url = new URL(`${baseUrl}/api/v2/nfr/transaction/status`);
|
||||
url.searchParams.set('appId', appId);
|
||||
url.searchParams.set('appKey', appKey);
|
||||
url.searchParams.set('transactionHash', required(txHash, 'Transaction Hash'));
|
||||
|
||||
const res = await fetch(url);
|
||||
const text = await res.text();
|
||||
let raw: Record<string, unknown> = { text };
|
||||
try {
|
||||
raw = text ? JSON.parse(text) : {};
|
||||
} catch {
|
||||
raw = { text };
|
||||
}
|
||||
if (!res.ok) {
|
||||
throw new Error(`天河链交易确认请求失败:HTTP ${res.status} ${text.slice(0, 200)}`);
|
||||
}
|
||||
|
||||
const code = pickString(raw, ['code', 'statusCode', 'errCode']);
|
||||
const success = code === null || code === '0' || code.toLowerCase() === 'success' || code === '200';
|
||||
if (!success) {
|
||||
const msg = pickString(raw, ['message', 'msg', 'error']) || '接口返回失败';
|
||||
throw new Error(`天河链交易确认失败:${msg}`);
|
||||
}
|
||||
|
||||
const data = responseData(raw);
|
||||
const blockHash = pickString(data, ['blockHash']);
|
||||
const blockHeight = pickNumber(data, ['blockNum', 'blockHeight', 'height']);
|
||||
const confirmed = Boolean(blockHash || blockHeight !== null);
|
||||
return {
|
||||
confirmed,
|
||||
txHash: pickString(data, ['transactionHash', 'txHash']) || txHash,
|
||||
blockHash,
|
||||
blockHeight,
|
||||
raw: {
|
||||
response: raw,
|
||||
transactionStatus: confirmed ? 'confirmed' : 'waiting',
|
||||
tokenId: pickString(data, ['tokenId']),
|
||||
status: pickString(data, ['status']),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user